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