1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/**************************************************************************/
/* Copyright 2012 Tim Day */
/* */
/* This file is part of Evolvotron */
/* */
/* Evolvotron is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* Evolvotron is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with Evolvotron. If not, see <http://www.gnu.org/licenses/>. */
/**************************************************************************/
/*! \file
\brief Interface for class XYZ.
*/
#ifndef _xyz_h_
#define _xyz_h_
#include "common.h"
#include "xy.h"
class Random01;
//! Class to hold vectors in 3D cartesian co-ordinates.
/*! Direct access to the x,y,z members is not permitted.
*/
class XYZ
{
protected:
boost::array<real,3> _rep;
public:
//@{
//! Accessor.
real x() const
{
return _rep[0];
}
real y() const
{
return _rep[1];
}
real z() const
{
return _rep[2];
}
const XY xy() const
{
return XY(x(),y());
}
void x(real v)
{
_rep[0]=v;
}
void y(real v)
{
_rep[1]=v;
}
void z(real v)
{
_rep[2]=v;
}
//@}
//! Null constructor.
/*! NB The components are not cleared to zero.
*/
XYZ()
{}
//! Copy constructor.
XYZ(const XYZ& v)
{
_rep[0]=v._rep[0];
_rep[1]=v._rep[1];
_rep[2]=v._rep[2];
}
//! Initialise from an XY and a z component.
XYZ(const XY& p,real vz)
{
_rep[0]=p.x();
_rep[1]=p.y();
_rep[2]=vz;
}
//! Initialise from separate components.
XYZ(real vx,real vy,real vz)
{
_rep[0]=vx;
_rep[1]=vy;
_rep[2]=vz;
}
//! Trivial destructor.
~XYZ()
{}
//! Subtract a vector
void operator-=(const XYZ& v)
{
_rep[0]-=v._rep[0];
_rep[1]-=v._rep[1];
_rep[2]-=v._rep[2];
}
//! Add a vector
void operator+=(const XYZ& v)
{
_rep[0]+=v._rep[0];
_rep[1]+=v._rep[1];
_rep[2]+=v._rep[2];
}
//! Multiply by scalar
void operator*=(real k)
{
_rep[0]*=k;
_rep[1]*=k;
_rep[2]*=k;
}
//! Divide by scalar.
/*! Implemented assuming one divide and three multiplies is faster than three divides.
*/
void operator/=(real k)
{
const real ik(1.0/k);
(*this)*=ik;
}
//! Assignment.
void assign(const XYZ& v)
{
x(v.x());
y(v.y());
z(v.z());
}
//! Negation.
const XYZ operator-() const
{
return XYZ(-x(),-y(),-z());
}
//! Return the square of the magnitude.
real magnitude2() const
{
return x()*x()+y()*y()+z()*z();
}
//! Return the magnitude.
real magnitude() const
{
return sqrt(magnitude2());
}
//! Returns sum of x, y and z components.
real sum_of_components() const
{
return x()+y()+z();
}
//! Return the vector normalised.
const XYZ normalised() const;
//! Normalise this vector.
void normalise();
//! Returns true if an origin centred cuboid with this vectors semi-axes contains the argument.
bool origin_centred_rect_contains(const XYZ& p) const
{
return (-x()<=p.x() && p.x()<=x() && -y()<=p.y() && p.y()<=y() && -z()<=p.z() && p.z()<=z());
}
//! Write the vector.
std::ostream& write(std::ostream&) const;
//! Helper for common case of creating an instance filled with a common value.
static const XYZ fill(real v)
{
return XYZ(v,v,v);
}
};
//! Cross product.
inline const XYZ operator*(const XYZ& a,const XYZ& b)
{
return XYZ(
a.y()*b.z()-a.z()*b.y(),
a.z()*b.x()-a.x()*b.z(),
a.x()*b.y()-a.y()*b.x()
);
}
//! Dot product.
/*! Perhaps a curious choice of operator but it works for me.
*/
inline real operator%(const XYZ& a,const XYZ& b)
{
return a.x()*b.x()+a.y()*b.y()+a.z()*b.z();
}
//! Vector addition.
inline const XYZ operator+(const XYZ& a,const XYZ& b)
{
return XYZ(a.x()+b.x(),a.y()+b.y(),a.z()+b.z());
}
//! Vector subtraction.
inline const XYZ operator-(const XYZ& a,const XYZ& b)
{
return XYZ(a.x()-b.x(),a.y()-b.y(),a.z()-b.z());
}
//! Multiplication by scalar.
inline const XYZ operator*(real k,const XYZ& v)
{
XYZ ret(v);
ret*=k;
return ret;
}
//! Multiplication by scalar.
inline const XYZ operator*(const XYZ& v,real k)
{
XYZ ret(v);
ret*=k;
return ret;
}
//! Division by scalar.
inline const XYZ operator/(const XYZ& v,real k)
{
return v*(1.0/k);
}
//! Modulus all components by 1.0
inline const XYZ modulusf(const XYZ& p)
{
return XYZ
(
modulusf(p.x(),1.0),
modulusf(p.y(),1.0),
modulusf(p.z(),1.0)
);
}
//! Componentwise modulus
inline const XYZ modulusf(const XYZ& p,const XYZ& q)
{
return XYZ
(
modulusf(p.x(),q.x()),
modulusf(p.y(),q.y()),
modulusf(p.z(),q.z())
);
}
/*! If magnitude is zero we return zero vector.
*/
inline const XYZ XYZ::normalised() const
{
const real m=magnitude();
return (m==0.0 ? XYZ(0.0,0.0,0.0) : (*this)/m);
}
inline void XYZ::normalise()
{
(*this)=normalised();
}
//! Stream output operator.
/*! Calls write().
*/
inline std::ostream& operator<<(std::ostream& out,const XYZ& v)
{
return v.write(out);
}
//! Generates a random point in the cube bounded by (0,0,0) and (1.0,1.0,1.0)
class RandomXYZInUnitCube : public XYZ
{
public:
//! Constructor.
RandomXYZInUnitCube(Random01&);
};
//! Generates random points in a recnangular box centred on the origin
class RandomXYZInBox : public XYZ
{
public:
//! Constructor.
RandomXYZInBox(Random01& rng,const XYZ& bounds);
};
//! Generates a random point in or on a unit-radius sphere centred on the origin.
class RandomXYZInSphere : public XYZ
{
public:
//! Constructor.
RandomXYZInSphere(Random01& rng,real radius);
};
//! Generates a random point on the surface of a unit-radius sphere
class RandomXYZSphereNormal : public XYZ
{
public:
//! Constructor.
RandomXYZSphereNormal(Random01& rng);
};
//! Generates a random point in or on an origin-centred ellipsoid with semi-axes of the specified size.
class RandomXYZInEllipsoid : public XYZ
{
public:
//! Constructor.
RandomXYZInEllipsoid(Random01& rng,const XYZ& axes);
};
//! Generates a random point in or on a disc in the XY plane of the specified radius.
class RandomXYZInXYDisc : public XYZ
{
public:
//! Constructor.
RandomXYZInXYDisc(Random01& rng,real radius);
};
#endif
|