Commit | Line | Data |
---|---|---|
5e27df78 | 1 | // Created by: Kirill GAVRILOV |
d5f74e42 | 2 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
5e27df78 | 3 | // |
973c2be1 | 4 | // This file is part of Open CASCADE Technology software library. |
5e27df78 | 5 | // |
d5f74e42 | 6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
10 | // distribution for complete text of the license and disclaimer of any warranty. | |
5e27df78 | 11 | // |
973c2be1 | 12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. | |
5e27df78 | 14 | |
15 | #ifndef _NCollection_Vec4_H__ | |
16 | #define _NCollection_Vec4_H__ | |
17 | ||
18 | #include <NCollection_Vec3.hxx> | |
19 | ||
20 | //! Generic 4-components vector. | |
21 | //! To be used as RGBA color vector or XYZW 3D-point with special W-component | |
22 | //! for operations with projection / model view matrices. | |
23 | //! Use this class for 3D-points carefully because declared W-component may | |
24 | //! results in incorrect results if used without matrices. | |
25 | template<typename Element_t> | |
26 | class NCollection_Vec4 | |
27 | { | |
28 | ||
29 | public: | |
30 | ||
31 | //! Returns the number of components. | |
871fa103 | 32 | static int Length() |
5e27df78 | 33 | { |
34 | return 4; | |
35 | } | |
36 | ||
37 | //! Empty constructor. Construct the zero vector. | |
38 | NCollection_Vec4() | |
39 | { | |
40 | std::memset (this, 0, sizeof(NCollection_Vec4)); | |
41 | } | |
42 | ||
43 | //! Initialize ALL components of vector within specified value. | |
44 | explicit NCollection_Vec4 (const Element_t theValue) | |
45 | { | |
46 | v[0] = v[1] = v[2] = v[3] = theValue; | |
47 | } | |
48 | ||
49 | //! Per-component constructor. | |
50 | explicit NCollection_Vec4 (const Element_t theX, | |
51 | const Element_t theY, | |
52 | const Element_t theZ, | |
53 | const Element_t theW) | |
54 | { | |
55 | v[0] = theX; | |
56 | v[1] = theY; | |
57 | v[2] = theZ; | |
58 | v[3] = theW; | |
59 | } | |
60 | ||
61 | //! Constructor from 2-components vector. | |
62 | explicit NCollection_Vec4 (const NCollection_Vec2<Element_t>& theVec2) | |
63 | { | |
64 | v[0] = theVec2[0]; | |
65 | v[1] = theVec2[1]; | |
66 | v[2] = v[3] = Element_t (0); | |
67 | } | |
68 | ||
69 | //! Constructor from 3-components vector. | |
70 | explicit NCollection_Vec4(const NCollection_Vec3<Element_t>& theVec3) | |
71 | { | |
72 | std::memcpy (this, &theVec3, sizeof(NCollection_Vec3<Element_t>)); | |
73 | v[3] = Element_t (0); | |
74 | } | |
75 | ||
76 | //! Constructor from 3-components vector + alpha value. | |
77 | explicit NCollection_Vec4(const NCollection_Vec3<Element_t>& theVec3, | |
78 | const Element_t theAlpha) { | |
79 | std::memcpy (this, &theVec3, sizeof(NCollection_Vec3<Element_t>)); | |
80 | v[3] = theAlpha; | |
81 | } | |
82 | ||
e958a649 | 83 | //! Assign new values to the vector. |
84 | void SetValues (const Element_t theX, | |
85 | const Element_t theY, | |
86 | const Element_t theZ, | |
87 | const Element_t theW) | |
88 | { | |
89 | v[0] = theX; | |
90 | v[1] = theY; | |
91 | v[2] = theZ; | |
92 | v[3] = theW; | |
93 | } | |
94 | ||
5e27df78 | 95 | //! Alias to 1st component as X coordinate in XYZW. |
96 | Element_t x() const { return v[0]; } | |
97 | ||
98 | //! Alias to 1st component as RED channel in RGBA. | |
99 | Element_t r() const { return v[0]; } | |
100 | ||
101 | //! Alias to 2nd component as Y coordinate in XYZW. | |
102 | Element_t y() const { return v[1]; } | |
103 | ||
104 | //! Alias to 2nd component as GREEN channel in RGBA. | |
105 | Element_t g() const { return v[1]; } | |
106 | ||
107 | //! Alias to 3rd component as Z coordinate in XYZW. | |
108 | Element_t z() const { return v[2]; } | |
109 | ||
110 | //! Alias to 3rd component as BLUE channel in RGBA. | |
111 | Element_t b() const { return v[2]; } | |
112 | ||
113 | //! Alias to 4th component as W coordinate in XYZW. | |
114 | Element_t w() const { return v[3]; } | |
115 | ||
116 | //! Alias to 4th component as ALPHA channel in RGBA. | |
117 | Element_t a() const { return v[3]; } | |
118 | ||
119 | //! @return 2 of XYZW components in specified order as vector in GLSL-style | |
5640d653 DB |
120 | NCOLLECTION_VEC_COMPONENTS_2D(x, y) |
121 | NCOLLECTION_VEC_COMPONENTS_2D(x, z) | |
122 | NCOLLECTION_VEC_COMPONENTS_2D(x, w) | |
123 | NCOLLECTION_VEC_COMPONENTS_2D(y, z) | |
124 | NCOLLECTION_VEC_COMPONENTS_2D(y, w) | |
125 | NCOLLECTION_VEC_COMPONENTS_2D(z, w) | |
5e27df78 | 126 | |
127 | //! @return 3 of XYZW components in specified order as vector in GLSL-style | |
5640d653 DB |
128 | NCOLLECTION_VEC_COMPONENTS_3D(x, y, z) |
129 | NCOLLECTION_VEC_COMPONENTS_3D(x, y, w) | |
130 | NCOLLECTION_VEC_COMPONENTS_3D(x, z, w) | |
131 | NCOLLECTION_VEC_COMPONENTS_3D(y, z, w) | |
5e27df78 | 132 | |
133 | //! @return RGB components as vector | |
5640d653 | 134 | NCOLLECTION_VEC_COMPONENTS_3D(r, g, b) |
5e27df78 | 135 | |
136 | //! Alias to 1st component as X coordinate in XYZW. | |
137 | Element_t& x() { return v[0]; } | |
138 | ||
139 | //! Alias to 1st component as RED channel in RGBA. | |
140 | Element_t& r() { return v[0]; } | |
141 | ||
142 | //! Alias to 2nd component as Y coordinate in XYZW. | |
143 | Element_t& y() { return v[1]; } | |
144 | ||
145 | //! Alias to 2nd component as GREEN channel in RGBA. | |
146 | Element_t& g() { return v[1]; } // Green color | |
147 | ||
148 | //! Alias to 3rd component as Z coordinate in XYZW. | |
149 | Element_t& z() { return v[2]; } | |
150 | ||
151 | //! Alias to 3rd component as BLUE channel in RGBA. | |
152 | Element_t& b() { return v[2]; } | |
153 | ||
154 | //! Alias to 4th component as W coordinate in XYZW. | |
155 | Element_t& w() { return v[3]; } | |
156 | ||
157 | //! Alias to 4th component as ALPHA channel in RGBA. | |
158 | Element_t& a() { return v[3]; } | |
159 | ||
160 | //! @return XY-components modifiable vector | |
161 | NCollection_Vec2<Element_t>& xy() | |
162 | { | |
163 | return *((NCollection_Vec2<Element_t>* )&v[0]); | |
164 | } | |
165 | ||
166 | //! @return YZ-components modifiable vector | |
167 | NCollection_Vec2<Element_t>& yz() | |
168 | { | |
169 | return *((NCollection_Vec2<Element_t>* )&v[1]); | |
170 | } | |
171 | ||
172 | //! @return YZ-components modifiable vector | |
173 | NCollection_Vec2<Element_t>& zw() | |
174 | { | |
175 | return *((NCollection_Vec2<Element_t>* )&v[2]); | |
176 | } | |
177 | ||
178 | //! @return XYZ-components modifiable vector | |
179 | NCollection_Vec3<Element_t>& xyz() | |
180 | { | |
181 | return *((NCollection_Vec3<Element_t>* )&v[0]); | |
182 | } | |
183 | ||
184 | //! @return YZW-components modifiable vector | |
185 | NCollection_Vec3<Element_t>& yzw() | |
186 | { | |
187 | return *((NCollection_Vec3<Element_t>* )&v[1]); | |
188 | } | |
189 | ||
8613985b | 190 | //! Check this vector with another vector for equality (without tolerance!). |
191 | bool IsEqual (const NCollection_Vec4& theOther) const | |
192 | { | |
193 | return v[0] == theOther.v[0] | |
194 | && v[1] == theOther.v[1] | |
195 | && v[2] == theOther.v[2] | |
196 | && v[3] == theOther.v[3]; | |
197 | } | |
198 | ||
199 | //! Check this vector with another vector for equality (without tolerance!). | |
200 | bool operator== (const NCollection_Vec4& theOther) { return IsEqual (theOther); } | |
201 | bool operator== (const NCollection_Vec4& theOther) const { return IsEqual (theOther); } | |
202 | ||
203 | //! Check this vector with another vector for non-equality (without tolerance!). | |
204 | bool operator!= (const NCollection_Vec4& theOther) { return !IsEqual (theOther); } | |
205 | bool operator!= (const NCollection_Vec4& theOther) const { return !IsEqual (theOther); } | |
206 | ||
5e27df78 | 207 | //! Raw access to the data (for OpenGL exchange). |
938d4544 | 208 | const Element_t* GetData() const { return v; } |
209 | Element_t* ChangeData() { return v; } | |
210 | operator const Element_t*() const { return v; } | |
211 | operator Element_t*() { return v; } | |
5e27df78 | 212 | |
213 | //! Compute per-component summary. | |
214 | NCollection_Vec4& operator+= (const NCollection_Vec4& theAdd) | |
215 | { | |
216 | v[0] += theAdd.v[0]; | |
217 | v[1] += theAdd.v[1]; | |
218 | v[2] += theAdd.v[2]; | |
219 | v[3] += theAdd.v[3]; | |
220 | return *this; | |
221 | } | |
222 | ||
223 | //! Compute per-component summary. | |
224 | friend NCollection_Vec4 operator+ (const NCollection_Vec4& theLeft, | |
225 | const NCollection_Vec4& theRight) | |
226 | { | |
227 | NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft); | |
228 | return aSumm += theRight; | |
229 | } | |
230 | ||
12381341 | 231 | //! Unary -. |
232 | NCollection_Vec4 operator-() const | |
233 | { | |
234 | return NCollection_Vec4 (-x(), -y(), -z(), -w()); | |
235 | } | |
236 | ||
5e27df78 | 237 | //! Compute per-component subtraction. |
238 | NCollection_Vec4& operator-= (const NCollection_Vec4& theDec) | |
239 | { | |
240 | v[0] -= theDec.v[0]; | |
241 | v[1] -= theDec.v[1]; | |
242 | v[2] -= theDec.v[2]; | |
243 | v[3] -= theDec.v[3]; | |
244 | return *this; | |
245 | } | |
246 | ||
247 | //! Compute per-component subtraction. | |
248 | friend NCollection_Vec4 operator- (const NCollection_Vec4& theLeft, | |
249 | const NCollection_Vec4& theRight) | |
250 | { | |
251 | NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft); | |
252 | return aSumm -= theRight; | |
253 | } | |
254 | ||
255 | //! Compute per-component multiplication. | |
256 | NCollection_Vec4& operator*= (const NCollection_Vec4& theRight) | |
257 | { | |
258 | v[0] *= theRight.v[0]; | |
259 | v[1] *= theRight.v[1]; | |
260 | v[2] *= theRight.v[2]; | |
261 | v[3] *= theRight.v[3]; | |
262 | return *this; | |
263 | } | |
264 | ||
265 | //! Compute per-component multiplication. | |
266 | friend NCollection_Vec4 operator* (const NCollection_Vec4& theLeft, | |
267 | const NCollection_Vec4& theRight) | |
268 | { | |
269 | NCollection_Vec4 aResult = NCollection_Vec4 (theLeft); | |
270 | return aResult *= theRight; | |
271 | } | |
272 | ||
273 | //! Compute per-component multiplication. | |
274 | void Multiply (const Element_t theFactor) | |
275 | { | |
276 | v[0] *= theFactor; | |
277 | v[1] *= theFactor; | |
278 | v[2] *= theFactor; | |
279 | v[3] *= theFactor; | |
280 | } | |
281 | ||
282 | //! Compute per-component multiplication. | |
283 | NCollection_Vec4& operator*=(const Element_t theFactor) | |
284 | { | |
285 | Multiply (theFactor); | |
286 | return *this; | |
287 | } | |
288 | ||
289 | //! Compute per-component multiplication. | |
290 | NCollection_Vec4 operator* (const Element_t theFactor) const | |
291 | { | |
292 | return Multiplied (theFactor); | |
293 | } | |
294 | ||
295 | //! Compute per-component multiplication. | |
296 | NCollection_Vec4 Multiplied (const Element_t theFactor) const | |
297 | { | |
298 | NCollection_Vec4 aCopyVec4 (*this); | |
299 | aCopyVec4 *= theFactor; | |
300 | return aCopyVec4; | |
301 | } | |
302 | ||
3c4e78f2 | 303 | //! Compute component-wise minimum of two vectors. |
304 | NCollection_Vec4 cwiseMin (const NCollection_Vec4& theVec) const | |
305 | { | |
200ed755 | 306 | return NCollection_Vec4 (v[0] < theVec.v[0] ? v[0] : theVec.v[0], |
307 | v[1] < theVec.v[1] ? v[1] : theVec.v[1], | |
308 | v[2] < theVec.v[2] ? v[2] : theVec.v[2], | |
309 | v[3] < theVec.v[3] ? v[3] : theVec.v[3]); | |
3c4e78f2 | 310 | } |
311 | ||
312 | //! Compute component-wise maximum of two vectors. | |
313 | NCollection_Vec4 cwiseMax (const NCollection_Vec4& theVec) const | |
314 | { | |
200ed755 | 315 | return NCollection_Vec4 (v[0] > theVec.v[0] ? v[0] : theVec.v[0], |
316 | v[1] > theVec.v[1] ? v[1] : theVec.v[1], | |
317 | v[2] > theVec.v[2] ? v[2] : theVec.v[2], | |
318 | v[3] > theVec.v[3] ? v[3] : theVec.v[3]); | |
3c4e78f2 | 319 | } |
320 | ||
91c60b57 | 321 | //! Compute component-wise modulus of the vector. |
322 | NCollection_Vec4 cwiseAbs() const | |
323 | { | |
324 | return NCollection_Vec4 (std::abs (v[0]), | |
325 | std::abs (v[1]), | |
326 | std::abs (v[2]), | |
327 | std::abs (v[3])); | |
328 | } | |
329 | ||
330 | //! Compute maximum component of the vector. | |
331 | Element_t maxComp() const | |
332 | { | |
333 | const Element_t aMax1 = v[0] > v[1] ? v[0] : v[1]; | |
334 | const Element_t aMax2 = v[2] > v[3] ? v[2] : v[3]; | |
335 | ||
336 | return aMax1 > aMax2 ? aMax1 : aMax2; | |
337 | } | |
338 | ||
339 | //! Compute minimum component of the vector. | |
340 | Element_t minComp() const | |
341 | { | |
342 | const Element_t aMin1 = v[0] < v[1] ? v[0] : v[1]; | |
343 | const Element_t aMin2 = v[2] < v[3] ? v[2] : v[3]; | |
344 | ||
345 | return aMin1 < aMin2 ? aMin1 : aMin2; | |
346 | } | |
347 | ||
1a7ece8f | 348 | //! Computes the dot product. |
349 | Element_t Dot (const NCollection_Vec4& theOther) const | |
350 | { | |
351 | return x() * theOther.x() + | |
352 | y() * theOther.y() + | |
353 | z() * theOther.z() + | |
354 | w() * theOther.w(); | |
355 | } | |
356 | ||
5e27df78 | 357 | //! Compute per-component division by scale factor. |
358 | NCollection_Vec4& operator/= (const Element_t theInvFactor) | |
359 | { | |
360 | v[0] /= theInvFactor; | |
361 | v[1] /= theInvFactor; | |
362 | v[2] /= theInvFactor; | |
363 | v[3] /= theInvFactor; | |
364 | return *this; | |
365 | } | |
366 | ||
367 | //! Compute per-component division by scale factor. | |
368 | NCollection_Vec4 operator/ (const Element_t theInvFactor) | |
369 | { | |
b7cd4ba7 | 370 | NCollection_Vec4 aResult(*this); |
5e27df78 | 371 | return aResult /= theInvFactor; |
372 | } | |
373 | ||
374 | private: | |
375 | ||
376 | Element_t v[4]; //!< define the vector as array to avoid structure alignment issues | |
377 | ||
378 | }; | |
379 | ||
380 | //! Optimized concretization for float type. | |
381 | template<> inline NCollection_Vec4<float>& NCollection_Vec4<float>::operator/= (const float theInvFactor) | |
382 | { | |
383 | Multiply (1.0f / theInvFactor); | |
384 | return *this; | |
385 | } | |
386 | ||
387 | //! Optimized concretization for double type. | |
388 | template<> inline NCollection_Vec4<double>& NCollection_Vec4<double>::operator/= (const double theInvFactor) | |
389 | { | |
390 | Multiply (1.0 / theInvFactor); | |
391 | return *this; | |
392 | } | |
393 | ||
394 | #endif // _NCollection_Vec4_H__ |