1#pragma once
2#ifndef VHACD_VECTOR_INL
3#define VHACD_VECTOR_INL
4namespace VHACD
5{
6 template <typename T>
7 inline Vec3<T> operator*(T lhs, const Vec3<T> & rhs)
8 {
9 return Vec3<T>(lhs * rhs.X(), lhs * rhs.Y(), lhs * rhs.Z());
10 }
11 template <typename T>
12 inline T & Vec3<T>::X()
13 {
14 return m_data[0];
15 }
16 template <typename T>
17 inline T & Vec3<T>::Y()
18 {
19 return m_data[1];
20 }
21 template <typename T>
22 inline T & Vec3<T>::Z()
23 {
24 return m_data[2];
25 }
26 template <typename T>
27 inline const T & Vec3<T>::X() const
28 {
29 return m_data[0];
30 }
31 template <typename T>
32 inline const T & Vec3<T>::Y() const
33 {
34 return m_data[1];
35 }
36 template <typename T>
37 inline const T & Vec3<T>::Z() const
38 {
39 return m_data[2];
40 }
41 template <typename T>
42 inline void Vec3<T>::Normalize()
43 {
44 T n = sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]+m_data[2]*m_data[2]);
45 if (n != 0.0) (*this) /= n;
46 }
47 template <typename T>
48 inline T Vec3<T>::GetNorm() const
49 {
50 return sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]+m_data[2]*m_data[2]);
51 }
52 template <typename T>
53 inline void Vec3<T>::operator= (const Vec3 & rhs)
54 {
55 this->m_data[0] = rhs.m_data[0];
56 this->m_data[1] = rhs.m_data[1];
57 this->m_data[2] = rhs.m_data[2];
58 }
59 template <typename T>
60 inline void Vec3<T>::operator+=(const Vec3 & rhs)
61 {
62 this->m_data[0] += rhs.m_data[0];
63 this->m_data[1] += rhs.m_data[1];
64 this->m_data[2] += rhs.m_data[2];
65 }
66 template <typename T>
67 inline void Vec3<T>::operator-=(const Vec3 & rhs)
68 {
69 this->m_data[0] -= rhs.m_data[0];
70 this->m_data[1] -= rhs.m_data[1];
71 this->m_data[2] -= rhs.m_data[2];
72 }
73 template <typename T>
74 inline void Vec3<T>::operator-=(T a)
75 {
76 this->m_data[0] -= a;
77 this->m_data[1] -= a;
78 this->m_data[2] -= a;
79 }
80 template <typename T>
81 inline void Vec3<T>::operator+=(T a)
82 {
83 this->m_data[0] += a;
84 this->m_data[1] += a;
85 this->m_data[2] += a;
86 }
87 template <typename T>
88 inline void Vec3<T>::operator/=(T a)
89 {
90 this->m_data[0] /= a;
91 this->m_data[1] /= a;
92 this->m_data[2] /= a;
93 }
94 template <typename T>
95 inline void Vec3<T>::operator*=(T a)
96 {
97 this->m_data[0] *= a;
98 this->m_data[1] *= a;
99 this->m_data[2] *= a;
100 }
101 template <typename T>
102 inline Vec3<T> Vec3<T>::operator^ (const Vec3<T> & rhs) const
103 {
104 return Vec3<T>(m_data[1] * rhs.m_data[2] - m_data[2] * rhs.m_data[1],
105 m_data[2] * rhs.m_data[0] - m_data[0] * rhs.m_data[2],
106 m_data[0] * rhs.m_data[1] - m_data[1] * rhs.m_data[0]);
107 }
108 template <typename T>
109 inline T Vec3<T>::operator*(const Vec3<T> & rhs) const
110 {
111 return (m_data[0] * rhs.m_data[0] + m_data[1] * rhs.m_data[1] + m_data[2] * rhs.m_data[2]);
112 }
113 template <typename T>
114 inline Vec3<T> Vec3<T>::operator+(const Vec3<T> & rhs) const
115 {
116 return Vec3<T>(m_data[0] + rhs.m_data[0],m_data[1] + rhs.m_data[1],m_data[2] + rhs.m_data[2]);
117 }
118 template <typename T>
119 inline Vec3<T> Vec3<T>::operator-(const Vec3<T> & rhs) const
120 {
121 return Vec3<T>(m_data[0] - rhs.m_data[0],m_data[1] - rhs.m_data[1],m_data[2] - rhs.m_data[2]) ;
122 }
123 template <typename T>
124 inline Vec3<T> Vec3<T>::operator-() const
125 {
126 return Vec3<T>(-m_data[0],-m_data[1],-m_data[2]) ;
127 }
128
129 template <typename T>
130 inline Vec3<T> Vec3<T>::operator*(T rhs) const
131 {
132 return Vec3<T>(rhs * this->m_data[0], rhs * this->m_data[1], rhs * this->m_data[2]);
133 }
134 template <typename T>
135 inline Vec3<T> Vec3<T>::operator/ (T rhs) const
136 {
137 return Vec3<T>(m_data[0] / rhs, m_data[1] / rhs, m_data[2] / rhs);
138 }
139 template <typename T>
140 inline Vec3<T>::Vec3(T a)
141 {
142 m_data[0] = m_data[1] = m_data[2] = a;
143 }
144 template <typename T>
145 inline Vec3<T>::Vec3(T x, T y, T z)
146 {
147 m_data[0] = x;
148 m_data[1] = y;
149 m_data[2] = z;
150 }
151 template <typename T>
152 inline Vec3<T>::Vec3(const Vec3 & rhs)
153 {
154 m_data[0] = rhs.m_data[0];
155 m_data[1] = rhs.m_data[1];
156 m_data[2] = rhs.m_data[2];
157 }
158 template <typename T>
159 inline Vec3<T>::~Vec3(void){};
160
161 template <typename T>
162 inline Vec3<T>::Vec3() {}
163
164 template<typename T>
165 inline const bool Colinear(const Vec3<T> & a, const Vec3<T> & b, const Vec3<T> & c)
166 {
167 return ((c.Z() - a.Z()) * (b.Y() - a.Y()) - (b.Z() - a.Z()) * (c.Y() - a.Y()) == 0.0 /*EPS*/) &&
168 ((b.Z() - a.Z()) * (c.X() - a.X()) - (b.X() - a.X()) * (c.Z() - a.Z()) == 0.0 /*EPS*/) &&
169 ((b.X() - a.X()) * (c.Y() - a.Y()) - (b.Y() - a.Y()) * (c.X() - a.X()) == 0.0 /*EPS*/);
170 }
171
172 template<typename T>
173 inline const T ComputeVolume4(const Vec3<T> & a, const Vec3<T> & b, const Vec3<T> & c, const Vec3<T> & d)
174 {
175 return (a-d) * ((b-d) ^ (c-d));
176 }
177
178 template <typename T>
179 inline bool Vec3<T>::operator<(const Vec3 & rhs) const
180 {
181 if (X() == rhs[0])
182 {
183 if (Y() == rhs[1])
184 {
185 return (Z()<rhs[2]);
186 }
187 return (Y()<rhs[1]);
188 }
189 return (X()<rhs[0]);
190 }
191 template <typename T>
192 inline bool Vec3<T>::operator>(const Vec3 & rhs) const
193 {
194 if (X() == rhs[0])
195 {
196 if (Y() == rhs[1])
197 {
198 return (Z()>rhs[2]);
199 }
200 return (Y()>rhs[1]);
201 }
202 return (X()>rhs[0]);
203 }
204 template <typename T>
205 inline Vec2<T> operator*(T lhs, const Vec2<T> & rhs)
206 {
207 return Vec2<T>(lhs * rhs.X(), lhs * rhs.Y());
208 }
209 template <typename T>
210 inline T & Vec2<T>::X()
211 {
212 return m_data[0];
213 }
214 template <typename T>
215 inline T & Vec2<T>::Y()
216 {
217 return m_data[1];
218 }
219 template <typename T>
220 inline const T & Vec2<T>::X() const
221 {
222 return m_data[0];
223 }
224 template <typename T>
225 inline const T & Vec2<T>::Y() const
226 {
227 return m_data[1];
228 }
229 template <typename T>
230 inline void Vec2<T>::Normalize()
231 {
232 T n = sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]);
233 if (n != 0.0) (*this) /= n;
234 }
235 template <typename T>
236 inline T Vec2<T>::GetNorm() const
237 {
238 return sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]);
239 }
240 template <typename T>
241 inline void Vec2<T>::operator= (const Vec2 & rhs)
242 {
243 this->m_data[0] = rhs.m_data[0];
244 this->m_data[1] = rhs.m_data[1];
245 }
246 template <typename T>
247 inline void Vec2<T>::operator+=(const Vec2 & rhs)
248 {
249 this->m_data[0] += rhs.m_data[0];
250 this->m_data[1] += rhs.m_data[1];
251 }
252 template <typename T>
253 inline void Vec2<T>::operator-=(const Vec2 & rhs)
254 {
255 this->m_data[0] -= rhs.m_data[0];
256 this->m_data[1] -= rhs.m_data[1];
257 }
258 template <typename T>
259 inline void Vec2<T>::operator-=(T a)
260 {
261 this->m_data[0] -= a;
262 this->m_data[1] -= a;
263 }
264 template <typename T>
265 inline void Vec2<T>::operator+=(T a)
266 {
267 this->m_data[0] += a;
268 this->m_data[1] += a;
269 }
270 template <typename T>
271 inline void Vec2<T>::operator/=(T a)
272 {
273 this->m_data[0] /= a;
274 this->m_data[1] /= a;
275 }
276 template <typename T>
277 inline void Vec2<T>::operator*=(T a)
278 {
279 this->m_data[0] *= a;
280 this->m_data[1] *= a;
281 }
282 template <typename T>
283 inline T Vec2<T>::operator^ (const Vec2<T> & rhs) const
284 {
285 return m_data[0] * rhs.m_data[1] - m_data[1] * rhs.m_data[0];
286 }
287 template <typename T>
288 inline T Vec2<T>::operator*(const Vec2<T> & rhs) const
289 {
290 return (m_data[0] * rhs.m_data[0] + m_data[1] * rhs.m_data[1]);
291 }
292 template <typename T>
293 inline Vec2<T> Vec2<T>::operator+(const Vec2<T> & rhs) const
294 {
295 return Vec2<T>(m_data[0] + rhs.m_data[0],m_data[1] + rhs.m_data[1]);
296 }
297 template <typename T>
298 inline Vec2<T> Vec2<T>::operator-(const Vec2<T> & rhs) const
299 {
300 return Vec2<T>(m_data[0] - rhs.m_data[0],m_data[1] - rhs.m_data[1]);
301 }
302 template <typename T>
303 inline Vec2<T> Vec2<T>::operator-() const
304 {
305 return Vec2<T>(-m_data[0],-m_data[1]) ;
306 }
307
308 template <typename T>
309 inline Vec2<T> Vec2<T>::operator*(T rhs) const
310 {
311 return Vec2<T>(rhs * this->m_data[0], rhs * this->m_data[1]);
312 }
313 template <typename T>
314 inline Vec2<T> Vec2<T>::operator/ (T rhs) const
315 {
316 return Vec2<T>(m_data[0] / rhs, m_data[1] / rhs);
317 }
318 template <typename T>
319 inline Vec2<T>::Vec2(T a)
320 {
321 m_data[0] = m_data[1] = a;
322 }
323 template <typename T>
324 inline Vec2<T>::Vec2(T x, T y)
325 {
326 m_data[0] = x;
327 m_data[1] = y;
328 }
329 template <typename T>
330 inline Vec2<T>::Vec2(const Vec2 & rhs)
331 {
332 m_data[0] = rhs.m_data[0];
333 m_data[1] = rhs.m_data[1];
334 }
335 template <typename T>
336 inline Vec2<T>::~Vec2(void){};
337
338 template <typename T>
339 inline Vec2<T>::Vec2() {}
340
341 /*
342 InsideTriangle decides if a point P is Inside of the triangle
343 defined by A, B, C.
344 */
345 template<typename T>
346 inline const bool InsideTriangle(const Vec2<T> & a, const Vec2<T> & b, const Vec2<T> & c, const Vec2<T> & p)
347 {
348 T ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
349 T cCROSSap, bCROSScp, aCROSSbp;
350 ax = c.X() - b.X(); ay = c.Y() - b.Y();
351 bx = a.X() - c.X(); by = a.Y() - c.Y();
352 cx = b.X() - a.X(); cy = b.Y() - a.Y();
353 apx= p.X() - a.X(); apy= p.Y() - a.Y();
354 bpx= p.X() - b.X(); bpy= p.Y() - b.Y();
355 cpx= p.X() - c.X(); cpy= p.Y() - c.Y();
356 aCROSSbp = ax*bpy - ay*bpx;
357 cCROSSap = cx*apy - cy*apx;
358 bCROSScp = bx*cpy - by*cpx;
359 return ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0));
360 }
361}
362#endif //VHACD_VECTOR_INL