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>
21 #include <Standard_Dump.hxx>
23 //! Auxiliary macros to define couple of similar access components as vector methods
24 #define NCOLLECTION_VEC_COMPONENTS_3D(theX, theY, theZ) \
25 const NCollection_Vec3<Element_t> theX##theY##theZ() const { return NCollection_Vec3<Element_t>(theX(), theY(), theZ()); } \
26 const NCollection_Vec3<Element_t> theX##theZ##theY() const { return NCollection_Vec3<Element_t>(theX(), theZ(), theY()); } \
27 const NCollection_Vec3<Element_t> theY##theX##theZ() const { return NCollection_Vec3<Element_t>(theY(), theX(), theZ()); } \
28 const NCollection_Vec3<Element_t> theY##theZ##theX() const { return NCollection_Vec3<Element_t>(theY(), theZ(), theX()); } \
29 const NCollection_Vec3<Element_t> theZ##theY##theX() const { return NCollection_Vec3<Element_t>(theZ(), theY(), theX()); } \
30 const NCollection_Vec3<Element_t> theZ##theX##theY() const { return NCollection_Vec3<Element_t>(theZ(), theX(), theY()); }
32 //! Generic 3-components vector.
33 //! To be used as RGB color pixel or XYZ 3D-point.
34 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
35 template<typename Element_t>
36 class NCollection_Vec3
41 //! Returns the number of components.
47 //! Empty constructor. Construct the zero vector.
50 std::memset (this, 0, sizeof(NCollection_Vec3));
53 //! Initialize ALL components of vector within specified value.
54 explicit NCollection_Vec3 (Element_t theValue)
56 v[0] = v[1] = v[2] = theValue;
59 //! Per-component constructor.
60 explicit NCollection_Vec3 (const Element_t theX,
69 //! Constructor from 2-components vector + optional 3rd value.
70 explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ = Element_t(0))
77 //! Conversion constructor (explicitly converts some 3-component vector with other element type
78 //! to a new 3-component vector with the element type Element_t,
79 //! whose elements are static_cast'ed corresponding elements of theOtherVec3 vector)
80 //! @tparam OtherElement_t the element type of the other 3-component vector theOtherVec3
81 //! @param theOtherVec3 the 3-component vector that needs to be converted
82 template <typename OtherElement_t>
83 explicit NCollection_Vec3 (const NCollection_Vec3<OtherElement_t>& theOtherVec3)
85 v[0] = static_cast<Element_t> (theOtherVec3[0]);
86 v[1] = static_cast<Element_t> (theOtherVec3[1]);
87 v[2] = static_cast<Element_t> (theOtherVec3[2]);
90 //! Assign new values to the vector.
91 void SetValues (const Element_t theX,
100 //! Assign new values to the vector.
101 void SetValues (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ)
108 //! Alias to 1st component as X coordinate in XYZ.
109 Element_t x() const { return v[0]; }
111 //! Alias to 1st component as RED channel in RGB.
112 Element_t r() const { return v[0]; }
114 //! Alias to 2nd component as Y coordinate in XYZ.
115 Element_t y() const { return v[1]; }
117 //! Alias to 2nd component as GREEN channel in RGB.
118 Element_t g() const { return v[1]; }
120 //! Alias to 3rd component as Z coordinate in XYZ.
121 Element_t z() const { return v[2]; }
123 //! Alias to 3rd component as BLUE channel in RGB.
124 Element_t b() const { return v[2]; }
126 //! @return 2 components by their names in specified order (in GLSL-style)
127 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
128 NCOLLECTION_VEC_COMPONENTS_2D(x, z)
129 NCOLLECTION_VEC_COMPONENTS_2D(y, z)
131 //! @return 3 components by their names in specified order (in GLSL-style)
132 NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
134 //! Alias to 1st component as X coordinate in XYZ.
135 Element_t& x() { return v[0]; }
137 //! Alias to 1st component as RED channel in RGB.
138 Element_t& r() { return v[0]; }
140 //! Alias to 2nd component as Y coordinate in XYZ.
141 Element_t& y() { return v[1]; }
143 //! Alias to 2nd component as GREEN channel in RGB.
144 Element_t& g() { return v[1]; }
146 //! Alias to 3rd component as Z coordinate in XYZ.
147 Element_t& z() { return v[2]; }
149 //! Alias to 3rd component as BLUE channel in RGB.
150 Element_t& b() { return v[2]; }
152 //! Check this vector with another vector for equality (without tolerance!).
153 bool IsEqual (const NCollection_Vec3& theOther) const
155 return v[0] == theOther.v[0]
156 && v[1] == theOther.v[1]
157 && v[2] == theOther.v[2];
160 //! Check this vector with another vector for equality (without tolerance!).
161 bool operator== (const NCollection_Vec3& theOther) { return IsEqual (theOther); }
162 bool operator== (const NCollection_Vec3& theOther) const { return IsEqual (theOther); }
164 //! Check this vector with another vector for non-equality (without tolerance!).
165 bool operator!= (const NCollection_Vec3& theOther) { return !IsEqual (theOther); }
166 bool operator!= (const NCollection_Vec3& theOther) const { return !IsEqual (theOther); }
168 //! Raw access to the data (for OpenGL exchange).
169 const Element_t* GetData() const { return v; }
170 Element_t* ChangeData() { return v; }
171 operator const Element_t*() const { return v; }
172 operator Element_t*() { return v; }
174 //! Compute per-component summary.
175 NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
183 //! Compute per-component summary.
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;
192 NCollection_Vec3 operator-() const
194 return NCollection_Vec3 (-x(), -y(), -z());
197 //! Compute per-component subtraction.
198 NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
206 //! Compute per-component subtraction.
207 friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
208 const NCollection_Vec3& theRight)
210 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
211 return aSumm -= theRight;
214 //! Compute per-component multiplication by scale factor.
215 void Multiply (const Element_t theFactor)
222 //! Compute per-component multiplication.
223 NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
225 v[0] *= theRight.v[0];
226 v[1] *= theRight.v[1];
227 v[2] *= theRight.v[2];
231 //! Compute per-component multiplication.
232 friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
233 const NCollection_Vec3& theRight)
235 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
236 return aResult *= theRight;
239 //! Compute per-component multiplication by scale factor.
240 NCollection_Vec3& operator*= (const Element_t theFactor)
242 Multiply (theFactor);
246 //! Compute per-component multiplication by scale factor.
247 NCollection_Vec3 operator* (const Element_t theFactor) const
249 return Multiplied (theFactor);
252 //! Compute per-component multiplication by scale factor.
253 NCollection_Vec3 Multiplied (const Element_t theFactor) const
255 NCollection_Vec3 aCopyVec3 (*this);
256 aCopyVec3 *= theFactor;
260 //! Compute component-wise minimum of two vectors.
261 NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const
263 return NCollection_Vec3 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
264 v[1] < theVec.v[1] ? v[1] : theVec.v[1],
265 v[2] < theVec.v[2] ? v[2] : theVec.v[2]);
268 //! Compute component-wise maximum of two vectors.
269 NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const
271 return NCollection_Vec3 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
272 v[1] > theVec.v[1] ? v[1] : theVec.v[1],
273 v[2] > theVec.v[2] ? v[2] : theVec.v[2]);
276 //! Compute component-wise modulus of the vector.
277 NCollection_Vec3 cwiseAbs() const
279 return NCollection_Vec3 (std::abs (v[0]),
284 //! Compute maximum component of the vector.
285 Element_t maxComp() const
287 return v[0] > v[1] ? (v[0] > v[2] ? v[0] : v[2])
288 : (v[1] > v[2] ? v[1] : v[2]);
291 //! Compute minimum component of the vector.
292 Element_t minComp() const
294 return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2])
295 : (v[1] < v[2] ? v[1] : v[2]);
298 //! Compute per-component division by scale factor.
299 NCollection_Vec3& operator/= (const Element_t theInvFactor)
301 v[0] /= theInvFactor;
302 v[1] /= theInvFactor;
303 v[2] /= theInvFactor;
307 //! Compute per-component division.
308 NCollection_Vec3& operator/= (const NCollection_Vec3& theRight)
310 v[0] /= theRight.v[0];
311 v[1] /= theRight.v[1];
312 v[2] /= theRight.v[2];
316 //! Compute per-component division by scale factor.
317 NCollection_Vec3 operator/ (const Element_t theInvFactor) const
319 NCollection_Vec3 aResult (*this);
320 return aResult /= theInvFactor;
323 //! Compute per-component division.
324 friend NCollection_Vec3 operator/ (const NCollection_Vec3& theLeft,
325 const NCollection_Vec3& theRight)
327 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
328 return aResult /= theRight;
331 //! Computes the dot product.
332 Element_t Dot (const NCollection_Vec3& theOther) const
334 return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
337 //! Computes the vector modulus (magnitude, length).
338 Element_t Modulus() const
340 return std::sqrt (x() * x() + y() * y() + z() * z());
343 //! Computes the square of vector modulus (magnitude, length).
344 //! This method may be used for performance tricks.
345 Element_t SquareModulus() const
347 return x() * x() + y() * y() + z() * z();
350 //! Normalize the vector.
353 Element_t aModulus = Modulus();
354 if (aModulus != Element_t(0)) // just avoid divide by zero
356 x() = x() / aModulus;
357 y() = y() / aModulus;
358 z() = z() / aModulus;
362 //! Normalize the vector.
363 NCollection_Vec3 Normalized() const
365 NCollection_Vec3 aCopy (*this);
370 //! Computes the cross product.
371 static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
372 const NCollection_Vec3& theVec2)
374 return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
375 theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
376 theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
379 //! Compute linear interpolation between to vectors.
380 //! @param theT - interpolation coefficient 0..1;
381 //! @return interpolation result.
382 static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
383 const NCollection_Vec3& theTo,
384 const Element_t theT)
386 return theFrom * (Element_t(1) - theT) + theTo * theT;
389 //! Constuct DX unit vector.
390 static NCollection_Vec3 DX()
392 return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
395 //! Constuct DY unit vector.
396 static NCollection_Vec3 DY()
398 return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
401 //! Constuct DZ unit vector.
402 static NCollection_Vec3 DZ()
404 return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
407 //! Dumps the content of me into the stream
408 void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const
411 OCCT_DUMP_FIELD_VALUES_NUMERICAL (theOStream, "Vec3", 3, v[0], v[1], v[2])
416 Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
420 //! Optimized concretization for float type.
421 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
423 Multiply (1.0f / theInvFactor);
427 //! Optimized concretization for double type.
428 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
430 Multiply (1.0 / theInvFactor);
434 #endif // _NCollection_Vec3_H__