From 6ebd0f4e5ba2cd902a7ad06aedce70a05422beb0 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 10 Sep 2019 23:21:09 +0200 Subject: [PATCH] move method setTransform() from ViewVolumeProjection to ViewProjMethod, restructure ViewProjMethod --- src/Base/ViewProj.cpp | 60 +++++++++++++++++++++++++++++++++++++++++-- src/Base/ViewProj.h | 17 ++++++++++-- src/Gui/Utilities.cpp | 26 +++---------------- src/Gui/Utilities.h | 3 --- 4 files changed, 77 insertions(+), 29 deletions(-) diff --git a/src/Base/ViewProj.cpp b/src/Base/ViewProj.cpp index b8d5096508..bf13085f20 100644 --- a/src/Base/ViewProj.cpp +++ b/src/Base/ViewProj.cpp @@ -26,6 +26,55 @@ using namespace Base; +ViewProjMethod::ViewProjMethod() + : hasTransform(false) +{ +} + +/*! Calculate the composed projection matrix which is a product of + * projection matrix multiplied with input transformation matrix. + */ +Matrix4D ViewProjMethod::getComposedProjectionMatrix (void) const +{ + Matrix4D mat = getProjectionMatrix(); + + // Compose the object transform, if defined + if (hasTransform) { + mat = mat * transform; + } + + return mat; +} + +/*! + * \brief This method applies an additional transformation to the input points + * passed with the () operator. + * \param mat + */ +void ViewProjMethod::setTransform(const Base::Matrix4D& mat) +{ + transform = mat; + hasTransform = (mat != Base::Matrix4D()); +} + +void ViewProjMethod::transformInput(const Base::Vector3f& src, Base::Vector3f& dst) const +{ + dst = src; + if (hasTransform) { + transform.multVec(dst, dst); + } +} + +void ViewProjMethod::transformInput(const Base::Vector3d& src, Base::Vector3d& dst) const +{ + dst = src; + if (hasTransform) { + transform.multVec(dst, dst); + } +} + +//----------------------------------------------------------------------------- + ViewProjMatrix::ViewProjMatrix (const Matrix4D &rclMtx) : _clMtx(rclMtx) { @@ -62,6 +111,7 @@ Matrix4D ViewProjMatrix::getProjectionMatrix (void) const mat.move(-0.5, -0.5, -0.5); mat.scale(2.0, 2.0, 2.0); } + return mat; } @@ -78,8 +128,11 @@ void perspectiveTransform(const Base::Matrix4D& mat, Vec& pnt) pnt /= w; } -Vector3f ViewProjMatrix::operator()(const Vector3f& src) const +Vector3f ViewProjMatrix::operator()(const Vector3f& inp) const { + Vector3f src; + transformInput(inp, src); + Vector3f dst; if (!isOrthographic) { dst = src; @@ -93,8 +146,11 @@ Vector3f ViewProjMatrix::operator()(const Vector3f& src) const return dst; } -Vector3d ViewProjMatrix::operator()(const Vector3d& src) const +Vector3d ViewProjMatrix::operator()(const Vector3d& inp) const { + Vector3d src; + transformInput(inp, src); + Vector3d dst; if (!isOrthographic) { dst = src; diff --git a/src/Base/ViewProj.h b/src/Base/ViewProj.h index 306c2f6ece..c0e5935fbd 100644 --- a/src/Base/ViewProj.h +++ b/src/Base/ViewProj.h @@ -47,10 +47,23 @@ public: /** Convert a 2D point on the projection plane in 3D space */ virtual Vector3d inverse (const Vector3d &rclPt) const = 0; /** Calculate the projection (+ mapping) matrix */ - virtual Matrix4D getProjectionMatrix (void) const = 0; + virtual Matrix4D getProjectionMatrix (void) const = 0; + /** Calculate the composed projection matrix */ + Matrix4D getComposedProjectionMatrix (void) const; + /** Apply an additional transformation to the input points */ + void setTransform(const Base::Matrix4D&); + const Base::Matrix4D& getTransform() const { + return transform; + } protected: - ViewProjMethod(){} + ViewProjMethod(); + void transformInput(const Base::Vector3f&, Base::Vector3f&) const; + void transformInput(const Base::Vector3d&, Base::Vector3d&) const; + +private: + bool hasTransform; + Base::Matrix4D transform; }; /** diff --git a/src/Gui/Utilities.cpp b/src/Gui/Utilities.cpp index 4335af7a88..40e4db7d1c 100644 --- a/src/Gui/Utilities.cpp +++ b/src/Gui/Utilities.cpp @@ -39,7 +39,6 @@ using namespace Gui; ViewVolumeProjection::ViewVolumeProjection (const SbViewVolume &vv) : viewVolume(vv) - , hasTransform(false) { matrix = viewVolume.getMatrix(); invert = matrix.inverse(); @@ -47,11 +46,10 @@ ViewVolumeProjection::ViewVolumeProjection (const SbViewVolume &vv) Base::Vector3f ViewVolumeProjection::operator()(const Base::Vector3f &pt) const { - SbVec3f pt3d(pt.x,pt.y,pt.z); - if (hasTransform) { - Base::Vector3f ptt = transform * pt; - pt3d.setValue(ptt.x, ptt.y, ptt.z); - } + Base::Vector3f src; + transformInput(pt, src); + + SbVec3f pt3d(src.x,src.y,src.z); // See SbViewVolume::projectToScreen matrix.multVecMatrix(pt3d, pt3d); @@ -80,17 +78,6 @@ Base::Vector3d ViewVolumeProjection::inverse (const Base::Vector3d &pt) const return Base::convertTo(ptf); } -/*! - * \brief This method applies an additional transformation to the input points - * passed with the () operator. - * \param mat - */ -void ViewVolumeProjection::setTransform(const Base::Matrix4D& mat) -{ - transform = mat; - hasTransform = (mat != Base::Matrix4D()); -} - Base::Matrix4D ViewVolumeProjection::getProjectionMatrix () const { // Inventor stores the transposed matrix @@ -101,11 +88,6 @@ Base::Matrix4D ViewVolumeProjection::getProjectionMatrix () const mat[i][j] = matrix[j][i]; } - // Compose the object transform, if defined - if (hasTransform) { - mat = mat * transform; - } - return mat; } diff --git a/src/Gui/Utilities.h b/src/Gui/Utilities.h index 8c900e78d9..5d6322d8f4 100644 --- a/src/Gui/Utilities.h +++ b/src/Gui/Utilities.h @@ -121,15 +121,12 @@ public: Base::Vector3f inverse (const Base::Vector3f &rclPt) const; Base::Vector3d inverse (const Base::Vector3d &rclPt) const; - void setTransform(const Base::Matrix4D&); Base::Matrix4D getProjectionMatrix () const; protected: SbViewVolume viewVolume; SbMatrix matrix; SbMatrix invert; - bool hasTransform; - Base::Matrix4D transform; }; class GuiExport Tessellator