73b97433689712512b8da9dc8b043ff882498b92
[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
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.
41   static int Length()
42   {
43     return 3;
44   }
45
46   //! Empty constructor. Construct the zero vector.
47   NCollection_Vec3()
48   {
49     std::memset (this, 0, sizeof(NCollection_Vec3));
50   }
51
52   //! Initialize ALL components of vector within specified value.
53   explicit NCollection_Vec3 (Element_t theValue)
54   {
55     v[0] = v[1] = v[2] = theValue;
56   }
57
58   //! Per-component constructor.
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.
69   explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2)
70   {
71     v[0] = theVec2[0];
72     v[1] = theVec2[1];
73     v[2] = Element_t(0);
74   }
75
76   //! Alias to 1st component as X coordinate in XYZ.
77   Element_t x() const { return v[0]; }
78
79   //! Alias to 1st component as RED channel in RGB.
80   Element_t r() const { return v[0]; }
81
82   //! Alias to 2nd component as Y coordinate in XYZ.
83   Element_t y() const { return v[1]; }
84
85   //! Alias to 2nd component as GREEN channel in RGB.
86   Element_t g() const { return v[1]; }
87
88   //! Alias to 3rd component as Z coordinate in XYZ.
89   Element_t z() const { return v[2]; }
90
91   //! Alias to 3rd component as BLUE channel in RGB.
92   Element_t b() const { return v[2]; }
93
94   //! @return 2 components by their names in specified order (in GLSL-style)
95   NCOLLECTION_VEC_COMPONENTS_2D(x, y)
96   NCOLLECTION_VEC_COMPONENTS_2D(x, z)
97   NCOLLECTION_VEC_COMPONENTS_2D(y, z)
98
99   //! @return 3 components by their names in specified order (in GLSL-style)
100   NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
101
102   //! Alias to 1st component as X coordinate in XYZ.
103   Element_t& x() { return v[0]; }
104
105   //! Alias to 1st component as RED channel in RGB.
106   Element_t& r() { return v[0]; }
107
108   //! Alias to 2nd component as Y coordinate in XYZ.
109   Element_t& y() { return v[1]; }
110
111   //! Alias to 2nd component as GREEN channel in RGB.
112   Element_t& g() { return v[1]; }
113
114   //! Alias to 3rd component as Z coordinate in XYZ.
115   Element_t& z() { return v[2]; }
116
117   //! Alias to 3rd component as BLUE channel in RGB.
118   Element_t& b() { return v[2]; }
119
120   //! @return XY-components modifiable vector
121   NCollection_Vec2<Element_t>& xy()
122   {
123     return *((NCollection_Vec2<Element_t>* )&v[0]);
124   }
125
126   //! @return YZ-components modifiable vector
127   NCollection_Vec2<Element_t>& yz()
128   {
129     return *((NCollection_Vec2<Element_t>* )&v[1]);
130   }
131
132   //! Raw access to the data (for OpenGL exchange).
133   const Element_t* GetData()    const { return v; }
134         Element_t* ChangeData()       { return v; }
135   operator const   Element_t*() const { return v; }
136   operator         Element_t*()       { return v; }
137
138   //! Compute per-component summary.
139   NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
140   {
141     v[0] += theAdd.v[0];
142     v[1] += theAdd.v[1];
143     v[2] += theAdd.v[2];
144     return *this;
145   }
146
147   //! Compute per-component summary.
148   friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
149                                      const NCollection_Vec3& theRight)
150   {
151     NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
152     return aSumm += theRight;
153   }
154
155   //! Unary -.
156   NCollection_Vec3 operator-() const
157   {
158     return NCollection_Vec3 (-x(), -y(), -z());
159   }
160
161   //! Compute per-component subtraction.
162   NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
163   {
164     v[0] -= theDec.v[0];
165     v[1] -= theDec.v[1];
166     v[2] -= theDec.v[2];
167     return *this;
168   }
169
170   //! Compute per-component subtraction.
171   friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
172                                      const NCollection_Vec3& theRight)
173   {
174     NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
175     return aSumm -= theRight;
176   }
177
178   //! Compute per-component multiplication by scale factor.
179   void Multiply (const Element_t theFactor)
180   {
181     v[0] *= theFactor;
182     v[1] *= theFactor;
183     v[2] *= theFactor;
184   }
185
186   //! Compute per-component multiplication.
187   NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
188   {
189     v[0] *= theRight.v[0];
190     v[1] *= theRight.v[1];
191     v[2] *= theRight.v[2];
192     return *this;
193   }
194
195   //! Compute per-component multiplication.
196   friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
197                                      const NCollection_Vec3& theRight)
198   {
199     NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
200     return aResult *= theRight;
201   }
202
203   //! Compute per-component multiplication by scale factor.
204   NCollection_Vec3& operator*= (const Element_t theFactor)
205   {
206     Multiply (theFactor);
207     return *this;
208   }
209
210   //! Compute per-component multiplication by scale factor.
211   NCollection_Vec3 operator* (const Element_t theFactor) const
212   {
213     return Multiplied (theFactor);
214   }
215
216   //! Compute per-component multiplication by scale factor.
217   NCollection_Vec3 Multiplied (const Element_t theFactor) const
218   {
219     NCollection_Vec3 aCopyVec3 (*this);
220     aCopyVec3 *= theFactor;
221     return aCopyVec3;
222   }
223
224   //! Compute component-wise minimum of two vectors.
225   NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const
226   {
227     return NCollection_Vec3 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
228                              v[1] < theVec.v[1] ? v[1] : theVec.v[1],
229                              v[2] < theVec.v[2] ? v[2] : theVec.v[2]);
230   }
231
232   //! Compute component-wise maximum of two vectors.
233   NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const
234   {
235     return NCollection_Vec3 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
236                              v[1] > theVec.v[1] ? v[1] : theVec.v[1],
237                              v[2] > theVec.v[2] ? v[2] : theVec.v[2]);
238   }
239
240   //! Compute component-wise modulus of the vector.
241   NCollection_Vec3 cwiseAbs() const
242   {
243     return NCollection_Vec3 (std::abs (v[0]),
244                              std::abs (v[1]),
245                              std::abs (v[2]));
246   }
247
248   //! Compute maximum component of the vector.
249   Element_t maxComp() const
250   {
251     return v[0] > v[1] ? (v[0] > v[2] ? v[0] : v[2])
252                        : (v[1] > v[2] ? v[1] : v[2]);
253   }
254
255   //! Compute minimum component of the vector.
256   Element_t minComp() const
257   {
258     return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2])
259                        : (v[1] < v[2] ? v[1] : v[2]);
260   }
261
262   //! Compute per-component division by scale factor.
263   NCollection_Vec3& operator/= (const Element_t theInvFactor)
264   {
265     v[0] /= theInvFactor;
266     v[1] /= theInvFactor;
267     v[2] /= theInvFactor;
268     return *this;
269   }
270
271   //! Compute per-component division by scale factor.
272   NCollection_Vec3 operator/ (const Element_t theInvFactor)
273   {
274     NCollection_Vec3 aResult (*this);
275     return aResult /= theInvFactor;
276   }
277
278   //! Computes the dot product.
279   Element_t Dot (const NCollection_Vec3& theOther) const
280   {
281     return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
282   }
283
284   //! Computes the vector modulus (magnitude, length).
285   Element_t Modulus() const
286   {
287     return std::sqrt (x() * x() + y() * y() + z() * z());
288   }
289
290   //! Computes the square of vector modulus (magnitude, length).
291   //! This method may be used for performance tricks.
292   Element_t SquareModulus() const
293   {
294     return x() * x() + y() * y() + z() * z();
295   }
296
297   //! Normalize the vector.
298   void Normalize()
299   {
300     Element_t aModulus = Modulus();
301     if (aModulus != Element_t(0)) // just avoid divide by zero
302     {
303       x() = x() / aModulus;
304       y() = y() / aModulus;
305       z() = z() / aModulus;
306     }
307   }
308
309   //! Normalize the vector.
310   NCollection_Vec3 Normalized() const
311   {
312     NCollection_Vec3 aCopy (*this);
313     aCopy.Normalize();
314     return aCopy;
315   }
316
317   //! Computes the cross product.
318   static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
319                                  const NCollection_Vec3& theVec2)
320   {
321     return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
322             theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
323             theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
324   }
325
326   //! Compute linear interpolation between to vectors.
327   //! @param theT - interpolation coefficient 0..1;
328   //! @return interpolation result.
329   static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
330                                    const NCollection_Vec3& theTo,
331                                    const Element_t         theT)
332   {
333     return theFrom * (Element_t(1) - theT) + theTo * theT;
334   }
335
336   //! Constuct DX unit vector.
337   static NCollection_Vec3 DX()
338   {
339     return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
340   }
341
342   //! Constuct DY unit vector.
343   static NCollection_Vec3 DY()
344   {
345     return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
346   }
347
348   //! Constuct DZ unit vector.
349   static NCollection_Vec3 DZ()
350   {
351     return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
352   }
353
354 private:
355
356   Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
357
358 };
359
360 //! Optimized concretization for float type.
361 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
362 {
363   Multiply (1.0f / theInvFactor);
364   return *this;
365 }
366
367 //! Optimized concretization for double type.
368 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
369 {
370   Multiply (1.0 / theInvFactor);
371   return *this;
372 }
373
374 #endif // _NCollection_Vec3_H__