30 #include "util/base/exception.h"
31 #include "model/metamodel/grids/cellgrid.h"
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";
39 Location::Location() {
43 Location::Location(
const Location& loc) {
45 m_layer = loc.m_layer;
46 m_exact_layer_coords = loc.m_exact_layer_coords;
49 Location::Location(Layer* layer) {
54 Location::~Location() {
57 void Location::reset() {
58 m_exact_layer_coords.x = 0;
59 m_exact_layer_coords.y = 0;
60 m_exact_layer_coords.z = 0;
64 Location& Location::operator=(
const Location& rhs) {
65 m_layer = rhs.m_layer;
66 m_exact_layer_coords.x = rhs.m_exact_layer_coords.x;
67 m_exact_layer_coords.y = rhs.m_exact_layer_coords.y;
68 m_exact_layer_coords.z = rhs.m_exact_layer_coords.z;
72 Map* Location::getMap()
const {
76 return m_layer->getMap();
79 void Location::setLayer(Layer* layer) {
83 Layer* Location::getLayer()
const {
87 void Location::setExactLayerCoordinates(
const ExactModelCoordinate& coordinates) {
89 throw NotSet(INVALID_LAYER_SET);
91 m_exact_layer_coords = coordinates;
94 void Location::setLayerCoordinates(
const ModelCoordinate& coordinates) {
98 void Location::setMapCoordinates(
const ExactModelCoordinate& coordinates) {
100 throw NotSet(INVALID_LAYER_SET);
102 m_exact_layer_coords = m_layer->getCellGrid()->toExactLayerCoordinates(coordinates);
105 ExactModelCoordinate& Location::getExactLayerCoordinatesRef() {
106 return m_exact_layer_coords;
109 ExactModelCoordinate Location::getExactLayerCoordinates()
const {
110 return m_exact_layer_coords;
113 ModelCoordinate Location::getLayerCoordinates()
const {
117 ExactModelCoordinate Location::getMapCoordinates()
const {
118 return m_layer->getCellGrid()->toMapCoordinates(m_exact_layer_coords);
121 bool Location::isValid()
const {
122 return isValid(m_layer);
125 bool Location::isValid(
const Layer* layer)
const {
126 return (layer && layer->getCellGrid());
129 ExactModelCoordinate Location::getExactLayerCoordinates(
const Layer* layer)
const {
130 if (!isValid(layer)) {
131 throw NotSet(INVALID_LAYER_GET);
134 if (layer == m_layer) {
135 return m_exact_layer_coords;
138 CellGrid* cg1 = m_layer->getCellGrid();
139 CellGrid* cg2 = layer->getCellGrid();
140 return cg2->toExactLayerCoordinates(cg1->toMapCoordinates(m_exact_layer_coords));
143 ModelCoordinate Location::getLayerCoordinates(
const Layer* layer)
const {
144 if (!isValid(layer)) {
145 throw NotSet(INVALID_LAYER_GET);
148 if (layer == m_layer) {
149 return getLayerCoordinates();
152 CellGrid* cg1 = m_layer->getCellGrid();
153 CellGrid* cg2 = layer->getCellGrid();
154 return cg2->toLayerCoordinates(cg1->toMapCoordinates(m_exact_layer_coords));
157 double Location::getCellOffsetDistance()
const {
158 const ExactModelCoordinate& pt = m_exact_layer_coords;
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);
164 std::ostream&
operator<<(std::ostream& os,
const Location& l) {
166 return os <<
"x=" << p.x <<
", y=" << p.y;
169 double Location::getMapDistanceTo(
const Location& location)
const{
170 ExactModelCoordinate current = getMapCoordinates();
171 ExactModelCoordinate target = location.getMapCoordinates();
173 double rx = current.x - target.x;
174 double ry = current.y - target.y;
175 double rz = current.z - target.z;
177 return Mathd::Sqrt(rx*rx + ry*ry + rz*rz);
180 double Location::getLayerDistanceTo(
const Location& location)
const{
181 ModelCoordinate current = getLayerCoordinates();
182 ModelCoordinate target = location.getLayerCoordinates(m_layer);
184 double rx = current.x - target.x;
185 double ry = current.y - target.y;
186 double rz = current.z - target.z;
188 return Mathd::Sqrt(rx*rx + ry*ry + rz*rz);
std::ostream & operator<<(std::ostream &os, const Location &l)
Point doublePt2intPt(DoublePoint pt)
DoublePoint intPt2doublePt(Point pt)
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...