Created osm.h, re-factoring, unit test script bug fix
[situare] / src / coordinates / scenecoordinate.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <cmath>
23
24 #include <QDebug>
25
26 #include "geocoordinate.h"
27 #include "map/osm.h"
28
29 #include "scenecoordinate.h"
30
31 SceneCoordinate::SceneCoordinate() :
32         m_x(0),
33         m_y(0)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36 }
37
38 SceneCoordinate::SceneCoordinate(double x, double y) :
39         m_x(x),
40         m_y(y)
41 {
42     qDebug() << __PRETTY_FUNCTION__;
43 }
44
45 SceneCoordinate::SceneCoordinate(GeoCoordinate &coordinate)
46 {
47     qDebug() << __PRETTY_FUNCTION__;
48
49     convertFrom(coordinate);
50 }
51
52 void SceneCoordinate::convertFrom(const GeoCoordinate &coordinate)
53 {
54     qDebug() << __PRETTY_FUNCTION__;
55
56     // calculate x & y positions in the map (0..1)
57     double worldX = static_cast<double>((coordinate.longitude() + 180.0) / 360.0);
58     double worldY = static_cast<double>((1.0 - log(tan(coordinate.latitude() * M_PI / 180.0) + 1.0
59                                 / cos(coordinate.latitude() * M_PI / 180.0)) / M_PI) / 2.0);
60
61     m_x = worldX * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_X;
62     m_y = worldY * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_Y;
63
64     normalize(m_x, OSM_MAP_MIN_PIXEL_X, OSM_MAP_MAX_PIXEL_X);
65 }
66
67 bool SceneCoordinate::isNull() const
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70
71     if (m_x == 0 && m_y == 0)
72         return true;
73
74     return false;
75 }
76
77 void SceneCoordinate::normalize(double &value, int min, int max)
78 {
79     qDebug() << __PRETTY_FUNCTION__;
80     Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
81
82     while (int(value) < min)
83         value += max - min + 1;
84
85     while (int(value) > max)
86         value -= max - min + 1;
87 }
88
89 double SceneCoordinate::x() const
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92
93     return m_x;
94 }
95
96 double SceneCoordinate::y() const
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     return m_y;
101 }
102
103 void SceneCoordinate::setX(double x)
104 {
105     qDebug() << __PRETTY_FUNCTION__;
106
107     m_x = x;
108 }
109
110 void SceneCoordinate::setY(double y)
111 {
112     qDebug() << __PRETTY_FUNCTION__;
113
114     m_y = y;
115 }
116
117 QDebug operator<<(QDebug dbg, const SceneCoordinate &coordinate)
118 {
119     dbg.nospace() << "(" << coordinate.x() << ", " << coordinate.y() << ")";
120
121     return dbg.space();
122 }