]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
1d2a16277a92434f9f39b833934d75366c6532af
[simgear.git] / simgear / math / SGVec3.hxx
1 // Copyright (C) 2006  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef SGVec3_H
19 #define SGVec3_H
20
21 /// 3D Vector Class
22 template<typename T>
23 class SGVec3 {
24 public:
25   typedef T value_type;
26
27   /// Default constructor. Does not initialize at all.
28   /// If you need them zero initialized, use SGVec3::zeros()
29   SGVec3(void)
30   {
31     /// Initialize with nans in the debug build, that will guarantee to have
32     /// a fast uninitialized default constructor in the release but shows up
33     /// uninitialized values in the debug build very fast ...
34 #ifndef NDEBUG
35     for (unsigned i = 0; i < 3; ++i)
36       _data[i] = SGLimits<T>::quiet_NaN();
37 #endif
38   }
39   /// Constructor. Initialize by the given values
40   SGVec3(T x, T y, T z)
41   { _data[0] = x; _data[1] = y; _data[2] = z; }
42   /// Constructor. Initialize by the content of a plain array,
43   /// make sure it has at least 3 elements
44   explicit SGVec3(const T* data)
45   { _data[0] = data[0]; _data[1] = data[1]; _data[2] = data[2]; }
46   /// Constructor. Initialize by a geodetic coordinate
47   /// Note that this conversion is relatively expensive to compute
48   /// depricated
49   SGVec3(const SGGeod& geod)
50   { SGGeodesy::SGGeodToCart(geod, *this); }
51   /// Constructor. Initialize by a geocentric coordinate
52   /// Note that this conversion is relatively expensive to compute
53   /// depricated
54   SGVec3(const SGGeoc& geoc)
55   { SGGeodesy::SGGeocToCart(geoc, *this); }
56
57   /// Access by index, the index is unchecked
58   const T& operator()(unsigned i) const
59   { return _data[i]; }
60   /// Access by index, the index is unchecked
61   T& operator()(unsigned i)
62   { return _data[i]; }
63
64   /// Access raw data by index, the index is unchecked
65   const T& operator[](unsigned i) const
66   { return _data[i]; }
67   /// Access raw data by index, the index is unchecked
68   T& operator[](unsigned i)
69   { return _data[i]; }
70
71   /// Access the x component
72   const T& x(void) const
73   { return _data[0]; }
74   /// Access the x component
75   T& x(void)
76   { return _data[0]; }
77   /// Access the y component
78   const T& y(void) const
79   { return _data[1]; }
80   /// Access the y component
81   T& y(void)
82   { return _data[1]; }
83   /// Access the z component
84   const T& z(void) const
85   { return _data[2]; }
86   /// Access the z component
87   T& z(void)
88   { return _data[2]; }
89
90   /// Get the data pointer
91   const T* data(void) const
92   { return _data; }
93   /// Get the data pointer
94   T* data(void)
95   { return _data; }
96
97   /// Readonly interface function to ssg's sgVec3/sgdVec3
98   const T (&sg(void) const)[3]
99   { return _data; }
100   /// Interface function to ssg's sgVec3/sgdVec3
101   T (&sg(void))[3]
102   { return _data; }
103
104   /// Inplace addition
105   SGVec3& operator+=(const SGVec3& v)
106   { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
107   /// Inplace subtraction
108   SGVec3& operator-=(const SGVec3& v)
109   { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
110   /// Inplace scalar multiplication
111   template<typename S>
112   SGVec3& operator*=(S s)
113   { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
114   /// Inplace scalar multiplication by 1/s
115   template<typename S>
116   SGVec3& operator/=(S s)
117   { return operator*=(1/T(s)); }
118
119   /// Return an all zero vector
120   static SGVec3 zeros(void)
121   { return SGVec3(0, 0, 0); }
122   /// Return unit vectors
123   static SGVec3 e1(void)
124   { return SGVec3(1, 0, 0); }
125   static SGVec3 e2(void)
126   { return SGVec3(0, 1, 0); }
127   static SGVec3 e3(void)
128   { return SGVec3(0, 0, 1); }
129
130   /// Constructor. Initialize by a geodetic coordinate
131   /// Note that this conversion is relatively expensive to compute
132   static SGVec3 fromGeod(const SGGeod& geod);
133   /// Constructor. Initialize by a geocentric coordinate
134   /// Note that this conversion is relatively expensive to compute
135   static SGVec3 fromGeoc(const SGGeoc& geoc);
136
137 private:
138   /// The actual data
139   T _data[3];
140 };
141
142 template<>
143 inline
144 SGVec3<double>
145 SGVec3<double>::fromGeod(const SGGeod& geod)
146 {
147   SGVec3<double> cart;
148   SGGeodesy::SGGeodToCart(geod, cart);
149   return cart;
150 }
151
152 template<>
153 inline
154 SGVec3<float>
155 SGVec3<float>::fromGeod(const SGGeod& geod)
156 {
157   SGVec3<double> cart;
158   SGGeodesy::SGGeodToCart(geod, cart);
159   return SGVec3<float>(cart(0), cart(1), cart(2));
160 }
161
162 template<>
163 inline
164 SGVec3<double>
165 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
166 {
167   SGVec3<double> cart;
168   SGGeodesy::SGGeocToCart(geoc, cart);
169   return cart;
170 }
171
172 template<>
173 inline
174 SGVec3<float>
175 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
176 {
177   SGVec3<double> cart;
178   SGGeodesy::SGGeocToCart(geoc, cart);
179   return SGVec3<float>(cart(0), cart(1), cart(2));
180 }
181
182 /// Unary +, do nothing ...
183 template<typename T>
184 inline
185 const SGVec3<T>&
186 operator+(const SGVec3<T>& v)
187 { return v; }
188
189 /// Unary -, do nearly nothing
190 template<typename T>
191 inline
192 SGVec3<T>
193 operator-(const SGVec3<T>& v)
194 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
195
196 /// Binary +
197 template<typename T>
198 inline
199 SGVec3<T>
200 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
201 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
202
203 /// Binary -
204 template<typename T>
205 inline
206 SGVec3<T>
207 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
208 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
209
210 /// Scalar multiplication
211 template<typename S, typename T>
212 inline
213 SGVec3<T>
214 operator*(S s, const SGVec3<T>& v)
215 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
216
217 /// Scalar multiplication
218 template<typename S, typename T>
219 inline
220 SGVec3<T>
221 operator*(const SGVec3<T>& v, S s)
222 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
223
224 /// Scalar dot product
225 template<typename T>
226 inline
227 T
228 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
229 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
230
231 /// The euclidean norm of the vector, that is what most people call length
232 template<typename T>
233 inline
234 T
235 norm(const SGVec3<T>& v)
236 { return sqrt(dot(v, v)); }
237
238 /// The euclidean norm of the vector, that is what most people call length
239 template<typename T>
240 inline
241 T
242 length(const SGVec3<T>& v)
243 { return sqrt(dot(v, v)); }
244
245 /// The 1-norm of the vector, this one is the fastest length function we
246 /// can implement on modern cpu's
247 template<typename T>
248 inline
249 T
250 norm1(const SGVec3<T>& v)
251 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
252
253 /// Vector cross product
254 template<typename T>
255 inline
256 SGVec3<T>
257 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
258 {
259   return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
260                    v1(2)*v2(0) - v1(0)*v2(2),
261                    v1(0)*v2(1) - v1(1)*v2(0));
262 }
263
264 /// The euclidean norm of the vector, that is what most people call length
265 template<typename T>
266 inline
267 SGVec3<T>
268 normalize(const SGVec3<T>& v)
269 { return (1/norm(v))*v; }
270
271 /// Return true if exactly the same
272 template<typename T>
273 inline
274 bool
275 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
276 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
277
278 /// Return true if not exactly the same
279 template<typename T>
280 inline
281 bool
282 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
283 { return ! (v1 == v2); }
284
285 /// Return true if equal to the relative tolerance tol
286 template<typename T>
287 inline
288 bool
289 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
290 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
291
292 /// Return true if equal to the relative tolerance tol
293 template<typename T>
294 inline
295 bool
296 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
297 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
298
299 /// Return true if about equal to roundoff of the underlying type
300 template<typename T>
301 inline
302 bool
303 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
304 {
305   T tol = 100*SGLimits<T>::epsilon();
306   return equivalent(v1, v2, tol, tol);
307 }
308
309 #ifndef NDEBUG
310 template<typename T>
311 inline
312 bool
313 isNaN(const SGVec3<T>& v)
314 {
315   return SGMisc<T>::isNaN(v(0)) ||
316     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
317 }
318 #endif
319
320 /// Output to an ostream
321 template<typename char_type, typename traits_type, typename T>
322 inline
323 std::basic_ostream<char_type, traits_type>&
324 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
325 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
326
327 /// Two classes doing actually the same on different types
328 typedef SGVec3<float> SGVec3f;
329 typedef SGVec3<double> SGVec3d;
330
331 inline
332 SGVec3f
333 toVec3f(const SGVec3d& v)
334 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
335
336 inline
337 SGVec3d
338 toVec3d(const SGVec3f& v)
339 { return SGVec3d(v(0), v(1), v(2)); }
340
341 #endif