Field3D
FieldMapping.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2009 Sony Pictures Imageworks Inc
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution. Neither the name of Sony Pictures Imageworks nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //----------------------------------------------------------------------------//
37 
44 //----------------------------------------------------------------------------//
45 
46 #ifndef _INCLUDED_Field3D_FieldMapping_H_
47 #define _INCLUDED_Field3D_FieldMapping_H_
48 
49 #include <vector>
50 #include <algorithm>
51 
52 #include "Curve.h"
53 #include "Exception.h"
54 #include "RefCount.h"
55 #include "Types.h"
56 
57 //----------------------------------------------------------------------------//
58 
59 #include "ns.h"
60 
62 
63 //----------------------------------------------------------------------------//
64 // FieldMapping
65 //----------------------------------------------------------------------------//
66 
84 //----------------------------------------------------------------------------//
85 
86 class FieldMapping : public RefBase
87 {
88  public:
89 
90  // Typedefs ------------------------------------------------------------------
91 
92  typedef boost::intrusive_ptr<FieldMapping> Ptr;
93 
94  // RTTI replacement ----------------------------------------------------------
95 
98 
99  static const char* classType()
100  {
101  return "FieldMapping";
102  }
103 
104  // Ctors, dtor ---------------------------------------------------------------
105 
108 
110  FieldMapping();
112  FieldMapping(const Box3i &extents);
114  virtual ~FieldMapping();
115 
117 
118  // Main methods --------------------------------------------------------------
119 
125  void setExtents(const Box3i &extents);
126 
128  const V3d& origin() const
129  { return m_origin; }
131  const V3d& resolution() const
132  { return m_res; }
133 
134  // To be implemented by subclasses -------------------------------------------
135 
138 
141  virtual Ptr clone() const = 0;
142 
144  virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const = 0;
145  virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const = 0;
147  virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const = 0;
148  virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float time) const = 0;
150  virtual void worldToLocal(const V3d &wsP, V3d &lsP) const = 0;
151  virtual void worldToLocal(const V3d &wsP, V3d &lsP, float time) const = 0;
153  virtual void localToWorld(const V3d &lsP, V3d &wsP) const = 0;
154  virtual void localToWorld(const V3d &lsP, V3d &wsP, float time) const = 0;
155 
157  virtual V3d wsVoxelSize(int i, int j, int k) const = 0;
158 
161  virtual void extentsChanged()
162  { /* Empty */ }
163 
165  virtual std::string className() const = 0;
166 
168  virtual bool isIdentical(FieldMapping::Ptr other,
169  double tolerance = 0.0) const = 0;
170 
172 
173  // Transform calls -----------------------------------------------------------
174 
177 
180  void localToVoxel(const V3d &lsP, V3d &vsP) const;
182  void voxelToLocal(const V3d &vsP, V3d &lsP) const;
183 
185 
186 protected:
187 
194 
195 private:
196 
197  // Typedefs ------------------------------------------------------------------
198 
200  typedef RefBase base;
201 
202 };
203 
204 //----------------------------------------------------------------------------//
205 // NullFieldMapping
206 //----------------------------------------------------------------------------//
207 
216 //----------------------------------------------------------------------------//
217 
219 {
220 public:
221 
222  // Typedefs ------------------------------------------------------------------
223 
225  typedef boost::intrusive_ptr<NullFieldMapping> Ptr;
226 
227  // RTTI replacement ----------------------------------------------------------
228 
231 
232  static const char* classType()
233  {
234  return "NullFieldMapping";
235  }
236 
237  // Ctors, dtor ---------------------------------------------------------------
238 
241 
243  : FieldMapping()
244  { /* Empty */ }
245  NullFieldMapping(const Box3i &extents)
246  : FieldMapping(extents)
247  { /* Empty */ }
248 
250 
251  // From FieldMapping ---------------------------------------------------------
252 
255 
256  virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
257  { localToVoxel(wsP, vsP); }
258  virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float /*time*/) const
259  { localToVoxel(wsP, vsP); }
260 
261  virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
262  { voxelToLocal(vsP, wsP); }
263  virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float /*time*/) const
264  { voxelToLocal(vsP, wsP); }
265 
266  virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
267  { lsP = wsP; }
268  virtual void worldToLocal(const V3d &wsP, V3d &lsP, float /*time*/) const
269  { lsP = wsP; }
270 
271  virtual void localToWorld(const V3d &lsP, V3d &wsP) const
272  { wsP = lsP; }
273  virtual void localToWorld(const V3d &lsP, V3d &wsP, float /*time*/) const
274  { wsP = lsP; }
275 
276  virtual std::string className() const;
277 
278  virtual bool isIdentical(FieldMapping::Ptr other,
279  double tolerance = 0.0) const;
280 
281  virtual V3d wsVoxelSize(int /*i*/, int /*j*/, int /*k*/) const
282  { return V3d(1.0 / m_res.x, 1.0 / m_res.y, 1.0 / m_res.z); }
283 
284  virtual FieldMapping::Ptr clone() const;
285 
287 
288 private:
289 
290  // Typedefs ------------------------------------------------------------------
291 
293  typedef FieldMapping base;
294 
295 };
296 
297 //----------------------------------------------------------------------------//
298 // MatrixFieldMapping
299 //----------------------------------------------------------------------------//
300 
314 //----------------------------------------------------------------------------//
315 
317 {
318 public:
319 
320  // Typedefs ------------------------------------------------------------------
321 
323  typedef boost::intrusive_ptr<MatrixFieldMapping> Ptr;
326 
327  // RTTI replacement ----------------------------------------------------------
328 
331 
332  static const char* classType ()
333  {
334  return "MatrixFieldMapping";
335  }
336 
337  // Ctors, dtor ---------------------------------------------------------------
338 
341 
343  MatrixFieldMapping(const Box3i &extents);
344 
346 
347  // Main methods --------------------------------------------------------------
348 
352  void setLocalToWorld(const M44d &lsToWs);
354  void setLocalToWorld(float t, const M44d &lsToWs);
355 
358  const M44d& localToWorld() const
359  { return m_lsToWs; }
360 
363  const M44d& worldToVoxel() const
364  { return m_wsToVs; }
365 
368  const M44d& voxelToWorld() const
369  { return m_vsToWs; }
370 
373  { return m_lsToWsCurve.samples(); }
374 
377  void makeIdentity();
378 
379  // From FieldMapping ---------------------------------------------------------
380 
383 
384  virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
385  { m_wsToVs.multVecMatrix(wsP, vsP); }
386  virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const
387  {
388  if (!m_isTimeVarying) {
389  m_wsToVs.multVecMatrix(wsP, vsP);
390  } else {
391  M44d wsToVs = m_vsToWsCurve.linear(time).inverse();
392  wsToVs.multVecMatrix(wsP, vsP);
393  }
394  }
395 
396  virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
397  { m_vsToWs.multVecMatrix(vsP, wsP); }
398  virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float time) const
399  {
400  if (!m_isTimeVarying) {
401  m_vsToWs.multVecMatrix(vsP, wsP);
402  } else {
403  M44d vsToWs = m_vsToWsCurve.linear(time);
404  vsToWs.multVecMatrix(vsP, wsP);
405  }
406  }
407 
408  virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
409  { m_wsToLs.multVecMatrix(wsP, lsP); }
410  virtual void worldToLocal(const V3d &wsP, V3d &lsP,
411  float time) const
412  {
413  if (!m_isTimeVarying) {
414  m_wsToLs.multVecMatrix(wsP, lsP);
415  } else {
416  M44d wsToLs = m_lsToWsCurve.linear(time).inverse();
417  wsToLs.multVecMatrix(wsP, lsP);
418  }
419  }
420 
421  virtual void localToWorld(const V3d &lsP, V3d &wsP) const
422  { m_lsToWs.multVecMatrix(lsP, wsP); }
423  virtual void localToWorld(const V3d &lsP, V3d &wsP, float time) const
424  {
425  if (!m_isTimeVarying) {
426  m_lsToWs.multVecMatrix(lsP, wsP);
427  } else {
428  M44d lsToWs = m_lsToWsCurve.linear(time);
429  lsToWs.multVecMatrix(lsP, wsP);
430  }
431  }
432 
434  void worldToVoxelDir(const V3d &wsV, V3d &vsV) const
435  { m_wsToVs.multDirMatrix(wsV, vsV); }
436 
438  void voxelToWorldDir(const V3d &vsV, V3d &wsV) const
439  { m_vsToWs.multDirMatrix(vsV, wsV); }
440 
442  void worldToLocalDir(const V3d &wsV, V3d &lsV) const
443  { m_wsToLs.multDirMatrix(wsV, lsV); }
444 
446  void localToWorldDir(const V3d &lsV, V3d &wsV) const
447  { m_lsToWs.multDirMatrix(lsV, wsV); }
448 
449  virtual void extentsChanged();
450 
451  virtual std::string className() const;
452 
453  virtual bool isIdentical(FieldMapping::Ptr other,
454  double tolerance = 0.0) const;
455 
456  virtual V3d wsVoxelSize(int /*i*/, int /*j*/, int /*k*/) const
457  { return m_wsVoxelSize; }
458 
459  virtual FieldMapping::Ptr clone() const;
460 
462 
463 private:
464 
466  void updateTransform();
467 
469  void getLocalToVoxelMatrix(M44d &result);
470 
471  // Data members -------------------------------------------------------------
472 
485 
490 
494 
498 
499  // Typedefs ------------------------------------------------------------------
500 
502  typedef FieldMapping base;
503 };
504 
505 //----------------------------------------------------------------------------//
506 // FrustumFieldMapping
507 //----------------------------------------------------------------------------//
508 
534 //----------------------------------------------------------------------------//
535 
537 {
538 public:
539 
540  // Typedefs ------------------------------------------------------------------
541 
543  typedef boost::intrusive_ptr<FrustumFieldMapping> Ptr;
548 
549  // Exceptions ----------------------------------------------------------------
550 
552 
553  // Enums ---------------------------------------------------------------------
554 
555 
556  enum ZDistribution {
560  };
561 
562  // RTTI replacement ----------------------------------------------------------
563 
566 
567  static const char* classType ()
568  {
569  return "FrustumFieldMapping";
570  }
571 
572  // Ctors, dtor ---------------------------------------------------------------
573 
576 
578  FrustumFieldMapping(const Box3i &extents);
579 
581 
582  // Main methods --------------------------------------------------------------
583 
590  void setTransforms(const M44d &ssToWs, const M44d &csToWs);
595  void setTransforms(float t, const M44d &ssToWs, const M44d &csToWs);
596 
599  { m_zDistribution = dist; }
602  { return m_zDistribution; }
603 
606  const M44d screenToWorld() const
607  { return m_ssToWsCurve.linear(0.0); }
608 
611  const M44d cameraToWorld() const
612  { return m_csToWsCurve.linear(0.0); }
613 
616  { return m_ssToWsCurve.samples(); }
617 
620  { return m_csToWsCurve.samples(); }
621 
624  { return m_nearCurve.samples(); }
625 
628  { return m_farCurve.samples(); }
629 
631  double nearPlane() const
632  { return m_nearCurve.linear(0.0); }
633 
635  double farPlane() const
636  { return m_farCurve.linear(0.0); }
637 
641  void reset();
642 
643  // From FieldMapping ---------------------------------------------------------
644 
647 
648  virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const;
649  virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const;
650 
651  virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const;
652  virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float time) const;
653 
654  virtual void worldToLocal(const V3d &wsP, V3d &lsP) const;
655  virtual void worldToLocal(const V3d &wsP, V3d &lsP, float time) const;
656 
657  virtual void localToWorld(const V3d &lsP, V3d &wsP) const;
658  virtual void localToWorld(const V3d &lsP, V3d &wsP, float time) const;
659 
660  virtual void extentsChanged();
661 
662  virtual std::string className() const;
663 
664  virtual bool isIdentical(FieldMapping::Ptr other,
665  double tolerance = 0.0) const;
666 
667  virtual V3d wsVoxelSize(int i, int j, int k) const;
668 
669  virtual FieldMapping::Ptr clone() const;
670 
672 
673 private:
674 
676  void computeVoxelSize();
677 
679  void getLocalToVoxelMatrix(M44d &result);
680 
683  void clearCurves();
684 
685  // Data members -------------------------------------------------------------
686 
689 
703 
706  std::vector<V3d> m_wsVoxelSize;
707 
713 
714  // Typedefs ------------------------------------------------------------------
715 
718 
719 };
720 
721 //----------------------------------------------------------------------------//
722 
724 
725 //----------------------------------------------------------------------------//
726 
727 #endif // Include guard