+ rename methods in Vector3 class

+ add convenience methods Cross and Dot to Vector3 class
+ fix bug in DistanceToLineSegment in Vector3 class
This commit is contained in:
wmayer 2016-07-30 15:14:47 +02:00
parent 5c095de599
commit 1e2e24b652
18 changed files with 241 additions and 212 deletions

View File

@ -833,7 +833,7 @@ inline Vector3<_Precision> BoundBox3<_Precision>::ClosestPoint (const Vector3<_P
for (int i = 0; i < 6; i++) {
Vector3<_Precision> clTemp = rclPt;
CalcPlane(i, cBase, cNormal);
clTemp.ProjToPlane(cBase, cNormal);
clTemp.ProjectToPlane(cBase, cNormal);
_Precision fDist = (clTemp - rclPt).Length();
if (fDist < fMinDist) {
fMinDist = fDist;

View File

@ -54,7 +54,7 @@ double Vector2D::GetAngle (const Vector2D &rclVect) const
return -FLOAT_MAX; // division by zero
}
void Vector2D::ProjToLine (const Vector2D &rclPt, const Vector2D &rclLine)
void Vector2D::ProjectToLine (const Vector2D &rclPt, const Vector2D &rclLine)
{
double l = rclLine.Length();
double t1 = (rclPt * rclLine) / l;

View File

@ -68,7 +68,7 @@ public:
inline void Scale (double fS);
inline void Normalize (void);
double GetAngle (const Vector2D &rclVect) const;
void ProjToLine (const Vector2D &rclPt, const Vector2D &rclLine);
void ProjectToLine (const Vector2D &rclPt, const Vector2D &rclLine);
};
/** BoundBox2D ********************************************/

View File

@ -22,6 +22,7 @@
#include "PreCompiled.h"
#include "Tools.h"
#include "Vector3D.h"
using namespace Base;
@ -165,6 +166,12 @@ _Precision Vector3<_Precision>::operator * (const Vector3<_Precision>& rcVct) c
return (x * rcVct.x) + (y * rcVct.y) + (z * rcVct.z);
}
template <class _Precision>
_Precision Vector3<_Precision>::Dot (const Vector3<_Precision>& rcVct) const
{
return (x * rcVct.x) + (y * rcVct.y) + (z * rcVct.z);
}
template <class _Precision>
Vector3<_Precision> Vector3<_Precision>::operator % (const Vector3<_Precision>& rcVct) const
{
@ -175,6 +182,16 @@ Vector3<_Precision> Vector3<_Precision>::operator % (const Vector3<_Precision>&
return cVctRes;
}
template <class _Precision>
Vector3<_Precision> Vector3<_Precision>::Cross(const Vector3<_Precision>& rcVct) const
{
Vector3<_Precision> cVctRes;
cVctRes.x = (y * rcVct.z) - (z * rcVct.y);
cVctRes.y = (z * rcVct.x) - (x * rcVct.z);
cVctRes.z = (x * rcVct.y) - (y * rcVct.x);
return cVctRes;
}
template <class _Precision>
bool Vector3<_Precision>::operator != (const Vector3<_Precision>& rcVct) const
{
@ -190,7 +207,7 @@ bool Vector3<_Precision>::operator == (const Vector3<_Precision>& rcVct) const
}
template <class _Precision>
Vector3<_Precision>& Vector3<_Precision>::ProjToPlane (const Vector3<_Precision> &rclBase,
Vector3<_Precision>& Vector3<_Precision>::ProjectToPlane (const Vector3<_Precision> &rclBase,
const Vector3<_Precision> &rclNorm)
{
Vector3<_Precision> clTemp(rclNorm);
@ -198,6 +215,15 @@ Vector3<_Precision>& Vector3<_Precision>::ProjToPlane (const Vector3<_Precision>
return *this;
}
template <class _Precision>
void Vector3<_Precision>::ProjectToPlane (const Vector3 &rclBase,
const Vector3 &rclNorm,
Vector3 &rclProj) const
{
Vector3<_Precision> clTemp(rclNorm);
rclProj = *this - (clTemp *= ((*this - rclBase) * clTemp) / clTemp.Sqr());
}
template <class _Precision>
_Precision Vector3<_Precision>::DistanceToPlane (const Vector3<_Precision> &rclBase,
const Vector3<_Precision> &rclNorm) const
@ -219,29 +245,23 @@ _Precision Vector3<_Precision>::DistanceToLine (const Vector3<_Precision> &rclBa
}
template <class _Precision>
Vector3<_Precision> Vector3<_Precision>::DistanceToLineSegment (const Vector3& rclP1,
Vector3<_Precision> Vector3<_Precision>::DistanceToLineSegment(const Vector3& rclP1,
const Vector3& rclP2) const
{
Vector3<_Precision> dir = rclP2-rclP1;
Vector3<_Precision> beg = *this-rclP1;
Vector3<_Precision> end = beg+dir;
_Precision len2 = Base::DistanceP2(rclP1, rclP2);
if (len2 == 0)
return rclP1;
Vector3<_Precision> proj, len;
proj.ProjToLine(beg, dir);
len = proj + beg;
if (len * dir < 0 || len.Length() > dir.Length()) {
if (beg.Length() < end.Length())
return beg;
else
return end;
}
else {
return proj;
}
Vector3<_Precision> p2p1 = rclP2-rclP1;
Vector3<_Precision> pXp1 = *this-rclP1;
_Precision dot = pXp1 * p2p1;
_Precision t = clamp<_Precision>(dot/len2, 0, 1);
Vector3<_Precision> dist = t * p2p1 - pXp1;
return dist;
}
template <class _Precision>
Vector3<_Precision>& Vector3<_Precision>::ProjToLine (const Vector3<_Precision> &rclPoint,
Vector3<_Precision>& Vector3<_Precision>::ProjectToLine (const Vector3<_Precision> &rclPoint,
const Vector3<_Precision> &rclLine)
{
return (*this = ((((rclPoint * rclLine) / rclLine.Sqr()) * rclLine) - rclPoint));

View File

@ -117,8 +117,12 @@ public:
Vector3 & operator = (const Vector3<_Precision>& rcVct);
/// Scalar product
_Precision operator * (const Vector3<_Precision>& rcVct) const;
/// Scalar product
_Precision Dot (const Vector3<_Precision>& rcVct) const;
/// Cross product
Vector3 operator % (const Vector3<_Precision>& rcVct) const;
/// Cross product
Vector3 Cross (const Vector3<_Precision>& rcVct) const;
/// Comparing for inequality
bool operator != (const Vector3<_Precision>& rcVct) const;
/// Comparing for equality
@ -159,7 +163,12 @@ public:
void TransformToCoordinateSystem (const Vector3 &rclBase, const Vector3 &rclDirX, const Vector3 &rclDirY);
//bool Equal(const Vector3 &rclVect) const;
/// Projects this point onto the plane given by the base \a rclBase and the normal \a rclNorm.
Vector3 & ProjToPlane (const Vector3 &rclBase, const Vector3 &rclNorm);
Vector3 & ProjectToPlane (const Vector3 &rclBase, const Vector3 &rclNorm);
/**
* Projects this point onto the plane given by the base \a rclBase and the normal \a rclNorm
* and stores the result in rclProj.
*/
void ProjectToPlane (const Vector3 &rclBase, const Vector3 &rclNorm, Vector3 &rclProj) const;
/// Projects this point onto the line given by the base \a rclPoint and the direction \a rclLine.
/**
* Projects a point \a rclPoint onto the line defined by the origin and the direction \a rclLine.
@ -167,10 +176,10 @@ public:
* is the distance from \a rclPoint to the line.
* Note: The resulting vector does not depend on the current vector.
*/
Vector3 & ProjToLine (const Vector3 &rclPoint, const Vector3 &rclLine);
Vector3 & ProjectToLine (const Vector3 &rclPoint, const Vector3 &rclLine);
/**
* Get the perpendicular of this point to the line defined by rclBase and rclDir.
* Note: Do not mix up this method with ProjToLine.
* Note: Do not mix up this method with ProjectToLine.
*/
Vector3 Perpendicular(const Vector3 &rclBase, const Vector3 &rclDir) const;
/** Computes the distance to the given plane. Depending on the side this point is located

View File

@ -364,7 +364,7 @@ PyObject* VectorPy::projectToLine(PyObject *args)
VectorPy::PointerType base_ptr = reinterpret_cast<VectorPy::PointerType>(base_vec->_pcTwinPointer);
VectorPy::PointerType line_ptr = reinterpret_cast<VectorPy::PointerType>(line_vec->_pcTwinPointer);
this_ptr->ProjToLine(*base_ptr, *line_ptr);
this_ptr->ProjectToLine(*base_ptr, *line_ptr);
return Py::new_reference_to(this);
}
@ -390,7 +390,7 @@ PyObject* VectorPy::projectToPlane(PyObject *args)
VectorPy::PointerType base_ptr = reinterpret_cast<VectorPy::PointerType>(base_vec->_pcTwinPointer);
VectorPy::PointerType line_ptr = reinterpret_cast<VectorPy::PointerType>(line_vec->_pcTwinPointer);
this_ptr->ProjToPlane(*base_ptr, *line_ptr);
this_ptr->ProjectToPlane(*base_ptr, *line_ptr);
return Py::new_reference_to(this);
}

View File

@ -252,7 +252,7 @@ bool MeshGeomFacet::IsPointOf (const Base::Vector3f &rclPoint, float fDistance)
float fLP, fLE;
clNorm.Normalize();
clProjPt.ProjToPlane(_aclPoints[0], clNorm);
clProjPt.ProjectToPlane(_aclPoints[0], clNorm);
// Kante P0 --> P1
@ -351,7 +351,7 @@ bool MeshGeomFacet::Weights(const Base::Vector3f& rclP, float& w0, float& w1, fl
void MeshGeomFacet::ProjectPointToPlane (Base::Vector3f &rclPoint) const
{
rclPoint.ProjToPlane(_aclPoints[0], GetNormal());
rclPoint.ProjectToPlane(_aclPoints[0], GetNormal());
}
void MeshGeomFacet::ProjectFacetToPlane (MeshGeomFacet &rclFacet) const

View File

@ -1070,7 +1070,7 @@ bool MeshEigensystem::Evaluate()
for (MeshPointArray::_TConstIterator it = aclPoints.begin(); it!=aclPoints.end(); ++it) {
// u-Richtung
clVect = *it - _cC;
clProj.ProjToLine(clVect, _cU);
clProj.ProjectToLine(clVect, _cU);
clVect = clVect + clProj;
fH = clVect.Length();
@ -1083,7 +1083,7 @@ bool MeshEigensystem::Evaluate()
// v-Richtung
clVect = *it - _cC;
clProj.ProjToLine(clVect, _cV);
clProj.ProjectToLine(clVect, _cV);
clVect = clVect + clProj;
fH = clVect.Length();
@ -1096,7 +1096,7 @@ bool MeshEigensystem::Evaluate()
// w-Richtung
clVect = *it - _cC;
clProj.ProjToLine(clVect, _cW);
clProj.ProjectToLine(clVect, _cW);
clVect = clVect + clProj;
fH = clVect.Length();

View File

@ -303,7 +303,7 @@ bool MeshProjection::isPointInsideDistance (const Base::Vector3f& p1,
// project point on line
Base::Vector3f proj, dir(p2 - p1);
Base::Vector3f move(pt - p1);
proj.ProjToLine(move, dir);
proj.ProjectToLine(move, dir);
proj = pt + proj;
return (((p1 - proj) * (p2 - proj)) < 0.0f);
}

View File

@ -3537,8 +3537,8 @@ bool findFilletCenter(const GeomLineSegment *lineSeg1, const GeomLineSegment *li
// Just project the given reference points onto the lines, just in case they are not already lying on
Base::Vector3d normPnt1, normPnt2;
normPnt1.ProjToLine(refPnt1-l1p1, l1p2-l1p1);
normPnt2.ProjToLine(refPnt2-l2p1, l2p2-l2p1);
normPnt1.ProjectToLine(refPnt1-l1p1, l1p2-l1p1);
normPnt2.ProjectToLine(refPnt2-l2p1, l2p2-l2p1);
normPnt1 += refPnt1;
normPnt2 += refPnt2;
@ -3587,8 +3587,8 @@ double suggestFilletRadius(const GeomLineSegment *lineSeg1, const GeomLineSegmen
Base::Vector3d dirBisect = (dir1.Normalize() + dir2.Normalize()).Normalize();
Base::Vector3d projPnt1, projPnt2;
projPnt1.ProjToLine(refPnt1-corner, dir1);
projPnt2.ProjToLine(refPnt2-corner, dir2);
projPnt1.ProjectToLine(refPnt1-corner, dir1);
projPnt2.ProjectToLine(refPnt2-corner, dir2);
projPnt1 += refPnt1;
projPnt2 += refPnt2;
@ -3617,8 +3617,8 @@ GeomArcOfCircle *createFilletGeometry(const GeomLineSegment *lineSeg1, const Geo
Base::Vector3d dir2 = lineSeg2->getEndPoint() - lineSeg2->getStartPoint();
Base::Vector3d radDir1, radDir2;
radDir1.ProjToLine(center - corner, dir1);
radDir2.ProjToLine(center - corner, dir2);
radDir1.ProjectToLine(center - corner, dir1);
radDir2.ProjectToLine(center - corner, dir2);
// Angle Variables
double startAngle, endAngle, range;

View File

@ -89,7 +89,7 @@ bool ViewProviderMirror::setEdit(int ModNum)
Base::Vector3d base = mf->Base.getValue();
Base::Vector3d norm = mf->Normal.getValue();
Base::Vector3d cent = bbox.GetCenter();
base = cent.ProjToPlane(base, norm);
base = cent.ProjectToPlane(base, norm);
// setup the graph for editing the mirror plane
SoTransform* trans = new SoTransform;

View File

@ -702,7 +702,7 @@ void ParameterCorrection::ProjectControlPointsOnPlane()
for (unsigned k=0;k<_usVCtrlpoints;k++) {
gp_Pnt pole = _vCtrlPntsOfSurf(j,k);
Base::Vector3d pnt(pole.X(), pole.Y(), pole.Z());
pnt.ProjToPlane(base, _clW);
pnt.ProjectToPlane(base, _clW);
pole.SetX(pnt.x);
pole.SetY(pnt.y);
pole.SetZ(pnt.z);

View File

@ -962,8 +962,8 @@ int SketchObject::fillet(int GeoId1, int GeoId2,
delete arc;
return -1;
}
dist1.ProjToLine(arc->getStartPoint(/*emulateCCW=*/true)-intersection, dir1);
dist2.ProjToLine(arc->getStartPoint(/*emulateCCW=*/true)-intersection, dir2);
dist1.ProjectToLine(arc->getStartPoint(/*emulateCCW=*/true)-intersection, dir1);
dist2.ProjectToLine(arc->getStartPoint(/*emulateCCW=*/true)-intersection, dir2);
Part::Geometry *newgeo = dynamic_cast<Part::Geometry* >(arc);
filletId = addGeometry(newgeo);
if (filletId < 0) {

View File

@ -759,7 +759,7 @@ public:
EditCurve[EditCurve.size()-1] = onSketchPos;
if (TransitionMode == TRANSITION_MODE_Tangent) {
Base::Vector2D Tangent(dirVec.x,dirVec.y);
EditCurve[1].ProjToLine(EditCurve[2] - EditCurve[0], Tangent);
EditCurve[1].ProjectToLine(EditCurve[2] - EditCurve[0], Tangent);
if (EditCurve[1] * Tangent < 0) {
EditCurve[1] = EditCurve[2];
suppressTransition = true;
@ -770,7 +770,7 @@ public:
else if (TransitionMode == TRANSITION_MODE_Perpendicular_L ||
TransitionMode == TRANSITION_MODE_Perpendicular_R) {
Base::Vector2D Perpendicular(-dirVec.y,dirVec.x);
EditCurve[1].ProjToLine(EditCurve[2] - EditCurve[0], Perpendicular);
EditCurve[1].ProjectToLine(EditCurve[2] - EditCurve[0], Perpendicular);
EditCurve[1] = EditCurve[0] + EditCurve[1];
}
@ -2452,7 +2452,7 @@ private:
Base::Vector2D cursor = Base::Vector2D(onSketchPos - f); // vector from f to cursor pos
// decompose cursor with a projection, then length of w_2 will give us b
Base::Vector2D w_1 = cursor;
w_1.ProjToLine(cursor, (periapsis - apoapsis)); // projection of cursor line onto apse line
w_1.ProjectToLine(cursor, (periapsis - apoapsis)); // projection of cursor line onto apse line
Base::Vector2D w_2 = (cursor - w_1);
b = w_2.Length();
@ -2512,7 +2512,7 @@ private:
Base::Vector2D cursor = Base::Vector2D(onSketchPos - centroid); // vector from centroid to cursor pos
// decompose cursor with a projection, then length of w_2 will give us b
Base::Vector2D w_1 = cursor;
w_1.ProjToLine(cursor, (fixedAxis - centroid)); // projection of cursor line onto fixed axis line
w_1.ProjectToLine(cursor, (fixedAxis - centroid)); // projection of cursor line onto fixed axis line
Base::Vector2D w_2 = (cursor - w_1);
if (w_2.Length() > fixedAxisLength) {
// b is fixed, we are seeking a

View File

@ -263,7 +263,7 @@ int DrawSketchHandler::seekAutoConstraint(std::vector<AutoConstraint> &suggested
continue;
Base::Vector3d projPnt(0.f, 0.f, 0.f);
projPnt = projPnt.ProjToLine(center - tmpPos, tmpDir);
projPnt = projPnt.ProjectToLine(center - tmpPos, tmpDir);
double projDist = std::abs(projPnt.Length() - radius);
// Find if nearest
@ -311,7 +311,7 @@ int DrawSketchHandler::seekAutoConstraint(std::vector<AutoConstraint> &suggested
continue;
Base::Vector3d projPnt(0.f, 0.f, 0.f);
projPnt = projPnt.ProjToLine(center - tmpPos, tmpDir);
projPnt = projPnt.ProjectToLine(center - tmpPos, tmpDir);
double projDist = std::abs(projPnt.Length() - radius);
if (projDist < tangDeviation) {

View File

@ -1204,7 +1204,7 @@ void ViewProviderSketch::moveConstraint(int constNum, const Base::Vector2D &toPo
Base::Vector3d l2p1 = lineSeg->getStartPoint();
Base::Vector3d l2p2 = lineSeg->getEndPoint();
// calculate the projection of p1 onto line2
p2.ProjToLine(p1-l2p1, l2p2-l2p1);
p2.ProjectToLine(p1-l2p1, l2p2-l2p1);
p2 += p1;
} else
return;
@ -3516,7 +3516,7 @@ Restart:
Base::Vector3d l2p1 = lineSeg->getStartPoint();
Base::Vector3d l2p2 = lineSeg->getEndPoint();
// calculate the projection of p1 onto line2
pnt2.ProjToLine(pnt1-l2p1, l2p2-l2p1);
pnt2.ProjectToLine(pnt1-l2p1, l2p2-l2p1);
pnt2 += pnt1;
} else
break;

View File

@ -164,7 +164,7 @@ App::DocumentObjectExecReturn *DrawViewSection::execute(void)
// Project each bounding box point onto projection plane and find larges u,v values
Base::Vector3d pnt = (*it);
pnt.ProjToPlane(plnPnt, plnNorm);
pnt.ProjectToPlane(plnPnt, plnNorm);
uMax = std::max(uMax, std::abs(plnPnt[0] - pnt[0]));
vMax = std::max(vMax, std::abs(plnPnt[1] - pnt[1]));

View File

@ -1190,8 +1190,8 @@ void QGIViewDimension::draw()
Base::Vector3d avg = (norm1 + norm2) / 2.;
norm1 = norm1.ProjToLine(avg, norm1);
norm2 = norm2.ProjToLine(avg, norm2);
norm1 = norm1.ProjectToLine(avg, norm1);
norm2 = norm2.ProjectToLine(avg, norm2);
ar1->setPos(ar1Pos.x,ar1Pos.y );
ar2->setPos(ar2Pos.x,ar2Pos.y );