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