1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 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
7 // under the terms of the GNU Lesser General Public 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_Vec3_H__
16 #define _NCollection_Vec3_H__
20 #include <NCollection_Vec2.hxx>
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()); }
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
40 //! Returns the number of components.
46 //! Empty constructor. Construct the zero vector.
49 std::memset (this, 0, sizeof(NCollection_Vec3));
52 //! Initialize ALL components of vector within specified value.
53 explicit NCollection_Vec3 (Element_t theValue)
55 v[0] = v[1] = v[2] = theValue;
58 //! Per-component constructor.
59 explicit NCollection_Vec3 (const Element_t theX,
68 //! Constructor from 2-components vector.
69 explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2)
77 NCollection_Vec3 (const NCollection_Vec3& theVec3)
79 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3));
82 //! Assignment operator.
83 const NCollection_Vec3& operator= (const NCollection_Vec3& theVec3)
85 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3));
89 //! Alias to 1st component as X coordinate in XYZ.
90 Element_t x() const { return v[0]; }
92 //! Alias to 1st component as RED channel in RGB.
93 Element_t r() const { return v[0]; }
95 //! Alias to 2nd component as Y coordinate in XYZ.
96 Element_t y() const { return v[1]; }
98 //! Alias to 2nd component as GREEN channel in RGB.
99 Element_t g() const { return v[1]; }
101 //! Alias to 3rd component as Z coordinate in XYZ.
102 Element_t z() const { return v[2]; }
104 //! Alias to 3rd component as BLUE channel in RGB.
105 Element_t b() const { return v[2]; }
107 //! @return 2 components by their names in specified order (in GLSL-style)
108 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
109 NCOLLECTION_VEC_COMPONENTS_2D(x, z)
110 NCOLLECTION_VEC_COMPONENTS_2D(y, z)
112 //! @return 3 components by their names in specified order (in GLSL-style)
113 NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
115 //! Alias to 1st component as X coordinate in XYZ.
116 Element_t& x() { return v[0]; }
118 //! Alias to 1st component as RED channel in RGB.
119 Element_t& r() { return v[0]; }
121 //! Alias to 2nd component as Y coordinate in XYZ.
122 Element_t& y() { return v[1]; }
124 //! Alias to 2nd component as GREEN channel in RGB.
125 Element_t& g() { return v[1]; }
127 //! Alias to 3rd component as Z coordinate in XYZ.
128 Element_t& z() { return v[2]; }
130 //! Alias to 3rd component as BLUE channel in RGB.
131 Element_t& b() { return v[2]; }
133 //! @return XY-components modifiable vector
134 NCollection_Vec2<Element_t>& xy()
136 return *((NCollection_Vec2<Element_t>* )&v[0]);
139 //! @return YZ-components modifiable vector
140 NCollection_Vec2<Element_t>& yz()
142 return *((NCollection_Vec2<Element_t>* )&v[1]);
145 //! Raw access to the data (for OpenGL exchange).
146 const Element_t* GetData() const { return v; }
147 Element_t* ChangeData() { return v; }
148 operator const Element_t*() const { return v; }
149 operator Element_t*() { return v; }
151 //! Compute per-component summary.
152 NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
160 //! Compute per-component summary.
161 friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
162 const NCollection_Vec3& theRight)
164 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
165 return aSumm += theRight;
169 NCollection_Vec3 operator-() const
171 return NCollection_Vec3 (-x(), -y(), -z());
174 //! Compute per-component subtraction.
175 NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
183 //! Compute per-component subtraction.
184 friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
185 const NCollection_Vec3& theRight)
187 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
188 return aSumm -= theRight;
191 //! Compute per-component multiplication by scale factor.
192 void Multiply (const Element_t theFactor)
199 //! Compute per-component multiplication.
200 NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
202 v[0] *= theRight.v[0];
203 v[1] *= theRight.v[1];
204 v[2] *= theRight.v[2];
208 //! Compute per-component multiplication.
209 friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
210 const NCollection_Vec3& theRight)
212 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
213 return aResult *= theRight;
216 //! Compute per-component multiplication by scale factor.
217 NCollection_Vec3& operator*= (const Element_t theFactor)
219 Multiply (theFactor);
223 //! Compute per-component multiplication by scale factor.
224 NCollection_Vec3 operator* (const Element_t theFactor) const
226 return Multiplied (theFactor);
229 //! Compute per-component multiplication by scale factor.
230 NCollection_Vec3 Multiplied (const Element_t theFactor) const
232 NCollection_Vec3 aCopyVec3 (*this);
233 aCopyVec3 *= theFactor;
237 //! Compute component-wise minimum of two vectors.
238 NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const
240 return NCollection_Vec3 (Min (v[0], theVec.v[0]),
241 Min (v[1], theVec.v[1]),
242 Min (v[2], theVec.v[2]));
245 //! Compute component-wise maximum of two vectors.
246 NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const
248 return NCollection_Vec3 (Max (v[0], theVec.v[0]),
249 Max (v[1], theVec.v[1]),
250 Max (v[2], theVec.v[2]));
253 //! Compute per-component division by scale factor.
254 NCollection_Vec3& operator/= (const Element_t theInvFactor)
256 v[0] /= theInvFactor;
257 v[1] /= theInvFactor;
258 v[2] /= theInvFactor;
262 //! Compute per-component division by scale factor.
263 NCollection_Vec3 operator/ (const Element_t theInvFactor)
265 NCollection_Vec3 aResult (this);
266 return aResult /= theInvFactor;
269 //! Computes the dot product.
270 Element_t Dot (const NCollection_Vec3& theOther) const
272 return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
275 //! Computes the vector modulus (magnitude, length).
276 Element_t Modulus() const
278 return std::sqrt (x() * x() + y() * y() + z() * z());
281 //! Computes the square of vector modulus (magnitude, length).
282 //! This method may be used for performance tricks.
283 Element_t SquareModulus() const
285 return x() * x() + y() * y() + z() * z();
288 //! Normalize the vector.
291 Element_t aModulus = Modulus();
292 if (aModulus != Element_t(0)) // just avoid divide by zero
294 x() = x() / aModulus;
295 y() = y() / aModulus;
296 z() = z() / aModulus;
300 //! Normalize the vector.
301 NCollection_Vec3 Normalized() const
303 NCollection_Vec3 aCopy (*this);
308 //! Computes the cross product.
309 static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
310 const NCollection_Vec3& theVec2)
312 return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
313 theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
314 theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
317 //! Compute linear interpolation between to vectors.
318 //! @param theT - interpolation coefficient 0..1;
319 //! @return interpolation result.
320 static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
321 const NCollection_Vec3& theTo,
322 const Element_t theT)
324 return theFrom * (Element_t(1) - theT) + theTo * theT;
327 //! Constuct DX unit vector.
328 static NCollection_Vec3 DX()
330 return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
333 //! Constuct DY unit vector.
334 static NCollection_Vec3 DY()
336 return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
339 //! Constuct DZ unit vector.
340 static NCollection_Vec3 DZ()
342 return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
347 Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
351 //! Optimized concretization for float type.
352 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
354 Multiply (1.0f / theInvFactor);
358 //! Optimized concretization for double type.
359 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
361 Multiply (1.0 / theInvFactor);
365 #endif // _NCollection_Vec3_H__