www.pudn.com > cad3d.zip > 3DVector.h
#ifndef __C3DVector_h__
#define __C3DVector_h__
#include "htf.h"
#include "3DMath/3DPoint.h"
class MATH3D_API C3DVector : public C3DPoint
{
public:
C3DVector();
explicit C3DVector(const C3DPoint& rPt);
C3DVector(math_real x, math_real y, math_real z);
C3DVector& operator = (const C3DPoint& rPt);
C3DVector& SetFromPoint(const C3DPoint& rPt);
C3DVector& Normalize();
C3DVector& SafeNormalize()
{
math_real k = (math_real)htf::max( htf::max(fabs(m_dPt[0]), fabs(m_dPt[1])), fabs(m_dPt[2]) );
CHECK(!IsZero(k));
(*this) /= k;
return Normalize();
}
math_real Norm() const;
math_real Norm2() const;
C3DVector operator ^ (const C3DVector& rVec) const;
bool IsColinear(const C3DVector& rVec) const;
void Set(math_real x = 0, math_real y = 0, math_real z = 0)
{
m_dPt[0] = x; m_dPt[1] = y; m_dPt[2] = z;
}
};
inline C3DVector::C3DVector()
: C3DPoint(0,0,0,0)
{
}
inline C3DVector::C3DVector(const C3DPoint& rPt)
: C3DPoint(rPt)
{
CHECK(rPt.IsVector());
}
inline C3DVector::C3DVector(math_real x, math_real y, math_real z)
: C3DPoint(x,y,z,0)
{
}
inline C3DVector& C3DVector::operator = (const C3DPoint& rPt)
{
CHECK( rPt.IsVector() );
this->C3DPoint::operator = (rPt);
return *this;
}
inline C3DVector& C3DVector::SetFromPoint(const C3DPoint& rPt)
{
CHECK( !rPt.IsVector() );
math_real dWInv = ((math_real)1.0)/rPt(3);
m_dPt[0] = rPt(0)*dWInv;
m_dPt[1] = rPt(1)*dWInv;
m_dPt[2] = rPt(2)*dWInv;
return *this;
}
inline C3DVector& C3DVector::Normalize()
{
/*
math_real dNorm = Norm();
if (!IsZero(dNorm))
{
*this /= dNorm;
}
*/
CHECK(!IsZero(Norm()));
(*this) /= Norm();
return *this;
}
inline math_real C3DVector::Norm() const
{
return (math_real)sqrt(Norm2());
}
inline math_real C3DVector::Norm2() const
{
return (m_dPt[0]*m_dPt[0] + m_dPt[1]*m_dPt[1] + m_dPt[2]*m_dPt[2]);
}
inline C3DVector C3DVector::operator ^ (const C3DVector& rVec) const
{
CHECK( IsVector() );
CHECK( rVec.IsVector() );
C3DVector res;
res.m_dPt[0] = m_dPt[1]*rVec.m_dPt[2] - m_dPt[2]*rVec.m_dPt[1];
res.m_dPt[1] = m_dPt[2]*rVec.m_dPt[0] - m_dPt[0]*rVec.m_dPt[2];
res.m_dPt[2] = m_dPt[0]*rVec.m_dPt[1] - m_dPt[1]*rVec.m_dPt[0];
return res;
}
inline bool C3DVector::IsColinear(const C3DVector& rVec) const
{
// return IsEq(fabs((*this)*rVec),sqrt(Norm2() * rVec.Norm2()) );
return IsEq((math_real)fabs((*this)*rVec),Norm()*rVec.Norm());
}
#endif //__C3DVector_h__