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_zshift(0),
00045         m_xscale(1),
00046         m_yscale(1),
00047         m_rotation(0),
00048         m_allow_diagonals(allow_diagonals) {
00049         updateMatrices();
00050     }
00051 
00052     CellGrid::~CellGrid() {
00053     }
00054 
00055     void CellGrid::getAccessibleCoordinates(const ModelCoordinate& curpos, std::vector<ModelCoordinate>& coordinates) {
00056         coordinates.clear();
00057         for (int32_t x = curpos.x - 1; x <= curpos.x + 1; x++) {
00058             for (int32_t y = curpos.y - 1; y <= curpos.y + 1; y++) {
00059                 ModelCoordinate pt;
00060                 pt.x = x;
00061                 pt.y = y;
00062                 if (isAccessible(curpos, pt)) {
00063                     coordinates.push_back(pt);
00064                 }
00065             }
00066         }
00067     }
00068 
00069     void CellGrid::updateMatrices() {
00070         m_matrix.loadRotate(m_rotation, 0.0, 0.0, 1.0);
00071         m_matrix.applyScale(m_xscale,m_yscale, 1);
00072         m_matrix.applyTranslate(m_xshift, m_yshift, m_zshift);
00073         m_inverse_matrix = m_matrix.inverse();
00074     }
00075 
00076     ExactModelCoordinate CellGrid::toMapCoordinates(const ModelCoordinate& layer_coords) {
00077         return toMapCoordinates(intPt2doublePt(layer_coords));
00078     }
00079 
00080     int32_t CellGrid::orientation(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2) {
00081         double o = (pt2.x - pt1.x) * (pt.y - pt1.y) - (pt.x - pt1.x) * (pt2.y - pt1.y);
00082         if (o > 0.0) {
00083             return 1;
00084         } else if (o < 0.0) {
00085             return -1;
00086         }
00087         return 0;
00088     }
00089     
00090     bool CellGrid::ptInTriangle(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2, const ExactModelCoordinate& pt3) {
00091         double o1 = orientation(pt1, pt2, pt);
00092         double o2 = orientation(pt2, pt3, pt);
00093         double o3 = orientation(pt3, pt1, pt);
00094         bool result = (o1 == o2) && (o2 == o3);
00095         FL_DBG(_log, LMsg("ptInTriangle, pt=") << pt << " pt1=" << pt1 << " pt2=" << pt2 << " pt3=" << pt3 << " in=" << result);
00096         return result;
00097     }
00098 }