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
87{
88 public:
89
90 // Typedefs ------------------------------------------------------------------
91
92 typedef boost::intrusive_ptr<FieldMapping> Ptr;
93
94 // RTTI replacement ----------------------------------------------------------
95
98
99 static const char* staticClassType()
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
186protected:
187
194
195private:
196
197 // Typedefs ------------------------------------------------------------------
198
200 typedef RefBase base;
201
202};
203
204//----------------------------------------------------------------------------//
205// Utilities
206//----------------------------------------------------------------------------//
207
211void worldToVoxel(const Field3D::FieldMapping* mapping,
212 const Box3d &wsBounds,
213 Box3d &vsBounds);
214
218void transformBounds(const M44d &mtx,
219 const Box3d &fromBounds,
220 Box3d &toBounds);
221
222//----------------------------------------------------------------------------//
223// NullFieldMapping
224//----------------------------------------------------------------------------//
225
234//----------------------------------------------------------------------------//
235
237{
238public:
239
240 // Typedefs ------------------------------------------------------------------
241
243 typedef boost::intrusive_ptr<NullFieldMapping> Ptr;
244
245 // RTTI replacement ----------------------------------------------------------
246
249
250 static const char* staticClassType()
251 {
252 return "NullFieldMapping";
253 }
254
255 // Ctors, dtor ---------------------------------------------------------------
256
259
261 : FieldMapping()
262 { /* Empty */ }
263 NullFieldMapping(const Box3i &extents)
264 : FieldMapping(extents)
265 { /* Empty */ }
266
268
269 // From FieldMapping ---------------------------------------------------------
270
273
274 virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
275 { localToVoxel(wsP, vsP); }
276 virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float /*time*/) const
277 { localToVoxel(wsP, vsP); }
278
279 virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
280 { voxelToLocal(vsP, wsP); }
281 virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float /*time*/) const
282 { voxelToLocal(vsP, wsP); }
283
284 virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
285 { lsP = wsP; }
286 virtual void worldToLocal(const V3d &wsP, V3d &lsP, float /*time*/) const
287 { lsP = wsP; }
288
289 virtual void localToWorld(const V3d &lsP, V3d &wsP) const
290 { wsP = lsP; }
291 virtual void localToWorld(const V3d &lsP, V3d &wsP, float /*time*/) const
292 { wsP = lsP; }
293
294 virtual std::string className() const;
295
296 virtual bool isIdentical(FieldMapping::Ptr other,
297 double tolerance = 0.0) const;
298
299 virtual V3d wsVoxelSize(int /*i*/, int /*j*/, int /*k*/) const
300 { return V3d(1.0 / m_res.x, 1.0 / m_res.y, 1.0 / m_res.z); }
301
302 virtual FieldMapping::Ptr clone() const;
303
305
306private:
307
308 // Typedefs ------------------------------------------------------------------
309
312
313};
314
315//----------------------------------------------------------------------------//
316// MatrixFieldMapping
317//----------------------------------------------------------------------------//
318
332//----------------------------------------------------------------------------//
333
335{
336public:
337
338 // Typedefs ------------------------------------------------------------------
339
341 typedef boost::intrusive_ptr<MatrixFieldMapping> Ptr;
344
345 // RTTI replacement ----------------------------------------------------------
346
349
350 static const char* staticClassType()
351 {
352 return "MatrixFieldMapping";
353 }
354
355 // Ctors, dtor ---------------------------------------------------------------
356
359
361 MatrixFieldMapping(const Box3i &extents);
362
364
365 // Main methods --------------------------------------------------------------
366
370 void setLocalToWorld(const M44d &lsToWs);
372 void setLocalToWorld(float t, const M44d &lsToWs);
373
376 const M44d& localToWorld() const
377 { return m_lsToWs; }
378
381 const M44d& worldToVoxel() const
382 { return m_wsToVs; }
383
385 M44d worldToVoxel(float time) const
386 {
387 if (!m_isTimeVarying) {
388 return m_wsToVs;
389 } else {
390 return m_vsToWsCurve.linear(time).inverse();
391 }
392 }
393
396 const M44d& voxelToWorld() const
397 { return m_vsToWs; }
398
401 { return m_lsToWsCurve.samples(); }
402
405 void makeIdentity();
406
407 // From FieldMapping ---------------------------------------------------------
408
411
412 virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
413 { m_wsToVs.multVecMatrix(wsP, vsP); }
414 virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const
415 {
416 if (!m_isTimeVarying) {
417 m_wsToVs.multVecMatrix(wsP, vsP);
418 } else {
419 M44d wsToVs = m_vsToWsCurve.linear(time).inverse();
420 wsToVs.multVecMatrix(wsP, vsP);
421 }
422 }
423
424 virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
425 { m_vsToWs.multVecMatrix(vsP, wsP); }
426 virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float time) const
427 {
428 if (!m_isTimeVarying) {
429 m_vsToWs.multVecMatrix(vsP, wsP);
430 } else {
431 M44d vsToWs = m_vsToWsCurve.linear(time);
432 vsToWs.multVecMatrix(vsP, wsP);
433 }
434 }
435
436 virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
437 { m_wsToLs.multVecMatrix(wsP, lsP); }
438 virtual void worldToLocal(const V3d &wsP, V3d &lsP,
439 float time) const
440 {
441 if (!m_isTimeVarying) {
442 m_wsToLs.multVecMatrix(wsP, lsP);
443 } else {
444 M44d wsToLs = m_lsToWsCurve.linear(time).inverse();
445 wsToLs.multVecMatrix(wsP, lsP);
446 }
447 }
448
449 virtual void localToWorld(const V3d &lsP, V3d &wsP) const
450 { m_lsToWs.multVecMatrix(lsP, wsP); }
451 virtual void localToWorld(const V3d &lsP, V3d &wsP, float time) const
452 {
453 if (!m_isTimeVarying) {
454 m_lsToWs.multVecMatrix(lsP, wsP);
455 } else {
456 M44d lsToWs = m_lsToWsCurve.linear(time);
457 lsToWs.multVecMatrix(lsP, wsP);
458 }
459 }
460
462 void worldToVoxelDir(const V3d &wsV, V3d &vsV) const
463 { m_wsToVs.multDirMatrix(wsV, vsV); }
464
466 void voxelToWorldDir(const V3d &vsV, V3d &wsV) const
467 { m_vsToWs.multDirMatrix(vsV, wsV); }
468
470 void worldToLocalDir(const V3d &wsV, V3d &lsV) const
471 { m_wsToLs.multDirMatrix(wsV, lsV); }
472
474 void localToWorldDir(const V3d &lsV, V3d &wsV) const
475 { m_lsToWs.multDirMatrix(lsV, wsV); }
476
477 virtual void extentsChanged();
478
479 virtual std::string className() const;
480
481 virtual bool isIdentical(FieldMapping::Ptr other,
482 double tolerance = 0.0) const;
483
484 virtual V3d wsVoxelSize(int /*i*/, int /*j*/, int /*k*/) const
485 { return m_wsVoxelSize; }
486
487 virtual FieldMapping::Ptr clone() const;
488
490
491private:
492
494 void updateTransform();
495
497 void getLocalToVoxelMatrix(M44d &result);
498
499 // Data members -------------------------------------------------------------
500
513
518
522
526
527 // Typedefs ------------------------------------------------------------------
528
531};
532
533//----------------------------------------------------------------------------//
534// FrustumFieldMapping
535//----------------------------------------------------------------------------//
536
562//----------------------------------------------------------------------------//
563
565{
566public:
567
568 // Typedefs ------------------------------------------------------------------
569
571 typedef boost::intrusive_ptr<FrustumFieldMapping> Ptr;
576
577 // Exceptions ----------------------------------------------------------------
578
580
581 // Enums ---------------------------------------------------------------------
582
583
587 UniformDistribution
588 };
589
590 // RTTI replacement ----------------------------------------------------------
591
594
595 static const char* staticClassType()
596 {
597 return "FrustumFieldMapping";
598 }
599
600 // Ctors, dtor ---------------------------------------------------------------
601
604
606 FrustumFieldMapping(const Box3i &extents);
607
609
610 // Main methods --------------------------------------------------------------
611
618 void setTransforms(const M44d &ssToWs, const M44d &csToWs);
623 void setTransforms(float t, const M44d &ssToWs, const M44d &csToWs);
624
627 { m_zDistribution = dist; }
630 { return m_zDistribution; }
631
634 const M44d screenToWorld() const
635 { return m_ssToWsCurve.linear(0.0); }
636
639 const M44d cameraToWorld() const
640 { return m_csToWsCurve.linear(0.0); }
641
644 { return m_ssToWsCurve.samples(); }
645
648 { return m_csToWsCurve.samples(); }
649
652 { return m_nearCurve.samples(); }
653
656 { return m_farCurve.samples(); }
657
659 double nearPlane() const
660 { return m_nearCurve.linear(0.0); }
661
663 double farPlane() const
664 { return m_farCurve.linear(0.0); }
665
669 void reset();
670
671 // From FieldMapping ---------------------------------------------------------
672
675
676 virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const;
677 virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const;
678
679 virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const;
680 virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float time) const;
681
682 virtual void worldToLocal(const V3d &wsP, V3d &lsP) const;
683 virtual void worldToLocal(const V3d &wsP, V3d &lsP, float time) const;
684
685 virtual void localToWorld(const V3d &lsP, V3d &wsP) const;
686 virtual void localToWorld(const V3d &lsP, V3d &wsP, float time) const;
687
688 virtual void extentsChanged();
689
690 virtual std::string className() const;
691
692 virtual bool isIdentical(FieldMapping::Ptr other,
693 double tolerance = 0.0) const;
694
695 virtual V3d wsVoxelSize(int i, int j, int k) const;
696
697 virtual FieldMapping::Ptr clone() const;
698
700
701private:
702
704 void computeVoxelSize();
705
707 void getLocalToVoxelMatrix(M44d &result);
708
711 void clearCurves();
712
713 // Data members -------------------------------------------------------------
714
717
731
734 std::vector<V3d> m_wsVoxelSize;
735
741
742 // Typedefs ------------------------------------------------------------------
743
746
747};
748
749//----------------------------------------------------------------------------//
750
752
753//----------------------------------------------------------------------------//
754
755#endif // Include guard
Contains the Curve class which is used to interpolate attributes in time.
Contains Exception base class.
#define DECLARE_FIELD3D_GENERIC_EXCEPTION(name, base_class)
Used to declare a generic but named exception.
Definition Exception.h:107
void worldToVoxel(const Field3D::FieldMapping *mapping, const Box3d &wsBounds, Box3d &vsBounds)
Computes a voxel space bounds given a bounding box in world space. This is done by transforming each ...
void transformBounds(const M44d &mtx, const Box3d &fromBounds, Box3d &toBounds)
Transforms a bounding box by a 4x4 matrix This is done by transforming each corner vertex from world ...
bool isIdentical(typename Field< Data_T >::Ptr a, typename Field< Data_T >::Ptr b)
Checks whether the span and data in two different fields are identical.
Definition Field.h:1037
Contains base class for reference counting with Mutex.
Imath::Box3d Box3d
Definition SpiMathLib.h:79
Imath::V3d V3d
Definition SpiMathLib.h:74
Imath::Box3i Box3i
Definition SpiMathLib.h:77
Imath::M44d M44d
Definition SpiMathLib.h:82
Contains typedefs for the commonly used types in Field3D.
Implements a simple function curve where samples of type T can be added along a 1D axis....
Definition Curve.h:96
std::vector< Sample > SampleVec
Definition Curve.h:102
Base class for mapping between world-, local- and voxel coordinates.
V3d m_origin
The integer voxel-space origin of the underlying Field object. Is equal to field.extents....
const V3d & resolution() const
Returns the resolution.
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
virtual V3d wsVoxelSize(int i, int j, int k) const =0
Returns world-space size of a voxel at the specified coordinate.
virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const =0
Transform from voxel space position into world space.
virtual void localToWorld(const V3d &lsP, V3d &wsP, float time) const =0
virtual void extentsChanged()
Implement this if the subclass needs to update itself when the resolution changes.
RefBase base
Convenience typedef for referring to base class.
boost::intrusive_ptr< FieldMapping > Ptr
V3d m_res
The integer voxel-space resolution of the underlying Field object. Is equal to field....
FieldMapping class_type
virtual Ptr clone() const =0
Returns a pointer to a copy of the mapping, pure virtual so ensure derived classes properly implement...
virtual void worldToLocal(const V3d &wsP, V3d &lsP) const =0
Transform from world space position into local space.
const V3d & origin() const
Returns the origin.
virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const =0
Transform from world space position into voxel space.
virtual void localToWorld(const V3d &lsP, V3d &wsP) const =0
Transform from local space position into world space.
virtual bool isIdentical(FieldMapping::Ptr other, double tolerance=0.0) const =0
Whether the mapping is identical to another mapping.
virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float time) const =0
virtual void worldToLocal(const V3d &wsP, V3d &lsP, float time) const =0
static const char * staticClassType()
virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const =0
Represents the mapping of a field by a perspective transform.
ZDistribution
Enumerates the Z slice distribution. .f3d files will store values as an int, so be very careful not t...
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
FieldMapping base
Convenience typedef for referring to base class.
bool m_defaultState
Boolean to tell us if the mapping is in its 'default' state. This is needed because the class has a d...
const FloatCurve::SampleVec & farPlaneSamples() const
Returns a vector of all motion samples for far plane.
Curve< double > FloatCurve
Time-varying float.
const MatrixCurve::SampleVec & cameraToWorldSamples() const
Returns a vector of all motion samples for camera to world transform.
MatrixCurve m_ssToWsCurve
Time-varying local perspective to world space transform This is not used in calculations,...
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
void setZDistribution(ZDistribution dist)
Sets the z slice distribution.
const MatrixCurve::SampleVec & screenToWorldSamples() const
Returns a vector of all motion samples for screen to world transform.
std::vector< V3d > m_wsVoxelSize
Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency.
ZDistribution zDistribution() const
Returns the z slice distribution.
boost::intrusive_ptr< FrustumFieldMapping > Ptr
Convenience typedef.
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
const M44d screenToWorld() const
Returns a reference to the screen to world space transform.
double farPlane() const
Returns the far plane.
const M44d cameraToWorld() const
Returns a reference to the camera to world space transform.
Curve< Imath::M44d > MatrixCurve
Time-varying matrix.
FrustumFieldMapping class_type
ZDistribution m_zDistribution
Slice distribution type.
double nearPlane() const
Returns the near plane.
static const char * staticClassType()
const FloatCurve::SampleVec & nearPlaneSamples() const
Returns a vector of all motion samples for near plane.
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Represents the mapping of a field by a matrix transform.
MatrixCurve m_vsToWsCurve
Time-varying voxel to world space transform.
static const char * staticClassType()
virtual V3d wsVoxelSize(int, int, int) const
Returns world-space size of a voxel at the specified coordinate.
virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float time) const
Curve< Imath::M44d > MatrixCurve
Time-varying matrix.
void voxelToWorldDir(const V3d &vsV, V3d &wsV) const
const M44d & localToWorld() const
Returns a reference to the local to world transform.
virtual void localToWorld(const V3d &lsP, V3d &wsP) const
Transform from local space position into world space.
MatrixFieldMapping class_type
M44d m_wsToVs
World space to voxel space.
M44d m_wsToLs
World space to local space.
bool m_isTimeVarying
Stores whether the curve has more than one time sample.
boost::intrusive_ptr< MatrixFieldMapping > Ptr
Convenience typedef.
virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
Transform from voxel space position into world space.
virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
Transform from world space position into local space.
virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
Transform from world space position into voxel space.
virtual void worldToLocal(const V3d &wsP, V3d &lsP, float time) const
M44d m_vsToWs
Voxel space to world space.
virtual void localToWorld(const V3d &lsP, V3d &wsP, float time) const
virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float time) const
const M44d & voxelToWorld() const
Returns a reference to the voxel to world space transform.
M44d worldToVoxel(float time) const
Returns the world to voxel space transform at a given time.
const MatrixCurve::SampleVec & localToWorldSamples() const
Returns a vector of all motion samples for local to world transform.
MatrixCurve m_lsToWsCurve
Time-varying local to world space transform.
const M44d & worldToVoxel() const
Returns a reference to the world to voxel space transform.
V3d m_wsVoxelSize
Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency.
void localToWorldDir(const V3d &lsV, V3d &wsV) const
FieldMapping base
Convenience typedef for referring to base class.
void worldToLocalDir(const V3d &wsV, V3d &lsV) const
void worldToVoxelDir(const V3d &wsV, V3d &vsV) const
M44d m_lsToWs
Local space to world space.
Trivial class, world space is equal to local space, i.e. the field is contained in the unit cube [0....
FieldMapping base
Convenience typedef for referring to base class.
NullFieldMapping class_type
virtual void worldToVoxel(const V3d &wsP, V3d &vsP, float) const
boost::intrusive_ptr< NullFieldMapping > Ptr
Convenience typedef.
virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
Transform from world space position into local space.
virtual void localToWorld(const V3d &lsP, V3d &wsP) const
Transform from local space position into world space.
virtual V3d wsVoxelSize(int, int, int) const
Returns world-space size of a voxel at the specified coordinate.
virtual void worldToLocal(const V3d &wsP, V3d &lsP, float) const
virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
Transform from world space position into voxel space.
virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
Transform from voxel space position into world space.
virtual void localToWorld(const V3d &lsP, V3d &wsP, float) const
static const char * staticClassType()
NullFieldMapping(const Box3i &extents)
virtual void voxelToWorld(const V3d &vsP, V3d &wsP, float) const
#define FIELD3D_API
Definition ns.h:77
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition ns.h:58