-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvec3.cu
223 lines (186 loc) · 5.33 KB
/
vec3.cu
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
/* vec3.cu: three-dimensional vector types for gpufield.
* Copyright (C) 2014 Bradley Worley.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* 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, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*/
/* include the vec3 header. */
#include "vec3.h"
/* define the permittivity of vacuum. */
#define MU_0 (4.0 * M_PI * 1.0e-7)
/* vector: create a 3-vector.
* @x: the x-component.
* @y: the y-component.
* @z: the z-component.
*/
vec3 vector (float x, float y, float z) {
/* declare the output vector. */
vec3 v;
/* set the vector components. */
v.x = x;
v.y = y;
v.z = z;
/* return the vector. */
return v;
}
/* vcmp: compare two 3-vectors.
* @a: the first vector.
* @b: the second vector.
*/
int vcmp (vec3 a, vec3 b) {
/* compute and return equality. */
return ((a.x == b.x) && (a.y == b.y) && (a.z == b.z));
}
/* len: compute the length (euclidean norm) of a 3-vector.
* @v: the vector to compute.
*/
float len (vec3 v) {
/* compute and return the length. */
return sqrt (v.x * v.x + v.y * v.y + v.z * v.z);
}
/* dot: compute the dot product of two 3-vectors.
* @a: the first vector in the product.
* @b: the second vector in the product.
*/
float dot (vec3 a, vec3 b) {
/* compute and return the dot product. */
return (a.x * b.x + a.y * b.y + a.z * b.z);
}
/* cross: compute the cross product of two 3-vectors.
* @a: the first vector in the product.
* @b: the second vector in the product.
*/
vec3 cross (vec3 a, vec3 b) {
/* declare the output vector. */
vec3 c;
/* compute the elements of the output vector. */
c.x = a.y * b.z - a.z * b.y;
c.y = a.z * b.x - a.x * b.z;
c.z = a.x * b.y - a.y * b.x;
/* return the output vector. */
return c;
}
/* unit: compute the unit 3-vector of a 3-vector.
* @v: the vector to compute.
*/
vec3 unit (vec3 v) {
/* declare the output vector. */
float l;
vec3 u;
/* compute the input vector length. */
l = len (v);
/* compute the components of the output vector. */
u.x = v.x / l;
u.y = v.y / l;
u.z = v.z / l;
/* return the output vector. */
return u;
}
/* scale: scale a 3-vector by a scalar value.
* @alpha: the scalar value.
* @v: the vector to scale.
*/
vec3 scale (float alpha, vec3 v) {
/* declare the output vector. */
vec3 s;
/* compute the elements of the output vector. */
s.x = alpha * v.x;
s.y = alpha * v.y;
s.z = alpha * v.z;
/* return the output vector. */
return s;
}
/* proj: project a 3-vector onto another unit 3-vector.
* @v: the 3-vector to compute.
* @u: the unit 3-vector to project @v onto.
*/
vec3 proj (vec3 v, vec3 u) {
/* compute and return the projection. */
return scale (dot (v, u), u);
}
/* add: add two 3-vectors.
* @a: the first vector.
* @b: the second vector.
*/
vec3 add (vec3 a, vec3 b) {
/* declare the output vector. */
vec3 c;
/* compute the elements of the output vector. */
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
/* return the output vector. */
return c;
}
/* sub: subtract two 3-vectors.
* @a: the first vector.
* @b: the second vector.
*/
vec3 sub (vec3 a, vec3 b) {
/* declare the output vector. */
vec3 c;
/* compute the elements of the output vector. */
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
/* return the output vector. */
return c;
}
/* vinterp: interpolates between two 3-vectors.
* @a: the starting vector.
* @b: the ending vector.
* @t: the interpolation factor.
*/
vec3 vinterp (vec3 a, vec3 b, float t) {
/* compute and return the output vector. */
return add (a, scale (t, sub (b, a)));
}
/* field: compute the magnetic field vector at a point M due to an
* infinitely narrow wire along segment AB carrying current I.
*/
vec3 field (vec3 A, vec3 B, vec3 M, float I) {
/* declare required variables. */
float c1, c2, dLM, mag;
vec3 vAB, vAM, vBM, vLM;
vec3 uAB, uAM, uBM, uLM;
vec3 f;
/* compute the vector between the endpoints of the wire. */
vAB = sub (B, A);
uAB = unit (vAB);
/* compute the vector from the start point to the calculation point. */
vAM = sub (M, A);
uAM = unit (vAM);
/* compute the vector from the end point to the calculation point. */
vBM = sub (M, B);
uBM = unit (vBM);
/* find the angles from the wire ends to the calculation point. */
c1 = dot (uAB, uAM);
c2 = dot (uAB, uBM);
/* compute a vector from the wire to the calculation point, such that
* the vector is perpindicular to the wire.
*/
vLM = sub (vAM, proj (vAM, uAB));
uLM = unit (vLM);
dLM = len (vLM);
/* compute the magnitude of the field vector. */
mag = ((MU_0 * I) / (4.0 * M_PI * dLM)) * (c1 - c2);
/* compute the direction (and scale it) of the field vector. */
f = cross (uAB, uLM);
f = scale (mag, f);
/* return the computed field value. */
return f;
}