FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
point.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 
29 // Platform specific includes
30 
31 // 3rd party library includes
32 
33 // FIFE includes
34 // These includes are split up in two parts, separated by one empty line
35 // First block: files included from the FIFE root src directory
36 // Second block: files included from the same folder
37 #include "util/base/fife_stdint.h"
38 #include "util/math/fife_math.h"
39 
40 namespace FIFE {
41 
47  template <typename T> class PointType2D {
48  public:
49  union {
50  T val[2];
51  struct {
52  T x,y;
53  };
54  };
55 
60  explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) {
61  }
62 
65  PointType2D(const PointType2D<T>& rhs): x(rhs.x), y(rhs.y) {
66  }
67 
71  return PointType2D<T>(x + p.x, y + p.y);
72  }
73 
77  return PointType2D<T>(x - p.x, y - p.y);
78  }
79 
83  x += p.x;
84  y += p.y;
85  return *this;
86  }
87 
91  x -= p.x;
92  y -= p.y;
93  return *this;
94  }
95 
98  PointType2D<T> operator*(const T& i) const {
99  return PointType2D<T>(x * i, y * i);
100  }
101 
104  PointType2D<T> operator/(const T& i) const {
105  return PointType2D<T>(x / i, y / i);
106  }
107 
110  bool operator==(const PointType2D<T>& p) const {
111  return x == p.x && y == p.y;
112  }
113 
116  bool operator!=(const PointType2D<T>& p) const {
117  return !(x == p.x && y == p.y);
118  }
119 
122  T length() const {
123  double sq;
124  sq = x*x + y*y;
125  return static_cast<T>(Mathd::Sqrt(sq));
126  }
127 
130  void normalize() {
131  T invLength = static_cast<T>(1.0/length());
132 
133  //TODO: get rid of this static cast
134  if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
135  x = x * invLength;
136  y = y * invLength;
137  }
138  else {
139  x = 0;
140  y = 0;
141  }
142  }
143 
146  void rotate(T angle){
147  //TODO: get rid of this static cast
148  T theta = (angle * static_cast<T>(Mathd::pi()))/180;
149  T costheta = static_cast<T>(Mathd::Cos(theta));
150  T sintheta = static_cast<T>(Mathd::Sin(theta));
151 
152  T nx = x;
153  T ny = y;
154 
155  x = costheta * nx - sintheta * ny;
156  y = sintheta * nx + costheta * ny;
157  }
158 
161  void rotate(const PointType2D<T>& origin, T angle){
162  //TODO: get rid of this static cast
163  T theta = (angle * static_cast<T>(Mathd::pi()))/180;
164  T costheta = static_cast<T>(Mathd::Cos(theta));
165  T sintheta = static_cast<T>(Mathd::Sin(theta));
166 
167  T nx = x - origin.x;
168  T ny = y - origin.y;
169 
170  x = costheta * nx - sintheta * ny;
171  y = sintheta * nx + costheta * ny;
172  }
173 
176  void set(T _x, T _y) {
177  x = _x;
178  y = _y;
179  }
180 
181  inline T& operator[] (int32_t ind) {
182  assert(ind > -1 && ind < 2);
183  return val[ind];
184  }
185  };
186 
189  template<typename T>
190  std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) {
191  return os << "(" << p.x << ":" << p.y << ")";
192  }
193 
196 
202  template <typename T> class PointType3D {
203  public:
204  union {
205  T val[3];
206  struct {
207  T x,y,z;
208  };
209  };
210 
215  explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) {
216  }
217 
220  PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) {
221  }
222 
226  return PointType3D<T>(x + p.x, y + p.y, z + p.z);
227  }
228 
232  return PointType3D<T>(x - p.x, y - p.y, z - p.z);
233  }
234 
238  x += p.x;
239  y += p.y;
240  z += p.z;
241  return *this;
242  }
243 
247  x -= p.x;
248  y -= p.y;
249  z -= p.z;
250  return *this;
251  }
252 
255  PointType3D<T> operator*(const T& i) const {
256  return PointType3D<T>(x * i, y * i, z * i);
257  }
258 
261  PointType3D<T> operator/(const T& i) const {
262  return PointType3D<T>(x / i, y / i, z / i);
263  }
264 
267  bool operator==(const PointType3D<T>& p) const {
268  /*return x == p.x && y == p.y && z == p.z;*/
269  return Mathd::Equal(x, p.x) && Mathd::Equal(y, p.y) && Mathd::Equal(z, p.z);
270  }
271 
274  bool operator!=(const PointType3D<T>& p) const {
275  return !(Mathd::Equal(x, p.x) && Mathd::Equal(y, p.y) && Mathd::Equal(z, p.z));
276  }
277 
280  T length() const {
281  double sq;
282  sq = x*x + y*y + z*z;
283  return static_cast<T>(Mathd::Sqrt(sq));
284  }
285 
288  void normalize() {
289  T invLength = static_cast<T>(1.0/length());
290 
291  //TODO: get rid of this static cast
292  if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
293  x = x * invLength;
294  y = y * invLength;
295  z = z * invLength;
296  }
297  else {
298  x = 0;
299  y = 0;
300  z = 0;
301  }
302  }
303 
306  void set(T _x, T _y, T _z) {
307  x = _x;
308  y = _y;
309  z = _z;
310  }
311 
312  inline T& operator[] (int32_t ind) {
313  assert(ind > -1 && ind < 3);
314  return val[ind];
315  }
316  };
317 
320  template<typename T>
321  std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) {
322  return os << "(" << p.x << ":" << p.y << ":" << p.z << ")";
323  }
324 
327 
331  Point tmp(static_cast<int32_t>(round(pt.x)), static_cast<int32_t>(round(pt.y)));
332  return tmp;
333  }
334 
338  Point3D tmp(static_cast<int32_t>(round(pt.x)), static_cast<int32_t>(round(pt.y)), static_cast<int32_t>(round(pt.z)));
339  return tmp;
340  }
341 
345  DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y));
346  return tmp;
347  }
348 
352  DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z));
353  return tmp;
354  }
355 
356 }
357 
358 #endif
void rotate(const PointType2D< T > &origin, T angle)
Rotates the point around an origin.
Definition: point.h:161
static T Cos(T _val)
Definition: fife_math.h:216
PointType2D(T _x=0, T _y=0)
Constructor.
Definition: point.h:60
static T Sqrt(T _val)
Definition: fife_math.h:276
PointType3D< T > operator-(const PointType3D< T > &p) const
Vector substraction.
Definition: point.h:231
void normalize()
Normalizes the point.
Definition: point.h:130
PointType3D< T > & operator-=(const PointType3D< T > &p)
Vector inplace substraction.
Definition: point.h:246
PointType3D< double > DoublePoint3D
Definition: point.h:326
PointType3D(const PointType3D< T > &rhs)
Copy Constructor.
Definition: point.h:220
bool operator!=(const PointType2D< T > &p) const
Equality comparision.
Definition: point.h:116
PointType3D< int32_t > Point3D
Definition: point.h:325
PointType2D< double > DoublePoint
Definition: point.h:195
PointType2D(const PointType2D< T > &rhs)
Copy Constructor.
Definition: point.h:65
PointType2D< T > & operator+=(const PointType2D< T > &p)
Vector inplace addition.
Definition: point.h:82
PointType3D< T > & operator+=(const PointType3D< T > &p)
Vector inplace addition.
Definition: point.h:237
static num_type zeroTolerance()
Definition: fife_math.h:131
bool operator==(const PointType3D< T > &p) const
Equality comparision.
Definition: point.h:267
bool operator!=(const PointType3D< T > &p) const
Equality comparision.
Definition: point.h:274
PointType2D< int32_t > Point
Definition: point.h:194
static bool Equal(T _val1, T _val2)
Definition: fife_math.h:286
static T Sin(T _val)
Definition: fife_math.h:266
PointType2D< T > & operator-=(const PointType2D< T > &p)
Vector inplace substraction.
Definition: point.h:90
PointType3D(T _x=0, T _y=0, T _z=0)
Constructor.
Definition: point.h:215
T length() const
Return length.
Definition: point.h:280
Point doublePt2intPt(DoublePoint pt)
Convert from 2D double point to 2D int32_t point.
Definition: point.h:330
PointType2D< T > operator*(const T &i) const
Scalar multiplication with an integer value.
Definition: point.h:98
bool operator==(const PointType2D< T > &p) const
Equality comparision.
Definition: point.h:110
PointType3D< T > operator+(const PointType3D< T > &p) const
Vector addition.
Definition: point.h:225
PointType3D< T > operator/(const T &i) const
Scalar division with an integer value.
Definition: point.h:261
void rotate(T angle)
Rotates the point around the origin.
Definition: point.h:146
A 3D Point.
Definition: point.h:202
A 2D Point.
Definition: point.h:47
T & operator[](int32_t ind)
Definition: point.h:181
DoublePoint intPt2doublePt(Point pt)
Convert from 2D int32_t point to 2D double point.
Definition: point.h:344
static num_type pi()
Definition: fife_math.h:133
void set(T _x, T _y, T _z)
Sets the x, y and z coordinates of the 3D point.
Definition: point.h:306
T length() const
Return length.
Definition: point.h:122
void normalize()
Normalizes the point.
Definition: point.h:288
PointType2D< T > operator-(const PointType2D< T > &p) const
Vector substraction.
Definition: point.h:76
PointType2D< T > operator+(const PointType2D< T > &p) const
Vector addition.
Definition: point.h:70
PointType3D< T > operator*(const T &i) const
Scalar multiplication with an integer value.
Definition: point.h:255
void set(T _x, T _y)
Sets the x and y coordinate of the 2D point.
Definition: point.h:176
PointType2D< T > operator/(const T &i) const
Scalar division with an integer value.
Definition: point.h:104
T & operator[](int32_t ind)
Definition: point.h:312