xyz.h 6.17 KB
Newer Older
Jonathan Carter's avatar
Jonathan Carter committed
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
/**************************************************************************/
/*  Copyright 2009 Tim Day                                                */
/*                                                                        */
/*  This file is part of Fracplanet                                       */
/*                                                                        */
/*  Fracplanet 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.                                   */
/*                                                                        */
/*  Fracplanet 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 Fracplanet.  If not, see <http://www.gnu.org/licenses/>.   */
/**************************************************************************/

/*! \file
  \brief Interface for class XYZ.
*/

#ifndef _xyz_h_
#define _xyz_h_

#include "common.h"
#include "random.h"

//! Class to hold vectors in 3D cartesian co-ordinates.
/*! Direct access to the x,y,z members is permitted.
    There is a general assumption that the co-ordinate system will be right handed,
    and that for terrain type applications x and y will be plan position and z will be height.
 */
class XYZ
{
 public:
  
  float x;
  float y;
  float z;

  //! Null constructor.
  /*! NB The components are not cleared to zero. 
   */
  XYZ()
    {}

  //! Copy constructor.
  XYZ(const XYZ& v)
    :x(v.x),y(v.y),z(v.z){}

  //! Initialise from separate components.
  XYZ(float vx,float vy,float vz)
    :x(vx),y(vy),z(vz){}

  //! Destructor.
  ~XYZ()
    {}

  typedef float XYZ::* ElementPtr;
  static ElementPtr element_table[3];

  //! Access by number
  const float& element(uint e) const
    {
      return this->*(element_table[e]);
    }

  //! Access by number
  float& element(uint e)
    {
      return this->*(element_table[e]);
    }

  //! Multiply by scalar.
  void operator*=(float k)
    {
      x*=k;
      y*=k;
      z*=k;
    }

  //! Divide by scalar.
  /*! Implemented assuming one divide and three multiplies is faster than three divides.
   */
  void operator/=(float k)
    {
      const float ik(1.0/k);
      x*=ik;
      y*=ik;
      z*=ik;
    }

  //! Vector addition.
  void operator+=(const XYZ& v)
    {
      x+=v.x;
      y+=v.y;
      z+=v.z;
    }

  //! Vector subtraction.
  void operator-=(const XYZ& v)
    {
      x-=v.x;
      y-=v.y;
      z-=v.z;
    }

  //! 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.
  float magnitude2() const
    {
      return x*x+y*y+z*z;
    }

  //! Return the magnitude.
  float magnitude() const
    {
      return sqrt(magnitude2());
    }

  //! Return the vector normalised.
  const XYZ normalised() const;

  //! Normalise this vector.
  void normalise();

  //! Write the vector (spaces as separators).
  std::ostream& write(std::ostream&) const;

  //! Alternate formatting.
  const std::string format_comma() const;

  //! Alternate formatting.
  const std::string format_blender() const;

  //! Alternate formatting.
  const std::string format_pov() const;
};

//! 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 float 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*(float k,const XYZ& v)
{  
  return XYZ(k*v.x,k*v.y,k*v.z);
}

//! Multiplication by scalar.
inline const XYZ operator*(const XYZ& v,float k)
{
  return XYZ(k*v.x,k*v.y,k*v.z);
}

//! Division by scalar.
inline const XYZ operator/(const XYZ& v,float k)
{
  return (1.0/k)*v;
}

//! Equality operator.
inline bool operator==(const XYZ& a,const XYZ& b)
{
  return (a.x==b.x && a.y==b.y && a.z==b.z);
}

//! Inequality operator.
inline bool operator!=(const XYZ& a,const XYZ& b)
{
  return (a.x!=b.x || a.y!=b.y || a.z!=b.z);
}

/*! Will fail assertion if the co-ordinate has zero magnitude.
 */
inline const XYZ XYZ::normalised() const
{
  const float m=magnitude();
  assert(m!=0.0);
  return (*this)/m;
}

/*! Will fail assertion if the co-ordinate has zero magnitude.
 */
inline void XYZ::normalise()
{
  (*this)=normalised();
}

//! Stream output operator.
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:
  RandomXYZInUnitCube(Random01&);
};

//! Generates random points in a recnangular box centred on the origin
class RandomXYZInBox : public XYZ
{
 public:
  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:
  RandomXYZInSphere(Random01& rng,float radius);
};

//! Generates a random point in or on an origin-centred ellipsoid with semi-axes of the specified size.
class RandomXYZInEllipsoid : public XYZ
{
 public:
  RandomXYZInEllipsoid(Random01& rng,const XYZ& axes);
};

//! Generates a random point on the surface of a unit-radius sphere
class RandomXYZSphereNormal : public XYZ
{
 public:
  RandomXYZSphereNormal(Random01& rng);
};

#endif