SHARE
TWEET

Untitled

a guest Dec 8th, 2015 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*************************************************************************************************
  2.  *
  3.  * Modeling and animation (TNM079) 2007
  4.  * Code base for lab assignments. Copyright:
  5.  *   Gunnar Johansson (gunnar.johansson@itn.liu.se)
  6.  *   Ken Museth (ken.museth@itn.liu.se)
  7.  *   Michael Bang Nielsen (bang@daimi.au.dk)
  8.  *   Ola Nilsson (ola.nilsson@itn.liu.se)
  9.  *   Andreas Sderstrm (andreas.soderstrom@itn.liu.se)
  10.  *
  11.  *************************************************************************************************/
  12. #ifndef _vector3_h
  13. #define _vector3_h
  14.  
  15. #include "Vector4.h"
  16. #include <cmath>
  17. #include <iostream>
  18. #include <string>
  19. #include <sstream>
  20. #include <cassert>
  21.  
  22. template <typename Real>
  23. class Vector3
  24. {
  25. public:
  26.   typedef Real ValueType;
  27.  
  28. public:
  29.   // Constructors, destructor and assignment operators
  30.   //! Default constructor, initializes to 0
  31.   Vector3() {
  32.     v[0] = v[1] = v[2] = Real(0.0);
  33.   }
  34.   /*Vector3(Real v0) {
  35.     v[0] = v[1] = v[2] = v0;
  36.   }*/
  37.   Vector3(Real v0, Real v1, Real v2) {
  38.     v[0] = v0;
  39.     v[1] = v1;
  40.     v[2] = v2;
  41.   }
  42.         /*
  43.   //! Constructor that takes a Vector4
  44.   Vector3(const Vector4<Real>& vec4) {
  45.     v[0]=vec4[0]/vec4[3];
  46.     v[1]=vec4[1]/vec4[3];
  47.     v[2]=vec4[2]/vec4[3];
  48.   }
  49.          */
  50.   //! Copy constructor
  51.   template <typename Real2>
  52.   Vector3(const Vector3<Real2>& vec3) {
  53.     *this = vec3;
  54.   }
  55.   //! Assignment operator = for vector
  56.   template <typename Real2>
  57.   Vector3& operator=(const Vector3<Real2>& vec3) {
  58.     v[0]=(Real)vec3[0];
  59.     v[1]=(Real)vec3[1];
  60.     v[2]=(Real)vec3[2];
  61.     return *this;
  62.   }
  63.   //! Assignment operator = for scalar
  64.   template <typename Scalar>
  65.   Vector3& operator=(Scalar s) {
  66.     v[0]=(Real)s;
  67.     v[1]=(Real)s;
  68.     v[2]=(Real)s;
  69.     return *this;
  70.   }
  71.   //! Constructor from std::string object
  72.   Vector3(std::string str) {
  73.     std::stringstream s;
  74.     s << str;
  75.     Load(s);
  76.   }
  77.   //! Destructor
  78.   ~Vector3() { }
  79.   //! Load a vector from an istream
  80.   void Load(std::istream& is) {
  81.     is >> v[0] >> v[1] >> v[2];
  82.   }
  83.   //! Save a vector from to a ostream
  84.   void Save(std::ostream& os) const {
  85.     os << v[0] << " " << v[1] << " " << v[2];
  86.   }
  87.  
  88.   // Access operators
  89.   Real& operator[](unsigned int i) {
  90.     assert(i >= 0 && i <= 2 && "Index out of bounds in Vector3");
  91.     return v[i];
  92.   }
  93.   Real operator[](unsigned int i) const {
  94.     assert(i >= 0 && i <= 2 && "Index out of bounds in Vector3");
  95.     return v[i];
  96.   }
  97. //   Real x() const {return v[0];}
  98. //   Real y() const {return v[1];}
  99. //   Real z() const {return v[2];}
  100. //   Real& x() {return v[0];}
  101. //   Real& y() {return v[1];}
  102. //   Real& z() {return v[2];}
  103.   Real *GetArrayPtr() { return v; }
  104.   const Real *GetArrayPtr() const { return v; }
  105.  
  106.   // Algebraic operators
  107.   Vector3 operator-(const Vector3& vec3) const {
  108.     return Vector3(v[0]-vec3.v[0], v[1]-vec3.v[1], v[2]-vec3.v[2]);
  109.   }
  110.   Vector3 operator+(const Vector3& vec3) const {
  111.     return Vector3(v[0]+vec3.v[0], v[1]+vec3.v[1], v[2]+vec3.v[2]);
  112.   }
  113.   Vector3 & operator+=(const Vector3& vec3) {
  114.     v[0]+=vec3.v[0];
  115.     v[1]+=vec3.v[1];
  116.     v[2]+=vec3.v[2];
  117.     return *this;
  118.   }
  119.   Vector3 & operator-=(const Vector3& vec3) {
  120.     v[0]-=vec3.v[0];
  121.     v[1]-=vec3.v[1];
  122.     v[2]-=vec3.v[2];
  123.     return *this;
  124.   }
  125.   //! Dot product
  126.   Real operator*(const Vector3<Real>& vec3) const {
  127.     return v[0]*vec3[0] + v[1]*vec3[1] + v[2]*vec3[2];
  128.   }
  129.   //! Scalar multiply for scaling the vector
  130.   template<typename Scalar>
  131.   Vector3<Real> operator*(Scalar s) const {
  132.     return Vector3<Real>((Real)(s*v[0]), (Real)(s*v[1]), (Real)(s*v[2]));
  133.   }
  134.   template<typename Scalar>
  135.   Vector3<Real>& operator*=(Scalar s) {
  136.     v[0]*=(Real)s;
  137.     v[1]*=(Real)s;
  138.     v[2]*=(Real)s;
  139.     return *this;
  140.   }
  141.   template<typename Scalar>
  142.   Vector3<Real>& operator/=(Scalar s) {
  143.     return *this *= 1.0/s;
  144.   }
  145.   //! Scaling of vector by division
  146.   template<typename Scalar>
  147.   Vector3<Real> operator/(Scalar s) const {
  148.     Real invS = 1.0/s;
  149.     return Vector3<Real>(v[0]*invS, v[1]*invS, v[2]*invS);
  150.   }
  151.   //! Multiplication with another vector entry by entry
  152.   Vector3 EntryMult(const Vector3& vec3) const {
  153.     return Vector3(v[0]*vec3.v[0], v[1]*vec3.v[1], v[2]*vec3.v[2]);
  154.   }
  155.  
  156.   // Comparison operators
  157.   //! Greater than operator for lexicographic comparison
  158.   bool operator >  ( const Vector3<Real> &A ) const;
  159.   //! Less than operator for lexicographic comparison
  160.   bool operator <  ( const Vector3<Real> &A ) const;
  161.   //! Less than or equal operator for lexicographic comparison
  162.   bool operator <= ( const Vector3<Real> &A ) const;
  163.   //! Greater than or equal operator for lexicographic comparison
  164.   bool operator >= ( const Vector3<Real> &A ) const;
  165.   //! Equal operator for lexicographic comparison
  166.   bool operator == ( const Vector3<Real> &A ) const;
  167.   //! Not equal operator for lexicographic comparison
  168.   bool operator != (const Vector3<Real>& other) const {return !(*this == other);}
  169.   //! Templated equal operator for lexicographic comparison of vectors of different type
  170.   template <typename Real2>
  171.   bool operator==(const Vector3<Real2>& vec3) const {
  172.     return v[0]==(Real)vec3[0] && v[1]==(Real)vec3[1] && v[2]==(Real)vec3[2];
  173.   }
  174.  
  175.   // Geometric operators
  176.   //! Norm of a vector
  177.   Real Norm() const {
  178.     return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  179.   }
  180.   //! Length of a vector, specilization exists for type float, see below
  181.   Real Length() const {
  182.     return sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  183.   }
  184.   //! Normalization of vector
  185.   Vector3& Normalize() {
  186.     Real len = Length();
  187.     v[0] /= len;
  188.     v[1] /= len;
  189.     v[2] /= len;
  190.     return *this;
  191.   }
  192.   // static methods
  193.   static Vector3 Floor(const Vector3& vec3) {
  194.     return Vector3(::floor(vec3[0]), ::floor(vec3[1]), ::floor(vec3[2]));
  195.   }
  196.   static Vector3 Ceil(const Vector3& vec3) {
  197.     return Vector3(::ceil(vec3[0]), ::ceil(vec3[1]), ::ceil(vec3[2]));
  198.   }
  199.  
  200. protected:
  201.   Real v[3];
  202.   // friends
  203.   //! Cross product
  204.   template<typename Real2>
  205.   friend Vector3<Real2> Cross(const Vector3<Real2>& v1, const Vector3<Real2>& v2);
  206. };
  207.  
  208. //! Specialization of length for float type, uses sqrtf instead.
  209. template<>
  210. inline float Vector3<float>::Length() const{
  211.   return sqrtf( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  212. }
  213.  
  214. //! Cross product
  215. template <typename Real2>
  216. Vector3<Real2> Cross(const Vector3<Real2>& v1, const Vector3<Real2>& v2)
  217. {
  218.   Vector3<Real2> res;
  219.  
  220.   res[0] = v1[1] * v2[2] - v1[2] * v2[1];
  221.   res[1] = v1[2] * v2[0] - v1[0] * v2[2];
  222.   res[2] = v1[0] * v2[1] - v1[1] * v2[0];
  223.  
  224.   return res;
  225. }
  226.  
  227. //! Less than operator for lexicographic comparison
  228. template <class Real>
  229. inline bool Vector3<Real>::operator < ( const Vector3<Real> &A ) const
  230. {
  231.   if ( v[0]<A[0] )
  232.     return true;
  233.   else if ( v[0]>A[0] )
  234.     return false;
  235.   else if ( v[1]<A[1] )
  236.     return true;
  237.   else if ( v[1]>A[1] )
  238.     return false;
  239.   else if ( v[2]<A[2] )
  240.     return true;
  241.   else
  242.     return false;
  243. }
  244. template <class Real>
  245. inline bool Vector3<Real>::operator > ( const Vector3<Real> &A ) const
  246. {
  247.   if ( v[0]>A[0] )
  248.     return true;
  249.   else if ( v[0]<A[0] )
  250.     return false;
  251.   else if ( v[1]>A[1] )
  252.     return true;
  253.   else if ( v[1]<A[1] )
  254.     return false;
  255.   else if ( v[2]>A[2] )
  256.     return true;
  257.   else
  258.     return false;
  259. }
  260. template <class Real>
  261. inline bool Vector3<Real>::operator <= ( const Vector3<Real> &A ) const
  262. {
  263.   if ( v[0]<A[0] )
  264.     return true;
  265.   else if ( v[0]>A[0] )
  266.     return false;
  267.   else if ( v[1]<A[1] )
  268.     return true;
  269.   else if ( v[1]>A[1] )
  270.     return false;
  271.   else if ( v[2]<=A[2] )
  272.     return true;
  273.   else
  274.     return false;
  275. }
  276. template <class Real>
  277. inline bool Vector3<Real>::operator >= ( const Vector3<Real> &A ) const
  278. {
  279.   if ( v[0]>A[0] )
  280.     return true;
  281.   else if ( v[0]<A[0] )
  282.     return false;
  283.   else if ( v[1]>A[1] )
  284.     return true;
  285.   else if ( v[1]<A[1] )
  286.     return false;
  287.   else if ( v[2]>=A[2] )
  288.     return true;
  289.   else
  290.     return false;
  291. }
  292. template <class Real>
  293. inline bool Vector3<Real>::operator == ( const Vector3<Real> &A ) const
  294. {
  295.   return ( (v[0]-A[0] > -0.01 && v[0]-A[0] < 0.01) && (v[1]-A[1] > -0.01 && v[1]-A[1] < 0.01) && (v[2]-A[2] > -0.01 && v[2]-A[2] < 0.01) );
  296. }
  297.  
  298.  
  299.  
  300. template <typename Real1, typename Real2>
  301. Vector3<Real2> operator*(Real1 r, const Vector3<Real2>& v)
  302. {
  303.   Vector3<Real2> res;
  304.  
  305.   res[0] = v[0]*r;
  306.   res[1] = v[1]*r;
  307.   res[2] = v[2]*r;
  308.  
  309.   return res;
  310. }
  311.  
  312. template <typename Real>
  313. const Vector3<Real> operator-(const Vector3<Real>& vec3)
  314. {
  315.   return Vector3<Real>(-vec3[0], -vec3[1], -vec3[2]);
  316. }
  317.  
  318. namespace std
  319. {
  320.   template <typename Real>
  321.   ostream& operator<<(ostream& os, const Vector3<Real>& m)
  322.   {
  323.     m.Save(os);
  324.     return os;
  325.   }
  326.  
  327.   template <typename Real>
  328.   istream& operator>>(istream& is, Vector3<Real>& m)
  329.   {
  330.     m.Load(is);
  331.     return is;
  332.   }
  333. }
  334.  
  335.  
  336. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top