1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _NCollection_Vec4_H__
16 #define _NCollection_Vec4_H__
18 #include <NCollection_Vec3.hxx>
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
31 //! Returns the number of components.
37 //! Empty constructor. Construct the zero vector.
40 std::memset (this, 0, sizeof(NCollection_Vec4));
43 //! Initialize ALL components of vector within specified value.
44 explicit NCollection_Vec4 (const Element_t theValue)
46 v[0] = v[1] = v[2] = v[3] = theValue;
49 //! Per-component constructor.
50 explicit NCollection_Vec4 (const Element_t theX,
61 //! Constructor from 2-components vector.
62 explicit NCollection_Vec4 (const NCollection_Vec2<Element_t>& theVec2)
66 v[2] = v[3] = Element_t (0);
69 //! Constructor from 3-components vector.
70 explicit NCollection_Vec4(const NCollection_Vec3<Element_t>& theVec3)
72 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3<Element_t>));
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>));
83 //! Alias to 1st component as X coordinate in XYZW.
84 Element_t x() const { return v[0]; }
86 //! Alias to 1st component as RED channel in RGBA.
87 Element_t r() const { return v[0]; }
89 //! Alias to 2nd component as Y coordinate in XYZW.
90 Element_t y() const { return v[1]; }
92 //! Alias to 2nd component as GREEN channel in RGBA.
93 Element_t g() const { return v[1]; }
95 //! Alias to 3rd component as Z coordinate in XYZW.
96 Element_t z() const { return v[2]; }
98 //! Alias to 3rd component as BLUE channel in RGBA.
99 Element_t b() const { return v[2]; }
101 //! Alias to 4th component as W coordinate in XYZW.
102 Element_t w() const { return v[3]; }
104 //! Alias to 4th component as ALPHA channel in RGBA.
105 Element_t a() const { return v[3]; }
107 //! @return 2 of XYZW components in specified order as vector in GLSL-style
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)
115 //! @return 3 of XYZW components in specified order as vector in GLSL-style
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)
121 //! @return RGB components as vector
122 NCOLLECTION_VEC_COMPONENTS_3D(r, g, b)
124 //! Alias to 1st component as X coordinate in XYZW.
125 Element_t& x() { return v[0]; }
127 //! Alias to 1st component as RED channel in RGBA.
128 Element_t& r() { return v[0]; }
130 //! Alias to 2nd component as Y coordinate in XYZW.
131 Element_t& y() { return v[1]; }
133 //! Alias to 2nd component as GREEN channel in RGBA.
134 Element_t& g() { return v[1]; } // Green color
136 //! Alias to 3rd component as Z coordinate in XYZW.
137 Element_t& z() { return v[2]; }
139 //! Alias to 3rd component as BLUE channel in RGBA.
140 Element_t& b() { return v[2]; }
142 //! Alias to 4th component as W coordinate in XYZW.
143 Element_t& w() { return v[3]; }
145 //! Alias to 4th component as ALPHA channel in RGBA.
146 Element_t& a() { return v[3]; }
148 //! @return XY-components modifiable vector
149 NCollection_Vec2<Element_t>& xy()
151 return *((NCollection_Vec2<Element_t>* )&v[0]);
154 //! @return YZ-components modifiable vector
155 NCollection_Vec2<Element_t>& yz()
157 return *((NCollection_Vec2<Element_t>* )&v[1]);
160 //! @return YZ-components modifiable vector
161 NCollection_Vec2<Element_t>& zw()
163 return *((NCollection_Vec2<Element_t>* )&v[2]);
166 //! @return XYZ-components modifiable vector
167 NCollection_Vec3<Element_t>& xyz()
169 return *((NCollection_Vec3<Element_t>* )&v[0]);
172 //! @return YZW-components modifiable vector
173 NCollection_Vec3<Element_t>& yzw()
175 return *((NCollection_Vec3<Element_t>* )&v[1]);
178 //! Raw access to the data (for OpenGL exchange).
179 const Element_t* GetData() const { return v; }
180 Element_t* ChangeData() { return v; }
181 operator const Element_t*() const { return v; }
182 operator Element_t*() { return v; }
184 //! Compute per-component summary.
185 NCollection_Vec4& operator+= (const NCollection_Vec4& theAdd)
194 //! Compute per-component summary.
195 friend NCollection_Vec4 operator+ (const NCollection_Vec4& theLeft,
196 const NCollection_Vec4& theRight)
198 NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft);
199 return aSumm += theRight;
203 NCollection_Vec4 operator-() const
205 return NCollection_Vec4 (-x(), -y(), -z(), -w());
208 //! Compute per-component subtraction.
209 NCollection_Vec4& operator-= (const NCollection_Vec4& theDec)
218 //! Compute per-component subtraction.
219 friend NCollection_Vec4 operator- (const NCollection_Vec4& theLeft,
220 const NCollection_Vec4& theRight)
222 NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft);
223 return aSumm -= theRight;
226 //! Compute per-component multiplication.
227 NCollection_Vec4& operator*= (const NCollection_Vec4& theRight)
229 v[0] *= theRight.v[0];
230 v[1] *= theRight.v[1];
231 v[2] *= theRight.v[2];
232 v[3] *= theRight.v[3];
236 //! Compute per-component multiplication.
237 friend NCollection_Vec4 operator* (const NCollection_Vec4& theLeft,
238 const NCollection_Vec4& theRight)
240 NCollection_Vec4 aResult = NCollection_Vec4 (theLeft);
241 return aResult *= theRight;
244 //! Compute per-component multiplication.
245 void Multiply (const Element_t theFactor)
253 //! Compute per-component multiplication.
254 NCollection_Vec4& operator*=(const Element_t theFactor)
256 Multiply (theFactor);
260 //! Compute per-component multiplication.
261 NCollection_Vec4 operator* (const Element_t theFactor) const
263 return Multiplied (theFactor);
266 //! Compute per-component multiplication.
267 NCollection_Vec4 Multiplied (const Element_t theFactor) const
269 NCollection_Vec4 aCopyVec4 (*this);
270 aCopyVec4 *= theFactor;
274 //! Compute component-wise minimum of two vectors.
275 NCollection_Vec4 cwiseMin (const NCollection_Vec4& theVec) const
277 return NCollection_Vec4 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
278 v[1] < theVec.v[1] ? v[1] : theVec.v[1],
279 v[2] < theVec.v[2] ? v[2] : theVec.v[2],
280 v[3] < theVec.v[3] ? v[3] : theVec.v[3]);
283 //! Compute component-wise maximum of two vectors.
284 NCollection_Vec4 cwiseMax (const NCollection_Vec4& theVec) const
286 return NCollection_Vec4 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
287 v[1] > theVec.v[1] ? v[1] : theVec.v[1],
288 v[2] > theVec.v[2] ? v[2] : theVec.v[2],
289 v[3] > theVec.v[3] ? v[3] : theVec.v[3]);
292 //! Compute component-wise modulus of the vector.
293 NCollection_Vec4 cwiseAbs() const
295 return NCollection_Vec4 (std::abs (v[0]),
301 //! Compute maximum component of the vector.
302 Element_t maxComp() const
304 const Element_t aMax1 = v[0] > v[1] ? v[0] : v[1];
305 const Element_t aMax2 = v[2] > v[3] ? v[2] : v[3];
307 return aMax1 > aMax2 ? aMax1 : aMax2;
310 //! Compute minimum component of the vector.
311 Element_t minComp() const
313 const Element_t aMin1 = v[0] < v[1] ? v[0] : v[1];
314 const Element_t aMin2 = v[2] < v[3] ? v[2] : v[3];
316 return aMin1 < aMin2 ? aMin1 : aMin2;
319 //! Compute per-component division by scale factor.
320 NCollection_Vec4& operator/= (const Element_t theInvFactor)
322 v[0] /= theInvFactor;
323 v[1] /= theInvFactor;
324 v[2] /= theInvFactor;
325 v[3] /= theInvFactor;
329 //! Compute per-component division by scale factor.
330 NCollection_Vec4 operator/ (const Element_t theInvFactor)
332 NCollection_Vec4 aResult(*this);
333 return aResult /= theInvFactor;
338 Element_t v[4]; //!< define the vector as array to avoid structure alignment issues
342 //! Optimized concretization for float type.
343 template<> inline NCollection_Vec4<float>& NCollection_Vec4<float>::operator/= (const float theInvFactor)
345 Multiply (1.0f / theInvFactor);
349 //! Optimized concretization for double type.
350 template<> inline NCollection_Vec4<double>& NCollection_Vec4<double>::operator/= (const double theInvFactor)
352 Multiply (1.0 / theInvFactor);
356 #endif // _NCollection_Vec4_H__