0030623: Draw Harness - support hex color codes within ViewerTest::ParseColor()
[occt.git] / src / NCollection / NCollection_Vec4.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
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.
32   static int Length()
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 + optional 4th value.
70   explicit NCollection_Vec4(const NCollection_Vec3<Element_t>& theVec3, const Element_t theW = Element_t(0))
71   {
72     std::memcpy (this, &theVec3, sizeof(NCollection_Vec3<Element_t>));
73     v[3] = theW;
74   }
75
76   //! Conversion constructor (explicitly converts some 4-component vector with other element type
77   //! to a new 4-component vector with the element type Element_t,
78   //! whose elements are static_cast'ed corresponding elements of theOtherVec4 vector)
79   //! @tparam OtherElement_t the element type of the other 4-component vector theOtherVec4
80   //! @param theOtherVec4 the 4-component vector that needs to be converted
81   template <typename OtherElement_t>
82   explicit NCollection_Vec4 (const NCollection_Vec4<OtherElement_t>& theOtherVec4)
83   {
84     v[0] = static_cast<Element_t> (theOtherVec4[0]);
85     v[1] = static_cast<Element_t> (theOtherVec4[1]);
86     v[2] = static_cast<Element_t> (theOtherVec4[2]);
87     v[3] = static_cast<Element_t> (theOtherVec4[3]);
88   }
89
90   //! Assign new values to the vector.
91   void SetValues (const Element_t theX,
92                   const Element_t theY,
93                   const Element_t theZ,
94                   const Element_t theW)
95   {
96     v[0] = theX;
97     v[1] = theY;
98     v[2] = theZ;
99     v[3] = theW;
100   }
101
102   //! Assign new values as 3-component vector and a 4-th value.
103   void SetValues (const NCollection_Vec3<Element_t>& theVec3, const Element_t theW)
104   {
105     v[0] = theVec3.x();
106     v[1] = theVec3.y();
107     v[2] = theVec3.z();
108     v[3] = theW;
109   }
110
111   //! Alias to 1st component as X coordinate in XYZW.
112   Element_t x() const { return v[0]; }
113
114   //! Alias to 1st component as RED channel in RGBA.
115   Element_t r() const { return v[0]; }
116
117   //! Alias to 2nd component as Y coordinate in XYZW.
118   Element_t y() const { return v[1]; }
119
120   //! Alias to 2nd component as GREEN channel in RGBA.
121   Element_t g() const { return v[1]; }
122
123   //! Alias to 3rd component as Z coordinate in XYZW.
124   Element_t z() const { return v[2]; }
125
126   //! Alias to 3rd component as BLUE channel in RGBA.
127   Element_t b() const { return v[2]; }
128
129   //! Alias to 4th component as W coordinate in XYZW.
130   Element_t w() const { return v[3]; }
131
132   //! Alias to 4th component as ALPHA channel in RGBA.
133   Element_t a() const { return v[3]; }
134
135   //! @return 2 of XYZW components in specified order as vector in GLSL-style
136   NCOLLECTION_VEC_COMPONENTS_2D(x, y)
137   NCOLLECTION_VEC_COMPONENTS_2D(x, z)
138   NCOLLECTION_VEC_COMPONENTS_2D(x, w)
139   NCOLLECTION_VEC_COMPONENTS_2D(y, z)
140   NCOLLECTION_VEC_COMPONENTS_2D(y, w)
141   NCOLLECTION_VEC_COMPONENTS_2D(z, w)
142
143   //! @return 3 of XYZW components in specified order as vector in GLSL-style
144   NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
145   NCOLLECTION_VEC_COMPONENTS_3D(x, y, w)
146   NCOLLECTION_VEC_COMPONENTS_3D(x, z, w)
147   NCOLLECTION_VEC_COMPONENTS_3D(y, z, w)
148
149   //! @return RGB components as vector
150   NCOLLECTION_VEC_COMPONENTS_3D(r, g, b)
151
152   //! Alias to 1st component as X coordinate in XYZW.
153   Element_t& x() { return v[0]; }
154
155   //! Alias to 1st component as RED channel in RGBA.
156   Element_t& r() { return v[0]; }
157
158   //! Alias to 2nd component as Y coordinate in XYZW.
159   Element_t& y() { return v[1]; }
160
161   //! Alias to 2nd component as GREEN channel in RGBA.
162   Element_t& g() { return v[1]; } // Green color
163
164   //! Alias to 3rd component as Z coordinate in XYZW.
165   Element_t& z() { return v[2]; }
166
167   //! Alias to 3rd component as BLUE channel in RGBA.
168   Element_t& b() { return v[2]; }
169
170   //! Alias to 4th component as W coordinate in XYZW.
171   Element_t& w() { return v[3]; }
172
173   //! Alias to 4th component as ALPHA channel in RGBA.
174   Element_t& a() { return v[3]; }
175
176   //! Check this vector with another vector for equality (without tolerance!).
177   bool IsEqual (const NCollection_Vec4& theOther) const
178   {
179     return v[0] == theOther.v[0]
180         && v[1] == theOther.v[1]
181         && v[2] == theOther.v[2]
182         && v[3] == theOther.v[3];
183   }
184
185   //! Check this vector with another vector for equality (without tolerance!).
186   bool operator== (const NCollection_Vec4& theOther)       { return IsEqual (theOther); }
187   bool operator== (const NCollection_Vec4& theOther) const { return IsEqual (theOther); }
188
189   //! Check this vector with another vector for non-equality (without tolerance!).
190   bool operator!= (const NCollection_Vec4& theOther)       { return !IsEqual (theOther); }
191   bool operator!= (const NCollection_Vec4& theOther) const { return !IsEqual (theOther); }
192
193   //! Raw access to the data (for OpenGL exchange).
194   const Element_t* GetData()    const { return v; }
195         Element_t* ChangeData()       { return v; }
196   operator const   Element_t*() const { return v; }
197   operator         Element_t*()       { return v; }
198
199   //! Compute per-component summary.
200   NCollection_Vec4& operator+= (const NCollection_Vec4& theAdd)
201   {
202     v[0] += theAdd.v[0];
203     v[1] += theAdd.v[1];
204     v[2] += theAdd.v[2];
205     v[3] += theAdd.v[3];
206     return *this;
207   }
208
209   //! Compute per-component summary.
210   friend NCollection_Vec4 operator+ (const NCollection_Vec4& theLeft,
211                                      const NCollection_Vec4& theRight)
212   {
213     NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft);
214     return aSumm += theRight;
215   }
216
217   //! Unary -.
218   NCollection_Vec4 operator-() const
219   {
220     return NCollection_Vec4 (-x(), -y(), -z(), -w());
221   }
222
223   //! Compute per-component subtraction.
224   NCollection_Vec4& operator-= (const NCollection_Vec4& theDec)
225   {
226     v[0] -= theDec.v[0];
227     v[1] -= theDec.v[1];
228     v[2] -= theDec.v[2];
229     v[3] -= theDec.v[3];
230     return *this;
231   }
232
233   //! Compute per-component subtraction.
234   friend NCollection_Vec4 operator- (const NCollection_Vec4& theLeft,
235                                      const NCollection_Vec4& theRight)
236   {
237     NCollection_Vec4 aSumm = NCollection_Vec4 (theLeft);
238     return aSumm -= theRight;
239   }
240
241   //! Compute per-component multiplication.
242   NCollection_Vec4& operator*= (const NCollection_Vec4& theRight)
243   {
244     v[0] *= theRight.v[0];
245     v[1] *= theRight.v[1];
246     v[2] *= theRight.v[2];
247     v[3] *= theRight.v[3];
248     return *this;
249   }
250
251   //! Compute per-component multiplication.
252   friend NCollection_Vec4 operator* (const NCollection_Vec4& theLeft,
253                                      const NCollection_Vec4& theRight)
254   {
255     NCollection_Vec4 aResult = NCollection_Vec4 (theLeft);
256     return aResult *= theRight;
257   }
258
259   //! Compute per-component multiplication.
260   void Multiply (const Element_t theFactor)
261   {
262     v[0] *= theFactor;
263     v[1] *= theFactor;
264     v[2] *= theFactor;
265     v[3] *= theFactor;
266   }
267
268   //! Compute per-component multiplication.
269   NCollection_Vec4& operator*=(const Element_t theFactor)
270   {
271     Multiply (theFactor);
272     return *this;
273   }
274
275   //! Compute per-component multiplication.
276   NCollection_Vec4 operator* (const Element_t theFactor) const
277   {
278     return Multiplied (theFactor);
279   }
280
281   //! Compute per-component multiplication.
282   NCollection_Vec4 Multiplied (const Element_t theFactor) const
283   {
284     NCollection_Vec4 aCopyVec4 (*this);
285     aCopyVec4 *= theFactor;
286     return aCopyVec4;
287   }
288
289   //! Compute component-wise minimum of two vectors.
290   NCollection_Vec4 cwiseMin (const NCollection_Vec4& theVec) const
291   {
292     return NCollection_Vec4 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
293                              v[1] < theVec.v[1] ? v[1] : theVec.v[1],
294                              v[2] < theVec.v[2] ? v[2] : theVec.v[2],
295                              v[3] < theVec.v[3] ? v[3] : theVec.v[3]);
296   }
297
298   //! Compute component-wise maximum of two vectors.
299   NCollection_Vec4 cwiseMax (const NCollection_Vec4& theVec) const
300   {
301     return NCollection_Vec4 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
302                              v[1] > theVec.v[1] ? v[1] : theVec.v[1],
303                              v[2] > theVec.v[2] ? v[2] : theVec.v[2],
304                              v[3] > theVec.v[3] ? v[3] : theVec.v[3]);
305   }
306
307   //! Compute component-wise modulus of the vector.
308   NCollection_Vec4 cwiseAbs() const
309   {
310     return NCollection_Vec4 (std::abs (v[0]),
311                              std::abs (v[1]),
312                              std::abs (v[2]),
313                              std::abs (v[3]));
314   }
315
316   //! Compute maximum component of the vector.
317   Element_t maxComp() const
318   {
319     const Element_t aMax1 = v[0] > v[1] ? v[0] : v[1];
320     const Element_t aMax2 = v[2] > v[3] ? v[2] : v[3];
321
322     return aMax1 > aMax2 ? aMax1 : aMax2;
323   }
324
325   //! Compute minimum component of the vector.
326   Element_t minComp() const
327   {
328     const Element_t aMin1 = v[0] < v[1] ? v[0] : v[1];
329     const Element_t aMin2 = v[2] < v[3] ? v[2] : v[3];
330
331     return aMin1 < aMin2 ? aMin1 : aMin2;
332   }
333
334   //! Computes the dot product.
335   Element_t Dot (const NCollection_Vec4& theOther) const
336   {
337     return x() * theOther.x() +
338            y() * theOther.y() +
339            z() * theOther.z() +
340            w() * theOther.w();
341   }
342
343   //! Compute per-component division by scale factor.
344   NCollection_Vec4& operator/= (const Element_t theInvFactor)
345   {
346     v[0] /= theInvFactor;
347     v[1] /= theInvFactor;
348     v[2] /= theInvFactor;
349     v[3] /= theInvFactor;
350     return *this;
351   }
352
353   //! Compute per-component division.
354   NCollection_Vec4& operator/= (const NCollection_Vec4& theRight)
355   {
356     v[0] /= theRight.v[0];
357     v[1] /= theRight.v[1];
358     v[2] /= theRight.v[2];
359     v[3] /= theRight.v[3];
360     return *this;
361   }
362
363   //! Compute per-component division by scale factor.
364   NCollection_Vec4 operator/ (const Element_t theInvFactor)
365   {
366     NCollection_Vec4 aResult(*this);
367     return aResult /= theInvFactor;
368   }
369
370   //! Compute per-component division.
371   friend NCollection_Vec4 operator/ (const NCollection_Vec4& theLeft,
372                                      const NCollection_Vec4& theRight)
373   {
374     NCollection_Vec4 aResult = NCollection_Vec4 (theLeft);
375     return aResult /= theRight;
376   }
377
378 private:
379
380   Element_t v[4]; //!< define the vector as array to avoid structure alignment issues
381
382 };
383
384 //! Optimized concretization for float type.
385 template<> inline NCollection_Vec4<float>& NCollection_Vec4<float>::operator/= (const float theInvFactor)
386 {
387   Multiply (1.0f / theInvFactor);
388   return *this;
389 }
390
391 //! Optimized concretization for double type.
392 template<> inline NCollection_Vec4<double>& NCollection_Vec4<double>::operator/= (const double theInvFactor)
393 {
394   Multiply (1.0 / theInvFactor);
395   return *this;
396 }
397
398 #endif // _NCollection_Vec4_H__