-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
181 lines (137 loc) · 4.57 KB
/
vector.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
#include "vector.h"
void add_v3d(v3d a, v3d b, v3d *ret) {
ret->x = a.x + b.x;
ret->y = a.y + b.y;
ret->z = a.z + b.z;
}
void sub_v3d(v3d a, v3d b, v3d *ret) {
ret->x = a.x - b.x;
ret->y = a.y - b.y;
ret->z = a.z - b.z;
}
void mul_v3d(v3d a, double b, v3d *ret) {
ret->x = a.x * b;
ret->y = a.y * b;
ret->z = a.z * b;
}
double dot_v3d(v3d a, v3d b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
double det(double a, double b, double c, double d) {
return a * d - b * c;
}
void cro_v3d(v3d a, v3d b, v3d *ret) {
double i = det(a.y, a.z, b.y, b.z);
double j = det(a.x, a.z, b.x, b.z) * -1;
double k = det(a.x, a.y, b.x, b.y);
ret->x = i;
ret->y = j;
ret->z = k;
}
double len_v3d(v3d a) {
return sqrt(lsq_v3d(a));
}
double lsq_v3d(v3d a) {
return a.x * a.x + a.y * a.y + a.z * a.z;
}
void nrm_v3d(v3d a, v3d *ret) {
double len = len_v3d(a);
ret->x = a.x / len;
ret->y = a.y / len;
ret->z = a.z / len;
}
void nrm_c_v3d(v3d *a) {
double len = len_v3d(*a);
a->x = a->x / len;
a->y = a->y / len;
a->z = a->z / len;
}
void prj_v3d(v3d a, v3d b, v3d *ret) {
//a|| = (a . b^) * b^
v3d bhat;
nrm_v3d(b, &bhat);
double dot = dot_v3d(a, bhat);
mul_v3d(bhat, dot, ret);
}
void rej_v3d(v3d a, v3d b, v3d *ret) {
v3d parallel;
prj_v3d(a, b, ¶llel);
sub_v3d(a, parallel, ret);
}
#define EQ_DOUBLE(X, Y) (fabs((X) - (Y)) < 0.0001)
int test_v3d() {
v3d a, b;
a.x = 5.1, a.y = 2.7, a.z = 3;
b.x = 3, b.y = 7, b.z = 4.2;
double scalar = 2.34;
double answer;
v3d ret;
add_v3d(a, b, &ret);
if(!(EQ_DOUBLE(ret.x, 8.1) && EQ_DOUBLE(ret.y, 9.7) && EQ_DOUBLE(ret.z, 7.2))) {
fprintf(stderr, "v3d add\nFILE: %s\nLINE: %d\nFUNCTION: %%s\n", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
sub_v3d(a, b, &ret);
if(!(EQ_DOUBLE(ret.x, 2.1) && EQ_DOUBLE(ret.y, -4.3) && EQ_DOUBLE(ret.z, -1.2))) {
fprintf(stderr, "v3d sub\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
mul_v3d(a, scalar, &ret);
if(!(EQ_DOUBLE(ret.x, 11.934) && EQ_DOUBLE(ret.y, 6.318) && EQ_DOUBLE(ret.z, 7.02))) {
fprintf(stderr, "v3d mul\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
answer = dot_v3d(a, b);
if(!EQ_DOUBLE(answer, 46.8)) {
fprintf(stderr, "v3d dot\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "ans: %f\n", answer);
return 0;
}
cro_v3d(a, b, &ret);
if(!(EQ_DOUBLE(ret.x, -9.66) && EQ_DOUBLE(ret.y, -12.42) && EQ_DOUBLE(ret.z, 27.6))) {
fprintf(stderr, "v3d cro\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
answer = lsq_v3d(a);
if(!EQ_DOUBLE(answer, 42.3)) {
fprintf(stderr, "v3d length squared\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "ans: %f\n", answer);
return 0;
}
answer = len_v3d(a);
if(!EQ_DOUBLE(answer, 6.503845016)) {
fprintf(stderr, "v3d length \nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "ans: %f\n", answer);
return 0;
}
nrm_v3d(a, &ret);
if(!(EQ_DOUBLE(ret.x, 0.784151526) && EQ_DOUBLE(ret.y, 0.415139043) && EQ_DOUBLE(ret.z, 0.461265604))) {
fprintf(stderr, "v3d normalized\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
ret = a;
nrm_c_v3d(&ret);
if(!(EQ_DOUBLE(ret.x, 0.784151526) && EQ_DOUBLE(ret.y, 0.415139043) && EQ_DOUBLE(ret.z, 0.461265604))) {
fprintf(stderr, "v3d normalized in place\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
prj_v3d(a, b, &ret);
if(!(EQ_DOUBLE(ret.x, 1.856160761) && EQ_DOUBLE(ret.y, 4.331041776) && EQ_DOUBLE(ret.z, 2.598625066))) {
fprintf(stderr, "v3d projection\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
rej_v3d(a, b, &ret);
if(!(EQ_DOUBLE(ret.x, 5.1 - 1.856160761) && EQ_DOUBLE(ret.y, 2.7 - 4.331041776) && EQ_DOUBLE(ret.z, 3 - 2.598625066))) {
fprintf(stderr, "v3d projection\nFILE: %s\nLINE: %d\nFUNCTION: %%s", __FILE__, __LINE__/*, __FUNCTION__*/);
fprintf(stderr, "a: %f\tb: %f\tc: %f\n", ret.x, ret.y, ret.z);
return 0;
}
return 1;
}