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   // RTTI replacement ----------------------------------------------------------
00133 
00134   typedef ProceduralField<Data_T> class_type;
00135   DEFINE_FIELD_RTTI_ABSTRACT_CLASS
00136 
00137   static const char *staticClassName()
00138   {
00139     return "ProceduralField";
00140   }
00141 
00142   static const char *classType()
00143   {
00144     return ProceduralField<Data_T>::ms_classType.name();
00145   }
00146 
00147   // Constructors --------------------------------------------------------------
00148 
00150   virtual ~ProceduralField()
00151   { /* Empty */ }
00152 
00153   // To be implemented by subclasses -------------------------------------------
00154 
00155   virtual Data_T lsSample(const V3d &lsP) const = 0;
00156 
00157   // From FieldBase ------------------------------------------------------------
00158 
00159   virtual std::string className() const
00160   { return staticClassName(); }
00161 
00162   // From Field ----------------------------------------------------------------
00163 
00166   virtual Data_T value(int i, int j, int k) const = 0;
00167 
00168   // Main methods --------------------------------------------------------------
00169 
00173   Data_T typedIntMetadata(const std::string &name, 
00174                           const Data_T& defaultVal) const;
00178   Data_T typedFloatMetadata(const std::string &name,
00179                             const Data_T& defaultVal) const;
00180 
00181 private:
00182 
00183   // Static data members -------------------------------------------------------
00184 
00185   static TemplatedFieldType<ProceduralField<Data_T> > ms_classType;
00186 
00187   // Typedefs ------------------------------------------------------------------
00188 
00189   typedef Field<Data_T> base;
00190 
00191 };
00192 
00193 //----------------------------------------------------------------------------//
00194 // Typedefs
00195 //----------------------------------------------------------------------------//
00196 
00197 typedef ProceduralField<half>   ProceduralFieldh;
00198 typedef ProceduralField<float>  ProceduralFieldf;
00199 typedef ProceduralField<double> ProceduralFieldd;
00200 typedef ProceduralField<V3h>    ProceduralField3h;
00201 typedef ProceduralField<V3f>    ProceduralField3f;
00202 typedef ProceduralField<V3d>    ProceduralField3d;
00203 
00204 //----------------------------------------------------------------------------//
00205 // Static member instantiation
00206 //----------------------------------------------------------------------------//
00207 
00208 FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(ProceduralField);
00209 
00210 //----------------------------------------------------------------------------//
00211 // Template specializations
00212 //----------------------------------------------------------------------------//
00213 
00214 template <>
00215 inline half
00216 ProceduralField<half>::typedIntMetadata(const std::string &name,
00217                                         const half& defaultVal) const
00218 {
00219   return metadata().intMetadata(name, static_cast<int>(defaultVal));
00220 }
00221 
00222 //----------------------------------------------------------------------------//
00223 
00224 template <>
00225 inline float
00226 ProceduralField<float>::typedIntMetadata(const std::string &name,
00227                                          const float& defaultVal) const
00228 {
00229   return metadata().intMetadata(name, static_cast<int>(defaultVal));
00230 }
00231 
00232 //----------------------------------------------------------------------------//
00233 
00234 template <>
00235 inline double
00236 ProceduralField<double>::typedIntMetadata(const std::string &name,
00237                                           const double& defaultVal) const
00238 {
00239   return metadata().intMetadata(name, static_cast<int>(defaultVal));
00240 }
00241 
00242 //----------------------------------------------------------------------------//
00243 
00244 template <>
00245 inline V3h
00246 ProceduralField<V3h>::typedIntMetadata(const std::string &name,
00247                                        const V3h& defaultVal) const
00248 {
00249   return V3h(metadata().vecIntMetadata(name, defaultVal));
00250 }
00251 
00252 //----------------------------------------------------------------------------//
00253 
00254 template <>
00255 inline V3f
00256 ProceduralField<V3f>::typedIntMetadata(const std::string &name,
00257                                        const V3f& defaultVal) const
00258 {
00259   return V3f(metadata().vecIntMetadata(name, defaultVal));
00260 }
00261 
00262 //----------------------------------------------------------------------------//
00263 
00264 template <>
00265 inline V3d
00266 ProceduralField<V3d>::typedIntMetadata(const std::string &name,
00267                                        const V3d& defaultVal) const
00268 {
00269   return V3d(metadata().vecIntMetadata(name, defaultVal));
00270 }
00271 
00272 //----------------------------------------------------------------------------//
00273 
00274 template <>
00275 inline half
00276 ProceduralField<half>::typedFloatMetadata(const std::string &name, 
00277                                           const half& defaultVal) const
00278 {
00279   return metadata().floatMetadata(name, static_cast<float>(defaultVal));
00280 }
00281 
00282 //----------------------------------------------------------------------------//
00283 
00284 template <>
00285 inline float
00286 ProceduralField<float>::typedFloatMetadata(const std::string &name, 
00287                                            const float& defaultVal) const
00288 {
00289   return metadata().floatMetadata(name, defaultVal);
00290 }
00291 
00292 //----------------------------------------------------------------------------//
00293 
00294 template <>
00295 inline double
00296 ProceduralField<double>::typedFloatMetadata(const std::string &name, 
00297                                             const double& defaultVal) const
00298 {
00299   return metadata().floatMetadata(name, static_cast<float>(defaultVal));
00300 }
00301 
00302 //----------------------------------------------------------------------------//
00303 
00304 template <>
00305 inline V3h
00306 ProceduralField<V3h>::typedFloatMetadata(const std::string &name, 
00307                                          const V3h& defaultVal) const
00308 {
00309   return V3h(metadata().vecFloatMetadata(name, defaultVal));
00310 }
00311 
00312 //----------------------------------------------------------------------------//
00313 
00314 template <>
00315 inline V3f
00316 ProceduralField<V3f>::typedFloatMetadata(const std::string &name, 
00317                                          const V3f& defaultVal) const
00318 {
00319   return V3f(metadata().vecFloatMetadata(name, defaultVal));
00320 }
00321 
00322 //----------------------------------------------------------------------------//
00323 
00324 template <>
00325 inline V3d
00326 ProceduralField<V3d>::typedFloatMetadata(const std::string &name, 
00327                                          const V3d& defaultVal) const
00328 {
00329   return V3d(metadata().vecFloatMetadata(name, defaultVal));
00330 }
00331 
00332 //----------------------------------------------------------------------------//
00333 
00334 FIELD3D_NAMESPACE_HEADER_CLOSE
00335 
00336 //----------------------------------------------------------------------------//
00337 
00338 #endif // Include guard
00339