summaryrefslogtreecommitdiff
path: root/src/modules/voronoi/v2f.h
blob: 8f51ee0f5a4e6491446289f654f70d17605db251 (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
/*
 *  Copyright (C) 2018-2019 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  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, see <http://www.gnu.org/licenses/>.
 */

/*
 * 2D vector operations header
 *
 * Variants prefixed with _ return a result vector struct by value.
 *
 * Variants missing the _ prefix for vector result operations return a pointer
 * and must be either supplied the result memory as the first "res" argument
 * which is then returned after being populated with the result's value, or
 * NULL to allocate space for the result and that pointer is returned after being
 * populated with its value.  When supplying NULL result pointers the functions
 * must allocate memory and thus may return NULL on OOM, so callers should
 * check for NULL returns when supplying NULL for "res".
 *
 * Example:
 *	v2f_t	foo, *foop;
 *
 *	foo = _v2f_mult(&(v2f_t){1.f,1.f}, &(v2f_t){2.f,2.f});
 *
 *	is equivalent to:
 *
 *	v2f_mult(&foo, &(v2f_t){1.f,1.f}, &(v2f_t){2.f,2.f});
 *
 *	or dynamically allocated:
 *
 *	foop = v2f_mult(NULL, &(v2f_t){1.f,1.f}, &(v2f_t){2.f,2.f});
 *	free(foop);
 *
 *	is equivalent to:
 *
 *	foop = malloc(sizeof(v2f_t));
 *	v2f_mult(foop, &(v2f_t){1.f,1.f}, &(v2f_t){2.f,2.f});
 *	free(foop);
 */

#ifndef _V2F_H
#define _V2F_H


#include <math.h>
#include <stdlib.h>


typedef struct v2f_t {
	float	x, y;
} v2f_t;


static inline v2f_t * _v2f_allocated(v2f_t **ptr)
{
	if (!*ptr)
		*ptr = malloc(sizeof(v2f_t));

	return *ptr;
}


static inline v2f_t _v2f_add(const v2f_t *a, const v2f_t *b)
{
	return (v2f_t){a->x + b->x, a->y + b->y};
}


static inline v2f_t * v2f_add(v2f_t *res, const v2f_t *a, const v2f_t *b)
{
	if (_v2f_allocated(&res))
		*res = _v2f_add(a, b);

	return res;
}


static inline v2f_t _v2f_sub(const v2f_t *a, const v2f_t *b)
{
	return (v2f_t){a->x - b->x, a->y - b->y};
}


static inline v2f_t * v2f_sub(v2f_t *res, const v2f_t *a, const v2f_t *b)
{
	if (_v2f_allocated(&res))
		*res = _v2f_sub(a, b);

	return res;
}


static inline v2f_t _v2f_mult(const v2f_t *a, const v2f_t *b)
{
	return (v2f_t){a->x * b->x, a->y * b->y};
}


static inline v2f_t * v2f_mult(v2f_t *res, const v2f_t *a, const v2f_t *b)
{
	if (_v2f_allocated(&res))
		*res = _v2f_mult(a, b);

	return res;
}


static inline v2f_t _v2f_mult_scalar(const v2f_t *v, float scalar)
{
	return (v2f_t){ v->x * scalar, v->y * scalar };
}


static inline v2f_t * v2f_mult_scalar(v2f_t *res, const v2f_t *v, float scalar)
{
	if (_v2f_allocated(&res))
		*res = _v2f_mult_scalar(v, scalar);

	return res;
}


static inline v2f_t _v2f_div_scalar(const v2f_t *v, float scalar)
{
	return _v2f_mult_scalar(v, 1.f / scalar);
}


static inline v2f_t * v2f_div_scalar(v2f_t *res, const v2f_t *v, float scalar)
{
	if (_v2f_allocated(&res))
		*res = _v2f_div_scalar(v, scalar);

	return res;
}


static inline float v2f_dot(const v2f_t *a, const v2f_t *b)
{
	return a->x * b->x + a->y * b->y;
}


static inline float v2f_length(const v2f_t *v)
{
	return sqrtf(v2f_dot(v, v));
}


static inline float v2f_distance(const v2f_t *a, const v2f_t *b)
{
	return v2f_length(v2f_sub(&(v2f_t){}, a, b));
}


static inline float v2f_distance_sq(const v2f_t *a, const v2f_t *b)
{
	v2f_t	d = _v2f_sub(a, b);

	return v2f_dot(&d, &d);
}


static inline v2f_t _v2f_normalize(const v2f_t *v)
{
	return _v2f_mult_scalar(v, 1.0f / v2f_length(v));
}


static inline v2f_t * v2f_normalize(v2f_t *res, const v2f_t *v)
{
	if (_v2f_allocated(&res))
		*res = _v2f_normalize(v);

	return res;
}


static inline v2f_t _v2f_lerp(const v2f_t *a, const v2f_t *b, float t)
{
	v2f_t	lerp_a, lerp_b;

	lerp_a = _v2f_mult_scalar(a, 1.0f - t);
	lerp_b = _v2f_mult_scalar(b, t);

	return _v2f_add(&lerp_a, &lerp_b);
}


static inline v2f_t * v2f_lerp(v2f_t *res, const v2f_t *a, const v2f_t *b, float t)
{
	if (_v2f_allocated(&res))
		*res = _v2f_lerp(a, b, t);

	return res;
}


static inline v2f_t _v2f_nlerp(const v2f_t *a, const v2f_t *b, float t)
{
	v2f_t	lerp = _v2f_lerp(a, b, t);

	return _v2f_normalize(&lerp);
}


static inline v2f_t * v2f_nlerp(v2f_t *res, const v2f_t *a, const v2f_t *b, float t)
{
	if (_v2f_allocated(&res))
		*res = _v2f_nlerp(a, b, t);

	return res;
}


/*
 *       1 ab-------bb
 *       | |         |
 *       | |         |
 *       | |         |
 *       0 aa-------ba
 *  t_x:   0---------1
 *       ^
 *       t_y
 */
static inline v2f_t _v2f_bilerp(const v2f_t *aa, const v2f_t *ab, const v2f_t *ba, const v2f_t *bb, float t_x, float t_y)
{
	v2f_t	xa = _v2f_lerp(aa, ba, t_x);
	v2f_t	xb = _v2f_lerp(ab, bb, t_x);

	return _v2f_lerp(&xa, &xb, t_y);
}


static inline v2f_t * v2f_bilerp(v2f_t *res, const v2f_t *aa, const v2f_t *ab, const v2f_t *ba, const v2f_t *bb, float t_x, float t_y)
{
	if (_v2f_allocated(&res))
		*res = _v2f_bilerp(aa, ab, ba, bb, t_x, t_y);

	return res;
}


/*
 *     abb-------bbb
 *     /|        /|
 *   aba-------bba|
 *    | |       | |
 *    |aab------|bab
 *    |/        |/
 *   aaa-------baa
 */
static inline v2f_t _v2f_trilerp(const v2f_t *aaa, const v2f_t *aba, const v2f_t *aab, const v2f_t *abb, const v2f_t *baa, const v2f_t *bba, const v2f_t *bab, const v2f_t *bbb, float t_x, float t_y, float t_z)
{
	v2f_t	xya = _v2f_bilerp(aaa, aba, baa, bba, t_x, t_y);
	v2f_t	xyb = _v2f_bilerp(aab, abb, bab, bbb, t_x, t_y);

	return _v2f_lerp(&xya, &xyb, t_z);
}


static inline v2f_t * v2f_trilerp(v2f_t *res, const v2f_t *aaa, const v2f_t *aba, const v2f_t *aab, const v2f_t *abb, const v2f_t *baa, const v2f_t *bba, const v2f_t *bab, const v2f_t *bbb, float t_x, float t_y, float t_z)
{
	if (_v2f_allocated(&res))
		*res = _v2f_trilerp(aaa, aba, aab, abb, baa, bba, bab, bbb, t_x, t_y, t_z);

	return res;
}


static inline v2f_t _v2f_rand(const v2f_t *min, const v2f_t *max)
{
	return (v2f_t){
		.x = min->x + (float)rand() * (1.f/RAND_MAX) * (max->x - min->x),
		.y = min->y + (float)rand() * (1.f/RAND_MAX) * (max->y - min->y),
	};
}


static inline v2f_t * v2f_rand(v2f_t *res, const v2f_t *min, const v2f_t *max)
{
	if (_v2f_allocated(&res))
		*res = _v2f_rand(min, max);

	return res;
}


static inline v2f_t _v2f_ceil(const v2f_t *v)
{
	return (v2f_t){
		.x = ceilf(v->x),
		.y = ceilf(v->y),
	};
}


static inline v2f_t * v2f_ceil(v2f_t *res, const v2f_t *v)
{
	if (_v2f_allocated(&res))
		*res = _v2f_ceil(v);

	return res;
}


static inline v2f_t _v2f_floor(const v2f_t *v)
{
	return (v2f_t){
		.x = floorf(v->x),
		.y = floorf(v->y),
	};
}


static inline v2f_t * v2f_floor(v2f_t *res, const v2f_t *v)
{
	if (_v2f_allocated(&res))
		*res = _v2f_floor(v);

	return res;
}


static inline v2f_t _v2f_clamp(const v2f_t *v, const v2f_t *min, const v2f_t *max)
{
	return (v2f_t){
		.x = v->x < min->x ? min->x : v->x > max->x ? max->x : v->x,
		.y = v->y < min->y ? min->y : v->y > max->y ? max->y : v->y,
	};
}


static inline v2f_t * v2f_clamp(v2f_t *res, const v2f_t *v, const v2f_t *min, const v2f_t *max)
{
	if (_v2f_allocated(&res))
		*res = _v2f_clamp(v, min, max);

	return res;
}

#endif
© All Rights Reserved