Fixed SceneCoordinate::azimuthTo() angle calculation
[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 #include <QLineF>
26 #include <QPointF>
27 #include <QVariant>
28
29 #include "geocoordinate.h"
30 #include "map/osm.h"
31
32 #include "scenecoordinate.h"
33
34 SceneCoordinate::SceneCoordinate() :
35         m_x(0),
36         m_y(0)
37 {
38     qDebug() << __PRETTY_FUNCTION__;
39 }
40
41 SceneCoordinate::SceneCoordinate(double x, double y) :
42         m_x(x),
43         m_y(y)
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46 }
47
48 SceneCoordinate::SceneCoordinate(const GeoCoordinate &coordinate)
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     convertFrom(coordinate);
53 }
54
55 qreal SceneCoordinate::azimuthTo(const SceneCoordinate &to) const
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58
59     // construct a line from this coordinate to target coordinate
60     QLineF line = QLineF(this->toPointF(), to.toPointF());
61
62     // get the angle from the line. Because QLineF::angle() returns positive value for a
63     // counter-clockwise direction, and we want the positive value to be in clockwise direction,
64     // the value is negated
65     qreal angle = -line.angle();
66
67     // QLineF::angle() returns value which has zero at the 3 o'clock position, and we want to have
68     // the zero pointing to the north, so we have to add 90 degrees
69     angle += 90;
70
71     // QLineF::angle() returns values from -180 to 180, an we want only positive values from 0 to
72     // 360 degrees, so full 360 degrees is added if the result would otherwise be negative
73     if (angle < 0)
74         angle += 360;
75
76     Q_ASSERT_X(angle >= 0.0 && angle <= 360, "return value", "value is out of range");
77
78     return angle;
79 }
80
81 void SceneCoordinate::convertFrom(const GeoCoordinate &coordinate)
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84
85     // calculate x & y positions in the map (0..1)
86     double worldX = static_cast<double>((coordinate.longitude() + 180.0) / 360.0);
87     double worldY = static_cast<double>((1.0 - log(tan(coordinate.latitude() * M_PI / 180.0) + 1.0
88                                 / cos(coordinate.latitude() * M_PI / 180.0)) / M_PI) / 2.0);
89
90     m_x = worldX * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_X;
91     m_y = worldY * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_Y;
92
93     normalize(m_x, OSM_MAP_MIN_PIXEL_X, OSM_MAP_MAX_PIXEL_X);
94 }
95
96 bool SceneCoordinate::isNull() const
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     if (m_x == 0 && m_y == 0)
101         return true;
102
103     return false;
104 }
105
106 void SceneCoordinate::normalize(double &value, int min, int max)
107 {
108     qDebug() << __PRETTY_FUNCTION__;
109     Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
110
111     while (int(value) < min)
112         value += max - min + 1;
113
114     while (int(value) > max)
115         value -= max - min + 1;
116 }
117
118 void SceneCoordinate::setX(double x)
119 {
120     qDebug() << __PRETTY_FUNCTION__;
121
122     m_x = x;
123 }
124
125 void SceneCoordinate::setY(double y)
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128
129     m_y = y;
130 }
131
132 QPointF SceneCoordinate::toPointF() const
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     return QPointF(m_x, m_y);
137 }
138
139 double SceneCoordinate::x() const
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142
143     return m_x;
144 }
145
146 double SceneCoordinate::y() const
147 {
148     qDebug() << __PRETTY_FUNCTION__;
149
150     return m_y;
151 }
152
153 SceneCoordinate::operator QVariant() const
154 {
155     return QVariant::fromValue(*this);
156 }
157
158 QDebug operator<<(QDebug dbg, const SceneCoordinate &coordinate)
159 {
160     dbg.nospace() << "(" << coordinate.x() << ", " << coordinate.y() << ")";
161
162     return dbg.space();
163 }
164
165 SceneCoordinate & SceneCoordinate::operator*=(double factor)
166 {
167     m_x *= factor;
168     m_y *= factor;
169     return *this;
170 }
171
172 SceneCoordinate & SceneCoordinate::operator+=(const SceneCoordinate &coordinate)
173 {
174     m_x += coordinate.x();
175     m_y += coordinate.y();
176     return *this;
177 }
178
179 SceneCoordinate & SceneCoordinate::operator-=(const SceneCoordinate &coordinate)
180 {
181     m_x -= coordinate.x();
182     m_y -= coordinate.y();
183     return *this;
184 }
185
186 const SceneCoordinate operator* (double factor, const SceneCoordinate &coordinate)
187 {
188     return SceneCoordinate(coordinate) *= factor;
189 }
190
191 const SceneCoordinate SceneCoordinate::operator+(const SceneCoordinate &other) const
192 {
193   return SceneCoordinate(*this) += other;
194 }
195
196 const SceneCoordinate SceneCoordinate::operator-(const SceneCoordinate &other) const
197 {
198   return SceneCoordinate(*this) -= other;
199 }