Line data Source code
1 0 : /*
2 :
3 : OOVector.m
4 :
5 : Oolite
6 : Copyright (C) 2004-2013 Giles C Williams and contributors
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License
10 : as published by the Free Software Foundation; either version 2
11 : of the License, or (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 : MA 02110-1301, USA.
22 :
23 : */
24 :
25 : #include "OOMaths.h"
26 :
27 :
28 0 : const Vector kZeroVector = { 0.0f, 0.0f, 0.0f };
29 0 : const Vector kBasisXVector = { 1.0f, 0.0f, 0.0f };
30 0 : const Vector kBasisYVector = { 0.0f, 1.0f, 0.0f };
31 0 : const Vector kBasisZVector = { 0.0f, 0.0f, 1.0f };
32 :
33 0 : const Vector2D kZeroVector2D = { 0.0f, 0.0f };
34 0 : const Vector2D kBasisXVector2D = { 1.0f, 0.0f };
35 0 : const Vector2D kBasisYVector2D = { 0.0f, 1.0f };
36 :
37 : #if !OOMATHS_STANDALONE
38 0 : const BoundingBox kZeroBoundingBox = {{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }};
39 : #endif
40 :
41 :
42 : #if __OBJC__
43 : NSString *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 : */
83 0 : Vector OORandomUnitVector(void)
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 :
99 0 : Vector OOVectorRandomSpatial(OOScalar maxLength)
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 :
115 0 : Vector OOVectorRandomRadial(OOScalar maxLength)
116 : {
117 : return vector_multiply_scalar(OORandomUnitVector(), randf() * maxLength);
118 : }
119 :
120 :
121 0 : Vector 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 :
144 : Vector 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
|