-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectNd.h
365 lines (324 loc) · 8.68 KB
/
vectNd.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
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
357
358
359
360
361
362
363
364
365
/*
* vectNd.h
* ndt: n-dimensional tracer
*
* Copyright (c) 2019-2021 Bryan Franklin. All rights reserved.
*/
#ifndef VECTND_H
#define VECTND_H
#include <math.h>
#if defined(__SSE__)
#include <xmmintrin.h>
#endif /* __SSE__ */
#include <string.h>
#define VECTND_SUCCESS 1
#define VECTND_FAIL 0
/* this needs to be an even number when using SSE */
#define VECTND_DEF_SIZE 4
#define rad2deg(x) ((x)*180.0/M_PI)
#define deg2rad(x) ((x)*M_PI/180.0)
#ifndef EPSILON
#define EPSILON (1e-4)
#endif /* EPSILON */
#ifndef EPSILON2
#define EPSILON2 ((EPSILON)*(EPSILON))
#endif /* EPSILON2 */
#if defined(__SSE__) && !defined(WITHOUT_SSE)
/* typecast v to __m128d* */
#define vectNd_SSE(x) ((__m128d*)((x)->v))
#else
#warning "Not using SSE"
#endif /* __SSE__ */
#ifndef __SSE__
#error "SSE not available"
#endif /* 0 */
typedef struct vectNd_t
{
double space[VECTND_DEF_SIZE];
double *v;
int n;
}
#if defined(__SSE__) && !defined(WITHOUT_SSE)
__attribute__((__aligned__(16)))
#endif /* __SSE__ */
vectNd;
int vectNd_cross(vectNd *v1, vectNd *res);
int vectNd_orthogonalize(vectNd *in1, vectNd *in2, vectNd *out1, vectNd *out2);
int vectNd_angle(vectNd *v1, vectNd *v2, double *angle);
int vectNd_angle3(vectNd *p1, vectNd *p2, vectNd *p3, double *angle);
int vectNd_reflect(vectNd *v, vectNd *normal, vectNd *res, double mag);
int vectNd_refract(vectNd *v, vectNd *normal, vectNd *res, double index);
int vectNd_interpolate(vectNd *s, vectNd *e, double x, vectNd *r);
int vectNd_rotate(vectNd *v, vectNd *center, int i, int j, double angle, vectNd *res);
int vectNd_rotate2(vectNd *v, vectNd *center, vectNd *v1, vectNd *v2, double angle, vectNd *res);
int vectNd_print(vectNd *v, char *name);
/* see: https://en.wikipedia.org/wiki/Inline_function#Nonstandard_extensions */
#ifdef _MSC_VER
#define forceinline __forceinline
#elif defined(__GNUC__)
#define forceinline inline __attribute__((__always_inline__))
#elif defined(__CLANG__)
#if __has_attribute(__always_inline__)
#define forceinline inline __attribute__((__always_inline__))
#else
#define forceinline inline
#endif
#else
#define forceinline inline
#endif
static forceinline int vectNd_fill(vectNd *v, double val)
{
int i=0;
#if defined(__SSE__) && !defined(WITHOUT_SSE)
int k=(v->n+1)/2;
for(i=0; i<k; ++i)
vectNd_SSE(v)[i] = _mm_set1_pd(val);
#else
for(i=0; i<v->n; ++i)
v->v[i] = val;
#endif /* __SSE__ */
return VECTND_SUCCESS;
}
static forceinline int vectNd_get(vectNd *v, int pos, double *val) {
if( pos<0 || pos >= v->n )
return VECTND_FAIL;
*val = v->v[pos];
return VECTND_SUCCESS;
}
static forceinline int vectNd_set(vectNd *v, int pos, double val)
{
if( pos<0 || pos >= v->n )
return VECTND_FAIL;
v->v[pos] = val;
return VECTND_SUCCESS;
}
static forceinline int vectNd_setStr(vectNd *v, char *str)
{
int pos=0;
char *lstr = strdup(str);
char *lasts=NULL;
char *a = strtok_r(lstr,",",&lasts);
while( a!=NULL ) {
vectNd_set(v,pos++,atof(a));
a = strtok_r(NULL,",",&lasts);
}
free(lstr); lstr=NULL;
return VECTND_SUCCESS;
}
static forceinline int vectNd_alloc(vectNd *v, int dim)
{
v->n = dim;
if( dim > VECTND_DEF_SIZE ) {
int alloc_dim = dim;
#if defined(__SSE__) && !defined(WITHOUT_SSE)
alloc_dim += alloc_dim&1; /* make even if needed */
#endif /* __SSE__ */
#ifdef __SSE__
void *ptr=NULL;
if( posix_memalign(&ptr, 16, alloc_dim*sizeof(double)) ) {
v->v = NULL;
return VECTND_FAIL;
}
v->v = ptr;
#else
v->v = (double*)malloc(alloc_dim*sizeof(double));
#endif /* 0 */
} else {
v->v = v->space;
}
#if defined(__SSE__) && !defined(WITHOUT_SSE)
/* set padding field to zero if an odd number of dimensions */
if( dim & 1 ) v->v[dim] = 0.0;
#endif /* __SSE__ */
return VECTND_SUCCESS;
}
static forceinline int vectNd_calloc(vectNd *v, int dim)
{
vectNd_alloc(v,dim);
vectNd_fill(v,0.0);
return VECTND_SUCCESS;
}
static forceinline int vectNd_free(vectNd *v)
{
if( v->n > VECTND_DEF_SIZE ) {
free(v->v); v->v = NULL;
}
v->v = NULL;
v->n = -1;
return VECTND_SUCCESS;
}
static forceinline int vectNd_reset(vectNd *v)
{
memset(v->v,'\0',v->n*sizeof(*v->v));
return VECTND_SUCCESS;
}
static forceinline void vectNd_min(vectNd *v, double *res) {
int dim = v->n;
int i;
double *vv = v->v;
double min = vv[0];
for(i=1; i<dim; ++i) {
double vvi = vv[i];
min = (vvi<min)?(vvi):(min);
}
*res = min;
}
static forceinline void vectNd_max(vectNd *v, double *res) {
int dim = v->n;
int i;
double *vv = v->v;
double max = vv[0];
for(i=1; i<dim; ++i) {
double vvi = vv[i];
max = (vvi>max)?(vvi):(max);
}
*res = max;
}
static forceinline void vectNd_mul(vectNd *v1, vectNd *v2, vectNd *res) {
int dim = v1->n;
int i;
double *v1v;
double *v2v;
double *r;
v1v = v1->v;
v2v = v2->v;
r = res->v;
for(i=0; i<dim; ++i) {
r[i] = v1v[i] * v2v[i];
}
}
static forceinline void vectNd_dot(vectNd *v1, vectNd *v2, double *res)
{
#if defined(__SSE__) && !defined(WITHOUT_SSE)
int k=(v1->n+1)>>1;
int i;
__m128d prod;
__m128d sums;
sums = _mm_mul_pd(vectNd_SSE(v1)[0],vectNd_SSE(v2)[0]);
for(i=1; i<k; ++i) {
prod = _mm_mul_pd(vectNd_SSE(v1)[i],vectNd_SSE(v2)[i]);
sums = _mm_add_pd(sums,prod);
}
*res = sums[0]+sums[1];
#else /* __SSE__ */
int dim = v1->n;
double sum = 0.0;
int i;
double *v1v;
double *v2v;
v1v = v1->v;
v2v = v2->v;
for(i=0; i<dim; ++i) {
sum += v1v[i] * v2v[i];
}
*res = sum;
#endif /* __SSE__ */
}
static forceinline void vectNd_add(vectNd *v1, vectNd *v2, vectNd *res)
{
#if defined(__SSE__) && !defined(WITHOUT_SSE)
int k=(v1->n+1)>>1;
int i;
vectNd_SSE(res)[0] = _mm_add_pd(vectNd_SSE(v1)[0],vectNd_SSE(v2)[0]);
for(i=1; i<k; ++i)
vectNd_SSE(res)[i] = _mm_add_pd(vectNd_SSE(v1)[i],vectNd_SSE(v2)[i]);
#else /* __SSE__ */
double *v1v;
double *v2v;
double *resv;
int n;
int i;
v1v = v1->v;
v2v = v2->v;
resv = res->v;
n = v1->n;
for(i=0; i<n; ++i)
resv[i] = v1v[i] + v2v[i];
#endif /* __SSE__ */
}
static forceinline void vectNd_sub(vectNd *v1, vectNd *v2, vectNd *res)
{
#if defined(__SSE__) && !defined(WITHOUT_SSE)
int k=(v1->n+1)>>1;
int i;
vectNd_SSE(res)[0] = _mm_sub_pd(vectNd_SSE(v1)[0],vectNd_SSE(v2)[0]);
for(i=1; i<k; ++i)
vectNd_SSE(res)[i] = _mm_sub_pd(vectNd_SSE(v1)[i],vectNd_SSE(v2)[i]);
#else /* __SSE__ */
double *v1v;
double *v2v;
double *resv;
int n;
int i;
n = v1->n;
v1v = v1->v;
v2v = v2->v;
resv = res->v;
for(i=0; i<n; ++i)
resv[i] = v1v[i] - v2v[i];
#endif /* __SSE__ */
}
static forceinline void vectNd_scale(vectNd *v, double s, vectNd *res)
{
#if defined(__SSE__) && !defined(WITHOUT_SSE)
int k=(v->n+1)>>1;
int i;
__m128d scale = _mm_set1_pd(s);
vectNd_SSE(res)[0] = _mm_mul_pd(vectNd_SSE(v)[0],scale);
for(i=1; i<k; ++i)
vectNd_SSE(res)[i] = _mm_mul_pd(vectNd_SSE(v)[i],scale);
#else /* __SSE__ */
double *vv;
double *resv;
int n;
int i;
vv = v->v;
resv = res->v;
n = v->n;
for(i=0; i<n; ++i)
resv[i] = vv[i]*s;
#endif /* __SSE__ */
}
static forceinline void vectNd_l2norm(vectNd *v, double *res)
{
double sum;
vectNd_dot(v,v,&sum);
*res = sqrt(sum);
}
#define vectNd_length vectNd_l2norm
static forceinline void vectNd_unitize(vectNd *v)
{
double len;
vectNd_l2norm(v,&len);
if( len > EPSILON || len < -EPSILON )
vectNd_scale(v,1.0/len,v);
}
static forceinline void vectNd_dist(vectNd *v1, vectNd *v2, double *res)
{
vectNd diff;
vectNd_alloc(&diff,v1->n);
vectNd_sub(v1,v2,&diff);
vectNd_l2norm(&diff,res);
vectNd_free(&diff);
}
static forceinline void vectNd_copy(vectNd *dst, vectNd *src)
{
memcpy(dst->v,src->v,src->n*sizeof(*dst->v));
}
/* project a vector onto a vector known to be unit length */
static forceinline void vectNd_proj_unit(vectNd *v, vectNd *onto, vectNd *res)
{
double ab;
vectNd_dot(v,onto,&ab);
vectNd_scale(onto,ab,res);
}
/* project a vector onto a vector of unknown length */
static forceinline void vectNd_proj(vectNd *v, vectNd *onto, vectNd *res)
{
/* see: http://en.wikipedia.org/wiki/Vector_projection#Vector_projection_2 */
double ab, bb;
vectNd_dot(onto,onto,&bb);
vectNd_dot(v,onto,&ab);
vectNd_scale(onto,ab/bb,res);
}
#endif /* VECTND_H */