Grid3
Vector3.h
Go to the documentation of this file.
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 value=0) : x(value), y(value), z(value) {}
18 
20  Vec3(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {}
21 
23  template<class T2>
24  Vec3(Vec3<T2> v): x(v.x), y(v.y), z(v.z) {}
25 
27  Vec3<T> operator+(const Vec3<T> &v) const {
28  return Vec3<T>(x+v.x,y+v.y,z+v.z);
29  }
30 
32  Vec3<T>& operator+=(const Vec3<T> &v) {
33  x += v.x;
34  y += v.y;
35  z += v.z;
36  return (*this);
37  }
38 
40  Vec3<T> operator-(const Vec3<T> &v) const {
41  return Vec3<T>(x-v.x,y-v.y,z-v.z);
42  }
43 
45  Vec3<T>& operator-=(const Vec3<T> &v) {
46  x -= v.x;
47  y -= v.y;
48  z -= v.z;
49  return (*this);
50  }
51 
53  Vec3<T> operator*(const double &d) const {
54  return Vec3<T>(T(x*d),T(y*d),T(z*d));
55  }
56 
58  Vec3<T>& operator*=(const double &d) {
59  x = T(x*d);
60  y = T(y*d);
61  z = T(z*d);
62  return (*this);
63  }
64 
66  Vec3<T> operator/(const double &d) const {
67  return Vec3<T>(T(x/d),T(y/d),T(z/d));
68  }
69 
71  Vec3<T>& operator/=(const double &d) {
72  x = T(x/d);
73  y = T(y/d);
74  z = T(z/d);
75  return (*this);
76  }
77 
79  double operator*(const Vec3<T> &v) const {
80  return x*v.x + y*v.y + z*v.z;
81  }
82 
84  Vec3<T> cross(const Vec3<T> &v) const {
85  Vec3<T> tmp;
86  tmp.x=y*v.z-z*v.y;
87  tmp.y=z*v.x-x*v.z;
88  tmp.z=x*v.y-y*v.x;
89  return tmp;
90  }
91 
93  Vec3<T> operator%(const Vec3<T> &v) const {
94  return Vec3<T>(x*v.x, y*v.y, z*v.z);
95  }
96 
99  double n = norm();
100  (*this) /= n;
101  return (*this);
102  }
103 
105  double norm() const {
106  return std::sqrt(double(x*x+y*y+z*z));
107  }
108 
110  double norm2() const {
111  return x*x+y*y+z*z;
112  }
113 
115  bool operator==(const Vec3<T> &v) const {
116  return (x==v.x && y==v.y && z==v.z);
117  }
118 
120  bool operator!=(const Vec3<T> &v) const {
121  return !((*this)==v);
122  }
123 
125  friend Vec3<T> operator*(const double &d, const Vec3<T> &v) {
126  return v*d;
127  }
128 
130  operator T() { return x; }
131 
133  friend std::ostream& operator<<(std::ostream &os, const Vec3<T> &v) {
134  os << v.x << " " << v.y<<" "<<v.z;
135  return os;
136  }
137 
138 
139 
140 };
141 
142 
143 
144 template class Vec3<float>;
145 template class Vec3<double>;
146 template class Vec3<int>;
147 
150 typedef Vec3<int> Vec3i;
151 
152 #endif
Vec3(T _x, T _y, T _z)
Constructor accepting three values, one for each vector component.
Definition: Vector3.h:20
Vec3< float > Vec3f
Definition: Vector3.h:148
T z
Definition: Vector3.h:13
Vec3< T > operator/(const double &d) const
Division with double.
Definition: Vector3.h:66
bool operator!=(const Vec3< T > &v) const
Not equal (used for integer position vectors)
Definition: Vector3.h:120
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:93
Vec3< T > & operator-=(const Vec3< T > &v)
In-place vector subtraction.
Definition: Vector3.h:45
Vec3< T > cross(const Vec3< T > &v) const
Cross product.
Definition: Vector3.h:84
Vec3< int > Vec3i
Definition: Vector3.h:150
T x
Definition: Vector3.h:11
Vec3< T > & operator/=(const double &d)
In-place division with double.
Definition: Vector3.h:71
Vec3< T > operator*(const double &d) const
Multiplication with double.
Definition: Vector3.h:53
friend Vec3< T > operator*(const double &d, const Vec3< T > &v)
Multiplication with double.
Definition: Vector3.h:125
Vec3< T > & operator*=(const double &d)
In-place multiplication with double.
Definition: Vector3.h:58
double operator*(const Vec3< T > &v) const
Scalar product.
Definition: Vector3.h:79
Vec3< T > & operator+=(const Vec3< T > &v)
In-place vector addition.
Definition: Vector3.h:32
double norm() const
Vector norm.
Definition: Vector3.h:105
double norm2() const
Vector norm squared.
Definition: Vector3.h:110
Vec3(Vec3< T2 > v)
Copy constructor.
Definition: Vector3.h:24
Vec3< T > operator+(const Vec3< T > &v) const
Vector addition.
Definition: Vector3.h:27
Vec3< double > Vec3d
Definition: Vector3.h:149
Vec3< T > operator-(const Vec3< T > &v) const
Vector subtraction.
Definition: Vector3.h:40
Vec3(T value=0)
Constructor accepting a single value, which defaults to 0. All three components are set to the same v...
Definition: Vector3.h:17
T y
Definition: Vector3.h:12
bool operator==(const Vec3< T > &v) const
Equal (used for integer position vectors)
Definition: Vector3.h:115
Vec3< T > & normalize()
Normalize.
Definition: Vector3.h:98