-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
302 lines (240 loc) · 5.35 KB
/
vector.h
File metadata and controls
302 lines (240 loc) · 5.35 KB
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
/*
* vector.h
*
* Copyright 2012 Juan I Carrano <juan@superfreak.com.ar>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <stdlib.h>
#include <math.h>
#include "vector_common.h"
#ifndef _VKW
#define _VKW extern
extern float rand_f(struct limit lim);
#else
/* non inlining functions*/
float rand_f(struct limit lim)
{
return fabsf(((float)rand())/RAND_MAX) * (lim.max - lim.min) + lim.min;
}
#endif /* _VKW */
_VKW inline struct limit l_make(float lmin, float lmax)
{
struct limit l;
l.min = lmin;
l.max = lmax;
return l;
}
_VKW inline struct r_vector r_make(float x, float y)
{
struct r_vector v;
v.x = x;
v.y = y;
return v;
}
_VKW inline struct p_vector p_make(float value, float titha)
{
struct p_vector v;
v.value = value;
v.titha = titha;
return v;
}
_VKW inline float r_abs(struct r_vector v)
{
return hypotf(v.x, v.y);
}
_VKW inline struct r_vector r_scale(struct r_vector v, float a)
{
v.x *= a;
v.y *= a;
return v;
}
_VKW inline struct r_vector r_unit(struct r_vector v)
{
return r_scale(v, 1.0f / r_abs(v));
}
_VKW inline float pmod(float x, float y)
{
return x - y * floorf(x / y);
}
_VKW inline float r_abs2(struct r_vector v)
{
return v.x* v.x + v.y*v.y;
}
_VKW inline struct p_vector p_scale(struct p_vector v, float a)
{
v.value *= a;
return v;
}
_VKW inline struct r_vector r_sum(struct r_vector v1, struct r_vector v2)
{
v1.x += v2.x;
v1.y += v2.y;
return v1;
}
_VKW inline struct r_vector r_subs(struct r_vector v1, struct r_vector v2)
{
v1.x -= v2.x;
v1.y -= v2.y;
return v1;
}
_VKW inline struct limit artolimit(float a[2])
{
struct limit r;
r.min = a[0];
r.max = a[1];
return r;
}
_VKW inline struct r_vector p_to_r(struct p_vector v)
{
struct r_vector r;
float s, c;
#ifdef _GNU_SOURCE
sincosf(v.titha, &s, &c)
#else
s = sinf(v.titha);
c = cosf(v.titha);
#endif /* _GNU_SOURCE */
r.x = v.value * c;
r.y = v.value * s;
return r;
}
_VKW inline struct p_vector r_to_p(struct r_vector r)
{
struct p_vector v;
v.value = hypotf(r.x, r.y);
v.titha = atan2f(r.y, r.x);
return v;
}
_VKW inline struct limit r_lim(struct r_vector v)
{
struct limit r;
r.min = v.x;
r.max = v.y;
return r;
}
_VKW inline struct r_vector lim_r(struct limit v)
{
struct r_vector r;
r.x = v.min;
r.y = v.max;
return r;
}
_VKW inline float clip(float a, struct limit lim)
{
if (a > lim.max)
a = lim.max;
else if (a < lim.min)
a = lim.min;
return a;
}
_VKW inline struct r_vector r_clip(struct r_vector v, struct limit lim_x,
struct limit lim_y)
{
v.x = clip(v.x, lim_x);
v.y = clip(v.y, lim_y);
return v;
}
_VKW inline struct r_vector norm_clip(struct r_vector v, float lim)
{
float vn = r_abs(v);
return (vn < lim)? v: r_scale(v, lim/vn);
}
_VKW inline float absclip(float a, struct limit lim)
{
if (fabsf(a) > lim.max)
a = copysignf(lim.max, a);
else if (fabsf(a) < lim.min)
a = copysignf(lim.min, a);
return a;
}
_VKW inline struct r_vector r_absclip(struct r_vector v, struct limit lim_x,
struct limit lim_y)
{
v.x = absclip(v.x, lim_x);
v.y = absclip(v.y, lim_y);
return v;
}
_VKW inline struct r_vector norm_clip2(struct r_vector v, struct limit lim)
{
float vn = r_abs(v);
return (vn < lim.max)?
((vn > lim.min)? v : r_scale(v, lim.min/vn))
: r_scale(v, lim.max/vn);
}
_VKW inline float wrap(float a, struct limit lim)
{
int range = lim.max - lim.min;
a = pmod(a - lim.min, range);
a += lim.min;
return a;
}
_VKW inline struct r_vector r_toruswrap(struct r_vector v, struct limit lim_x,
struct limit lim_y)
{
v.x = wrap(v.x, lim_x);
v.y = wrap(v.y, lim_y);
return v;
}
_VKW inline float r_dist(struct r_vector v1, struct r_vector v2)
{
return hypotf(v1.x - v2.x, v1.y - v2.y);
}
_VKW inline float r_angle(struct r_vector v)
{
return atan2(v.y, v.x);
}
_VKW inline float r_dot(struct r_vector v1, struct r_vector v2)
{
return v1.x * v2.x + v1.y * v2.y;
}
_VKW inline float r_cos2(struct r_vector v1, struct r_vector v2)
{
return r_dot(v1, v2) / (r_abs(v1) * r_abs(v2) );
}
_VKW inline struct r_vector r_cbrt(struct r_vector v)
{
v.x = cbrtf(v.x);
v.y = cbrtf(v.y);
return v;
}
_VKW inline struct r_vector r_normal(struct r_vector v)
{
struct r_vector r;
r.x = -v.y;
r.y = v.x;
return r;
}
/*
_VKW inline struct r_vector r_project(struct r_vector source, struct r_vector dest)
{
}
*/
_VKW inline float cross_z(struct r_vector v1, struct r_vector v2)
{
return v1.x*v2.y - v1.y*v2.x;
}
_VKW inline float cross_z_sign(struct r_vector v1, struct r_vector v2)
{
return copysignf(cross_z(v1,v2), r_dot(v1, v2));
}
_VKW inline float cross_z_sign_a(struct r_vector v1, struct r_vector v2, float a)
{
return copysignf(cross_z(v1,v2)+a, r_dot(v1, v2));
}
#endif /* _VECTOR_H_ */