FIFE  2008.0
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 = 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 
194  typedef PointType2D<int32_t> Point;
195  typedef PointType2D<double> DoublePoint;
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  }
270 
273  bool operator!=(const PointType3D<T>& p) const {
274  return !(x == p.x && y == p.y && z == p.z);
275  }
276 
279  T length() const {
280  double sq;
281  sq = x*x + y*y + z*z;
282  return static_cast<T>(Mathd::Sqrt(sq));
283  }
284 
287  void normalize() {
288  T invLength = static_cast<T>(1.0/length());
289 
290  //TODO: get rid of this static cast
291  if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
292  x = x * invLength;
293  y = y * invLength;
294  z = z * invLength;
295  }
296  else {
297  x = 0;
298  y = 0;
299  z = 0;
300  }
301  }
302 
305  void set(T _x, T _y, T _z) {
306  x = _x;
307  y = _y;
308  z = _z;
309  }
310 
311  inline T& operator[] (int32_t ind) {
312  assert(ind > -1 && ind < 3);
313  return val[ind];
314  }
315  };
316 
319  template<typename T>
320  std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) {
321  return os << "(" << p.x << ":" << p.y << ":" << p.z << ")";
322  }
323 
324  typedef PointType3D<int32_t> Point3D;
325  typedef PointType3D<double> DoublePoint3D;
326 
329  inline Point doublePt2intPt(DoublePoint pt) {
330  Point tmp(static_cast<int32_t>(round(pt.x)), static_cast<int32_t>(round(pt.y)));
331  return tmp;
332  }
333 
336  inline Point3D doublePt2intPt(DoublePoint3D pt) {
337  Point3D tmp(static_cast<int32_t>(round(pt.x)), static_cast<int32_t>(round(pt.y)), static_cast<int32_t>(round(pt.z)));
338  return tmp;
339  }
340 
343  inline DoublePoint intPt2doublePt(Point pt) {
344  DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y));
345  return tmp;
346  }
347 
350  inline DoublePoint3D intPt2doublePt(Point3D pt) {
351  DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z));
352  return tmp;
353  }
354 
355 }
356 
357 #endif
void rotate(const PointType2D< T > &origin, T angle)
Definition: point.h:161
PointType2D(T _x=0, T _y=0)
Definition: point.h:60
PointType3D< T > operator-(const PointType3D< T > &p) const
Definition: point.h:231
void normalize()
Definition: point.h:130
PointType3D< T > & operator-=(const PointType3D< T > &p)
Definition: point.h:246
PointType3D(const PointType3D< T > &rhs)
Definition: point.h:220
bool operator!=(const PointType2D< T > &p) const
Definition: point.h:116
PointType2D(const PointType2D< T > &rhs)
Definition: point.h:65
PointType2D< T > & operator+=(const PointType2D< T > &p)
Definition: point.h:82
PointType3D< T > & operator+=(const PointType3D< T > &p)
Definition: point.h:237
bool operator==(const PointType3D< T > &p) const
Definition: point.h:267
bool operator!=(const PointType3D< T > &p) const
Definition: point.h:273
PointType2D< T > & operator-=(const PointType2D< T > &p)
Definition: point.h:90
PointType3D(T _x=0, T _y=0, T _z=0)
Definition: point.h:215
T length() const
Definition: point.h:279
Point doublePt2intPt(DoublePoint pt)
Definition: point.h:329
PointType2D< T > operator*(const T &i) const
Definition: point.h:98
bool operator==(const PointType2D< T > &p) const
Definition: point.h:110
PointType3D< T > operator+(const PointType3D< T > &p) const
Definition: point.h:225
PointType3D< T > operator/(const T &i) const
Definition: point.h:261
void rotate(T angle)
Definition: point.h:146
DoublePoint intPt2doublePt(Point pt)
Definition: point.h:343
void set(T _x, T _y, T _z)
Definition: point.h:305
T length() const
Definition: point.h:122
void normalize()
Definition: point.h:287
PointType2D< T > operator-(const PointType2D< T > &p) const
Definition: point.h:76
PointType2D< T > operator+(const PointType2D< T > &p) const
Definition: point.h:70
PointType3D< T > operator*(const T &i) const
Definition: point.h:255
void set(T _x, T _y)
Definition: point.h:176
PointType2D< T > operator/(const T &i) const
Definition: point.h:104
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39