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 //! Alias to 1st component as X coordinate in XYZ.
77 Element_t x() const { return v[0]; }
79 //! Alias to 1st component as RED channel in RGB.
80 Element_t r() const { return v[0]; }
82 //! Alias to 2nd component as Y coordinate in XYZ.
83 Element_t y() const { return v[1]; }
85 //! Alias to 2nd component as GREEN channel in RGB.
86 Element_t g() const { return v[1]; }
88 //! Alias to 3rd component as Z coordinate in XYZ.
89 Element_t z() const { return v[2]; }
91 //! Alias to 3rd component as BLUE channel in RGB.
92 Element_t b() const { return v[2]; }
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)
99 //! @return 3 components by their names in specified order (in GLSL-style)
100 NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
102 //! Alias to 1st component as X coordinate in XYZ.
103 Element_t& x() { return v[0]; }
105 //! Alias to 1st component as RED channel in RGB.
106 Element_t& r() { return v[0]; }
108 //! Alias to 2nd component as Y coordinate in XYZ.
109 Element_t& y() { return v[1]; }
111 //! Alias to 2nd component as GREEN channel in RGB.
112 Element_t& g() { return v[1]; }
114 //! Alias to 3rd component as Z coordinate in XYZ.
115 Element_t& z() { return v[2]; }
117 //! Alias to 3rd component as BLUE channel in RGB.
118 Element_t& b() { return v[2]; }
120 //! @return XY-components modifiable vector
121 NCollection_Vec2<Element_t>& xy()
123 return *((NCollection_Vec2<Element_t>* )&v[0]);
126 //! @return YZ-components modifiable vector
127 NCollection_Vec2<Element_t>& yz()
129 return *((NCollection_Vec2<Element_t>* )&v[1]);
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; }
138 //! Compute per-component summary.
139 NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
147 //! Compute per-component summary.
148 friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
149 const NCollection_Vec3& theRight)
151 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
152 return aSumm += theRight;
156 NCollection_Vec3 operator-() const
158 return NCollection_Vec3 (-x(), -y(), -z());
161 //! Compute per-component subtraction.
162 NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
170 //! Compute per-component subtraction.
171 friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
172 const NCollection_Vec3& theRight)
174 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
175 return aSumm -= theRight;
178 //! Compute per-component multiplication by scale factor.
179 void Multiply (const Element_t theFactor)
186 //! Compute per-component multiplication.
187 NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
189 v[0] *= theRight.v[0];
190 v[1] *= theRight.v[1];
191 v[2] *= theRight.v[2];
195 //! Compute per-component multiplication.
196 friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
197 const NCollection_Vec3& theRight)
199 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
200 return aResult *= theRight;
203 //! Compute per-component multiplication by scale factor.
204 NCollection_Vec3& operator*= (const Element_t theFactor)
206 Multiply (theFactor);
210 //! Compute per-component multiplication by scale factor.
211 NCollection_Vec3 operator* (const Element_t theFactor) const
213 return Multiplied (theFactor);
216 //! Compute per-component multiplication by scale factor.
217 NCollection_Vec3 Multiplied (const Element_t theFactor) const
219 NCollection_Vec3 aCopyVec3 (*this);
220 aCopyVec3 *= theFactor;
224 //! Compute component-wise minimum of two vectors.
225 NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const
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]);
232 //! Compute component-wise maximum of two vectors.
233 NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const
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]);
240 //! Compute component-wise modulus of the vector.
241 NCollection_Vec3 cwiseAbs() const
243 return NCollection_Vec3 (std::abs (v[0]),
248 //! Compute maximum component of the vector.
249 Element_t maxComp() const
251 return v[0] > v[1] ? (v[0] > v[2] ? v[0] : v[2])
252 : (v[1] > v[2] ? v[1] : v[2]);
255 //! Compute minimum component of the vector.
256 Element_t minComp() const
258 return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2])
259 : (v[1] < v[2] ? v[1] : v[2]);
262 //! Compute per-component division by scale factor.
263 NCollection_Vec3& operator/= (const Element_t theInvFactor)
265 v[0] /= theInvFactor;
266 v[1] /= theInvFactor;
267 v[2] /= theInvFactor;
271 //! Compute per-component division by scale factor.
272 NCollection_Vec3 operator/ (const Element_t theInvFactor)
274 NCollection_Vec3 aResult (*this);
275 return aResult /= theInvFactor;
278 //! Computes the dot product.
279 Element_t Dot (const NCollection_Vec3& theOther) const
281 return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
284 //! Computes the vector modulus (magnitude, length).
285 Element_t Modulus() const
287 return std::sqrt (x() * x() + y() * y() + z() * z());
290 //! Computes the square of vector modulus (magnitude, length).
291 //! This method may be used for performance tricks.
292 Element_t SquareModulus() const
294 return x() * x() + y() * y() + z() * z();
297 //! Normalize the vector.
300 Element_t aModulus = Modulus();
301 if (aModulus != Element_t(0)) // just avoid divide by zero
303 x() = x() / aModulus;
304 y() = y() / aModulus;
305 z() = z() / aModulus;
309 //! Normalize the vector.
310 NCollection_Vec3 Normalized() const
312 NCollection_Vec3 aCopy (*this);
317 //! Computes the cross product.
318 static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
319 const NCollection_Vec3& theVec2)
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());
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)
333 return theFrom * (Element_t(1) - theT) + theTo * theT;
336 //! Constuct DX unit vector.
337 static NCollection_Vec3 DX()
339 return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
342 //! Constuct DY unit vector.
343 static NCollection_Vec3 DY()
345 return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
348 //! Constuct DZ unit vector.
349 static NCollection_Vec3 DZ()
351 return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
356 Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
360 //! Optimized concretization for float type.
361 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
363 Multiply (1.0f / theInvFactor);
367 //! Optimized concretization for double type.
368 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
370 Multiply (1.0 / theInvFactor);
374 #endif // _NCollection_Vec3_H__