Direction indicator triange updating
[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     QLineF line = QLineF(this->toPointF(), to.toPointF());
58     return -line.angle() + 90.0;
59 }
60
61 void SceneCoordinate::convertFrom(const GeoCoordinate &coordinate)
62 {
63     qDebug() << __PRETTY_FUNCTION__;
64
65     // calculate x & y positions in the map (0..1)
66     double worldX = static_cast<double>((coordinate.longitude() + 180.0) / 360.0);
67     double worldY = static_cast<double>((1.0 - log(tan(coordinate.latitude() * M_PI / 180.0) + 1.0
68                                 / cos(coordinate.latitude() * M_PI / 180.0)) / M_PI) / 2.0);
69
70     m_x = worldX * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_X;
71     m_y = worldY * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_Y;
72
73     normalize(m_x, OSM_MAP_MIN_PIXEL_X, OSM_MAP_MAX_PIXEL_X);
74 }
75
76 bool SceneCoordinate::isNull() const
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79
80     if (m_x == 0 && m_y == 0)
81         return true;
82
83     return false;
84 }
85
86 void SceneCoordinate::normalize(double &value, int min, int max)
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89     Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
90
91     while (int(value) < min)
92         value += max - min + 1;
93
94     while (int(value) > max)
95         value -= max - min + 1;
96 }
97
98 void SceneCoordinate::setX(double x)
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     m_x = x;
103 }
104
105 void SceneCoordinate::setY(double y)
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108
109     m_y = y;
110 }
111
112 QPointF SceneCoordinate::toPointF() const
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     return QPointF(m_x, m_y);
117 }
118
119 double SceneCoordinate::x() const
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     return m_x;
124 }
125
126 double SceneCoordinate::y() const
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     return m_y;
131 }
132
133 SceneCoordinate::operator QVariant() const
134 {
135     return QVariant::fromValue(*this);
136 }
137
138 QDebug operator<<(QDebug dbg, const SceneCoordinate &coordinate)
139 {
140     dbg.nospace() << "(" << coordinate.x() << ", " << coordinate.y() << ")";
141
142     return dbg.space();
143 }
144
145 SceneCoordinate & SceneCoordinate::operator*=(double factor)
146 {
147     m_x *= factor;
148     m_y *= factor;
149     return *this;
150 }
151
152 SceneCoordinate & SceneCoordinate::operator+=(const SceneCoordinate &coordinate)
153 {
154     m_x += coordinate.x();
155     m_y += coordinate.y();
156     return *this;
157 }
158
159 SceneCoordinate & SceneCoordinate::operator-=(const SceneCoordinate &coordinate)
160 {
161     m_x -= coordinate.x();
162     m_y -= coordinate.y();
163     return *this;
164 }
165
166 const SceneCoordinate operator* (double factor, const SceneCoordinate &coordinate)
167 {
168     return SceneCoordinate(coordinate) *= factor;
169 }
170
171 const SceneCoordinate SceneCoordinate::operator+(const SceneCoordinate &other) const
172 {
173   return SceneCoordinate(*this) += other;
174 }
175
176 const SceneCoordinate SceneCoordinate::operator-(const SceneCoordinate &other) const
177 {
178   return SceneCoordinate(*this) -= other;
179 }