Oolite 1.91.0.7604-240417-a536cbe
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
27
28const Vector kZeroVector = { 0.0f, 0.0f, 0.0f };
29const Vector kBasisXVector = { 1.0f, 0.0f, 0.0f };
30const Vector kBasisYVector = { 0.0f, 1.0f, 0.0f };
31const Vector kBasisZVector = { 0.0f, 0.0f, 1.0f };
32
33const Vector2D kZeroVector2D = { 0.0f, 0.0f };
34const Vector2D kBasisXVector2D = { 1.0f, 0.0f };
35const Vector2D kBasisYVector2D = { 0.0f, 1.0f };
36
37#if !OOMATHS_STANDALONE
38const BoundingBox kZeroBoundingBox = {{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }};
39#endif
40
41
42#if __OBJC__
43NSString *VectorDescription(Vector vector)
44{
45 return [NSString stringWithFormat:@"(%g, %g, %g)", vector.x, vector.y, vector.z];
46}
47
48@implementation OONativeVector
49
50- (id) initWithVector:(Vector)vect
51{
52 self = [super init];
53 if (EXPECT_NOT(self == nil)) return nil;
54
55 v = vect;
56
57 return self;
58}
59
60- (Vector) getVector
61{
62 return v;
63}
64
65
66@end
67
68#endif
69
70
71
72#if !OOMATHS_STANDALONE
73/* This generates random vectors distrubuted evenly over the surface of the
74 unit sphere. It does this the simple way, by generating vectors in the
75 half-unit cube and rejecting those outside the half-unit sphere (and the
76 zero vector), then normalizing the result. (Half-unit measures are used
77 to avoid unnecessary multiplications of randf() values.)
78
79 In principle, using three normally-distributed co-ordinates (and again
80 normalizing the result) would provide the right result without looping, but
81 I don't trust bellf() so I'll go with the simple approach for now.
82*/
84{
85 Vector v;
86 float m;
87
88 do
89 {
90 v = make_vector(randf() - 0.5f, randf() - 0.5f, randf() - 0.5f);
91 m = magnitude2(v);
92 }
93 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.
94
95 return vector_normal(v);
96}
97
98
100{
101 Vector v;
102 float m;
103
104 do
105 {
106 v = make_vector(randf() - 0.5f, randf() - 0.5f, randf() - 0.5f);
107 m = magnitude2(v);
108 }
109 while (m > 0.25f); // We're confining to a sphere of radius 0.5 using the sqared magnitude; 0.5 squared is 0.25.
110
111 return vector_multiply_scalar(v, maxLength * 2.0f); // 2.0 is to compensate for the 0.5-radius sphere.
112}
113
114
116{
117 return vector_multiply_scalar(OORandomUnitVector(), randf() * maxLength);
118}
119
120
121Vector OORandomPositionInBoundingBox(BoundingBox bb)
122{
123 Vector result;
124 result.x = bb.min.x + randf() * (bb.max.x - bb.min.x);
125 result.y = bb.min.y + randf() * (bb.max.y - bb.min.y);
126 result.z = bb.min.z + randf() * (bb.max.z - bb.min.z);
127 return result;
128}
129
130// only need high precision versions of these
131/*Vector OORandomPositionInCylinder(Vector centre1, OOScalar exclusion1, Vector centre2, OOScalar exclusion2, OOScalar radius)
132{
133 OOScalar exc12 = exclusion1*exclusion1;
134 OOScalar exc22 = exclusion2*exclusion2;
135 Vector result;
136 do
137 {
138 result = vector_add(OOVectorInterpolate(centre1,centre2,randf()),OOVectorRandomSpatial(radius));
139 }
140 while(distance2(result,centre1)<exc12 || distance2(result,centre2)<exc22);
141 return result;
142}
143
144Vector OORandomPositionInShell(Vector centre, OOScalar inner, OOScalar outer)
145{
146 Vector result;
147 OOScalar inner2 = inner*inner;
148 do
149 {
150 result = vector_add(centre,OOVectorRandomSpatial(outer));
151 } while(distance2(result,centre)<inner2);
152 return result;
153 }*/
154
155#endif
#define EXPECT_NOT(x)
GLfloat OOScalar
Definition OOMaths.h:64
return nil
const Vector2D kBasisYVector2D
Definition OOVector.m:35
const BoundingBox kZeroBoundingBox
Definition OOVector.m:38
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
Vector OORandomPositionInBoundingBox(BoundingBox bb)
Definition OOVector.m:121
const Vector kBasisXVector
Definition OOVector.m:29
Vector OOVectorRandomRadial(OOScalar maxLength)
Definition OOVector.m:115
Vector OOVectorRandomSpatial(OOScalar maxLength)
Definition OOVector.m:99
float randf(void)