i6engine  1.0
i6eVector.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_MATH_I6EVECTOR_H__
26 #define __I6ENGINE_MATH_I6EVECTOR_H__
27 
28 #include <map>
29 #include <string>
30 
32 
33 namespace Ogre {
34  class Vector2;
35  class Vector3;
36 } /* namespace Ogre */
37 class btVector3;
38 
39 namespace i6e {
40 namespace math {
41 
49  static const double EPSILON;
50 
51  public:
52  static const i6eVector ZERO;
53 
57  i6eVector() : _x(0.0), _y(0.0), _z(0.0), _valid(false) {
58  }
59 
63  i6eVector(const double x, const double y, const double z) : _x(x), _y(y), _z(z), _valid(true) {
64  }
65 
71  explicit i6eVector(const Ogre::Vector3 & ogre);
72 
78  explicit i6eVector(const btVector3 & bullet);
79 
80  /*
81  * \brief Constructs a vector from a std::string
82  *
83  * is std::string matches the format "x y z"
84  */
85  explicit i6eVector(const std::string & s);
86 
87  /*
88  * \brief Constructs a vector from an attributeMap
89  *
90  * \param[in] params the attributeMap containing informations for the vector
91  * \param[in] prefix prefix of the values
92  */
93  i6eVector(const std::map<std::string, std::string> & params, const std::string & prefix);
94 
99  }
100 
104  double getX() const {
105  return _x;
106  }
107  double getY() const {
108  return _y;
109  }
110  double getZ() const {
111  return _z;
112  }
113 
117  void setX(const double x) {
118  _x = x;
119  }
120  void setY(const double y) {
121  _y = y;
122  }
123  void setZ(const double z) {
124  _z = z;
125  }
126 
130  i6eVector operator+(const i6eVector & b) const {
131  return i6eVector(getX() + b.getX(), getY() + b.getY(), getZ() + b.getZ());
132  }
133 
137  i6eVector operator-(const i6eVector & b) const {
138  return i6eVector(getX() - b.getX(), getY() - b.getY(), getZ() - b.getZ());
139  }
140 
144  i6eVector operator*(const i6eVector & b) const {
145  return i6eVector(_y * b._z - _z * b._y, _z * b._x - _x * b._z, _x * b._y - _y * b._x);
146  }
147 
151  i6eVector operator*(const double b) const {
152  return i6eVector(_x * b, _y * b, _z * b);
153  }
154 
158  static double scalProd(const i6eVector & a, const i6eVector & b) {
159  return a._x * b._x + a._y * b._y + a._z * b._z;
160  }
161 
165  i6eVector operator/(const double b) const {
166  return i6eVector(getX() / b, getY() / b, getZ() / b);
167  }
168 
173  setX(getX() + b.getX());
174  setY(getY() + b.getY());
175  setZ(getZ() + b.getZ());
176  return *this;
177  }
178 
183  setX(_x - b.getX());
184  setY(_y - b.getY());
185  setZ(_z - b.getZ());
186  return *this;
187  }
188 
192  bool operator==(const i6eVector & b) const;
193 
197  bool operator!=(const i6eVector & b) const {
198  return !(*this == b);
199  }
200 
204  i6eVector normalize() const;
205 
209  static i6eVector crossProd(const i6eVector & a, const i6eVector & b) {
210  return i6eVector(a._y * b._z - a._z * b._y, a._z * b._x - a._x * b._z, a._x * b._y - a._y * b._x);
211  }
212 
219  static double crossAngle(const i6eVector & a, const i6eVector & b);
220 
228  static double crossAngleSigned(const i6eVector & a, const i6eVector & b, const i6eVector & n);
229 
233  void insertInMap(const std::string & prefix, std::map<std::string, std::string> & map) const;
234 
239  void mulComponents(const i6eVector & b) {
240  _x = _x * b.getX();
241  _y = _y * b.getY();
242  _z = _z * b.getZ();
243  }
244 
250  bool isValid() const;
251  void setValid(bool b);
252 
256  double length() const;
257 
263  Ogre::Vector3 toOgre() const;
264 
270  Ogre::Vector2 toOgre2() const;
271 
277  btVector3 toBullet() const;
278 
284  std::string toString() const;
285 
289  template<class Archive>
290  void serialize(Archive & ar, const unsigned int) {
291  ar & _x;
292  ar & _y;
293  ar & _z;
294  }
295 
296  private:
297  double _x, _y, _z;
298  bool _valid;
299  };
300 
304  ISIXE_MATH_API std::ostream & operator<<(std::ostream & stream, const i6eVector & v);
305 
306 } /* namespace math */
307 } /* namespace i6e */
308 
310 
311 #endif /* __I6ENGINE_MATH_I6EVECTOR_H__ */
312 
bool operator!=(const i6eVector &b) const
Operator '!=' for Vector.
Definition: i6eVector.h:197
i6e::math::i6eVector Vec3
Definition: i6eVector.h:309
void serialize(Archive &ar, const unsigned int)
serializer for the vector
Definition: i6eVector.h:290
i6eVector operator/(const double b) const
Operator '/' for Vectors with a scalar.
Definition: i6eVector.h:165
std::enable_if< std::is_integral< T >::value, bool >::type operator==(const i6eVector2< T > &first, const i6eVector2< T > &second)
Definition: i6eVector2.h:261
static double scalProd(const i6eVector &a, const i6eVector &b)
operator '*' for vectors
Definition: i6eVector.h:158
#define ISIXE_MATH_API
~i6eVector()
destructor
Definition: i6eVector.h:98
double getY() const
Definition: i6eVector.h:107
static i6eVector crossProd(const i6eVector &a, const i6eVector &b)
Operator '*' for Vectors (cross product)
Definition: i6eVector.h:209
i6eVector operator*(const double b) const
operator '*' for scalars
Definition: i6eVector.h:151
Implements 3-dimensional vectors.
Definition: i6eVector.h:48
void setZ(const double z)
Definition: i6eVector.h:123
void setY(const double y)
Definition: i6eVector.h:120
i6eVector operator-(const i6eVector &b) const
Operator '-' for Vectors.
Definition: i6eVector.h:137
i6eVector()
Creates a new vector with all values set to 0.
Definition: i6eVector.h:57
static const i6eVector ZERO
Definition: i6eVector.h:52
i6eVector operator+(const i6eVector &b) const
Operator '+' for Vectors.
Definition: i6eVector.h:130
i6eVector(const double x, const double y, const double z)
Creates a new vector with all values set to the given values.
Definition: i6eVector.h:63
i6eVector operator*(const i6eVector &b) const
operator '*' for vectors
Definition: i6eVector.h:144
void mulComponents(const i6eVector &b)
muliplies two Vectors per component
Definition: i6eVector.h:239
i6eVector operator-=(const i6eVector &b)
Operator '-=' for Vectors.
Definition: i6eVector.h:182
double getX() const
getters for the values of the Vector
Definition: i6eVector.h:104
double getZ() const
Definition: i6eVector.h:110
ISIXE_MATH_API std::ostream & operator<<(std::ostream &stream, const i6eQuaternion &q)
stream operator for quaternion
void setX(const double x)
setters for the values of the Vector
Definition: i6eVector.h:117
i6eVector operator+=(const i6eVector &b)
Operator '+=' for Vectors.
Definition: i6eVector.h:172