point.h

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<T> operator+(const PointType2D<T>& p) const {
00066             return PointType2D<T>(x + p.x, y + p.y);
00067         }
00068 
00071         PointType2D<T> operator-(const PointType2D<T>& p) const {
00072             return PointType2D<T>(x - p.x, y - p.y);
00073         }
00074 
00077         PointType2D<T>& operator+=(const PointType2D<T>& p) {
00078             x += p.x;
00079             y += p.y;
00080             return *this;
00081         }
00082 
00085         PointType2D<T>& operator-=(const PointType2D<T>& p) {
00086             x -= p.x;
00087             y -= p.y;
00088             return *this;
00089         }
00090 
00093         PointType2D<T> operator*(const T& i) const {
00094             return PointType2D<T>(x * i, y * i);
00095         }
00096 
00099         PointType2D<T> operator/(const T& i) const {
00100             return PointType2D<T>(x / i, y / i);
00101         }
00102 
00105         bool operator==(const PointType2D<T>& p) const {
00106             return x == p.x && y == p.y;
00107         }
00108 
00111         bool operator!=(const PointType2D<T>& p) const {
00112             return !(x == p.x && y == p.y);
00113         }
00114 
00117         T length() const {
00118             double sq;
00119             sq = x*x + y*y;
00120             return static_cast<T>(sqrt(sq));
00121         }
00122 
00123         inline T& operator[] (int ind) { 
00124             assert(ind > -1 && ind < 2);
00125             return val[ind];
00126         }
00127     };
00128 
00131     template<typename T>
00132     std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) {
00133         return os << "(" << p.x << ":" << p.y << ")";
00134     }
00135 
00136     typedef PointType2D<int> Point;
00137     typedef PointType2D<double> DoublePoint;
00138 
00144     template <typename T> class PointType3D {
00145     public:
00146         union {
00147             T val[3];
00148             struct {
00149                 T x,y,z;
00150             };
00151         };
00152 
00157         explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) {
00158         }
00159 
00162         PointType3D<T> operator+(const PointType3D<T>& p) const {
00163             return PointType3D<T>(x + p.x, y + p.y, z + p.z);
00164         }
00165 
00168         PointType3D<T> operator-(const PointType3D<T>& p) const {
00169             return PointType3D<T>(x - p.x, y - p.y, z - p.z);
00170         }
00171 
00174         PointType3D<T>& operator+=(const PointType3D<T>& p) {
00175             x += p.x;
00176             y += p.y;
00177             z += p.z;
00178             return *this;
00179         }
00180 
00183         PointType3D<T>& operator-=(const PointType3D<T>& p) {
00184             x -= p.x;
00185             y -= p.y;
00186             z -= p.z;
00187             return *this;
00188         }
00189 
00192         PointType3D<T> operator*(const T& i) const {
00193             return PointType3D<T>(x * i, y * i, z * i);
00194         }
00195 
00198         PointType3D<T> operator/(const T& i) const {
00199             return PointType3D<T>(x / i, y / i, z / i);
00200         }
00201 
00204         bool operator==(const PointType3D<T>& p) const {
00205             return x == p.x && y == p.y && z == p.z;
00206         }
00207 
00210         bool operator!=(const PointType3D<T>& p) const {
00211             return !(x == p.x && y == p.y && z == p.z);
00212         }
00213 
00216         T length() const {
00217             double sq;
00218             sq = x*x + y*y + z*z;
00219             return static_cast<T>(sqrt(sq));
00220         }
00221 
00222         inline T& operator[] (int ind) { 
00223             assert(ind > -1 && ind < 3); 
00224             return val[ind]; 
00225         }
00226     };
00227 
00230     template<typename T>
00231     std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) {
00232         return os << "(" << p.x << ":" << p.y << ":" << p.z << ")";
00233     }
00234 
00235     typedef PointType3D<int> Point3D;
00236     typedef PointType3D<double> DoublePoint3D;
00237 
00240     inline Point doublePt2intPt(DoublePoint pt) {
00241         Point tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)));
00242         return tmp;
00243     }
00244 
00247     inline Point3D doublePt2intPt(DoublePoint3D pt) {
00248         Point3D tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)), static_cast<int>(round(pt.z)));
00249         return tmp;
00250     }
00251 
00254     inline DoublePoint intPt2doublePt(Point pt) {
00255         DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y));
00256         return tmp;
00257     }
00258 
00261     inline DoublePoint3D intPt2doublePt(Point3D pt) {
00262         DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z));
00263         return tmp;
00264     }
00265 
00266 }
00267 
00268 #endif
Generated by  doxygen 1.6.2-20100208