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