📄 imathvec.h
字号:
Vec2<T>::equalWithAbsError (const Vec2<T> &v, T e) const
{
for (int i = 0; i < 2; i++)
if (!Imath::equalWithAbsError ((*this)[i], v[i], e))
return false;
return true;
}
template <class T>
bool
Vec2<T>::equalWithRelError (const Vec2<T> &v, T e) const
{
for (int i = 0; i < 2; i++)
if (!Imath::equalWithRelError ((*this)[i], v[i], e))
return false;
return true;
}
template <class T>
inline T
Vec2<T>::dot (const Vec2 &v) const
{
return x * v.x + y * v.y;
}
template <class T>
inline T
Vec2<T>::operator ^ (const Vec2 &v) const
{
return dot (v);
}
template <class T>
inline T
Vec2<T>::cross (const Vec2 &v) const
{
return x * v.y - y * v.x;
}
template <class T>
inline T
Vec2<T>::operator % (const Vec2 &v) const
{
return x * v.y - y * v.x;
}
template <class T>
inline const Vec2<T> &
Vec2<T>::operator += (const Vec2 &v)
{
x += v.x;
y += v.y;
return *this;
}
template <class T>
inline Vec2<T>
Vec2<T>::operator + (const Vec2 &v) const
{
return Vec2 (x + v.x, y + v.y);
}
template <class T>
inline const Vec2<T> &
Vec2<T>::operator -= (const Vec2 &v)
{
x -= v.x;
y -= v.y;
return *this;
}
template <class T>
inline Vec2<T>
Vec2<T>::operator - (const Vec2 &v) const
{
return Vec2 (x - v.x, y - v.y);
}
template <class T>
inline Vec2<T>
Vec2<T>::operator - () const
{
return Vec2 (-x, -y);
}
template <class T>
inline const Vec2<T> &
Vec2<T>::negate ()
{
x = -x;
y = -y;
return *this;
}
template <class T>
inline const Vec2<T> &
Vec2<T>::operator *= (const Vec2 &v)
{
x *= v.x;
y *= v.y;
return *this;
}
template <class T>
inline const Vec2<T> &
Vec2<T>::operator *= (T a)
{
x *= a;
y *= a;
return *this;
}
template <class T>
inline Vec2<T>
Vec2<T>::operator * (const Vec2 &v) const
{
return Vec2 (x * v.x, y * v.y);
}
template <class T>
inline Vec2<T>
Vec2<T>::operator * (T a) const
{
return Vec2 (x * a, y * a);
}
template <class T>
inline const Vec2<T> &
Vec2<T>::operator /= (const Vec2 &v)
{
x /= v.x;
y /= v.y;
return *this;
}
template <class T>
inline const Vec2<T> &
Vec2<T>::operator /= (T a)
{
x /= a;
y /= a;
return *this;
}
template <class T>
inline Vec2<T>
Vec2<T>::operator / (const Vec2 &v) const
{
return Vec2 (x / v.x, y / v.y);
}
template <class T>
inline Vec2<T>
Vec2<T>::operator / (T a) const
{
return Vec2 (x / a, y / a);
}
template <class T>
inline T
Vec2<T>::length () const
{
return Math<T>::sqrt (dot (*this));
}
template <class T>
inline T
Vec2<T>::length2 () const
{
return dot (*this);
}
template <class T>
const Vec2<T> &
Vec2<T>::normalize ()
{
T l = length();
if (l != 0)
{
x /= l;
y /= l;
}
return *this;
}
template <class T>
const Vec2<T> &
Vec2<T>::normalizeExc () throw (Iex::MathExc)
{
T l = length();
if (l == 0)
throw NullVecExc ("Cannot normalize null vector.");
x /= l;
y /= l;
return *this;
}
template <class T>
inline
const Vec2<T> &
Vec2<T>::normalizeNonNull ()
{
T l = length();
x /= l;
y /= l;
return *this;
}
template <class T>
Vec2<T>
Vec2<T>::normalized () const
{
T l = length();
if (l == 0)
return Vec2 (T (0));
return Vec2 (x / l, y / l);
}
template <class T>
Vec2<T>
Vec2<T>::normalizedExc () const throw (Iex::MathExc)
{
T l = length();
if (l == 0)
throw NullVecExc ("Cannot normalize null vector.");
return Vec2 (x / l, y / l);
}
template <class T>
inline
Vec2<T>
Vec2<T>::normalizedNonNull () const
{
T l = length();
return Vec2 (x / l, y / l);
}
//-----------------------
// Implementation of Vec3
//-----------------------
template <class T>
inline T &
Vec3<T>::operator [] (int i)
{
return (&x)[i];
}
template <class T>
inline const T &
Vec3<T>::operator [] (int i) const
{
return (&x)[i];
}
template <class T>
inline
Vec3<T>::Vec3 ()
{
// empty
}
template <class T>
inline
Vec3<T>::Vec3 (T a)
{
x = y = z = a;
}
template <class T>
inline
Vec3<T>::Vec3 (T a, T b, T c)
{
x = a;
y = b;
z = c;
}
template <class T>
inline
Vec3<T>::Vec3 (const Vec3 &v)
{
x = v.x;
y = v.y;
z = v.z;
}
template <class T>
template <class S>
inline
Vec3<T>::Vec3 (const Vec3<S> &v)
{
x = T (v.x);
y = T (v.y);
z = T (v.z);
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator = (const Vec3 &v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
template <class T>
template <class S>
inline void
Vec3<T>::setValue (S a, S b, S c)
{
x = T (a);
y = T (b);
z = T (c);
}
template <class T>
template <class S>
inline void
Vec3<T>::setValue (const Vec3<S> &v)
{
x = T (v.x);
y = T (v.y);
z = T (v.z);
}
template <class T>
template <class S>
inline void
Vec3<T>::getValue (S &a, S &b, S &c) const
{
a = S (x);
b = S (y);
c = S (z);
}
template <class T>
template <class S>
inline void
Vec3<T>::getValue (Vec3<S> &v) const
{
v.x = S (x);
v.y = S (y);
v.z = S (z);
}
template <class T>
inline T *
Vec3<T>::getValue()
{
return (T *) &x;
}
template <class T>
inline const T *
Vec3<T>::getValue() const
{
return (const T *) &x;
}
template <class T>
template <class S>
inline bool
Vec3<T>::operator == (const Vec3<S> &v) const
{
return x == v.x && y == v.y && z == v.z;
}
template <class T>
template <class S>
inline bool
Vec3<T>::operator != (const Vec3<S> &v) const
{
return x != v.x || y != v.y || z != v.z;
}
template <class T>
bool
Vec3<T>::equalWithAbsError (const Vec3<T> &v, T e) const
{
for (int i = 0; i < 3; i++)
if (!Imath::equalWithAbsError ((*this)[i], v[i], e))
return false;
return true;
}
template <class T>
bool
Vec3<T>::equalWithRelError (const Vec3<T> &v, T e) const
{
for (int i = 0; i < 3; i++)
if (!Imath::equalWithRelError ((*this)[i], v[i], e))
return false;
return true;
}
template <class T>
inline T
Vec3<T>::dot (const Vec3 &v) const
{
return x * v.x + y * v.y + z * v.z;
}
template <class T>
inline T
Vec3<T>::operator ^ (const Vec3 &v) const
{
return dot (v);
}
template <class T>
inline Vec3<T>
Vec3<T>::cross (const Vec3 &v) const
{
return Vec3 (y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x);
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator %= (const Vec3 &v)
{
T a = y * v.z - z * v.y;
T b = z * v.x - x * v.z;
T c = x * v.y - y * v.x;
x = a;
y = b;
z = c;
return *this;
}
template <class T>
inline Vec3<T>
Vec3<T>::operator % (const Vec3 &v) const
{
return Vec3 (y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x);
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator += (const Vec3 &v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
template <class T>
inline Vec3<T>
Vec3<T>::operator + (const Vec3 &v) const
{
return Vec3 (x + v.x, y + v.y, z + v.z);
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator -= (const Vec3 &v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
template <class T>
inline Vec3<T>
Vec3<T>::operator - (const Vec3 &v) const
{
return Vec3 (x - v.x, y - v.y, z - v.z);
}
template <class T>
inline Vec3<T>
Vec3<T>::operator - () const
{
return Vec3 (-x, -y, -z);
}
template <class T>
inline const Vec3<T> &
Vec3<T>::negate ()
{
x = -x;
y = -y;
z = -z;
return *this;
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator *= (const Vec3 &v)
{
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator *= (T a)
{
x *= a;
y *= a;
z *= a;
return *this;
}
template <class T>
inline Vec3<T>
Vec3<T>::operator * (const Vec3 &v) const
{
return Vec3 (x * v.x, y * v.y, z * v.z);
}
template <class T>
inline Vec3<T>
Vec3<T>::operator * (T a) const
{
return Vec3 (x * a, y * a, z * a);
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator /= (const Vec3 &v)
{
x /= v.x;
y /= v.y;
z /= v.z;
return *this;
}
template <class T>
inline const Vec3<T> &
Vec3<T>::operator /= (T a)
{
x /= a;
y /= a;
z /= a;
return *this;
}
template <class T>
inline Vec3<T>
Vec3<T>::operator / (const Vec3 &v) const
{
return Vec3 (x / v.x, y / v.y, z / v.z);
}
template <class T>
inline Vec3<T>
Vec3<T>::operator / (T a) const
{
return Vec3 (x / a, y / a, z / a);
}
template <class T>
inline T
Vec3<T>::length () const
{
return Math<T>::sqrt (dot (*this));
}
template <class T>
inline T
Vec3<T>::length2 () const
{
return dot (*this);
}
template <class T>
const Vec3<T> &
Vec3<T>::normalize ()
{
T l = length();
if (l != 0)
{
x /= l;
y /= l;
z /= l;
}
return *this;
}
template <class T>
const Vec3<T> &
Vec3<T>::normalizeExc () throw (Iex::MathExc)
{
T l = length();
if (l == 0)
throw NullVecExc ("Cannot normalize null vector.");
x /= l;
y /= l;
z /= l;
return *this;
}
template <class T>
inline
const Vec3<T> &
Vec3<T>::normalizeNonNull ()
{
T l = length();
x /= l;
y /= l;
z /= l;
return *this;
}
template <class T>
Vec3<T>
Vec3<T>::normalized () const
{
T l = length();
if (l == 0)
return Vec3 (T (0));
return Vec3 (x / l, y / l, z / l);
}
template <class T>
Vec3<T>
Vec3<T>::normalizedExc () const throw (Iex::MathExc)
{
T l = length();
if (l == 0)
throw NullVecExc ("Cannot normalize null vector.");
return Vec3 (x / l, y / l, z / l);
}
template <class T>
inline
Vec3<T>
Vec3<T>::normalizedNonNull () const
{
T l = length();
return Vec3 (x / l, y / l, z / l);
}
//-----------------------------
// Stream output implementation
//-----------------------------
template <class T>
std::ostream &
operator << (std::ostream &s, const Vec2<T> &v)
{
return s << '(' << v.x << ' ' << v.y << ')';
}
template <class T>
std::ostream &
operator << (std::ostream &s, const Vec3<T> &v)
{
return s << '(' << v.x << ' ' << v.y << ' ' << v.z << ')';
}
//-----------------------------------------
// Implementation of reverse multiplication
//-----------------------------------------
template <class T>
inline Vec2<T>
operator * (T a, const Vec2<T> &v)
{
return Vec2<T> (a * v.x, a * v.y);
}
template <class T>
inline Vec3<T>
operator * (T a, const Vec3<T> &v)
{
return Vec3<T> (a * v.x, a * v.y, a * v.z);
}
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
#pragma warning(default:4290)
#endif
} // namespace Imath
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -