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