tf Vector3

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

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值