]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
473fad4cb18797266d9c844bb09560d7c8cf387b
[simgear.git] / simgear / math / SGVec3.hxx
1 #ifndef SGVec3_H
2 #define SGVec3_H
3
4 /// 3D Vector Class
5 template<typename T>
6 class SGVec3 {
7 public:
8   typedef T value_type;
9
10   /// Default constructor. Does not initialize at all.
11   /// If you need them zero initialized, use SGVec3::zeros()
12   SGVec3(void)
13   {
14     /// Initialize with nans in the debug build, that will guarantee to have
15     /// a fast uninitialized default constructor in the release but shows up
16     /// uninitialized values in the debug build very fast ...
17 #ifndef NDEBUG
18     for (unsigned i = 0; i < 3; ++i)
19       _data[i] = SGLimits<T>::quiet_NaN();
20 #endif
21   }
22   /// Constructor. Initialize by the given values
23   SGVec3(T x, T y, T z)
24   { _data[0] = x; _data[1] = y; _data[2] = z; }
25   /// Constructor. Initialize by the content of a plain array,
26   /// make sure it has at least 3 elements
27   explicit SGVec3(const T* data)
28   { _data[0] = data[0]; _data[1] = data[1]; _data[2] = data[2]; }
29   /// Constructor. Initialize by a geodetic coordinate
30   /// Note that this conversion is relatively expensive to compute
31   SGVec3(const SGGeod& geod)
32   { SGGeodesy::SGGeodToCart(geod, *this); }
33   /// Constructor. Initialize by a geocentric coordinate
34   /// Note that this conversion is relatively expensive to compute
35   SGVec3(const SGGeoc& geoc)
36   { SGGeodesy::SGGeocToCart(geoc, *this); }
37
38   /// Access by index, the index is unchecked
39   const T& operator()(unsigned i) const
40   { return _data[i]; }
41   /// Access by index, the index is unchecked
42   T& operator()(unsigned i)
43   { return _data[i]; }
44
45   /// Access raw data by index, the index is unchecked
46   const T& operator[](unsigned i) const
47   { return _data[i]; }
48   /// Access raw data by index, the index is unchecked
49   T& operator[](unsigned i)
50   { return _data[i]; }
51
52   /// Access the x component
53   const T& x(void) const
54   { return _data[0]; }
55   /// Access the x component
56   T& x(void)
57   { return _data[0]; }
58   /// Access the y component
59   const T& y(void) const
60   { return _data[1]; }
61   /// Access the y component
62   T& y(void)
63   { return _data[1]; }
64   /// Access the z component
65   const T& z(void) const
66   { return _data[2]; }
67   /// Access the z component
68   T& z(void)
69   { return _data[2]; }
70
71   /// Get the data pointer
72   const T* data(void) const
73   { return _data; }
74   /// Get the data pointer
75   T* data(void)
76   { return _data; }
77
78   /// Readonly interface function to ssg's sgVec3/sgdVec3
79   const T (&sg(void) const)[3]
80   { return _data; }
81   /// Interface function to ssg's sgVec3/sgdVec3
82   T (&sg(void))[3]
83   { return _data; }
84
85   /// Inplace addition
86   SGVec3& operator+=(const SGVec3& v)
87   { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
88   /// Inplace subtraction
89   SGVec3& operator-=(const SGVec3& v)
90   { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
91   /// Inplace scalar multiplication
92   template<typename S>
93   SGVec3& operator*=(S s)
94   { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
95   /// Inplace scalar multiplication by 1/s
96   template<typename S>
97   SGVec3& operator/=(S s)
98   { return operator*=(1/T(s)); }
99
100   /// Return an all zero vector
101   static SGVec3 zeros(void)
102   { return SGVec3(0, 0, 0); }
103   /// Return unit vectors
104   static SGVec3 e1(void)
105   { return SGVec3(1, 0, 0); }
106   static SGVec3 e2(void)
107   { return SGVec3(0, 1, 0); }
108   static SGVec3 e3(void)
109   { return SGVec3(0, 0, 1); }
110
111 private:
112   /// The actual data
113   T _data[3];
114 };
115
116 /// Unary +, do nothing ...
117 template<typename T>
118 inline
119 const SGVec3<T>&
120 operator+(const SGVec3<T>& v)
121 { return v; }
122
123 /// Unary -, do nearly nothing
124 template<typename T>
125 inline
126 SGVec3<T>
127 operator-(const SGVec3<T>& v)
128 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
129
130 /// Binary +
131 template<typename T>
132 inline
133 SGVec3<T>
134 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
135 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
136
137 /// Binary -
138 template<typename T>
139 inline
140 SGVec3<T>
141 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
142 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
143
144 /// Scalar multiplication
145 template<typename S, typename T>
146 inline
147 SGVec3<T>
148 operator*(S s, const SGVec3<T>& v)
149 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
150
151 /// Scalar multiplication
152 template<typename S, typename T>
153 inline
154 SGVec3<T>
155 operator*(const SGVec3<T>& v, S s)
156 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
157
158 /// Scalar dot product
159 template<typename T>
160 inline
161 T
162 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
163 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
164
165 /// The euclidean norm of the vector, that is what most people call length
166 template<typename T>
167 inline
168 T
169 norm(const SGVec3<T>& v)
170 { return sqrt(dot(v, v)); }
171
172 /// The euclidean norm of the vector, that is what most people call length
173 template<typename T>
174 inline
175 T
176 length(const SGVec3<T>& v)
177 { return sqrt(dot(v, v)); }
178
179 /// The 1-norm of the vector, this one is the fastest length function we
180 /// can implement on modern cpu's
181 template<typename T>
182 inline
183 T
184 norm1(const SGVec3<T>& v)
185 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
186
187 /// Vector cross product
188 template<typename T>
189 inline
190 SGVec3<T>
191 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
192 {
193   return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
194                    v1(2)*v2(0) - v1(0)*v2(2),
195                    v1(0)*v2(1) - v1(1)*v2(0));
196 }
197
198 /// The euclidean norm of the vector, that is what most people call length
199 template<typename T>
200 inline
201 SGVec3<T>
202 normalize(const SGVec3<T>& v)
203 { return (1/norm(v))*v; }
204
205 /// Return true if exactly the same
206 template<typename T>
207 inline
208 bool
209 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
210 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
211
212 /// Return true if not exactly the same
213 template<typename T>
214 inline
215 bool
216 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
217 { return ! (v1 == v2); }
218
219 /// Return true if equal to the relative tolerance tol
220 template<typename T>
221 inline
222 bool
223 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
224 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
225
226 /// Return true if equal to the relative tolerance tol
227 template<typename T>
228 inline
229 bool
230 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
231 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
232
233 /// Return true if about equal to roundoff of the underlying type
234 template<typename T>
235 inline
236 bool
237 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
238 {
239   T tol = 100*SGLimits<T>::epsilon();
240   return equivalent(v1, v2, tol, tol);
241 }
242
243 #ifndef NDEBUG
244 template<typename T>
245 inline
246 bool
247 isNaN(const SGVec3<T>& v)
248 {
249   return SGMisc<T>::isNaN(v(0)) ||
250     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
251 }
252 #endif
253
254 /// Output to an ostream
255 template<typename char_type, typename traits_type, typename T>
256 inline
257 std::basic_ostream<char_type, traits_type>&
258 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
259 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
260
261 /// Two classes doing actually the same on different types
262 typedef SGVec3<float> SGVec3f;
263 typedef SGVec3<double> SGVec3d;
264
265 inline
266 SGVec3f
267 toVec3f(const SGVec3d& v)
268 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
269
270 inline
271 SGVec3d
272 toVec3d(const SGVec3f& v)
273 { return SGVec3d(v(0), v(1), v(2)); }
274
275 #endif