libosmscout  1.1.1
Projection.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_UTIL_PROJECTION_H
2 #define OSMSCOUT_UTIL_PROJECTION_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2010 Tim Teulings
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
24 
25 #include <osmscout/GeoCoord.h>
26 
27 #include <osmscout/util/GeoBox.h>
29 #include <osmscout/util/Tiling.h>
30 
32 
33 namespace osmscout {
34 
44  {
45  protected:
46  double lon;
47  double lat;
48  double angle;
49  Magnification magnification;
50  double dpi;
51  size_t width;
52  size_t height;
53 
54  double lonMin;
55  double latMin;
56  double lonMax;
57  double latMax;
58 
59  double pixelSize;
60  double meterInPixel;
61  double meterInMM;
62 
63  public:
64 
69  {
70  public:
71  // this should be private, but that would exclude future projection
72  // implementors. I don't know a nice way to handle this
74 #ifdef OSMSCOUT_HAVE_SSE2
75  int count;
76  ALIGN16_BEG double lon[2] ALIGN16_END;
77  ALIGN16_BEG double lat[2] ALIGN16_END;
78  double* xPointer[2];
79  double* yPointer[2];
80 #endif
81 
82  public:
83  explicit BatchTransformer(const Projection& projection)
84  : projection(projection)
85 #ifdef OSMSCOUT_HAVE_SSE2
86  ,count(0)
87 #endif
88  {
89  }
90 
92  {
93  Flush();
94  }
95 
96  void GeoToPixel(double lon,
97  double lat,
98  double& x,
99  double& y)
100  {
101 #ifdef OSMSCOUT_HAVE_SSE2
102  if (projection.CanBatch()) {
103  this->lon[count]=lon;
104  this->lat[count]=lat;
105  xPointer[count]=&x;
106  yPointer[count]=&y;
107  count++;
108 
109  if (count==2) {
110  count=0;
111  projection.GeoToPixel(*this);
112  }
113  }
114  else {
115  projection.GeoToPixel(GeoCoord(lat,lon),
116  x,y);
117  }
118 #else
119  projection.GeoToPixel(GeoCoord(lat,lon),
120  x,y);
121 #endif
122  }
123 
124  void Flush()
125  {
126 #ifdef OSMSCOUT_HAVE_SSE2
127  if (count!=0) {
128  count=0;
129  projection.GeoToPixel(GeoCoord(lat[0],
130  lon[0]),
131  *xPointer[0],
132  *yPointer[0]);
133  }
134 #endif
135  }
136  };
137 
138  Projection();
139  virtual ~Projection() = default;
140 
141  virtual bool CanBatch() const = 0;
142  virtual bool IsValid() const = 0;
143 
147  virtual bool IsValidFor(const GeoCoord& coord) const = 0;
148 
149  GeoCoord GetCenter() const
150  {
151  return GeoCoord(lat,lon);
152  }
153 
158  double GetLon() const
159  {
160  return lon;
161  }
162 
167  double GetLat() const
168  {
169  return lat;
170  }
171 
176  double GetAngle() const
177  {
178  return angle;
179  }
180 
184  size_t GetWidth() const
185  {
186  return width;
187  }
188 
192  size_t GetHeight() const
193  {
194  return height;
195  }
196 
200  Magnification GetMagnification() const
201  {
202  return magnification;
203  }
204 
208  double GetDPI() const
209  {
210  return dpi;
211  }
212 
216  bool GeoIsIn(double lon, double lat) const
217  {
218  return lon>=lonMin && lon<=lonMax && lat>=latMin && lat<=latMax;
219  }
220 
224  bool GeoIsIn(double lonMin, double latMin,
225  double lonMax, double latMax) const
226  {
227  return !(lonMin>this->lonMax ||
228  lonMax<this->lonMin ||
229  latMin>this->latMax ||
230  latMax<this->latMin);
231  }
232 
233  GeoBox GetDimensions() const
234  {
235  return GeoBox(GeoCoord(latMin,lonMin),
236  GeoCoord(latMax,lonMax));
237  }
238 
242  void GetDimensions(GeoBox& boundingBox) const
243  {
244  boundingBox.Set(GeoCoord(latMin,lonMin),
245  GeoCoord(latMax,lonMax));
246  }
247 
251  double GetPixelSize() const
252  {
253  return pixelSize;
254  }
255 
259  double GetMeterInPixel() const
260  {
261  return meterInPixel;
262  }
263 
267  double GetMeterInMM() const
268  {
269  return meterInMM;
270  }
271 
280  double ConvertWidthToPixel(double width) const
281  {
282  return width*dpi/25.4;
283  }
284 
293  double ConvertPixelToWidth(double pixel) const
294  {
295  return pixel*25.4/dpi;
296  }
297 
305  virtual bool PixelToGeo(double x, double y,
306  double& lon, double& lat) const = 0;
307 
314  virtual bool GeoToPixel(const GeoCoord& coord,
315  double& x, double& y) const = 0;
316 
317  protected:
318  virtual void GeoToPixel(const BatchTransformer& transformData) const = 0;
319 
320  friend class BatchTransformer;
321  };
322 
334  {
335  protected:
336  bool valid;
337 
338  double latOffset;
339  double angleSin;
340  double angleCos;
341  double angleNegSin;
342  double angleNegCos;
343 
344  double scale;
345  double scaleGradtorad;
346 
347  double scaledLatDeriv;
349 
350  public:
351  static const double MaxLat;
352  static const double MinLat;
353  static const double MaxLon;
354  static const double MinLon;
355 
357 
358  bool CanBatch() const override
359  {
360  return false;
361  }
362 
363  bool IsValid() const override
364  {
365  return valid;
366  }
367 
368  bool IsValidFor(const GeoCoord& coord) const override
369  {
370  return coord.GetLat() >= MinLat && coord.GetLat() <= MaxLat &&
371  coord.GetLon() >= MinLon && coord.GetLon() <= MaxLon;
372  }
373 
374  bool Set(const GeoCoord& coord,
375  const Magnification& magnification,
376  size_t width,size_t height)
377  {
378  return Set(coord,0.0,magnification,GetDPI(),width,height);
379  }
380 
381  bool Set(const GeoCoord& coord,
382  double angle,
383  const Magnification& magnification,
384  size_t width, size_t height)
385  {
386  return Set(coord,angle,magnification,GetDPI(),width,height);
387  }
388 
389  bool Set(const GeoCoord& coord,
390  const Magnification& magnification,
391  double dpi,
392  size_t width, size_t height)
393  {
394  return Set(coord,0.0,magnification,dpi,width,height);
395  }
396 
419  bool Set(const GeoCoord& coord,
420  double angle,
421  const Magnification& magnification,
422  double dpi,
423  size_t width, size_t height);
424 
425  bool PixelToGeo(double x, double y,
426  double& lon, double& lat) const override;
427 
428  bool PixelToGeo(double x, double y,
429  GeoCoord &coord) const
430  {
431  double lat;
432  double lon;
433  if (PixelToGeo(x,y,lon,lat)){
434  coord.Set(lat,lon);
435  return true;
436  }
437  return false;
438  }
439 
440  bool GeoToPixel(const GeoCoord& coord,
441  double& x, double& y) const override;
442 
443  bool Move(double horizPixel,
444  double vertPixel);
445 
446  bool MoveUp(double pixel)
447  {
448  return Move(0,pixel);
449  }
450 
451  bool MoveDown(double pixel)
452  {
453  return Move(0,-pixel);
454  }
455 
456  bool MoveLeft(double pixel)
457  {
458  return Move(-pixel,0);
459  }
460 
461  bool MoveRight(double pixel)
462  {
463  return Move(pixel,0);
464  }
465 
467  {
468  return useLinearInterpolation;
469  }
470 
475  void SetLinearInterpolationUsage(bool useLinearInterpolation)
476  {
477  this->useLinearInterpolation=useLinearInterpolation;
478  }
479 
480  protected:
481  void GeoToPixel(const BatchTransformer& transformData) const override;
482  };
483 
484 
492  {
493  protected:
494  bool valid;
495 
496  double lonOffset;
497  double latOffset;
498  double scale;
499  double scaleGradtorad;
500 
501  double scaledLatDeriv;
503 
504 #ifdef OSMSCOUT_HAVE_SSE2
505  //some extra vars for special sse needs
506  v2df sse2LonOffset;
507  v2df sse2LatOffset;
508  v2df sse2Scale;
509  v2df sse2ScaleGradtorad;
510  v2df sse2Height;
511 #endif
512 
513  protected:
514  virtual bool SetInternal(double lonMin,double latMin,
515  double lonMax,double latMax,
516  const Magnification& magnification,
517  double dpi,
518  size_t width,size_t height);
519 
520  public:
521  TileProjection();
522 
523  bool CanBatch() const override
524  {
525  return true;
526  }
527 
528  bool IsValid() const override
529  {
530  return valid;
531  }
532 
533  bool IsValidFor(const GeoCoord& coord) const override
534  {
535  return coord.GetLat() >= -85.0511 && coord.GetLat() <= +85.0511 &&
536  coord.GetLon() >= -180.0 && coord.GetLon() <= +180.0;
537  }
538 
539  bool Set(const OSMTileId& tile,
540  const Magnification& magnification,
541  size_t width, size_t height)
542  {
543  return Set(tile,magnification,GetDPI(),width,height);
544  }
545 
546  bool Set(const OSMTileId& tile,
547  const Magnification& magnification,
548  double dpi,
549  size_t width, size_t height);
550 
551  bool Set(const OSMTileIdBox& tileBox,
552  const Magnification& magnification,
553  double dpi,
554  size_t width, size_t height);
555 
556  bool PixelToGeo(double x, double y,
557  double& lon, double& lat) const override;
558 
559  bool GeoToPixel(const GeoCoord& coord,
560  double& x, double& y) const override;
561 
563  {
564  return useLinearInterpolation;
565  }
566 
572  {
573  useLinearInterpolation = b;
574  }
575 
576  protected:
577 
578  void GeoToPixel(const BatchTransformer& transformData) const override;
579  };
580 
581 }
582 
583 #endif
bool useLinearInterpolation
switch to enable linear interpolation of latitude to pixel computation
Definition: Projection.h:502
double pixelSize
Size of a pixel in meter.
Definition: Projection.h:59
double ConvertWidthToPixel(double width) const
Definition: Projection.h:280
GeoCoord GetCenter() const
Definition: Projection.h:149
double GetAngle() const
Definition: Projection.h:176
void SetLinearInterpolationUsage(bool b)
Definition: Projection.h:571
double GetLon() const
Definition: Projection.h:158
double meterInMM
Number of on screen millimeters for one meter on the ground.
Definition: Projection.h:61
double scaleGradtorad
Precalculated scale*Gradtorad.
Definition: Projection.h:499
double lonOffset
Definition: Projection.h:496
double angle
Display rotation angle in radians, canvas clockwise.
Definition: Projection.h:48
Definition: Projection.h:491
double GetPixelSize() const
Definition: Projection.h:251
double scaledLatDeriv
precalculated derivation of "latToYPixel" function in projection center scaled by gradtorad * scale ...
Definition: Projection.h:501
bool IsValidFor(const GeoCoord &coord) const override
Definition: Projection.h:368
bool MoveRight(double pixel)
Definition: Projection.h:461
double latOffset
Definition: Projection.h:497
Definition: Projection.h:333
double angleNegSin
Definition: Projection.h:341
Magnification GetMagnification() const
Definition: Projection.h:200
bool CanBatch() const override
Definition: Projection.h:358
bool GeoIsIn(double lon, double lat) const
Definition: Projection.h:216
bool Set(const OSMTileId &tile, const Magnification &magnification, size_t width, size_t height)
Definition: Projection.h:539
Magnification magnification
Current magnification.
Definition: Projection.h:49
size_t GetHeight() const
Definition: Projection.h:192
double latMax
Latitude of the lower right corner of the image.
Definition: Projection.h:57
bool Set(const GeoCoord &coord, const Magnification &magnification, size_t width, size_t height)
Definition: Projection.h:374
size_t width
Width of image.
Definition: Projection.h:51
double GetMeterInPixel() const
Definition: Projection.h:259
Definition: Projection.h:43
Definition: Projection.h:68
Definition: Area.h:38
double lonMax
Longitude of the lower right corner of the image.
Definition: Projection.h:56
double latMin
Latitude of the upper left corner of the image.
Definition: Projection.h:55
double scaledLatDeriv
precalculated derivation of "latToYPixel" function in projection center scaled by gradtorad * scale ...
Definition: Projection.h:347
bool MoveDown(double pixel)
Definition: Projection.h:451
double scale
Definition: Projection.h:344
double angleCos
Definition: Projection.h:340
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
static const double MinLat
Definition: Projection.h:352
double GetMeterInMM() const
Definition: Projection.h:267
double lonMin
Longitude of the upper left corner of the image.
Definition: Projection.h:54
double angleNegCos
Definition: Projection.h:342
bool CanBatch() const override
Definition: Projection.h:523
double ConvertPixelToWidth(double pixel) const
Definition: Projection.h:293
bool valid
projection is valid
Definition: Projection.h:494
double scale
Definition: Projection.h:498
static const double MaxLat
Definition: Projection.h:351
bool Set(const GeoCoord &coord, const Magnification &magnification, double dpi, size_t width, size_t height)
Definition: Projection.h:389
double lat
Latitude coordinate of the center of the image.
Definition: Projection.h:47
double lon
Longitude coordinate of the center of the image.
Definition: Projection.h:46
size_t GetWidth() const
Definition: Projection.h:184
size_t height
Height of image.
Definition: Projection.h:52
double GetDPI() const
Definition: Projection.h:208
bool IsLinearInterpolationEnabled() const
Definition: Projection.h:562
bool IsValid() const override
Definition: Projection.h:363
static const double MinLon
Definition: Projection.h:354
const Projection & projection
Definition: Projection.h:73
bool IsValid() const override
Definition: Projection.h:528
bool valid
projection is valid
Definition: Projection.h:336
GeoBox GetDimensions() const
Definition: Projection.h:233
bool Set(const GeoCoord &coord, double angle, const Magnification &magnification, size_t width, size_t height)
Definition: Projection.h:381
static const double MaxLon
Definition: Projection.h:353
bool GeoIsIn(double lonMin, double latMin, double lonMax, double latMax) const
Definition: Projection.h:224
double meterInPixel
Number of on screen pixel for one meter on the ground.
Definition: Projection.h:60
bool MoveLeft(double pixel)
Definition: Projection.h:456
double angleSin
Definition: Projection.h:339
double dpi
Screen DPI.
Definition: Projection.h:50
void GetDimensions(GeoBox &boundingBox) const
Definition: Projection.h:242
~BatchTransformer()
Definition: Projection.h:91
void Flush()
Definition: Projection.h:124
virtual bool CanBatch() const =0
void SetLinearInterpolationUsage(bool useLinearInterpolation)
Definition: Projection.h:475
bool IsLinearInterpolationEnabled() const
Definition: Projection.h:466
BatchTransformer(const Projection &projection)
Definition: Projection.h:83
double GetLat() const
Definition: Projection.h:167
virtual bool GeoToPixel(const GeoCoord &coord, double &x, double &y) const =0
bool PixelToGeo(double x, double y, GeoCoord &coord) const
Definition: Projection.h:428
double scaleGradtorad
Precalculated scale*Gradtorad.
Definition: Projection.h:345
bool IsValidFor(const GeoCoord &coord) const override
Definition: Projection.h:533
void GeoToPixel(double lon, double lat, double &x, double &y)
Definition: Projection.h:96
double latOffset
Absolute and untransformed screen position of lat coordinate.
Definition: Projection.h:338
bool MoveUp(double pixel)
Definition: Projection.h:446
bool useLinearInterpolation
switch to enable linear interpolation of latitude to pixel computation
Definition: Projection.h:348