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_Vec2_H__
16 #define _NCollection_Vec2_H__
18 #include <cmath> // std::sqrt()
20 //! Auxiliary macros to define couple of similar access components as vector methods.
21 //! @return 2 components by their names in specified order
22 #define NCOLLECTION_VEC_COMPONENTS_2D(theX, theY) \
23 const NCollection_Vec2<Element_t> theX##theY() const { return NCollection_Vec2<Element_t>(theX(), theY()); } \
24 const NCollection_Vec2<Element_t> theY##theX() const { return NCollection_Vec2<Element_t>(theY(), theX()); }
26 //! Defines the 2D-vector template.
27 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
28 template<typename Element_t>
29 class NCollection_Vec2
34 //! Returns the number of components.
40 //! Empty constructor. Construct the zero vector.
43 v[0] = v[1] = Element_t(0);
46 //! Initialize ALL components of vector within specified value.
47 explicit NCollection_Vec2 (const Element_t theXY)
52 //! Per-component constructor.
53 explicit NCollection_Vec2 (const Element_t theX,
60 //! Alias to 1st component as X coordinate in XY.
61 Element_t x() const { return v[0]; }
63 //! Alias to 2nd component as Y coordinate in XY.
64 Element_t y() const { return v[1]; }
66 //! @return 2 components by their names in specified order (in GLSL-style)
67 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
69 //! Alias to 1st component as X coordinate in XY.
70 Element_t& x() { return v[0]; }
72 //! Alias to 2nd component as Y coordinate in XY.
73 Element_t& y() { return v[1]; }
75 //! Check this vector with another vector for equality (without tolerance!).
76 bool IsEqual (const NCollection_Vec2& theOther) const
78 return v[0] == theOther.v[0]
79 && v[1] == theOther.v[1];
82 //! Check this vector with another vector for equality (without tolerance!).
83 bool operator== (const NCollection_Vec2& theOther) { return IsEqual (theOther); }
84 bool operator== (const NCollection_Vec2& theOther) const { return IsEqual (theOther); }
86 //! Check this vector with another vector for non-equality (without tolerance!).
87 bool operator!= (const NCollection_Vec2& theOther) { return !IsEqual (theOther); }
88 bool operator!= (const NCollection_Vec2& theOther) const { return !IsEqual (theOther); }
90 //! Raw access to the data (for OpenGL exchange).
91 const Element_t* GetData() const { return v; }
92 Element_t* ChangeData() { return v; }
93 operator const Element_t*() const { return v; }
94 operator Element_t*() { return v; }
96 //! Compute per-component summary.
97 NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
104 //! Compute per-component summary.
105 friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft,
106 const NCollection_Vec2& theRight)
108 return NCollection_Vec2 (theLeft.v[0] + theRight.v[0],
109 theLeft.v[1] + theRight.v[1]);
112 //! Compute per-component subtraction.
113 NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
120 //! Compute per-component subtraction.
121 friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
122 const NCollection_Vec2& theRight)
124 return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
125 theLeft.v[1] - theRight.v[1]);
129 NCollection_Vec2 operator-() const
131 return NCollection_Vec2 (-x(), -y());
134 //! Compute per-component multiplication.
135 NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
137 v[0] *= theRight.v[0];
138 v[1] *= theRight.v[1];
142 //! Compute per-component multiplication.
143 friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
144 const NCollection_Vec2& theRight)
146 return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
147 theLeft.v[1] * theRight.v[1]);
150 //! Compute per-component multiplication by scale factor.
151 void Multiply (const Element_t theFactor)
157 //! Compute per-component multiplication by scale factor.
158 NCollection_Vec2 Multiplied (const Element_t theFactor) const
160 return NCollection_Vec2 (v[0] * theFactor,
164 //! Compute component-wise minimum of two vectors.
165 NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
167 return NCollection_Vec2 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
168 v[1] < theVec.v[1] ? v[1] : theVec.v[1]);
171 //! Compute component-wise maximum of two vectors.
172 NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
174 return NCollection_Vec2 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
175 v[1] > theVec.v[1] ? v[1] : theVec.v[1]);
178 //! Compute component-wise modulus of the vector.
179 NCollection_Vec2 cwiseAbs() const
181 return NCollection_Vec2 (std::abs (v[0]),
185 //! Compute maximum component of the vector.
186 Element_t maxComp() const
188 return v[0] > v[1] ? v[0] : v[1];
191 //! Compute minimum component of the vector.
192 Element_t minComp() const
194 return v[0] < v[1] ? v[0] : v[1];
197 //! Compute per-component multiplication by scale factor.
198 NCollection_Vec2& operator*= (const Element_t theFactor)
200 Multiply (theFactor);
204 //! Compute per-component division by scale factor.
205 NCollection_Vec2& operator/= (const Element_t theInvFactor)
207 v[0] /= theInvFactor;
208 v[1] /= theInvFactor;
212 //! Compute per-component multiplication by scale factor.
213 NCollection_Vec2 operator* (const Element_t theFactor) const
215 return Multiplied (theFactor);
218 //! Compute per-component division by scale factor.
219 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
221 return NCollection_Vec2(v[0] / theInvFactor,
222 v[1] / theInvFactor);
225 //! Computes the dot product.
226 Element_t Dot (const NCollection_Vec2& theOther) const
228 return x() * theOther.x() + y() * theOther.y();
231 //! Computes the vector modulus (magnitude, length).
232 Element_t Modulus() const
234 return std::sqrt (x() * x() + y() * y());
237 //! Computes the square of vector modulus (magnitude, length).
238 //! This method may be used for performance tricks.
239 Element_t SquareModulus() const
241 return x() * x() + y() * y();
244 //! Constuct DX unit vector.
245 static NCollection_Vec2 DX()
247 return NCollection_Vec2 (Element_t(1), Element_t(0));
250 //! Constuct DY unit vector.
251 static NCollection_Vec2 DY()
253 return NCollection_Vec2 (Element_t(0), Element_t(1));
262 #endif // _NCollection_Vec2_H__