FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
point.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_VIDEO_POINT_H
23 #define FIFE_VIDEO_POINT_H
24 
25 // Standard C++ library includes
26 #include <iostream>
27 #include <cassert>
28 #include <vector>
29 
30 // Platform specific includes
31 
32 // 3rd party library includes
33 
34 // FIFE includes
35 // These includes are split up in two parts, separated by one empty line
36 // First block: files included from the FIFE root src directory
37 // Second block: files included from the same folder
38 #include "util/base/fife_stdint.h"
39 #include "util/math/fife_math.h"
40 
41 namespace FIFE {
42 
48  template <typename T> class PointType2D {
49  public:
50  union {
51  T val[2];
52  struct {
53  T x,y;
54  };
55  };
56 
61  explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) {
62  }
63 
66  PointType2D(const PointType2D<T>& rhs): x(rhs.x), y(rhs.y) {
67  }
68 
72  return PointType2D<T>(x + p.x, y + p.y);
73  }
74 
78  return PointType2D<T>(x - p.x, y - p.y);
79  }
80 
84  x += p.x;
85  y += p.y;
86  return *this;
87  }
88 
92  x -= p.x;
93  y -= p.y;
94  return *this;
95  }
96 
99  PointType2D<T> operator*(const T& i) const {
100  return PointType2D<T>(x * i, y * i);
101  }
102 
105  PointType2D<T> operator/(const T& i) const {
106  return PointType2D<T>(x / i, y / i);
107  }
108 
111  bool operator==(const PointType2D<T>& p) const {
112  return x == p.x && y == p.y;
113  }
114 
117  bool operator!=(const PointType2D<T>& p) const {
118  return !(x == p.x && y == p.y);
119  }
120 
123  T length() const {
124  double sq;
125  sq = x*x + y*y;
126  return static_cast<T>(Mathd::Sqrt(sq));
127  }
128 
131  void normalize() {
132  T invLength = static_cast<T>(1.0/length());
133 
134  //TODO: get rid of this static cast
135  if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
136  x = x * invLength;
137  y = y * invLength;
138  }
139  else {
140  x = 0;
141  y = 0;
142  }
143  }
144 
147  void rotate(T angle){
148  //TODO: get rid of this static cast
149  T theta = (angle * static_cast<T>(Mathd::pi()))/180;
150  T costheta = static_cast<T>(Mathd::Cos(theta));
151  T sintheta = static_cast<T>(Mathd::Sin(theta));
152 
153  T nx = x;
154  T ny = y;
155 
156  x = costheta * nx - sintheta * ny;
157  y = sintheta * nx + costheta * ny;
158  }
159 
162  void rotate(const PointType2D<T>& origin, T angle){
163  //TODO: get rid of this static cast
164  T theta = (angle * static_cast<T>(Mathd::pi()))/180;
165  T costheta = static_cast<T>(Mathd::Cos(theta));
166  T sintheta = static_cast<T>(Mathd::Sin(theta));
167 
168  T nx = x - origin.x;
169  T ny = y - origin.y;
170 
171  x = costheta * nx - sintheta * ny;
172  y = sintheta * nx + costheta * ny;
173  }
174 
177  void set(T _x, T _y) {
178  x = _x;
179  y = _y;
180  }
181 
182  inline T& operator[] (int32_t ind) {
183  assert(ind > -1 && ind < 2);
184  return val[ind];
185  }
186  };
187 
190  template<typename T>
191  std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) {
192  return os << "(" << p.x << ":" << p.y << ")";
193  }
194 
197  typedef std::vector<Point> PointVector;
198  typedef std::vector<DoublePoint> DoublePointVector;
199 
205  template <typename T> class PointType3D {
206  public:
207  union {
208  T val[3];
209  struct {
210  T x,y,z;
211  };
212  };
213 
218  explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) {
219  }
220 
223  PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) {
224  }
225 
229  return PointType3D<T>(x + p.x, y + p.y, z + p.z);
230  }
231 
235  return PointType3D<T>(x - p.x, y - p.y, z - p.z);
236  }
237 
241  x += p.x;
242  y += p.y;
243  z += p.z;
244  return *this;
245  }
246 
250  x -= p.x;
251  y -= p.y;
252  z -= p.z;
253  return *this;
254  }
255 
258  PointType3D<T> operator*(const T& i) const {
259  return PointType3D<T>(x * i, y * i, z * i);
260  }
261 
264  PointType3D<T> operator/(const T& i) const {
265  return PointType3D<T>(x / i, y / i, z / i);
266  }
267 
270  bool operator==(const PointType3D<T>& p) const {
271  /*return x == p.x && y == p.y && z == p.z;*/
272  return Mathd::Equal(x, p.x) && Mathd::Equal(y, p.y) && Mathd::Equal(z, p.z);
273  }
274 
277  bool operator!=(const PointType3D<T>& p) const {
278  return !(Mathd::Equal(x, p.x) && Mathd::Equal(y, p.y) && Mathd::Equal(z, p.z));
279  }
280 
283  T length() const {
284  double sq;
285  sq = x*x + y*y + z*z;
286  return static_cast<T>(Mathd::Sqrt(sq));
287  }
288 
291  void normalize() {
292  T invLength = static_cast<T>(1.0/length());
293 
294  //TODO: get rid of this static cast
295  if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
296  x = x * invLength;
297  y = y * invLength;
298  z = z * invLength;
299  }
300  else {
301  x = 0;
302  y = 0;
303  z = 0;
304  }
305  }
306 
309  void set(T _x, T _y, T _z) {
310  x = _x;
311  y = _y;
312  z = _z;
313  }
314 
315  inline T& operator[] (int32_t ind) {
316  assert(ind > -1 && ind < 3);
317  return val[ind];
318  }
319  };
320 
323  template<typename T>
324  std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) {
325  return os << "(" << p.x << ":" << p.y << ":" << p.z << ")";
326  }
327 
330  typedef std::vector<Point3D> Point3DVector;
331  typedef std::vector<DoublePoint3D> DoublePoint3DVector;
332 
335  inline Point doublePt2intPt(DoublePoint pt) {
336  Point tmp(static_cast<int32_t>(round(pt.x)), static_cast<int32_t>(round(pt.y)));
337  return tmp;
338  }
339 
342  inline Point3D doublePt2intPt(DoublePoint3D pt) {
343  Point3D tmp(static_cast<int32_t>(round(pt.x)), static_cast<int32_t>(round(pt.y)), static_cast<int32_t>(round(pt.z)));
344  return tmp;
345  }
346 
349  inline DoublePoint intPt2doublePt(Point pt) {
350  DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y));
351  return tmp;
352  }
353 
356  inline DoublePoint3D intPt2doublePt(Point3D pt) {
357  DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z));
358  return tmp;
359  }
360 
361 }
362 
363 #endif
void rotate(const PointType2D< T > &origin, T angle)
Rotates the point around an origin.
Definition: point.h:162
static T Cos(T _val)
Definition: fife_math.h:217
PointType2D(T _x=0, T _y=0)
Constructor.
Definition: point.h:61
static T Sqrt(T _val)
Definition: fife_math.h:277
PointType3D< T > operator-(const PointType3D< T > &p) const
Vector substraction.
Definition: point.h:234
void normalize()
Normalizes the point.
Definition: point.h:131
PointType3D< T > & operator-=(const PointType3D< T > &p)
Vector inplace substraction.
Definition: point.h:249
PointType3D< double > DoublePoint3D
Definition: point.h:329
PointType3D(const PointType3D< T > &rhs)
Copy Constructor.
Definition: point.h:223
bool operator!=(const PointType2D< T > &p) const
Equality comparision.
Definition: point.h:117
std::vector< Point > PointVector
Definition: point.h:197
PointType3D< int32_t > Point3D
Definition: point.h:328
PointType2D< double > DoublePoint
Definition: point.h:196
std::vector< DoublePoint3D > DoublePoint3DVector
Definition: point.h:331
PointType2D(const PointType2D< T > &rhs)
Copy Constructor.
Definition: point.h:66
PointType2D< T > & operator+=(const PointType2D< T > &p)
Vector inplace addition.
Definition: point.h:83
PointType3D< T > & operator+=(const PointType3D< T > &p)
Vector inplace addition.
Definition: point.h:240
std::vector< Point3D > Point3DVector
Definition: point.h:330
static num_type zeroTolerance()
Definition: fife_math.h:132
bool operator==(const PointType3D< T > &p) const
Equality comparision.
Definition: point.h:270
bool operator!=(const PointType3D< T > &p) const
Equality comparision.
Definition: point.h:277
PointType2D< int32_t > Point
Definition: point.h:195
static bool Equal(T _val1, T _val2)
Definition: fife_math.h:287
static T Sin(T _val)
Definition: fife_math.h:267
PointType2D< T > & operator-=(const PointType2D< T > &p)
Vector inplace substraction.
Definition: point.h:91
PointType3D(T _x=0, T _y=0, T _z=0)
Constructor.
Definition: point.h:218
T length() const
Return length.
Definition: point.h:283
Point doublePt2intPt(DoublePoint pt)
Convert from 2D double point to 2D int32_t point.
Definition: point.h:335
PointType2D< T > operator*(const T &i) const
Scalar multiplication with an integer value.
Definition: point.h:99
bool operator==(const PointType2D< T > &p) const
Equality comparision.
Definition: point.h:111
PointType3D< T > operator+(const PointType3D< T > &p) const
Vector addition.
Definition: point.h:228
std::vector< DoublePoint > DoublePointVector
Definition: point.h:198
PointType3D< T > operator/(const T &i) const
Scalar division with an integer value.
Definition: point.h:264
void rotate(T angle)
Rotates the point around the origin.
Definition: point.h:147
A 3D Point.
Definition: point.h:205
A 2D Point.
Definition: point.h:48
T & operator[](int32_t ind)
Definition: point.h:182
DoublePoint intPt2doublePt(Point pt)
Convert from 2D int32_t point to 2D double point.
Definition: point.h:349
static num_type pi()
Definition: fife_math.h:134
T length() const
Return length.
Definition: point.h:123
void normalize()
Normalizes the point.
Definition: point.h:291
PointType2D< T > operator-(const PointType2D< T > &p) const
Vector substraction.
Definition: point.h:77
PointType2D< T > operator+(const PointType2D< T > &p) const
Vector addition.
Definition: point.h:71
PointType3D< T > operator*(const T &i) const
Scalar multiplication with an integer value.
Definition: point.h:258
PointType2D< T > operator/(const T &i) const
Scalar division with an integer value.
Definition: point.h:105