Fixed SceneCoordinate::azimuthTo() angle calculation
[situare] / src / coordinates / scenecoordinate.h
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
23 #ifndef SCENECOORDINATE_H
24 #define SCENECOORDINATE_H
25
26 #include <QDebug>
27 #include <QMetaType>
28
29 class GeoCoordinate;
30
31 /**
32 * @brief Scene coordinate
33 *
34 * @author Sami Rämö - sami.ramo@ixonos.com
35 */
36 class SceneCoordinate
37 {
38 public:
39     /**
40     * @brief Constructs a null coordinate
41     */
42     SceneCoordinate();
43
44     /**
45     * @brief Constructs a coordinate with given x and y
46     *
47     * @param x X value
48     * @param y Y value
49     */
50     SceneCoordinate(double x, double y);
51
52     /**
53     * @brief Constructs a coordinate with values converted from the given GeoCoordinate
54     *
55     * Uses convertFrom() method.
56     *
57     * @param coordinate Geological coordinate
58     */
59     SceneCoordinate(const GeoCoordinate &coordinate);
60
61 /*******************************************************************************
62  * MEMBER FUNCTIONS AND SLOTS
63  ******************************************************************************/
64 public:
65     /**
66     * @brief Returns the azimuth from this coordinate to other coordinate
67     *
68     * Zero is pointing to north. Returned value is from 0 to 360.
69     *
70     * @param to Target coordinate
71     * @returns Azimuth in degrees
72     */
73     qreal azimuthTo(const SceneCoordinate &to) const;
74
75     /**
76     * @brief Check if coordinate is (0.0, 0.0)
77     *
78     * @returns True if both X and Y are 0.0, otherwise false
79     */
80     bool isNull() const;
81
82     /**
83     * @brief Sets the latitude
84     *
85     * @param x X value
86     */
87     void setX(double x);
88
89     /**
90     * @brief Sets the longitude
91     *
92     * @param y Y value
93     */
94     void setY(double y);
95
96     /**
97     * @brief Convert to QPointF
98     *
99     * @returns a QPointF object
100     */
101     QPointF toPointF() const;
102
103     /**
104     * @brief Returns the x value
105     *
106     * @returns x
107     */
108     double x() const;
109
110     /**
111     * @brief Returns the y value
112     *
113     * @returns y
114     */
115     double y() const;
116
117 private:
118     /**
119      * @brief Convert values from GeoCoordinate
120      *
121      * Does run normalize() for the x value after the conversion to make sure that the result
122      * is inside the allowed map pixel values.
123      *
124      * In horizontal direction:
125      *    -180º equals scene pixel 0 (first scene pixel)
126      *    +180º equals -180º
127      *
128      *    scene has 2^18 * 256 - 1 = 67108864 pixels per side
129      *    one pixel width is 360º / 67108864 = 0.00000536441802978516º
130      *    so the last scene pixel is 180º - 0.00000536441802978516º = 179.99999463558197021484º
131      *
132      * @param coordinate Geological coordinate
133      */
134     void convertFrom(const GeoCoordinate &coordinate);
135
136     /**
137      * @brief Translate integer part of the given value between min and max
138      *
139      * If given value is not inside the given range (min <= value <= max), then the allowed range
140      * is adder or subtracted until the value does fit in the range. Only integer part is compared.
141      *
142      * @param value Value to be normalized
143      * @param min Minimum allowed value
144      * @param max Maximum allowed value
145      */
146     void normalize(double &value, int min, int max);
147
148 /*******************************************************************************
149  * DATA MEMBERS
150  ******************************************************************************/
151 private:
152     double m_x;         ///< X value
153     double m_y;         ///< Y value
154
155 /*******************************************************************************
156  * OPERATORS
157  ******************************************************************************/
158 public:
159     /**
160     * @brief Operator for creating QVariant
161     */
162     operator QVariant() const;
163
164     /**
165     * @brief Multiplies this coordinate's values by the given factor, and returns a reference
166     *        to this coordinate.
167     */
168     SceneCoordinate & operator*=(double factor);
169
170     /**
171     * @brief Adds the given coordinate to this coordinate and returns a reference to this
172              coordinate
173     */
174     SceneCoordinate & operator+=(const SceneCoordinate &coordinate);
175
176     /**
177     * @brief Subtracts the given coordinate from this coordinate and returns a reference to this
178              coordinate.
179     */
180     SceneCoordinate & operator-=(const SceneCoordinate &coordinate);
181
182     /**
183     * @brief Returns a SceneCoordinate object that is the sum of the coordinates.
184     */
185     const SceneCoordinate operator+(const SceneCoordinate &other) const;
186
187     /**
188     * @brief Returns a SceneCoordinate object that is formed by subtracting the coordinates.
189     */
190     const SceneCoordinate operator-(const SceneCoordinate &other) const;
191 };
192
193 /**
194 * @brief Operator for writing the coordinate to QDebug stream.
195 */
196 QDebug operator<<(QDebug dbg, const SceneCoordinate &coordinate);
197
198 /**
199 * @brief Returns a SceneCoordinate object that is multiplied by the given factor.
200 */
201 const SceneCoordinate operator*(double factor, const SceneCoordinate &coordinate);
202
203 Q_DECLARE_METATYPE(SceneCoordinate)
204
205 #endif // SCENECOORDINATE_H