31 #include "util/base/exception.h"
32 #include "util/structures/purge.h"
33 #include "model/metamodel/grids/cellgrid.h"
39 static std::string INVALID_LAYER_SET =
"Cannot set layer coordinates, given layer is not initialized properly";
40 static std::string INVALID_LAYER_GET =
"Cannot get layer coordinates, layer is not initialized properly";
42 Location::Location() {
46 Location::Location(
const Location& loc) {
48 m_layer = loc.m_layer;
49 m_exact_layer_coords = loc.m_exact_layer_coords;
52 Location::Location(Layer* layer) {
57 Location::~Location() {
61 void Location::reset() {
62 m_exact_layer_coords.x = 0;
63 m_exact_layer_coords.y = 0;
67 Location& Location::operator=(
const Location& rhs) {
68 m_layer = rhs.m_layer;
69 m_exact_layer_coords.x = rhs.m_exact_layer_coords.x;
70 m_exact_layer_coords.y = rhs.m_exact_layer_coords.y;
74 Map* Location::getMap()
const {
78 return m_layer->getMap();
81 void Location::setLayer(Layer* layer) {
85 Layer* Location::getLayer()
const {
89 void Location::setExactLayerCoordinates(
const ExactModelCoordinate& coordinates) {
91 throw NotSet(INVALID_LAYER_SET);
93 m_exact_layer_coords = coordinates;
96 void Location::setLayerCoordinates(
const ModelCoordinate& coordinates) {
100 void Location::setMapCoordinates(
const ExactModelCoordinate& coordinates) {
102 throw NotSet(INVALID_LAYER_SET);
104 m_exact_layer_coords = m_layer->getCellGrid()->toExactLayerCoordinates(coordinates);
107 ExactModelCoordinate& Location::getExactLayerCoordinatesRef() {
108 return m_exact_layer_coords;
111 ExactModelCoordinate Location::getExactLayerCoordinates()
const {
112 return m_exact_layer_coords;
115 ModelCoordinate Location::getLayerCoordinates()
const {
116 return getLayerCoordinates(m_layer);
119 ExactModelCoordinate Location::getMapCoordinates()
const {
120 return m_layer->getCellGrid()->toMapCoordinates(m_exact_layer_coords);
123 bool Location::isValid()
const {
124 return isValid(m_layer);
127 bool Location::isValid(
const Layer* layer)
const {
128 return (layer && layer->getCellGrid());
131 ExactModelCoordinate Location::getExactLayerCoordinates(
const Layer* layer)
const {
132 return m_exact_layer_coords;
135 ModelCoordinate Location::getLayerCoordinates(
const Layer* layer)
const {
136 if (!isValid(layer)) {
137 throw NotSet(INVALID_LAYER_GET);
139 CellGrid* cg1 = m_layer->getCellGrid();
140 CellGrid* cg2 = layer->getCellGrid();
141 return cg2->toLayerCoordinates(cg1->toMapCoordinates(m_exact_layer_coords));
144 double Location::getCellOffsetDistance()
const {
145 const ExactModelCoordinate& pt = m_exact_layer_coords;
146 double dx = pt.x -
static_cast<double>(
static_cast<int>(pt.x));
147 double dy = pt.y -
static_cast<double>(
static_cast<int>(pt.y));
148 return sqrt(dx*dx + dy*dy);
151 std::ostream&
operator<<(std::ostream& os,
const Location& l) {
153 return os <<
"x=" << p.x <<
", y=" << p.y;
156 double Location::getMapDistanceTo(
const Location& location)
const{
157 ExactModelCoordinate current = getMapCoordinates();
158 ExactModelCoordinate target = location.getMapCoordinates();
160 double rx = current.x - target.x;
161 double ry = current.y - target.y;
162 double rz = current.z - target.z;
164 return sqrt(rx*rx + ry*ry + rz*rz);
167 double Location::getLayerDistanceTo(
const Location& location)
const{
168 ModelCoordinate current = getLayerCoordinates();
169 ModelCoordinate target = location.getLayerCoordinates(m_layer);
171 double rx = current.x - target.x;
172 double ry = current.y - target.y;
173 double rz = current.z - target.z;
175 return sqrt(rx*rx + ry*ry + rz*rz);