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