-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.cpp
84 lines (72 loc) · 1.43 KB
/
Vector.cpp
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
#include "precompiled.h"
vec2 vec2_origin( 0.0f, 0.0f );
vec3 vec3_origin( 0.0f, 0.0f, 0.0f );
/*
====================
Lerp
Linearly interpolates one vector to another.
l has a range of 0.0f to 1.0f
====================
*/
void vec2::Lerp( const vec2& v, const vec2& w, const float scalar )
{
if( scalar <= 0.0f )
( *this ) = v;
else if( scalar >= 1.0f )
( *this ) = w;
else
( *this ) = v + scalar * ( w - v );
}
/*
====================
Lerp
Linearly interpolates one vector to another.
scalar has a range of 0.0f to 1.0f
====================
*/
void vec3::Lerp( const vec3& v, const vec3& w, const float scalar )
{
if( scalar <= 0.0f )
( *this ) = v;
else if( scalar >= 1.0f )
( *this ) = w;
else
( *this ) = v + scalar * ( w - v );
}
/*
====================
Slerp
Spherical linear interpolation from v to w.
Vectors are expected to be normalized.
l has a range of 0.0f to 1.0f
====================
*/
#define LERP_DELTA 1e-6
void vec3::SLerp( const vec3& v, const vec3& w, const float t )
{
float omega, cosom, sinom, scale0, scale1;
if( t <= 0.0f )
{
( *this ) = v;
return;
}
else if( t >= 1.0f )
{
( *this ) = w;
return;
}
cosom = Dot( v, w );
if(( 1.0f - cosom ) > LERP_DELTA )
{
omega = acos( cosom );
sinom = sin( omega );
scale0 = sin(( 1.0f - t ) * omega ) / sinom;
scale1 = sin( t * omega ) / sinom;
}
else
{
scale0 = 1.0f - t;
scale1 = t;
}
( *this ) = ( v * scale0 + w * scale1 );
}