Oolite
Loading...
Searching...
No Matches
OOHPVector.h
Go to the documentation of this file.
1/*
2
3OOHPVector.h
4
5Mathematical framework for Oolite.
6High-precision vectors for world-space coordinates
7
8Oolite
9Copyright (C) 2004-2013 Giles C Williams and contributors
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24MA 02110-1301, USA.
25
26*/
27
28#pragma once
29
30#include "OOFastArithmetic.h"
31#include "OOVector.h"
32
33#ifndef OOMATHS_EXTERNAL_VECTOR_TYPES
34
40
45
46#endif
47
48extern const HPVector kZeroHPVector, /* 0, 0, 0 */
49 kBasisXHPVector, /* 1, 0, 0 */
50 kBasisYHPVector, /* 0, 1, 0 */
51 kBasisZHPVector; /* 0, 0, 1 */
52
53extern const HPVector2D kZeroHPVector2D, /* 0, 0 */
56
57/* Construct vector */
60
63
64/* Generate random vectors. */
66HPVector OOHPVectorRandomSpatial(OOHPScalar maxLength); // Random vector uniformly distributed in radius-maxLength sphere. (Longer vectors are more common.)
67HPVector OOHPVectorRandomRadial(OOHPScalar maxLength); // Random vector with uniform distribution of direction and radius in radius-maxLength sphere. (Causes clustering at centre.)
68HPVector OORandomPositionInCylinder(HPVector centre1, OOHPScalar exclusion1, HPVector centre2, OOHPScalar exclusion2, OOHPScalar radius);
70/* returns the projection of 'point' to the plane defined by the point
71 'plane' and the normal vector 'normal' */
73
74/* Multiply vector by scalar (in place) */
76
77/* Multiply vector by scalar */
79
80/* Addition and subtraction of vectors */
83#define HPvector_between(a, b) HPvector_subtract(b, a)
85
86/* HPVector linear interpolation */
89
90/* Comparison of vectors */
92
93/* Square of magnitude of vector */
95
96/* Magnitude of vector */
98
99/* Normalize vector */
101
102/* Normalize vector, returning fallback if zero vector. */
107
108/* Square of distance between vectors */
110
111/* Distance between vectors */
113
114/* Dot product */
116
117/* NORMALIZED cross product */
119
120/* General cross product */
122
123/* Triple product */
125
126/* Given three points on a surface, returns the normal to the surface. */
128
129#if __OBJC__
130NSString* HPVectorDescription(HPVector vector); // @"(x, y, z)"
131NSArray* ArrayFromHPVector(HPVector vector);
132
133#endif
134
135/* OpenGL conveniences. Need to be macros to work with OOMacroOpenGL. */
136#define GLVertexOOHPVector(v) \
137 do { \
138 HPVector v_ = v; \
139 glVertex3f(v_.x, v_.y, v_.z); \
140 } while (0)
141#define GLTranslateOOHPVector(v) \
142 do { \
143 HPVector 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 HPVector result;
152 result.x = vx;
153 result.y = vy;
154 result.z = vz;
155 return result;
156}
157
159{
160 HPVector2D result;
161 result.x = vx;
162 result.y = vy;
163 return result;
164}
165
167{
168 HPVector result;
169 result.x = (OOHPScalar)v.x;
170 result.y = (OOHPScalar)v.y;
171 result.z = (OOHPScalar)v.z;
172 return result;
173}
174
176{
177 Vector result;
178 result.x = (OOScalar)v.x;
179 result.y = (OOScalar)v.y;
180 result.z = (OOScalar)v.z;
181 return result;
182}
183
185{
186 /*
187 Clang static analyzer: reports an unintialized value here when called
188 from -[HeadUpDisplay rescaleByFactor:]. This is blatantly wrong, as
189 the array the vector comes from is fully initialized in the range being
190 looped over.
191 -- Ahruman 2012-09-14
192 */
193 vec->x *= factor;
194 vec->y *= factor;
195 vec->z *= factor;
196}
197
199{
200 /*
201 Clang static analyzer: reports a garbage value here when called from
202 -[OOMesh rescaleByFactor:], apparently on baseless assumption that
203 OOMesh._vertices points to only one vertex.
204 -- Ahruman 2012-09-14
205 */
206 HPVector r;
207 r.x = v.x * s;
208 r.y = v.y * s;
209 r.z = v.z * s;
210 return r;
211}
212
214{
215 HPVector r;
216 r.x = a.x + b.x;
217 r.y = a.y + b.y;
218 r.z = a.z + b.z;
219 return r;
220}
221
223{
224 return make_HPvector(OOLerpd(a.x, b.x, where),
225 OOLerpd(a.y, b.y, where),
226 OOLerpd(a.z, b.z, where));
227}
228
230{
231 return make_HPvector(a.x + b.x * where,
232 a.y + b.y * where,
233 a.z + b.z * where);
234}
235
237{
238 HPVector r;
239 r.x = a.x - b.x;
240 r.y = a.y - b.y;
241 r.z = a.z - b.z;
242 return r;
243}
244
249
251{
252 return a.x == b.x && a.y == b.y && a.z == b.z;
253}
254
256{
257 return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z;
258}
259
261{
262 return sqrt(HPmagnitude2(vec));
263}
264
266{
267 OOHPScalar mag2 = HPmagnitude2(vec);
268 if (EXPECT_NOT(mag2 == 0.0))
269 return fallback;
270 return HPvector_multiply_scalar(vec, 1.0 / sqrt(mag2));
271}
272
277
282
287
292
297
302
304{
305 HPVector result;
306 result.x = (first.y * second.z) - (first.z * second.y);
307 result.y = (first.z * second.x) - (first.x * second.z);
308 result.z = (first.x * second.y) - (first.y * second.x);
309 return result;
310}
311
313{
314 return HPvector_normal(HPtrue_cross_product(first, second));
315}
316
318{
319 return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
320}
321
323{
324 return HPdot_product(first, HPtrue_cross_product(second, third));
325}
326
328{
329 HPVector d0, d1;
330 d0 = HPvector_subtract(v2, v1);
331 d1 = HPvector_subtract(v3, v2);
332 return HPcross_product(d0, d1);
333}
OOINLINE double OOLerpd(double v0, double v1, double fraction) INLINE_CONST_FUNC
#define EXPECT_NOT(x)
#define ALWAYS_INLINE_FUNC
#define NONNULL_FUNC
#define INLINE_CONST_FUNC
#define OOINLINE
#define CONST_FUNC
const HPVector kBasisXHPVector
Definition OOHPVector.h:49
HPVector OOHPVectorRandomRadial(OOHPScalar maxLength)
Definition OOHPVector.m:88
HPVector OOProjectHPVectorToPlane(HPVector point, HPVector plane, HPVector normal)
Definition OOHPVector.m:126
OOINLINE HPVector HPvector_flip(HPVector v) INLINE_CONST_FUNC
Definition OOHPVector.h:245
HPVector OORandomPositionInShell(HPVector centre, OOHPScalar inner, OOHPScalar outer)
Definition OOHPVector.m:116
OOINLINE HPVector HPvector_normal_or_ybasis(HPVector vec) INLINE_CONST_FUNC
Definition OOHPVector.h:278
const HPVector2D kBasisYHPVector2D
Definition OOHPVector.h:55
OOINLINE HPVector vectorToHPVector(Vector v) INLINE_CONST_FUNC
Definition OOHPVector.h:166
OOINLINE OOHPScalar HPdot_product(HPVector first, HPVector second) INLINE_CONST_FUNC
Definition OOHPVector.h:317
const HPVector kBasisYHPVector
Definition OOHPVector.h:50
OOINLINE HPVector HPvector_subtract(HPVector a, HPVector b) INLINE_CONST_FUNC
Definition OOHPVector.h:236
OOINLINE HPVector HPvector_multiply_scalar(HPVector v, OOHPScalar s) INLINE_CONST_FUNC
Definition OOHPVector.h:198
OOINLINE HPVector HPcross_product(HPVector first, HPVector second) INLINE_CONST_FUNC
Definition OOHPVector.h:312
const HPVector kZeroHPVector
Definition OOHPVector.m:27
OOINLINE HPVector HPnormal_to_surface(HPVector v1, HPVector v2, HPVector v3) CONST_FUNC
Definition OOHPVector.h:327
OOINLINE void HPscale_vector(HPVector *outHPVector, OOHPScalar factor) ALWAYS_INLINE_FUNC NONNULL_FUNC
Definition OOHPVector.h:184
OOINLINE bool HPvector_equal(HPVector a, HPVector b) INLINE_CONST_FUNC
Definition OOHPVector.h:250
OOINLINE HPVector HPvector_normal_or_fallback(HPVector vec, HPVector fallback) INLINE_CONST_FUNC
Definition OOHPVector.h:265
OOINLINE OOHPScalar HPmagnitude(HPVector vec) INLINE_CONST_FUNC
Definition OOHPVector.h:260
OOINLINE Vector HPVectorToVector(HPVector v) INLINE_CONST_FUNC
Definition OOHPVector.h:175
OOINLINE HPVector HPvector_normal_or_xbasis(HPVector vec) INLINE_CONST_FUNC
Definition OOHPVector.h:273
OOINLINE HPVector make_HPvector(OOHPScalar vx, OOHPScalar vy, OOHPScalar vz) INLINE_CONST_FUNC
Definition OOHPVector.h:149
HPVector OORandomUnitHPVector(void)
Definition OOHPVector.m:62
OOINLINE OOHPScalar HPdistance2(HPVector v1, HPVector v2) INLINE_CONST_FUNC
Definition OOHPVector.h:293
OOINLINE HPVector HPvector_normal(HPVector vec) INLINE_CONST_FUNC
Definition OOHPVector.h:288
OOINLINE HPVector OOHPVectorTowards(HPVector a, HPVector b, OOHPScalar where) INLINE_CONST_FUNC
Definition OOHPVector.h:229
OOINLINE HPVector HPvector_normal_or_zbasis(HPVector vec) INLINE_CONST_FUNC
Definition OOHPVector.h:283
const HPVector2D kBasisXHPVector2D
Definition OOHPVector.h:54
OOINLINE HPVector HPvector_add(HPVector a, HPVector b) INLINE_CONST_FUNC
Definition OOHPVector.h:213
OOINLINE HPVector HPtrue_cross_product(HPVector first, HPVector second) CONST_FUNC
Definition OOHPVector.h:303
HPVector OOHPVectorRandomSpatial(OOHPScalar maxLength)
Definition OOHPVector.m:75
OOINLINE OOHPScalar HPtriple_product(HPVector first, HPVector second, HPVector third) INLINE_CONST_FUNC
Definition OOHPVector.h:322
const HPVector2D kZeroHPVector2D
Definition OOHPVector.m:32
const HPVector kBasisZHPVector
Definition OOHPVector.h:51
OOINLINE OOHPScalar HPmagnitude2(HPVector vec) INLINE_CONST_FUNC
Definition OOHPVector.h:255
OOINLINE HPVector2D MakeHPVector2D(OOHPScalar vx, OOHPScalar vy) INLINE_CONST_FUNC
Definition OOHPVector.h:158
OOINLINE OOHPScalar HPdistance(HPVector v1, HPVector v2) INLINE_CONST_FUNC
Definition OOHPVector.h:298
OOINLINE HPVector OOHPVectorInterpolate(HPVector a, HPVector b, OOHPScalar where) INLINE_CONST_FUNC
Definition OOHPVector.h:222
HPVector OORandomPositionInCylinder(HPVector centre1, OOHPScalar exclusion1, HPVector centre2, OOHPScalar exclusion2, OOHPScalar radius)
Definition OOHPVector.m:102
double OOHPScalar
Definition OOMaths.h:51
GLfloat OOScalar
Definition OOMaths.h:49
OOHPScalar y
Definition OOHPVector.h:43
OOHPScalar x
Definition OOHPVector.h:42
OOHPScalar x
Definition OOHPVector.h:36
OOHPScalar y
Definition OOHPVector.h:37
OOHPScalar z
Definition OOHPVector.h:38
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34