home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / vector3.h < prev    next >
C/C++ Source or Header  |  2008-12-16  |  6KB  |  265 lines

  1. #ifndef K3DSDK_VECTOR3_H
  2. #define K3DSDK_VECTOR3_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2006, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.     \brief Vector (points, vectors and normals) routines
  25.     \author Timothy M. Shead (tshead@k-3d.com)
  26. */
  27.  
  28. /****************************************************************
  29. *
  30. * C++ Vector and Matrix Algebra routines
  31. * Author: Jean-Francois DOUE
  32. * Version 3.1 --- October 1993
  33. *
  34. ****************************************************************/
  35.  
  36. //
  37. //    From "Graphics Gems IV / Edited by Paul S. Heckbert
  38. //    Academic Press, 1994, ISBN 0-12-336156-9
  39. //    "You are free to use and modify this code in any way
  40. //    you like." (p. xv)
  41. //
  42. //    Modified by J. Nagle, March 1997
  43. //    -    All functions are inline.
  44. //    -    All functions are const-correct.
  45. //    -    All checking is via the standard "assert" macro.
  46. //
  47.  
  48. // Modified by Tim Shead for use with K-3D, January 1998
  49.  
  50. #include "almost_equal.h"
  51. #include "result.h"
  52.  
  53. #include <boost/io/ios_state.hpp>
  54.  
  55. #include <cmath>
  56. #include <iomanip>
  57.  
  58. namespace k3d
  59. {
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // vector3
  63.  
  64. /// Encapsulates a direction vector in three-dimensional space
  65. class vector3
  66. {
  67. public:
  68.     /// Stores the vector values
  69.     double n[3];
  70.  
  71.     vector3()
  72.     {
  73.         n[0] = n[1] = n[2] = 0.0;
  74.     }
  75.  
  76.     vector3(const double x, const double y, const double z)
  77.     {
  78.         n[0] = x;
  79.         n[1] = y;
  80.         n[2] = z;
  81.     }
  82.  
  83.     vector3& operator+=(const vector3& v)
  84.     {
  85.         n[0] += v.n[0];
  86.         n[1] += v.n[1];
  87.         n[2] += v.n[2];
  88.         return *this;
  89.     }
  90.  
  91.     vector3& operator-=(const vector3& v)
  92.     {
  93.         n[0] -= v.n[0];
  94.         n[1] -= v.n[1];
  95.         n[2] -= v.n[2];
  96.         return *this;
  97.     }
  98.  
  99.     vector3& operator*=(const double d)
  100.     {
  101.         n[0] *= d;
  102.         n[1] *= d;
  103.         n[2] *= d;
  104.         return *this;
  105.     }
  106.  
  107.     vector3& operator/=(const double d)
  108.     {
  109.         return_val_if_fail(d, *this);
  110.  
  111.         const double d_inv = 1./d;
  112.         n[0] *= d_inv;
  113.         n[1] *= d_inv;
  114.         n[2] *= d_inv;
  115.         return *this;
  116.     }
  117.  
  118.     double& operator[](const unsigned int i)
  119.     {
  120.         assert_warning((i >= 0) && (i <= 2));
  121.         return n[i];
  122.     }
  123.  
  124.     double operator[](const unsigned int i) const
  125.     {
  126.         return_val_if_fail((i >= 0) && (i <= 2), 0);
  127.         return n[i];
  128.     }
  129.  
  130.     /// Returns the normal length
  131.     double length() const
  132.     {
  133.         return std::sqrt(length2());
  134.     }
  135.  
  136.     /// Returns the squared normal length
  137.     double length2() const
  138.     {
  139.         return n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
  140.     }
  141.  
  142.     friend std::ostream& operator<<(std::ostream& Stream, const vector3& RHS)
  143.     {
  144.         boost::io::ios_flags_saver stream_state(Stream);
  145.         Stream << std::setprecision(17) << RHS.n[0] << " " << RHS.n[1] << " " << RHS.n[2];
  146.         return Stream;
  147.     }
  148.  
  149.     friend std::istream& operator>>(std::istream& Stream, vector3& RHS)
  150.     {
  151.         Stream >> RHS.n[0] >> RHS.n[1] >> RHS.n[2];
  152.         return Stream;
  153.     }
  154. };
  155.  
  156. /// Negation
  157. inline const vector3 operator-(const vector3& v)
  158. {
  159.     return vector3(-v.n[0], -v.n[1], -v.n[2]);
  160. }
  161.  
  162. /// Addition
  163. inline const vector3 operator+(const vector3& a, const vector3& b)
  164. {
  165.     return vector3(a.n[0] + b.n[0], a.n[1] + b.n[1], a.n[2] + b.n[2]);
  166. }
  167.  
  168. /// Subtraction
  169. inline const vector3 operator-(const vector3& a, const vector3& b)
  170. {
  171.     return vector3(a.n[0] - b.n[0], a.n[1] - b.n[1], a.n[2] - b.n[2]);
  172. }
  173.  
  174. /// Multiplication by a constant
  175. inline const vector3 operator*(const vector3& a, const double d)
  176. {
  177.     return vector3(a.n[0] * d, a.n[1] * d, a.n[2] * d);
  178. }
  179.  
  180. /// Multiplication by a constant
  181. inline const vector3 operator*(const double d, const vector3& a)
  182. {
  183.     return vector3(a.n[0] * d, a.n[1] * d, a.n[2] * d);
  184. }
  185.  
  186. /// Returns the dot product of two vectors
  187. inline const double operator*(const vector3& a, const vector3& b)
  188. {
  189.     return a.n[0] * b.n[0] + a.n[1] * b.n[1] + a.n[2] * b.n[2];
  190. }
  191.  
  192. /// Division by a constant
  193. inline const vector3 operator/(const vector3& a, const double d)
  194. {
  195.     return_val_if_fail(d, vector3());
  196.     return vector3(a.n[0] / d, a.n[1] / d, a.n[2] / d);
  197. }
  198.  
  199. /// Returns the cross product of two vectors
  200. inline const vector3 operator^(const vector3& a, const vector3& b)
  201. {
  202.     return vector3(a.n[1] * b.n[2] - a.n[2] * b.n[1], a.n[2] * b.n[0] - a.n[0] * b.n[2], a.n[0] * b.n[1] - a.n[1] * b.n[0]);
  203. }
  204.  
  205. /// Equality
  206. inline const bool operator==(const vector3& a, const vector3& b)
  207. {
  208.     return a.n[0] == b.n[0] && a.n[1] == b.n[1] && a.n[2] == b.n[2];
  209. }
  210.  
  211. /// Inequality
  212. inline const bool operator!=(const vector3& a, const vector3& b)
  213. {
  214.     return a.n[0] != b.n[0] || a.n[1] != b.n[1] || a.n[2] != b.n[2];
  215. }
  216.  
  217. /// Returns the length of a vector
  218. inline const double length(const vector3& Vector)
  219. {
  220.     return Vector.length();
  221. }
  222.  
  223. /// Returns the normalized form of a vector
  224. inline const vector3 normalize(const vector3& Vector)
  225. {
  226.     const double length = Vector.length();
  227.     return_val_if_fail(length, Vector);
  228.     return Vector / length;
  229. }
  230.  
  231. /// Converts Cartesian coordinates to spherical coordinates
  232. inline const vector3 spherical(const vector3& Vector)
  233. {
  234.     return vector3(Vector.length(), std::atan2(Vector[0], Vector[2]), std::atan2(Vector[1], sqrt(Vector[0] * Vector[0] + Vector[2] * Vector[2])));
  235. /*
  236.     // Handle the singularity at the poles
  237.     if(0.0 == Vector[0] && 0.0 == Vector[2])
  238.         return point3(n[1] > 0.0 ? sdpPiOver2 : -sdpPiOver2, 0.0, Vector.length());
  239.  
  240.  
  241.     return point3(atan2(Vector[1], sqrt(Vector[0] * Vector[0] + Vector[2] * Vector[2])), atan2(Vector[0], Vector[2]), Vector.length());
  242. */
  243. }
  244.  
  245. /// Specialization of almost_equal that tests two vector3 objects for near-equality
  246. template<>
  247. class almost_equal<vector3>
  248. {
  249.     typedef vector3 T;
  250. public:
  251.     almost_equal(const boost::uint64_t Threshold) : threshold(Threshold) { }
  252.     inline const bool operator()(const T& A, const T& B) const
  253.     {
  254.         return std::equal(A.n, A.n + 3, B.n, almost_equal<double>(threshold));
  255.     }
  256.  
  257. private:
  258.     const boost::uint64_t threshold;
  259. };
  260.  
  261. } // namespace k3d
  262.  
  263. #endif // !K3DSDK_VECTOR3_H
  264.  
  265.