+ fix cutting or selecting mesh in perspective projection mode
This commit is contained in:
@@ -274,6 +274,7 @@ SET(FreeCADBase_CPP_SRCS
|
||||
Uuid.cpp
|
||||
Vector3D.cpp
|
||||
VectorPyImp.cpp
|
||||
ViewProj.cpp
|
||||
Writer.cpp
|
||||
XMLTools.cpp
|
||||
)
|
||||
|
||||
126
src/Base/ViewProj.cpp
Normal file
126
src/Base/ViewProj.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2005 Imetric 3D GmbH *
|
||||
* *
|
||||
* This file is part of the FreeCAD CAx development system. *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Library General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Library General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Library General Public *
|
||||
* License along with this library; see the file COPYING.LIB. If not, *
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
||||
* Suite 330, Boston, MA 02111-1307, USA *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
#include "ViewProj.h"
|
||||
|
||||
using namespace Base;
|
||||
|
||||
ViewProjMatrix::ViewProjMatrix (const Matrix4D &rclMtx)
|
||||
: _clMtx(rclMtx)
|
||||
{
|
||||
double m30 = _clMtx[3][0];
|
||||
double m31 = _clMtx[3][1];
|
||||
double m32 = _clMtx[3][2];
|
||||
double m33 = _clMtx[3][3];
|
||||
isOrthographic = (m30 == 0 && m31 == 0 && m32 == 0 && m33 == 1);
|
||||
|
||||
// Only for orthographic projection mode we can compute a single
|
||||
// matrix performing all steps.
|
||||
// For perspective projection the scaling and translation must
|
||||
// be done afterwards because it depends on the input point.
|
||||
if (isOrthographic) {
|
||||
// Scale from [-1,1] to [0,1]
|
||||
// As done in OpenInventor sources (see SbDPViewVolume::projectToScreen)
|
||||
_clMtx.scale(0.5, 0.5, 0.5);
|
||||
_clMtx.move(0.5, 0.5, 0.5);
|
||||
}
|
||||
|
||||
_clMtxInv = _clMtx;
|
||||
_clMtxInv.inverseGauss();
|
||||
}
|
||||
|
||||
ViewProjMatrix::~ViewProjMatrix()
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Vec>
|
||||
void perspectiveTransform(const Base::Matrix4D& mat, Vec& pnt)
|
||||
{
|
||||
double m30 = mat[3][0];
|
||||
double m31 = mat[3][1];
|
||||
double m32 = mat[3][2];
|
||||
double m33 = mat[3][3];
|
||||
double w = (pnt.x * m30 + pnt.y * m31 + pnt.z * m32 + m33);
|
||||
|
||||
mat.multVec(pnt, pnt);
|
||||
pnt /= w;
|
||||
}
|
||||
|
||||
Vector3f ViewProjMatrix::operator()(const Vector3f& src) const
|
||||
{
|
||||
Vector3f dst;
|
||||
if (!isOrthographic) {
|
||||
dst = src;
|
||||
perspectiveTransform<Vector3f>(_clMtx, dst);
|
||||
dst.Set(0.5*dst.x+0.5, 0.5*dst.y+0.5, 0.5*dst.z+0.5);
|
||||
}
|
||||
else {
|
||||
_clMtx.multVec(src, dst);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
Vector3d ViewProjMatrix::operator()(const Vector3d& src) const
|
||||
{
|
||||
Vector3d dst;
|
||||
if (!isOrthographic) {
|
||||
dst = src;
|
||||
perspectiveTransform<Vector3d>(_clMtx, dst);
|
||||
dst.Set(0.5*dst.x+0.5, 0.5*dst.y+0.5, 0.5*dst.z+0.5);
|
||||
}
|
||||
else {
|
||||
_clMtx.multVec(src, dst);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
Vector3f ViewProjMatrix::inverse (const Vector3f& src) const
|
||||
{
|
||||
Vector3f dst;
|
||||
if (!isOrthographic) {
|
||||
dst.Set(2.0*src.x-1.0, 2.0*src.y-1.0, 2.0*src.z-1.0);
|
||||
perspectiveTransform<Vector3f>(_clMtxInv, dst);
|
||||
}
|
||||
else {
|
||||
_clMtxInv.multVec(src, dst);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
Vector3d ViewProjMatrix::inverse (const Vector3d& src) const
|
||||
{
|
||||
Vector3d dst;
|
||||
if (!isOrthographic) {
|
||||
dst.Set(2.0*src.x-1.0, 2.0*src.y-1.0, 2.0*src.z-1.0);
|
||||
perspectiveTransform<Vector3d>(_clMtxInv, dst);
|
||||
}
|
||||
else {
|
||||
_clMtxInv.multVec(src, dst);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
@@ -60,40 +60,21 @@ protected:
|
||||
class BaseExport ViewProjMatrix : public ViewProjMethod
|
||||
{
|
||||
public:
|
||||
ViewProjMatrix (const Matrix4D &rclMtx) : _clMtx(rclMtx) { _clMtxInv = _clMtx; _clMtxInv.inverse(); }
|
||||
virtual ~ViewProjMatrix(){}
|
||||
ViewProjMatrix (const Matrix4D &rclMtx);
|
||||
virtual ~ViewProjMatrix();
|
||||
|
||||
inline Vector3f operator()(const Vector3f &rclPt) const;
|
||||
inline Vector3d operator()(const Vector3d &rclPt) const;
|
||||
inline Vector3f inverse (const Vector3f &rclPt) const;
|
||||
inline Vector3d inverse (const Vector3d &rclPt) const;
|
||||
Vector3f operator()(const Vector3f &rclPt) const;
|
||||
Vector3d operator()(const Vector3d &rclPt) const;
|
||||
Vector3f inverse (const Vector3f &rclPt) const;
|
||||
Vector3d inverse (const Vector3d &rclPt) const;
|
||||
|
||||
Matrix4D getProjectionMatrix (void) const { return _clMtx; }
|
||||
|
||||
protected:
|
||||
bool isOrthographic;
|
||||
Matrix4D _clMtx, _clMtxInv;
|
||||
};
|
||||
|
||||
inline Vector3f ViewProjMatrix::operator()(const Vector3f &rclPt) const
|
||||
{
|
||||
return Vector3f(_clMtx * rclPt);
|
||||
}
|
||||
|
||||
inline Vector3d ViewProjMatrix::operator()(const Vector3d &rclPt) const
|
||||
{
|
||||
return Vector3d(_clMtx * rclPt);
|
||||
}
|
||||
|
||||
inline Vector3f ViewProjMatrix::inverse (const Vector3f &rclPt) const
|
||||
{
|
||||
return Vector3f(_clMtxInv * rclPt);
|
||||
}
|
||||
|
||||
inline Vector3d ViewProjMatrix::inverse (const Vector3d &rclPt) const
|
||||
{
|
||||
return Vector3d(_clMtxInv * rclPt);
|
||||
}
|
||||
|
||||
} // namespace Base
|
||||
|
||||
#endif // BASE_VIEWPROJ_H
|
||||
|
||||
@@ -41,6 +41,8 @@ ViewVolumeProjection::ViewVolumeProjection (const SbViewVolume &vv)
|
||||
: viewVolume(vv)
|
||||
, hasTransform(false)
|
||||
{
|
||||
matrix = viewVolume.getMatrix();
|
||||
invert = matrix.inverse();
|
||||
}
|
||||
|
||||
Base::Vector3f ViewVolumeProjection::operator()(const Base::Vector3f &pt) const
|
||||
@@ -51,9 +53,10 @@ Base::Vector3f ViewVolumeProjection::operator()(const Base::Vector3f &pt) const
|
||||
pt3d.setValue(ptt.x, ptt.y, ptt.z);
|
||||
}
|
||||
|
||||
// Calling this function is expensive as the complete projection matrix is recomputed on each step
|
||||
viewVolume.projectToScreen(pt3d,pt3d);
|
||||
return Base::Vector3f(pt3d[0],pt3d[1],pt3d[2]);
|
||||
// See SbViewVolume::projectToScreen
|
||||
matrix.multVecMatrix(pt3d, pt3d);
|
||||
|
||||
return Base::Vector3f(0.5*pt3d[0]+0.5, 0.5*pt3d[1]+0.5, 0.5*pt3d[2]+0.5);
|
||||
}
|
||||
|
||||
Base::Vector3d ViewVolumeProjection::operator()(const Base::Vector3d &pt) const
|
||||
@@ -65,17 +68,8 @@ Base::Vector3d ViewVolumeProjection::operator()(const Base::Vector3d &pt) const
|
||||
|
||||
Base::Vector3f ViewVolumeProjection::inverse (const Base::Vector3f &pt) const
|
||||
{
|
||||
#if 1
|
||||
SbVec3f pt3d(2.0f*pt.x-1.0f, 2.0f*pt.y-1.0f, 2.0f*pt.z-1.0f);
|
||||
viewVolume.getMatrix().inverse().multVecMatrix(pt3d, pt3d);
|
||||
#elif 1
|
||||
SbLine line; SbVec3f pt3d;
|
||||
SbPlane distPlane = viewVolume.getPlane(viewVolume.getNearDist());
|
||||
viewVolume.projectPointToLine(SbVec2f(pt.x,pt.x), line);
|
||||
distPlane.intersect(line, pt3d);
|
||||
#else
|
||||
SbVec3f pt3d = viewVolume.getPlanePoint(viewVolume.getNearDist(), SbVec2f(pt.x,pt.y));
|
||||
#endif
|
||||
invert.multVecMatrix(pt3d, pt3d);
|
||||
return Base::Vector3f(pt3d[0],pt3d[1],pt3d[2]);
|
||||
}
|
||||
|
||||
@@ -101,15 +95,10 @@ Base::Matrix4D ViewVolumeProjection::getProjectionMatrix () const
|
||||
{
|
||||
// Inventor stores the transposed matrix
|
||||
Base::Matrix4D mat;
|
||||
SbMatrix affine, proj;
|
||||
|
||||
// The Inventor projection matrix is obtained by multiplying both matrices together (cf source)
|
||||
viewVolume.getMatrices(affine, proj);
|
||||
SbMatrix pmatrix = affine.multRight(proj);
|
||||
|
||||
for (int i=0; i<4; i++) {
|
||||
for (int j=0; j<4; j++)
|
||||
mat[i][j] = pmatrix[j][i];
|
||||
mat[i][j] = matrix[j][i];
|
||||
}
|
||||
|
||||
// Compose the object transform, if defined
|
||||
@@ -117,11 +106,6 @@ Base::Matrix4D ViewVolumeProjection::getProjectionMatrix () const
|
||||
mat = mat * transform;
|
||||
}
|
||||
|
||||
// Scale from [-1,1] to [0,1]
|
||||
// As done in OpenInventor sources (see SbDPViewVolume::projectToScreen)
|
||||
mat.scale(0.5, 0.5, 0.5);
|
||||
mat.move(0.5, 0.5, 0.5);
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <Inventor/SbColor.h>
|
||||
#include <Inventor/SbVec2f.h>
|
||||
#include <Inventor/SbViewVolume.h>
|
||||
#include <Inventor/SbMatrix.h>
|
||||
|
||||
class SbViewVolume;
|
||||
class QAbstractItemView;
|
||||
@@ -108,6 +109,8 @@ public:
|
||||
|
||||
protected:
|
||||
SbViewVolume viewVolume;
|
||||
SbMatrix matrix;
|
||||
SbMatrix invert;
|
||||
bool hasTransform;
|
||||
Base::Matrix4D transform;
|
||||
};
|
||||
|
||||
@@ -1175,13 +1175,13 @@ void MeshAlgorithm::CheckFacets(const Base::ViewProjMethod* pclProj, const Base:
|
||||
Base::Vector3f pt2d;
|
||||
// Use a bounding box to reduce number of call to Polygon::Contains
|
||||
Base::BoundBox2d bb = rclPoly.CalcBoundBox();
|
||||
// Precompute the screen projection matrix as COIN's projection function is expensive
|
||||
Base::Matrix4D pmat = pclProj->getProjectionMatrix();
|
||||
// Precompute the screen projection matrix as COIN's projection function is expensive
|
||||
Base::ViewProjMatrix fixedProj(pclProj->getProjectionMatrix());
|
||||
|
||||
unsigned long index=0;
|
||||
for (MeshFacetArray::_TConstIterator it = f.begin(); it != f.end(); ++it,++index) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
pt2d = pmat * p[it->_aulPoints[i]];
|
||||
pt2d = fixedProj(p[it->_aulPoints[i]]);
|
||||
|
||||
// First check whether the point is in the bounding box of the polygon
|
||||
if ((bb.Contains(Base::Vector2d(pt2d.x, pt2d.y)) &&
|
||||
|
||||
Reference in New Issue
Block a user