1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
19 #ifndef _NCollection_Vec3_H__
20 #define _NCollection_Vec3_H__
24 #include <NCollection_Vec2.hxx>
26 //! Auxiliary macros to define couple of similar access components as vector methods
27 #define NCOLLECTION_VEC_COMPONENTS_3D(theX, theY, theZ) \
28 const NCollection_Vec3<Element_t> theX##theY##theZ() const { return NCollection_Vec3<Element_t>(theX(), theY(), theZ()); } \
29 const NCollection_Vec3<Element_t> theX##theZ##theY() const { return NCollection_Vec3<Element_t>(theX(), theZ(), theY()); } \
30 const NCollection_Vec3<Element_t> theY##theX##theZ() const { return NCollection_Vec3<Element_t>(theY(), theX(), theZ()); } \
31 const NCollection_Vec3<Element_t> theY##theZ##theX() const { return NCollection_Vec3<Element_t>(theY(), theZ(), theX()); } \
32 const NCollection_Vec3<Element_t> theZ##theY##theX() const { return NCollection_Vec3<Element_t>(theZ(), theY(), theX()); } \
33 const NCollection_Vec3<Element_t> theZ##theX##theY() const { return NCollection_Vec3<Element_t>(theZ(), theX(), theY()); }
35 //! Generic 3-components vector.
36 //! To be used as RGB color pixel or XYZ 3D-point.
37 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
38 template<typename Element_t>
39 class NCollection_Vec3
44 //! Returns the number of components.
50 //! Empty constructor. Construct the zero vector.
53 std::memset (this, 0, sizeof(NCollection_Vec3));
56 //! Initialize ALL components of vector within specified value.
57 explicit NCollection_Vec3 (Element_t theValue)
59 v[0] = v[1] = v[2] = theValue;
62 //! Per-component constructor.
63 explicit NCollection_Vec3 (const Element_t theX,
72 //! Constructor from 2-components vector.
73 explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2)
81 NCollection_Vec3 (const NCollection_Vec3& theVec3)
83 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3));
86 //! Assignment operator.
87 const NCollection_Vec3& operator= (const NCollection_Vec3& theVec3)
89 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3));
93 //! Alias to 1st component as X coordinate in XYZ.
94 Element_t x() const { return v[0]; }
96 //! Alias to 1st component as RED channel in RGB.
97 Element_t r() const { return v[0]; }
99 //! Alias to 2nd component as Y coordinate in XYZ.
100 Element_t y() const { return v[1]; }
102 //! Alias to 2nd component as GREEN channel in RGB.
103 Element_t g() const { return v[1]; }
105 //! Alias to 3rd component as Z coordinate in XYZ.
106 Element_t z() const { return v[2]; }
108 //! Alias to 3rd component as BLUE channel in RGB.
109 Element_t b() const { return v[2]; }
111 //! @return 2 components by their names in specified order (in GLSL-style)
112 NCOLLECTION_VEC_COMPONENTS_2D(x, y);
113 NCOLLECTION_VEC_COMPONENTS_2D(x, z);
114 NCOLLECTION_VEC_COMPONENTS_2D(y, z);
116 //! @return 3 components by their names in specified order (in GLSL-style)
117 NCOLLECTION_VEC_COMPONENTS_3D(x, y, z);
119 //! Alias to 1st component as X coordinate in XYZ.
120 Element_t& x() { return v[0]; }
122 //! Alias to 1st component as RED channel in RGB.
123 Element_t& r() { return v[0]; }
125 //! Alias to 2nd component as Y coordinate in XYZ.
126 Element_t& y() { return v[1]; }
128 //! Alias to 2nd component as GREEN channel in RGB.
129 Element_t& g() { return v[1]; }
131 //! Alias to 3rd component as Z coordinate in XYZ.
132 Element_t& z() { return v[2]; }
134 //! Alias to 3rd component as BLUE channel in RGB.
135 Element_t& b() { return v[2]; }
137 //! @return XY-components modifiable vector
138 NCollection_Vec2<Element_t>& xy()
140 return *((NCollection_Vec2<Element_t>* )&v[0]);
143 //! @return YZ-components modifiable vector
144 NCollection_Vec2<Element_t>& yz()
146 return *((NCollection_Vec2<Element_t>* )&v[1]);
149 //! Raw access to the data (for OpenGL exchange).
150 const Element_t* GetData() const { return v; }
151 operator const Element_t*() const { return v; }
152 operator Element_t*() { return v; }
154 //! Compute per-component summary.
155 NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
163 //! Compute per-component summary.
164 friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
165 const NCollection_Vec3& theRight)
167 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
168 return aSumm += theRight;
172 NCollection_Vec3 operator-() const
174 return NCollection_Vec3 (-x(), -y(), -z());
177 //! Compute per-component subtraction.
178 NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
186 //! Compute per-component subtraction.
187 friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
188 const NCollection_Vec3& theRight)
190 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
191 return aSumm -= theRight;
194 //! Compute per-component multiplication by scale factor.
195 void Multiply (const Element_t theFactor)
202 //! Compute per-component multiplication.
203 NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
205 v[0] *= theRight.v[0];
206 v[1] *= theRight.v[1];
207 v[2] *= theRight.v[2];
211 //! Compute per-component multiplication.
212 friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
213 const NCollection_Vec3& theRight)
215 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
216 return aResult *= theRight;
219 //! Compute per-component multiplication by scale factor.
220 NCollection_Vec3& operator*= (const Element_t theFactor)
222 Multiply (theFactor);
226 //! Compute per-component multiplication by scale factor.
227 NCollection_Vec3 operator* (const Element_t theFactor) const
229 return Multiplied (theFactor);
232 //! Compute per-component multiplication by scale factor.
233 NCollection_Vec3 Multiplied (const Element_t theFactor) const
235 NCollection_Vec3 aCopyVec3 (*this);
236 aCopyVec3 *= theFactor;
240 //! Compute per-component division by scale factor.
241 NCollection_Vec3& operator/= (const Element_t theInvFactor)
243 v[0] /= theInvFactor;
244 v[1] /= theInvFactor;
245 v[2] /= theInvFactor;
249 //! Compute per-component division by scale factor.
250 NCollection_Vec3 operator/ (const Element_t theInvFactor)
252 NCollection_Vec3 aResult (this);
253 return aResult /= theInvFactor;
256 //! Computes the dot product.
257 Element_t Dot (const NCollection_Vec3& theOther) const
259 return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
262 //! Computes the vector modulus (magnitude, length).
263 Element_t Modulus() const
265 return std::sqrt (x() * x() + y() * y() + z() * z());
268 //! Computes the square of vector modulus (magnitude, length).
269 //! This method may be used for performance tricks.
270 Element_t SquareModulus() const
272 return x() * x() + y() * y() + z() * z();
275 //! Normalize the vector.
278 Element_t aModulus = Modulus();
279 if (aModulus != Element_t(0)) // just avoid divide by zero
281 x() = x() / aModulus;
282 y() = y() / aModulus;
283 z() = z() / aModulus;
287 //! Normalize the vector.
288 NCollection_Vec3 Normalized() const
290 NCollection_Vec3 aCopy (*this);
295 //! Computes the cross product.
296 static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
297 const NCollection_Vec3& theVec2)
299 return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
300 theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
301 theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
304 //! Compute linear interpolation between to vectors.
305 //! @param theT - interpolation coefficient 0..1;
306 //! @return interpolation result.
307 static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
308 const NCollection_Vec3& theTo,
309 const Element_t theT)
311 return theFrom * (Element_t(1) - theT) + theTo * theT;
314 //! Constuct DX unit vector.
315 static NCollection_Vec3 DX()
317 return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
320 //! Constuct DY unit vector.
321 static NCollection_Vec3 DY()
323 return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
326 //! Constuct DZ unit vector.
327 static NCollection_Vec3 DZ()
329 return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
334 Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
338 //! Optimized concretization for float type.
339 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
341 Multiply (1.0f / theInvFactor);
345 //! Optimized concretization for double type.
346 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
348 Multiply (1.0 / theInvFactor);
352 #endif // _NCollection_Vec3_H__