0031671: Coding Rules - eliminate warnings issued by clang 11
[occt.git] / src / NCollection / NCollection_Vec3.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_Vec3_H__
16 #define _NCollection_Vec3_H__
17
18 #include <cstring>
19 #include <cmath>
20 #include <NCollection_Vec2.hxx>
21 #include <Standard_Dump.hxx>
22
23 //! Auxiliary macros to define couple of similar access components as vector methods
24 #define NCOLLECTION_VEC_COMPONENTS_3D(theX, theY, theZ) \
25   const NCollection_Vec3<Element_t> theX##theY##theZ() const { return NCollection_Vec3<Element_t>(theX(), theY(), theZ()); } \
26   const NCollection_Vec3<Element_t> theX##theZ##theY() const { return NCollection_Vec3<Element_t>(theX(), theZ(), theY()); } \
27   const NCollection_Vec3<Element_t> theY##theX##theZ() const { return NCollection_Vec3<Element_t>(theY(), theX(), theZ()); } \
28   const NCollection_Vec3<Element_t> theY##theZ##theX() const { return NCollection_Vec3<Element_t>(theY(), theZ(), theX()); } \
29   const NCollection_Vec3<Element_t> theZ##theY##theX() const { return NCollection_Vec3<Element_t>(theZ(), theY(), theX()); } \
30   const NCollection_Vec3<Element_t> theZ##theX##theY() const { return NCollection_Vec3<Element_t>(theZ(), theX(), theY()); }
31
32 //! Generic 3-components vector.
33 //! To be used as RGB color pixel or XYZ 3D-point.
34 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
35 template<typename Element_t>
36 class NCollection_Vec3
37 {
38
39 public:
40
41   //! Returns the number of components.
42   static int Length()
43   {
44     return 3;
45   }
46
47   //! Empty constructor. Construct the zero vector.
48   NCollection_Vec3()
49   {
50     std::memset (this, 0, sizeof(NCollection_Vec3));
51   }
52
53   //! Initialize ALL components of vector within specified value.
54   explicit NCollection_Vec3 (Element_t theValue)
55   {
56     v[0] = v[1] = v[2] = theValue;
57   }
58
59   //! Per-component constructor.
60   explicit NCollection_Vec3 (const Element_t theX,
61                              const Element_t theY,
62                              const Element_t theZ)
63   {
64     v[0] = theX;
65     v[1] = theY;
66     v[2] = theZ;
67   }
68
69   //! Constructor from 2-components vector + optional 3rd value.
70   explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ = Element_t(0))
71   {
72     v[0] = theVec2[0];
73     v[1] = theVec2[1];
74     v[2] = theZ;
75   }
76
77   //! Conversion constructor (explicitly converts some 3-component vector with other element type
78   //! to a new 3-component vector with the element type Element_t,
79   //! whose elements are static_cast'ed corresponding elements of theOtherVec3 vector)
80   //! @tparam OtherElement_t the element type of the other 3-component vector theOtherVec3
81   //! @param theOtherVec3 the 3-component vector that needs to be converted
82   template <typename OtherElement_t>
83   explicit NCollection_Vec3 (const NCollection_Vec3<OtherElement_t>& theOtherVec3)
84   {
85     v[0] = static_cast<Element_t> (theOtherVec3[0]);
86     v[1] = static_cast<Element_t> (theOtherVec3[1]);
87     v[2] = static_cast<Element_t> (theOtherVec3[2]);
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   {
95     v[0] = theX;
96     v[1] = theY;
97     v[2] = theZ;
98   }
99
100   //! Assign new values to the vector.
101   void SetValues (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ)
102   {
103     v[0] = theVec2.x();
104     v[1] = theVec2.y();
105     v[2] = theZ;
106   }
107
108   //! Alias to 1st component as X coordinate in XYZ.
109   Element_t x() const { return v[0]; }
110
111   //! Alias to 1st component as RED channel in RGB.
112   Element_t r() const { return v[0]; }
113
114   //! Alias to 2nd component as Y coordinate in XYZ.
115   Element_t y() const { return v[1]; }
116
117   //! Alias to 2nd component as GREEN channel in RGB.
118   Element_t g() const { return v[1]; }
119
120   //! Alias to 3rd component as Z coordinate in XYZ.
121   Element_t z() const { return v[2]; }
122
123   //! Alias to 3rd component as BLUE channel in RGB.
124   Element_t b() const { return v[2]; }
125
126   //! @return 2 components by their names in specified order (in GLSL-style)
127   NCOLLECTION_VEC_COMPONENTS_2D(x, y)
128   NCOLLECTION_VEC_COMPONENTS_2D(x, z)
129   NCOLLECTION_VEC_COMPONENTS_2D(y, z)
130
131   //! @return 3 components by their names in specified order (in GLSL-style)
132   NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
133
134   //! Alias to 1st component as X coordinate in XYZ.
135   Element_t& x() { return v[0]; }
136
137   //! Alias to 1st component as RED channel in RGB.
138   Element_t& r() { return v[0]; }
139
140   //! Alias to 2nd component as Y coordinate in XYZ.
141   Element_t& y() { return v[1]; }
142
143   //! Alias to 2nd component as GREEN channel in RGB.
144   Element_t& g() { return v[1]; }
145
146   //! Alias to 3rd component as Z coordinate in XYZ.
147   Element_t& z() { return v[2]; }
148
149   //! Alias to 3rd component as BLUE channel in RGB.
150   Element_t& b() { return v[2]; }
151
152   //! Check this vector with another vector for equality (without tolerance!).
153   bool IsEqual (const NCollection_Vec3& theOther) const
154   {
155     return v[0] == theOther.v[0]
156         && v[1] == theOther.v[1]
157         && v[2] == theOther.v[2];
158   }
159
160   //! Check this vector with another vector for equality (without tolerance!).
161   bool operator== (const NCollection_Vec3& theOther)       { return IsEqual (theOther); }
162   bool operator== (const NCollection_Vec3& theOther) const { return IsEqual (theOther); }
163
164   //! Check this vector with another vector for non-equality (without tolerance!).
165   bool operator!= (const NCollection_Vec3& theOther)       { return !IsEqual (theOther); }
166   bool operator!= (const NCollection_Vec3& theOther) const { return !IsEqual (theOther); }
167
168   //! Raw access to the data (for OpenGL exchange).
169   const Element_t* GetData()    const { return v; }
170         Element_t* ChangeData()       { return v; }
171   operator const   Element_t*() const { return v; }
172   operator         Element_t*()       { return v; }
173
174   //! Compute per-component summary.
175   NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
176   {
177     v[0] += theAdd.v[0];
178     v[1] += theAdd.v[1];
179     v[2] += theAdd.v[2];
180     return *this;
181   }
182
183   //! Compute per-component summary.
184   friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
185                                      const NCollection_Vec3& theRight)
186   {
187     NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
188     return aSumm += theRight;
189   }
190
191   //! Unary -.
192   NCollection_Vec3 operator-() const
193   {
194     return NCollection_Vec3 (-x(), -y(), -z());
195   }
196
197   //! Compute per-component subtraction.
198   NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
199   {
200     v[0] -= theDec.v[0];
201     v[1] -= theDec.v[1];
202     v[2] -= theDec.v[2];
203     return *this;
204   }
205
206   //! Compute per-component subtraction.
207   friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
208                                      const NCollection_Vec3& theRight)
209   {
210     NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
211     return aSumm -= theRight;
212   }
213
214   //! Compute per-component multiplication by scale factor.
215   void Multiply (const Element_t theFactor)
216   {
217     v[0] *= theFactor;
218     v[1] *= theFactor;
219     v[2] *= theFactor;
220   }
221
222   //! Compute per-component multiplication.
223   NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
224   {
225     v[0] *= theRight.v[0];
226     v[1] *= theRight.v[1];
227     v[2] *= theRight.v[2];
228     return *this;
229   }
230
231   //! Compute per-component multiplication.
232   friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
233                                      const NCollection_Vec3& theRight)
234   {
235     NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
236     return aResult *= theRight;
237   }
238
239   //! Compute per-component multiplication by scale factor.
240   NCollection_Vec3& operator*= (const Element_t theFactor)
241   {
242     Multiply (theFactor);
243     return *this;
244   }
245
246   //! Compute per-component multiplication by scale factor.
247   NCollection_Vec3 operator* (const Element_t theFactor) const
248   {
249     return Multiplied (theFactor);
250   }
251
252   //! Compute per-component multiplication by scale factor.
253   NCollection_Vec3 Multiplied (const Element_t theFactor) const
254   {
255     NCollection_Vec3 aCopyVec3 (*this);
256     aCopyVec3 *= theFactor;
257     return aCopyVec3;
258   }
259
260   //! Compute component-wise minimum of two vectors.
261   NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const
262   {
263     return NCollection_Vec3 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
264                              v[1] < theVec.v[1] ? v[1] : theVec.v[1],
265                              v[2] < theVec.v[2] ? v[2] : theVec.v[2]);
266   }
267
268   //! Compute component-wise maximum of two vectors.
269   NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const
270   {
271     return NCollection_Vec3 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
272                              v[1] > theVec.v[1] ? v[1] : theVec.v[1],
273                              v[2] > theVec.v[2] ? v[2] : theVec.v[2]);
274   }
275
276   //! Compute component-wise modulus of the vector.
277   NCollection_Vec3 cwiseAbs() const
278   {
279     return NCollection_Vec3 (std::abs (v[0]),
280                              std::abs (v[1]),
281                              std::abs (v[2]));
282   }
283
284   //! Compute maximum component of the vector.
285   Element_t maxComp() const
286   {
287     return v[0] > v[1] ? (v[0] > v[2] ? v[0] : v[2])
288                        : (v[1] > v[2] ? v[1] : v[2]);
289   }
290
291   //! Compute minimum component of the vector.
292   Element_t minComp() const
293   {
294     return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2])
295                        : (v[1] < v[2] ? v[1] : v[2]);
296   }
297
298   //! Compute per-component division by scale factor.
299   NCollection_Vec3& operator/= (const Element_t theInvFactor)
300   {
301     v[0] /= theInvFactor;
302     v[1] /= theInvFactor;
303     v[2] /= theInvFactor;
304     return *this;
305   }
306
307   //! Compute per-component division.
308   NCollection_Vec3& operator/= (const NCollection_Vec3& theRight)
309   {
310     v[0] /= theRight.v[0];
311     v[1] /= theRight.v[1];
312     v[2] /= theRight.v[2];
313     return *this;
314   }
315
316   //! Compute per-component division by scale factor.
317   NCollection_Vec3 operator/ (const Element_t theInvFactor) const
318   {
319     NCollection_Vec3 aResult (*this);
320     return aResult /= theInvFactor;
321   }
322
323   //! Compute per-component division.
324   friend NCollection_Vec3 operator/ (const NCollection_Vec3& theLeft,
325                                      const NCollection_Vec3& theRight)
326   {
327     NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
328     return aResult /= theRight;
329   }
330
331   //! Computes the dot product.
332   Element_t Dot (const NCollection_Vec3& theOther) const
333   {
334     return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
335   }
336
337   //! Computes the vector modulus (magnitude, length).
338   Element_t Modulus() const
339   {
340     return std::sqrt (x() * x() + y() * y() + z() * z());
341   }
342
343   //! Computes the square of vector modulus (magnitude, length).
344   //! This method may be used for performance tricks.
345   Element_t SquareModulus() const
346   {
347     return x() * x() + y() * y() + z() * z();
348   }
349
350   //! Normalize the vector.
351   void Normalize()
352   {
353     Element_t aModulus = Modulus();
354     if (aModulus != Element_t(0)) // just avoid divide by zero
355     {
356       x() = x() / aModulus;
357       y() = y() / aModulus;
358       z() = z() / aModulus;
359     }
360   }
361
362   //! Normalize the vector.
363   NCollection_Vec3 Normalized() const
364   {
365     NCollection_Vec3 aCopy (*this);
366     aCopy.Normalize();
367     return aCopy;
368   }
369
370   //! Computes the cross product.
371   static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
372                                  const NCollection_Vec3& theVec2)
373   {
374     return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
375             theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
376             theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
377   }
378
379   //! Compute linear interpolation between to vectors.
380   //! @param theT - interpolation coefficient 0..1;
381   //! @return interpolation result.
382   static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
383                                    const NCollection_Vec3& theTo,
384                                    const Element_t         theT)
385   {
386     return theFrom * (Element_t(1) - theT) + theTo * theT;
387   }
388
389   //! Constuct DX unit vector.
390   static NCollection_Vec3 DX()
391   {
392     return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
393   }
394
395   //! Constuct DY unit vector.
396   static NCollection_Vec3 DY()
397   {
398     return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
399   }
400
401   //! Constuct DZ unit vector.
402   static NCollection_Vec3 DZ()
403   {
404     return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
405   }
406
407   //! Dumps the content of me into the stream
408   void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const
409   {
410     (void)theDepth;
411     OCCT_DUMP_FIELD_VALUES_NUMERICAL (theOStream, "Vec3", 3, v[0], v[1], v[2])
412   }
413
414 private:
415
416   Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
417
418 };
419
420 //! Optimized concretization for float type.
421 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
422 {
423   Multiply (1.0f / theInvFactor);
424   return *this;
425 }
426
427 //! Optimized concretization for double type.
428 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
429 {
430   Multiply (1.0 / theInvFactor);
431   return *this;
432 }
433
434 #endif // _NCollection_Vec3_H__