| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 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 |
|
| 43 |
/** A 2D Point |
| 44 |
* |
| 45 |
* This is a small helper class to aid in 2d vector arithmetics. |
| 46 |
* @see Rect |
| 47 |
*/ |
| 48 |
template <typename T> class PointType2D { |
| 49 |
public: |
| 50 |
union { |
| 51 |
T val[2]; |
| 52 |
struct { |
| 53 |
T x,y; |
| 54 |
}; |
| 55 |
}; |
| 56 |
|
| 57 |
/** Constructor |
| 58 |
* |
| 59 |
* Creates a with 0 as default values. |
| 60 |
*/ |
| 61 |
explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) { |
| 62 |
} |
| 63 |
|
| 64 |
/** Copy Constructor |
| 65 |
*/ |
| 66 |
PointType2D(const PointType2D<T>& rhs): x(rhs.x), y(rhs.y) { |
| 67 |
} |
| 68 |
|
| 69 |
/** Vector addition |
| 70 |
*/ |
| 71 |
PointType2D<T> operator+(const PointType2D<T>& p) const { |
| 72 |
return PointType2D<T>(x + p.x, y + p.y); |
| 73 |
} |
| 74 |
|
| 75 |
/** Vector substraction |
| 76 |
*/ |
| 77 |
PointType2D<T> operator-(const PointType2D<T>& p) const { |
| 78 |
return PointType2D<T>(x - p.x, y - p.y); |
| 79 |
} |
| 80 |
|
| 81 |
/** Vector inplace addition |
| 82 |
*/ |
| 83 |
PointType2D<T>& operator+=(const PointType2D<T>& p) { |
| 84 |
x += p.x; |
| 85 |
y += p.y; |
| 86 |
return *this; |
| 87 |
} |
| 88 |
|
| 89 |
/** Vector inplace substraction |
| 90 |
*/ |
| 91 |
PointType2D<T>& operator-=(const PointType2D<T>& p) { |
| 92 |
x -= p.x; |
| 93 |
y -= p.y; |
| 94 |
return *this; |
| 95 |
} |
| 96 |
|
| 97 |
/** Scalar multiplication with an integer value |
| 98 |
*/ |
| 99 |
PointType2D<T> operator*(const T& i) const { |
| 100 |
return PointType2D<T>(x * i, y * i); |
| 101 |
} |
| 102 |
|
| 103 |
/** Scalar division with an integer value |
| 104 |
*/ |
| 105 |
PointType2D<T> operator/(const T& i) const { |
| 106 |
return PointType2D<T>(x / i, y / i); |
| 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 |
/** Equality comparision |
| 116 |
*/ |
| 117 |
bool operator!=(const PointType2D<T>& p) const { |
| 118 |
return !(x == p.x && y == p.y); |
| 119 |
} |
| 120 |
|
| 121 |
/** Return length |
| 122 |
*/ |
| 123 |
T length() const { |
| 124 |
double sq; |
| 125 |
sq = x*x + y*y; |
| 126 |
return static_cast<T>(Mathd::Sqrt(sq)); |
| 127 |
} |
| 128 |
|
| 129 |
/** Normalizes the point |
| 130 |
*/ |
| 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 |
|
| 145 |
/** Rotates the point around the origin |
| 146 |
*/ |
| 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 |
|
| 160 |
/** Rotates the point around an origin |
| 161 |
*/ |
| 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 |
|
| 175 |
/** Sets the x and y coordinate of the 2D point |
| 176 |
*/ |
| 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 |
|
| 188 |
/** Print coords of the Point to a stream |
| 189 |
*/ |
| 190 |
template<typename T> |
| 191 |
std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) { |
| 192 |
return os << "(" << p.x << ":" << p.y << ")"; |
| 193 |
} |
| 194 |
|
| 195 |
typedef PointType2D<int32_t> Point; |
| 196 |
typedef PointType2D<double> DoublePoint; |
| 197 |
typedef std::vector<Point> PointVector; |
| 198 |
typedef std::vector<DoublePoint> DoublePointVector; |
| 199 |
|
| 200 |
/** A 3D Point |
| 201 |
* |
| 202 |
* This is a small helper class to aid in 3d vector arithmetics. |
| 203 |
* @see Rect |
| 204 |
*/ |
| 205 |
template <typename T> class PointType3D { |
| 206 |
public: |
| 207 |
union { |
| 208 |
T val[3]; |
| 209 |
struct { |
| 210 |
T x,y,z; |
| 211 |
}; |
| 212 |
}; |
| 213 |
|
| 214 |
/** Constructor |
| 215 |
* |
| 216 |
* Creates a with 0 as default values. |
| 217 |
*/ |
| 218 |
explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) { |
| 219 |
} |
| 220 |
|
| 221 |
/** Copy Constructor |
| 222 |
*/ |
| 223 |
PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) { |
| 224 |
} |
| 225 |
|
| 226 |
/** Vector addition |
| 227 |
*/ |
| 228 |
PointType3D<T> operator+(const PointType3D<T>& p) const { |
| 229 |
return PointType3D<T>(x + p.x, y + p.y, z + p.z); |
| 230 |
} |
| 231 |
|
| 232 |
/** Vector substraction |
| 233 |
*/ |
| 234 |
PointType3D<T> operator-(const PointType3D<T>& p) const { |
| 235 |
return PointType3D<T>(x - p.x, y - p.y, z - p.z); |
| 236 |
} |
| 237 |
|
| 238 |
/** Vector inplace addition |
| 239 |
*/ |
| 240 |
PointType3D<T>& operator+=(const PointType3D<T>& p) { |
| 241 |
x += p.x; |
| 242 |
y += p.y; |
| 243 |
z += p.z; |
| 244 |
return *this; |
| 245 |
} |
| 246 |
|
| 247 |
/** Vector inplace substraction |
| 248 |
*/ |
| 249 |
PointType3D<T>& operator-=(const PointType3D<T>& p) { |
| 250 |
x -= p.x; |
| 251 |
y -= p.y; |
| 252 |
z -= p.z; |
| 253 |
return *this; |
| 254 |
} |
| 255 |
|
| 256 |
/** Scalar multiplication with an integer value |
| 257 |
*/ |
| 258 |
PointType3D<T> operator*(const T& i) const { |
| 259 |
return PointType3D<T>(x * i, y * i, z * i); |
| 260 |
} |
| 261 |
|
| 262 |
/** Scalar division with an integer value |
| 263 |
*/ |
| 264 |
PointType3D<T> operator/(const T& i) const { |
| 265 |
return PointType3D<T>(x / i, y / i, z / i); |
| 266 |
} |
| 267 |
|
| 268 |
/** Equality comparision |
| 269 |
*/ |
| 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 |
|
| 275 |
/** Equality comparision |
| 276 |
*/ |
| 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 |
|
| 281 |
/** Return length |
| 282 |
*/ |
| 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 |
|
| 289 |
/** Normalizes the point |
| 290 |
*/ |
| 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 |
|
| 307 |
/** Sets the x, y and z coordinates of the 3D point |
| 308 |
*/ |
| 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 |
|
| 321 |
/** Print coords of the Point to a stream |
| 322 |
*/ |
| 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 |
|
| 328 |
typedef PointType3D<int32_t> Point3D; |
| 329 |
typedef PointType3D<double> DoublePoint3D; |
| 330 |
typedef std::vector<Point3D> Point3DVector; |
| 331 |
typedef std::vector<DoublePoint3D> DoublePoint3DVector; |
| 332 |
|
| 333 |
/** Convert from 2D double point to 2D int32_t point |
| 334 |
*/ |
| 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 |
|
| 340 |
/** Convert from 3D double point to 3D int32_t point |
| 341 |
*/ |
| 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 |
|
| 347 |
/** Convert from 2D int32_t point to 2D double point |
| 348 |
*/ |
| 349 |
inline DoublePoint intPt2doublePt(Point pt) { |
| 350 |
DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y)); |
| 351 |
return tmp; |
| 352 |
} |
| 353 |
|
| 354 |
/** Convert from 3D int32_t point to 3D double point |
| 355 |
*/ |
| 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 |