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 Element_t* ChangeData() { return v; }
152 operator const Element_t*() const { return v; }
153 operator Element_t*() { return v; }
155 //! Compute per-component summary.
156 NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
164 //! Compute per-component summary.
165 friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
166 const NCollection_Vec3& theRight)
168 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
169 return aSumm += theRight;
173 NCollection_Vec3 operator-() const
175 return NCollection_Vec3 (-x(), -y(), -z());
178 //! Compute per-component subtraction.
179 NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
187 //! Compute per-component subtraction.
188 friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
189 const NCollection_Vec3& theRight)
191 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
192 return aSumm -= theRight;
195 //! Compute per-component multiplication by scale factor.
196 void Multiply (const Element_t theFactor)
203 //! Compute per-component multiplication.
204 NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
206 v[0] *= theRight.v[0];
207 v[1] *= theRight.v[1];
208 v[2] *= theRight.v[2];
212 //! Compute per-component multiplication.
213 friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
214 const NCollection_Vec3& theRight)
216 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
217 return aResult *= theRight;
220 //! Compute per-component multiplication by scale factor.
221 NCollection_Vec3& operator*= (const Element_t theFactor)
223 Multiply (theFactor);
227 //! Compute per-component multiplication by scale factor.
228 NCollection_Vec3 operator* (const Element_t theFactor) const
230 return Multiplied (theFactor);
233 //! Compute per-component multiplication by scale factor.
234 NCollection_Vec3 Multiplied (const Element_t theFactor) const
236 NCollection_Vec3 aCopyVec3 (*this);
237 aCopyVec3 *= theFactor;
241 //! Compute per-component division by scale factor.
242 NCollection_Vec3& operator/= (const Element_t theInvFactor)
244 v[0] /= theInvFactor;
245 v[1] /= theInvFactor;
246 v[2] /= theInvFactor;
250 //! Compute per-component division by scale factor.
251 NCollection_Vec3 operator/ (const Element_t theInvFactor)
253 NCollection_Vec3 aResult (this);
254 return aResult /= theInvFactor;
257 //! Computes the dot product.
258 Element_t Dot (const NCollection_Vec3& theOther) const
260 return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
263 //! Computes the vector modulus (magnitude, length).
264 Element_t Modulus() const
266 return std::sqrt (x() * x() + y() * y() + z() * z());
269 //! Computes the square of vector modulus (magnitude, length).
270 //! This method may be used for performance tricks.
271 Element_t SquareModulus() const
273 return x() * x() + y() * y() + z() * z();
276 //! Normalize the vector.
279 Element_t aModulus = Modulus();
280 if (aModulus != Element_t(0)) // just avoid divide by zero
282 x() = x() / aModulus;
283 y() = y() / aModulus;
284 z() = z() / aModulus;
288 //! Normalize the vector.
289 NCollection_Vec3 Normalized() const
291 NCollection_Vec3 aCopy (*this);
296 //! Computes the cross product.
297 static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
298 const NCollection_Vec3& theVec2)
300 return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
301 theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
302 theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
305 //! Compute linear interpolation between to vectors.
306 //! @param theT - interpolation coefficient 0..1;
307 //! @return interpolation result.
308 static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
309 const NCollection_Vec3& theTo,
310 const Element_t theT)
312 return theFrom * (Element_t(1) - theT) + theTo * theT;
315 //! Constuct DX unit vector.
316 static NCollection_Vec3 DX()
318 return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
321 //! Constuct DY unit vector.
322 static NCollection_Vec3 DY()
324 return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
327 //! Constuct DZ unit vector.
328 static NCollection_Vec3 DZ()
330 return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
335 Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
339 //! Optimized concretization for float type.
340 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
342 Multiply (1.0f / theInvFactor);
346 //! Optimized concretization for double type.
347 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
349 Multiply (1.0 / theInvFactor);
353 #endif // _NCollection_Vec3_H__