+ 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:
@@ -29,7 +29,7 @@
|
||||
#include "ViewProj.h"
|
||||
#include "Tools2D.h"
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <limits>
|
||||
|
||||
namespace Base {
|
||||
|
||||
@@ -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;
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 ********************************************/
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
#include "Tools.h"
|
||||
#include "Vector3D.h"
|
||||
|
||||
using namespace Base;
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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,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
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user