Vector3D.cpp
1 #include <cmath>
2 #include <math.h>
3 #include "Vector3D.hpp"
4 
5 #define EPSILON 1e-12
6 
7 using namespace std;
8 
9 namespace
10 {
11  static inline double my_round(double val)
12  {
13  return floor(val + 0.5);
14  }
15 }
16 
17 Vector3D RotateX(Vector3D const& v, double a)
18 {
19  Vector3D res;
20  res.x = v.x;
21  res.y = v.y*cos(a) - v.z*sin(a);
22  res.z = v.y*sin(a) + v.z*cos(a);
23  return res;
24 }
25 
26 Vector3D RotateY(Vector3D const& v, double a)
27 {
28  Vector3D res;
29  res.x = v.x*cos(a) + v.z*sin(a);
30  res.y = v.y;
31  res.z = -v.x*sin(a) + v.z*cos(a);
32  return res;
33 }
34 
35 Vector3D RotateZ(Vector3D const& v, double a)
36 {
37  Vector3D res;
38  res.x = v.x*cos(a) - v.y*sin(a);
39  res.y = v.x*sin(a) + v.y*cos(a);
40  res.z = v.z;
41  return res;
42 }
43 
44 double abs(Vector3D const& v)
45 {
46  return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
47 }
48 
50 x(0), y(0), z(0) {}
51 
52 Vector3D::Vector3D(double ix, double iy, double iz) :
53 x(ix), y(iy), z(iz) {}
54 
56 x(v.x), y(v.y), z(v.z) {}
57 
58 void Vector3D::Set(double ix, double iy, double iz)
59 {
60  x = ix;
61  y = iy;
62  z = iz;
63 }
64 
66 {
67  if (this == &v)
68  return *this;
69  x = v.x;
70  y = v.y;
71  z = v.z;
72  return *this;
73 }
74 
76 {
77  x *= s;
78  y *= s;
79  z *= s;
80  return *this;
81 }
82 
84 {
85  x += v.x;
86  y += v.y;
87  z += v.z;
88  return *this;
89 }
90 
92 {
93  x -= v.x;
94  y -= v.y;
95  z -= v.z;
96  return *this;
97 }
98 
99 // Note - since working with double precision, two vectors are assumed to be "equal",
100 // if their coordinates agree up to precision EPSILON
102 {
103  return (abs(x - v.x) < EPSILON) && (abs(y - v.y) < EPSILON) && (abs(z - v.z) < EPSILON);
104 }
105 
106 void Vector3D::RotateX(double a)
107 {
108  Vector3D v;
109  v.x = x;
110  v.y = y*cos(a) - z*sin(a);
111  v.z = y*sin(a) + z*cos(a);
112 
113  *this = v;
114 }
115 
116 void Vector3D::RotateY(double a)
117 {
118  Vector3D v;
119  v.x = x*cos(a) + z*sin(a);
120  v.y = y;
121  v.z = -x*sin(a) + z*cos(a);
122 
123  *this = v;
124 }
125 
126 void Vector3D::RotateZ(double a)
127 {
128  Vector3D v;
129  v.x = x*cos(a) - y*sin(a);
130  v.y = x*sin(a) + y*cos(a);
131  v.z = z;
132 
133  *this = v;
134 }
135 
137 {
138  x = my_round(x);
139  y = my_round(y);
140  z = my_round(z);
141 }
142 
143 Vector3D operator+(Vector3D const& v1, Vector3D const& v2)
144 {
145  Vector3D res;
146  res.x = v1.x + v2.x;
147  res.y = v1.y + v2.y;
148  res.z = v1.z + v2.z;
149  return res;
150 }
151 
153  Vector3D const& v2)
154 {
155  Vector3D res;
156  res.x = v1.x - v2.x;
157  res.y = v1.y - v2.y;
158  res.z = v1.z - v2.z;
159  return res;
160 }
161 
162 Vector3D operator*(double d, Vector3D const& v)
163 {
164  Vector3D res;
165  res.x = v.x * d;
166  res.y = v.y * d;
167  res.z = v.z * d;
168  return res;
169 }
170 
171 Vector3D operator*(Vector3D const& v, double d)
172 {
173  return d*v;
174 }
175 
176 Vector3D operator/(Vector3D const& v, double d)
177 {
178  Vector3D res;
179  res.x = v.x / d;
180  res.y = v.y / d;
181  res.z = v.z / d;
182  return res;
183 }
184 
185 double ScalarProd(Vector3D const& v1,
186  Vector3D const& v2)
187 {
188  return v1.x*v2.x +
189  v1.y*v2.y +
190  v1.z*v2.z;
191 }
192 
193 double Projection(Vector3D const& v1, Vector3D const& v2)
194 {
195  return ScalarProd(v1, v2) / abs(v2);
196 }
197 
198 double CalcAngle(Vector3D const& v1, Vector3D const& v2)
199 {
200  return acos(ScalarProd(v1, v2) / abs(v1) / abs(v2));
201 }
202 
203 Vector3D Reflect(Vector3D const& v, Vector3D const& normal)
204 {
205  return v - 2 * ScalarProd(v, normal)*normal / pow(abs(normal), 2);
206 }
207 
208 double distance(Vector3D const& v1, Vector3D const& v2)
209 {
210  return abs(v1 - v2);
211 }
212 
213 Vector3D CrossProduct(Vector3D const& v1, Vector3D const& v2)
214 {
215  double x = v1.y*v2.z - v1.z*v2.y;
216  double y = v1.z*v2.x - v1.x*v2.z;
217  double z = v1.x*v2.y - v1.y*v2.x;
218 
219  return Vector3D(x, y, z);
220 }
221 
222 void Split(vector<Vector3D> const & vIn, vector<double> & vX, vector<double> & vY, vector<double> & vZ)
223 {
224  vX.resize(vIn.size());
225  vY.resize(vIn.size());
226  vZ.resize(vIn.size());
227 
228  for (size_t ii = 0; ii < vIn.size(); ++ii)
229  {
230  vX[ii] = vIn[ii].x;
231  vY[ii] = vIn[ii].y;
232  vZ[ii] = vIn[ii].z;
233  }
234  return;
235 }
void RotateY(double a)
Rotates the vector around the Y axes.
Definition: Vector3D.cpp:116
Vector3D RotateZ(Vector3D const &v, double a)
Rotates a 3D-vector around the Z axis.
Definition: Vector3D.cpp:35
Vector3D CrossProduct(Vector3D const &v1, Vector3D const &v2)
Returns the cross product of two vectors.
Definition: Vector3D.cpp:213
double z
Component in the z direction.
Definition: Vector3D.hpp:50
Vector3D & operator-=(Vector3D const &v)
Subtraction.
Definition: Vector3D.cpp:91
Vector3D & operator*=(double s)
Scalar product.
Definition: Vector3D.cpp:75
Vector3D RotateX(Vector3D const &v, double a)
Rotates a 3D-vector around the X axis.
Definition: Vector3D.cpp:17
Vector3D & operator+=(Vector3D const &v)
Addition.
Definition: Vector3D.cpp:83
3D Geometrical calculations
Vector3D Reflect(Vector3D const &v, Vector3D const &normal)
Reflect vector.
Definition: Vector3D.cpp:203
double Projection(Vector3D const &v1, Vector3D const &v2)
Calculates the projection of one vector in the direction of the second.
Definition: Vector3D.cpp:193
double CalcAngle(Vector3D const &v1, Vector3D const &v2)
Returns the angle between two vectors (in radians)
Definition: Vector3D.cpp:198
Vector3D RotateY(Vector3D const &v, double a)
Rotates a 3D-vector around the Y axis.
Definition: Vector3D.cpp:26
void RotateX(double a)
Rotates the vector around the X axes.
Definition: Vector3D.cpp:106
void Set(double ix, double iy, double iz)
Set vector components.
Definition: Vector3D.cpp:58
Vector3D(void)
Null constructor.
Definition: Vector3D.cpp:49
Vector3D & operator=(Vector3D const &v)
Assignment operator.
Definition: Vector3D.cpp:65
3D Mathematical vector
Definition: Vector3D.hpp:15
Vector3D operator*(double d, Vector3D const &v)
Scalar product.
Definition: Vector3D.cpp:162
double ScalarProd(Vector3D const &v1, Vector3D const &v2)
Scalar product of two vectors.
Definition: Vector3D.cpp:185
double y
Component in the y direction.
Definition: Vector3D.hpp:47
Vector3D operator/(Vector3D const &v, double d)
Scalar division.
Definition: Vector3D.cpp:176
void RotateZ(double a)
Rotates the vector around the Z axes.
Definition: Vector3D.cpp:126
void Round()
Integer round of the vector&#39;s entries.
Definition: Vector3D.cpp:136
void Split(vector< Vector3D > const &vIn, vector< double > &vX, vector< double > &vY, vector< double > &vZ)
Splits a vector of 3D points to components.
Definition: Vector3D.cpp:222
double abs(Vector3D const &v)
Norm of a vector.
Definition: Vector3D.cpp:44
Vector3D operator+(Vector3D const &v1, Vector3D const &v2)
Term by term addition.
Definition: Vector3D.cpp:143
double x
Component in the x direction.
Definition: Vector3D.hpp:44
double distance(Vector3D const &v1, Vector3D const &v2)
Calculates the distance between two vectors.
Definition: Vector3D.cpp:208
Vector3D operator-(Vector3D const &v1, Vector3D const &v2)
Term by term subtraction.
Definition: Vector3D.cpp:152
bool operator==(Vector3D const &v)
Compare 3D-Vectors (up to an arbitrary precision)
Definition: Vector3D.cpp:101