cellgrid.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_MODEL_GRIDS_CELLGRID_H
00023 #define FIFE_MODEL_GRIDS_CELLGRID_H
00024 
00025 // Standard C++ library includes
00026 #include <vector>
00027 
00028 // 3rd party library includes
00029 
00030 // FIFE includes
00031 // These includes are split up in two parts, separated by one empty line
00032 // First block: files included from the FIFE root src directory
00033 // Second block: files included from the same folder
00034 #include "model/metamodel/modelcoords.h"
00035 #include "util/math/matrix.h"
00036 #include "util/base/fifeclass.h"
00037 #include "util/base/fife_stdint.h"
00038 
00039 namespace FIFE {
00040     class CellGrid: public FifeClass {
00041     public:
00045         CellGrid(bool allow_diagonals=false);
00046 
00049         virtual ~CellGrid();
00050 
00056         void getAccessibleCoordinates(const ModelCoordinate& curpos, std::vector<ModelCoordinate>& coordinates);
00057 
00060         virtual const std::string& getType() const = 0;
00061 
00064         virtual const std::string& getName() const = 0;
00065 
00072         virtual bool isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) = 0;
00073 
00080         virtual double getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) = 0;
00081 
00085         virtual uint32_t getCellSideCount() const = 0;
00086 
00090         ExactModelCoordinate toMapCoordinates(const ModelCoordinate& layer_coords);
00091 
00095         virtual ExactModelCoordinate toMapCoordinates(const ExactModelCoordinate& layer_coords) = 0;
00096 
00100         virtual ModelCoordinate toLayerCoordinates(const ExactModelCoordinate& map_coord) = 0;
00101 
00105         virtual ExactModelCoordinate toExactLayerCoordinates(const ExactModelCoordinate& map_coord) = 0;
00106 
00111         virtual void getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) = 0;
00112 
00116         void setXShift(const double& xshift) {
00117             m_xshift = xshift;
00118             updateMatrices();
00119         }
00120 
00124         const double getXShift() const { return m_xshift; }
00125 
00129         void setYShift(const double yshift) {
00130             m_yshift = yshift;
00131             updateMatrices();
00132         }
00133 
00137         const double getYShift() const { return m_yshift; }
00138 
00142         void setZShift(const double zshift) {
00143             m_zshift = zshift;
00144             updateMatrices();
00145         }
00146 
00150         const double getZShift() const { return m_zshift; }
00151 
00155         void setXScale(const double scale) {
00156             m_xscale = scale;
00157             updateMatrices();
00158         }
00159 
00163         void setYScale(const double scale) {
00164             m_yscale = scale;
00165             updateMatrices();
00166         }
00167 
00171         const double getXScale() const { return m_xscale; }
00172 
00176         const double getYScale() const { return m_yscale; }
00177 
00181         void setRotation(const double rotation) {
00182             m_rotation = rotation;
00183             updateMatrices();
00184         }
00185 
00189         const double getRotation() const { return m_rotation; }
00190 
00194         void setAllowDiagonals(const bool allow_diagonals) {
00195             m_allow_diagonals = allow_diagonals;
00196         }
00197 
00201         const bool getAllowDiagonals() const { return m_allow_diagonals; }
00202 
00205         virtual CellGrid* clone() = 0;
00206 
00207     protected:
00208         void updateMatrices();
00209         bool ptInTriangle(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2, const ExactModelCoordinate& pt3);
00210 
00211         DoubleMatrix m_matrix;
00212         DoubleMatrix m_inverse_matrix;
00213         double m_xshift;
00214         double m_yshift;
00215         double m_zshift;
00216         double m_xscale;
00217         double m_yscale;
00218         double m_rotation;
00219         bool m_allow_diagonals;
00220 
00221     private:
00222         int32_t orientation(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2);
00223     };
00224 }
00225 
00226 #endif