-
Notifications
You must be signed in to change notification settings - Fork 0
/
vecn.c
209 lines (177 loc) · 4.63 KB
/
vecn.c
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
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include "util.c"
#define vec(n) struct { float f[n]; }
#define vecn(x) ((int)(sizeof(x) / sizeof(float)))
#define vecn2(name, ...) vec(countargs(__VA_ARGS__)) name = {0}; vset(name, __VA_ARGS__)
#define vfrom(dest, src) vec(vecn(src)) dest = {0}; vcpy(dest, src)
#define vcpy(a, b) \
vcpy_(vecn(a), (a).f, vecn(b), (b).f)
#define vadd(a, b) \
vadd_(vecn(a), (a).f, vecn(b), (b).f)
#define vsub(a, b) \
vsub_(vecn(a), (a).f, vecn(b), (b).f)
#define vscale(a, b) \
vscale_(vecn(a), (a).f, (b))
#define vlen2(a) \
vlen2_(vecn(a), (a).f)
#define vlen(a) \
vlen_(vecn(a), (a).f)
#define vnorm(a) \
vnorm_(vecn(a), (a).f)
#define vdist2(a, b) \
vdist2_(vecn(a), (a).f, vecn(b), (b).f)
#define vdist(a, b) \
vdist_(vecn(a), (a).f, vecn(b), (b).f)
#define vdot(a, b) \
vdot_(vecn(a), (a).f, vecn(b), (b).f)
#define vdir2(a, b) \
vdir2_(vecn(a), (a).f, vecn(b), (b).f)
#define vlerp(a, b, t) \
vlerp_(vecn(a), (a).f, vecn(b), (b).f, (t))
#define vdebug(a) \
vdebug_(#a, vecn(a), (a).f)
#define vset(x, ...) \
vset_(vecn(x), (x).f, countargs(__VA_ARGS__), __VA_ARGS__)
void vset_va(int xn, float *x, int va_count, va_list va)
{
xn = xn > va_count ? va_count : xn;
for (int i = 0; i < xn; i++, x++) {
float v = (float) va_arg(va, double);
*x = v;
}
}
void vset_(int xn, float *x, int va_count, ...)
{
va_list va;
va_start(va, va_count);
vset_va(xn, x, va_count, va);
va_end(va);
}
void vcpy_(int an, float *a, int bn, float *b)
{
int n = min(an, bn);
for (int i = 0; i < n; i++, a++, b++)
*a = *b;
}
void vadd_(int an, float *a, int bn, float *b)
{
int n = min(an, bn);
for (int i = 0; i < n; i++, a++, b++)
*a += *b;
}
void vsub_(int an, float *a, int bn, float *b)
{
int n = min(an, bn);
for (int i = 0; i < n; i++, a++, b++)
*a -= *b;
}
void vscale_(int an, float *a, float b)
{
for (int i = 0; i < an; i++, a++)
*a *= b;
}
float vlen2_(int an, float *a)
{
float r = 0;
for (int i = 0; i < an; i++, a++)
r += sqr(*a);
return r;
}
float vlen_(int an, float *a)
{
float r = vlen2_(an, a);
r = sqrtf(r);
return r;
}
void vnorm_(int an, float *a)
{
float len = vlen_(an, a);
if (len > 0) {
float ilen = 1 / len;
for (int i = 0; i < an; i++, a++)
*a *= ilen;
}
}
float vdist2_(int an, float *a, int bn, float *b)
{
float r = 0;
int n = min(an, bn);
for (int i = 0; i < n; i++, a++, b++)
r += sqr(*b - *a);
return r;
}
float vdist_(int an, float *a, int bn, float *b)
{
float r = vdist2_(an, a, bn, b);
r = sqrtf(r);
return r;
}
float vdot_(int an, float *a, int bn, float *b)
{
float r = 0;
int n = min(an, bn);
for (int i = 0; i < n; i++, a++, b++)
r += *a * *b;
return r;
}
void vdir2_(int an, float *a, int bn, float *b)
{
float *src = a;
int n = min(an, bn);
for (int i = 0; i < n; i++, a++, b++)
*a = *b - *a;
vnorm_(an, src);
}
void vlerp_(int an, float *a, int bn, float *b, float t)
{
int n = min(an, bn);
for (int i = 0; i < n; i++, a++, b++)
*a = (1.0f - t) * *a + t * *b;
}
void vdebug_(const char *name, int an, float *a)
{
fprintf(stderr, "%s -> (", name);
for (int i = 0; i < an; i++, a++)
fprintf(stderr, "%f%s", *a, i + 1 == an ? "" : ", ");
fprintf(stderr, ")\n");
}
#ifdef run_vecn
#include <stdio.h>
struct entity {
vec(3) position;
vec(2) acceleration;
};
int main() {
fprintf(stderr, "\n\nrunning tests from vecn.c\n\n");
struct entity monster = {0};
vset(monster.position, 3.0f, 2.0f);
fprintf(stderr, "initial monster position\n");
vdebug(monster.position);
// or...
// monster.position.f[0] = 3.0f;
// monster.position.f[1] = 2.0f;
monster.acceleration.f[0] = 1.0f;
monster.acceleration.f[1] = 2.0f;
fprintf(stderr, "\ninitial monster acceleration\n");
vdebug(monster.acceleration);
// vec(2) wind;
// vset(wind, 2.0f, 3.0f);
// or...
vecn2(wind, 2.0f, 3.0f);
// monster.acceleration += wind
vadd(monster.acceleration, wind);
fprintf(stderr, "\nacceleration affected by wind\n");
vdebug(monster.acceleration);
// monster.position += monster.acceleration
vadd(monster.position, monster.acceleration);
fprintf(stderr, "\nmonster position updated by acceleration\n");
vdebug(monster.position);
fprintf(stderr, "\ncreate a copy from a vec\n");
vfrom(monster_copied_position, monster.position);
vdebug(monster_copied_position);
fprintf(stderr, "all done!\n");
return 0;
}
#endif