MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
28 #ifndef VECTOR_H
32 #define VECTOR_H
33 
34 #include <cmath>
35 #include <sstream>
36 #include <iostream>
37 #include <stdlib.h>
38 #include "ExtendedMath.h"
39 
40 class Vec3D
41 {
42 public:
43 
44  Mdouble X, Y, Z;
45 
46  inline Vec3D(void){
47  set_zero();
48  }
49 
50  inline Vec3D (const Mdouble x, const Mdouble y, const Mdouble z)
51  {
52  X = x; Y = y; Z = z;
53  }
54 
55  inline void set_zero ()
56  {
57  X = 0.0; Y = 0.0; Z = 0.0;
58  }
59 
60  inline bool is_zero ()
61  {
62  return X == 0.0 && Y == 0.0 && Z == 0.0;
63  }
64 
65  inline Vec3D operator + (const Vec3D &A) const
66  {
67  return Vec3D(X + A.X, Y + A.Y, Z + A.Z);
68  }
69 
70  inline Vec3D operator - (const Vec3D &A) const
71  {
72  return Vec3D(X - A.X, Y - A.Y, Z - A.Z);
73  }
74 
75  inline Vec3D operator + (const Mdouble A) const
76  {
77  return Vec3D(X + A, Y + A, Z + A);
78  }
79 
80  inline Vec3D operator - (const Mdouble A) const
81  {
82  return Vec3D(X - A, Y - A, Z - A);
83  }
84 
85  inline Vec3D operator * (const Mdouble A) const
86  {
87  return Vec3D(X * A, Y * A, Z * A);
88  }
89 
90  inline Vec3D operator / (const Mdouble A) const
91  {
92  return Vec3D(X / A, Y / A, Z / A);
93  }
94 
95  inline Vec3D& operator+=(const Vec3D &A)
96  {
97  X += A.X;
98  Y += A.Y;
99  Z += A.Z;
100  return *this;
101  }
102 
103  inline Vec3D& operator-=(const Vec3D &A)
104  {
105  X -= A.X;
106  Y -= A.Y;
107  Z -= A.Z;
108  return *this;
109  }
110 
111  inline Vec3D& operator*=(const Mdouble a)
112  {
113  X *= a;
114  Y *= a;
115  Z *= a;
116  return *this;
117  }
118 
119  inline Vec3D& operator/=(const Mdouble a)
120  {
121  X /= a;
122  Y /= a;
123  Z /= a;
124  return *this;
125  }
126 
127  // Dot product
128  friend inline Mdouble Dot(const Vec3D &A, const Vec3D &B)
129  {
130  return A.X * B.X + A.Y * B.Y + A.Z * B.Z;
131  }
132 
133  // Piecewise max
134  friend inline Vec3D max(const Vec3D &A, const Vec3D &B)
135  {
136  return Vec3D(std::max(A.X,B.X), std::max(A.Y,B.Y), std::max(A.Z,B.Z));
137  }
138 
139  // Piecewise min
140  friend inline Vec3D min(const Vec3D &A, const Vec3D &B)
141  {
142  return Vec3D(std::min(A.X,B.X), std::min(A.Y,B.Y), std::min(A.Z,B.Z));
143  }
144 
145  // Pointwise square
146  friend inline Vec3D square(const Vec3D &A)
147  {
148  return Vec3D(A.X * A.X, A.Y * A.Y, A.Z * A.Z);
149  }
150 
151  // make this vector unit length
152  inline void normalize() {
153  *this /= this->GetLength();
154  }
155 
156  // make this vector unit length
157  inline void SetLength(Mdouble length) {
158  *this /= this->GetLength()*length;
159  }
160 
161  // Pointwise square root
162  friend inline Vec3D sqrt(const Vec3D &A)
163  {
164  return Vec3D(sqrt(A.X), sqrt(A.Y), sqrt(A.Z));
165  }
166 
167  // Cross product
168  friend inline Vec3D Cross(const Vec3D &A, const Vec3D &B)
169  {
170  return Vec3D(A.Y*B.Z-A.Z*B.Y, A.Z*B.X-A.X*B.Z, A.X*B.Y-A.Y*B.X);
171  }
172 
173  friend inline Mdouble GetDistance(const Vec3D &A, const Vec3D &B)
174  {
175  return sqrt(GetDistance2(A, B));
176  }
177 
178  friend inline Mdouble GetDistance2(const Vec3D &A, const Vec3D &B)
179  {
180  return ((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y) + (A.Z - B.Z) * (A.Z - B.Z));
181  }
182 
183  friend Mdouble GetLength2(const Vec3D &A)
184  {
185  return (A.X * A.X + A.Y * A.Y + A.Z * A.Z);
186  }
187 
189  {
190  return (X * X + Y * Y + Z * Z);
191  }
192 
193  Mdouble getComponent(const int index) const
194  {
195  switch(index)
196  {
197  case 0:
198  return X;
199  case 1:
200  return Y;
201  case 2:
202  return Z;
203  default:
204  std::cerr<<"Index="<<index<<" is to high for 3D vector"<<std::endl;
205  exit(-1);
206  }
207  }
208 
209  void setComponent(int index,double val)
210  {
211  switch(index)
212  {
213  case 0:
214  X=val;
215  return;
216  case 1:
217  Y=val;
218  return;
219  case 2:
220  Z=val;
221  return;
222  default:
223  std::cerr<<"Index="<<index<<" is to high for 3D vector"<<std::endl;
224  exit(-1);
225  }
226  }
227 
229  {
230  return Vec3D(sqrt(X * X + Y * Y), atan2(Y, X), Z);
231  }
232 
234  {
235  return Vec3D(X * cos(Y), X * sin(Y), Z);
236  }
237 
238 // void ConvertToCylindricalCoordinates()
239 // {
240 // double R = sqrt(X*X+Y*Y); Y = atan2(Y,X); X = R; return;
241 // }
242 //
243 // void ConvertFromCylindricalCoordinates()
244 // {
245 // double Xnew = X * cos(Y); Y = X * sin(Y); X = Xnew; return;
246 // }
247 
248  inline Mdouble GetLength() const
249  {
250  return sqrt(GetLength2());
251  }
252 
253  friend inline Mdouble GetLength(const Vec3D &A)
254  {
255  return A.GetLength();
256  }
257 
258  friend inline Vec3D GetUnitVector(const Vec3D &A)
259  {
260  Mdouble Length2 = A.GetLength2();
261  if (Length2) return A/sqrt(Length2);
262  else return Vec3D(0,0,0);
263  }
264 
265  friend inline std::ostream& operator<<(std::ostream& os, const Vec3D &A)
266  {
267  os << A.X << ' ' << A.Y << ' ' << A.Z;
268  return os;
269  }
270 
271  friend inline std::istream& operator>>(std::istream& is, Vec3D &A)
272  {
273  is >> A.X >> A.Y >> A.Z;
274  return is;
275  }
276 
277  friend inline Vec3D operator+ (const Mdouble& a, const Vec3D &A) {return Vec3D(A.X + a, A.Y + a, A.Z + a);}
278  friend inline Vec3D operator- (const Mdouble& a, const Vec3D &A) {return Vec3D(A.X - a, A.Y - a, A.Z - a);}
279  friend inline Vec3D operator- (const Vec3D &A) {return Vec3D(-A.X, -A.Y, -A.Z);}
280  friend inline Vec3D operator* (const Mdouble& a, const Vec3D &A) {return Vec3D(A.X * a, A.Y * a, A.Z * a);}
281 
282 };
283 
284 #endif
Mdouble X
Definition: Vector.h:44
friend Vec3D square(const Vec3D &A)
Definition: Vector.h:146
bool is_zero()
Definition: Vector.h:60
friend Vec3D GetUnitVector(const Vec3D &A)
Definition: Vector.h:258
Vec3D operator/(const Mdouble A) const
Definition: Vector.h:90
void normalize()
Definition: Vector.h:152
Vec3D operator-(const Vec3D &A) const
Definition: Vector.h:70
Vec3D & operator*=(const Mdouble a)
Definition: Vector.h:111
Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
Definition: Vector.h:50
friend Mdouble Dot(const Vec3D &A, const Vec3D &B)
Definition: Vector.h:128
Vec3D operator*(const Mdouble A) const
Definition: Vector.h:85
friend std::istream & operator>>(std::istream &is, Vec3D &A)
Definition: Vector.h:271
friend Mdouble GetLength2(const Vec3D &A)
Definition: Vector.h:183
void setComponent(int index, double val)
Definition: Vector.h:209
friend Vec3D Cross(const Vec3D &A, const Vec3D &B)
Definition: Vector.h:168
Vec3D GetFromCylindricalCoordinates()
Definition: Vector.h:233
Vec3D & operator/=(const Mdouble a)
Definition: Vector.h:119
friend std::ostream & operator<<(std::ostream &os, const Vec3D &A)
Definition: Vector.h:265
Vec3D & operator-=(const Vec3D &A)
Definition: Vector.h:103
friend Mdouble GetLength(const Vec3D &A)
Definition: Vector.h:253
void SetLength(Mdouble length)
Definition: Vector.h:157
double Mdouble
Definition: ExtendedMath.h:33
friend Vec3D min(const Vec3D &A, const Vec3D &B)
Definition: Vector.h:140
Vec3D operator+(const Vec3D &A) const
Definition: Vector.h:65
Vec3D & operator+=(const Vec3D &A)
Definition: Vector.h:95
Mdouble GetLength2() const
Definition: Vector.h:188
friend Mdouble GetDistance2(const Vec3D &A, const Vec3D &B)
Definition: Vector.h:178
friend Mdouble GetDistance(const Vec3D &A, const Vec3D &B)
Definition: Vector.h:173
Mdouble Y
Definition: Vector.h:44
void set_zero()
Definition: Vector.h:55
Vec3D GetCylindricalCoordinates() const
Definition: Vector.h:228
friend Vec3D max(const Vec3D &A, const Vec3D &B)
Definition: Vector.h:134
friend Vec3D sqrt(const Vec3D &A)
Definition: Vector.h:162
Vec3D(void)
Definition: Vector.h:46
Mdouble GetLength() const
Definition: Vector.h:248
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:40
Mdouble Z
Definition: Vector.h:44
Mdouble getComponent(const int index) const
Definition: Vector.h:193