Field3D
FieldMapping.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_FieldMapping_H_
00047 #define _INCLUDED_Field3D_FieldMapping_H_
00048 
00049 #include <vector>
00050 #include <algorithm>
00051 
00052 #include "RefCount.h"
00053 #include "Types.h"
00054 
00055 //----------------------------------------------------------------------------//
00056 
00057 #include "ns.h"
00058 
00059 FIELD3D_NAMESPACE_OPEN
00060 
00061 //----------------------------------------------------------------------------//
00062 // FieldMapping
00063 //----------------------------------------------------------------------------//
00064 
00080 //----------------------------------------------------------------------------//
00081 
00082 class FieldMapping : public RefBase
00083 {
00084  public:
00085 
00086   // Typedefs ------------------------------------------------------------------
00087 
00088   typedef boost::intrusive_ptr<FieldMapping> Ptr;
00089 
00090   // Ctors, dtor ---------------------------------------------------------------
00091 
00094 
00096   FieldMapping();
00098   FieldMapping(const Box3i &extents);
00100   virtual ~FieldMapping();
00101 
00103 
00104   // Main methods --------------------------------------------------------------
00105 
00111   void setExtents(const Box3i &extents);
00112 
00114   const V3d& origin() const
00115   { return m_origin; }
00117   const V3d& resolution() const
00118   { return m_res; }
00119 
00120   
00121   // To be implemented by subclasses -------------------------------------------
00122 
00125 
00128   virtual Ptr clone() const = 0;
00129 
00131   virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const = 0;
00132   virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const = 0;
00134   virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const = 0;
00136   virtual void worldToLocal(const V3d &wsP, V3d &lsP) const = 0;
00137   virtual void worldToLocal(const V3d &wsP, V3d &lsP, float time) const = 0;
00139   virtual void localToWorld(const V3d &lsP, V3d &wsP) const = 0;
00140 
00145   virtual void voxelToWorld(std::vector<V3d>::const_iterator vsP, 
00146                             std::vector<V3d>::const_iterator end, 
00147                             std::vector<V3d>::iterator wsP) const = 0;
00148   virtual void worldToVoxel(std::vector<V3d>::const_iterator wsP, 
00149                             std::vector<V3d>::const_iterator end, 
00150                             std::vector<V3d>::iterator vsP) const = 0;
00151   virtual void worldToLocal(std::vector<V3d>::const_iterator wsP, 
00152                             std::vector<V3d>::const_iterator end, 
00153                             std::vector<V3d>::iterator lsP) const = 0;
00154 
00156   virtual V3d wsVoxelSize(int i, int j, int k) const = 0;
00157 
00160   virtual void extentsChanged()
00161   { /* Empty */ }
00162   
00164   virtual std::string className() const = 0;
00166   virtual bool isIdentical(FieldMapping::Ptr other, 
00167                            double tolerance = 0.0) const = 0;
00168 
00170 
00171   // Transform calls -----------------------------------------------------------
00172 
00175 
00178   void localToVoxel(const V3d &lsP, V3d &vsP) const;
00179   void localToVoxel(const V3d &lsP, V3d &vsP, float time) const;
00180   void localToVoxel(std::vector<V3d>::const_iterator lsP, 
00181                     std::vector<V3d>::const_iterator end, 
00182                     std::vector<V3d>::iterator vsP) const;
00184   void voxelToLocal(const V3d &vsP, V3d &lsP) const;
00185 
00187  
00188  protected:
00189 
00192   V3d m_origin;
00195   V3d m_res;
00196 
00197 };
00198 
00199 
00200 //----------------------------------------------------------------------------//
00201 // NullFieldMapping
00202 //----------------------------------------------------------------------------//
00203 
00212 //----------------------------------------------------------------------------//
00213 
00214 class NullFieldMapping : public FieldMapping
00215 {
00216 public:
00217 
00218   // Typedefs ------------------------------------------------------------------
00219 
00221   typedef boost::intrusive_ptr<NullFieldMapping> Ptr;
00222   
00223   // Ctors, dtor ---------------------------------------------------------------
00224 
00227 
00228   NullFieldMapping()
00229     : FieldMapping()
00230   { /* Empty */ }
00231   NullFieldMapping(const Box3i &extents)
00232     : FieldMapping(extents)
00233   { /* Empty */ }
00234 
00236 
00237   // From FieldMapping ---------------------------------------------------------
00238 
00241 
00242   virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const 
00243   { localToVoxel(wsP, vsP); }
00244   virtual void worldToVoxel(const V3d &wsP, V3d &vsP,
00245                             float time) const 
00246   { localToVoxel(wsP, vsP, time); }
00247   virtual void worldToVoxel(std::vector<V3d>::const_iterator wsP,
00248                             std::vector<V3d>::const_iterator end,
00249                             std::vector<V3d>::iterator vsP) const 
00250   { localToVoxel(wsP, end, vsP); }
00251   virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const 
00252   { voxelToLocal(vsP, wsP); }
00253 
00254   virtual void voxelToWorld(std::vector<V3d>::const_iterator vsP, 
00255                             std::vector<V3d>::const_iterator end, 
00256                             std::vector<V3d>::iterator wsP) const 
00257   { 
00258     for (; vsP != end; ++vsP, ++wsP) 
00259       voxelToLocal(*vsP, *wsP);
00260   }
00261 
00262   virtual void worldToLocal(const V3d &wsP, V3d &lsP) const 
00263   { lsP = wsP; }
00264   virtual void worldToLocal(const V3d &wsP, V3d &lsP,
00265                             float /*time*/) const 
00266   { lsP = wsP; }
00267   virtual void worldToLocal(std::vector<V3d>::const_iterator wsP,
00268                             std::vector<V3d>::const_iterator end,
00269                             std::vector<V3d>::iterator lsP) const 
00270   { std::copy(wsP, end, lsP); }
00271   virtual void localToWorld(const V3d &lsP, V3d &wsP) const 
00272   { wsP = lsP; }
00273   virtual std::string className() const;
00274   virtual bool isIdentical(FieldMapping::Ptr other, 
00275                            double tolerance = 0.0) const;
00276   virtual V3d wsVoxelSize(int /*i*/, int /*j*/, int /*k*/) const
00277   { return V3d(1.0 / m_res.x, 1.0 / m_res.y, 1.0 / m_res.z); }
00278 
00279   virtual FieldMapping::Ptr clone() const;
00280 
00282 
00283 };
00284 
00285 //----------------------------------------------------------------------------//
00286 // MatrixFieldMapping
00287 //----------------------------------------------------------------------------//
00288 
00299 //----------------------------------------------------------------------------//
00300 
00301 class MatrixFieldMapping : public FieldMapping
00302 {
00303 public:
00304 
00305   // Typedefs ------------------------------------------------------------------
00306 
00308   typedef boost::intrusive_ptr<MatrixFieldMapping> Ptr;
00309 
00310   // Ctors, dtor ---------------------------------------------------------------
00311 
00314 
00315   MatrixFieldMapping();
00316   MatrixFieldMapping(const Box3i &extents);
00317 
00319 
00320   // Main methods --------------------------------------------------------------
00321   
00324   void setLocalToWorld(const M44d &lsToWs);
00325 
00327   const M44d& localToWorld() const
00328   { return m_lsToWs; }
00329 
00331   const M44d& worldToVoxel() const
00332   { return m_wsToVs; }
00333 
00336   void makeIndentity();
00337 
00338   // From FieldMapping ---------------------------------------------------------
00339 
00342 
00343   virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const 
00344   { m_wsToVs.multVecMatrix(wsP, vsP); }
00345 
00347   virtual void worldToVoxel(const V3d &wsP, V3d &vsP,
00348                             float /*time*/) const 
00349   { m_wsToVs.multVecMatrix(wsP, vsP); }
00350 
00351   virtual void worldToVoxel(std::vector<V3d>::const_iterator wsP, 
00352                             std::vector<V3d>::const_iterator end, 
00353                             std::vector<V3d>::iterator vsP) const 
00354   { 
00355     for (; wsP != end; ++wsP, ++vsP) 
00356       m_wsToVs.multVecMatrix(*wsP, *vsP);
00357   }
00358 
00359   virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const 
00360   { m_vsToWs.multVecMatrix(vsP, wsP); }
00361 
00362   virtual void voxelToWorld(std::vector<V3d>::const_iterator vsP, 
00363                             std::vector<V3d>::const_iterator end, 
00364                             std::vector<V3d>::iterator wsP) const 
00365   { 
00366     for (; vsP != end; ++vsP, ++wsP) 
00367       m_vsToWs.multVecMatrix(*vsP, *wsP);
00368   }
00369 
00370   virtual void worldToLocal(const V3d &wsP, V3d &lsP) const 
00371   { m_wsToLs.multVecMatrix(wsP, lsP); }
00372 
00374   virtual void worldToLocal(const V3d &wsP, V3d &lsP,
00375                             float /*time*/) const 
00376   { m_wsToLs.multVecMatrix(wsP, lsP); }
00377 
00378   virtual void worldToLocal(std::vector<V3d>::const_iterator wsP, 
00379                             std::vector<V3d>::const_iterator end, 
00380                             std::vector<V3d>::iterator lsP) const 
00381   { 
00382     for (; wsP != end; ++wsP, ++lsP) 
00383       m_wsToLs.multVecMatrix(*wsP, *lsP);
00384   }
00385 
00386   virtual void localToWorld(const V3d &lsP, V3d &wsP) const 
00387   { m_lsToWs.multVecMatrix(lsP, wsP); }
00388 
00389   void worldToVoxelDir(const V3d &wsV, V3d &vsV) const 
00390   { m_wsToVs.multDirMatrix(wsV, vsV); }
00391 
00392   void voxelToWorldDir(const V3d &vsV, V3d &wsV) const 
00393   { m_vsToWs.multDirMatrix(vsV, wsV); }
00394 
00395   void worldToLocalDir(const V3d &wsV, V3d &lsV) const 
00396   { m_wsToLs.multDirMatrix(wsV, lsV); }
00397 
00398   void localToWorldDir(const V3d &lsV, V3d &wsV) const 
00399   { m_lsToWs.multDirMatrix(lsV, wsV); }
00400 
00401   virtual void extentsChanged();
00402 
00403   virtual std::string className() const;
00404   virtual bool isIdentical(FieldMapping::Ptr other, 
00405                            double tolerance = 0.0) const;
00406 
00407   virtual V3d wsVoxelSize(int /*i*/, int /*j*/, int /*k*/) const
00408   { return m_wsVoxelSize; }
00409 
00410   virtual FieldMapping::Ptr clone() const;
00411 
00413 
00414 private:
00415 
00417   void updateTransform();
00418 
00420   void getLocalToVoxelMatrix(M44d &result);
00421 
00423   M44d m_lsToWs;
00425   M44d m_wsToLs;
00427   M44d m_vsToWs;
00429   M44d m_wsToVs;
00432   V3d m_wsVoxelSize;
00433 };
00434 
00435 //----------------------------------------------------------------------------//
00436 
00437 FIELD3D_NAMESPACE_HEADER_CLOSE
00438 
00439 //----------------------------------------------------------------------------//
00440 
00441 #endif // Include guard