FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
squaregrid.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
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 #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 
41  CellGrid() {
42  }
43 
45  SquareGrid* nGrid = new SquareGrid();
46  nGrid->setRotation(m_rotation);
47  nGrid->setXScale(m_xscale);
48  nGrid->setYScale(m_yscale);
49  nGrid->setXShift(m_xshift);
50  nGrid->setYShift(m_yshift);
51  nGrid->setZShift(m_zshift);
53 
54  return nGrid;
55  }
56 
58  }
59 
60  bool SquareGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
61  uint8_t x = ABS(target.x-curpos.x);
62  uint8_t y = ABS(target.y-curpos.y);
63  if ((x<=1) && (y<=1)) {
64  if (m_allow_diagonals) {
65  return true;
66  } else if (x^y) {
67  return true;
68  }
69  }
70 
71  return false;
72  }
73 
74  double SquareGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
75  if (curpos == target) {
76  return 0.0;
77  } else if (ABS(target.x-curpos.x)^ABS(target.y-curpos.y)) {
78  return 1.0;
79  }
80  return 1.4;
81  }
82 
83  double SquareGrid::getHeuristicCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
84  return static_cast<double>(ABS(target.x - curpos.x) + ABS(target.y - curpos.y));
85  }
86 
87  const std::string& SquareGrid::getType() const {
88  static std::string type("square");
89  return type;
90  }
91 
92  const std::string& SquareGrid::getName() const {
93  static std::string squareGrid("Square Grid");
94  return squareGrid;
95  }
96 
98  return m_matrix * layer_coords;
99  }
100 
102  return m_inverse_matrix * map_coord;
103  }
104 
107  ModelCoordinate result(round(dblpt.x), round(dblpt.y), round(dblpt.z));
108 
109  return result;
110  }
111 
112  void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
113  vtx.clear();
114  double x = static_cast<double>(cell.x);
115  double y = static_cast<double>(cell.y);
116  vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
117  vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
118  vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
119  vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));
120  }
121 
122  std::vector<ModelCoordinate> SquareGrid::toMultiCoordinates(const ModelCoordinate& position, const std::vector<ModelCoordinate>& orig, bool reverse) {
123  std::vector<ModelCoordinate> coords;
124  std::vector<ModelCoordinate>::const_iterator it = orig.begin();
125  if (reverse) {
126  for (; it != orig.end(); ++it) {
127  ModelCoordinate mc = position;
128  mc.x -= (*it).x;
129  mc.y -= (*it).y;
130  coords.push_back(mc);
131  }
132  } else {
133  for (; it != orig.end(); ++it) {
134  ModelCoordinate mc = position;
135  mc.x += (*it).x;
136  mc.y += (*it).y;
137  coords.push_back(mc);
138  }
139  }
140  return coords;
141  }
142 }
double m_xscale
Definition: cellgrid.h:243
double getAdjacentCost(const ModelCoordinate &curpos, const ModelCoordinate &target)
Returns distance const from curpos to target point only cells adjacent to curpos are considered in th...
Definition: squaregrid.cpp:74
#define ABS(x)
Definition: fife_math.h:40
void setZShift(const double zshift)
Set the cellgrid z shift.
Definition: cellgrid.h:156
double m_yshift
Definition: cellgrid.h:241
void getVertices(std::vector< ExactModelCoordinate > &vtx, const ModelCoordinate &cell)
Fills given point vector with vertices from selected cell.
Definition: squaregrid.cpp:112
static Logger _log(LM_AUDIO)
DoubleMatrix m_inverse_matrix
Definition: cellgrid.h:239
void setXShift(const double &xshift)
Set the cellgrid x shift.
Definition: cellgrid.h:130
void setYScale(const double scale)
Set the cellgrid y-scaling.
Definition: cellgrid.h:177
virtual ~SquareGrid()
Definition: squaregrid.cpp:57
void setXScale(const double scale)
Set the cellgrid x-scaling.
Definition: cellgrid.h:169
unsigned char uint8_t
Definition: core.h:38
void setYShift(const double yshift)
Set the cellgrid y shift.
Definition: cellgrid.h:143
const std::string & getName() const
Name of the cellgrid (DEPRECATED? -jwt)
Definition: squaregrid.cpp:92
CellGrid * clone()
Returns clone of this cellgrid.
Definition: squaregrid.cpp:44
bool m_allow_diagonals
Definition: cellgrid.h:247
double m_rotation
Definition: cellgrid.h:246
ModelCoordinate toLayerCoordinates(const ExactModelCoordinate &map_coord)
Transforms given point from map coordinates to layer coordinates.
Definition: squaregrid.cpp:105
DoubleMatrix m_matrix
Definition: cellgrid.h:238
bool isAccessible(const ModelCoordinate &curpos, const ModelCoordinate &target)
Tells if given target point is accessible from curpos only cells adjacent to curpos are considered in...
Definition: squaregrid.cpp:60
ExactModelCoordinate toExactLayerCoordinates(const ExactModelCoordinate &map_coord)
Transforms given point from map coordinates to layer coordinates.
Definition: squaregrid.cpp:101
void setAllowDiagonals(const bool allow_diagonals)
Set whether diagonal cell access is allowed.
Definition: cellgrid.h:221
const std::string & getType() const
Type of cellgrid.
Definition: squaregrid.cpp:87
DoublePoint3D ExactModelCoordinate
Definition: modelcoords.h:36
double m_zshift
Definition: cellgrid.h:242
double getHeuristicCost(const ModelCoordinate &curpos, const ModelCoordinate &target)
Returns distance const from curpos to target point.
Definition: squaregrid.cpp:83
A 3D Point.
Definition: point.h:202
double m_yscale
Definition: cellgrid.h:244
void setRotation(const double rotation)
Set the cellgrid rotation.
Definition: cellgrid.h:208
double m_xshift
Definition: cellgrid.h:240
std::vector< ModelCoordinate > toMultiCoordinates(const ModelCoordinate &position, const std::vector< ModelCoordinate > &orig, bool reverse)
Returns point vector with coordinates for a multi object.
Definition: squaregrid.cpp:122
ExactModelCoordinate toMapCoordinates(const ExactModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: squaregrid.cpp:97