53 #define isnan(__x__) _isnan(__x__)
58 using namespace boost;
85 template <
class Matrix_T>
89 if (m1.equalWithRelError(m2, tolerance)) {
92 V3d s1, r1, t1, sh1, s2, r2, t2, sh2;
99 if (!s1.equalWithRelError(s2, tolerance) ||
100 !r1.equalWithAbsError(r2, tolerance) ||
101 !t1.equalWithRelError(t2, tolerance)) {
149 m_res = extents.max - extents.min +
V3i(1);
174 const Box3d &wsBounds,
178 mapping->worldToVoxel(test1, test2);
181 V3d(wsBounds.min.x, wsBounds.min.y, wsBounds.min.z),
182 V3d(wsBounds.max.x, wsBounds.min.y, wsBounds.min.z),
183 V3d(wsBounds.min.x, wsBounds.max.y, wsBounds.min.z),
184 V3d(wsBounds.max.x, wsBounds.max.y, wsBounds.min.z),
185 V3d(wsBounds.min.x, wsBounds.min.y, wsBounds.max.z),
186 V3d(wsBounds.max.x, wsBounds.min.y, wsBounds.max.z),
187 V3d(wsBounds.min.x, wsBounds.max.y, wsBounds.max.z),
188 V3d(wsBounds.max.x, wsBounds.max.y, wsBounds.max.z)
190 vsBounds.makeEmpty();
192 for (
int i = 0; i < 8; i++) {
193 mapping->worldToVoxel(wsVerts[i], vsP);
194 vsBounds.extendBy(vsP);
201 const Box3d &fromBounds,
205 V3d(fromBounds.min.x, fromBounds.min.y, fromBounds.min.z),
206 V3d(fromBounds.max.x, fromBounds.min.y, fromBounds.min.z),
207 V3d(fromBounds.min.x, fromBounds.max.y, fromBounds.min.z),
208 V3d(fromBounds.max.x, fromBounds.max.y, fromBounds.min.z),
209 V3d(fromBounds.min.x, fromBounds.min.y, fromBounds.max.z),
210 V3d(fromBounds.max.x, fromBounds.min.y, fromBounds.max.z),
211 V3d(fromBounds.min.x, fromBounds.max.y, fromBounds.max.z),
212 V3d(fromBounds.max.x, fromBounds.max.y, fromBounds.max.z)
214 toBounds.makeEmpty();
216 for (
int i = 0; i < 8; i++) {
217 mtx.multVecMatrix(verts[i], toP);
218 toBounds.extendBy(toP);
310 double tolerance)
const
319 FIELD_DYNAMIC_CAST<MatrixFieldMapping>(other);
324 const SampleVec lsToWs2 = mm->m_lsToWsCurve.samples();
326 const SampleVec vsToWs2 = mm->m_vsToWsCurve.samples();
328 size_t numSamples = lsToWs1.size();
332 if (lsToWs1.size() != lsToWs2.size()) {
338 for (
size_t i = 0; i < numSamples; ++i) {
339 if (lsToWs1[i].first != lsToWs2[i].first) {
365 typedef MatrixCurve::SampleVec::const_iterator SampleIter;
370 M44d vsToLs = lsToVs.inverse();
376 for (SampleIter i = lsToWs.begin(), end = lsToWs.end(); i != end; i++) {
390 V3d voxelOrigin, nextVoxel;
406 M44d scaling, translation;
407 scaling.setScale(
m_res);
408 translation.setTranslation(
m_origin);
409 result = scaling * translation;
425 m_zDistribution(PerspectiveDistribution),
449 const M44d &ssToWs,
const M44d &csToWs)
457 M44d lsToSs, scale, translation;
458 scale.setScale(
V3d(2.0, 2.0, 1.0));
459 translation.setTranslation(
V3d(-1.0, -1.0, 0.0));
460 lsToSs = scale * translation;
461 M44d lpsToWs = lsToSs * ssToWs;
474 V3d lsNearP(0.5, 0.5, 0.0), lsFarP(0.5, 0.5, 1.0);
475 V3d wsNearP, wsFarP, csNearP, csFarP;
477 lpsToWs.multVecMatrix(lsNearP, wsNearP);
478 lpsToWs.multVecMatrix(lsFarP, wsFarP);
480 M44d wsToCs = csToWs.inverse();
481 wsToCs.multVecMatrix(wsNearP, csNearP);
482 wsToCs.multVecMatrix(wsFarP, csFarP);
484 double near = -csNearP.z;
485 double far = -csFarP.z;
488 if (std::isnan(near) || std::isnan(far)) {
489 throw BadPerspectiveMatrix(
"FrustumFieldMapping::setTransforms "
490 "received bad screen-to-world matrix");
506 csToWs.makeIdentity();
512 double fovRadians = 45.0 * M_PI / 180.0;
513 double invTan = 1.0 / std::tan(fovRadians / 2.0);
514 double imageAspectRatio = 1.0;
516 M44d perspective(1, 0, 0, 0,
518 0, 0, (far) / (far - near), 1,
519 0, 0, (- far * near) / (far - near), 0);
522 fov.setScale(
V3d(invTan / imageAspectRatio, invTan, 1.0));
525 flipZ.setScale(
V3d(1.0, 1.0, -1.0));
527 M44d csToSs = flipZ * perspective * fov;
529 M44d standardSsToWs = csToSs.inverse() * csToWs;
637 V3d lpsCenterP, wsCenterP, csCenterP(0.0, 0.0, -wsDepthFromCam);
645 V3d lpsP(lsP.x, lsP.y, lpsCenterP.z);
670 double tolerance)
const
679 FIELD_DYNAMIC_CAST<FrustumFieldMapping>(other);
684 const SampleVec lpsToWs2 = fm->m_lpsToWsCurve.samples();
686 const SampleVec csToWs2 = fm->m_csToWsCurve.samples();
688 size_t numSamples = lpsToWs1.size();
697 if (lpsToWs1.size() != lpsToWs2.size()) {
703 for (
size_t i = 0; i < numSamples; ++i) {
704 if (lpsToWs1[i].first != lpsToWs2[i].first) {
747 int zMin = static_cast<int>(
m_origin.z);
750 for (
int k = zMin, idx = 0; k < zMax; ++k, ++idx) {
751 V3d wsP, wsPx, wsPy, wsPz;
761 (wsPy - wsP).length(),
762 (wsPz - wsP).length());
779 M44d scaling, translation;
780 scaling.setScale(
m_res);
781 translation.setTranslation(
m_origin);
782 result = scaling * translation;