00001 /* 00002 * Copyright (C) 2006-2011, SRI International (R) 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #pragma once 00019 00020 #ifndef __OpenKarto_CoordinateConverter_h__ 00021 #define __OpenKarto_CoordinateConverter_h__ 00022 00023 #include <OpenKarto/Geometry.h> 00024 00025 namespace karto 00026 { 00027 00029 00030 00034 00040 class CoordinateConverter 00041 { 00042 public: 00046 CoordinateConverter() 00047 : m_Scale(20.0) 00048 { 00049 } 00050 00051 public: 00057 KARTO_DEPRECATED inline kt_double Transform(kt_double value) 00058 { 00059 return value * m_Scale; 00060 } 00061 00068 inline Vector2i WorldToGrid(const Vector2d& rWorld, kt_bool flipY = false) const 00069 { 00070 kt_double gridX = (rWorld.GetX() - m_Offset.GetX()) * m_Scale; 00071 kt_double gridY = 0.0; 00072 00073 if (flipY == false) 00074 { 00075 gridY = (rWorld.GetY() - m_Offset.GetY()) * m_Scale; 00076 } 00077 else 00078 { 00079 gridY = (m_Size.GetHeight() / m_Scale - rWorld.GetY() + m_Offset.GetY()) * m_Scale; 00080 } 00081 00082 return Vector2i(static_cast<kt_int32s>(math::Round(gridX)), static_cast<kt_int32s>(math::Round(gridY))); 00083 } 00084 00091 inline Vector2d GridToWorld(const Vector2i& rGrid, kt_bool flipY = false) const 00092 { 00093 kt_double worldX = m_Offset.GetX() + rGrid.GetX() / m_Scale; 00094 kt_double worldY = 0.0; 00095 00096 if (flipY == false) 00097 { 00098 worldY = m_Offset.GetY() + rGrid.GetY() / m_Scale; 00099 } 00100 else 00101 { 00102 worldY = m_Offset.GetY() + (m_Size.GetHeight() - rGrid.GetY()) / m_Scale; 00103 } 00104 00105 return Vector2d(worldX, worldY); 00106 } 00107 00112 inline kt_double GetScale() const 00113 { 00114 return m_Scale; 00115 } 00116 00121 inline void SetScale(kt_double scale) 00122 { 00123 m_Scale = scale; 00124 } 00125 00130 inline const Vector2d& GetOffset() const 00131 { 00132 return m_Offset; 00133 } 00134 00139 inline void SetOffset(const Vector2d& rOffset) 00140 { 00141 m_Offset = rOffset; 00142 } 00143 00148 inline void SetSize(const Size2<kt_int32s>& rSize) 00149 { 00150 m_Size = rSize; 00151 } 00152 00157 inline const Size2<kt_int32s>& GetSize() const 00158 { 00159 return m_Size; 00160 } 00161 00166 inline kt_double GetResolution() const 00167 { 00168 return 1.0 / m_Scale; 00169 } 00170 00175 inline void SetResolution(kt_double resolution) 00176 { 00177 m_Scale = 1.0 / resolution; 00178 } 00179 00184 inline BoundingBox2 GetBoundingBox() const 00185 { 00186 BoundingBox2 box; 00187 00188 kt_double minX = GetOffset().GetX(); 00189 kt_double minY = GetOffset().GetY(); 00190 kt_double maxX = minX + GetSize().GetWidth() * GetResolution(); 00191 kt_double maxY = minY + GetSize().GetHeight() * GetResolution(); 00192 00193 box.SetMinimum(GetOffset()); 00194 box.SetMaximum(Vector2d(maxX, maxY)); 00195 return box; 00196 } 00197 00198 private: 00199 Size2<kt_int32s> m_Size; 00200 kt_double m_Scale; 00201 00202 Vector2d m_Offset; 00203 }; // CoordinateConverter 00204 00206 00207 } 00208 00209 #endif // __OpenKarto_CoordinateConverter_h__