-
Notifications
You must be signed in to change notification settings - Fork 105
/
vec.h
207 lines (165 loc) · 3.85 KB
/
vec.h
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
#pragma once
#include "common.h"
struct vec2
{
vec2() : x(0.0f), y(0.0f) {}
vec2(float _x, float _y) : x(_x), y(_y) {}
float x, y;
};
static inline vec2 operator+(float s, vec2 v)
{
return vec2(v.x + s, v.y + s);
}
static inline vec2 operator+(vec2 v, float s)
{
return vec2(v.x + s, v.y + s);
}
static inline vec2 operator+(vec2 v, vec2 w)
{
return vec2(v.x + w.x, v.y + w.y);
}
static inline vec2 operator-(float s, vec2 v)
{
return vec2(s - v.x, s - v.y);
}
static inline vec2 operator-(vec2 v, float s)
{
return vec2(v.x - s, v.y - s);
}
static inline vec2 operator-(vec2 v, vec2 w)
{
return vec2(v.x - w.x, v.y - w.y);
}
static inline vec2 operator*(float s, vec2 v)
{
return vec2(v.x * s, v.y * s);
}
static inline vec2 operator*(vec2 v, float s)
{
return vec2(v.x * s, v.y * s);
}
static inline vec2 operator*(vec2 v, vec2 w)
{
return vec2(v.x * w.x, v.y * w.y);
}
static inline vec2 operator/(vec2 v, float s)
{
return vec2(v.x / s, v.y / s);
}
static inline vec2 operator/(float s, vec2 v)
{
return vec2(s / v.x, s / v.y);
}
static inline vec2 operator/(vec2 v, vec2 w)
{
return vec2(v.x / w.x, v.y / w.y);
}
static inline vec2 operator-(vec2 v)
{
return vec2(-v.x, -v.y);
}
static inline float dot(vec2 v, vec2 w)
{
return v.x*w.x + v.y*w.y;
}
static inline float length(vec2 v)
{
return sqrtf(v.x*v.x + v.y*v.y);
}
static inline vec2 normalize(vec2 v, float eps=1e-8f)
{
return v / (length(v) + eps);
}
static inline vec2 lerp(vec2 v, vec2 w, float alpha)
{
return v * (1.0f - alpha) + w * alpha;
}
//--------------------------------------
struct vec3
{
vec3() : x(0.0f), y(0.0f), z(0.0f) {}
vec3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
float x, y, z;
};
static inline vec3 operator+(float s, vec3 v)
{
return vec3(v.x + s, v.y + s, v.z + s);
}
static inline vec3 operator+(vec3 v, float s)
{
return vec3(v.x + s, v.y + s, v.z + s);
}
static inline vec3 operator+(vec3 v, vec3 w)
{
return vec3(v.x + w.x, v.y + w.y, v.z + w.z);
}
static inline vec3 operator-(float s, vec3 v)
{
return vec3(s - v.x, s - v.y, s - v.z);
}
static inline vec3 operator-(vec3 v, float s)
{
return vec3(v.x - s, v.y - s, v.z - s);
}
static inline vec3 operator-(vec3 v, vec3 w)
{
return vec3(v.x - w.x, v.y - w.y, v.z - w.z);
}
static inline vec3 operator*(float s, vec3 v)
{
return vec3(v.x * s, v.y * s, v.z * s);
}
static inline vec3 operator*(vec3 v, float s)
{
return vec3(v.x * s, v.y * s, v.z * s);
}
static inline vec3 operator*(vec3 v, vec3 w)
{
return vec3(v.x * w.x, v.y * w.y, v.z * w.z);
}
static inline vec3 operator/(vec3 v, float s)
{
return vec3(v.x / s, v.y / s, v.z / s);
}
static inline vec3 operator/(float s, vec3 v)
{
return vec3(s / v.x, s / v.y, s / v.z);
}
static inline vec3 operator/(vec3 v, vec3 w)
{
return vec3(v.x / w.x, v.y / w.y, v.z / w.z);
}
static inline vec3 operator-(vec3 v)
{
return vec3(-v.x, -v.y, -v.z);
}
static inline float dot(vec3 v, vec3 w)
{
return v.x*w.x + v.y*w.y + v.z*w.z;
}
static inline vec3 cross(vec3 v, vec3 w)
{
return vec3(
v.y*w.z - v.z*w.y,
v.z*w.x - v.x*w.z,
v.x*w.y - v.y*w.x);
}
static inline float length(vec3 v)
{
return sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
}
static inline vec3 normalize(vec3 v, float eps=1e-8f)
{
return v / (length(v) + eps);
}
static inline vec3 lerp(vec3 v, vec3 w, float alpha)
{
return v * (1.0f - alpha) + w * alpha;
}
static inline vec3 clamp(vec3 v, vec3 min, vec3 max)
{
return vec3(
clampf(v.x, min.x, max.x),
clampf(v.y, min.y, max.y),
clampf(v.z, min.z, max.z));
}