FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
cellgrid.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cassert>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/log/logger.h"
32 
33 #include "cellgrid.h"
34 
35 namespace FIFE {
36  static Logger _log(LM_CELLGRID);
37 
38  CellGrid::CellGrid(bool allow_diagonals):
39  FifeClass(),
40  m_matrix(),
41  m_inverse_matrix(),
42  m_xshift(0),
43  m_yshift(0),
44  m_zshift(0),
45  m_xscale(1),
46  m_yscale(1),
47  m_rotation(0),
48  m_allow_diagonals(allow_diagonals) {
49  updateMatrices();
50  }
51 
52  CellGrid::~CellGrid() {
53  }
54 
55  void CellGrid::getAccessibleCoordinates(const ModelCoordinate& curpos, std::vector<ModelCoordinate>& coordinates) {
56  coordinates.clear();
57  for (int32_t x = curpos.x - 1; x <= curpos.x + 1; x++) {
58  for (int32_t y = curpos.y - 1; y <= curpos.y + 1; y++) {
59  ModelCoordinate pt;
60  pt.x = x;
61  pt.y = y;
62  if (isAccessible(curpos, pt)) {
63  coordinates.push_back(pt);
64  }
65  }
66  }
67  }
68 
69  void CellGrid::updateMatrices() {
70  m_matrix.loadRotate(m_rotation, 0.0, 0.0, 1.0);
71  m_matrix.applyScale(m_xscale,m_yscale, 1);
72  m_matrix.applyTranslate(m_xshift, m_yshift, m_zshift);
73  m_inverse_matrix = m_matrix.inverse();
74  }
75 
76  ExactModelCoordinate CellGrid::toMapCoordinates(const ModelCoordinate& layer_coords) {
77  return toMapCoordinates(intPt2doublePt(layer_coords));
78  }
79 
80  int32_t CellGrid::orientation(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2) {
81  double o = (pt2.x - pt1.x) * (pt.y - pt1.y) - (pt.x - pt1.x) * (pt2.y - pt1.y);
82  if (o > 0.0) {
83  return 1;
84  } else if (o < 0.0) {
85  return -1;
86  }
87  return 0;
88  }
89 
90  bool CellGrid::ptInTriangle(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2, const ExactModelCoordinate& pt3) {
91  double o1 = orientation(pt1, pt2, pt);
92  double o2 = orientation(pt2, pt3, pt);
93  double o3 = orientation(pt3, pt1, pt);
94  bool result = (o1 == o2) && (o2 == o3);
95  FL_DBG(_log, LMsg("ptInTriangle, pt=") << pt << " pt1=" << pt1 << " pt2=" << pt2 << " pt3=" << pt3 << " in=" << result);
96  return result;
97  }
98 }