2
// Copyright (c) 2009 Mikko Mononen memon@inside.org
4
// This software is provided 'as-is', without any express or implied
5
// warranty. In no event will the authors be held liable for any damages
6
// arising from the use of this software.
7
// Permission is granted to anyone to use this software for any purpose,
8
// including commercial applications, and to alter it and redistribute it
9
// freely, subject to the following restrictions:
10
// 1. The origin of this software must not be misrepresented; you must not
11
// claim that you wrote the original software. If you use this software
12
// in a product, an acknowledgment in the product documentation would be
13
// appreciated but is not required.
14
// 2. Altered source versions must be plainly marked as such, and must not be
15
// misrepresented as being the original software.
16
// 3. This notice may not be removed or altered from any source distribution.
19
#ifndef DETOURCOMMON_H
20
#define DETOURCOMMON_H
22
//////////////////////////////////////////////////////////////////////////////////////////
24
template<class T> inline void swap(T& a, T& b) { T t = a; a = b; b = t; }
25
template<class T> inline T min(T a, T b) { return a < b ? a : b; }
26
template<class T> inline T max(T a, T b) { return a > b ? a : b; }
27
template<class T> inline T abs(T a) { return a < 0 ? -a : a; }
28
template<class T> inline T sqr(T a) { return a*a; }
29
template<class T> inline T clamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
31
inline void vcross(float* dest, const float* v1, const float* v2)
33
dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
34
dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
35
dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
38
inline float vdot(const float* v1, const float* v2)
40
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
43
inline void vmad(float* dest, const float* v1, const float* v2, const float s)
45
dest[0] = v1[0]+v2[0]*s;
46
dest[1] = v1[1]+v2[1]*s;
47
dest[2] = v1[2]+v2[2]*s;
50
inline void vadd(float* dest, const float* v1, const float* v2)
52
dest[0] = v1[0]+v2[0];
53
dest[1] = v1[1]+v2[1];
54
dest[2] = v1[2]+v2[2];
57
inline void vsub(float* dest, const float* v1, const float* v2)
59
dest[0] = v1[0]-v2[0];
60
dest[1] = v1[1]-v2[1];
61
dest[2] = v1[2]-v2[2];
64
inline void vmin(float* mn, const float* v)
66
mn[0] = min(mn[0], v[0]);
67
mn[1] = min(mn[1], v[1]);
68
mn[2] = min(mn[2], v[2]);
71
inline void vmax(float* mx, const float* v)
73
mx[0] = max(mx[0], v[0]);
74
mx[1] = max(mx[1], v[1]);
75
mx[2] = max(mx[2], v[2]);
78
inline void vcopy(float* dest, const float* a)
85
inline float vdist(const float* v1, const float* v2)
87
float dx = v2[0] - v1[0];
88
float dy = v2[1] - v1[1];
89
float dz = v2[2] - v1[2];
90
return sqrtf(dx*dx + dy*dy + dz*dz);
93
inline float vdistSqr(const float* v1, const float* v2)
95
float dx = v2[0] - v1[0];
96
float dy = v2[1] - v1[1];
97
float dz = v2[2] - v1[2];
98
return dx*dx + dy*dy + dz*dz;
101
inline void vnormalize(float* v)
103
float d = 1.0f / sqrtf(sqr(v[0]) + sqr(v[1]) + sqr(v[2]));
109
inline bool vequal(const float* p0, const float* p1)
111
static const float thr = sqr(1.0f/16384.0f);
112
const float d = vdistSqr(p0, p1);
116
inline int nextPow2(int v)
128
inline float vdot2D(const float* u, const float* v)
130
return u[0]*v[0] + u[2]*v[2];
133
inline float vperp2D(const float* u, const float* v)
135
return u[2]*v[0] - u[0]*v[2];
138
inline float triArea2D(const float* a, const float* b, const float* c)
140
return ((b[0]*a[2] - a[0]*b[2]) + (c[0]*b[2] - b[0]*c[2]) + (a[0]*c[2] - c[0]*a[2])) * 0.5f;
143
inline bool checkOverlapBox(const unsigned short amin[3], const unsigned short amax[3],
144
const unsigned short bmin[3], const unsigned short bmax[3])
147
overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
148
overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
149
overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
153
void closestPtPointTriangle(float* closest, const float* p,
154
const float* a, const float* b, const float* c);
156
bool closestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
158
bool intersectSegmentPoly2D(const float* p0, const float* p1,
159
const float* verts, int nverts,
160
float& tmin, float& tmax,
161
int& segMin, int& segMax);
163
float distancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
165
void calcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
167
#endif // DETOURCOMMON_H
b'\\ No newline at end of file'