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

  1. #ifndef K3DSDK_POINT4_H
  2. #define K3DSDK_POINT4_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. #include <iomanip>
  55.  
  56. namespace k3d
  57. {
  58.  
  59. class vector4;
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // point4
  63.  
  64. /// Encapsulates a four-dimensional location
  65. class point4
  66. {
  67. public:
  68.     /// Stores the point values
  69.     double n[4];
  70.  
  71.     point4()
  72.     {
  73.         n[0] = n[1] = n[2] = n[3] = 0.0;
  74.     }
  75.  
  76.     point4(const double x, const double y, const double z, const double w)
  77.     {
  78.         n[0] = x;
  79.         n[1] = y;
  80.         n[2] = z;
  81.         n[3] = w;
  82.     }
  83.  
  84.     /// Addition of a vector
  85.     point4& operator+=(const vector4& v);
  86.  
  87.     /// Subtraction of a vector
  88.     point4& operator-=(const vector4& v);
  89.  
  90.     /// Multiplication by a constant
  91.     point4& operator*=(const double d)
  92.     {
  93.         n[0] *= d;
  94.         n[1] *= d;
  95.         n[2] *= d;
  96.         n[3] *= d;
  97.         return *this;
  98.     }
  99.  
  100.     /// Division by a constant
  101.     point4& operator/=(const double d)
  102.     {
  103.         return_val_if_fail(d, *this);
  104.  
  105.         double d_inv = 1./d;
  106.         n[0] *= d_inv;
  107.         n[1] *= d_inv;
  108.         n[2] *= d_inv;
  109.         n[3] *= d_inv;
  110.         return *this;
  111.     }
  112.  
  113.     /// Returns an indexed dimension by reference
  114.     double& operator[](int i)
  115.     {
  116.         return n[i];
  117.     }
  118.  
  119.     /// Returns an indexed dimension by value
  120.     double operator[](int i) const
  121.     {
  122.         return n[i];
  123.     }
  124.  
  125.     friend std::ostream& operator<<(std::ostream& Stream, const point4& RHS)
  126.     {
  127.         boost::io::ios_flags_saver stream_state(Stream);
  128.         Stream << std::setprecision(17) << RHS.n[0] << " " << RHS.n[1] << " " << RHS.n[2] << " " << RHS.n[3];
  129.         return Stream;
  130.     }
  131.  
  132.     friend std::istream& operator>>(std::istream& Stream, point4& RHS)
  133.     {
  134.         Stream >> RHS.n[0] >> RHS.n[1] >> RHS.n[2] >> RHS.n[3];
  135.         return Stream;
  136.     }
  137. };
  138.  
  139. /// Negation
  140. inline const point4 operator-(const point4& a)
  141. {
  142.     return point4(-a.n[0],-a.n[1],-a.n[2],-a.n[3]);
  143. }
  144.  
  145. /// Addition
  146. /** \note We implement this as a convenience for the benefit of linear interpolation - adding two points has no geometric meaning */
  147. inline const point4 operator+(const point4& a, const point4& b)
  148. {
  149.     return point4(a.n[0] + b.n[0], a.n[1] + b.n[1], a.n[2] + b.n[2], a.n[3] + b.n[3]);
  150. }
  151.  
  152. /// Multiplication by a constant
  153. inline const point4 operator*(const point4& a, const double d)
  154. {
  155.     return point4(d*a.n[0], d*a.n[1], d*a.n[2], d*a.n[3] );
  156. }
  157.  
  158. /// Multiplication by a constant
  159. inline const point4 operator*(const double d, const point4& a)
  160. {
  161.     return a*d;
  162. }
  163.  
  164. /// Division by a constant
  165. inline const point4 operator/(const point4& a, const double d)
  166. {
  167.     return_val_if_fail(d, point4());
  168.  
  169.     double d_inv = 1./d;
  170.     return point4(a.n[0]*d_inv, a.n[1]*d_inv, a.n[2]*d_inv, a.n[3]*d_inv);
  171. }
  172.  
  173. /// Equality
  174. inline const bool operator==(const point4& a, const point4& b)
  175. {
  176.     return (a.n[0] == b.n[0]) && (a.n[1] == b.n[1]) && (a.n[2] == b.n[2]) && (a.n[3] == b.n[3]);
  177. }
  178.  
  179. /// Inequality
  180. inline bool operator!=(const point4& a, const point4& b)
  181. {
  182.     return !(a == b);
  183. }
  184.  
  185. /// Specialization of almost_equal that tests two point4 objects for near-equality
  186. template<>
  187. class almost_equal<point4>
  188. {
  189.     typedef point4 T;
  190. public:
  191.     almost_equal(const boost::uint64_t Threshold) : threshold(Threshold) { }
  192.     inline const bool operator()(const T& A, const T& B) const
  193.     {
  194.         return std::equal(A.n, A.n + 4, B.n, almost_equal<double>(threshold));
  195.     }
  196.  
  197. private:
  198.     const boost::uint64_t threshold;
  199. };
  200.  
  201. } // namespace k3d
  202.  
  203. #endif // !K3DSDK_POINT4_H
  204.  
  205.