summaryrefslogtreecommitdiff
path: root/Runtime/Math/Vector3.cpp
blob: 3195ef2b65b50d9b2ba4d2dbe6aa5a14f29d1d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "UnityPrefix.h"
#include "Vector3.h"
#include "Matrix3x3.h"
#include <limits>

#define FPFIXES 1

using namespace std;

const float		Vector3f::epsilon = 0.00001F;
const float		Vector3f::infinity = numeric_limits<float>::infinity ();
const Vector3f	Vector3f::infinityVec = Vector3f (numeric_limits<float>::infinity (), numeric_limits<float>::infinity (), numeric_limits<float>::infinity ());

const Vector3f	Vector3f::zero  = Vector3f (0, 0, 0);
const Vector3f	Vector3f::one  = Vector3f (1.0F, 1.0F, 1.0F);
const Vector3f	Vector3f::xAxis = Vector3f (1, 0, 0);
const Vector3f	Vector3f::yAxis = Vector3f (0, 1, 0);
const Vector3f	Vector3f::zAxis = Vector3f (0, 0, 1);


void OrthoNormalizeFast (Vector3f* inU, Vector3f* inV, Vector3f* inW)
{
	// compute u0
	*inU = Normalize (*inU);

	// compute u1
	float dot0 = Dot (*inU, *inV); 
	*inV -= dot0 * *inU;
	*inV = Normalize (*inV);

	// compute u2
	float dot1 = Dot (*inV, *inW);
	dot0 = Dot (*inU, *inW);
	*inW -= dot0 * *inU + dot1 * *inV;
	*inW = Normalize (*inW);
}

void OrthoNormalize (Vector3f* inU, Vector3f* inV)
{
	// compute u0
	float mag = Magnitude (*inU);
	if (mag > Vector3f::epsilon)
		*inU /= mag;
	else
		*inU = Vector3f (1.0F, 0.0F, 0.0F);

	// compute u1
	float dot0 = Dot (*inU, *inV);
	*inV -= dot0 * *inU;
	mag = Magnitude (*inV);
	if (mag < Vector3f::epsilon)
		*inV = OrthoNormalVectorFast (*inU);
	else
		*inV /= mag;
}

void OrthoNormalize (Vector3f* inU, Vector3f* inV, Vector3f* inW)
{
	// compute u0
	float mag = Magnitude (*inU);
	if (mag > Vector3f::epsilon)
		*inU /= mag;
	else
		*inU = Vector3f (1.0F, 0.0F, 0.0F);

	// compute u1
	float dot0 = Dot (*inU, *inV);
	*inV -= dot0 * *inU;
	mag = Magnitude (*inV);
	if (mag > Vector3f::epsilon)
		*inV /= mag;
	else
		*inV = OrthoNormalVectorFast (*inU);

	// compute u2
	float dot1 = Dot (*inV, *inW);
	dot0 = Dot (*inU, *inW);
	*inW -= dot0 * *inU + dot1 * *inV;
	mag = Magnitude (*inW);
	if (mag > Vector3f::epsilon)
		*inW /= mag;
	else
		*inW = Cross (*inU, *inV);
}

#define k1OverSqrt2 float(0.7071067811865475244008443621048490)

Vector3f OrthoNormalVectorFast (const Vector3f& n)
{
	Vector3f res;
	if (Abs (n.z) > k1OverSqrt2)
	{
		// choose p in y-z plane
		float a = n.y*n.y + n.z*n.z;
		float k = 1.0F / sqrt (a);
		res.x = 0;
		res.y = -n.z*k;
		res.z = n.y*k;
	}
	else
	{
		// choose p in x-y plane
		float a = n.x*n.x + n.y*n.y;
		float k = 1.0F / sqrt (a);
		res.x = -n.y*k;
		res.y = n.x*k;
		res.z = 0;
	}
	return res;
}

/* from chris hecker (Generates Orthonormal basis)
void 
DextralBases(real32 const *XAxis, real32 *YAxis, real32 *ZAxis)
{
    real32 CrossVector[3] = {1.0f, 1.0f, 1.0f};

    real32 MaximumElement = 0.0f;

    int MaximumElementIndex = 0;
    {for(int ElementIndex = 0;
         ElementIndex < 3;
         ++ElementIndex)
    {
        real32 ElementValue = AbsoluteValue(XAxis[ElementIndex]);
        if(ElementValue > MaximumElement)
        {
            MaximumElement = ElementValue;
            MaximumElementIndex = ElementIndex;
        }
    }}

    CrossVector[MaximumElementIndex] = 0.0f;

    VectorCrossProduct3(YAxis, CrossVector, XAxis);
    Normalize3(YAxis);

    VectorCrossProduct3(ZAxis, XAxis, YAxis);
    Normalize3(ZAxis);
}

*/

/// Returns a Vector3 that moves lhs towards rhs by a maximum of clampedDistance
Vector3f MoveTowards (const Vector3f& lhs, const Vector3f& rhs, float clampedDistance)
{
	Vector3f delta = rhs - lhs;
	float sqrDelta = SqrMagnitude (delta);
	float sqrClampedDistance = clampedDistance * clampedDistance;
	if (sqrDelta > sqrClampedDistance)
	{
		float deltaMag = sqrt (sqrDelta);
		if (deltaMag > Vector3f::epsilon)
			return lhs + delta / deltaMag * clampedDistance;
		else	
			return lhs;
	}	
	else
		return rhs;
}

static inline float ClampedMove (float lhs, float rhs, float clampedDelta)
{
	float delta = rhs - lhs;
	if (delta > 0.0F)
		return lhs + min (delta, clampedDelta);
	else
		return lhs - min (-delta, clampedDelta);
}

Vector3f RotateTowards (const Vector3f& lhs, const Vector3f& rhs, float angleMove, float magnitudeMove)
{
	float lhsMag = Magnitude (lhs);
	float rhsMag = Magnitude (rhs);
	
	// both vectors are non-zero
	if (lhsMag > Vector3f::epsilon && rhsMag > Vector3f::epsilon)
	{
		Vector3f lhsNorm = lhs / lhsMag;
		Vector3f rhsNorm = rhs / rhsMag;
		
		float dot = Dot (lhsNorm, rhsNorm);
		// direction is almost the same
		if (dot > 1.0F - Vector3f::epsilon)
		{
			return MoveTowards (lhs, rhs, magnitudeMove);
		}
		// directions are almost opposite
		else if (dot < -1.0F + Vector3f::epsilon)
		{
			Vector3f axis = OrthoNormalVectorFast (lhsNorm);
			Matrix3x3f m;
			m.SetAxisAngle (axis, angleMove);
			Vector3f rotated = m.MultiplyPoint3 (lhsNorm);
			rotated *= ClampedMove (lhsMag, rhsMag, magnitudeMove);
			return rotated;
		}
		// normal case
		else
		{
			float angle = acos (dot);
			Vector3f axis = Normalize (Cross (lhsNorm, rhsNorm));
			Matrix3x3f m;
			m.SetAxisAngle (axis, min (angleMove, angle));
			Vector3f rotated = m.MultiplyPoint3 (lhsNorm);
			rotated *= ClampedMove (lhsMag, rhsMag, magnitudeMove);
			return rotated;
		}
	}
	// at least one of the vectors is almost zero
	else
	{
		return MoveTowards (lhs, rhs, magnitudeMove);
	}
}


Vector3f Slerp (const Vector3f& lhs, const Vector3f& rhs, float t) {
	
	float lhsMag = Magnitude (lhs);
	float rhsMag = Magnitude (rhs);
	
	if (lhsMag < Vector3f::epsilon || rhsMag < Vector3f::epsilon)
		return Lerp (lhs, rhs, t);

	float lerpedMagnitude = Lerp (lhsMag, rhsMag, t);
	
	float dot = Dot (lhs, rhs) / (lhsMag * rhsMag);
	// direction is almost the same
	if (dot > 1.0F - Vector3f::epsilon)
	{
		return Lerp (lhs, rhs, t);
	}
	// directions are almost opposite
	else if (dot < -1.0F + Vector3f::epsilon)
	{
		Vector3f lhsNorm = lhs / lhsMag;
		Vector3f axis = OrthoNormalVectorFast (lhsNorm);
		Matrix3x3f m;
		m.SetAxisAngle (axis, kPI * t);
		Vector3f slerped = m.MultiplyPoint3 (lhsNorm);
		slerped *= lerpedMagnitude;
		return slerped;
	}
	// normal case
	else
	{
		Vector3f axis = Cross (lhs, rhs);
		Vector3f lhsNorm = lhs / lhsMag;
		axis = Normalize (axis);
		float angle = acos (dot) * t;
		
		Matrix3x3f m;
		m.SetAxisAngle (axis, angle);
		Vector3f slerped = m.MultiplyPoint3 (lhsNorm);
		slerped *= lerpedMagnitude;
		return slerped;
	}
}

inline static Vector3f NormalizeRobust (const Vector3f& a, float &l, float &div)
{
	float a0,a1,a2,aa0,aa1,aa2;
	a0 = a[0];
	a1 = a[1];
	a2 = a[2];

#if FPFIXES
	if ( CompareApproximately( a0, 0.0F, 0.00001F ) )
		a0 = aa0 = 0;
	else
#endif	
	{
		aa0 = Abs (a0);
	}

#if FPFIXES	
	if ( CompareApproximately( a1, 0.0F, 0.00001F ) )
		a1 = aa1 =0;
	else
#endif	
	{
		aa1 = Abs (a1);
	}

#if FPFIXES	
	if ( CompareApproximately( a2, 0.0F, 0.00001F ) )
		a2 = aa2 = 0;
	else
#endif	
	{
		aa2 = Abs (a2);
	}
	
	if (aa1 > aa0)
	{
		if (aa2 > aa1)
		{
			a0 /= aa2;
			a1 /= aa2;
			l = InvSqrt (a0*a0 + a1*a1 + 1.0F);
			div = aa2;
			return Vector3f (a0*l, a1*l, CopySignf (l,a2));
		}
		else
		{	
			// aa1 is largest
			a0 /= aa1;
			a2 /= aa1;
			l = InvSqrt (a0*a0 + a2*a2 + 1.0F);
			div = aa1;
			return Vector3f (a0*l, CopySignf (l, a1), a2*l);
		}
	}
	else
	{
		if (aa2 > aa0)
		{
			// aa2 is largest
			a0 /= aa2;
			a1 /= aa2;
			l = InvSqrt (a0*a0 + a1*a1 + 1.0F);
			div = aa2;
			return Vector3f (a0*l, a1*l, CopySignf (l,a2));
		}
		else
		{	
			// aa0 is largest
			if (aa0 <= 0)
			{
				l = 0;
				div = 1;
				return Vector3f (0.0F, 1.0F, 0.0F);
			}
				
			a1 /= aa0;
			a2 /= aa0;
			l = InvSqrt (a1*a1 + a2*a2 + 1.0F);
			div = aa0;
			return Vector3f (CopySignf (l,a0), a1*l, a2*l);
		}
	}
}

Vector3f NormalizeRobust (const Vector3f& a)
{
	float l, div;
	return NormalizeRobust(a, l, div);
}

Vector3f NormalizeRobust (const Vector3f& a, float &invOriginalLength)
{
	float l, div;
	const Vector3f &n = NormalizeRobust(a, l, div);
	invOriginalLength = l/div;
	// guard for NaNs
	Assert (n == n);
	Assert (invOriginalLength == invOriginalLength);
	Assert (IsNormalized(n));
	return n;
}