| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#pragma once |
| 16 |
#ifndef VHACD_VECTOR_H |
| 17 |
#define VHACD_VECTOR_H |
| 18 |
#include <iostream> |
| 19 |
#include <math.h> |
| 20 |
|
| 21 |
namespace VHACD { |
| 22 |
|
| 23 |
template <typename T> |
| 24 |
class Vec3 { |
| 25 |
public: |
| 26 |
T& operator[](size_t i) { return m_data[i]; } |
| 27 |
const T& operator[](size_t i) const { return m_data[i]; } |
| 28 |
T& X(); |
| 29 |
T& Y(); |
| 30 |
T& Z(); |
| 31 |
const T& X() const; |
| 32 |
const T& Y() const; |
| 33 |
const T& Z() const; |
| 34 |
void Normalize(); |
| 35 |
T GetNorm() const; |
| 36 |
void operator=(const Vec3& rhs); |
| 37 |
void operator+=(const Vec3& rhs); |
| 38 |
void operator-=(const Vec3& rhs); |
| 39 |
void operator-=(T a); |
| 40 |
void operator+=(T a); |
| 41 |
void operator/=(T a); |
| 42 |
void operator*=(T a); |
| 43 |
Vec3 operator^(const Vec3& rhs) const; |
| 44 |
T operator*(const Vec3& rhs) const; |
| 45 |
Vec3 operator+(const Vec3& rhs) const; |
| 46 |
Vec3 operator-(const Vec3& rhs) const; |
| 47 |
Vec3 operator-() const; |
| 48 |
Vec3 operator*(T rhs) const; |
| 49 |
Vec3 operator/(T rhs) const; |
| 50 |
bool operator<(const Vec3& rhs) const; |
| 51 |
bool operator>(const Vec3& rhs) const; |
| 52 |
Vec3(); |
| 53 |
Vec3(T a); |
| 54 |
Vec3(T x, T y, T z); |
| 55 |
Vec3(const Vec3& rhs); |
| 56 |
~Vec3(void); |
| 57 |
|
| 58 |
|
| 59 |
T GetCenter(const Vec3 &bmin, const Vec3 &bmax) |
| 60 |
{ |
| 61 |
X() = (bmin.X() + bmax.X())*0.5; |
| 62 |
Y() = (bmin.Y() + bmax.Y())*0.5; |
| 63 |
Z() = (bmin.Z() + bmax.Z())*0.5; |
| 64 |
T dx = bmax.X() - bmin.X(); |
| 65 |
T dy = bmax.Y() - bmin.Y(); |
| 66 |
T dz = bmax.Z() - bmin.Z(); |
| 67 |
T diagonal = T(sqrt(dx*dx + dy*dy + dz*dz)); |
| 68 |
return diagonal; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
void UpdateMinMax(Vec3 &bmin,Vec3 &bmax) const |
| 73 |
{ |
| 74 |
if (X() < bmin.X()) |
| 75 |
{ |
| 76 |
bmin.X() = X(); |
| 77 |
} |
| 78 |
if (Y() < bmin.Y()) |
| 79 |
{ |
| 80 |
bmin.Y() = Y(); |
| 81 |
} |
| 82 |
if (Z() < bmin.Z()) |
| 83 |
{ |
| 84 |
bmin.Z() = Z(); |
| 85 |
} |
| 86 |
if (X() > bmax.X()) |
| 87 |
{ |
| 88 |
bmax.X() = X(); |
| 89 |
} |
| 90 |
if (X() > bmax.X()) |
| 91 |
{ |
| 92 |
bmax.X() = X(); |
| 93 |
} |
| 94 |
if (Y() > bmax.Y()) |
| 95 |
{ |
| 96 |
bmax.Y() = Y(); |
| 97 |
} |
| 98 |
if (Z() > bmax.Z()) |
| 99 |
{ |
| 100 |
bmax.Z() = Z(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
T GetDistanceSquared(const Vec3 &p) const |
| 106 |
{ |
| 107 |
T dx = X() - p.X(); |
| 108 |
T dy = Y() - p.Y(); |
| 109 |
T dz = Z() - p.Z(); |
| 110 |
return dx*dx + dy*dy + dz*dz; |
| 111 |
} |
| 112 |
|
| 113 |
T GetDistance(const Vec3 &p) const |
| 114 |
{ |
| 115 |
return sqrt(GetDistanceSquared(p)); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
T* GetData(void) |
| 120 |
{ |
| 121 |
return m_data; |
| 122 |
} |
| 123 |
private: |
| 124 |
T m_data[3]; |
| 125 |
}; |
| 126 |
|
| 127 |
template <typename T> |
| 128 |
class Vec2 { |
| 129 |
public: |
| 130 |
T& operator[](size_t i) { return m_data[i]; } |
| 131 |
const T& operator[](size_t i) const { return m_data[i]; } |
| 132 |
T& X(); |
| 133 |
T& Y(); |
| 134 |
const T& X() const; |
| 135 |
const T& Y() const; |
| 136 |
void Normalize(); |
| 137 |
T GetNorm() const; |
| 138 |
void operator=(const Vec2& rhs); |
| 139 |
void operator+=(const Vec2& rhs); |
| 140 |
void operator-=(const Vec2& rhs); |
| 141 |
void operator-=(T a); |
| 142 |
void operator+=(T a); |
| 143 |
void operator/=(T a); |
| 144 |
void operator*=(T a); |
| 145 |
T operator^(const Vec2& rhs) const; |
| 146 |
T operator*(const Vec2& rhs) const; |
| 147 |
Vec2 operator+(const Vec2& rhs) const; |
| 148 |
Vec2 operator-(const Vec2& rhs) const; |
| 149 |
Vec2 operator-() const; |
| 150 |
Vec2 operator*(T rhs) const; |
| 151 |
Vec2 operator/(T rhs) const; |
| 152 |
Vec2(); |
| 153 |
Vec2(T a); |
| 154 |
Vec2(T x, T y); |
| 155 |
Vec2(const Vec2& rhs); |
| 156 |
~Vec2(void); |
| 157 |
|
| 158 |
private: |
| 159 |
T m_data[2]; |
| 160 |
}; |
| 161 |
|
| 162 |
template <typename T> |
| 163 |
const bool Colinear(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c); |
| 164 |
template <typename T> |
| 165 |
const T ComputeVolume4(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c, const Vec3<T>& d); |
| 166 |
} |
| 167 |
#include "vhacdVector.inl" |
| 168 |
#endif |