FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
location.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 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 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "util/base/exception.h"
32 
33 #include "layer.h"
34 
35 namespace FIFE {
36  static std::string INVALID_LAYER_SET = "Cannot set layer coordinates, given layer is not initialized properly";
37  static std::string INVALID_LAYER_GET = "Cannot get layer coordinates, layer is not initialized properly";
38 
40  reset();
41  }
42 
44  reset();
45  m_layer = loc.m_layer;
47  }
48 
50  reset();
51  m_layer = layer;
52  }
53 
55  }
56 
57  void Location::reset() {
61  m_layer = NULL;
62  }
63 
65  m_layer = rhs.m_layer;
69  return *this;
70  }
71 
72  Map* Location::getMap() const {
73  if (!m_layer) {
74  return NULL;
75  }
76  return m_layer->getMap();
77  }
78 
79  void Location::setLayer(Layer* layer) {
80  m_layer = layer;
81  }
82 
84  return m_layer;
85  }
86 
88  if (!isValid()) {
89  throw NotSet(INVALID_LAYER_SET);
90  }
91  m_exact_layer_coords = coordinates;
92  }
93 
96  }
97 
99  if (!isValid()) {
100  throw NotSet(INVALID_LAYER_SET);
101  }
103  }
104 
106  return m_exact_layer_coords;
107  }
108 
110  return m_exact_layer_coords;
111  }
112 
115  }
116 
119  }
120 
121  bool Location::isValid() const {
122  return isValid(m_layer);
123  }
124 
125  bool Location::isValid(const Layer* layer) const {
126  return (layer && layer->getCellGrid());
127  }
128 
130  if (!isValid(layer)) {
131  throw NotSet(INVALID_LAYER_GET);
132  }
133 
134  if (layer == m_layer) {
135  return m_exact_layer_coords;
136  }
137 
138  CellGrid* cg1 = m_layer->getCellGrid();
139  CellGrid* cg2 = layer->getCellGrid();
141  }
142 
144  if (!isValid(layer)) {
145  throw NotSet(INVALID_LAYER_GET);
146  }
147 
148  if (layer == m_layer) {
149  return getLayerCoordinates();
150  }
151 
152  CellGrid* cg1 = m_layer->getCellGrid();
153  CellGrid* cg2 = layer->getCellGrid();
155  }
156 
159  double dx = pt.x - static_cast<double>(static_cast<int32_t>(pt.x));
160  double dy = pt.y - static_cast<double>(static_cast<int32_t>(pt.y));
161  return Mathd::Sqrt(dx*dx + dy*dy);
162  }
163 
164  std::ostream& operator<<(std::ostream& os, const Location& l) {
166  return os << "x=" << p.x << ", y=" << p.y;
167  }
168 
169  double Location::getMapDistanceTo(const Location& location) const{
171  ExactModelCoordinate target = location.getMapCoordinates();
172 
173  double rx = current.x - target.x;
174  double ry = current.y - target.y;
175  double rz = current.z - target.z;
176 
177  return Mathd::Sqrt(rx*rx + ry*ry + rz*rz);
178  }
179 
180  double Location::getLayerDistanceTo(const Location& location) const{
182  ModelCoordinate target = location.getLayerCoordinates(m_layer);
183 
184  double rx = current.x - target.x;
185  double ry = current.y - target.y;
186  double rz = current.z - target.z;
187 
188  return Mathd::Sqrt(rx*rx + ry*ry + rz*rz);
189  }
190 }
ExactModelCoordinate getExactLayerCoordinates() const
Gets exact layer coordinates set to this location.
Definition: location.cpp:109
void setLayerCoordinates(const ModelCoordinate &coordinates)
Sets "cell precise" layer coordinates to this location.
Definition: location.cpp:94
void setExactLayerCoordinates(const ExactModelCoordinate &coordinates)
Sets precise layer coordinates to this location.
Definition: location.cpp:87
static T Sqrt(T _val)
Definition: fife_math.h:277
Layer * getLayer() const
Gets the layer where this location is pointing to.
Definition: location.cpp:83
ExactModelCoordinate m_exact_layer_coords
Definition: location.h:177
void setLayer(Layer *layer)
Sets layer where this location is pointing to.
Definition: location.cpp:79
void setMapCoordinates(const ExactModelCoordinate &coordinates)
Sets map coordinates to this location.
Definition: location.cpp:98
Location()
Default constructor.
Definition: location.cpp:39
bool isValid() const
Tells if location is valid Location is valid if:
Definition: location.cpp:121
void reset()
Resets location (so that layer and coordinate information becomes invalid)
Definition: location.cpp:57
static std::string INVALID_LAYER_GET
Definition: location.cpp:37
Map * getMap() const
Gets the map where this location is pointing to.
Definition: location.cpp:72
double getLayerDistanceTo(const Location &location) const
Gets layer distance to another location.
Definition: location.cpp:180
double getMapDistanceTo(const Location &location) const
Gets distance in map coordinates to another location on the Map.
Definition: location.cpp:169
std::ostream & operator<<(std::ostream &os, const Location &l)
Stream output operator.
Definition: location.cpp:164
A basic layer on a map.
Definition: layer.h:99
double getCellOffsetDistance() const
Gets offset distance from cell center.
Definition: location.cpp:157
ExactModelCoordinate & getExactLayerCoordinatesRef()
Gets reference to exact layer coordinates.
Definition: location.cpp:105
Location & operator=(const Location &rhs)
Assignment operator.
Definition: location.cpp:64
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
DoublePoint intPt2doublePt(Point pt)
Convert from 2D int32_t point to 2D double point.
Definition: point.h:349
static std::string INVALID_LAYER_SET
Definition: location.cpp:36
ExactModelCoordinate getMapCoordinates() const
Gets map coordinates set to this location.
Definition: location.cpp:117
ModelCoordinate getLayerCoordinates() const
Gets cell precision layer coordinates set to this location.
Definition: location.cpp:113
~Location()
Destructor.
Definition: location.cpp:54
Map * getMap() const
Get the map this layer is contained in.
Definition: layer.cpp:89
A container of Layer(s).
Definition: map.h:88
ExactModelCoordinate toMapCoordinates(const ModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: cellgrid.cpp:75
virtual ExactModelCoordinate toExactLayerCoordinates(const ExactModelCoordinate &map_coord)=0
Transforms given point from map coordinates to layer coordinates.
virtual ModelCoordinate toLayerCoordinatesFromExactLayerCoordinates(const ExactModelCoordinate &exact_layer_coords)=0
Transforms given point from exact layer coordinates to cell precision layer coordinates.
Layer * m_layer
Definition: location.h:176
virtual ModelCoordinate toLayerCoordinates(const ExactModelCoordinate &map_coord)=0
Transforms given point from map coordinates to layer coordinates.