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