Skip to content

Commit

Permalink
+ rename methods in Vector3 class
Browse files Browse the repository at this point in the history
+ add convenience methods Cross and Dot to Vector3 class
+ fix bug in DistanceToLineSegment in Vector3 class
  • Loading branch information
wwmayer committed Jul 30, 2016
1 parent 5c095de commit 1e2e24b
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 212 deletions.
54 changes: 27 additions & 27 deletions src/Base/BoundBox.h
Expand Up @@ -29,7 +29,7 @@
#include "ViewProj.h"
#include "Tools2D.h"
#include <iostream>
#include <limits>
#include <limits>

namespace Base {

Expand Down Expand Up @@ -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;
Expand All @@ -843,31 +843,31 @@ inline Vector3<_Precision> BoundBox3<_Precision>::ClosestPoint (const Vector3<_P

return clRet;
#else
Vector3<_Precision> closest = rclPt;

Vector3<_Precision> center = GetCenter();
_Precision devx = closest.x - center.x;
_Precision devy = closest.y - center.y;
_Precision devz = closest.z - center.z;

_Precision halfwidth = (MaxX - MinX) / 2;
_Precision halfheight = (MaxY - MinY) / 2;
_Precision halfdepth = (MaxZ - MinZ) / 2;

// Move point to be on the nearest plane of the box.
if ((fabs(devx) > fabs(devy)) && (fabs(devx) > fabs(devz)))
closest.x = center.x + halfwidth * ((devx < 0.0) ? -1.0 : 1.0);
else if (fabs(devy) > fabs(devz))
closest.y = center.y + halfheight * ((devy < 0.0) ? -1.0 : 1.0);
else
closest.z = center.z + halfdepth * ((devz < 0.0) ? -1.0 : 1.0);

// Clamp to be inside box.
closest.x = std::min<_Precision>(std::max<_Precision>(closest.x, MinX), MaxX);
closest.y = std::min<_Precision>(std::max<_Precision>(closest.y, MinY), MaxY);
closest.z = std::min<_Precision>(std::max<_Precision>(closest.z, MinZ), MaxZ);

return closest;
Vector3<_Precision> closest = rclPt;

Vector3<_Precision> center = GetCenter();
_Precision devx = closest.x - center.x;
_Precision devy = closest.y - center.y;
_Precision devz = closest.z - center.z;

_Precision halfwidth = (MaxX - MinX) / 2;
_Precision halfheight = (MaxY - MinY) / 2;
_Precision halfdepth = (MaxZ - MinZ) / 2;

// Move point to be on the nearest plane of the box.
if ((fabs(devx) > fabs(devy)) && (fabs(devx) > fabs(devz)))
closest.x = center.x + halfwidth * ((devx < 0.0) ? -1.0 : 1.0);
else if (fabs(devy) > fabs(devz))
closest.y = center.y + halfheight * ((devy < 0.0) ? -1.0 : 1.0);
else
closest.z = center.z + halfdepth * ((devz < 0.0) ? -1.0 : 1.0);

// Clamp to be inside box.
closest.x = std::min<_Precision>(std::max<_Precision>(closest.x, MinX), MaxX);
closest.y = std::min<_Precision>(std::max<_Precision>(closest.y, MinY), MaxY);
closest.z = std::min<_Precision>(std::max<_Precision>(closest.z, MinZ), MaxZ);

return closest;
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/Base/Tools2D.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Base/Tools2D.h
Expand Up @@ -68,7 +68,7 @@ class BaseExport Vector2D
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 ********************************************/
Expand Down
64 changes: 42 additions & 22 deletions src/Base/Vector3D.cpp
Expand Up @@ -22,6 +22,7 @@


#include "PreCompiled.h"
#include "Tools.h"
#include "Vector3D.h"

using namespace Base;
Expand Down Expand Up @@ -116,7 +117,7 @@ Vector3<_Precision>& Vector3<_Precision>::operator -= (const Vector3<_Precision>
{
x -= rcVct.x;
y -= rcVct.y;
z -= rcVct.z;
z -= rcVct.z;
return *this;
}

Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -190,14 +207,23 @@ bool Vector3<_Precision>::operator == (const Vector3<_Precision>& rcVct) const
}

template <class _Precision>
Vector3<_Precision>& Vector3<_Precision>::ProjToPlane (const Vector3<_Precision> &rclBase,
const Vector3<_Precision> &rclNorm)
Vector3<_Precision>& Vector3<_Precision>::ProjectToPlane (const Vector3<_Precision> &rclBase,
const Vector3<_Precision> &rclNorm)
{
Vector3<_Precision> clTemp(rclNorm);
*this = *this - (clTemp *= ((*this - rclBase) * clTemp) / clTemp.Sqr());
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
Expand All @@ -219,30 +245,24 @@ _Precision Vector3<_Precision>::DistanceToLine (const Vector3<_Precision> &rclBa
}

template <class _Precision>
Vector3<_Precision> Vector3<_Precision>::DistanceToLineSegment (const Vector3& rclP1,
const Vector3& rclP2) const
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,
const Vector3<_Precision> &rclLine)
Vector3<_Precision>& Vector3<_Precision>::ProjectToLine (const Vector3<_Precision> &rclPoint,
const Vector3<_Precision> &rclLine)
{
return (*this = ((((rclPoint * rclLine) / rclLine.Sqr()) * rclLine) - rclPoint));
}
Expand Down
15 changes: 12 additions & 3 deletions src/Base/Vector3D.h
Expand Up @@ -117,8 +117,12 @@ class Vector3
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
Expand Down Expand Up @@ -159,18 +163,23 @@ class Vector3
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.
* The result is a vector from \a rclPoint to the point on the line. The length of this vector
* 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
Expand Down
4 changes: 2 additions & 2 deletions src/Base/VectorPyImp.cpp
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Mesh/App/Core/Elements.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1e2e24b

Please sign in to comment.