Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOVector.h
Go to the documentation of this file.
1/*
2
3OOVector.h
4
5Mathematical framework for Oolite.
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
27
28#ifndef INCLUDED_OOMATHS_h
29 #error Do not include OOVector.h directly; include OOMaths.h.
30#else
31
32
33#ifndef OOMATHS_EXTERNAL_VECTOR_TYPES
34
35typedef struct Vector
36{
37 OOScalar x;
38 OOScalar y;
39 OOScalar z;
40} Vector;
41
42
43typedef struct Vector2D
44{
45 OOScalar x;
46 OOScalar y;
47} Vector2D;
48
49#endif
50
51
52extern const Vector kZeroVector, /* 0, 0, 0 */
53 kBasisXVector, /* 1, 0, 0 */
54 kBasisYVector, /* 0, 1, 0 */
55 kBasisZVector; /* 0, 0, 1 */
56
57
58extern const Vector2D kZeroVector2D, /* 0, 0 */
59 kBasisXVector2D, /* 1, 0 */
60 kBasisYVector2D; /* 0, 1 */
61
62
63/* Construct vector */
64OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC;
65OOINLINE Vector2D MakeVector2D(OOScalar vx, OOScalar vy) INLINE_CONST_FUNC;
66
67#if !OOMATHS_STANDALONE
68/* Generate random vectors. */
69Vector OORandomUnitVector(void);
70Vector OOVectorRandomSpatial(OOScalar maxLength); // Random vector uniformly distributed in radius-maxLength sphere. (Longer vectors are more common.)
71Vector OOVectorRandomRadial(OOScalar maxLength); // Random vector with uniform distribution of direction and radius in radius-maxLength sphere. (Causes clustering at centre.)
72// only needed in high-precision forms so far
73//Vector OORandomPositionInCylinder(Vector centre1, OOScalar exclusion1, Vector centre2, OOScalar exclusion2, OOScalar radius);
74//Vector OORandomPositionInShell(Vector centre, OOScalar inner, OOScalar outer);
75#endif
76
77/* Multiply vector by scalar (in place) */
78OOINLINE void scale_vector(Vector *outVector, OOScalar factor) ALWAYS_INLINE_FUNC NONNULL_FUNC;
79
80/* Multiply vector by scalar */
81OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s) INLINE_CONST_FUNC;
82
83/* Addition and subtraction of vectors */
84OOINLINE Vector vector_add(Vector a, Vector b) INLINE_CONST_FUNC;
85OOINLINE Vector vector_subtract(Vector a, Vector b) INLINE_CONST_FUNC;
86#define vector_between(a, b) vector_subtract(b, a)
87OOINLINE Vector vector_flip(Vector v) INLINE_CONST_FUNC;
88
89/* Vector linear interpolation */
90OOINLINE Vector OOVectorInterpolate(Vector a, Vector b, OOScalar where) INLINE_CONST_FUNC;
91OOINLINE Vector OOVectorTowards(Vector a, Vector b, OOScalar where) INLINE_CONST_FUNC;
92
93/* Comparison of vectors */
94OOINLINE bool vector_equal(Vector a, Vector b) INLINE_CONST_FUNC;
95
96/* Square of magnitude of vector */
97OOINLINE OOScalar magnitude2(Vector vec) INLINE_CONST_FUNC;
98
99/* Magnitude of vector */
100OOINLINE OOScalar magnitude(Vector vec) INLINE_CONST_FUNC;
101
102/* Normalize vector */
103OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC;
104
105/* Normalize vector, returning fallback if zero vector. */
106OOINLINE Vector vector_normal_or_fallback(Vector vec, Vector fallback) INLINE_CONST_FUNC;
107OOINLINE Vector vector_normal_or_xbasis(Vector vec) INLINE_CONST_FUNC;
108OOINLINE Vector vector_normal_or_ybasis(Vector vec) INLINE_CONST_FUNC;
109OOINLINE Vector vector_normal_or_zbasis(Vector vec) INLINE_CONST_FUNC;
110
111/* Square of distance between vectors */
112OOINLINE OOScalar distance2(Vector v1, Vector v2) INLINE_CONST_FUNC;
113
114/* Distance between vectors */
115OOINLINE OOScalar distance(Vector v1, Vector v2) INLINE_CONST_FUNC;
116
117/* Dot product */
118OOINLINE OOScalar dot_product (Vector first, Vector second) INLINE_CONST_FUNC;
119
120/* NORMALIZED cross product */
121OOINLINE Vector cross_product(Vector first, Vector second) INLINE_CONST_FUNC;
122
123/* General cross product */
124OOINLINE Vector true_cross_product(Vector first, Vector second) CONST_FUNC;
125
126/* Triple product */
127OOINLINE OOScalar triple_product(Vector first, Vector second, Vector third) INLINE_CONST_FUNC;
128
129/* Given three points on a surface, returns the normal to the surface. */
130OOINLINE Vector normal_to_surface(Vector v1, Vector v2, Vector v3) CONST_FUNC;
131
132#if __OBJC__
133NSString *VectorDescription(Vector vector); // @"(x, y, z)"
134
135/* For storing vectors in NSArrays */
136@interface OONativeVector: NSObject
137{
138@private
139 Vector v;
140}
141- (id) initWithVector:(Vector)vect;
142- (Vector) getVector;
143
144@end
145
146#endif
147
148#if OOMATHS_OPENGL_INTEGRATION
149/* OpenGL conveniences. Need to be macros to work with OOMacroOpenGL. */
150#define GLVertexOOVector(v) do { Vector v_ = v; glVertex3f(v_.x, v_.y, v_.z); } while (0)
151#define GLTranslateOOVector(v) do { Vector v_ = v; OOGL(glTranslatef(v_.x, v_.y, v_.z)); } while (0)
152#endif
153
154
155/*** Only inline definitions beyond this point ***/
156
157OOINLINE Vector make_vector (OOScalar vx, OOScalar vy, OOScalar vz)
158{
159 Vector result;
160 result.x = vx;
161 result.y = vy;
162 result.z = vz;
163 return result;
164}
165
166
167OOINLINE Vector2D MakeVector2D(OOScalar vx, OOScalar vy)
168{
169 Vector2D result;
170 result.x = vx;
171 result.y = vy;
172 return result;
173}
174
175
176OOINLINE void scale_vector(Vector *vec, OOScalar factor)
177{
178 /*
179 Clang static analyzer: reports an unintialized value here when called
180 from -[HeadUpDisplay rescaleByFactor:]. This is blatantly wrong, as
181 the array the vector comes from is fully initialized in the range being
182 looped over.
183 -- Ahruman 2012-09-14
184 */
185 vec->x *= factor;
186 vec->y *= factor;
187 vec->z *= factor;
188}
189
190
191OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s)
192{
193 /*
194 Clang static analyzer: reports a garbage value here when called from
195 -[OOMesh rescaleByFactor:], apparently on baseless assumption that
196 OOMesh._vertices points to only one vertex.
197 -- Ahruman 2012-09-14
198 */
199 Vector r;
200 r.x = v.x * s;
201 r.y = v.y * s;
202 r.z = v.z * s;
203 return r;
204}
205
206
207OOINLINE Vector vector_add(Vector a, Vector b)
208{
209 Vector r;
210 r.x = a.x + b.x;
211 r.y = a.y + b.y;
212 r.z = a.z + b.z;
213 return r;
214}
215
216
217OOINLINE Vector OOVectorInterpolate(Vector a, Vector b, OOScalar where)
218{
219 return make_vector(OOLerp(a.x, b.x, where),
220 OOLerp(a.y, b.y, where),
221 OOLerp(a.z, b.z, where));
222}
223
224
225OOINLINE Vector OOVectorTowards(Vector a, Vector b, OOScalar where)
226{
227 return make_vector(a.x + b.x * where,
228 a.y + b.y * where,
229 a.z + b.z * where);
230}
231
232
233OOINLINE Vector vector_subtract(Vector a, Vector b)
234{
235 Vector r;
236 r.x = a.x - b.x;
237 r.y = a.y - b.y;
238 r.z = a.z - b.z;
239 return r;
240}
241
242
243OOINLINE Vector vector_flip(Vector v)
244{
245 return vector_subtract(kZeroVector, v);
246}
247
248
249OOINLINE bool vector_equal(Vector a, Vector b)
250{
251 return a.x == b.x && a.y == b.y && a.z == b.z;
252}
253
254
255OOINLINE OOScalar magnitude2(Vector vec)
256{
257 return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z;
258}
259
260
261OOINLINE OOScalar magnitude(Vector vec)
262{
263 return sqrt(magnitude2(vec));
264}
265
266
267OOINLINE Vector vector_normal_or_fallback(Vector vec, Vector fallback)
268{
269 OOScalar mag2 = magnitude2(vec);
270 if (EXPECT_NOT(mag2 == 0.0f)) return fallback;
271 return vector_multiply_scalar(vec, 1.0f / sqrt(mag2));
272}
273
274
275OOINLINE Vector vector_normal_or_xbasis(Vector vec)
276{
277 return vector_normal_or_fallback(vec, kBasisXVector);
278}
279
280
281OOINLINE Vector vector_normal_or_ybasis(Vector vec)
282{
283 return vector_normal_or_fallback(vec, kBasisYVector);
284}
285
286
287OOINLINE Vector vector_normal_or_zbasis(Vector vec)
288{
289 return vector_normal_or_fallback(vec, kBasisZVector);
290}
291
292
293OOINLINE Vector vector_normal(Vector vec)
294{
295 return vector_normal_or_fallback(vec, kZeroVector);
296}
297
298
299OOINLINE OOScalar distance2(Vector v1, Vector v2)
300{
301 return magnitude2(vector_subtract(v1, v2));
302}
303
304
305OOINLINE OOScalar distance(Vector v1, Vector v2)
306{
307 return magnitude(vector_subtract(v1, v2));
308}
309
310
311OOINLINE Vector true_cross_product(Vector first, Vector second)
312{
313 Vector result;
314 result.x = (first.y * second.z) - (first.z * second.y);
315 result.y = (first.z * second.x) - (first.x * second.z);
316 result.z = (first.x * second.y) - (first.y * second.x);
317 return result;
318}
319
320
321OOINLINE Vector cross_product(Vector first, Vector second)
322{
323 return vector_normal(true_cross_product(first, second));
324}
325
326
327OOINLINE OOScalar dot_product (Vector a, Vector b)
328{
329 return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
330}
331
332
333OOINLINE OOScalar triple_product(Vector first, Vector second, Vector third)
334{
335 return dot_product(first, true_cross_product(second, third));
336}
337
338
339OOINLINE Vector normal_to_surface(Vector v1, Vector v2, Vector v3)
340{
341 Vector d0, d1;
342 d0 = vector_subtract(v2, v1);
343 d1 = vector_subtract(v3, v2);
344 return cross_product(d0, d1);
345}
346
347
348#endif /* INCLUDED_OOMATHS_h */
#define EXPECT_NOT(x)
#define ALWAYS_INLINE_FUNC
#define NONNULL_FUNC
#define INLINE_CONST_FUNC
#define OOINLINE
#define CONST_FUNC
GLfloat OOScalar
Definition OOMaths.h:64
float y
float x
const Vector2D kBasisYVector2D
Definition OOVector.m:35
const Vector kZeroVector
Definition OOVector.m:28
const Vector2D kZeroVector2D
Definition OOVector.m:33
const Vector kBasisYVector
Definition OOVector.m:30
Vector OORandomUnitVector(void)
Definition OOVector.m:83
const Vector2D kBasisXVector2D
Definition OOVector.m:34
const Vector kBasisZVector
Definition OOVector.m:31
const Vector kBasisXVector
Definition OOVector.m:29
Vector OOVectorRandomRadial(OOScalar maxLength)
Definition OOVector.m:115
Vector OOVectorRandomSpatial(OOScalar maxLength)
Definition OOVector.m:99