class Vector3
{
public:
tfScalar m_floats[4];
public:
TFSIMD_FORCE_INLINE Vector3(const tfScalar& x, const tfScalar& y, const tfScalar& z)
{
m_floats[0] = x;
m_floats[1] = y;
m_floats[2] = z;
m_floats[3] = tfScalar(0);
}
TFSIMD_FORCE_INLINE Vector3& oprator+=(const Vector3& v)
{
m_floats[0] += v.m_floats[0]; m_floats[1] += v.m_floats[1]; m_floats[2] += v.m_floats[2];
return *this;
}
// +-
TFSIMD_FORCE_INLINE Vector3& operator*=(const tfScalar& s)
{
m_floats[0] *=s; m_floats[1] *=s; m_floats[2] *=s;
return *this;
}
// */
TFSIMD_FORCE_INLINE tfScalar dot(const Vector3& v)const
{
return m_floats[0] * v.m_floats[0] + m_floats[1] * v.m_floats[1] + m_floats[2] * v.m_floats[2];
}
TFSIMD_FORCE_INLINE tfScalar length2() const
{
return dot(*this);
}
TFSIMD_FORCE_INLINE tfScalar length() const
{
return tfSqrt(length2());
}
TFSIMD_FORCE_INLINE tfScalar distance2(const Vector3& v)const
{
return (v - *this).length2();
}
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3& v)const
{
return (v - *this).length();
}
TFSIMD_FORCE_INLINE Vector3& normalize()
{
return *this /= length();
}
}
绕某一向量旋转某一角度
TFSIMD_FORCE_INLINE Vector3 Vector3::rotate( const Vector3& wAxis, const tfScalar angle) const
{
// wAxis must be a unit lenght vector
Vector3 o = wAxis * wAxis.dot(*this);
Vector3 x = *this - o;
Vector3 y;
y = wAxis.cross( *this );
return ( o + x * tfCos( angle ) + y * tfSin( angle ) );
}
//罗德里格斯公式, 原始向量v, 旋转轴k, 旋转角度theta
// v * cos(theta) + (k × v )*sin(theta) + k(k*v)*(1-cos(theta))
// = k(k*v) + (v - k(k*v))*cos(theta) + (k × v )*sin(theta)
两向量的夹角
TFSIMD_FORCE_INLINE tfSca;ar angle(const Vector3& v) const
{
tfScalar s = tfSqrt(length2() * v.length2());
tfFullAssert(s != tfScalar(0.0));
return tfAcos(dot(v) /s );
}
两向量叉乘
TFSIMD_FORCE_INLINE Vector3 cross(const Vector3& v) const
{
return Vector3(
m_floats[1] * v.m_floats[2] - m_floats[2] * v.m_floats[1],
m_floats[2] * v.m_floats[0] - m_floats[0] * v.m_floats[2],
m_floats[0] * v.m_floats[1] - m_floats[1] * v.m_floats[0]);
}