FIFE 2008.0
cellgrid.cpp
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 // Standard C++ library includes
00023 #include <cassert>
00024 
00025 // 3rd party library includes
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "util/log/logger.h"
00032 
00033 #include "cellgrid.h"
00034 
00035 namespace FIFE {
00036     static Logger _log(LM_CELLGRID);
00037 
00038     CellGrid::CellGrid(bool allow_diagonals):
00039         FifeClass(),
00040         m_matrix(),
00041         m_inverse_matrix(),
00042         m_xshift(0),
00043         m_yshift(0),
00044         m_xscale(1),
00045         m_yscale(1),
00046         m_rotation(0),
00047         m_allow_diagonals(allow_diagonals) {
00048         updateMatrices();
00049     }
00050 
00051     CellGrid::~CellGrid() {
00052     }
00053 
00054     void CellGrid::getAccessibleCoordinates(const ModelCoordinate& curpos, std::vector<ModelCoordinate>& coordinates) {
00055         coordinates.clear();
00056         for (int x = curpos.x - 1; x <= curpos.x + 1; x++) {
00057             for (int y = curpos.y - 1; y <= curpos.y + 1; y++) {
00058                 ModelCoordinate pt;
00059                 pt.x = x;
00060                 pt.y = y;
00061                 if (isAccessible(curpos, pt)) {
00062                     coordinates.push_back(pt);
00063                 }
00064             }
00065         }
00066     }
00067 
00068     void CellGrid::updateMatrices() {
00069         m_matrix.loadRotate(m_rotation, 0.0, 0.0, 1.0);
00070         m_matrix.applyScale(m_xscale,m_yscale, 1);
00071         m_matrix.applyTranslate(m_xshift, m_yshift, 0);
00072         m_inverse_matrix = m_matrix.inverse();
00073     }
00074 
00075     ExactModelCoordinate CellGrid::toMapCoordinates(const ModelCoordinate& layer_coords) {
00076         return toMapCoordinates(intPt2doublePt(layer_coords));
00077     }
00078 
00079     int CellGrid::orientation(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2) {
00080         double o = (pt2.x - pt1.x) * (pt.y - pt1.y) - (pt.x - pt1.x) * (pt2.y - pt1.y);
00081         if (o > 0.0) {
00082             return 1;
00083         } else if (o < 0.0) {
00084             return -1;
00085         }
00086         return 0;
00087     }
00088     
00089     bool CellGrid::ptInTriangle(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2, const ExactModelCoordinate& pt3) {
00090         double o1 = orientation(pt1, pt2, pt);
00091         double o2 = orientation(pt2, pt3, pt);
00092         double o3 = orientation(pt3, pt1, pt);
00093         bool result = (o1 == o2) && (o2 == o3);
00094         FL_DBG(_log, LMsg("ptInTriangle, pt=") << pt << " pt1=" << pt1 << " pt2=" << pt2 << " pt3=" << pt3 << " in=" << result);
00095         return result;
00096     }
00097 }
 All Classes Namespaces Functions Variables Enumerations Enumerator