FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
point.h
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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 = 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 
174  inline T& operator[] (int ind) {
175  assert(ind > -1 && ind < 2);
176  return val[ind];
177  }
178  };
179 
182  template<typename T>
183  std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) {
184  return os << "(" << p.x << ":" << p.y << ")";
185  }
186 
187  typedef PointType2D<int> Point;
188  typedef PointType2D<double> DoublePoint;
189 
195  template <typename T> class PointType3D {
196  public:
197  union {
198  T val[3];
199  struct {
200  T x,y,z;
201  };
202  };
203 
208  explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) {
209  }
210 
213  PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) {
214  }
215 
219  return PointType3D<T>(x + p.x, y + p.y, z + p.z);
220  }
221 
225  return PointType3D<T>(x - p.x, y - p.y, z - p.z);
226  }
227 
231  x += p.x;
232  y += p.y;
233  z += p.z;
234  return *this;
235  }
236 
240  x -= p.x;
241  y -= p.y;
242  z -= p.z;
243  return *this;
244  }
245 
248  PointType3D<T> operator*(const T& i) const {
249  return PointType3D<T>(x * i, y * i, z * i);
250  }
251 
254  PointType3D<T> operator/(const T& i) const {
255  return PointType3D<T>(x / i, y / i, z / i);
256  }
257 
260  bool operator==(const PointType3D<T>& p) const {
261  return x == p.x && y == p.y && z == p.z;
262  }
263 
266  bool operator!=(const PointType3D<T>& p) const {
267  return !(x == p.x && y == p.y && z == p.z);
268  }
269 
272  T length() const {
273  double sq;
274  sq = x*x + y*y + z*z;
275  return static_cast<T>(sqrt(sq));
276  }
277 
280  void normalize() {
281  T invLength = 1.0/length();
282 
283  //TODO: get rid of this static cast
284  if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
285  x = x * invLength;
286  y = y * invLength;
287  z = z * invLength;
288  }
289  else {
290  x = 0;
291  y = 0;
292  z = 0;
293  }
294  }
295 
296  inline T& operator[] (int ind) {
297  assert(ind > -1 && ind < 3);
298  return val[ind];
299  }
300  };
301 
304  template<typename T>
305  std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) {
306  return os << "(" << p.x << ":" << p.y << ":" << p.z << ")";
307  }
308 
309  typedef PointType3D<int> Point3D;
310  typedef PointType3D<double> DoublePoint3D;
311 
315  Point tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)));
316  return tmp;
317  }
318 
322  Point3D tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)), static_cast<int>(round(pt.z)));
323  return tmp;
324  }
325 
329  DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y));
330  return tmp;
331  }
332 
336  DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z));
337  return tmp;
338  }
339 
340 }
341 
342 #endif