Oolite
Loading...
Searching...
No Matches
OOHPVector.m
Go to the documentation of this file.
1/*
2
3OOHPVector.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 HPVector kZeroHPVector = { 0.0, 0.0, 0.0 };
28const HPVector kBasisXHPVector = { 1.0, 0.0, 0.0 };
29const HPVector kBasisYHPVector = { 0.0, 1.0, 0.0 };
30const HPVector kBasisZHPVector = { 0.0, 0.0, 1.0 };
31
32const HPVector2D kZeroHPVector2D = { 0.0, 0.0 };
33const HPVector2D kBasisXHPVector2D = { 1.0, 0.0 };
34const HPVector2D kBasisYHPVector2D = { 0.0, 1.0 };
35
36#if __OBJC__
37NSString* HPVectorDescription(HPVector vector)
38{
39 return [NSString stringWithFormat:@"(%g, %g, %g)", vector.x, vector.y, vector.z];
40}
41
42NSArray* ArrayFromHPVector(HPVector vec)
43{
44 return [NSArray arrayWithObjects:[NSNumber numberWithDouble:vec.x],
45 [NSNumber numberWithDouble:vec.y],
46 [NSNumber numberWithDouble:vec.z],
47 nil];
48}
49
50#endif
51
52/* This generates random vectors distrubuted evenly over the surface of the
53 unit sphere. It does this the simple way, by generating vectors in the
54 half-unit cube and rejecting those outside the half-unit sphere (and the
55 zero vector), then normalizing the result. (Half-unit measures are used
56 to avoid unnecessary multiplications of randf() values.)
57
58 In principle, using three normally-distributed co-ordinates (and again
59 normalizing the result) would provide the right result without looping, but
60 I don't trust bellf() so I'll go with the simple approach for now.
61*/
63{
64 HPVector v;
65 OOHPScalar m;
66
67 do {
68 v = make_HPvector(randf() - 0.5f, randf() - 0.5f, randf() - 0.5f);
69 m = HPmagnitude2(v);
70 } while (m > 0.25 || m == 0.0); // We're confining to a sphere of radius 0.5 using the sqared magnitude; 0.5 squared is 0.25.
71
72 return HPvector_normal(v);
73}
74
76{
77 HPVector v;
78 OOHPScalar m;
79
80 do {
81 v = make_HPvector(randf() - 0.5f, randf() - 0.5f, randf() - 0.5f);
82 m = HPmagnitude2(v);
83 } while (m > 0.25); // We're confining to a sphere of radius 0.5 using the sqared magnitude; 0.5 squared is 0.25.
84
85 return HPvector_multiply_scalar(v, maxLength * 2.0); // 2.0 is to compensate for the 0.5-radius sphere.
86}
87
92
94{
95 HPVector result;
96 result.x = (OOHPScalar)(bb.min.x + randf() * (bb.max.x - bb.min.x));
97 result.y = (OOHPScalar)(bb.min.y + randf() * (bb.max.y - bb.min.y));
98 result.z = (OOHPScalar)(bb.min.z + randf() * (bb.max.z - bb.min.z));
99 return result;
100}
101
102HPVector OORandomPositionInCylinder(HPVector centre1, OOHPScalar exclusion1, HPVector centre2, OOHPScalar exclusion2, OOHPScalar radius)
103{
104 OOHPScalar exc12 = exclusion1 * exclusion1;
105 OOHPScalar exc22 = exclusion2 * exclusion2;
106 if (HPdistance(centre1, centre2) < (exclusion1 + exclusion2) * 1.2) {
107 OOLog(@"position.cylinder.error", @"Trying to generate cylinder position in range %f long with exclusions %f and %f", HPdistance(centre1, centre2), exclusion1, exclusion2);
108 }
109 HPVector result;
110 do {
111 result = HPvector_add(OOHPVectorInterpolate(centre1, centre2, randf()), OOHPVectorRandomSpatial(radius));
112 } while (HPdistance2(result, centre1) < exc12 || HPdistance2(result, centre2) < exc22);
113 return result;
114}
115
117{
118 HPVector result;
119 OOHPScalar inner2 = inner * inner;
120 do {
121 result = HPvector_add(centre, OOHPVectorRandomSpatial(outer));
122 } while (HPdistance2(result, centre) < inner2);
123 return result;
124}
125
127{
128 return HPvector_subtract(point, HPvector_multiply_scalar(normal, HPdot_product(HPvector_subtract(point, plane), normal)));
129}
const HPVector kBasisXHPVector
Definition OOHPVector.h:49
const HPVector2D kBasisYHPVector2D
Definition OOHPVector.h:55
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
const HPVector kZeroHPVector
Definition OOHPVector.m:27
OOINLINE HPVector make_HPvector(OOHPScalar vx, OOHPScalar vy, OOHPScalar vz) INLINE_CONST_FUNC
Definition OOHPVector.h:149
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
const HPVector2D kBasisXHPVector2D
Definition OOHPVector.h:54
OOINLINE HPVector HPvector_add(HPVector a, HPVector b) INLINE_CONST_FUNC
Definition OOHPVector.h:213
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 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 OOHPVectorRandomRadial(OOHPScalar maxLength)
Definition OOHPVector.m:88
HPVector OOProjectHPVectorToPlane(HPVector point, HPVector plane, HPVector normal)
Definition OOHPVector.m:126
HPVector OORandomPositionInShell(HPVector centre, OOHPScalar inner, OOHPScalar outer)
Definition OOHPVector.m:116
HPVector OOHPRandomPositionInBoundingBox(BoundingBox bb)
Definition OOHPVector.m:93
HPVector OORandomUnitHPVector(void)
Definition OOHPVector.m:62
HPVector OOHPVectorRandomSpatial(OOHPScalar maxLength)
Definition OOHPVector.m:75
HPVector OORandomPositionInCylinder(HPVector centre1, OOHPScalar exclusion1, HPVector centre2, OOHPScalar exclusion2, OOHPScalar radius)
Definition OOHPVector.m:102
#define OOLog(class, format,...)
Definition OOLogging.h:84
double OOHPScalar
Definition OOMaths.h:51
float randf(void)
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