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