1 /*
2 * Copyright 2010-2012 OpenXcom Developers.
3 *
4 * This file is part of OpenXcom.
5 *
6 * OpenXcom is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef OPENXCOM_CORD_H
20 #define OPENXCOM_CORD_H
21
22 #include <cmath>
23 #include "../fmath.h"
24
25 namespace OpenXcom
26 {
27 struct Cord;
28
29 struct CordPolar
30 {
31 double lon, lat;
32
CordPolarCordPolar33 inline CordPolar(double plon, double plat)
34 {
35 lon = plon;
36 lat = plat;
37 }
CordPolarCordPolar38 inline CordPolar(const CordPolar& pol)
39 {
40 lon = pol.lon;
41 lat = pol.lat;
42 }
CordPolarCordPolar43 inline CordPolar()
44 {
45 lon = 0;
46 lat = 0;
47 }
48 explicit inline CordPolar(const Cord&);
49 };
50
51 struct Cord
52 {
53 double x, y, z;
54
CordCord55 inline Cord(double px, double py, double pz)
56 {
57 x = px;
58 y = py;
59 z = pz;
60 }
CordCord61 inline Cord(const Cord& c)
62 {
63 x = c.x;
64 y = c.y;
65 z = c.z;
66 }
CordCord67 inline Cord()
68 {
69 x = 0.0;
70 y = 0.0;
71 z = 0.0;
72 }
73 explicit inline Cord(const CordPolar&);
74
75 inline Cord operator +()
76 {
77 return *this;
78 }
79 inline Cord operator -()
80 {
81 return Cord(-x, -y, -z);
82 }
83 inline Cord& operator *=(double d)
84 {
85 x *= d;
86 y *= d;
87 z *= d;
88 return *this;
89 }
90 inline Cord& operator /=(double d)
91 {
92 double re = 1./d;
93 x *= re;
94 y *= re;
95 z *= re;
96 return *this;
97 }
98 inline Cord& operator +=(const Cord& c)
99 {
100 x += c.x;
101 y += c.y;
102 z += c.z;
103 return *this;
104 }
105 inline Cord& operator -=(const Cord& c)
106 {
107 x -= c.x;
108 y -= c.y;
109 z -= c.z;
110 return *this;
111 }
112 inline bool operator ==(const Cord& c)
113 {
114 return AreSame(x, c.x) && AreSame(y, c.y) && AreSame(z, c.z);
115 }
116
normCord117 inline double norm() const
118 {
119 return std::sqrt(x*x + y*y + z*z);
120 }
121 };
122
Cord(const CordPolar & pol)123 inline Cord::Cord(const CordPolar& pol)
124 {
125 x = std::sin(pol.lon) * std::cos(pol.lat);
126 y = std::sin(pol.lat);
127 z = std::cos(pol.lon) * std::cos(pol.lat);
128 }
129
CordPolar(const Cord & c)130 inline CordPolar::CordPolar(const Cord& c)
131 {
132 double inv = 1/c.norm();
133 lat = asin(c.y * inv);
134 lon = atan2(c.x, c.z);
135 }
136
137 }//namespace OpenXcom
138 #endif /* OPENXCOM_CORD_H */
139
140