MercuryDPM  Beta
 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 
39 #include "GeneralDefine.h"
40 
45 class Vec3D
46 {
47 public:
48 
52  Mdouble X, Y, Z;
53 
57  Vec3D();
58 
62  Vec3D(const Mdouble x, const Mdouble y, const Mdouble z);
63 
67  void setZero();
68 
72  bool isZero() const;
73 
77  Vec3D operator +(const Vec3D& a) const;
78 
82  Vec3D operator -(const Vec3D& a) const;
83 
87  Vec3D operator +(const Mdouble a) const;
88 
92  Vec3D operator -(const Mdouble a) const;
93 
97  Vec3D operator *(const Mdouble a) const;
98 
102  Vec3D operator /(const Mdouble a) const;
103 
107  Vec3D& operator+=(const Vec3D& a);
108 
112  Vec3D& operator-=(const Vec3D& a);
113 
117  Vec3D& operator*=(const Mdouble a);
118 
122  Vec3D& operator/=(const Mdouble a);
123 
127  static Mdouble dot(const Vec3D& a, const Vec3D& b);
128 
132  static Vec3D max(const Vec3D& a, const Vec3D& b);
133 
137  static Vec3D min(const Vec3D& a, const Vec3D& b);
138 
142  static Vec3D square(const Vec3D& a);
143 
147  void normalize();
148 
152  void setLength(Mdouble length);
153 
157  static Vec3D sqrt(const Vec3D& a);
158 
162  static Vec3D cross(const Vec3D& a, const Vec3D& b);
163 
167  static Mdouble getDistance(const Vec3D& a, const Vec3D& b);
168 
172  static Mdouble getDistanceSquared(const Vec3D& a, const Vec3D& b);
173 
177  static Mdouble getLength(const Vec3D& a);
178 
182  static Mdouble getLengthSquared(const Vec3D& a);
183 
187  Mdouble getLength() const;
188 
192  Mdouble getLengthSquared() const;
193 
197  Mdouble getComponent(const int index) const;
198 
202  void setComponent(const int index, const double val);
203 
208 
213 
217  bool isEqualTo(const Vec3D& other, const double tol) const;
218 
219 // void ConvertToCylindricalCoordinates()
220 // {
221 // double R = sqrt(X*X+Y*Y); Y = atan2(Y,X); X = R; return;
222 // }
223 //
224 // void ConvertFromCylindricalCoordinates()
225 // {
226 // double Xnew = X * cos(Y); Y = X * sin(Y); X = Xnew; return;
227 // }
228 
232  static Vec3D getUnitVector(const Vec3D& a);
233 
237  friend std::ostream& operator<<(std::ostream& os, const Vec3D& a);
238 
242  friend std::istream& operator>>(std::istream& is, Vec3D& a);
243 
247  friend Vec3D operator+(const Mdouble a, const Vec3D& b);
248 
252  friend Vec3D operator-(const Mdouble a, const Vec3D& b);
253 
257  friend Vec3D operator-(const Vec3D& a);
258 
262  friend Vec3D operator*(const Mdouble a, const Vec3D& b);
263 };
264 
265 #endif
Vec3D getCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:367
Mdouble X
the vector components
Definition: Vector.h:52
static Vec3D getUnitVector(const Vec3D &a)
Returns a unit Vec3D based on a.
Definition: Vector.cc:441
Vec3D()
constructor
Definition: Vector.cc:31
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cartesian coordinates.
Definition: Vector.cc:376
void normalize()
Makes this Vec3D unit length.
Definition: Vector.cc:234
void setLength(Mdouble length)
Make this Vec3D a certain length.
Definition: Vector.cc:244
Vec3D operator*(const Mdouble a) const
Multiplies by a scalar.
Definition: Vector.cc:113
double Mdouble
Vec3D & operator-=(const Vec3D &a)
Subtracts another vector.
Definition: Vector.cc:146
Vec3D & operator+=(const Vec3D &a)
Adds another vector.
Definition: Vector.cc:133
void setZero()
Sets all elements to zero.
Definition: Vector.cc:52
static Vec3D sqrt(const Vec3D &a)
Calculates the pointwise square root of a Vec3D.
Definition: Vector.cc:256
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:187
Vec3D operator/(const Mdouble a) const
Divides by a scalar.
Definition: Vector.cc:123
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:225
bool isEqualTo(const Vec3D &other, const double tol) const
Checks if the length this Vec3D is equal the length of other with a certain tolerance.
Definition: Vector.cc:390
bool isZero() const
Checks if ALL elements are zero.
Definition: Vector.cc:63
Mdouble getLengthSquared() const
Calculates the squared length of this Vec3D: .
Definition: Vector.cc:313
Vec3D operator+(const Vec3D &a) const
Adds another vector.
Definition: Vector.cc:73
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:213
friend std::istream & operator>>(std::istream &is, Vec3D &a)
Adds elements to an input stream.
Definition: Vector.cc:470
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:280
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:268
void setComponent(const int index, const double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:345
Mdouble Y
Definition: Vector.h:52
Vec3D & operator/=(const Mdouble a)
Divides by a scalar.
Definition: Vector.cc:172
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:416
static Mdouble getDistanceSquared(const Vec3D &a, const Vec3D &b)
Calculates the squared distance between two Vec3D: .
Definition: Vector.cc:293
Vec3D operator-(const Vec3D &a) const
Subtracts another vector.
Definition: Vector.cc:83
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
Mdouble Z
Definition: Vector.h:52
Mdouble getComponent(const int index) const
Returns the requested component of this Vec3D.
Definition: Vector.cc:323
friend std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Adds elements to an output stream.
Definition: Vector.cc:457
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:200
Vec3D & operator*=(const Mdouble a)
Multiplies by a scalar.
Definition: Vector.cc:159