00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #pragma once
00016 #ifndef VHACD_VECTOR_H
00017 #define VHACD_VECTOR_H
00018 #include <iostream>
00019 #include <math.h>
00020
00021 namespace VHACD {
00023 template <typename T>
00024 class Vec3 {
00025 public:
00026 T& operator[](size_t i) { return m_data[i]; }
00027 const T& operator[](size_t i) const { return m_data[i]; }
00028 T& X();
00029 T& Y();
00030 T& Z();
00031 const T& X() const;
00032 const T& Y() const;
00033 const T& Z() const;
00034 void Normalize();
00035 T GetNorm() const;
00036 void operator=(const Vec3& rhs);
00037 void operator+=(const Vec3& rhs);
00038 void operator-=(const Vec3& rhs);
00039 void operator-=(T a);
00040 void operator+=(T a);
00041 void operator/=(T a);
00042 void operator*=(T a);
00043 Vec3 operator^(const Vec3& rhs) const;
00044 T operator*(const Vec3& rhs) const;
00045 Vec3 operator+(const Vec3& rhs) const;
00046 Vec3 operator-(const Vec3& rhs) const;
00047 Vec3 operator-() const;
00048 Vec3 operator*(T rhs) const;
00049 Vec3 operator/(T rhs) const;
00050 bool operator<(const Vec3& rhs) const;
00051 bool operator>(const Vec3& rhs) const;
00052 Vec3();
00053 Vec3(T a);
00054 Vec3(T x, T y, T z);
00055 Vec3(const Vec3& rhs);
00056 ~Vec3(void);
00057
00058
00059 T GetCenter(const Vec3 &bmin, const Vec3 &bmax)
00060 {
00061 X() = (bmin.X() + bmax.X())*0.5;
00062 Y() = (bmin.Y() + bmax.Y())*0.5;
00063 Z() = (bmin.Z() + bmax.Z())*0.5;
00064 T dx = bmax.X() - bmin.X();
00065 T dy = bmax.Y() - bmin.Y();
00066 T dz = bmax.Z() - bmin.Z();
00067 T diagonal = T(sqrt(dx*dx + dy*dy + dz*dz));
00068 return diagonal;
00069 }
00070
00071
00072 void UpdateMinMax(Vec3 &bmin,Vec3 &bmax) const
00073 {
00074 if (X() < bmin.X())
00075 {
00076 bmin.X() = X();
00077 }
00078 if (Y() < bmin.Y())
00079 {
00080 bmin.Y() = Y();
00081 }
00082 if (Z() < bmin.Z())
00083 {
00084 bmin.Z() = Z();
00085 }
00086 if (X() > bmax.X())
00087 {
00088 bmax.X() = X();
00089 }
00090 if (X() > bmax.X())
00091 {
00092 bmax.X() = X();
00093 }
00094 if (Y() > bmax.Y())
00095 {
00096 bmax.Y() = Y();
00097 }
00098 if (Z() > bmax.Z())
00099 {
00100 bmax.Z() = Z();
00101 }
00102 }
00103
00104
00105 T GetDistanceSquared(const Vec3 &p) const
00106 {
00107 T dx = X() - p.X();
00108 T dy = Y() - p.Y();
00109 T dz = Z() - p.Z();
00110 return dx*dx + dy*dy + dz*dz;
00111 }
00112
00113 T GetDistance(const Vec3 &p) const
00114 {
00115 return sqrt(GetDistanceSquared(p));
00116 }
00117
00118
00119 T* GetData(void)
00120 {
00121 return m_data;
00122 }
00123 private:
00124 T m_data[3];
00125 };
00127 template <typename T>
00128 class Vec2 {
00129 public:
00130 T& operator[](size_t i) { return m_data[i]; }
00131 const T& operator[](size_t i) const { return m_data[i]; }
00132 T& X();
00133 T& Y();
00134 const T& X() const;
00135 const T& Y() const;
00136 void Normalize();
00137 T GetNorm() const;
00138 void operator=(const Vec2& rhs);
00139 void operator+=(const Vec2& rhs);
00140 void operator-=(const Vec2& rhs);
00141 void operator-=(T a);
00142 void operator+=(T a);
00143 void operator/=(T a);
00144 void operator*=(T a);
00145 T operator^(const Vec2& rhs) const;
00146 T operator*(const Vec2& rhs) const;
00147 Vec2 operator+(const Vec2& rhs) const;
00148 Vec2 operator-(const Vec2& rhs) const;
00149 Vec2 operator-() const;
00150 Vec2 operator*(T rhs) const;
00151 Vec2 operator/(T rhs) const;
00152 Vec2();
00153 Vec2(T a);
00154 Vec2(T x, T y);
00155 Vec2(const Vec2& rhs);
00156 ~Vec2(void);
00157
00158 private:
00159 T m_data[2];
00160 };
00161
00162 template <typename T>
00163 const bool Colinear(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c);
00164 template <typename T>
00165 const T ComputeVolume4(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c, const Vec3<T>& d);
00166 }
00167 #include "vhacdVector.inl"
00168 #endif