-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.c
193 lines (149 loc) · 3.83 KB
/
vec.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
#include "vec.h"
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define _VEC_MIN_LENGTH(a, b) ({ \
double _a = VEC_HEADER(a)->length; \
double _b = VEC_HEADER(b)->length; \
_a < _b ? _a : _b; \
})
static inline bool length_will_overflow(const uint64_t length)
{
if (length > (sizeof(uint64_t) >> 1) - sizeof(struct vec_header))
return true;
return false;
}
double *vec_new(const uint64_t length, const double * const values)
{
if (length_will_overflow(length))
return NULL;
struct vec_header *header = malloc(sizeof(struct vec_header) * (length + 1));
header->temp = true;
header->length = length;
memcpy(header->vector, values, length * sizeof(double));
return (double *)(header->vector);
}
double *vec_blank(const uint64_t length)
{
if (length_will_overflow(length))
return NULL;
struct vec_header *header = calloc(length + 1, sizeof(struct vec_header));
header->temp = true;
header->length = length;
return (double *)(header->vector);
}
void vec_del(double * const vector)
{
free(vector - 1);
}
void vec_deltemp(double * const vector)
{
if (VEC_HEADER(vector)->temp)
vec_del(vector);
}
inline double *vec_temp(double * const vector)
{
VEC_HEADER(vector)->temp = 1;
return vector;
}
inline double *vec_perm(double * const vector)
{
VEC_HEADER(vector)->temp = 0;
return vector;
}
double vec_dot(double * const vector_a, double * const vector_b)
{
uint64_t length = _VEC_MIN_LENGTH(vector_a, vector_b);
double result = 0;
for (uint64_t i = 0; i < length; i++)
result += vector_a[i] * vector_b[i];
if (vector_b != vector_a)
vec_deltemp(vector_b);
vec_deltemp(vector_a);
return result;
}
double *vec_cross(double * const vector_a, double * const vector_b)
{
struct vec_header *vech_a = VEC_HEADER(vector_a);
struct vec_header *vech_b = VEC_HEADER(vector_b);
if (vech_a->length != vech_b->length && vech_a->length != 3)
return NULL;
double i, j, k;
i = vector_a[1] * vector_b[2] - vector_a[2] * vector_b[1];
j = vector_a[2] * vector_b[0] - vector_a[0] * vector_b[2];
k = vector_a[0] * vector_b[1] - vector_a[1] * vector_b[0];
if (vech_a->temp) {
vec_deltemp(vector_b);
vector_a[0] = i;
vector_a[1] = j;
vector_a[2] = k;
return vector_a;
} else if (vech_b->temp) {
vector_b[0] = i;
vector_b[1] = j;
vector_b[2] = k;
return vector_b;
} else {
return vec_new(3, (double []){i, j, k});
}
}
double vec_len(double * const vector)
{
return sqrtf(vec_dot(vector, vector));
}
double *vec_addm(double * const vector_a, double * const vector_b, const double multiplier)
{
struct vec_header *vech_a = VEC_HEADER(vector_a);
struct vec_header *vech_b = VEC_HEADER(vector_b);
uint64_t length = _VEC_MIN_LENGTH(vector_a, vector_b);
double *output;
if (vech_a->temp)
output = vector_a;
else if (vech_b->temp)
output = vector_b;
else
output = vec_blank(length);
for (uint64_t i = 0; i < length; i++)
output[i] = vector_a[i] + vector_b[i] * multiplier;
if (vech_a->temp && vech_b->temp)
vec_deltemp(vector_b);
return output;
}
double *vec_norm(double * const vector)
{
struct vec_header *vech = VEC_HEADER(vector);
double *output;
#ifdef FAST
double ilength;
if (vech->temp) {
ilength = 1 / vec_len(vec_perm(vector));
vec_temp(vector);
output = vector;
} else {
ilength = 1 / vec_len(vector);
output = vec_blank(vech->length);
}
for (uint64_t i = 0; i < vech->length; i++)
output[i] = vector[i] * ilength;
#else
double length;
if (vech->temp) {
length = vec_len(vec_perm(vector));
vec_temp(vector);
output = vector;
} else {
length = vec_len(vector);
output = vec_blank(vech->length);
}
for (uint64_t i = 0; i < vech->length; i++)
output[i] = vector[i] / length; /* SLOW */
#endif
return output;
}
double *vec_mirr(double * const vector, double * const normal)
{
return vec(0, 0, 0);
}