comparison engine/core/model/metamodel/grids/squaregrid.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 112fc4af772d
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
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 modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <cassert>
24 #include <iostream>
25
26 // 3rd party library includes
27
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/math/fife_math.h"
33 #include "util/log/logger.h"
34
35 #include "squaregrid.h"
36
37 namespace FIFE {
38 static Logger _log(LM_SQUAREGRID);
39
40 SquareGrid::SquareGrid(bool allow_diagonals):
41 CellGrid(allow_diagonals) {
42 }
43
44 SquareGrid::~SquareGrid() {
45 }
46
47 bool SquareGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
48 if (curpos == target)
49 return true;
50 if ((curpos.x == target.x) && (curpos.y - 1 == target.y))
51 return true;
52 if ((curpos.x == target.x) && (curpos.y + 1 == target.y))
53 return true;
54 if ((curpos.x + 1 == target.x) && (curpos.y == target.y))
55 return true;
56 if ((curpos.x - 1 == target.x) && (curpos.y == target.y))
57 return true;
58
59 if (m_allow_diagonals) {
60 return isAccessibleDiagonal(curpos, target);
61 }
62
63 return false;
64 }
65
66 bool SquareGrid::isAccessibleDiagonal(const ModelCoordinate& curpos, const ModelCoordinate& target) {
67 if ((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y))
68 return true;
69 if ((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y))
70 return true;
71 if ((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y))
72 return true;
73 if ((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y))
74 return true;
75 return false;
76 }
77
78 float SquareGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
79 assert(isAccessible(curpos, target));
80 if (curpos == target) {
81 return 0;
82 }
83 if (isAccessibleDiagonal(curpos, target)) {
84 return sqrt(m_xscale*m_xscale + m_yscale*m_yscale);
85 }
86 if (curpos.x == target.x) {
87 return m_xscale;
88 }
89 return m_yscale;
90 }
91
92 const std::string& SquareGrid::getType() const {
93 static std::string type("square");
94 return type;
95 }
96
97 const std::string& SquareGrid::getName() const {
98 static std::string squareGrid("Square Grid");
99 return squareGrid;
100 }
101
102 ExactModelCoordinate SquareGrid::toMapCoordinates(const ExactModelCoordinate& layer_coords) {
103 return m_matrix * layer_coords;
104 }
105
106 ExactModelCoordinate SquareGrid::toExactLayerCoordinates(const ExactModelCoordinate& map_coord) {
107 return m_inverse_matrix * map_coord;
108 }
109
110 ModelCoordinate SquareGrid::toLayerCoordinates(const ExactModelCoordinate& map_coord) {
111 ExactModelCoordinate dblpt = toExactLayerCoordinates(map_coord);
112 ModelCoordinate result;
113 result.x = static_cast<int>(floor(dblpt.x));
114 result.y = static_cast<int>(floor(dblpt.y));
115
116 if ((dblpt.x - static_cast<double>(result.x)) > 0.5) {
117 result.x++;
118 }
119 if ((dblpt.y - static_cast<double>(result.y)) > 0.5) {
120 result.y++;
121 }
122
123 return result;
124 }
125
126 void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
127 vtx.clear();
128 double x = static_cast<double>(cell.x);
129 double y = static_cast<double>(cell.y);
130 vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
131 vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
132 vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
133 vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));
134 }
135 }