54 using namespace boost;
81 template <
class Matrix_T>
85 if (m1.equalWithRelError(m2, tolerance)) {
88 V3d s1, r1, t1, sh1, s2, r2, t2, sh2;
95 if (!s1.equalWithRelError(s2, tolerance) ||
96 !r1.equalWithAbsError(r2, tolerance) ||
97 !t1.equalWithRelError(t2, tolerance)) {
145 m_res = extents.max - extents.min +
V3i(1);
177 double tolerance)
const
253 double tolerance)
const
262 FIELD_DYNAMIC_CAST<MatrixFieldMapping>(other);
267 const SampleVec lsToWs2 = mm->m_lsToWsCurve.samples();
269 const SampleVec vsToWs2 = mm->m_vsToWsCurve.samples();
271 size_t numSamples = lsToWs1.size();
275 if (lsToWs1.size() != lsToWs2.size()) {
281 for (
size_t i = 0; i < numSamples; ++i) {
282 if (lsToWs1[i].first != lsToWs2[i].first) {
308 typedef MatrixCurve::SampleVec::const_iterator SampleIter;
313 M44d vsToLs = lsToVs.inverse();
319 for (SampleIter i = lsToWs.begin(), end = lsToWs.end(); i != end; i++) {
333 V3d voxelOrigin, nextVoxel;
349 M44d scaling, translation;
350 scaling.setScale(
m_res);
351 translation.setTranslation(
m_origin);
352 result = scaling * translation;
368 m_zDistribution(PerspectiveDistribution),
392 const M44d &ssToWs,
const M44d &csToWs)
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;
417 V3d lsNearP(0.5, 0.5, 0.0), lsFarP(0.5, 0.5, 1.0);
418 V3d wsNearP, wsFarP, csNearP, csFarP;
420 lpsToWs.multVecMatrix(lsNearP, wsNearP);
421 lpsToWs.multVecMatrix(lsFarP, wsFarP);
423 M44d wsToCs = csToWs.inverse();
424 wsToCs.multVecMatrix(wsNearP, csNearP);
425 wsToCs.multVecMatrix(wsFarP, csFarP);
427 double near = -csNearP.z;
428 double far = -csFarP.z;
431 if (isnan(near) || isnan(far)) {
432 throw BadPerspectiveMatrix(
"FrustumFieldMapping::setTransforms "
433 "received bad screen-to-world matrix");
449 csToWs.makeIdentity();
455 double fovRadians = 45.0 * M_PI / 180.0;
456 double invTan = 1.0 / std::tan(fovRadians / 2.0);
457 double imageAspectRatio = 1.0;
459 M44d perspective(1, 0, 0, 0,
461 0, 0, (far) / (far - near), 1,
462 0, 0, (- far * near) / (far - near), 0);
465 fov.setScale(
V3d(invTan / imageAspectRatio, invTan, 1.0));
468 flipZ.setScale(
V3d(1.0, 1.0, -1.0));
470 M44d csToSs = flipZ * perspective * fov;
472 M44d standardSsToWs = csToSs.inverse() * csToWs;
580 V3d lpsCenterP, wsCenterP, csCenterP(0.0, 0.0, -wsDepthFromCam);
588 V3d lpsP(lsP.x, lsP.y, lpsCenterP.z);
613 double tolerance)
const
622 FIELD_DYNAMIC_CAST<FrustumFieldMapping>(other);
627 const SampleVec lpsToWs2 = fm->m_lpsToWsCurve.samples();
629 const SampleVec csToWs2 = fm->m_csToWsCurve.samples();
631 size_t numSamples = lpsToWs1.size();
640 if (lpsToWs1.size() != lpsToWs2.size()) {
646 for (
size_t i = 0; i < numSamples; ++i) {
647 if (lpsToWs1[i].first != lpsToWs2[i].first) {
673 k = std::min(std::max(k, static_cast<int>(
m_origin.z)),
690 int zMin =
static_cast<int>(
m_origin.z);
693 for (
int k = zMin, idx = 0; k < zMax; ++k, ++idx) {
694 V3d wsP, wsPx, wsPy, wsPz;
704 (wsPy - wsP).length(),
705 (wsPz - wsP).length());
722 M44d scaling, translation;
723 scaling.setScale(
m_res);
724 translation.setTranslation(
m_origin);
725 result = scaling * translation;
Contains typedefs for the commonly used types in Field3D.
size_t numSamples() const
Returns the number of samples in the curve.
V3d m_origin
The integer voxel-space origin of the underlying Field object. Is equal to field.extents.min.
virtual FieldMapping::Ptr clone() const
Returns a pointer to a copy of the mapping, pure virtual so ensure derived classes properly implement...
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
const string k_matrixMappingName("MatrixFieldMapping")
FloatCurve m_farCurve
Time-varying far plane. Computed from m_lpsToWsCurve.
void clearCurves()
Clears all Curve data members. Used by setTransforms() to prepare for the first sample to be added...
M44d m_vsToWs
Voxel space to world space.
virtual bool isIdentical(FieldMapping::Ptr other, double tolerance=0.0) const
Whether the mapping is identical to another mapping.
MatrixCurve m_vsToWsCurve
Time-varying voxel to world space transform.
FieldMapping()
Constructor.
void setLocalToWorld(const M44d &lsToWs)
Sets the local to world transform. All other matrices will be updated based on this.
virtual void voxelToWorld(const V3d &vsP, V3d &wsP) const
Transform from voxel space position into world space.
virtual void extentsChanged()
Implement this if the subclass needs to update itself when the resolution changes.
void getLocalToVoxelMatrix(M44d &result)
virtual void localToWorld(const V3d &lsP, V3d &wsP) const
Transform from local space position into world space.
Base class for mapping between world-, local- and voxel coordinates.
void voxelToLocal(const V3d &vsP, V3d &lsP) const
Inverse of localToVoxel.
MatrixCurve m_lsToWsCurve
Time-varying local to world space transform.
M44d m_lsToWs
Local space to 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.
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...
virtual void worldToVoxel(const V3d &wsP, V3d &vsP) const
Transform from world space position into voxel space.
virtual std::string className() const
Returns the FieldMapping type name. Used when writing/reading from disk.
void computeVoxelSize()
Updates the local to world transformation matrix.
boost::intrusive_ptr< FieldMapping > Ptr
void addSample(const float t, const T &value)
Adds a sample point to the curve.
static const char * classType()
MatrixCurve m_lpsToWsCurve
Time-varying local perspective to world space transform. Computed from m_ssToWsCurve.
std::vector< V3d > m_wsVoxelSize
Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency...
virtual bool isIdentical(FieldMapping::Ptr other, double tolerance=0.0) const
Whether the mapping is identical to another mapping.
void reset()
Resets the transform. Makes a perspective transform at the origin, looking down the negative Z axis w...
boost::intrusive_ptr< NullFieldMapping > Ptr
Convenience typedef.
static const char * classType()
virtual bool isIdentical(FieldMapping::Ptr other, double tolerance=0.0) const
Whether the mapping is identical to another mapping.
bool m_defaultState
Boolean to tell us if the mapping is in its 'default' state. This is needed because the class has a d...
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.
static const char * classType()
virtual ~FieldMapping()
Destructor.
Contains the FieldMapping base class and the NullFieldMapping and MatrixFieldMapping subclasses...
const SampleVec & samples() const
Returns a const reference to the samples in the curve.
virtual void extentsChanged()
Implement this if the subclass needs to update itself when the resolution changes.
const string k_mappingName("FieldMapping")
virtual FieldMapping::Ptr clone() const
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
Transform from world space position into local space.
virtual void extentsChanged()
Implement this if the subclass needs to update itself when the resolution changes.
#define FIELD3D_EXTRACT_SHRT
FloatCurve m_nearCurve
Time-varying near plane. Computed from m_lpsToWsCurve.
Contains Field, WritableField and ResizableField classes.
T linear(const float t) const
Linearly interpolates a value from the curve.
void getLocalToVoxelMatrix(M44d &result)
virtual V3d wsVoxelSize(int i, int j, int k) const
Returns world-space size of a voxel at the specified coordinate.
void setTransforms(const M44d &ssToWs, const M44d &csToWs)
Sets the screenToWorld and cameraToWorld transforms. All other internal matrices will be updated base...
MatrixCurve m_csToWsCurve
Time-varying camera to world space transform.
bool m_isTimeVarying
Stores whether the curve has more than one time sample.
void makeIdentity()
Sets the transform to identity. This makes it functionally equivalent to a NullFieldMapping.
void updateTransform()
Updates the local to world transformation matrix.
const string k_nullMappingName("NullFieldMapping")
virtual std::string className() const =0
Returns the FieldMapping type name. Used when writing/reading from disk.
std::vector< Sample > SampleVec
ZDistribution m_zDistribution
Slice distribution type.
virtual std::string className() const
Returns the FieldMapping type name. Used when writing/reading from disk.
V3d m_wsVoxelSize
Precomputed world-space voxel size. Calculations may assume orthogonal transformation for efficiency...
virtual FieldMapping::Ptr clone() const
Returns a pointer to a copy of the mapping, pure virtual so ensure derived classes properly implement...
void clear()
Clears all samples in curve.
boost::intrusive_ptr< MatrixFieldMapping > Ptr
Convenience typedef.
M44d m_wsToLs
World space to local space.
bool checkMatricesIdentical(const Matrix_T &m1, const Matrix_T &m2, double tolerance)
boost::intrusive_ptr< FrustumFieldMapping > Ptr
Convenience typedef.
const string k_frustumMappingName("FrustumFieldMapping")
M44d m_wsToVs
World space to voxel space.
double discToCont(int discCoord)
Goes from discrete coordinates to continuous coordinates See Graphics Gems - What is a pixel...
virtual std::string className() const
Returns the FieldMapping type name. Used when writing/reading from disk.
void setExtents(const Box3i &extents)
This sets the field extents information to use for defining the local coordinate space.
#define FIELD3D_LERPFACTOR
static const char * classType()