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