FIFE 2008.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2005-2008 by the FIFE team * 00003 * http://www.fifengine.de * 00004 * This file is part of FIFE. * 00005 * * 00006 * FIFE is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with this library; if not, write to the * 00018 * Free Software Foundation, Inc., * 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00020 ***************************************************************************/ 00021 00022 #ifndef FIFE_VIDEO_POINT_H 00023 #define FIFE_VIDEO_POINT_H 00024 00025 // Standard C++ library includes 00026 #include <iostream> 00027 #include <cassert> 00028 00029 // Platform specific includes 00030 00031 // 3rd party library includes 00032 00033 // FIFE includes 00034 // These includes are split up in two parts, separated by one empty line 00035 // First block: files included from the FIFE root src directory 00036 // Second block: files included from the same folder 00037 #include "util/base/fife_stdint.h" 00038 #include "util/math/fife_math.h" 00039 00040 namespace FIFE { 00041 00047 template <typename T> class PointType2D { 00048 public: 00049 union { 00050 T val[2]; 00051 struct { 00052 T x,y; 00053 }; 00054 }; 00055 00060 explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) { 00061 } 00062 00065 PointType2D(const PointType2D<T>& rhs): x(rhs.x), y(rhs.y) { 00066 } 00067 00070 PointType2D<T> operator+(const PointType2D<T>& p) const { 00071 return PointType2D<T>(x + p.x, y + p.y); 00072 } 00073 00076 PointType2D<T> operator-(const PointType2D<T>& p) const { 00077 return PointType2D<T>(x - p.x, y - p.y); 00078 } 00079 00082 PointType2D<T>& operator+=(const PointType2D<T>& p) { 00083 x += p.x; 00084 y += p.y; 00085 return *this; 00086 } 00087 00090 PointType2D<T>& operator-=(const PointType2D<T>& p) { 00091 x -= p.x; 00092 y -= p.y; 00093 return *this; 00094 } 00095 00098 PointType2D<T> operator*(const T& i) const { 00099 return PointType2D<T>(x * i, y * i); 00100 } 00101 00104 PointType2D<T> operator/(const T& i) const { 00105 return PointType2D<T>(x / i, y / i); 00106 } 00107 00110 bool operator==(const PointType2D<T>& p) const { 00111 return x == p.x && y == p.y; 00112 } 00113 00116 bool operator!=(const PointType2D<T>& p) const { 00117 return !(x == p.x && y == p.y); 00118 } 00119 00122 T length() const { 00123 double sq; 00124 sq = x*x + y*y; 00125 return static_cast<T>(Mathd::Sqrt(sq)); 00126 } 00127 00130 void normalize() { 00131 T invLength = 1.0/length(); 00132 00133 //TODO: get rid of this static cast 00134 if (invLength > static_cast<T>(Mathd::zeroTolerance())) { 00135 x = x * invLength; 00136 y = y * invLength; 00137 } 00138 else { 00139 x = 0; 00140 y = 0; 00141 } 00142 } 00143 00146 void rotate(T angle){ 00147 //TODO: get rid of this static cast 00148 T theta = (angle * static_cast<T>(Mathd::pi()))/180; 00149 T costheta = static_cast<T>(Mathd::Cos(theta)); 00150 T sintheta = static_cast<T>(Mathd::Sin(theta)); 00151 00152 T nx = x; 00153 T ny = y; 00154 00155 x = costheta * nx - sintheta * ny; 00156 y = sintheta * nx + costheta * ny; 00157 } 00158 00161 void rotate(const PointType2D<T>& origin, T angle){ 00162 //TODO: get rid of this static cast 00163 T theta = (angle * static_cast<T>(Mathd::pi()))/180; 00164 T costheta = static_cast<T>(Mathd::Cos(theta)); 00165 T sintheta = static_cast<T>(Mathd::Sin(theta)); 00166 00167 T nx = x - origin.x; 00168 T ny = y - origin.y; 00169 00170 x = costheta * nx - sintheta * ny; 00171 y = sintheta * nx + costheta * ny; 00172 } 00173 00174 inline T& operator[] (int ind) { 00175 assert(ind > -1 && ind < 2); 00176 return val[ind]; 00177 } 00178 }; 00179 00182 template<typename T> 00183 std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) { 00184 return os << "(" << p.x << ":" << p.y << ")"; 00185 } 00186 00187 typedef PointType2D<int> Point; 00188 typedef PointType2D<double> DoublePoint; 00189 00195 template <typename T> class PointType3D { 00196 public: 00197 union { 00198 T val[3]; 00199 struct { 00200 T x,y,z; 00201 }; 00202 }; 00203 00208 explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) { 00209 } 00210 00213 PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) { 00214 } 00215 00218 PointType3D<T> operator+(const PointType3D<T>& p) const { 00219 return PointType3D<T>(x + p.x, y + p.y, z + p.z); 00220 } 00221 00224 PointType3D<T> operator-(const PointType3D<T>& p) const { 00225 return PointType3D<T>(x - p.x, y - p.y, z - p.z); 00226 } 00227 00230 PointType3D<T>& operator+=(const PointType3D<T>& p) { 00231 x += p.x; 00232 y += p.y; 00233 z += p.z; 00234 return *this; 00235 } 00236 00239 PointType3D<T>& operator-=(const PointType3D<T>& p) { 00240 x -= p.x; 00241 y -= p.y; 00242 z -= p.z; 00243 return *this; 00244 } 00245 00248 PointType3D<T> operator*(const T& i) const { 00249 return PointType3D<T>(x * i, y * i, z * i); 00250 } 00251 00254 PointType3D<T> operator/(const T& i) const { 00255 return PointType3D<T>(x / i, y / i, z / i); 00256 } 00257 00260 bool operator==(const PointType3D<T>& p) const { 00261 return x == p.x && y == p.y && z == p.z; 00262 } 00263 00266 bool operator!=(const PointType3D<T>& p) const { 00267 return !(x == p.x && y == p.y && z == p.z); 00268 } 00269 00272 T length() const { 00273 double sq; 00274 sq = x*x + y*y + z*z; 00275 return static_cast<T>(sqrt(sq)); 00276 } 00277 00280 void normalize() { 00281 T invLength = 1.0/length(); 00282 00283 //TODO: get rid of this static cast 00284 if (invLength > static_cast<T>(Mathd::zeroTolerance())) { 00285 x = x * invLength; 00286 y = y * invLength; 00287 z = z * invLength; 00288 } 00289 else { 00290 x = 0; 00291 y = 0; 00292 z = 0; 00293 } 00294 } 00295 00296 inline T& operator[] (int ind) { 00297 assert(ind > -1 && ind < 3); 00298 return val[ind]; 00299 } 00300 }; 00301 00304 template<typename T> 00305 std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) { 00306 return os << "(" << p.x << ":" << p.y << ":" << p.z << ")"; 00307 } 00308 00309 typedef PointType3D<int> Point3D; 00310 typedef PointType3D<double> DoublePoint3D; 00311 00314 inline Point doublePt2intPt(DoublePoint pt) { 00315 Point tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y))); 00316 return tmp; 00317 } 00318 00321 inline Point3D doublePt2intPt(DoublePoint3D pt) { 00322 Point3D tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)), static_cast<int>(round(pt.z))); 00323 return tmp; 00324 } 00325 00328 inline DoublePoint intPt2doublePt(Point pt) { 00329 DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y)); 00330 return tmp; 00331 } 00332 00335 inline DoublePoint3D intPt2doublePt(Point3D pt) { 00336 DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z)); 00337 return tmp; 00338 } 00339 00340 } 00341 00342 #endif