Mercurial > fife-parpg
comparison engine/core/util/structures/point.h @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
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 modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, 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 | |
42 /** A 2D Point | |
43 * | |
44 * This is a small helper class to aid in 2d vector arithmetics. | |
45 * @see Rect | |
46 */ | |
47 template <typename T> class PointType2D { | |
48 public: | |
49 union { | |
50 T val[2]; | |
51 struct { | |
52 T x,y; | |
53 }; | |
54 }; | |
55 | |
56 /** Constructor | |
57 * | |
58 * Creates a with 0 as default values. | |
59 */ | |
60 explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) { | |
61 } | |
62 | |
63 /** Vector addition | |
64 */ | |
65 PointType2D<T> operator+(const PointType2D<T>& p) const { | |
66 return PointType2D<T>(x + p.x, y + p.y); | |
67 } | |
68 | |
69 /** Vector substraction | |
70 */ | |
71 PointType2D<T> operator-(const PointType2D<T>& p) const { | |
72 return PointType2D<T>(x - p.x, y - p.y); | |
73 } | |
74 | |
75 /** Vector inplace addition | |
76 */ | |
77 PointType2D<T>& operator+=(const PointType2D<T>& p) { | |
78 x += p.x; | |
79 y += p.y; | |
80 return *this; | |
81 } | |
82 | |
83 /** Vector inplace substraction | |
84 */ | |
85 PointType2D<T>& operator-=(const PointType2D<T>& p) { | |
86 x -= p.x; | |
87 y -= p.y; | |
88 return *this; | |
89 } | |
90 | |
91 /** Scalar multiplication with an integer value | |
92 */ | |
93 PointType2D<T> operator*(const T& i) const { | |
94 return PointType2D<T>(x * i, y * i); | |
95 } | |
96 | |
97 /** Scalar division with an integer value | |
98 */ | |
99 PointType2D<T> operator/(const T& i) const { | |
100 return PointType2D<T>(x / i, y / i); | |
101 } | |
102 | |
103 /** Equality comparision | |
104 */ | |
105 bool operator==(const PointType2D<T>& p) const { | |
106 return x == p.x && y == p.y; | |
107 } | |
108 | |
109 /** Equality comparision | |
110 */ | |
111 bool operator!=(const PointType2D<T>& p) const { | |
112 return !(x == p.x && y == p.y); | |
113 } | |
114 | |
115 /** Return length | |
116 */ | |
117 T length() const { | |
118 double sq; | |
119 sq = x*x + y*y; | |
120 return static_cast<T>(sqrt(sq)); | |
121 } | |
122 | |
123 inline T& operator[] (int ind) { | |
124 assert(ind > -1 && ind < 2); | |
125 return val[ind]; | |
126 } | |
127 }; | |
128 | |
129 /** Print coords of the Point to a stream | |
130 */ | |
131 template<typename T> | |
132 std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) { | |
133 return os << "(" << p.x << ":" << p.y << ")"; | |
134 } | |
135 | |
136 typedef PointType2D<int> Point; | |
137 typedef PointType2D<double> DoublePoint; | |
138 | |
139 /** A 3D Point | |
140 * | |
141 * This is a small helper class to aid in 3d vector arithmetics. | |
142 * @see Rect | |
143 */ | |
144 template <typename T> class PointType3D { | |
145 public: | |
146 union { | |
147 T val[3]; | |
148 struct { | |
149 T x,y,z; | |
150 }; | |
151 }; | |
152 | |
153 /** Constructor | |
154 * | |
155 * Creates a with 0 as default values. | |
156 */ | |
157 explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) { | |
158 } | |
159 | |
160 /** Vector addition | |
161 */ | |
162 PointType3D<T> operator+(const PointType3D<T>& p) const { | |
163 return PointType3D<T>(x + p.x, y + p.y, z + p.z); | |
164 } | |
165 | |
166 /** Vector substraction | |
167 */ | |
168 PointType3D<T> operator-(const PointType3D<T>& p) const { | |
169 return PointType3D<T>(x - p.x, y - p.y, z - p.z); | |
170 } | |
171 | |
172 /** Vector inplace addition | |
173 */ | |
174 PointType3D<T>& operator+=(const PointType3D<T>& p) { | |
175 x += p.x; | |
176 y += p.y; | |
177 z += p.z; | |
178 return *this; | |
179 } | |
180 | |
181 /** Vector inplace substraction | |
182 */ | |
183 PointType3D<T>& operator-=(const PointType3D<T>& p) { | |
184 x -= p.x; | |
185 y -= p.y; | |
186 z -= p.z; | |
187 return *this; | |
188 } | |
189 | |
190 /** Scalar multiplication with an integer value | |
191 */ | |
192 PointType3D<T> operator*(const T& i) const { | |
193 return PointType3D<T>(x * i, y * i, z * i); | |
194 } | |
195 | |
196 /** Scalar division with an integer value | |
197 */ | |
198 PointType3D<T> operator/(const T& i) const { | |
199 return PointType3D<T>(x / i, y / i, z / i); | |
200 } | |
201 | |
202 /** Equality comparision | |
203 */ | |
204 bool operator==(const PointType3D<T>& p) const { | |
205 return x == p.x && y == p.y && z == p.z; | |
206 } | |
207 | |
208 /** Equality comparision | |
209 */ | |
210 bool operator!=(const PointType3D<T>& p) const { | |
211 return !(x == p.x && y == p.y && z == p.z); | |
212 } | |
213 | |
214 /** Return length | |
215 */ | |
216 T length() const { | |
217 double sq; | |
218 sq = x*x + y*y + z*z; | |
219 return static_cast<T>(sqrt(sq)); | |
220 } | |
221 | |
222 inline T& operator[] (int ind) { | |
223 assert(ind > -1 && ind < 3); | |
224 return val[ind]; | |
225 } | |
226 }; | |
227 | |
228 /** Print coords of the Point to a stream | |
229 */ | |
230 template<typename T> | |
231 std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) { | |
232 return os << "(" << p.x << ":" << p.y << ":" << p.z << ")"; | |
233 } | |
234 | |
235 typedef PointType3D<int> Point3D; | |
236 typedef PointType3D<double> DoublePoint3D; | |
237 | |
238 /** Convert from 2D double point to 2D int point | |
239 */ | |
240 inline Point doublePt2intPt(DoublePoint pt) { | |
241 Point tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y))); | |
242 return tmp; | |
243 } | |
244 | |
245 /** Convert from 3D double point to 3D int point | |
246 */ | |
247 inline Point3D doublePt2intPt(DoublePoint3D pt) { | |
248 Point3D tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)), static_cast<int>(round(pt.z))); | |
249 return tmp; | |
250 } | |
251 | |
252 /** Convert from 2D int point to 2D double point | |
253 */ | |
254 inline DoublePoint intPt2doublePt(Point pt) { | |
255 DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y)); | |
256 return tmp; | |
257 } | |
258 | |
259 /** Convert from 3D int point to 3D double point | |
260 */ | |
261 inline DoublePoint3D intPt2doublePt(Point3D pt) { | |
262 DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z)); | |
263 return tmp; | |
264 } | |
265 | |
266 } | |
267 | |
268 #endif |