Oolite
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#pragma once
28
29#ifndef OOMATHS_EXTERNAL_VECTOR_TYPES
30
36
41
42#endif
43
44extern const Vector kZeroVector, /* 0, 0, 0 */
45 kBasisXVector, /* 1, 0, 0 */
46 kBasisYVector, /* 0, 1, 0 */
47 kBasisZVector; /* 0, 0, 1 */
48
49extern const Vector2D kZeroVector2D, /* 0, 0 */
50 kBasisXVector2D, /* 1, 0 */
51 kBasisYVector2D; /* 0, 1 */
52
53/* Construct vector */
56
57/* Generate random vectors. */
59Vector OOVectorRandomSpatial(OOScalar maxLength); // Random vector uniformly distributed in radius-maxLength sphere. (Longer vectors are more common.)
60Vector OOVectorRandomRadial(OOScalar maxLength); // Random vector with uniform distribution of direction and radius in radius-maxLength sphere. (Causes clustering at centre.)
61// only needed in high-precision forms so far
62// Vector OORandomPositionInCylinder(Vector centre1, OOScalar exclusion1, Vector centre2, OOScalar exclusion2, OOScalar radius);
63// Vector OORandomPositionInShell(Vector centre, OOScalar inner, OOScalar outer);
64
65/* Multiply vector by scalar (in place) */
67
68/* Multiply vector by scalar */
70
71/* Addition and subtraction of vectors */
74#define vector_between(a, b) vector_subtract(b, a)
76
77/* Vector linear interpolation */
80
81/* Comparison of vectors */
83
84/* Square of magnitude of vector */
86
87/* Magnitude of vector */
89
90/* Normalize vector */
92
93/* Normalize vector, returning fallback if zero vector. */
98
99/* Square of distance between vectors */
101
102/* Distance between vectors */
104
105/* Dot product */
107
108/* NORMALIZED cross product */
110
111/* General cross product */
113
114/* Triple product */
116
117/* Given three points on a surface, returns the normal to the surface. */
119
120#if __OBJC__
121NSString* VectorDescription(Vector vector); // @"(x, y, z)"
122
123/* For storing vectors in NSArrays */
124@interface OONativeVector : NSObject {
125@private
126 Vector v;
127}
128- (id)initWithVector:(Vector)vect;
129- (Vector)getVector;
130
131@end
132
133#endif
134
135/* OpenGL conveniences. Need to be macros to work with OOMacroOpenGL. */
136#define GLVertexOOVector(v) \
137 do { \
138 Vector v_ = v; \
139 glVertex3f(v_.x, v_.y, v_.z); \
140 } while (0)
141#define GLTranslateOOVector(v) \
142 do { \
143 Vector v_ = v; \
144 OOGL(glTranslatef(v_.x, v_.y, v_.z)); \
145 } while (0)
146
147/*** Only inline definitions beyond this point ***/
148
150{
151 Vector result;
152 result.x = vx;
153 result.y = vy;
154 result.z = vz;
155 return result;
156}
157
159{
160 Vector2D result;
161 result.x = vx;
162 result.y = vy;
163 return result;
164}
165
167{
168 /*
169 Clang static analyzer: reports an unintialized value here when called
170 from -[HeadUpDisplay rescaleByFactor:]. This is blatantly wrong, as
171 the array the vector comes from is fully initialized in the range being
172 looped over.
173 -- Ahruman 2012-09-14
174 */
175 vec->x *= factor;
176 vec->y *= factor;
177 vec->z *= factor;
178}
179
181{
182 /*
183 Clang static analyzer: reports a garbage value here when called from
184 -[OOMesh rescaleByFactor:], apparently on baseless assumption that
185 OOMesh._vertices points to only one vertex.
186 -- Ahruman 2012-09-14
187 */
188 Vector r;
189 r.x = v.x * s;
190 r.y = v.y * s;
191 r.z = v.z * s;
192 return r;
193}
194
196{
197 Vector r;
198 r.x = a.x + b.x;
199 r.y = a.y + b.y;
200 r.z = a.z + b.z;
201 return r;
202}
203
205{
206 return make_vector(OOLerp(a.x, b.x, where),
207 OOLerp(a.y, b.y, where),
208 OOLerp(a.z, b.z, where));
209}
210
212{
213 return make_vector(a.x + b.x * where,
214 a.y + b.y * where,
215 a.z + b.z * where);
216}
217
219{
220 Vector r;
221 r.x = a.x - b.x;
222 r.y = a.y - b.y;
223 r.z = a.z - b.z;
224 return r;
225}
226
231
233{
234 return a.x == b.x && a.y == b.y && a.z == b.z;
235}
236
238{
239 return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z;
240}
241
243{
244 return sqrt(magnitude2(vec));
245}
246
248{
249 OOScalar mag2 = magnitude2(vec);
250 if (EXPECT_NOT(mag2 == 0.0f))
251 return fallback;
252 return vector_multiply_scalar(vec, 1.0f / sqrt(mag2));
253}
254
259
264
269
274
276{
277 return magnitude2(vector_subtract(v1, v2));
278}
279
281{
282 return magnitude(vector_subtract(v1, v2));
283}
284
286{
287 Vector result;
288 result.x = (first.y * second.z) - (first.z * second.y);
289 result.y = (first.z * second.x) - (first.x * second.z);
290 result.z = (first.x * second.y) - (first.y * second.x);
291 return result;
292}
293
295{
296 return vector_normal(true_cross_product(first, second));
297}
298
300{
301 return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
302}
303
305{
306 return dot_product(first, true_cross_product(second, third));
307}
308
310{
311 Vector d0, d1;
312 d0 = vector_subtract(v2, v1);
313 d1 = vector_subtract(v3, v2);
314 return cross_product(d0, d1);
315}
OOINLINE float OOLerp(float v0, float v1, float fraction) INLINE_CONST_FUNC
#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:49
OOINLINE Vector vector_flip(Vector v) INLINE_CONST_FUNC
Definition OOVector.h:227
OOINLINE Vector OOVectorTowards(Vector a, Vector b, OOScalar where) INLINE_CONST_FUNC
Definition OOVector.h:211
OOINLINE Vector vector_add(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:195
OOINLINE OOScalar triple_product(Vector first, Vector second, Vector third) INLINE_CONST_FUNC
Definition OOVector.h:304
OOINLINE Vector vector_subtract(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:218
OOINLINE OOScalar magnitude(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:242
OOINLINE OOScalar distance2(Vector v1, Vector v2) INLINE_CONST_FUNC
Definition OOVector.h:275
const Vector2D kBasisYVector2D
Definition OOVector.h:51
OOINLINE Vector vector_normal_or_zbasis(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:265
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
OOINLINE OOScalar dot_product(Vector first, Vector second) INLINE_CONST_FUNC
Definition OOVector.h:299
OOINLINE Vector vector_normal_or_fallback(Vector vec, Vector fallback) INLINE_CONST_FUNC
Definition OOVector.h:247
OOINLINE Vector true_cross_product(Vector first, Vector second) CONST_FUNC
Definition OOVector.h:285
const Vector kZeroVector
Definition OOVector.m:27
OOINLINE Vector normal_to_surface(Vector v1, Vector v2, Vector v3) CONST_FUNC
Definition OOVector.h:309
OOINLINE bool vector_equal(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:232
const Vector2D kZeroVector2D
Definition OOVector.m:32
OOINLINE Vector OOVectorInterpolate(Vector a, Vector b, OOScalar where) INLINE_CONST_FUNC
Definition OOVector.h:204
OOINLINE Vector2D MakeVector2D(OOScalar vx, OOScalar vy) INLINE_CONST_FUNC
Definition OOVector.h:158
const Vector kBasisYVector
Definition OOVector.h:46
OOINLINE Vector cross_product(Vector first, Vector second) INLINE_CONST_FUNC
Definition OOVector.h:294
OOINLINE void scale_vector(Vector *outVector, OOScalar factor) ALWAYS_INLINE_FUNC NONNULL_FUNC
Definition OOVector.h:166
OOINLINE OOScalar magnitude2(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:237
Vector OORandomUnitVector(void)
Definition OOVector.m:76
const Vector2D kBasisXVector2D
Definition OOVector.h:50
OOINLINE Vector vector_normal_or_ybasis(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:260
const Vector kBasisZVector
Definition OOVector.h:47
const Vector kBasisXVector
Definition OOVector.h:45
OOINLINE Vector vector_normal_or_xbasis(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:255
Vector OOVectorRandomRadial(OOScalar maxLength)
Definition OOVector.m:102
Vector OOVectorRandomSpatial(OOScalar maxLength)
Definition OOVector.m:89
OOINLINE OOScalar distance(Vector v1, Vector v2) INLINE_CONST_FUNC
Definition OOVector.h:280
OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s) INLINE_CONST_FUNC
Definition OOVector.h:180
OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:270
OOScalar y
Definition OOVector.h:39
OOScalar x
Definition OOVector.h:38
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34