summaryrefslogtreecommitdiff
path: root/src/modules/meta2d/v3f.h
blob: b1d3f4ca2623741c02cbb6e3dfe330c34f2b4ec6 (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
353
354
355
356
/*
 *  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/>.
 */

/*
 * 3D 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:
 *	v3f_t	foo, *foop;
 *
 *	foo = _v3f_mult(&(v3f_t){1.f,1.f,1.f}, &(v3f_t){2.f,2.f,2.f});
 *
 *	is equivalent to:
 *
 *	v3f_mult(&foo, &(v3f_t){1.f,1.f,1.f}, &(v3f_t){2.f,2.f,2.f});
 *
 *	or dynamically allocated:
 *
 *	foop = v3f_mult(NULL, &(v3f_t){1.f,1.f,1.f}, &(v3f_t){2.f,2.f,2.f});
 *	free(foop);
 *
 *	is equivalent to:
 *
 *	foop = malloc(sizeof(v3f_t));
 *	v3f_mult(foop, &(v3f_t){1.f,1.f,1.f}, &(v3f_t){2.f,2.f,2.f});
 *	free(foop);
 */

#ifndef _V3F_H
#define _V3F_H


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


typedef struct v3f_t {
	float	x, y, z;
} v3f_t;


static inline v3f_t * _v3f_allocated(v3f_t **ptr)
{
	if (!*ptr)
		*ptr = malloc(sizeof(v3f_t));

	return *ptr;
}


static inline v3f_t _v3f_add(const v3f_t *a, const v3f_t *b)
{
	return (v3f_t){a->x + b->x, a->y + b->y, a->z + b->z};
}


static inline v3f_t * v3f_add(v3f_t *res, const v3f_t *a, const v3f_t *b)
{
	if (_v3f_allocated(&res))
		*res = _v3f_add(a, b);

	return res;
}


static inline v3f_t _v3f_sub(const v3f_t *a, const v3f_t *b)
{
	return (v3f_t){a->x - b->x, a->y - b->y, a->z - b->z};
}


static inline v3f_t * v3f_sub(v3f_t *res, const v3f_t *a, const v3f_t *b)
{
	if (_v3f_allocated(&res))
		*res = _v3f_sub(a, b);

	return res;
}


static inline v3f_t _v3f_mult(const v3f_t *a, const v3f_t *b)
{
	return (v3f_t){a->x * b->x, a->y * b->y, a->z * b->z};
}


static inline v3f_t * v3f_mult(v3f_t *res, const v3f_t *a, const v3f_t *b)
{
	if (_v3f_allocated(&res))
		*res = _v3f_mult(a, b);

	return res;
}


static inline v3f_t _v3f_mult_scalar(const v3f_t *v, float scalar)
{
	return (v3f_t){v->x * scalar, v->y * scalar, v->z * scalar};
}


static inline v3f_t * v3f_mult_scalar(v3f_t *res, const v3f_t *v, float scalar)
{
	if (_v3f_allocated(&res))
		*res = _v3f_mult_scalar(v, scalar);

	return res;
}


static inline v3f_t _v3f_div_scalar(const v3f_t *v, float scalar)
{
	return _v3f_mult_scalar(v, 1.f / scalar);
}


static inline v3f_t * v3f_div_scalar(v3f_t *res, const v3f_t *v, float scalar)
{
	if (_v3f_allocated(&res))
		*res = _v3f_div_scalar(v, scalar);

	return res;
}


static inline float v3f_dot(const v3f_t *a, const v3f_t *b)
{
	return a->x * b->x + a->y * b->y + a->z * b->z;
}


static inline float v3f_length(const v3f_t *v)
{
	return sqrtf(v3f_dot(v, v));
}


static inline float v3f_distance(const v3f_t *a, const v3f_t *b)
{
	return v3f_length(v3f_sub(&(v3f_t){}, a, b));
}


static inline float v3f_distance_sq(const v3f_t *a, const v3f_t *b)
{
	v3f_t	d = _v3f_sub(a, b);

	return v3f_dot(&d, &d);
}


static inline v3f_t _v3f_normalize(const v3f_t *v)
{
	return _v3f_mult_scalar(v, 1.0f / v3f_length(v));
}


static inline v3f_t * v3f_normalize(v3f_t *res, const v3f_t *v)
{
	if (_v3f_allocated(&res))
		*res = _v3f_normalize(v);

	return res;
}


static inline v3f_t _v3f_lerp(const v3f_t *a, const v3f_t *b, float t)
{
	v3f_t	lerp_a, lerp_b;

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

	return _v3f_add(&lerp_a, &lerp_b);
}


static inline v3f_t * v3f_lerp(v3f_t *res, const v3f_t *a, const v3f_t *b, float t)
{
	if (_v3f_allocated(&res))
		*res = _v3f_lerp(a, b, t);

	return res;
}


static inline v3f_t _v3f_nlerp(const v3f_t *a, const v3f_t *b, float t)
{
	v3f_t	lerp = _v3f_lerp(a, b, t);

	return _v3f_normalize(&lerp);
}


static inline v3f_t * v3f_nlerp(v3f_t *res, const v3f_t *a, const v3f_t *b, float t)
{
	if (_v3f_allocated(&res))
		*res = _v3f_nlerp(a, b, t);

	return res;
}


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

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


static inline v3f_t * v3f_bilerp(v3f_t *res, const v3f_t *aa, const v3f_t *ab, const v3f_t *ba, const v3f_t *bb, float t_x, float t_y)
{
	if (_v3f_allocated(&res))
		*res = _v3f_bilerp(aa, ab, ba, bb, t_x, t_y);

	return res;
}


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

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


static inline v3f_t * v3f_trilerp(v3f_t *res, const v3f_t *aaa, const v3f_t *aba, const v3f_t *aab, const v3f_t *abb, const v3f_t *baa, const v3f_t *bba, const v3f_t *bab, const v3f_t *bbb, float t_x, float t_y, float t_z)
{
	if (_v3f_allocated(&res))
		*res = _v3f_trilerp(aaa, aba, aab, abb, baa, bba, bab, bbb, t_x, t_y, t_z);

	return res;
}


static inline v3f_t _v3f_cross(const v3f_t *a, const v3f_t *b)
{
	return (v3f_t){
		.x = a->y * b->z - a->z * b->y,
		.y = a->z * b->x - a->x * b->z,
		.z = a->x * b->y - a->y * b->x,
	};
}


static inline v3f_t * v3f_cross(v3f_t *res, const v3f_t *a, const v3f_t *b)
{
	if (_v3f_allocated(&res))
		*res = _v3f_cross(a, b);

	return res;
}


static inline v3f_t _v3f_rand(unsigned *seedp, const v3f_t *min, const v3f_t *max)
{
	return (v3f_t){
		.x = min->x + (float)rand_r(seedp) * (1.f/(float)RAND_MAX) * (max->x - min->x),
		.y = min->y + (float)rand_r(seedp) * (1.f/(float)RAND_MAX) * (max->y - min->y),
		.z = min->z + (float)rand_r(seedp) * (1.f/(float)RAND_MAX) * (max->z - min->z),
	};
}


static inline v3f_t * v3f_rand(v3f_t *res, unsigned *seedp, const v3f_t *min, const v3f_t *max)
{
	if (_v3f_allocated(&res))
		*res = _v3f_rand(seedp, min, max);

	return res;
}


static inline v3f_t _v3f_ceil(const v3f_t *v)
{
	return (v3f_t){
		.x = ceilf(v->x),
		.y = ceilf(v->y),
		.z = ceilf(v->z),
	};
}


static inline v3f_t * v3f_ceil(v3f_t *res, const v3f_t *v)
{
	if (_v3f_allocated(&res))
		*res = _v3f_ceil(v);

	return res;
}


static inline v3f_t _v3f_floor(const v3f_t *v)
{
	return (v3f_t){
		.x = floorf(v->x),
		.y = floorf(v->y),
		.z = floorf(v->z),
	};
}


static inline v3f_t * v3f_floor(v3f_t *res, const v3f_t *v)
{
	if (_v3f_allocated(&res))
		*res = _v3f_floor(v);

	return res;
}

#endif
© All Rights Reserved