1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-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 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.
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)
76 //! Assign new values to the vector.
77 void SetValues (const Element_t theX,
86 //! Alias to 1st component as X coordinate in XYZ.
87 Element_t x() const { return v[0]; }
89 //! Alias to 1st component as RED channel in RGB.
90 Element_t r() const { return v[0]; }
92 //! Alias to 2nd component as Y coordinate in XYZ.
93 Element_t y() const { return v[1]; }
95 //! Alias to 2nd component as GREEN channel in RGB.
96 Element_t g() const { return v[1]; }
98 //! Alias to 3rd component as Z coordinate in XYZ.
99 Element_t z() const { return v[2]; }
101 //! Alias to 3rd component as BLUE channel in RGB.
102 Element_t b() const { return v[2]; }
104 //! @return 2 components by their names in specified order (in GLSL-style)
105 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
106 NCOLLECTION_VEC_COMPONENTS_2D(x, z)
107 NCOLLECTION_VEC_COMPONENTS_2D(y, z)
109 //! @return 3 components by their names in specified order (in GLSL-style)
110 NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
112 //! Alias to 1st component as X coordinate in XYZ.
113 Element_t& x() { return v[0]; }
115 //! Alias to 1st component as RED channel in RGB.
116 Element_t& r() { return v[0]; }
118 //! Alias to 2nd component as Y coordinate in XYZ.
119 Element_t& y() { return v[1]; }
121 //! Alias to 2nd component as GREEN channel in RGB.
122 Element_t& g() { return v[1]; }
124 //! Alias to 3rd component as Z coordinate in XYZ.
125 Element_t& z() { return v[2]; }
127 //! Alias to 3rd component as BLUE channel in RGB.
128 Element_t& b() { return v[2]; }
130 //! @return XY-components modifiable vector
131 NCollection_Vec2<Element_t>& xy()
133 return *((NCollection_Vec2<Element_t>* )&v[0]);
136 //! @return YZ-components modifiable vector
137 NCollection_Vec2<Element_t>& yz()
139 return *((NCollection_Vec2<Element_t>* )&v[1]);
142 //! Check this vector with another vector for equality (without tolerance!).
143 bool IsEqual (const NCollection_Vec3& theOther) const
145 return v[0] == theOther.v[0]
146 && v[1] == theOther.v[1]
147 && v[2] == theOther.v[2];
150 //! Check this vector with another vector for equality (without tolerance!).
151 bool operator== (const NCollection_Vec3& theOther) { return IsEqual (theOther); }
152 bool operator== (const NCollection_Vec3& theOther) const { return IsEqual (theOther); }
154 //! Check this vector with another vector for non-equality (without tolerance!).
155 bool operator!= (const NCollection_Vec3& theOther) { return !IsEqual (theOther); }
156 bool operator!= (const NCollection_Vec3& theOther) const { return !IsEqual (theOther); }
158 //! Raw access to the data (for OpenGL exchange).
159 const Element_t* GetData() const { return v; }
160 Element_t* ChangeData() { return v; }
161 operator const Element_t*() const { return v; }
162 operator Element_t*() { return v; }
164 //! Compute per-component summary.
165 NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
173 //! Compute per-component summary.
174 friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
175 const NCollection_Vec3& theRight)
177 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
178 return aSumm += theRight;
182 NCollection_Vec3 operator-() const
184 return NCollection_Vec3 (-x(), -y(), -z());
187 //! Compute per-component subtraction.
188 NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
196 //! Compute per-component subtraction.
197 friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
198 const NCollection_Vec3& theRight)
200 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
201 return aSumm -= theRight;
204 //! Compute per-component multiplication by scale factor.
205 void Multiply (const Element_t theFactor)
212 //! Compute per-component multiplication.
213 NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
215 v[0] *= theRight.v[0];
216 v[1] *= theRight.v[1];
217 v[2] *= theRight.v[2];
221 //! Compute per-component multiplication.
222 friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
223 const NCollection_Vec3& theRight)
225 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
226 return aResult *= theRight;
229 //! Compute per-component multiplication by scale factor.
230 NCollection_Vec3& operator*= (const Element_t theFactor)
232 Multiply (theFactor);
236 //! Compute per-component multiplication by scale factor.
237 NCollection_Vec3 operator* (const Element_t theFactor) const
239 return Multiplied (theFactor);
242 //! Compute per-component multiplication by scale factor.
243 NCollection_Vec3 Multiplied (const Element_t theFactor) const
245 NCollection_Vec3 aCopyVec3 (*this);
246 aCopyVec3 *= theFactor;
250 //! Compute component-wise minimum of two vectors.
251 NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const
253 return NCollection_Vec3 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
254 v[1] < theVec.v[1] ? v[1] : theVec.v[1],
255 v[2] < theVec.v[2] ? v[2] : theVec.v[2]);
258 //! Compute component-wise maximum of two vectors.
259 NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const
261 return NCollection_Vec3 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
262 v[1] > theVec.v[1] ? v[1] : theVec.v[1],
263 v[2] > theVec.v[2] ? v[2] : theVec.v[2]);
266 //! Compute component-wise modulus of the vector.
267 NCollection_Vec3 cwiseAbs() const
269 return NCollection_Vec3 (std::abs (v[0]),
274 //! Compute maximum component of the vector.
275 Element_t maxComp() const
277 return v[0] > v[1] ? (v[0] > v[2] ? v[0] : v[2])
278 : (v[1] > v[2] ? v[1] : v[2]);
281 //! Compute minimum component of the vector.
282 Element_t minComp() const
284 return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2])
285 : (v[1] < v[2] ? v[1] : v[2]);
288 //! Compute per-component division by scale factor.
289 NCollection_Vec3& operator/= (const Element_t theInvFactor)
291 v[0] /= theInvFactor;
292 v[1] /= theInvFactor;
293 v[2] /= theInvFactor;
297 //! Compute per-component division by scale factor.
298 NCollection_Vec3 operator/ (const Element_t theInvFactor)
300 NCollection_Vec3 aResult (*this);
301 return aResult /= theInvFactor;
304 //! Computes the dot product.
305 Element_t Dot (const NCollection_Vec3& theOther) const
307 return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
310 //! Computes the vector modulus (magnitude, length).
311 Element_t Modulus() const
313 return std::sqrt (x() * x() + y() * y() + z() * z());
316 //! Computes the square of vector modulus (magnitude, length).
317 //! This method may be used for performance tricks.
318 Element_t SquareModulus() const
320 return x() * x() + y() * y() + z() * z();
323 //! Normalize the vector.
326 Element_t aModulus = Modulus();
327 if (aModulus != Element_t(0)) // just avoid divide by zero
329 x() = x() / aModulus;
330 y() = y() / aModulus;
331 z() = z() / aModulus;
335 //! Normalize the vector.
336 NCollection_Vec3 Normalized() const
338 NCollection_Vec3 aCopy (*this);
343 //! Computes the cross product.
344 static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
345 const NCollection_Vec3& theVec2)
347 return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
348 theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
349 theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
352 //! Compute linear interpolation between to vectors.
353 //! @param theT - interpolation coefficient 0..1;
354 //! @return interpolation result.
355 static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
356 const NCollection_Vec3& theTo,
357 const Element_t theT)
359 return theFrom * (Element_t(1) - theT) + theTo * theT;
362 //! Constuct DX unit vector.
363 static NCollection_Vec3 DX()
365 return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
368 //! Constuct DY unit vector.
369 static NCollection_Vec3 DY()
371 return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
374 //! Constuct DZ unit vector.
375 static NCollection_Vec3 DZ()
377 return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
382 Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
386 //! Optimized concretization for float type.
387 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
389 Multiply (1.0f / theInvFactor);
393 //! Optimized concretization for double type.
394 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
396 Multiply (1.0 / theInvFactor);
400 #endif // _NCollection_Vec3_H__