i6engine  1.0
i6eVector2.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_I6EVECTOR2_H__
26 #define __I6ENGINE_MATH_I6EVECTOR2_H__
27 
28 #include <cfloat>
29 #include <cmath>
30 #include <cstdint>
31 #include <map>
32 #include <string>
33 
36 
37 namespace i6e {
38 namespace math {
39 
46  template<typename T>
47  class i6eVector2 {
48  static const double EPSILON;
49 
50  public:
51  static const i6eVector2 ZERO;
52 
56  i6eVector2() : _x(T()), _y(T()), _valid(false) {
57  }
58 
62  i6eVector2(const T x, const T y) : _x(x), _y(y), _valid(true) {
63  }
64 
65  /*
66  * \brief Constructs a vector from a std::string
67  *
68  * is std::string matches the format "x y"
69  */
70  explicit i6eVector2(const std::string & s) : _x(), _y(), _valid(true) {
71  std::stringstream stream(s);
72  stream >> _x >> _y;
73  }
74 
75  /*
76  * \brief Constructs a vector from an attributeMap
77  *
78  * \param[in] params the attributeMap containing informations for the vector
79  * \param[in] prefix prefix of the values
80  */
81  i6eVector2(const std::map<std::string, std::string> & params, const std::string & prefix) : _x(), _y(), _valid(true) {
82  std::stringstream stream(params.find(prefix)->second);
83  stream >> _x >> _y;
84  }
85 
90  }
91 
95  T getX() const {
96  return _x;
97  }
98  T getY() const {
99  return _y;
100  }
101 
105  void setX(const T x) {
106  _x = x;
107  }
108  void setY(const T y) {
109  _y = y;
110  }
111 
115  i6eVector2 operator+(const i6eVector2 & b) const {
116  return i6eVector2(getX() + b.getX(), getY() + b.getY());
117  }
118 
122  i6eVector2 operator-(const i6eVector2 & b) const {
123  return i6eVector2(getX() - b.getX(), getY() - b.getY());
124  }
125 
129  i6eVector2 operator*(const T b) const {
130  return i6eVector2(_x * b, _y * b);
131  }
132 
137  _x *= d;
138  _y *= d;
139  return *this;
140  }
141 
145  static T scalProd(const i6eVector2 & a, const i6eVector2 & b) {
146  return a._x * b._x + a._y * b._y;
147  }
148 
152  i6eVector2 operator/(const T b) const {
153  return i6eVector2(getX() / b, getY() / b);
154  }
155 
160  setX(getX() + b.getX());
161  setY(getY() + b.getY());
162  return *this;
163  }
164 
169  setX(_x - b.getX());
170  setY(_y - b.getY());
171  return *this;
172  }
173 
177  bool operator!=(const i6eVector2 & b) const {
178  return !(*this == b);
179  }
180 
185  if (length() < EPSILON) {
186  ISIXE_THROW_API("i6eVector2", "Tried to normalize a (0, 0) vector");
187  }
188  return i6eVector2(*this / length());
189  }
190 
194  void insertInMap(const std::string & prefix, std::map<std::string, std::string> & map) const {
195  std::stringstream ss;
196  ss << _x << " " << _y;
197  map[prefix] = ss.str();
198  }
199 
204  void mulComponents(const i6eVector2 & b) {
205  _x = _x * b.getX();
206  _y = _y * b.getY();
207  }
208 
214  bool isValid() const {
215  return _valid;
216  }
217  void setValid(bool b) {
218  _valid = b;
219  }
220 
224  T length() const {
225  return T(std::sqrt(_x * _x + _y * _y));
226  }
227 
233  std::string toString() const {
234  std::stringstream s;
235  s.precision(100);
236  s << _x << " " << _y;
237  return s.str();
238  }
239 
243  template<class Archive>
244  void serialize(Archive & ar, const unsigned int) {
245  ar & _x;
246  ar & _y;
247  }
248 
249  private:
250  T _x, _y;
251  bool _valid;
252  };
253 
254  template<typename T>
255  const double i6eVector2<T>::EPSILON = 1e-15;
256 
257  template<typename T>
258  const i6eVector2<T> i6eVector2<T>::ZERO = i6eVector2<T>(T(), T());
259 
260  template<typename T>
261  typename std::enable_if<std::is_integral<T>::value, bool>::type operator==(const i6eVector2<T> & first, const i6eVector2<T> & second) {
262  return first.getX() == second.getX() && first.getY() == second.getY();
263  }
264 
265  template<typename T>
266  typename std::enable_if<std::is_floating_point<T>::value, bool>::type operator==(const i6eVector2<T> & first, const i6eVector2<T> & second) {
267  return std::fabs(first.getX() - second.getX()) < FLT_EPSILON && std::fabs(first.getY() - second.getY()) < FLT_EPSILON;
268  }
269 
270 } /* namespace math */
271 } /* namespace i6e */
272 
277 
281 ISIXE_MATH_API std::ostream & operator<<(std::ostream & stream, const Vec2 & v);
282 ISIXE_MATH_API std::ostream & operator<<(std::ostream & stream, const Vec2f & v);
283 ISIXE_MATH_API std::ostream & operator<<(std::ostream & stream, const Vec2i & v);
284 ISIXE_MATH_API std::ostream & operator<<(std::ostream & stream, const Vec2ui & v);
285 
286 #endif /* __I6ENGINE_MATH_I6EVECTOR2_H__ */
287 
i6eVector2 operator-(const i6eVector2 &b) const
Operator '-' for Vectors.
Definition: i6eVector2.h:122
ISIXE_MATH_API std::ostream & operator<<(std::ostream &stream, const Vec2 &v)
stream operator for the i6eVector2
i6e::math::i6eVector2< uint32_t > Vec2ui
Definition: i6eVector2.h:276
bool operator!=(const i6eVector2 &b) const
Operator '!=' for Vector.
Definition: i6eVector2.h:177
std::enable_if< std::is_integral< T >::value, bool >::type operator==(const i6eVector2< T > &first, const i6eVector2< T > &second)
Definition: i6eVector2.h:261
i6e::math::i6eVector2< double > Vec2
Definition: i6eVector2.h:273
i6eVector2 operator/(const T b) const
Operator '/' for Vectors with a scalar.
Definition: i6eVector2.h:152
i6eVector2 operator*=(T d)
operator '*=' for scalars
Definition: i6eVector2.h:136
Implements 2-dimensional vectors.
Definition: i6eVector2.h:47
#define ISIXE_MATH_API
i6eVector2 operator*(const T b) const
operator '*' for scalars
Definition: i6eVector2.h:129
static const i6eVector2 ZERO
Definition: i6eVector2.h:51
#define ISIXE_THROW_API(module, message)
Definition: Exceptions.h:45
i6eVector2 normalize() const
returns the normalized Vector
Definition: i6eVector2.h:184
i6e::math::i6eVector2< int32_t > Vec2i
Definition: i6eVector2.h:275
i6eVector2 operator+=(const i6eVector2 &b)
Operator '+=' for Vectors.
Definition: i6eVector2.h:159
i6eVector2(const T x, const T y)
Creates a new vector with all values set to the given values.
Definition: i6eVector2.h:62
bool isValid() const
determins whether the vector contains valid data This has to be set by the user. Operations resulting...
Definition: i6eVector2.h:214
T getX() const
getters for the values of the Vector
Definition: i6eVector2.h:95
~i6eVector2()
destructor
Definition: i6eVector2.h:89
static T scalProd(const i6eVector2 &a, const i6eVector2 &b)
operator '*' for vectors
Definition: i6eVector2.h:145
i6eVector2 operator-=(const i6eVector2 &b)
Operator '-=' for Vectors.
Definition: i6eVector2.h:168
void setX(const T x)
setters for the values of the Vector
Definition: i6eVector2.h:105
i6e::math::i6eVector2< float > Vec2f
Definition: i6eVector2.h:274
void mulComponents(const i6eVector2 &b)
muliplies two Vectors per component
Definition: i6eVector2.h:204
i6eVector2(const std::string &s)
Definition: i6eVector2.h:70
void setValid(bool b)
Definition: i6eVector2.h:217
std::string toString() const
converts Vector to std::string
Definition: i6eVector2.h:233
i6eVector2(const std::map< std::string, std::string > &params, const std::string &prefix)
Definition: i6eVector2.h:81
T length() const
length of the Vector
Definition: i6eVector2.h:224
void setY(const T y)
Definition: i6eVector2.h:108
void insertInMap(const std::string &prefix, std::map< std::string, std::string > &map) const
inserts this vector into an attributeMap using the given prefix
Definition: i6eVector2.h:194
i6eVector2 operator+(const i6eVector2 &b) const
Operator '+' for Vectors.
Definition: i6eVector2.h:115
void serialize(Archive &ar, const unsigned int)
serializer for the vector
Definition: i6eVector2.h:244
i6eVector2()
Creates a new vector with all values set to 0.
Definition: i6eVector2.h:56