]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
Remove fastmath funktions like discussed on the list.
[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
47   /// Access by index, the index is unchecked
48   const T& operator()(unsigned i) const
49   { return _data[i]; }
50   /// Access by index, the index is unchecked
51   T& operator()(unsigned i)
52   { return _data[i]; }
53
54   /// Access raw data by index, the index is unchecked
55   const T& operator[](unsigned i) const
56   { return _data[i]; }
57   /// Access raw data by index, the index is unchecked
58   T& operator[](unsigned i)
59   { return _data[i]; }
60
61   /// Access the x component
62   const T& x(void) const
63   { return _data[0]; }
64   /// Access the x component
65   T& x(void)
66   { return _data[0]; }
67   /// Access the y component
68   const T& y(void) const
69   { return _data[1]; }
70   /// Access the y component
71   T& y(void)
72   { return _data[1]; }
73   /// Access the z component
74   const T& z(void) const
75   { return _data[2]; }
76   /// Access the z component
77   T& z(void)
78   { return _data[2]; }
79
80   /// Get the data pointer
81   const T* data(void) const
82   { return _data; }
83   /// Get the data pointer
84   T* data(void)
85   { return _data; }
86
87   /// Readonly interface function to ssg's sgVec3/sgdVec3
88   const T (&sg(void) const)[3]
89   { return _data; }
90   /// Interface function to ssg's sgVec3/sgdVec3
91   T (&sg(void))[3]
92   { return _data; }
93
94   /// Inplace addition
95   SGVec3& operator+=(const SGVec3& v)
96   { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
97   /// Inplace subtraction
98   SGVec3& operator-=(const SGVec3& v)
99   { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
100   /// Inplace scalar multiplication
101   template<typename S>
102   SGVec3& operator*=(S s)
103   { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
104   /// Inplace scalar multiplication by 1/s
105   template<typename S>
106   SGVec3& operator/=(S s)
107   { return operator*=(1/T(s)); }
108
109   /// Return an all zero vector
110   static SGVec3 zeros(void)
111   { return SGVec3(0, 0, 0); }
112   /// Return unit vectors
113   static SGVec3 e1(void)
114   { return SGVec3(1, 0, 0); }
115   static SGVec3 e2(void)
116   { return SGVec3(0, 1, 0); }
117   static SGVec3 e3(void)
118   { return SGVec3(0, 0, 1); }
119
120   /// Constructor. Initialize by a geodetic coordinate
121   /// Note that this conversion is relatively expensive to compute
122   static SGVec3 fromGeod(const SGGeod& geod);
123   /// Constructor. Initialize by a geocentric coordinate
124   /// Note that this conversion is relatively expensive to compute
125   static SGVec3 fromGeoc(const SGGeoc& geoc);
126
127 private:
128   /// The actual data
129   T _data[3];
130 };
131
132 template<>
133 inline
134 SGVec3<double>
135 SGVec3<double>::fromGeod(const SGGeod& geod)
136 {
137   SGVec3<double> cart;
138   SGGeodesy::SGGeodToCart(geod, cart);
139   return cart;
140 }
141
142 template<>
143 inline
144 SGVec3<float>
145 SGVec3<float>::fromGeod(const SGGeod& geod)
146 {
147   SGVec3<double> cart;
148   SGGeodesy::SGGeodToCart(geod, cart);
149   return SGVec3<float>(cart(0), cart(1), cart(2));
150 }
151
152 template<>
153 inline
154 SGVec3<double>
155 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
156 {
157   SGVec3<double> cart;
158   SGGeodesy::SGGeocToCart(geoc, cart);
159   return cart;
160 }
161
162 template<>
163 inline
164 SGVec3<float>
165 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
166 {
167   SGVec3<double> cart;
168   SGGeodesy::SGGeocToCart(geoc, cart);
169   return SGVec3<float>(cart(0), cart(1), cart(2));
170 }
171
172 /// Unary +, do nothing ...
173 template<typename T>
174 inline
175 const SGVec3<T>&
176 operator+(const SGVec3<T>& v)
177 { return v; }
178
179 /// Unary -, do nearly nothing
180 template<typename T>
181 inline
182 SGVec3<T>
183 operator-(const SGVec3<T>& v)
184 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
185
186 /// Binary +
187 template<typename T>
188 inline
189 SGVec3<T>
190 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
191 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
192
193 /// Binary -
194 template<typename T>
195 inline
196 SGVec3<T>
197 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
198 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
199
200 /// Scalar multiplication
201 template<typename S, typename T>
202 inline
203 SGVec3<T>
204 operator*(S s, const SGVec3<T>& v)
205 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
206
207 /// Scalar multiplication
208 template<typename S, typename T>
209 inline
210 SGVec3<T>
211 operator*(const SGVec3<T>& v, S s)
212 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
213
214 /// Scalar dot product
215 template<typename T>
216 inline
217 T
218 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
219 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
220
221 /// The euclidean norm of the vector, that is what most people call length
222 template<typename T>
223 inline
224 T
225 norm(const SGVec3<T>& v)
226 { return sqrt(dot(v, v)); }
227
228 /// The euclidean norm of the vector, that is what most people call length
229 template<typename T>
230 inline
231 T
232 length(const SGVec3<T>& v)
233 { return sqrt(dot(v, v)); }
234
235 /// The 1-norm of the vector, this one is the fastest length function we
236 /// can implement on modern cpu's
237 template<typename T>
238 inline
239 T
240 norm1(const SGVec3<T>& v)
241 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
242
243 /// Vector cross product
244 template<typename T>
245 inline
246 SGVec3<T>
247 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
248 {
249   return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
250                    v1(2)*v2(0) - v1(0)*v2(2),
251                    v1(0)*v2(1) - v1(1)*v2(0));
252 }
253
254 /// The euclidean norm of the vector, that is what most people call length
255 template<typename T>
256 inline
257 SGVec3<T>
258 normalize(const SGVec3<T>& v)
259 { return (1/norm(v))*v; }
260
261 /// Return true if exactly the same
262 template<typename T>
263 inline
264 bool
265 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
266 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
267
268 /// Return true if not exactly the same
269 template<typename T>
270 inline
271 bool
272 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
273 { return ! (v1 == v2); }
274
275 /// Return true if equal to the relative tolerance tol
276 template<typename T>
277 inline
278 bool
279 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
280 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
281
282 /// Return true if equal to the relative tolerance tol
283 template<typename T>
284 inline
285 bool
286 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
287 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
288
289 /// Return true if about equal to roundoff of the underlying type
290 template<typename T>
291 inline
292 bool
293 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
294 {
295   T tol = 100*SGLimits<T>::epsilon();
296   return equivalent(v1, v2, tol, tol);
297 }
298
299 /// The euclidean distance of the two vectors
300 template<typename T>
301 inline
302 T
303 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
304 { return norm(v1 - v2); }
305
306 /// The squared euclidean distance of the two vectors
307 template<typename T>
308 inline
309 T
310 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
311 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
312
313 #ifndef NDEBUG
314 template<typename T>
315 inline
316 bool
317 isNaN(const SGVec3<T>& v)
318 {
319   return SGMisc<T>::isNaN(v(0)) ||
320     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
321 }
322 #endif
323
324 /// Output to an ostream
325 template<typename char_type, typename traits_type, typename T>
326 inline
327 std::basic_ostream<char_type, traits_type>&
328 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
329 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
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