Oolite
Loading...
Searching...
No Matches
OOVector.m
Go to the documentation of this file.
1/*
2
3OOVector.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#include "OOMaths.h"
26
27const Vector kZeroVector = { 0.0f, 0.0f, 0.0f };
28const Vector kBasisXVector = { 1.0f, 0.0f, 0.0f };
29const Vector kBasisYVector = { 0.0f, 1.0f, 0.0f };
30const Vector kBasisZVector = { 0.0f, 0.0f, 1.0f };
31
32const Vector2D kZeroVector2D = { 0.0f, 0.0f };
33const Vector2D kBasisXVector2D = { 1.0f, 0.0f };
34const Vector2D kBasisYVector2D = { 0.0f, 1.0f };
35
36const BoundingBox kZeroBoundingBox = { { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f } };
37
38#if __OBJC__
39NSString* VectorDescription(Vector vector)
40{
41 return [NSString stringWithFormat:@"(%g, %g, %g)", vector.x, vector.y, vector.z];
42}
43
44@implementation OONativeVector
45
46- (id)initWithVector:(Vector)vect
47{
48 self = [super init];
49 if (EXPECT_NOT(self == nil))
50 return nil;
51
52 v = vect;
53
54 return self;
55}
56
57- (Vector)getVector
58{
59 return v;
60}
61
62@end
63
64#endif
65
66/* This generates random vectors distrubuted evenly over the surface of the
67 unit sphere. It does this the simple way, by generating vectors in the
68 half-unit cube and rejecting those outside the half-unit sphere (and the
69 zero vector), then normalizing the result. (Half-unit measures are used
70 to avoid unnecessary multiplications of randf() values.)
71
72 In principle, using three normally-distributed co-ordinates (and again
73 normalizing the result) would provide the right result without looping, but
74 I don't trust bellf() so I'll go with the simple approach for now.
75*/
77{
78 Vector v;
79 float m;
80
81 do {
82 v = make_vector(randf() - 0.5f, randf() - 0.5f, randf() - 0.5f);
83 m = magnitude2(v);
84 } while (m > 0.25f || m == 0.0f); // We're confining to a sphere of radius 0.5 using the sqared magnitude; 0.5 squared is 0.25.
85
86 return vector_normal(v);
87}
88
90{
91 Vector v;
92 float m;
93
94 do {
95 v = make_vector(randf() - 0.5f, randf() - 0.5f, randf() - 0.5f);
96 m = magnitude2(v);
97 } while (m > 0.25f); // We're confining to a sphere of radius 0.5 using the sqared magnitude; 0.5 squared is 0.25.
98
99 return vector_multiply_scalar(v, maxLength * 2.0f); // 2.0 is to compensate for the 0.5-radius sphere.
100}
101
103{
104 return vector_multiply_scalar(OORandomUnitVector(), randf() * maxLength);
105}
106
108{
109 Vector result;
110 result.x = bb.min.x + randf() * (bb.max.x - bb.min.x);
111 result.y = bb.min.y + randf() * (bb.max.y - bb.min.y);
112 result.z = bb.min.z + randf() * (bb.max.z - bb.min.z);
113 return result;
114}
115
116// only need high precision versions of these
117/*Vector OORandomPositionInCylinder(Vector centre1, OOScalar exclusion1, Vector centre2, OOScalar exclusion2, OOScalar radius)
118{
119 OOScalar exc12 = exclusion1*exclusion1;
120 OOScalar exc22 = exclusion2*exclusion2;
121 Vector result;
122 do
123 {
124 result = vector_add(OOVectorInterpolate(centre1,centre2,randf()),OOVectorRandomSpatial(radius));
125 }
126 while(distance2(result,centre1)<exc12 || distance2(result,centre2)<exc22);
127 return result;
128}
129
130Vector OORandomPositionInShell(Vector centre, OOScalar inner, OOScalar outer)
131{
132 Vector result;
133 OOScalar inner2 = inner*inner;
134 do
135 {
136 result = vector_add(centre,OOVectorRandomSpatial(outer));
137 } while(distance2(result,centre)<inner2);
138 return result;
139 }*/
const BoundingBox kZeroBoundingBox
Definition OOVector.m:36
#define EXPECT_NOT(x)
GLfloat OOScalar
Definition OOMaths.h:49
return nil
const Vector2D kBasisYVector2D
Definition OOVector.h:51
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
const Vector kZeroVector
Definition OOVector.m:27
const Vector2D kZeroVector2D
Definition OOVector.m:32
const Vector kBasisYVector
Definition OOVector.h:46
OOINLINE OOScalar magnitude2(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:237
const Vector2D kBasisXVector2D
Definition OOVector.h:50
const Vector kBasisZVector
Definition OOVector.h:47
const Vector kBasisXVector
Definition OOVector.h:45
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
Vector OORandomUnitVector(void)
Definition OOVector.m:76
Vector OORandomPositionInBoundingBox(BoundingBox bb)
Definition OOVector.m:107
Vector OOVectorRandomRadial(OOScalar maxLength)
Definition OOVector.m:102
Vector OOVectorRandomSpatial(OOScalar maxLength)
Definition OOVector.m:89
float randf(void)
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34