CoordinateConverter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #ifndef __OpenKarto_CoordinateConverter_h__
21 #define __OpenKarto_CoordinateConverter_h__
22 
23 #include <OpenKarto/Geometry.h>
24 
25 namespace karto
26 {
27 
29 
30 
34 
41  {
42  public:
47  : m_Scale(20.0)
48  {
49  }
50 
51  public:
58  {
59  return value * m_Scale;
60  }
61 
68  inline Vector2i WorldToGrid(const Vector2d& rWorld, kt_bool flipY = false) const
69  {
70  kt_double gridX = (rWorld.GetX() - m_Offset.GetX()) * m_Scale;
71  kt_double gridY = 0.0;
72 
73  if (flipY == false)
74  {
75  gridY = (rWorld.GetY() - m_Offset.GetY()) * m_Scale;
76  }
77  else
78  {
79  gridY = (m_Size.GetHeight() / m_Scale - rWorld.GetY() + m_Offset.GetY()) * m_Scale;
80  }
81 
82  return Vector2i(static_cast<kt_int32s>(math::Round(gridX)), static_cast<kt_int32s>(math::Round(gridY)));
83  }
84 
91  inline Vector2d GridToWorld(const Vector2i& rGrid, kt_bool flipY = false) const
92  {
93  kt_double worldX = m_Offset.GetX() + rGrid.GetX() / m_Scale;
94  kt_double worldY = 0.0;
95 
96  if (flipY == false)
97  {
98  worldY = m_Offset.GetY() + rGrid.GetY() / m_Scale;
99  }
100  else
101  {
102  worldY = m_Offset.GetY() + (m_Size.GetHeight() - rGrid.GetY()) / m_Scale;
103  }
104 
105  return Vector2d(worldX, worldY);
106  }
107 
112  inline kt_double GetScale() const
113  {
114  return m_Scale;
115  }
116 
121  inline void SetScale(kt_double scale)
122  {
123  m_Scale = scale;
124  }
125 
130  inline const Vector2d& GetOffset() const
131  {
132  return m_Offset;
133  }
134 
139  inline void SetOffset(const Vector2d& rOffset)
140  {
141  m_Offset = rOffset;
142  }
143 
148  inline void SetSize(const Size2<kt_int32s>& rSize)
149  {
150  m_Size = rSize;
151  }
152 
157  inline const Size2<kt_int32s>& GetSize() const
158  {
159  return m_Size;
160  }
161 
166  inline kt_double GetResolution() const
167  {
168  return 1.0 / m_Scale;
169  }
170 
175  inline void SetResolution(kt_double resolution)
176  {
177  m_Scale = 1.0 / resolution;
178  }
179 
185  {
186  BoundingBox2 box;
187 
188  kt_double minX = GetOffset().GetX();
189  kt_double minY = GetOffset().GetY();
190  kt_double maxX = minX + GetSize().GetWidth() * GetResolution();
191  kt_double maxY = minY + GetSize().GetHeight() * GetResolution();
192 
193  box.SetMinimum(GetOffset());
194  box.SetMaximum(Vector2d(maxX, maxY));
195  return box;
196  }
197 
198  private:
201 
203  }; // CoordinateConverter
204 
206 
207 }
208 
209 #endif // __OpenKarto_CoordinateConverter_h__
void SetSize(const Size2< kt_int32s > &rSize)
bool kt_bool
Definition: Types.h:145
const Size2< kt_int32s > & GetSize() const
void SetMaximum(const Vector2d &rMaximum)
Definition: Geometry.h:1640
const T & GetY() const
Definition: Geometry.h:369
BoundingBox2 GetBoundingBox() const
#define KARTO_DEPRECATED
Definition: Macros.h:39
kt_double Round(kt_double value)
Definition: Math.h:114
KARTO_DEPRECATED kt_double Transform(kt_double value)
void SetOffset(const Vector2d &rOffset)
const T & GetX() const
Definition: Geometry.h:351
void SetResolution(kt_double resolution)
void SetScale(kt_double scale)
const Vector2d & GetOffset() const
Vector2i WorldToGrid(const Vector2d &rWorld, kt_bool flipY=false) const
Vector2d GridToWorld(const Vector2i &rGrid, kt_bool flipY=false) const
Vector2< kt_double > Vector2d
Definition: Geometry.h:610
double kt_double
Definition: Types.h:160
kt_double GetResolution() const
const T GetWidth() const
Definition: Geometry.h:80
Definition: Any.cpp:20
Vector2< kt_int32s > Vector2i
Definition: Geometry.h:600
const T GetHeight() const
Definition: Geometry.h:98
void SetMinimum(const Vector2d &rMinimum)
Definition: Geometry.h:1622


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36