923ad90474c9a43356d5da8a8fc311eb908af80c
[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 <QDebug>
23
24 #include "geocoordinate.h"
25 #include "map/mapcommon.h"
26
27 #include "scenecoordinate.h"
28
29 SceneCoordinate::SceneCoordinate() :
30         m_x(0),
31         m_y(0)
32 {
33     qDebug() << __PRETTY_FUNCTION__;
34 }
35
36 SceneCoordinate::SceneCoordinate(double x, double y) :
37         m_x(x),
38         m_y(y)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41 }
42
43 SceneCoordinate::SceneCoordinate(GeoCoordinate &coordinate)
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     convertFrom(coordinate);
48 }
49
50 void SceneCoordinate::convertFrom(const GeoCoordinate &coordinate)
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     // calculate x & y positions in the map (0..1)
55     double worldX = static_cast<double>((coordinate.longitude() + 180.0) / 360.0);
56     double worldY = static_cast<double>((1.0 - log(tan(coordinate.latitude() * M_PI / 180.0) + 1.0
57                                 / cos(coordinate.latitude() * M_PI / 180.0)) / M_PI) / 2.0);
58
59     m_x = worldX * MAX_TILES_PER_SIDE * TILE_SIZE_X;
60     m_y = worldY * MAX_TILES_PER_SIDE * TILE_SIZE_Y;
61
62     normalize(m_x, MAP_MIN_PIXEL_X, MAP_MAX_PIXEL_X);
63 }
64
65 bool SceneCoordinate::isNull() const
66 {
67     qDebug() << __PRETTY_FUNCTION__;
68
69     if (m_x == 0 && m_y == 0)
70         return true;
71
72     return false;
73 }
74
75 void SceneCoordinate::normalize(double &value, int min, int max)
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78     Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
79
80     while (int(value) < min)
81         value += max - min + 1;
82
83     while (int(value) > max)
84         value -= max - min + 1;
85 }
86
87 double SceneCoordinate::x() const
88 {
89     qDebug() << __PRETTY_FUNCTION__;
90
91     return m_x;
92 }
93
94 double SceneCoordinate::y() const
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     return m_y;
99 }
100
101 void SceneCoordinate::setX(double x)
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     m_x = x;
106 }
107
108 void SceneCoordinate::setY(double y)
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111
112     m_y = y;
113 }
114
115 QDebug operator<<(QDebug dbg, const SceneCoordinate &coordinate)
116 {
117     dbg.nospace() << "(" << coordinate.x() << ", " << coordinate.y() << ")";
118
119     return dbg.space();
120 }