ToolMap
Loading...
Searching...
No Matches
tmgisscale.h
1/***************************************************************************
2 tmgisscale.h
3 Deals with GIS scale and conversion
4 -------------------
5 copyright : (C) 2007 CREALP Lucien Schreiber
6 ***************************************************************************/
7
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#ifndef _TM_GIS_SCALE_H_
18#define _TM_GIS_SCALE_H_
19
20#include <wx/wxprec.h>
21
22#ifndef WX_PRECOMP
23#include <wx/wx.h>
24#endif
25
26#include <wx/graphics.h>
27
28#include <algorithm>
29
30#include "../core/projectdefmemory.h"
31#include "../core/tmcoordconvert.h"
32#include "../core/vrrealrect.h"
33#include "ogrsf_frmts.h" // OGR accessing
34
35const int tmSCALE_MARGIN = 10; // margin between image and border when zooming to full extent
36bool tmIsSameDouble(double left, double right, double epsilon);
37
38/***************************************************************************/
45 private:
46 double GetDifferences(const double& min, const double& max);
47
48 public:
49 double x_min;
50 double y_min;
51 double x_max;
52 double y_max;
53
55 : x_min(0.0),
56 y_min(0.0),
57 x_max(0.0),
58 y_max(0.0) {}
59
60 tmRealRect(double xmin, double ymin, double xmax, double ymax)
61 : x_min(xmin),
62 y_min(ymin),
63 x_max(xmax),
64 y_max(ymax) {}
65
66 bool Clip(const tmRealRect& src, tmRealRect& result);
67
68 bool operator==(const tmRealRect& pt) const {
69 return wxIsSameDouble(x_min, pt.x_min) && wxIsSameDouble(y_min, pt.y_min) && wxIsSameDouble(x_max, pt.x_max) &&
70 wxIsSameDouble(y_max, pt.y_max);
71 }
72
73 bool operator!=(const tmRealRect& pt) const {
74 if (!wxIsSameDouble(x_min, pt.x_min)) {
75 return true;
76 }
77 if (!wxIsSameDouble(y_min, pt.y_min)) {
78 return true;
79 }
80 if (!wxIsSameDouble(x_max, pt.x_max)) {
81 return true;
82 }
83 if (!wxIsSameDouble(y_max, pt.y_max)) {
84 return true;
85 }
86 return false;
87 }
88
89 double GetWidth() {
90 return GetDifferences(x_min, x_max);
91 }
92
93 double GetHeight() {
94 return GetDifferences(y_min, y_max);
95 }
96
97 OGRGeometry* GetPolygonGeometry();
98};
99
100class tmGISScale : public wxObject {
101 private:
102 tmRealRect m_ExtentMaxLayers;
103 wxRect m_ExtentWnd;
104 tmRealRect m_ExtentWndReal;
105 double m_PixelSize;
106 wxSize m_ExtentWndMM;
107 long m_UnitScale;
108 double m_WidthDistanceInM;
109 wxSize m_PPI;
110 PRJDEF_UNIT_TYPE m_ProjectUnit;
111 PRJDEF_PROJ_TYPE m_ProjectProjection;
112
113 void InitMemberValues();
114
115 void _ComputeUnitScale();
116
117 protected:
118 public:
119 tmGISScale();
120
121 ~tmGISScale();
122
123 // static functions
124 static tmRealRect ComputeMaxCoord(const tmRealRect& r1, const tmRealRect& r2);
125
126 void SetPPI(const wxSize& ppi) {
127 m_PPI = ppi;
128 }
129
130 void SetUnitAndProj(PRJDEF_PROJ_TYPE proj, PRJDEF_UNIT_TYPE units);
131
132 PRJDEF_PROJ_TYPE GetProjection() {
133 return m_ProjectProjection;
134 }
135
136 // setter, mostly used for unit testing
137 void SetExtentWndReal(const tmRealRect& ExtentWndReal) {
138 m_ExtentWndReal = ExtentWndReal;
139 }
140
141 void SetWidthDistanceInM(double WidthDistanceInM) {
142 m_WidthDistanceInM = WidthDistanceInM;
143 }
144
145 // setter and getter for layers
146 void SetMaxLayersExtentAsExisting(const tmRealRect& r);
147
148 void SetMaxLayersExtent(const tmRealRect& r) {
149 m_ExtentMaxLayers = r;
150 }
151
152 tmRealRect GetMaxLayersExtent() {
153 return m_ExtentMaxLayers;
154 }
155
156 double GetLayersExtentWidth();
157
158 double GetLayersExtentHeight();
159
160 double GetPixelSize() {
161 return m_PixelSize;
162 }
163
164 wxPoint2DDouble GetTopLeftValue() {
165 return wxPoint2DDouble(m_ExtentWndReal.x_min, m_ExtentWndReal.y_max);
166 }
167
168 // setter and getters for windows
169 void SetWindowExtent(const wxRect& extent) {
170 m_ExtentWnd = extent;
171 }
172
173 wxRect GetWindowExtent() {
174 return m_ExtentWnd;
175 }
176
177 tmRealRect GetWindowExtentReal() {
178 return m_ExtentWndReal;
179 }
180
181 void SetWindowExtentMM(const wxSize& size) {
182 m_ExtentWndMM = size;
183 _ComputeUnitScale();
184 }
185
186 long GetActualScale() {
187 return m_UnitScale;
188 }
189
190 wxString GetVisibleWidthText();
191
192 double GetWindowRealWidth();
193
194 double GetwindowRealHeight();
195
196 // computing scale and reduction factor
197 double ComputeDivFactor(wxSize wnd_extent = wxDefaultSize);
198
199 bool ComputeMaxExtent();
200
201 wxSize ComputeCenterPxWnd(double divratio, wxSize wnd_extent = wxDefaultSize);
202
203 bool ComputeMaxExtentReal(wxSize wnd_offset = wxDefaultSize);
204
205 // size windows changed -> px size change too
206 bool ComptuteNewWindowSize(const wxSize& oldsize, const wxSize& newsize);
207
208 // scale function
209 void ComputeNewScaleExtent(const long& scale);
210
211 // zoom functions
212 double GetBestDivFactor(const wxRect& selected_rect);
213
214 void ComputeNewRealZoomExtent(const wxRect& calc_wnd_extent, const wxPoint& top_left);
215
216 void ComputePrevZoomExtent(double pixelsize, const wxPoint2DDouble& topleft);
217
218 // pan functions
219 void ComputeNewRealPanExtent(const wxPoint& offsetxtop);
220
221 // moving, zooming display
222 bool MoveViewTo(const vrRealRect& rect);
223
224 bool ZoomViewTo(const vrRealRect& rect);
225
226 // mostly used for snapping
227 double MetersToPixels(int meters);
228
229 double MetersToRealUnits(int meters);
230
231 // converting pixels - real (with inverting y axis)
232 inline wxRealPoint PixelToReal(wxPoint pt) {
233 return (wxRealPoint(m_ExtentWndReal.x_min + (((double)pt.x) * m_PixelSize),
234 m_ExtentWndReal.y_max - (((double)pt.y) * m_PixelSize)));
235 }
236
237 wxPoint RealToPixel(wxRealPoint realpt);
238
239 tmRealRect PixelsToReal(const wxRect& rectpx);
240
241 static inline double DifferenceDouble(const double& d1, const double& d2) {
242 if (wxIsSameDouble(d1, d2)) return 0;
243 if (d1 > d2)
244 return d1 - d2;
245 else
246 return d2 - d1;
247 }
248
249 static inline double DifferenceCoord(const double& coordmax, const double& coordmin) {
250 if (wxIsSameDouble(coordmax, coordmin)) return 0;
251 if (coordmax <= 0 && coordmin >= 0) return coordmax + coordmin;
252 if (coordmax >= 0) //&& coordmin < coordmax)
253 return coordmax - coordmin;
254
255 // if case isn't taken into account
256 wxString sFunction = wxString::FromAscii(__FUNCTION__);
257 wxString sFunctionLineError = wxString::Format(_T("%s line %d : "), sFunction.c_str(), __LINE__);
258 wxString sErrMsg = wxString::Format(_T("%s values are coord min-max : %.*f - %.*f "),
259 sFunctionLineError.c_str(), 2, coordmin, 2, coordmax);
260 wxASSERT_MSG(0, sErrMsg);
261
262 return 0;
263 }
264
265 static inline double RemoveFromCoord(const double& coord1, const double& value) {
266 if (coord1 > 0)
267 return coord1 - value;
268 else
269 return coord1 + value;
270 }
271
272 static inline double AppendToCoord(const double& coord1, const double& value) {
273 // if (coord1 > 0)
274 return coord1 + value;
275 // else
276 // return coord1 - value;
277 }
278
279 // extent validity
280 bool IsLayerExtentValid();
281};
282
283#endif
Definition tmgisscale.h:100
tmRealRect PixelsToReal(const wxRect &rectpx)
Convert pixel rectange to real units.
Definition tmgisscale.cpp:388
bool ComptuteNewWindowSize(const wxSize &oldsize, const wxSize &newsize)
Compute new real extend when windows size change.
Definition tmgisscale.cpp:231
Class representing real rectangle.
Definition tmgisscale.h:44
bool Clip(const tmRealRect &src, tmRealRect &result)
Clip rectangle with another.
Definition tmgisscale.cpp:40
Definition vrrealrect.h:27