Field3D
FrustumFieldMapping Class Reference

Represents the mapping of a field by a perspective transform. More...

#include <FieldMapping.h>

Inheritance diagram for FrustumFieldMapping:
FieldMapping RefBase

Public Types

typedef FrustumFieldMapping class_type
 
typedef Curve< double > FloatCurve
 Time-varying float. More...
 
typedef Curve< Imath::M44dMatrixCurve
 Time-varying matrix. More...
 
typedef boost::intrusive_ptr
< FrustumFieldMapping
Ptr
 Convenience typedef. More...
 
enum  ZDistribution { PerspectiveDistribution, UniformDistribution }
 Enumerates the Z slice distribution. .f3d files will store values as an int, so be very careful not to change the order of these. More...
 
- Public Types inherited from FieldMapping
typedef FieldMapping class_type
 
typedef boost::intrusive_ptr
< FieldMapping
Ptr
 
- Public Types inherited from RefBase
typedef boost::intrusive_ptr
< RefBase
Ptr
 

Public Member Functions

const M44d cameraToWorld () const
 Returns a reference to the camera to world space transform. More...
 
const MatrixCurve::SampleVeccameraToWorldSamples () const
 Returns a vector of all motion samples for camera to world transform. More...
 
double farPlane () const
 Returns the far plane. More...
 
const FloatCurve::SampleVecfarPlaneSamples () const
 Returns a vector of all motion samples for far plane. More...
 
double nearPlane () const
 Returns the near plane. More...
 
const FloatCurve::SampleVecnearPlaneSamples () const
 Returns a vector of all motion samples for near plane. More...
 
void reset ()
 Resets the transform. Makes a perspective transform at the origin, looking down the negative Z axis with a 45 degree FOV and square projection. More...
 
const M44d screenToWorld () const
 Returns a reference to the screen to world space transform. More...
 
const MatrixCurve::SampleVecscreenToWorldSamples () const
 Returns a vector of all motion samples for screen to world transform. More...
 
void setTransforms (const M44d &ssToWs, const M44d &csToWs)
 Sets the screenToWorld and cameraToWorld transforms. All other internal matrices will be updated based on these. More...
 
void setTransforms (float t, const M44d &ssToWs, const M44d &csToWs)
 Sets time-varying screenToWorld and cameraToWorld transforms. All other internal matrices will be updated based on these. More...
 
void setZDistribution (ZDistribution dist)
 Sets the z slice distribution. More...
 
ZDistribution zDistribution () const
 Returns the z slice distribution. More...
 
Constructors & destructor
 FrustumFieldMapping ()
 
 FrustumFieldMapping (const Box3i &extents)
 
From FieldMapping
virtual void worldToVoxel (const V3d &wsP, V3d &vsP) const
 Transform from world space position into voxel space. More...
 
virtual void worldToVoxel (const V3d &wsP, V3d &vsP, float time) const
 
virtual void voxelToWorld (const V3d &vsP, V3d &wsP) const
 Transform from voxel space position into world space. More...
 
virtual void voxelToWorld (const V3d &vsP, V3d &wsP, float time) const
 
virtual void worldToLocal (const V3d &wsP, V3d &lsP) const
 Transform from world space position into local space. More...
 
virtual void worldToLocal (const V3d &wsP, V3d &lsP, float time) const
 
virtual void localToWorld (const V3d &lsP, V3d &wsP) const
 Transform from local space position into world space. More...
 
virtual void localToWorld (const V3d &lsP, V3d &wsP, float time) const
 
virtual void extentsChanged ()
 Implement this if the subclass needs to update itself when the resolution changes. More...
 
virtual std::string className () const
 Returns the FieldMapping type name. Used when writing/reading from disk. More...
 
virtual bool isIdentical (FieldMapping::Ptr other, double tolerance=0.0) const
 Whether the mapping is identical to another mapping. More...
 
virtual V3d wsVoxelSize (int i, int j, int k) const
 Returns world-space size of a voxel at the specified coordinate. More...
 
virtual FieldMapping::Ptr clone () const
 Returns a pointer to a copy of the mapping, pure virtual so ensure derived classes properly implement it. More...
 
- Public Member Functions inherited from FieldMapping
const V3dorigin () const
 Returns the origin. More...
 
const V3dresolution () const
 Returns the resolution. More...
 
void setExtents (const Box3i &extents)
 This sets the field extents information to use for defining the local coordinate space. More...
 
 FieldMapping ()
 Constructor. More...
 
 FieldMapping (const Box3i &extents)
 Construct with known extents. More...
 
virtual ~FieldMapping ()
 Destructor. More...
 
void localToVoxel (const V3d &lsP, V3d &vsP) const
 Transform from local space to voxel space. This is just a multiplication by the resolution of the Field that we're mapping. More...
 
void voxelToLocal (const V3d &vsP, V3d &lsP) const
 Inverse of localToVoxel. More...
 
- Public Member Functions inherited from RefBase
void ref () const
 Used by boost::intrusive_pointer. More...
 
size_t refcnt ()
 Used by boost::intrusive_pointer. More...
 
void unref () const
 Used by boost::intrusive_pointer. More...
 
 RefBase ()
 
 RefBase (const RefBase &)
 Copy constructor. More...
 
RefBaseoperator= (const RefBase &)
 Assignment operator. More...
 
virtual ~RefBase ()
 Destructor. More...
 
virtual bool checkRTTI (const char *typenameStr)=0
 This function is only implemented by concrete classes and triggers the actual RTTI check through matchRTTI();. More...
 
bool matchRTTI (const char *typenameStr)
 Performs a check to see if the given typename string matches this class' This needs to be implemented in -all- subclasses, even abstract ones. More...
 

Static Public Member Functions

static const char * classType ()
 
- Static Public Member Functions inherited from FieldMapping
static const char * classType ()
 
- Static Public Member Functions inherited from RefBase
static const char * classType ()
 

Public Attributes

 DEFINE_FIELD_RTTI_CONCRETE_CLASS
 
- Public Attributes inherited from FieldMapping
 DEFINE_FIELD_RTTI_ABSTRACT_CLASS
 

Private Types

typedef FieldMapping base
 Convenience typedef for referring to base class. More...
 

Private Member Functions

void clearCurves ()
 Clears all Curve data members. Used by setTransforms() to prepare for the first sample to be added. More...
 
void computeVoxelSize ()
 Updates the local to world transformation matrix. More...
 
void getLocalToVoxelMatrix (M44d &result)
 

Private Attributes

MatrixCurve m_csToWsCurve
 Time-varying camera to world space transform. More...
 
bool m_defaultState
 Boolean to tell us if the mapping is in its 'default' state. This is needed because the class has a default configuration where there is a single sample in all the curves. Once a new transform is set through setTransforms(), the default samples must be cleared. More...
 
FloatCurve m_farCurve
 Time-varying far plane. Computed from m_lpsToWsCurve. More...
 
MatrixCurve m_lpsToWsCurve
 Time-varying local perspective to world space transform. Computed from m_ssToWsCurve. More...
 
FloatCurve m_nearCurve
 Time-varying near plane. Computed from m_lpsToWsCurve. More...
 
MatrixCurve m_ssToWsCurve
 Time-varying local perspective to world space transform This is not used in calculations, but rather as the public interface to the class. More...
 
std::vector< V3dm_wsVoxelSize
 Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency. More...
 
ZDistribution m_zDistribution
 Slice distribution type. More...
 

Additional Inherited Members

- Protected Attributes inherited from FieldMapping
V3d m_origin
 The integer voxel-space origin of the underlying Field object. Is equal to field.extents.min. More...
 
V3d m_res
 The integer voxel-space resolution of the underlying Field object. Is equal to field.extents.max - field.extents.min + 1. More...
 

Detailed Description

Represents the mapping of a field by a perspective transform.

Refer to using_mappings for examples of how to use this in your code.

Frustum mappings can use two approaches in determining the distribution of "Z slices". By transforming from world space into screen space and using the Z component in perspective space, the slices in Z will be distributed in world space accordingly. It is also possible to use a uniform distribution of Z slices by specifying a near and far clip plane and normalizing the camera-space Z distance between those.

Note
Screen space is defined left-handed as [-1.0,1.0] in all three dimensions
Camera space is defined right-handed with the camera looking down negative Z.
Todo:
Define local perspective space
Note
Regarding time-varying matrices. If setTransforms() is called, an underlying Curve object is created with just one sample at time=0.0.

Definition at line 536 of file FieldMapping.h.

Member Typedef Documentation

typedef boost::intrusive_ptr<FrustumFieldMapping> FrustumFieldMapping::Ptr

Convenience typedef.

Definition at line 543 of file FieldMapping.h.

Time-varying matrix.

Definition at line 545 of file FieldMapping.h.

Time-varying float.

Definition at line 547 of file FieldMapping.h.

Convenience typedef for referring to base class.

Definition at line 717 of file FieldMapping.h.

Member Enumeration Documentation

Enumerates the Z slice distribution. .f3d files will store values as an int, so be very careful not to change the order of these.

Enumerator
PerspectiveDistribution 
UniformDistribution 

Definition at line 557 of file FieldMapping.h.

Constructor & Destructor Documentation

FrustumFieldMapping::FrustumFieldMapping ( )

Definition at line 366 of file FieldMapping.cpp.

References reset().

Referenced by clone().

367  : FieldMapping(),
369  m_defaultState(true)
370 {
371  reset();
372 }
FieldMapping()
Constructor.
void reset()
Resets the transform. Makes a perspective transform at the origin, looking down the negative Z axis w...
bool m_defaultState
Boolean to tell us if the mapping is in its 'default' state. This is needed because the class has a d...
Definition: FieldMapping.h:712
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:688
FrustumFieldMapping::FrustumFieldMapping ( const Box3i extents)

Definition at line 376 of file FieldMapping.cpp.

References reset().

377  : FieldMapping(extents)
378 {
379  reset();
380 }
FieldMapping()
Constructor.
void reset()
Resets the transform. Makes a perspective transform at the origin, looking down the negative Z axis w...

Member Function Documentation

static const char* FrustumFieldMapping::classType ( )
inlinestatic

Definition at line 567 of file FieldMapping.h.

Referenced by className().

568  {
569  return "FrustumFieldMapping";
570  }
void FrustumFieldMapping::setTransforms ( const M44d ssToWs,
const M44d csToWs 
)

Sets the screenToWorld and cameraToWorld transforms. All other internal matrices will be updated based on these.

Note
This resets the transform Curve instances to contain just one sample at time=0.0
Parameters
ssToWsSee class documentation for definition.
csToWsSee class documentation for definition.

Definition at line 384 of file FieldMapping.cpp.

Referenced by reset().

385 {
386  setTransforms(0.0, ssToWs, csToWs);
387 }
void setTransforms(const M44d &ssToWs, const M44d &csToWs)
Sets the screenToWorld and cameraToWorld transforms. All other internal matrices will be updated base...
void FrustumFieldMapping::setTransforms ( float  t,
const M44d ssToWs,
const M44d csToWs 
)

Sets time-varying screenToWorld and cameraToWorld transforms. All other internal matrices will be updated based on these.

Parameters
ssToWsSee class documentation for definition.
csToWsSee class documentation for definition.

Definition at line 391 of file FieldMapping.cpp.

References Curve< T >::addSample(), clearCurves(), computeVoxelSize(), m_csToWsCurve, m_defaultState, m_farCurve, m_lpsToWsCurve, m_nearCurve, and m_ssToWsCurve.

393 {
394  if (m_defaultState) {
395  clearCurves();
396  m_defaultState = false;
397  }
398 
399  // Construct local-to-world transform from ssToWs
400  M44d lsToSs, scale, translation;
401  scale.setScale(V3d(2.0, 2.0, 1.0));
402  translation.setTranslation(V3d(-1.0, -1.0, 0.0));
403  lsToSs = scale * translation;
404  M44d lpsToWs = lsToSs * ssToWs;
405 
406  // Add samples to Curves
407  m_ssToWsCurve.addSample(t, ssToWs);
408  m_lpsToWsCurve.addSample(t, lpsToWs);
409  m_csToWsCurve.addSample(t, csToWs);
410 
411  // Compute near and far planes ---
412 
413  // Because the frustum may be skewed we can't just measure distance from
414  // the apex of the frustum to the world-space center point of the frustum.
415  // Instead, we transform into camera space and measure z depth there.
416 
417  V3d lsNearP(0.5, 0.5, 0.0), lsFarP(0.5, 0.5, 1.0);
418  V3d wsNearP, wsFarP, csNearP, csFarP;
419 
420  lpsToWs.multVecMatrix(lsNearP, wsNearP);
421  lpsToWs.multVecMatrix(lsFarP, wsFarP);
422 
423  M44d wsToCs = csToWs.inverse();
424  wsToCs.multVecMatrix(wsNearP, csNearP);
425  wsToCs.multVecMatrix(wsFarP, csFarP);
426 
427  double near = -csNearP.z;
428  double far = -csFarP.z;
429 
430  // Catch NaN here
431  if (isnan(near) || isnan(far)) {
432  throw BadPerspectiveMatrix("FrustumFieldMapping::setTransforms "
433  "received bad screen-to-world matrix");
434  }
435 
436  m_nearCurve.addSample(t, near);
437  m_farCurve.addSample(t, far);
438 
440 }
Imath::M44d M44d
Definition: SpiMathLib.h:82
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:702
void clearCurves()
Clears all Curve data members. Used by setTransforms() to prepare for the first sample to be added...
void computeVoxelSize()
Updates the local to world transformation matrix.
void addSample(const float t, const T &value)
Adds a sample point to the curve.
Definition: Curve.h:172
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:698
bool m_defaultState
Boolean to tell us if the mapping is in its 'default' state. This is needed because the class has a d...
Definition: FieldMapping.h:712
Imath::V3d V3d
Definition: SpiMathLib.h:74
MatrixCurve m_ssToWsCurve
Time-varying local perspective to world space transform This is not used in calculations, but rather as the public interface to the class.
Definition: FieldMapping.h:693
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:700
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:695
void FrustumFieldMapping::setZDistribution ( ZDistribution  dist)
inline

Sets the z slice distribution.

Definition at line 598 of file FieldMapping.h.

References m_zDistribution.

599  { m_zDistribution = dist; }
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:688
ZDistribution FrustumFieldMapping::zDistribution ( ) const
inline

Returns the z slice distribution.

Definition at line 601 of file FieldMapping.h.

References m_zDistribution.

602  { return m_zDistribution; }
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:688
const M44d FrustumFieldMapping::screenToWorld ( ) const
inline

Returns a reference to the screen to world space transform.

Note
This assumes the query to be at time=0.0

Definition at line 606 of file FieldMapping.h.

References Curve< T >::linear(), and m_ssToWsCurve.

607  { return m_ssToWsCurve.linear(0.0); }
MatrixCurve m_ssToWsCurve
Time-varying local perspective to world space transform This is not used in calculations, but rather as the public interface to the class.
Definition: FieldMapping.h:693
T linear(const float t) const
Linearly interpolates a value from the curve.
Definition: Curve.h:199
const M44d FrustumFieldMapping::cameraToWorld ( ) const
inline

Returns a reference to the camera to world space transform.

Note
This assumes the query to be at time=0.0

Definition at line 611 of file FieldMapping.h.

References Curve< T >::linear(), and m_csToWsCurve.

612  { return m_csToWsCurve.linear(0.0); }
T linear(const float t) const
Linearly interpolates a value from the curve.
Definition: Curve.h:199
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:695
const MatrixCurve::SampleVec& FrustumFieldMapping::screenToWorldSamples ( ) const
inline

Returns a vector of all motion samples for screen to world transform.

Definition at line 615 of file FieldMapping.h.

References m_ssToWsCurve, and Curve< T >::samples().

616  { return m_ssToWsCurve.samples(); }
MatrixCurve m_ssToWsCurve
Time-varying local perspective to world space transform This is not used in calculations, but rather as the public interface to the class.
Definition: FieldMapping.h:693
const SampleVec & samples() const
Returns a const reference to the samples in the curve.
Definition: Curve.h:103
const MatrixCurve::SampleVec& FrustumFieldMapping::cameraToWorldSamples ( ) const
inline

Returns a vector of all motion samples for camera to world transform.

Definition at line 619 of file FieldMapping.h.

References m_csToWsCurve, and Curve< T >::samples().

620  { return m_csToWsCurve.samples(); }
const SampleVec & samples() const
Returns a const reference to the samples in the curve.
Definition: Curve.h:103
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:695
const FloatCurve::SampleVec& FrustumFieldMapping::nearPlaneSamples ( ) const
inline

Returns a vector of all motion samples for near plane.

Definition at line 623 of file FieldMapping.h.

References m_nearCurve, and Curve< T >::samples().

624  { return m_nearCurve.samples(); }
const SampleVec & samples() const
Returns a const reference to the samples in the curve.
Definition: Curve.h:103
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:700
const FloatCurve::SampleVec& FrustumFieldMapping::farPlaneSamples ( ) const
inline

Returns a vector of all motion samples for far plane.

Definition at line 627 of file FieldMapping.h.

References m_farCurve, and Curve< T >::samples().

628  { return m_farCurve.samples(); }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:702
const SampleVec & samples() const
Returns a const reference to the samples in the curve.
Definition: Curve.h:103
double FrustumFieldMapping::nearPlane ( ) const
inline

Returns the near plane.

Definition at line 631 of file FieldMapping.h.

References Curve< T >::linear(), and m_nearCurve.

632  { return m_nearCurve.linear(0.0); }
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:700
T linear(const float t) const
Linearly interpolates a value from the curve.
Definition: Curve.h:199
double FrustumFieldMapping::farPlane ( ) const
inline

Returns the far plane.

Definition at line 635 of file FieldMapping.h.

References Curve< T >::linear(), and m_farCurve.

636  { return m_farCurve.linear(0.0); }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:702
T linear(const float t) const
Linearly interpolates a value from the curve.
Definition: Curve.h:199
void FrustumFieldMapping::reset ( )

Resets the transform. Makes a perspective transform at the origin, looking down the negative Z axis with a 45 degree FOV and square projection.

Definition at line 444 of file FieldMapping.cpp.

References clearCurves(), computeVoxelSize(), m_defaultState, and setTransforms().

Referenced by FrustumFieldMapping().

445 {
446  // Default camera to world ---
447 
448  M44d csToWs;
449  csToWs.makeIdentity();
450 
451  // Default screen to world ---
452 
453  double near = 1;
454  double far = 2;
455  double fovRadians = 45.0 * M_PI / 180.0;
456  double invTan = 1.0 / std::tan(fovRadians / 2.0);
457  double imageAspectRatio = 1.0;
458 
459  M44d perspective(1, 0, 0, 0,
460  0, 1, 0, 0,
461  0, 0, (far) / (far - near), 1,
462  0, 0, (- far * near) / (far - near), 0);
463 
464  M44d fov;
465  fov.setScale(V3d(invTan / imageAspectRatio, invTan, 1.0));
466 
467  M44d flipZ;
468  flipZ.setScale(V3d(1.0, 1.0, -1.0));
469 
470  M44d csToSs = flipZ * perspective * fov;
471 
472  M44d standardSsToWs = csToSs.inverse() * csToWs;
473 
474  // Set default state ---
475 
476  clearCurves();
477  setTransforms(standardSsToWs, csToWs);
478 
479  m_defaultState = true;
480 
482 }
Imath::M44d M44d
Definition: SpiMathLib.h:82
void clearCurves()
Clears all Curve data members. Used by setTransforms() to prepare for the first sample to be added...
void computeVoxelSize()
Updates the local to world transformation matrix.
bool m_defaultState
Boolean to tell us if the mapping is in its 'default' state. This is needed because the class has a d...
Definition: FieldMapping.h:712
Imath::V3d V3d
Definition: SpiMathLib.h:74
void setTransforms(const M44d &ssToWs, const M44d &csToWs)
Sets the screenToWorld and cameraToWorld transforms. All other internal matrices will be updated base...
void FrustumFieldMapping::worldToVoxel ( const V3d wsP,
V3d vsP 
) const
virtual

Transform from world space position into voxel space.

Implements FieldMapping.

Definition at line 493 of file FieldMapping.cpp.

494 {
495  worldToVoxel(wsP, vsP, 0.0);
496 }
virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
Transform from world space position into voxel space.
void FrustumFieldMapping::worldToVoxel ( const V3d wsP,
V3d vsP,
float  time 
) const
virtual

Implements FieldMapping.

Definition at line 500 of file FieldMapping.cpp.

References FieldMapping::localToVoxel(), and worldToLocal().

501 {
502  V3d lsP;
503  worldToLocal(wsP, lsP, time);
504  localToVoxel(lsP, vsP);
505 }
void localToVoxel(const V3d &lsP, V3d &vsP) const
Transform from local space to voxel space. This is just a multiplication by the resolution of the Fie...
Imath::V3d V3d
Definition: SpiMathLib.h:74
virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
Transform from world space position into local space.
void FrustumFieldMapping::voxelToWorld ( const V3d vsP,
V3d wsP 
) const
virtual

Transform from voxel space position into world space.

Implements FieldMapping.

Definition at line 509 of file FieldMapping.cpp.

Referenced by computeVoxelSize().

510 {
511  voxelToWorld(vsP, wsP, 0.0);
512 }
virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
Transform from voxel space position into world space.
void FrustumFieldMapping::voxelToWorld ( const V3d vsP,
V3d wsP,
float  time 
) const
virtual

Implements FieldMapping.

Definition at line 516 of file FieldMapping.cpp.

References localToWorld(), and FieldMapping::voxelToLocal().

517 {
518  V3d lsP;
519  voxelToLocal(vsP, lsP);
520  localToWorld(lsP, wsP, time);
521 }
virtual void localToWorld(const V3d &lsP, V3d &wsP) const
Transform from local space position into world space.
void voxelToLocal(const V3d &vsP, V3d &lsP) const
Inverse of localToVoxel.
Imath::V3d V3d
Definition: SpiMathLib.h:74
void FrustumFieldMapping::worldToLocal ( const V3d wsP,
V3d lsP 
) const
virtual

Transform from world space position into local space.

Implements FieldMapping.

Definition at line 525 of file FieldMapping.cpp.

Referenced by worldToVoxel().

526 {
527  worldToLocal(wsP, lsP, 0.0);
528 }
virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
Transform from world space position into local space.
void FrustumFieldMapping::worldToLocal ( const V3d wsP,
V3d lsP,
float  time 
) const
virtual

Implements FieldMapping.

Definition at line 532 of file FieldMapping.cpp.

References FIELD3D_LERPFACTOR, Curve< T >::linear(), m_csToWsCurve, m_farCurve, m_lpsToWsCurve, m_nearCurve, m_zDistribution, PerspectiveDistribution, and UniformDistribution.

533 {
534  switch (m_zDistribution) {
535  case UniformDistribution:
536  {
537  // First transform to local perspective space
538  V3d lpsP;
539  m_lpsToWsCurve.linear(time).inverse().multVecMatrix(wsP, lpsP);
540  // Also transform to camera space
541  V3d csP;
542  m_csToWsCurve.linear(time).inverse().multVecMatrix(wsP, csP);
543  // Interpolate near and far plane at current time
544  double near = m_nearCurve.linear(time);
545  double far = m_farCurve.linear(time);
546  // Use perspective-space X/Y and normalized depth for Z.
547  lsP = V3d(lpsP.x, lpsP.y, FIELD3D_LERPFACTOR(-csP.z, near, far));
548  break;
549  }
551  default:
552  {
553  m_lpsToWsCurve.linear(time).inverse().multVecMatrix(wsP, lsP);
554  break;
555  }
556  }
557 }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:702
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:698
Imath::V3d V3d
Definition: SpiMathLib.h:74
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:700
T linear(const float t) const
Linearly interpolates a value from the curve.
Definition: Curve.h:199
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:695
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:688
#define FIELD3D_LERPFACTOR
Definition: SpiMathLib.h:92
void FrustumFieldMapping::localToWorld ( const V3d lsP,
V3d wsP 
) const
virtual

Transform from local space position into world space.

Implements FieldMapping.

Definition at line 561 of file FieldMapping.cpp.

Referenced by voxelToWorld().

562 {
563  localToWorld(lsP, wsP, 0.0);
564 }
virtual void localToWorld(const V3d &lsP, V3d &wsP) const
Transform from local space position into world space.
void FrustumFieldMapping::localToWorld ( const V3d lsP,
V3d wsP,
float  time 
) const
virtual

Implements FieldMapping.

Definition at line 568 of file FieldMapping.cpp.

References FIELD3D_LERP, Curve< T >::linear(), m_csToWsCurve, m_farCurve, m_lpsToWsCurve, m_nearCurve, m_zDistribution, PerspectiveDistribution, and UniformDistribution.

569 {
570  switch (m_zDistribution) {
571  case UniformDistribution:
572  {
573  // Interpolate near and far plane at current time
574  double near = m_nearCurve.linear(time);
575  double far = m_farCurve.linear(time);
576  // In this case, local space is -not- equal to local perspective space
577  // Determine distance from camera
578  double wsDepthFromCam = FIELD3D_LERP(near, far, lsP.z);
579  // Transform point right in front of camera, X units away into world space
580  V3d lpsCenterP, wsCenterP, csCenterP(0.0, 0.0, -wsDepthFromCam);
581  m_csToWsCurve.linear(time).multVecMatrix(csCenterP, wsCenterP);
582  // Transform center point into screen space so we know what depth
583  // (in screen space) the voxel would live at -if- it were in local
584  // perspective space.
585  m_lpsToWsCurve.linear(time).inverse().multVecMatrix(wsCenterP, lpsCenterP);
586  // Now we create a local perspective coordinate that can be transformed
587  // using m_lpsToWsCurve
588  V3d lpsP(lsP.x, lsP.y, lpsCenterP.z);
589  // Now we can use m_lpsToWsCurve to transform the actual voxel location
590  m_lpsToWsCurve.linear(time).multVecMatrix(lpsP, wsP);
591  break;
592  }
594  default:
595  {
596  // In this case, local space and local perspective space are the same.
597  m_lpsToWsCurve.linear(time).multVecMatrix(lsP, wsP);
598  break;
599  }
600  }
601 }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:702
#define FIELD3D_LERP
Definition: SpiMathLib.h:91
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:698
Imath::V3d V3d
Definition: SpiMathLib.h:74
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:700
T linear(const float t) const
Linearly interpolates a value from the curve.
Definition: Curve.h:199
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:695
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:688
void FrustumFieldMapping::extentsChanged ( )
virtual

Implement this if the subclass needs to update itself when the resolution changes.

Reimplemented from FieldMapping.

Definition at line 486 of file FieldMapping.cpp.

References computeVoxelSize().

487 {
489 }
void computeVoxelSize()
Updates the local to world transformation matrix.
std::string FrustumFieldMapping::className ( ) const
virtual

Returns the FieldMapping type name. Used when writing/reading from disk.

Implements FieldMapping.

Definition at line 605 of file FieldMapping.cpp.

References classType().

606 {
607  return std::string(classType());
608 }
static const char * classType()
Definition: FieldMapping.h:567
bool FrustumFieldMapping::isIdentical ( FieldMapping::Ptr  other,
double  tolerance = 0.0 
) const
virtual

Whether the mapping is identical to another mapping.

Implements FieldMapping.

Definition at line 612 of file FieldMapping.cpp.

References FIELD3D_NAMESPACE_OPEN::checkMatricesIdentical(), FIELD3D_NAMESPACE_OPEN::k_frustumMappingName(), m_csToWsCurve, m_lpsToWsCurve, m_zDistribution, and Curve< T >::samples().

614 {
616 
617  if (other->className() != k_frustumMappingName) {
618  return false;
619  } else {
620 
622  FIELD_DYNAMIC_CAST<FrustumFieldMapping>(other);
623 
624  if (fm) {
625 
626  const SampleVec lpsToWs1 = m_lpsToWsCurve.samples();
627  const SampleVec lpsToWs2 = fm->m_lpsToWsCurve.samples();
628  const SampleVec csToWs1 = m_csToWsCurve.samples();
629  const SampleVec csToWs2 = fm->m_csToWsCurve.samples();
630 
631  size_t numSamples = lpsToWs1.size();
632 
633  // Check that slice distributions match
634  if (m_zDistribution != fm->m_zDistribution) {
635  return false;
636  }
637 
638  // First check if time sample counts differ
639  // lpsToWs and csToWs are guaranteed to have same sample count.
640  if (lpsToWs1.size() != lpsToWs2.size()) {
641  return false;
642  }
643 
644  // Then check if all time samples match, then check localToWorld
645  // and voxelToWorld matrices
646  for (size_t i = 0; i < numSamples; ++i) {
647  if (lpsToWs1[i].first != lpsToWs2[i].first) {
648  return false;
649  }
650  if (!checkMatricesIdentical(lpsToWs1[i].second, lpsToWs2[i].second,
651  tolerance)) {
652  return false;
653  }
654  if (!checkMatricesIdentical(csToWs1[i].second, csToWs2[i].second,
655  tolerance)) {
656  return false;
657  }
658  }
659 
660  return true;
661 
662  } else {
663  return false;
664  }
665  }
666  return false;
667 }
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:698
const SampleVec & samples() const
Returns a const reference to the samples in the curve.
Definition: Curve.h:103
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:695
std::vector< Sample > SampleVec
Definition: Curve.h:85
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:688
bool checkMatricesIdentical(const Matrix_T &m1, const Matrix_T &m2, double tolerance)
boost::intrusive_ptr< FrustumFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:543
const string k_frustumMappingName("FrustumFieldMapping")
V3d FrustumFieldMapping::wsVoxelSize ( int  i,
int  j,
int  k 
) const
virtual

Returns world-space size of a voxel at the specified coordinate.

Implements FieldMapping.

Definition at line 671 of file FieldMapping.cpp.

References FieldMapping::m_origin, FieldMapping::m_res, and m_wsVoxelSize.

672 {
673  k = std::min(std::max(k, static_cast<int>(m_origin.z)),
674  static_cast<int>(m_origin.z + m_res.z - 1));
675  return m_wsVoxelSize[k - static_cast<int>(m_origin.z)];
676 }
V3d m_origin
The integer voxel-space origin of the underlying Field object. Is equal to field.extents.min.
Definition: FieldMapping.h:190
V3d m_res
The integer voxel-space resolution of the underlying Field object. Is equal to field.extents.max - field.extents.min + 1.
Definition: FieldMapping.h:193
std::vector< V3d > m_wsVoxelSize
Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency...
Definition: FieldMapping.h:706
FieldMapping::Ptr FrustumFieldMapping::clone ( ) const
virtual

Returns a pointer to a copy of the mapping, pure virtual so ensure derived classes properly implement it.

Implements FieldMapping.

Definition at line 740 of file FieldMapping.cpp.

References FrustumFieldMapping().

741 {
742  return Ptr(new FrustumFieldMapping(*this));
743 }
boost::intrusive_ptr< FrustumFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:543
void FrustumFieldMapping::computeVoxelSize ( )
private

Updates the local to world transformation matrix.

Definition at line 680 of file FieldMapping.cpp.

References discToCont(), FieldMapping::m_origin, FieldMapping::m_res, m_wsVoxelSize, and voxelToWorld().

Referenced by extentsChanged(), reset(), and setTransforms().

681 {
682  // Precompute the voxel size ---
683 
684  m_wsVoxelSize.resize(static_cast<int>(m_res.z),V3d(0.0));
685 
686  int i = m_origin.x + m_res.x / 2;
687  int j = m_origin.y + m_res.y / 2;
688 
689  // Do all z slices except last
690  int zMin = static_cast<int>(m_origin.z);
691  int zMax = static_cast<int>(m_origin.z + m_res.z - 1);
692 
693  for (int k = zMin, idx = 0; k < zMax; ++k, ++idx) {
694  V3d wsP, wsPx, wsPy, wsPz;
695  V3d vsP = discToCont(V3i(i, j, k));
696  V3d vsPx = discToCont(V3i(i + 1, j, k));
697  V3d vsPy = discToCont(V3i(i, j + 1, k));
698  V3d vsPz = discToCont(V3i(i, j, k + 1));
699  voxelToWorld(vsP, wsP);
700  voxelToWorld(vsPx, wsPx);
701  voxelToWorld(vsPy, wsPy);
702  voxelToWorld(vsPz, wsPz);
703  m_wsVoxelSize[idx] = V3d((wsPx - wsP).length(),
704  (wsPy - wsP).length(),
705  (wsPz - wsP).length());
706  }
707 
708  // Duplicate last value since there are no further slices to differentiate
709  // against
710  if (m_res.z >= 2) {
711  m_wsVoxelSize[m_res.z - 1] = m_wsVoxelSize[m_res.z - 2];
712  }
713 
714 }
V3d m_origin
The integer voxel-space origin of the underlying Field object. Is equal to field.extents.min.
Definition: FieldMapping.h:190
virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
Transform from voxel space position into world space.
V3d m_res
The integer voxel-space resolution of the underlying Field object. Is equal to field.extents.max - field.extents.min + 1.
Definition: FieldMapping.h:193
Imath::V3i V3i
Definition: SpiMathLib.h:71
std::vector< V3d > m_wsVoxelSize
Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency...
Definition: FieldMapping.h:706
Imath::V3d V3d
Definition: SpiMathLib.h:74
double discToCont(int discCoord)
Goes from discrete coordinates to continuous coordinates See Graphics Gems - What is a pixel...
Definition: Field.h:1027
void FrustumFieldMapping::getLocalToVoxelMatrix ( M44d result)
private
Todo:
Unit test this

Definition at line 718 of file FieldMapping.cpp.

References FieldMapping::m_origin, and FieldMapping::m_res.

719 {
720  // Local to voxel is a scale by the resolution of the field, offset
721  // to the origin of the extents
722  M44d scaling, translation;
723  scaling.setScale(m_res);
724  translation.setTranslation(m_origin);
725  result = scaling * translation;
726 }
Imath::M44d M44d
Definition: SpiMathLib.h:82
V3d m_origin
The integer voxel-space origin of the underlying Field object. Is equal to field.extents.min.
Definition: FieldMapping.h:190
V3d m_res
The integer voxel-space resolution of the underlying Field object. Is equal to field.extents.max - field.extents.min + 1.
Definition: FieldMapping.h:193
void FrustumFieldMapping::clearCurves ( )
private

Clears all Curve data members. Used by setTransforms() to prepare for the first sample to be added.

Definition at line 730 of file FieldMapping.cpp.

References Curve< T >::clear(), m_csToWsCurve, m_farCurve, m_lpsToWsCurve, and m_nearCurve.

Referenced by reset(), and setTransforms().

731 {
734  m_nearCurve.clear();
735  m_farCurve.clear();
736 }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:702
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:698
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:700
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:695
void clear()
Clears all samples in curve.
Definition: Curve.h:107

Member Data Documentation

FrustumFieldMapping::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 565 of file FieldMapping.h.

ZDistribution FrustumFieldMapping::m_zDistribution
private

Slice distribution type.

Definition at line 688 of file FieldMapping.h.

Referenced by isIdentical(), localToWorld(), setZDistribution(), worldToLocal(), and zDistribution().

MatrixCurve FrustumFieldMapping::m_ssToWsCurve
private

Time-varying local perspective to world space transform This is not used in calculations, but rather as the public interface to the class.

Definition at line 693 of file FieldMapping.h.

Referenced by screenToWorld(), screenToWorldSamples(), and setTransforms().

MatrixCurve FrustumFieldMapping::m_csToWsCurve
private

Time-varying camera to world space transform.

Definition at line 695 of file FieldMapping.h.

Referenced by cameraToWorld(), cameraToWorldSamples(), clearCurves(), isIdentical(), localToWorld(), setTransforms(), and worldToLocal().

MatrixCurve FrustumFieldMapping::m_lpsToWsCurve
private

Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.

Definition at line 698 of file FieldMapping.h.

Referenced by clearCurves(), isIdentical(), localToWorld(), setTransforms(), and worldToLocal().

FloatCurve FrustumFieldMapping::m_nearCurve
private

Time-varying near plane. Computed from m_lpsToWsCurve.

Definition at line 700 of file FieldMapping.h.

Referenced by clearCurves(), localToWorld(), nearPlane(), nearPlaneSamples(), setTransforms(), and worldToLocal().

FloatCurve FrustumFieldMapping::m_farCurve
private

Time-varying far plane. Computed from m_lpsToWsCurve.

Definition at line 702 of file FieldMapping.h.

Referenced by clearCurves(), farPlane(), farPlaneSamples(), localToWorld(), setTransforms(), and worldToLocal().

std::vector<V3d> FrustumFieldMapping::m_wsVoxelSize
private

Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency.

Definition at line 706 of file FieldMapping.h.

Referenced by computeVoxelSize(), and wsVoxelSize().

bool FrustumFieldMapping::m_defaultState
private

Boolean to tell us if the mapping is in its 'default' state. This is needed because the class has a default configuration where there is a single sample in all the curves. Once a new transform is set through setTransforms(), the default samples must be cleared.

Definition at line 712 of file FieldMapping.h.

Referenced by reset(), and setTransforms().


The documentation for this class was generated from the following files: