My Project
 All Classes Files Functions Variables Enumerations Pages
xyz.h
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Copyright 2009 Tim Day */
3 /* */
4 /* This file is part of Fracplanet */
5 /* */
6 /* Fracplanet is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* Fracplanet is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with Fracplanet. If not, see <http://www.gnu.org/licenses/>. */
18 /**************************************************************************/
19 
24 #ifndef _xyz_h_
25 #define _xyz_h_
26 
27 #include "random.h"
28 
30 
34 class XYZ
35 {
36  public:
37 
38  float x;
39  float y;
40  float z;
41 
43 
45  XYZ()
46  {}
47 
49  XYZ(const XYZ& v)
50  :x(v.x),y(v.y),z(v.z){}
51 
53  XYZ(float vx,float vy,float vz)
54  :x(vx),y(vy),z(vz){}
55 
57  ~XYZ()
58  {}
59 
60  typedef float XYZ::* ElementPtr;
61  static ElementPtr element_table[3];
62 
64  const float& element(uint e) const
65  {
66  return this->*(element_table[e]);
67  }
68 
70  float& element(uint e)
71  {
72  return this->*(element_table[e]);
73  }
74 
76  void operator*=(float k)
77  {
78  x*=k;
79  y*=k;
80  z*=k;
81  }
82 
84 
86  void operator/=(float k)
87  {
88  const float ik(1.0/k);
89  x*=ik;
90  y*=ik;
91  z*=ik;
92  }
93 
95  void operator+=(const XYZ& v)
96  {
97  x+=v.x;
98  y+=v.y;
99  z+=v.z;
100  }
101 
103  void operator-=(const XYZ& v)
104  {
105  x-=v.x;
106  y-=v.y;
107  z-=v.z;
108  }
109 
111  void assign(const XYZ& v)
112  {
113  x=v.x;
114  y=v.y;
115  z=v.z;
116  }
117 
119  const XYZ operator-() const
120  {
121  return XYZ(-x,-y,-z);
122  }
123 
125  float magnitude2() const
126  {
127  return x*x+y*y+z*z;
128  }
129 
131  float magnitude() const
132  {
133  return sqrt(magnitude2());
134  }
135 
137  const XYZ normalised() const;
138 
140  void normalise();
141 
143  std::ostream& write(std::ostream&) const;
144 
146  const std::string format_comma() const;
147 
149  const std::string format_blender() const;
150 
152  const std::string format_pov() const;
153 };
154 
156 inline const XYZ operator*(const XYZ& a,const XYZ& b)
157 {
158  return XYZ(
159  a.y*b.z-a.z*b.y,
160  a.z*b.x-a.x*b.z,
161  a.x*b.y-a.y*b.x
162  );
163 }
164 
166 
168 inline float operator%(const XYZ& a,const XYZ& b)
169 {
170  return a.x*b.x+a.y*b.y+a.z*b.z;
171 }
172 
174 inline const XYZ operator+(const XYZ& a,const XYZ& b)
175 {
176  return XYZ(a.x+b.x,a.y+b.y,a.z+b.z);
177 }
178 
180 inline const XYZ operator-(const XYZ& a,const XYZ& b)
181 {
182  return XYZ(a.x-b.x,a.y-b.y,a.z-b.z);
183 }
184 
186 inline const XYZ operator*(float k,const XYZ& v)
187 {
188  return XYZ(k*v.x,k*v.y,k*v.z);
189 }
190 
192 inline const XYZ operator*(const XYZ& v,float k)
193 {
194  return XYZ(k*v.x,k*v.y,k*v.z);
195 }
196 
198 inline const XYZ operator/(const XYZ& v,float k)
199 {
200  return (1.0/k)*v;
201 }
202 
204 inline bool operator==(const XYZ& a,const XYZ& b)
205 {
206  return (a.x==b.x && a.y==b.y && a.z==b.z);
207 }
208 
210 inline bool operator!=(const XYZ& a,const XYZ& b)
211 {
212  return (a.x!=b.x || a.y!=b.y || a.z!=b.z);
213 }
214 
217 inline const XYZ XYZ::normalised() const
218 {
219  const float m=magnitude();
220  assert(m!=0.0);
221  return (*this)/m;
222 }
223 
226 inline void XYZ::normalise()
227 {
228  (*this)=normalised();
229 }
230 
232 inline std::ostream& operator<<(std::ostream& out,const XYZ& v)
233 {
234  return v.write(out);
235 }
236 
238 class RandomXYZInUnitCube : public XYZ
239 {
240  public:
242 };
243 
245 class RandomXYZInBox : public XYZ
246 {
247  public:
248  RandomXYZInBox(Random01& rng,const XYZ& bounds);
249 };
250 
252 class RandomXYZInSphere : public XYZ
253 {
254  public:
255  RandomXYZInSphere(Random01& rng,float radius);
256 };
257 
259 class RandomXYZInEllipsoid : public XYZ
260 {
261  public:
262  RandomXYZInEllipsoid(Random01& rng,const XYZ& axes);
263 };
264 
267 {
268  public:
270 };
271 
272 #endif
Generates a random point in or on a unit-radius sphere centred on the origin.
Definition: xyz.h:252
Generates a random point in the cube bounded by (0,0,0) and (1.0,1.0,1.0)
Definition: xyz.h:238
const float & element(uint e) const
Access by number.
Definition: xyz.h:64
float & element(uint e)
Access by number.
Definition: xyz.h:70
XYZ(float vx, float vy, float vz)
Initialise from separate components.
Definition: xyz.h:53
float magnitude2() const
Return the square of the magnitude.
Definition: xyz.h:125
Generates random points in a recnangular box centred on the origin.
Definition: xyz.h:245
std::ostream & write(std::ostream &) const
Write the vector (spaces as separators).
Definition: xyz.cpp:28
float operator%(const XYZ &a, const XYZ &b)
Dot product.
Definition: xyz.h:168
std::ostream & operator<<(std::ostream &out, const XYZ &v)
Stream output operator.
Definition: xyz.h:232
bool operator==(const XYZ &a, const XYZ &b)
Equality operator.
Definition: xyz.h:204
const XYZ operator-() const
Negation.
Definition: xyz.h:119
void assign(const XYZ &v)
Assignment.
Definition: xyz.h:111
Class to hold vectors in 3D cartesian co-ordinates.
Definition: xyz.h:34
float magnitude() const
Return the magnitude.
Definition: xyz.h:131
const std::string format_comma() const
Alternate formatting.
Definition: xyz.cpp:33
void normalise()
Normalise this vector.
Definition: xyz.h:226
const XYZ operator-(const XYZ &a, const XYZ &b)
Vector subtraction.
Definition: xyz.h:180
const std::string format_blender() const
Alternate formatting.
Definition: xyz.cpp:40
void operator*=(float k)
Multiply by scalar.
Definition: xyz.h:76
Generates random numbers in the range [0,1).
Definition: random.h:28
RandomXYZInEllipsoid(Random01 &rng, const XYZ &axes)
Definition: xyz.cpp:87
Generates a random point in or on an origin-centred ellipsoid with semi-axes of the specified size...
Definition: xyz.h:259
const XYZ operator+(const XYZ &a, const XYZ &b)
Vector addition.
Definition: xyz.h:174
const XYZ operator*(const XYZ &a, const XYZ &b)
Cross product.
Definition: xyz.h:156
bool operator!=(const XYZ &a, const XYZ &b)
Inequality operator.
Definition: xyz.h:210
Generates a random point on the surface of a unit-radius sphere.
Definition: xyz.h:266
Interface for class Random and derived classes.
XYZ()
Null constructor.
Definition: xyz.h:45
void operator+=(const XYZ &v)
Vector addition.
Definition: xyz.h:95
const XYZ operator/(const XYZ &v, float k)
Division by scalar.
Definition: xyz.h:198
const XYZ normalised() const
Return the vector normalised.
Definition: xyz.h:217
static ElementPtr element_table[3]
Definition: xyz.h:61
const std::string format_pov() const
Alternate formatting.
Definition: xyz.cpp:49
void operator-=(const XYZ &v)
Vector subtraction.
Definition: xyz.h:103
~XYZ()
Destructor.
Definition: xyz.h:57
XYZ(const XYZ &v)
Copy constructor.
Definition: xyz.h:49
void operator/=(float k)
Divide by scalar.
Definition: xyz.h:86