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 | ||
bc379358 | 69 | //! Constructor from 3-components vector + optional 4th value. |
70 | explicit NCollection_Vec4(const NCollection_Vec3<Element_t>& theVec3, const Element_t theW = Element_t(0)) | |
5e27df78 | 71 | { |
72 | std::memcpy (this, &theVec3, sizeof(NCollection_Vec3<Element_t>)); | |
bc379358 | 73 | v[3] = theW; |
5e27df78 | 74 | } |
75 | ||
e958a649 | 76 | //! Assign new values to the vector. |
77 | void SetValues (const Element_t theX, | |
78 | const Element_t theY, | |
79 | const Element_t theZ, | |
80 | const Element_t theW) | |
81 | { | |
82 | v[0] = theX; | |
83 | v[1] = theY; | |
84 | v[2] = theZ; | |
85 | v[3] = theW; | |
86 | } | |
87 | ||
bc379358 | 88 | //! Assign new values as 3-component vector and a 4-th value. |
89 | void SetValues (const NCollection_Vec3<Element_t>& theVec3, const Element_t theW) | |
90 | { | |
91 | v[0] = theVec3.x(); | |
92 | v[1] = theVec3.y(); | |
93 | v[2] = theVec3.z(); | |
94 | v[3] = theW; | |
95 | } | |
96 | ||
5e27df78 | 97 | //! Alias to 1st component as X coordinate in XYZW. |
98 | Element_t x() const { return v[0]; } | |
99 | ||
100 | //! Alias to 1st component as RED channel in RGBA. | |
101 | Element_t r() const { return v[0]; } | |
102 | ||
103 | //! Alias to 2nd component as Y coordinate in XYZW. | |
104 | Element_t y() const { return v[1]; } | |
105 | ||
106 | //! Alias to 2nd component as GREEN channel in RGBA. | |
107 | Element_t g() const { return v[1]; } | |
108 | ||
109 | //! Alias to 3rd component as Z coordinate in XYZW. | |
110 | Element_t z() const { return v[2]; } | |
111 | ||
112 | //! Alias to 3rd component as BLUE channel in RGBA. | |
113 | Element_t b() const { return v[2]; } | |
114 | ||
115 | //! Alias to 4th component as W coordinate in XYZW. | |
116 | Element_t w() const { return v[3]; } | |
117 | ||
118 | //! Alias to 4th component as ALPHA channel in RGBA. | |
119 | Element_t a() const { return v[3]; } | |
120 | ||
121 | //! @return 2 of XYZW components in specified order as vector in GLSL-style | |
5640d653 DB |
122 | NCOLLECTION_VEC_COMPONENTS_2D(x, y) |
123 | NCOLLECTION_VEC_COMPONENTS_2D(x, z) | |
124 | NCOLLECTION_VEC_COMPONENTS_2D(x, w) | |
125 | NCOLLECTION_VEC_COMPONENTS_2D(y, z) | |
126 | NCOLLECTION_VEC_COMPONENTS_2D(y, w) | |
127 | NCOLLECTION_VEC_COMPONENTS_2D(z, w) | |
5e27df78 | 128 | |
129 | //! @return 3 of XYZW components in specified order as vector in GLSL-style | |
5640d653 DB |
130 | NCOLLECTION_VEC_COMPONENTS_3D(x, y, z) |
131 | NCOLLECTION_VEC_COMPONENTS_3D(x, y, w) | |
132 | NCOLLECTION_VEC_COMPONENTS_3D(x, z, w) | |
133 | NCOLLECTION_VEC_COMPONENTS_3D(y, z, w) | |
5e27df78 | 134 | |
135 | //! @return RGB components as vector | |
5640d653 | 136 | NCOLLECTION_VEC_COMPONENTS_3D(r, g, b) |
5e27df78 | 137 | |
138 | //! Alias to 1st component as X coordinate in XYZW. | |
139 | Element_t& x() { return v[0]; } | |
140 | ||
141 | //! Alias to 1st component as RED channel in RGBA. | |
142 | Element_t& r() { return v[0]; } | |
143 | ||
144 | //! Alias to 2nd component as Y coordinate in XYZW. | |
145 | Element_t& y() { return v[1]; } | |
146 | ||
147 | //! Alias to 2nd component as GREEN channel in RGBA. | |
148 | Element_t& g() { return v[1]; } // Green color | |
149 | ||
150 | //! Alias to 3rd component as Z coordinate in XYZW. | |
151 | Element_t& z() { return v[2]; } | |
152 | ||
153 | //! Alias to 3rd component as BLUE channel in RGBA. | |
154 | Element_t& b() { return v[2]; } | |
155 | ||
156 | //! Alias to 4th component as W coordinate in XYZW. | |
157 | Element_t& w() { return v[3]; } | |
158 | ||
159 | //! Alias to 4th component as ALPHA channel in RGBA. | |
160 | Element_t& a() { return v[3]; } | |
161 | ||
8613985b | 162 | //! Check this vector with another vector for equality (without tolerance!). |
163 | bool IsEqual (const NCollection_Vec4& theOther) const | |
164 | { | |
165 | return v[0] == theOther.v[0] | |
166 | && v[1] == theOther.v[1] | |
167 | && v[2] == theOther.v[2] | |
168 | && v[3] == theOther.v[3]; | |
169 | } | |
170 | ||
171 | //! Check this vector with another vector for equality (without tolerance!). | |
172 | bool operator== (const NCollection_Vec4& theOther) { return IsEqual (theOther); } | |
173 | bool operator== (const NCollection_Vec4& theOther) const { return IsEqual (theOther); } | |
174 | ||
175 | //! Check this vector with another vector for non-equality (without tolerance!). | |
176 | bool operator!= (const NCollection_Vec4& theOther) { return !IsEqual (theOther); } | |
177 | bool operator!= (const NCollection_Vec4& theOther) const { return !IsEqual (theOther); } | |
178 | ||
5e27df78 | 179 | //! Raw access to the data (for OpenGL exchange). |
938d4544 | 180 | const Element_t* GetData() const { return v; } |
181 | Element_t* ChangeData() { return v; } | |
182 | operator const Element_t*() const { return v; } | |
183 | operator Element_t*() { return v; } | |
5e27df78 | 184 | |
185 | //! Compute per-component summary. | |
186 | NCollection_Vec4& operator+= (const NCollection_Vec4& theAdd) | |
187 | { | |
188 | v[0] += theAdd.v[0]; | |
189 | v[1] += theAdd.v[1]; | |
190 | v[2] += theAdd.v[2]; | |
191 | v[3] += theAdd.v[3]; | |
192 | return *this; | |
193 | } | |
194 | ||
195 | //! Compute per-component summary. | |
196 | friend NCollection_Vec4 operator+ (const NCollection_Vec4& theLeft, | |
197 | const NCollection_Vec4& theRight) | |
198 | { | |
199 | NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft); | |
200 | return aSumm += theRight; | |
201 | } | |
202 | ||
12381341 | 203 | //! Unary -. |
204 | NCollection_Vec4 operator-() const | |
205 | { | |
206 | return NCollection_Vec4 (-x(), -y(), -z(), -w()); | |
207 | } | |
208 | ||
5e27df78 | 209 | //! Compute per-component subtraction. |
210 | NCollection_Vec4& operator-= (const NCollection_Vec4& theDec) | |
211 | { | |
212 | v[0] -= theDec.v[0]; | |
213 | v[1] -= theDec.v[1]; | |
214 | v[2] -= theDec.v[2]; | |
215 | v[3] -= theDec.v[3]; | |
216 | return *this; | |
217 | } | |
218 | ||
219 | //! Compute per-component subtraction. | |
220 | friend NCollection_Vec4 operator- (const NCollection_Vec4& theLeft, | |
221 | const NCollection_Vec4& theRight) | |
222 | { | |
223 | NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft); | |
224 | return aSumm -= theRight; | |
225 | } | |
226 | ||
227 | //! Compute per-component multiplication. | |
228 | NCollection_Vec4& operator*= (const NCollection_Vec4& theRight) | |
229 | { | |
230 | v[0] *= theRight.v[0]; | |
231 | v[1] *= theRight.v[1]; | |
232 | v[2] *= theRight.v[2]; | |
233 | v[3] *= theRight.v[3]; | |
234 | return *this; | |
235 | } | |
236 | ||
237 | //! Compute per-component multiplication. | |
238 | friend NCollection_Vec4 operator* (const NCollection_Vec4& theLeft, | |
239 | const NCollection_Vec4& theRight) | |
240 | { | |
241 | NCollection_Vec4 aResult = NCollection_Vec4 (theLeft); | |
242 | return aResult *= theRight; | |
243 | } | |
244 | ||
245 | //! Compute per-component multiplication. | |
246 | void Multiply (const Element_t theFactor) | |
247 | { | |
248 | v[0] *= theFactor; | |
249 | v[1] *= theFactor; | |
250 | v[2] *= theFactor; | |
251 | v[3] *= theFactor; | |
252 | } | |
253 | ||
254 | //! Compute per-component multiplication. | |
255 | NCollection_Vec4& operator*=(const Element_t theFactor) | |
256 | { | |
257 | Multiply (theFactor); | |
258 | return *this; | |
259 | } | |
260 | ||
261 | //! Compute per-component multiplication. | |
262 | NCollection_Vec4 operator* (const Element_t theFactor) const | |
263 | { | |
264 | return Multiplied (theFactor); | |
265 | } | |
266 | ||
267 | //! Compute per-component multiplication. | |
268 | NCollection_Vec4 Multiplied (const Element_t theFactor) const | |
269 | { | |
270 | NCollection_Vec4 aCopyVec4 (*this); | |
271 | aCopyVec4 *= theFactor; | |
272 | return aCopyVec4; | |
273 | } | |
274 | ||
3c4e78f2 | 275 | //! Compute component-wise minimum of two vectors. |
276 | NCollection_Vec4 cwiseMin (const NCollection_Vec4& theVec) const | |
277 | { | |
200ed755 | 278 | return NCollection_Vec4 (v[0] < theVec.v[0] ? v[0] : theVec.v[0], |
279 | v[1] < theVec.v[1] ? v[1] : theVec.v[1], | |
280 | v[2] < theVec.v[2] ? v[2] : theVec.v[2], | |
281 | v[3] < theVec.v[3] ? v[3] : theVec.v[3]); | |
3c4e78f2 | 282 | } |
283 | ||
284 | //! Compute component-wise maximum of two vectors. | |
285 | NCollection_Vec4 cwiseMax (const NCollection_Vec4& theVec) const | |
286 | { | |
200ed755 | 287 | return NCollection_Vec4 (v[0] > theVec.v[0] ? v[0] : theVec.v[0], |
288 | v[1] > theVec.v[1] ? v[1] : theVec.v[1], | |
289 | v[2] > theVec.v[2] ? v[2] : theVec.v[2], | |
290 | v[3] > theVec.v[3] ? v[3] : theVec.v[3]); | |
3c4e78f2 | 291 | } |
292 | ||
91c60b57 | 293 | //! Compute component-wise modulus of the vector. |
294 | NCollection_Vec4 cwiseAbs() const | |
295 | { | |
296 | return NCollection_Vec4 (std::abs (v[0]), | |
297 | std::abs (v[1]), | |
298 | std::abs (v[2]), | |
299 | std::abs (v[3])); | |
300 | } | |
301 | ||
302 | //! Compute maximum component of the vector. | |
303 | Element_t maxComp() const | |
304 | { | |
305 | const Element_t aMax1 = v[0] > v[1] ? v[0] : v[1]; | |
306 | const Element_t aMax2 = v[2] > v[3] ? v[2] : v[3]; | |
307 | ||
308 | return aMax1 > aMax2 ? aMax1 : aMax2; | |
309 | } | |
310 | ||
311 | //! Compute minimum component of the vector. | |
312 | Element_t minComp() const | |
313 | { | |
314 | const Element_t aMin1 = v[0] < v[1] ? v[0] : v[1]; | |
315 | const Element_t aMin2 = v[2] < v[3] ? v[2] : v[3]; | |
316 | ||
317 | return aMin1 < aMin2 ? aMin1 : aMin2; | |
318 | } | |
319 | ||
1a7ece8f | 320 | //! Computes the dot product. |
321 | Element_t Dot (const NCollection_Vec4& theOther) const | |
322 | { | |
323 | return x() * theOther.x() + | |
324 | y() * theOther.y() + | |
325 | z() * theOther.z() + | |
326 | w() * theOther.w(); | |
327 | } | |
328 | ||
5e27df78 | 329 | //! Compute per-component division by scale factor. |
330 | NCollection_Vec4& operator/= (const Element_t theInvFactor) | |
331 | { | |
332 | v[0] /= theInvFactor; | |
333 | v[1] /= theInvFactor; | |
334 | v[2] /= theInvFactor; | |
335 | v[3] /= theInvFactor; | |
336 | return *this; | |
337 | } | |
338 | ||
339 | //! Compute per-component division by scale factor. | |
340 | NCollection_Vec4 operator/ (const Element_t theInvFactor) | |
341 | { | |
b7cd4ba7 | 342 | NCollection_Vec4 aResult(*this); |
5e27df78 | 343 | return aResult /= theInvFactor; |
344 | } | |
345 | ||
346 | private: | |
347 | ||
348 | Element_t v[4]; //!< define the vector as array to avoid structure alignment issues | |
349 | ||
350 | }; | |
351 | ||
352 | //! Optimized concretization for float type. | |
353 | template<> inline NCollection_Vec4<float>& NCollection_Vec4<float>::operator/= (const float theInvFactor) | |
354 | { | |
355 | Multiply (1.0f / theInvFactor); | |
356 | return *this; | |
357 | } | |
358 | ||
359 | //! Optimized concretization for double type. | |
360 | template<> inline NCollection_Vec4<double>& NCollection_Vec4<double>::operator/= (const double theInvFactor) | |
361 | { | |
362 | Multiply (1.0 / theInvFactor); | |
363 | return *this; | |
364 | } | |
365 | ||
366 | #endif // _NCollection_Vec4_H__ |