Field3D
ProceduralField.h
Go to the documentation of this file.
00001 //----------------------------------------------------------------------------//
00002 
00003 /*
00004  * Copyright (c) 2009 Sony Pictures Imageworks Inc
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * Redistributions of source code must retain the above copyright
00013  * notice, this list of conditions and the following disclaimer.
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the
00017  * distribution.  Neither the name of Sony Pictures Imageworks nor the
00018  * names of its contributors may be used to endorse or promote
00019  * products derived from this software without specific prior written
00020  * permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
00026  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00028  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00031  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00033  * OF THE POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 
00036 //----------------------------------------------------------------------------//
00037 
00044 //----------------------------------------------------------------------------//
00045 
00046 #ifndef _INCLUDED_Field3D_ProceduralField_H_
00047 #define _INCLUDED_Field3D_ProceduralField_H_
00048 
00049 //----------------------------------------------------------------------------//
00050 
00051 #include "Field.h"
00052 
00053 //----------------------------------------------------------------------------//
00054 
00055 #include "ns.h"
00056 
00057 FIELD3D_NAMESPACE_OPEN
00058 
00059 //----------------------------------------------------------------------------//
00060 // Forward declarations 
00061 //----------------------------------------------------------------------------//
00062 
00063 template <class T>
00064 class ProceduralFieldLookup; 
00065 
00066 //----------------------------------------------------------------------------//
00067 // Utility macros
00068 //----------------------------------------------------------------------------//
00069 
00070 #define REGISTER_FIELD_TYPES(FIELDCLASS) \
00071   factory.registerField(FIELDCLASS<half>::create); \
00072   factory.registerField(FIELDCLASS<float>::create); \
00073   factory.registerField(FIELDCLASS<double>::create); \
00074   factory.registerField(FIELDCLASS<V3h>::create); \
00075   factory.registerField(FIELDCLASS<V3f>::create); \
00076   factory.registerField(FIELDCLASS<V3d>::create)
00077 
00078 #define INSTANTIATE_FIELD_TYPES(FIELDCLASS) \
00079   template class FIELDCLASS<half>; \
00080   template class FIELDCLASS<float>; \
00081   template class FIELDCLASS<double>; \
00082   template class FIELDCLASS<V3h>; \
00083   template class FIELDCLASS<V3f>; \
00084   template class FIELDCLASS<V3d>
00085 
00086 //----------------------------------------------------------------------------//
00087 // ProceduralField
00088 //----------------------------------------------------------------------------//
00089 
00117 //----------------------------------------------------------------------------//
00118 
00119 template <class Data_T>
00120 class ProceduralField : public Field<Data_T>
00121 {
00122 
00123 public:
00124 
00125   // Typedefs ------------------------------------------------------------------
00126 
00127   typedef boost::intrusive_ptr<ProceduralField> Ptr;
00128 
00129   typedef ProceduralFieldLookup<Data_T> LinearInterp;
00130   typedef ProceduralFieldLookup<Data_T> CubicInterp;
00131 
00132   // Constructors --------------------------------------------------------------
00133 
00135   virtual ~ProceduralField()
00136   { /* Empty */ }
00137 
00138   // To be implemented by subclasses -------------------------------------------
00139 
00140   virtual Data_T lsSample(const V3d &lsP) const = 0;
00141 
00142   // From Field ----------------------------------------------------------------
00143 
00146   virtual Data_T value(int i, int j, int k) const = 0;
00147 
00148   // Main methods --------------------------------------------------------------
00149 
00153   Data_T typedIntMetadata(const std::string &name, 
00154                                 const Data_T& defaultVal) const;
00158   Data_T typedFloatMetadata(const std::string &name,
00159                                   const Data_T& defaultVal) const;
00160 
00161   // RTTI replacement ----------------------------------------------------------
00162 
00163   typedef ProceduralField<Data_T> class_type;
00164   DEFINE_FIELD_RTTI_ABSTRACT_CLASS
00165 
00166   virtual std::string className() const
00167     { return std::string("ProceduralField"); }
00168 
00169   // Typedefs ------------------------------------------------------------------
00170 
00171   typedef Field<Data_T> base;
00172 
00173 };
00174 
00175 //----------------------------------------------------------------------------//
00176 // Typedefs
00177 //----------------------------------------------------------------------------//
00178 
00179 typedef ProceduralField<half>   ProceduralFieldh;
00180 typedef ProceduralField<float>  ProceduralFieldf;
00181 typedef ProceduralField<double> ProceduralFieldd;
00182 typedef ProceduralField<V3h>    ProceduralField3h;
00183 typedef ProceduralField<V3f>    ProceduralField3f;
00184 typedef ProceduralField<V3d>    ProceduralField3d;
00185 
00186 //----------------------------------------------------------------------------//
00187 // Template specializations
00188 //----------------------------------------------------------------------------//
00189 
00190 template <>
00191 inline half
00192 ProceduralField<half>::typedIntMetadata(const std::string &name,
00193                                         const half& defaultVal) const
00194 {
00195   return metadata().intMetadata(name, static_cast<int>(defaultVal));
00196 }
00197 
00198 //----------------------------------------------------------------------------//
00199 
00200 template <>
00201 inline float
00202 ProceduralField<float>::typedIntMetadata(const std::string &name,
00203                                          const float& defaultVal) const
00204 {
00205   return metadata().intMetadata(name, static_cast<int>(defaultVal));
00206 }
00207 
00208 //----------------------------------------------------------------------------//
00209 
00210 template <>
00211 inline double
00212 ProceduralField<double>::typedIntMetadata(const std::string &name,
00213                                           const double& defaultVal) const
00214 {
00215   return metadata().intMetadata(name, static_cast<int>(defaultVal));
00216 }
00217 
00218 //----------------------------------------------------------------------------//
00219 
00220 template <>
00221 inline V3h
00222 ProceduralField<V3h>::typedIntMetadata(const std::string &name,
00223                                        const V3h& defaultVal) const
00224 {
00225   return V3h(metadata().vecIntMetadata(name, defaultVal));
00226 }
00227 
00228 //----------------------------------------------------------------------------//
00229 
00230 template <>
00231 inline V3f
00232 ProceduralField<V3f>::typedIntMetadata(const std::string &name,
00233                                        const V3f& defaultVal) const
00234 {
00235   return V3f(metadata().vecIntMetadata(name, defaultVal));
00236 }
00237 
00238 //----------------------------------------------------------------------------//
00239 
00240 template <>
00241 inline V3d
00242 ProceduralField<V3d>::typedIntMetadata(const std::string &name,
00243                                        const V3d& defaultVal) const
00244 {
00245   return V3d(metadata().vecIntMetadata(name, defaultVal));
00246 }
00247 
00248 //----------------------------------------------------------------------------//
00249 
00250 template <>
00251 inline half
00252 ProceduralField<half>::typedFloatMetadata(const std::string &name, 
00253                                           const half& defaultVal) const
00254 {
00255   return metadata().floatMetadata(name, static_cast<float>(defaultVal));
00256 }
00257 
00258 //----------------------------------------------------------------------------//
00259 
00260 template <>
00261 inline float
00262 ProceduralField<float>::typedFloatMetadata(const std::string &name, 
00263                                            const float& defaultVal) const
00264 {
00265   return metadata().floatMetadata(name, defaultVal);
00266 }
00267 
00268 //----------------------------------------------------------------------------//
00269 
00270 template <>
00271 inline double
00272 ProceduralField<double>::typedFloatMetadata(const std::string &name, 
00273                                             const double& defaultVal) const
00274 {
00275   return metadata().floatMetadata(name, static_cast<float>(defaultVal));
00276 }
00277 
00278 //----------------------------------------------------------------------------//
00279 
00280 template <>
00281 inline V3h
00282 ProceduralField<V3h>::typedFloatMetadata(const std::string &name, 
00283                                          const V3h& defaultVal) const
00284 {
00285   return V3h(metadata().vecFloatMetadata(name, defaultVal));
00286 }
00287 
00288 //----------------------------------------------------------------------------//
00289 
00290 template <>
00291 inline V3f
00292 ProceduralField<V3f>::typedFloatMetadata(const std::string &name, 
00293                                          const V3f& defaultVal) const
00294 {
00295   return V3f(metadata().vecFloatMetadata(name, defaultVal));
00296 }
00297 
00298 //----------------------------------------------------------------------------//
00299 
00300 template <>
00301 inline V3d
00302 ProceduralField<V3d>::typedFloatMetadata(const std::string &name, 
00303                                          const V3d& defaultVal) const
00304 {
00305   return V3d(metadata().vecFloatMetadata(name, defaultVal));
00306 }
00307 
00308 //----------------------------------------------------------------------------//
00309 
00310 FIELD3D_NAMESPACE_HEADER_CLOSE
00311 
00312 //----------------------------------------------------------------------------//
00313 
00314 #endif // Include guard
00315