MercuryDPM  Alpha
 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 
84 // /*!
85 // * \brief Adds a scalar
86 // */
87 // Vec3D operator +(const Mdouble a) const;
88 //
89 // /*!
90 // * \brief Subtracts a scalar
91 // */
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 
244  //what a stupid idea! don't implement that!
245 // /*!
246 // * \brief Adds a scalar to a vector
247 // */
248 // friend Vec3D operator+(const Mdouble a, const Vec3D& b);
249 //
250 // /*!
251 // * \brief Subtracts the elements of a vector from a scalar
252 // */
253 // friend Vec3D operator-(const Mdouble a, const Vec3D& b);
254 //
258  friend Vec3D operator-(const Vec3D& a);
259 
263  friend Vec3D operator*(const Mdouble a, const Vec3D& b);
264 };
265 
266 #endif
Vec3D getCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:354
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:428
Vec3D()
constructor
Definition: Vector.cc:31
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cartesian coordinates.
Definition: Vector.cc:363
void normalize()
Makes this Vec3D unit length.
Definition: Vector.cc:214
void setLength(Mdouble length)
Make this Vec3D a certain length.
Definition: Vector.cc:230
Vec3D operator*(const Mdouble a) const
Adds a scalar.
Definition: Vector.cc:93
double Mdouble
Vec3D & operator-=(const Vec3D &a)
Subtracts another vector.
Definition: Vector.cc:126
Vec3D & operator+=(const Vec3D &a)
Adds another vector.
Definition: Vector.cc:113
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:243
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:167
Vec3D operator/(const Mdouble a) const
Divides by a scalar.
Definition: Vector.cc:103
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:205
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:377
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:300
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:193
friend std::istream & operator>>(std::istream &is, Vec3D &a)
Adds elements to an input stream.
Definition: Vector.cc:457
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:267
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:255
void setComponent(const int index, const double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:332
Mdouble Y
Definition: Vector.h:52
Vec3D & operator/=(const Mdouble a)
Divides by a scalar.
Definition: Vector.cc:152
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:403
static Mdouble getDistanceSquared(const Vec3D &a, const Vec3D &b)
Calculates the squared distance between two Vec3D: .
Definition: Vector.cc:280
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:310
friend std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Adds elements to an output stream.
Definition: Vector.cc:444
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:180
Vec3D & operator*=(const Mdouble a)
Multiplies by a scalar.
Definition: Vector.cc:139