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< FrustumFieldMappingPtr
 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< FieldMappingPtr
 
- Public Types inherited from RefBase
typedef boost::intrusive_ptr< RefBasePtr
 
typedef boost::weak_ptr< RefBaseWeakPtr
 

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...
 
WeakPtr weakPtr () const
 
 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 * staticClassType ()
 
- Static Public Member Functions inherited from FieldMapping
static const char * staticClassType ()
 
- Static Public Member Functions inherited from RefBase
static const char * staticClassType ()
 

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 547 of file FieldMapping.h.

Member Typedef Documentation

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

Convenience typedef.

Definition at line 554 of file FieldMapping.h.

Time-varying matrix.

Definition at line 556 of file FieldMapping.h.

Time-varying float.

Definition at line 558 of file FieldMapping.h.

Convenience typedef for referring to base class.

Definition at line 728 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 568 of file FieldMapping.h.

Constructor & Destructor Documentation

FrustumFieldMapping::FrustumFieldMapping ( )

Definition at line 399 of file FieldMapping.cpp.

References reset().

Referenced by clone().

400  : FieldMapping(),
402  m_defaultState(true)
403 {
404  reset();
405 }
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 &#39;default&#39; state. This is needed because the class has a d...
Definition: FieldMapping.h:723
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:699
FrustumFieldMapping::FrustumFieldMapping ( const Box3i extents)

Definition at line 409 of file FieldMapping.cpp.

References reset().

410  : FieldMapping(extents)
411 {
412  reset();
413 }
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::staticClassType ( )
inlinestatic

Definition at line 578 of file FieldMapping.h.

Referenced by className().

579  {
580  return "FrustumFieldMapping";
581  }
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 417 of file FieldMapping.cpp.

Referenced by reset().

418 {
419  setTransforms(0.0, ssToWs, csToWs);
420 }
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 424 of file FieldMapping.cpp.

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

426 {
427  if (m_defaultState) {
428  clearCurves();
429  m_defaultState = false;
430  }
431 
432  // Construct local-to-world transform from ssToWs
433  M44d lsToSs, scale, translation;
434  scale.setScale(V3d(2.0, 2.0, 1.0));
435  translation.setTranslation(V3d(-1.0, -1.0, 0.0));
436  lsToSs = scale * translation;
437  M44d lpsToWs = lsToSs * ssToWs;
438 
439  // Add samples to Curves
440  m_ssToWsCurve.addSample(t, ssToWs);
441  m_lpsToWsCurve.addSample(t, lpsToWs);
442  m_csToWsCurve.addSample(t, csToWs);
443 
444  // Compute near and far planes ---
445 
446  // Because the frustum may be skewed we can't just measure distance from
447  // the apex of the frustum to the world-space center point of the frustum.
448  // Instead, we transform into camera space and measure z depth there.
449 
450  V3d lsNearP(0.5, 0.5, 0.0), lsFarP(0.5, 0.5, 1.0);
451  V3d wsNearP, wsFarP, csNearP, csFarP;
452 
453  lpsToWs.multVecMatrix(lsNearP, wsNearP);
454  lpsToWs.multVecMatrix(lsFarP, wsFarP);
455 
456  M44d wsToCs = csToWs.inverse();
457  wsToCs.multVecMatrix(wsNearP, csNearP);
458  wsToCs.multVecMatrix(wsFarP, csFarP);
459 
460  double near = -csNearP.z;
461  double far = -csFarP.z;
462 
463  // Catch NaN here
464  if (isnan(near) || isnan(far)) {
465  throw BadPerspectiveMatrix("FrustumFieldMapping::setTransforms "
466  "received bad screen-to-world matrix");
467  }
468 
469  m_nearCurve.addSample(t, near);
470  m_farCurve.addSample(t, far);
471 
473 }
Imath::M44d M44d
Definition: SpiMathLib.h:82
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:713
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:709
bool m_defaultState
Boolean to tell us if the mapping is in its &#39;default&#39; state. This is needed because the class has a d...
Definition: FieldMapping.h:723
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:704
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:711
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:706
void FrustumFieldMapping::setZDistribution ( ZDistribution  dist)
inline

Sets the z slice distribution.

Definition at line 609 of file FieldMapping.h.

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

Returns the z slice distribution.

Definition at line 612 of file FieldMapping.h.

613  { return m_zDistribution; }
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:699
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 617 of file FieldMapping.h.

618  { 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:704
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 622 of file FieldMapping.h.

623  { 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:706
const MatrixCurve::SampleVec& FrustumFieldMapping::screenToWorldSamples ( ) const
inline

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

Definition at line 626 of file FieldMapping.h.

627  { 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:704
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 630 of file FieldMapping.h.

631  { 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:706
const FloatCurve::SampleVec& FrustumFieldMapping::nearPlaneSamples ( ) const
inline

Returns a vector of all motion samples for near plane.

Definition at line 634 of file FieldMapping.h.

635  { 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:711
const FloatCurve::SampleVec& FrustumFieldMapping::farPlaneSamples ( ) const
inline

Returns a vector of all motion samples for far plane.

Definition at line 638 of file FieldMapping.h.

639  { return m_farCurve.samples(); }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:713
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 642 of file FieldMapping.h.

643  { return m_nearCurve.linear(0.0); }
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:711
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 646 of file FieldMapping.h.

References FieldMapping::className(), FieldMapping::clone(), FieldMapping::extentsChanged(), FieldMapping::isIdentical(), FieldMapping::localToWorld(), FieldMapping::voxelToWorld(), FieldMapping::worldToLocal(), FieldMapping::worldToVoxel(), and FieldMapping::wsVoxelSize().

647  { return m_farCurve.linear(0.0); }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:713
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 477 of file FieldMapping.cpp.

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

Referenced by FrustumFieldMapping().

478 {
479  // Default camera to world ---
480 
481  M44d csToWs;
482  csToWs.makeIdentity();
483 
484  // Default screen to world ---
485 
486  double near = 1;
487  double far = 2;
488  double fovRadians = 45.0 * M_PI / 180.0;
489  double invTan = 1.0 / std::tan(fovRadians / 2.0);
490  double imageAspectRatio = 1.0;
491 
492  M44d perspective(1, 0, 0, 0,
493  0, 1, 0, 0,
494  0, 0, (far) / (far - near), 1,
495  0, 0, (- far * near) / (far - near), 0);
496 
497  M44d fov;
498  fov.setScale(V3d(invTan / imageAspectRatio, invTan, 1.0));
499 
500  M44d flipZ;
501  flipZ.setScale(V3d(1.0, 1.0, -1.0));
502 
503  M44d csToSs = flipZ * perspective * fov;
504 
505  M44d standardSsToWs = csToSs.inverse() * csToWs;
506 
507  // Set default state ---
508 
509  clearCurves();
510  setTransforms(standardSsToWs, csToWs);
511 
512  m_defaultState = true;
513 
515 }
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 &#39;default&#39; state. This is needed because the class has a d...
Definition: FieldMapping.h:723
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 526 of file FieldMapping.cpp.

527 {
528  worldToVoxel(wsP, vsP, 0.0);
529 }
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 533 of file FieldMapping.cpp.

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

534 {
535  V3d lsP;
536  worldToLocal(wsP, lsP, time);
537  localToVoxel(lsP, vsP);
538 }
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 542 of file FieldMapping.cpp.

Referenced by computeVoxelSize().

543 {
544  voxelToWorld(vsP, wsP, 0.0);
545 }
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 549 of file FieldMapping.cpp.

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

550 {
551  V3d lsP;
552  voxelToLocal(vsP, lsP);
553  localToWorld(lsP, wsP, time);
554 }
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 558 of file FieldMapping.cpp.

Referenced by worldToVoxel().

559 {
560  worldToLocal(wsP, lsP, 0.0);
561 }
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 565 of file FieldMapping.cpp.

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

566 {
567  switch (m_zDistribution) {
568  case UniformDistribution:
569  {
570  // First transform to local perspective space
571  V3d lpsP;
572  m_lpsToWsCurve.linear(time).inverse().multVecMatrix(wsP, lpsP);
573  // Also transform to camera space
574  V3d csP;
575  m_csToWsCurve.linear(time).inverse().multVecMatrix(wsP, csP);
576  // Interpolate near and far plane at current time
577  double near = m_nearCurve.linear(time);
578  double far = m_farCurve.linear(time);
579  // Use perspective-space X/Y and normalized depth for Z.
580  lsP = V3d(lpsP.x, lpsP.y, FIELD3D_LERPFACTOR(-csP.z, near, far));
581  break;
582  }
584  default:
585  {
586  m_lpsToWsCurve.linear(time).inverse().multVecMatrix(wsP, lsP);
587  break;
588  }
589  }
590 }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:713
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:709
Imath::V3d V3d
Definition: SpiMathLib.h:74
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:711
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:706
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:699
#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 594 of file FieldMapping.cpp.

Referenced by FieldGroup< BaseTypeList_T, Dims_T >::GetIntersections::intersectFrustumMapping(), and voxelToWorld().

595 {
596  localToWorld(lsP, wsP, 0.0);
597 }
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 601 of file FieldMapping.cpp.

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

602 {
603  switch (m_zDistribution) {
604  case UniformDistribution:
605  {
606  // Interpolate near and far plane at current time
607  double near = m_nearCurve.linear(time);
608  double far = m_farCurve.linear(time);
609  // In this case, local space is -not- equal to local perspective space
610  // Determine distance from camera
611  double wsDepthFromCam = FIELD3D_LERP(near, far, lsP.z);
612  // Transform point right in front of camera, X units away into world space
613  V3d lpsCenterP, wsCenterP, csCenterP(0.0, 0.0, -wsDepthFromCam);
614  m_csToWsCurve.linear(time).multVecMatrix(csCenterP, wsCenterP);
615  // Transform center point into screen space so we know what depth
616  // (in screen space) the voxel would live at -if- it were in local
617  // perspective space.
618  m_lpsToWsCurve.linear(time).inverse().multVecMatrix(wsCenterP, lpsCenterP);
619  // Now we create a local perspective coordinate that can be transformed
620  // using m_lpsToWsCurve
621  V3d lpsP(lsP.x, lsP.y, lpsCenterP.z);
622  // Now we can use m_lpsToWsCurve to transform the actual voxel location
623  m_lpsToWsCurve.linear(time).multVecMatrix(lpsP, wsP);
624  break;
625  }
627  default:
628  {
629  // In this case, local space and local perspective space are the same.
630  m_lpsToWsCurve.linear(time).multVecMatrix(lsP, wsP);
631  break;
632  }
633  }
634 }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:713
#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:709
Imath::V3d V3d
Definition: SpiMathLib.h:74
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:711
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:706
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:699
void FrustumFieldMapping::extentsChanged ( )
virtual

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

Reimplemented from FieldMapping.

Definition at line 519 of file FieldMapping.cpp.

References computeVoxelSize().

520 {
522 }
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 638 of file FieldMapping.cpp.

References staticClassType().

639 {
640  return std::string(staticClassType());
641 }
static const char * staticClassType()
Definition: FieldMapping.h:578
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 645 of file FieldMapping.cpp.

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

647 {
649 
650  if (other->className() != k_frustumMappingName) {
651  return false;
652  } else {
653 
655  FIELD_DYNAMIC_CAST<FrustumFieldMapping>(other);
656 
657  if (fm) {
658 
659  const SampleVec lpsToWs1 = m_lpsToWsCurve.samples();
660  const SampleVec lpsToWs2 = fm->m_lpsToWsCurve.samples();
661  const SampleVec csToWs1 = m_csToWsCurve.samples();
662  const SampleVec csToWs2 = fm->m_csToWsCurve.samples();
663 
664  size_t numSamples = lpsToWs1.size();
665 
666  // Check that slice distributions match
667  if (m_zDistribution != fm->m_zDistribution) {
668  return false;
669  }
670 
671  // First check if time sample counts differ
672  // lpsToWs and csToWs are guaranteed to have same sample count.
673  if (lpsToWs1.size() != lpsToWs2.size()) {
674  return false;
675  }
676 
677  // Then check if all time samples match, then check localToWorld
678  // and voxelToWorld matrices
679  for (size_t i = 0; i < numSamples; ++i) {
680  if (lpsToWs1[i].first != lpsToWs2[i].first) {
681  return false;
682  }
683  if (!checkMatricesIdentical(lpsToWs1[i].second, lpsToWs2[i].second,
684  tolerance)) {
685  return false;
686  }
687  if (!checkMatricesIdentical(csToWs1[i].second, csToWs2[i].second,
688  tolerance)) {
689  return false;
690  }
691  }
692 
693  return true;
694 
695  } else {
696  return false;
697  }
698  }
699  return false;
700 }
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:709
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:706
std::vector< Sample > SampleVec
Definition: Curve.h:85
ZDistribution m_zDistribution
Slice distribution type.
Definition: FieldMapping.h:699
bool checkMatricesIdentical(const Matrix_T &m1, const Matrix_T &m2, double tolerance)
boost::intrusive_ptr< FrustumFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:554
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 704 of file FieldMapping.cpp.

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

Referenced by FieldGroup< BaseTypeList_T, Dims_T >::GetIntersections::intersectFrustumMapping().

705 {
706  k = std::min(std::max(k, static_cast<int>(m_origin.z)),
707  static_cast<int>(m_origin.z + m_res.z - 1));
708  return m_wsVoxelSize[k - static_cast<int>(m_origin.z)];
709 }
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:717
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 773 of file FieldMapping.cpp.

References FIELD3D_NAMESPACE_SOURCE_CLOSE, and FrustumFieldMapping().

774 {
775  return Ptr(new FrustumFieldMapping(*this));
776 }
boost::intrusive_ptr< FrustumFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:554
void FrustumFieldMapping::computeVoxelSize ( )
private

Updates the local to world transformation matrix.

Definition at line 713 of file FieldMapping.cpp.

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

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

714 {
715  // Precompute the voxel size ---
716 
717  m_wsVoxelSize.resize(static_cast<int>(m_res.z),V3d(0.0));
718 
719  int i = static_cast<int>(m_origin.x + m_res.x / 2);
720  int j = static_cast<int>(m_origin.y + m_res.y / 2);
721 
722  // Do all z slices except last
723  int zMin = static_cast<int>(m_origin.z);
724  int zMax = static_cast<int>(m_origin.z + m_res.z - 1);
725 
726  for (int k = zMin, idx = 0; k < zMax; ++k, ++idx) {
727  V3d wsP, wsPx, wsPy, wsPz;
728  V3d vsP = discToCont(V3i(i, j, k));
729  V3d vsPx = discToCont(V3i(i + 1, j, k));
730  V3d vsPy = discToCont(V3i(i, j + 1, k));
731  V3d vsPz = discToCont(V3i(i, j, k + 1));
732  voxelToWorld(vsP, wsP);
733  voxelToWorld(vsPx, wsPx);
734  voxelToWorld(vsPy, wsPy);
735  voxelToWorld(vsPz, wsPz);
736  m_wsVoxelSize[idx] = V3d((wsPx - wsP).length(),
737  (wsPy - wsP).length(),
738  (wsPz - wsP).length());
739  }
740 
741  // Duplicate last value since there are no further slices to differentiate
742  // against
743  if (m_res.z >= 2) {
744  m_wsVoxelSize[static_cast<int>(m_res.z - 1)] = m_wsVoxelSize[static_cast<int>(m_res.z - 2)];
745  }
746 
747 }
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:717
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:1075
void FrustumFieldMapping::getLocalToVoxelMatrix ( M44d result)
private
Todo:
Unit test this

Definition at line 751 of file FieldMapping.cpp.

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

752 {
753  // Local to voxel is a scale by the resolution of the field, offset
754  // to the origin of the extents
755  M44d scaling, translation;
756  scaling.setScale(m_res);
757  translation.setTranslation(m_origin);
758  result = scaling * translation;
759 }
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 763 of file FieldMapping.cpp.

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

Referenced by reset(), and setTransforms().

764 {
767  m_nearCurve.clear();
768  m_farCurve.clear();
769 }
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:713
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
Definition: FieldMapping.h:709
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Definition: FieldMapping.h:711
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
Definition: FieldMapping.h:706
void clear()
Clears all samples in curve.
Definition: Curve.h:107

Member Data Documentation

FrustumFieldMapping::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 576 of file FieldMapping.h.

ZDistribution FrustumFieldMapping::m_zDistribution
private

Slice distribution type.

Definition at line 699 of file FieldMapping.h.

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

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 704 of file FieldMapping.h.

Referenced by setTransforms().

MatrixCurve FrustumFieldMapping::m_csToWsCurve
private

Time-varying camera to world space transform.

Definition at line 706 of file FieldMapping.h.

Referenced by 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 709 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 711 of file FieldMapping.h.

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

FloatCurve FrustumFieldMapping::m_farCurve
private

Time-varying far plane. Computed from m_lpsToWsCurve.

Definition at line 713 of file FieldMapping.h.

Referenced by clearCurves(), 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 717 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 723 of file FieldMapping.h.

Referenced by reset(), and setTransforms().


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