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
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
#ifndef BASE_VMATH_H
#define BASE_VMATH_H
// ------------------------------------
template<typename T>
class vector2_base
{
public:
union { T x,u; };
union { T y,v; };
vector2_base() {}
vector2_base(float nx, float ny)
{
x = nx;
y = ny;
}
vector2_base operator -() const { return vector2_base(-x, -y); }
vector2_base operator -(const vector2_base &v) const { return vector2_base(x-v.x, y-v.y); }
vector2_base operator +(const vector2_base &v) const { return vector2_base(x+v.x, y+v.y); }
vector2_base operator *(const T v) const { return vector2_base(x*v, y*v); }
const vector2_base &operator =(const vector2_base &v) { x = v.x; y = v.y; return *this; }
const vector2_base &operator +=(const vector2_base &v) { x += v.x; y += v.y; return *this; }
const vector2_base &operator -=(const vector2_base &v) { x -= v.x; y -= v.y; return *this; }
const vector2_base &operator *=(const T v) { x *= v; y *= v; return *this; }
bool operator ==(const vector2_base &v) const { return x == v.x && y == v.y; } //TODO: do this with an eps instead
operator const T* () { return &x; }
};
template<typename T>
inline T length(const vector2_base<T> &a)
{
return sqrtf(a.x*a.x + a.y*a.y);
}
template<typename T>
inline T distance(const vector2_base<T> a, const vector2_base<T> &b)
{
return length(a-b);
}
template<typename T>
inline T dot(const vector2_base<T> a, const vector2_base<T> &b)
{
return a.x*b.x + a.y*b.y;
}
template<typename T>
inline vector2_base<T> normalize(const vector2_base<T> &v)
{
T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y));
return vector2_base<T>(v.x*l, v.y*l);
}
typedef vector2_base<float> vec2;
typedef vector2_base<bool> bvec2;
typedef vector2_base<int> ivec2;
template<typename T>
inline vector2_base<T> closest_point_on_line(vector2_base<T> line_point0, vector2_base<T> line_point1, vector2_base<T> target_point)
{
vector2_base<T> c = target_point - line_point0;
vector2_base<T> v = (line_point1 - line_point0);
v = normalize(v);
T d = length(line_point0-line_point1);
T t = dot(v, c)/d;
return mix(line_point0, line_point1, clamp(t, (T)0, (T)1));
/*
if (t < 0) t = 0;
if (t > 1.0f) return 1.0f;
return t;*/
}
// ------------------------------------
template<typename T>
class vector3_base
{
public:
union { T x,r,h; };
union { T y,g,s; };
union { T z,b,v,l; };
vector3_base() {}
vector3_base(float nx, float ny, float nz)
{
x = nx;
y = ny;
z = nz;
}
const vector3_base &operator =(const vector3_base &v) { x = v.x; y = v.y; z = v.z; return *this; }
vector3_base operator -(const vector3_base &v) const { return vector3_base(x-v.x, y-v.y, z-v.z); }
vector3_base operator -() const { return vector3_base(-x, -y, -z); }
vector3_base operator +(const vector3_base &v) const { return vector3_base(x+v.x, y+v.y, z+v.z); }
vector3_base operator *(const T v) const { return vector3_base(x*v, y*v, z*v); }
vector3_base operator *(const vector3_base &v) const { return vector3_base(x*v.x, y*v.y, z*v.z); }
vector3_base operator /(const T v) const { return vector3_base(x/v, y/v, z/v); }
const vector3_base &operator +=(const vector3_base &v) { x += v.x; y += v.y; z += v.z; return *this; }
const vector3_base &operator -=(const vector3_base &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
const vector3_base &operator *=(const T v) { x *= v; y *= v; z *= v; return *this; }
bool operator ==(const vector3_base &v) const { return x == v.x && y == v.y && z == v.z; } //TODO: do this with an eps instead
operator const T* () { return &x; }
};
template<typename T>
inline T length(const vector3_base<T> &a)
{
return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z);
}
template<typename T>
inline T distance(const vector3_base<T> &a, const vector3_base<T> &b)
{
return length(a-b);
}
template<typename T>
inline T dot(const vector3_base<T> &a, const vector3_base<T> &b)
{
return a.x*b.x + a.y*b.y + a.z*b.z;
}
template<typename T>
inline vector3_base<T> normalize(const vector3_base<T> &v)
{
T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y + v.z*v.z));
return vector3_base<T>(v.x*l, v.y*l, v.z*l);
}
template<typename T>
inline vector3_base<T> cross(const vector3_base<T> &a, const vector3_base<T> &b)
{
return vector3_base<T>(
a.y*b.z - a.z*b.y,
a.z*b.x - a.x*b.z,
a.x*b.y - a.y*b.x);
}
typedef vector3_base<float> vec3;
typedef vector3_base<bool> bvec3;
typedef vector3_base<int> ivec3;
// ------------------------------------
template<typename T>
class vector4_base
{
public:
union { T x,r; };
union { T y,g; };
union { T z,b; };
union { T w,a; };
vector4_base() {}
vector4_base(float nx, float ny, float nz, float nw)
{
x = nx;
y = ny;
z = nz;
w = nw;
}
vector4_base operator +(const vector4_base &v) const { return vector4_base(x+v.x, y+v.y, z+v.z, w+v.w); }
vector4_base operator -(const vector4_base &v) const { return vector4_base(x-v.x, y-v.y, z-v.z, w-v.w); }
vector4_base operator -() const { return vector4_base(-x, -y, -z, -w); }
vector4_base operator *(const vector4_base &v) const { return vector4_base(x*v.x, y*v.y, z*v.z, w*v.w); }
vector4_base operator *(const T v) const { return vector4_base(x*v, y*v, z*v, w*v); }
const vector4_base &operator =(const vector4_base &v) { x = v.x; y = v.y; z = v.z; w = v.w; return *this; }
const vector4_base &operator +=(const vector4_base &v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
const vector4_base &operator -=(const vector4_base &v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
const vector4_base &operator *=(const T v) { x *= v; y *= v; z *= v; w *= v; return *this; }
bool operator ==(const vector4_base &v) const { return x == v.x && y == v.y && z == v.z && w == v.w; } //TODO: do this with an eps instead
operator const T* () { return &x; }
};
typedef vector4_base<float> vec4;
typedef vector4_base<bool> bvec4;
typedef vector4_base<int> ivec4;
#endif
|