Grid3
Vector3.h
1 #ifndef VECTOR3
2 #define VECTOR3
3 
4 #include <cmath>
5 #include <ostream>
6 
8 template<class T>
9 class Vec3 {
10 public:
11  T x;
12  T y;
13  T z;
14 
15 public:
17  Vec3(T _x=0, T _y=0, T _z=0) : x(_x), y(_y), z(_z) {}
18 
20  template<class T2>
21  Vec3(Vec3<T2> v): x(v.x), y(v.y), z(v.z) {}
22 
24  Vec3<T> operator+(const Vec3<T> &v) const {
25  return Vec3<T>(x+v.x,y+v.y,z+v.z);
26  }
27 
29  Vec3<T>& operator+=(const Vec3<T> &v) {
30  x += v.x;
31  y += v.y;
32  z += v.z;
33  return (*this);
34  }
35 
37  Vec3<T> operator-(const Vec3<T> &v) const {
38  return Vec3<T>(x-v.x,y-v.y,z-v.z);
39  }
40 
42  Vec3<T>& operator-=(const Vec3<T> &v) {
43  x -= v.x;
44  y -= v.y;
45  z -= v.z;
46  return (*this);
47  }
48 
50  Vec3<T> operator*(const double &d) const {
51  return Vec3<T>(T(x*d),T(y*d),T(z*d));
52  }
53 
55  Vec3<T>& operator*=(const double &d) {
56  x = T(x*d);
57  y = T(y*d);
58  z = T(z*d);
59  return (*this);
60  }
61 
63  Vec3<T> operator/(const double &d) const {
64  return Vec3<T>(T(x/d),T(y/d),T(z/d));
65  }
66 
68  Vec3<T>& operator/=(const double &d) {
69  x = T(x/d);
70  y = T(y/d);
71  z = T(z/d);
72  return (*this);
73  }
74 
76  double operator*(const Vec3<T> &v) const {
77  return x*v.x + y*v.y + z*v.z;
78  }
79 
81  Vec3<T> cross(const Vec3<T> &v) const {
82  Vec3<T> tmp;
83  tmp.x=y*v.z-z*v.y;
84  tmp.y=z*v.x-x*v.z;
85  tmp.z=x*v.y-y*v.x;
86  return tmp;
87  }
88 
90  Vec3<T> operator%(const Vec3<T> &v) const {
91  return Vec3<T>(x*v.x, y*v.y, z*v.z);
92  }
93 
96  double n = norm();
97  (*this) /= n;
98  return (*this);
99  }
100 
102  double norm() const {
103  return std::sqrt(double(x*x+y*y+z*z));
104  }
105 
107  double norm2() const {
108  return x*x+y*y+z*z;
109  }
110 
112  bool operator==(const Vec3<T> &v) const {
113  return (x==v.x && y==v.y && z==v.z);
114  }
115 
117  bool operator!=(const Vec3<T> &v) const {
118  return !((*this)==v);
119  }
120 
122  friend Vec3<T> operator*(const double &d, const Vec3<T> &v) {
123  return v*d;
124  }
125 
126 
128  friend std::ostream& operator<<(std::ostream &os, const Vec3<T> &v) {
129  os << v.x << " " << v.y<<" "<<v.z;
130  return os;
131  }
132 
133 
134 
135 };
136 
137 
138 
139 template class Vec3<float>;
140 template class Vec3<double>;
141 template class Vec3<int>;
142 
143 typedef Vec3<float> Vec3f;
144 typedef Vec3<double> Vec3d;
145 typedef Vec3<int> Vec3i;
146 
147 #endif
Vec3< T > operator/(const double &d) const
Division with double.
Definition: Vector3.h:63
bool operator!=(const Vec3< T > &v) const
Not equal (used for integer position vectors)
Definition: Vector3.h:117
Class representing 3D vectors, with elements of type T.
Definition: Vector3.h:9
Vec3< T > operator%(const Vec3< T > &v) const
Element-wise multiplication (hadamard product)
Definition: Vector3.h:90
Vec3< T > & operator-=(const Vec3< T > &v)
In-place vector subtraction.
Definition: Vector3.h:42
Vec3< T > cross(const Vec3< T > &v) const
Cross product.
Definition: Vector3.h:81
Vec3< T > & operator/=(const double &d)
In-place division with double.
Definition: Vector3.h:68
Vec3< T > operator*(const double &d) const
Multiplication with double.
Definition: Vector3.h:50
friend Vec3< T > operator*(const double &d, const Vec3< T > &v)
Multiplication with double.
Definition: Vector3.h:122
Vec3< T > & operator*=(const double &d)
In-place multiplication with double.
Definition: Vector3.h:55
double operator*(const Vec3< T > &v) const
Scalar product.
Definition: Vector3.h:76
Vec3< T > & operator+=(const Vec3< T > &v)
In-place vector addition.
Definition: Vector3.h:29
double norm() const
Vector norm.
Definition: Vector3.h:102
double norm2() const
Vector norm squared.
Definition: Vector3.h:107
Vec3(Vec3< T2 > v)
Copy constructor.
Definition: Vector3.h:21
Vec3< T > operator+(const Vec3< T > &v) const
Vector addition.
Definition: Vector3.h:24
Vec3< T > operator-(const Vec3< T > &v) const
Vector subtraction.
Definition: Vector3.h:37
bool operator==(const Vec3< T > &v) const
Equal (used for integer position vectors)
Definition: Vector3.h:112
Vec3(T _x=0, T _y=0, T _z=0)
Standard constructor.
Definition: Vector3.h:17
Vec3< T > & normalize()
Normalize.
Definition: Vector3.h:95