comparison engine/core/model/metamodel/grids/hexgrid.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
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/math/fife_math.h"
32 #include "util/log/logger.h"
33
34 #include "hexgrid.h"
35
36 namespace FIFE {
37 static Logger _log(LM_HEXGRID);
38
39 static const double HEX_WIDTH = 1;
40 static const double HEX_TO_EDGE = HEX_WIDTH / 2;
41 static const double HEX_TO_CORNER = 0.5 / cos(M_PI / 6);
42 static const double HEX_EDGE_HALF = HEX_TO_CORNER * sin(M_PI / 6);
43 static const double VERTICAL_MULTIP = sqrt(HEX_WIDTH*HEX_WIDTH - HEX_TO_EDGE*HEX_TO_EDGE);
44 static const double VERTICAL_MULTIP_INV = 1 / VERTICAL_MULTIP;
45
46 HexGrid::HexGrid(bool allow_diagonals): CellGrid(allow_diagonals) {
47 FL_DBG(_log, "Constructing new HexGrid");
48 FL_DBG(_log, LMsg("HEX_WIDTH ") << HEX_WIDTH);
49 FL_DBG(_log, LMsg("HEX_TO_EDGE ") << HEX_TO_EDGE);
50 FL_DBG(_log, LMsg("HEX_TO_CORNER ") << HEX_TO_CORNER);
51 FL_DBG(_log, LMsg("HEX_EDGE_HALF ") << HEX_EDGE_HALF);
52 FL_DBG(_log, LMsg("VERTICAL_MULTIP ") << VERTICAL_MULTIP);
53 }
54
55 HexGrid::~HexGrid() {
56 }
57
58 bool HexGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
59 if (curpos == target) {
60 return true;
61 }
62
63 if(curpos.y % 2) {
64
65 if((curpos.x == target.x) && (curpos.y - 1 == target.y)) {
66 return true;
67 }
68
69 if((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y)) {
70 return true;
71 }
72
73 if((curpos.x + 1 == target.x) && (curpos.y == target.y)) {
74 return true;
75 }
76
77 if((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y)) {
78 return true;
79 }
80
81 if((curpos.x == target.x) && (curpos.y + 1 == target.y)) {
82 return true;
83 }
84
85 if((curpos.x - 1 == target.x) && (curpos.y == target.y)) {
86 return true;
87 }
88
89 } else {
90
91 if((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y)) {
92 return true;
93 }
94
95 if((curpos.x == target.x) && (curpos.y - 1 == target.y)) {
96 return true;
97 }
98
99 if((curpos.x + 1 == target.x) && (curpos.y == target.y)) {
100 return true;
101 }
102
103 if((curpos.x == target.x) && (curpos.y + 1 == target.y)) {
104 return true;
105 }
106
107 if((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y)) {
108 return true;
109 }
110
111 if((curpos.x - 1 == target.x) && (curpos.y == target.y)) {
112 return true;
113 }
114 }
115
116 return false;
117
118 }
119
120 float HexGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
121 assert(isAccessible(curpos, target));
122 if (curpos == target) {
123 return 0;
124 } else if (curpos.y == target.y) {
125 return m_xscale;
126 } else {
127 double a = VERTICAL_MULTIP * m_yscale;
128 double b = HEX_TO_EDGE * m_xscale;
129 return sqrt((a * a) + (b * b));
130 }
131 }
132
133 const std::string& HexGrid::getType() const {
134 static std::string type("hexagonal");
135 return type;
136 }
137
138 const std::string& HexGrid::getName() const {
139 static std::string hexGrid("Hex Grid");
140 return hexGrid;
141 }
142
143 double HexGrid::getXZigzagOffset(double y) {
144 // each uneven row has shifted coordinate of 0.5 horizontally
145 // shift has to be gradual on vertical axis
146 double ay = ABS(y);
147 int i_layer_y = static_cast<int>(ay);
148 double offset = ay - static_cast<double>(i_layer_y);
149 if ((i_layer_y % 2) == 1) {
150 offset = 1 - offset;
151 }
152 return HEX_TO_EDGE * offset;
153 }
154
155 ExactModelCoordinate HexGrid::toMapCoordinates(const ExactModelCoordinate& layer_coords) {
156 ExactModelCoordinate tranformed_coords(layer_coords);
157 tranformed_coords.x += getXZigzagOffset(layer_coords.y);
158 tranformed_coords.y *= VERTICAL_MULTIP;
159 ExactModelCoordinate result = m_matrix * tranformed_coords;
160 FL_DBG(_log, LMsg("layercoords ") << layer_coords << " converted to map: " << result);
161 return result;
162 }
163
164 ExactModelCoordinate HexGrid::toExactLayerCoordinates(const ExactModelCoordinate& map_coord) {
165 ExactModelCoordinate layer_coords = m_inverse_matrix * map_coord;
166 layer_coords.y /= VERTICAL_MULTIP;
167 layer_coords.x -= getXZigzagOffset(layer_coords.y);
168 FL_DBG(_log, LMsg("mapcoords ") << map_coord << " converted to layer: " << layer_coords);
169 return layer_coords;
170 }
171
172 ModelCoordinate HexGrid::toLayerCoordinates(const ExactModelCoordinate& map_coord) {
173 FL_DBG(_log, LMsg("==============\nConverting map coords ") << map_coord << " to int layer coords...");
174 ExactModelCoordinate elc = m_inverse_matrix * map_coord;
175 elc.y *= VERTICAL_MULTIP_INV;
176 ExactModelCoordinate lc = ExactModelCoordinate(floor(elc.x), floor(elc.y));
177 double dx = elc.x - lc.x;
178 double dy = elc.y - lc.y;
179 int x = static_cast<int>(lc.x);
180 int y = static_cast<int>(lc.y);
181 FL_DBG(_log, LMsg("elc=") << elc << ", lc=" << lc);
182 FL_DBG(_log, LMsg("x=") << x << ", y=" << y << ", dx=" << dx << ", dy=" << dy);
183 ModelCoordinate result;
184
185 if ((y % 2) == 0) {
186 FL_DBG(_log, "In even row");
187 if ((1 - dy) < HEX_EDGE_HALF) {
188 FL_DBG(_log, "In lower rect area");
189 result = ModelCoordinate(x, y+1);
190 }
191 else if (dy < HEX_EDGE_HALF) {
192 FL_DBG(_log, "In upper rect area");
193 if (dx > 0.5) {
194 FL_DBG(_log, "...on right");
195 result = ModelCoordinate(x+1, y);
196 }
197 else {
198 FL_DBG(_log, "...on left");
199 result = ModelCoordinate(x, y);
200 }
201 }
202 // in middle triangle area
203 else {
204 FL_DBG(_log, "In middle triangle area");
205 if (dx < 0.5) {
206 FL_DBG(_log, "In left triangles");
207 if (ptInTriangle(ExactModelCoordinate(dx, dy),
208 ExactModelCoordinate(0, VERTICAL_MULTIP * HEX_EDGE_HALF),
209 ExactModelCoordinate(0, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
210 ExactModelCoordinate(0.5, VERTICAL_MULTIP * HEX_EDGE_HALF)
211 )) {
212 FL_DBG(_log, "..upper part");
213 result = ModelCoordinate(x, y);
214 } else {
215 FL_DBG(_log, "..lower part");
216 result = ModelCoordinate(x, y+1);
217 }
218 } else {
219 FL_DBG(_log, "In right triangles");
220 if (ptInTriangle(ExactModelCoordinate(dx, dy),
221 ExactModelCoordinate(1, VERTICAL_MULTIP * HEX_EDGE_HALF),
222 ExactModelCoordinate(1, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
223 ExactModelCoordinate(0.5, VERTICAL_MULTIP * HEX_EDGE_HALF)
224 )) {
225 FL_DBG(_log, "..upper part");
226 result = ModelCoordinate(x+1, y);
227 } else {
228 FL_DBG(_log, "..lower part");
229 result = ModelCoordinate(x, y+1);
230 }
231 }
232 }
233 }
234 else {
235 FL_DBG(_log, "In uneven row");
236 if (dy < HEX_EDGE_HALF) {
237 FL_DBG(_log, "In upper rect area");
238 result = ModelCoordinate(x, y);
239 }
240 else if ((1 - dy) < HEX_EDGE_HALF) {
241 FL_DBG(_log, "In lower rect area");
242 if (dx > 0.5) {
243 FL_DBG(_log, "...on right");
244 result = ModelCoordinate(x+1, y+1);
245 }
246 else {
247 FL_DBG(_log, "...on left");
248 result = ModelCoordinate(x, y+1);
249 }
250 }
251 else {
252 FL_DBG(_log, "In middle triangle area");
253 if (dx < 0.5) {
254 FL_DBG(_log, "In left triangles");
255 if (ptInTriangle(ExactModelCoordinate(dx, dy),
256 ExactModelCoordinate(0, VERTICAL_MULTIP * HEX_EDGE_HALF),
257 ExactModelCoordinate(0, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
258 ExactModelCoordinate(0.5, VERTICAL_MULTIP * (1-HEX_EDGE_HALF))
259 )) {
260 FL_DBG(_log, "..lower part");
261 result = ModelCoordinate(x, y+1);
262 } else {
263 FL_DBG(_log, "..upper part");
264 result = ModelCoordinate(x, y);
265 }
266 } else {
267 FL_DBG(_log, "In right triangles");
268 if (ptInTriangle(ExactModelCoordinate(dx, dy),
269 ExactModelCoordinate(1, VERTICAL_MULTIP * HEX_EDGE_HALF),
270 ExactModelCoordinate(1, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
271 ExactModelCoordinate(0.5, VERTICAL_MULTIP * (1-HEX_EDGE_HALF))
272 )) {
273 FL_DBG(_log, "..lower part");
274 result = ModelCoordinate(x+1, y+1);
275 } else {
276 FL_DBG(_log, "..upper part");
277 result = ModelCoordinate(x, y);
278 }
279 }
280 }
281 }
282 FL_DBG(_log, LMsg(" result = ") << result);
283 return result;
284 }
285
286 void HexGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
287 FL_DBG(_log, LMsg("===============\ngetting vertices for ") << cell);
288 vtx.clear();
289 double x = static_cast<double>(cell.x);
290 double y = static_cast<double>(cell.y);
291 double horiz_shift = 0;
292 if (cell.y % 2 != 0) {
293 horiz_shift = HEX_TO_EDGE;
294 FL_DBG(_log, "on uneven row");
295 }
296 double tx, ty;
297
298 #define ADD_PT(_x, _y) vtx.push_back(ExactModelCoordinate(_x, _y));
299 // FL_DBG(_log, LMsg("Added point ") << _x << ", " << _y)
300 ty = y - VERTICAL_MULTIP_INV * HEX_EDGE_HALF;
301 tx = x - HEX_TO_EDGE - getXZigzagOffset(ty) + horiz_shift;
302 ADD_PT(tx, ty);
303
304 ty = y - VERTICAL_MULTIP_INV * HEX_TO_CORNER;
305 tx = x - getXZigzagOffset(ty) + horiz_shift;
306 ADD_PT(tx, ty);
307
308 ty = y - VERTICAL_MULTIP_INV * HEX_EDGE_HALF;
309 tx = x + HEX_TO_EDGE - getXZigzagOffset(ty) + horiz_shift;
310 ADD_PT(tx, ty);
311
312 ty = y + VERTICAL_MULTIP_INV * HEX_EDGE_HALF;
313 tx = x + HEX_TO_EDGE - getXZigzagOffset(ty) + horiz_shift;
314 ADD_PT(tx, ty);
315
316 ty = y + VERTICAL_MULTIP_INV * HEX_TO_CORNER;
317 tx = x - getXZigzagOffset(ty) + horiz_shift;
318 ADD_PT(tx, ty);
319
320 ty = y + VERTICAL_MULTIP_INV * HEX_EDGE_HALF;
321 tx = x - HEX_TO_EDGE - getXZigzagOffset(ty) + horiz_shift;
322 ADD_PT(tx, ty);
323 }
324 }