+ 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 56f5b9c568
commit c294c8bdbd
18 changed files with 241 additions and 212 deletions

View File

@@ -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));
}