2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
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.
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.
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,
29 #include "geocoordinate.h"
32 #include "scenecoordinate.h"
34 SceneCoordinate::SceneCoordinate() :
38 qDebug() << __PRETTY_FUNCTION__;
41 SceneCoordinate::SceneCoordinate(double x, double y) :
45 qDebug() << __PRETTY_FUNCTION__;
48 SceneCoordinate::SceneCoordinate(const GeoCoordinate &coordinate)
50 qDebug() << __PRETTY_FUNCTION__;
52 convertFrom(coordinate);
55 qreal SceneCoordinate::azimuthTo(const SceneCoordinate &to) const
57 qDebug() << __PRETTY_FUNCTION__;
59 // construct a line from this coordinate to target coordinate
60 QLineF line = QLineF(this->toPointF(), to.toPointF());
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();
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
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
76 Q_ASSERT_X(angle >= 0.0 && angle <= 360, "return value", "value is out of range");
81 void SceneCoordinate::convertFrom(const GeoCoordinate &coordinate)
83 qDebug() << __PRETTY_FUNCTION__;
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);
90 m_x = worldX * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_X;
91 m_y = worldY * OSM_TILES_PER_SIDE * OSM_TILE_SIZE_Y;
93 normalize(m_x, OSM_MAP_MIN_PIXEL_X, OSM_MAP_MAX_PIXEL_X);
96 bool SceneCoordinate::isNull() const
98 qDebug() << __PRETTY_FUNCTION__;
100 if (m_x == 0 && m_y == 0)
106 void SceneCoordinate::normalize(double &value, int min, int max)
108 qDebug() << __PRETTY_FUNCTION__;
109 Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
111 while (int(value) < min)
112 value += max - min + 1;
114 while (int(value) > max)
115 value -= max - min + 1;
118 void SceneCoordinate::setX(double x)
120 qDebug() << __PRETTY_FUNCTION__;
125 void SceneCoordinate::setY(double y)
127 qDebug() << __PRETTY_FUNCTION__;
132 QPointF SceneCoordinate::toPointF() const
134 qDebug() << __PRETTY_FUNCTION__;
136 return QPointF(m_x, m_y);
139 double SceneCoordinate::x() const
141 qDebug() << __PRETTY_FUNCTION__;
146 double SceneCoordinate::y() const
148 qDebug() << __PRETTY_FUNCTION__;
153 SceneCoordinate::operator QVariant() const
155 return QVariant::fromValue(*this);
158 QDebug operator<<(QDebug dbg, const SceneCoordinate &coordinate)
160 dbg.nospace() << "(" << coordinate.x() << ", " << coordinate.y() << ")";
165 SceneCoordinate & SceneCoordinate::operator*=(double factor)
172 SceneCoordinate & SceneCoordinate::operator+=(const SceneCoordinate &coordinate)
174 m_x += coordinate.x();
175 m_y += coordinate.y();
179 SceneCoordinate & SceneCoordinate::operator-=(const SceneCoordinate &coordinate)
181 m_x -= coordinate.x();
182 m_y -= coordinate.y();
186 const SceneCoordinate operator* (double factor, const SceneCoordinate &coordinate)
188 return SceneCoordinate(coordinate) *= factor;
191 const SceneCoordinate SceneCoordinate::operator+(const SceneCoordinate &other) const
193 return SceneCoordinate(*this) += other;
196 const SceneCoordinate SceneCoordinate::operator-(const SceneCoordinate &other) const
198 return SceneCoordinate(*this) -= other;