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 //! Assign new values to the vector.
61 void SetValues (const Element_t theX,
68 //! Alias to 1st component as X coordinate in XY.
69 Element_t x() const { return v[0]; }
71 //! Alias to 2nd component as Y coordinate in XY.
72 Element_t y() const { return v[1]; }
74 //! @return 2 components by their names in specified order (in GLSL-style)
75 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
77 //! Alias to 1st component as X coordinate in XY.
78 Element_t& x() { return v[0]; }
80 //! Alias to 2nd component as Y coordinate in XY.
81 Element_t& y() { return v[1]; }
83 //! Check this vector with another vector for equality (without tolerance!).
84 bool IsEqual (const NCollection_Vec2& theOther) const
86 return v[0] == theOther.v[0]
87 && v[1] == theOther.v[1];
90 //! Check this vector with another vector for equality (without tolerance!).
91 bool operator== (const NCollection_Vec2& theOther) { return IsEqual (theOther); }
92 bool operator== (const NCollection_Vec2& theOther) const { return IsEqual (theOther); }
94 //! Check this vector with another vector for non-equality (without tolerance!).
95 bool operator!= (const NCollection_Vec2& theOther) { return !IsEqual (theOther); }
96 bool operator!= (const NCollection_Vec2& theOther) const { return !IsEqual (theOther); }
98 //! Raw access to the data (for OpenGL exchange).
99 const Element_t* GetData() const { return v; }
100 Element_t* ChangeData() { return v; }
101 operator const Element_t*() const { return v; }
102 operator Element_t*() { return v; }
104 //! Compute per-component summary.
105 NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
112 //! Compute per-component summary.
113 friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft,
114 const NCollection_Vec2& theRight)
116 return NCollection_Vec2 (theLeft.v[0] + theRight.v[0],
117 theLeft.v[1] + theRight.v[1]);
120 //! Compute per-component subtraction.
121 NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
128 //! Compute per-component subtraction.
129 friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
130 const NCollection_Vec2& theRight)
132 return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
133 theLeft.v[1] - theRight.v[1]);
137 NCollection_Vec2 operator-() const
139 return NCollection_Vec2 (-x(), -y());
142 //! Compute per-component multiplication.
143 NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
145 v[0] *= theRight.v[0];
146 v[1] *= theRight.v[1];
150 //! Compute per-component multiplication.
151 friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
152 const NCollection_Vec2& theRight)
154 return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
155 theLeft.v[1] * theRight.v[1]);
158 //! Compute per-component multiplication by scale factor.
159 void Multiply (const Element_t theFactor)
165 //! Compute per-component multiplication by scale factor.
166 NCollection_Vec2 Multiplied (const Element_t theFactor) const
168 return NCollection_Vec2 (v[0] * theFactor,
172 //! Compute component-wise minimum of two vectors.
173 NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
175 return NCollection_Vec2 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
176 v[1] < theVec.v[1] ? v[1] : theVec.v[1]);
179 //! Compute component-wise maximum of two vectors.
180 NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
182 return NCollection_Vec2 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
183 v[1] > theVec.v[1] ? v[1] : theVec.v[1]);
186 //! Compute component-wise modulus of the vector.
187 NCollection_Vec2 cwiseAbs() const
189 return NCollection_Vec2 (std::abs (v[0]),
193 //! Compute maximum component of the vector.
194 Element_t maxComp() const
196 return v[0] > v[1] ? v[0] : v[1];
199 //! Compute minimum component of the vector.
200 Element_t minComp() const
202 return v[0] < v[1] ? v[0] : v[1];
205 //! Compute per-component multiplication by scale factor.
206 NCollection_Vec2& operator*= (const Element_t theFactor)
208 Multiply (theFactor);
212 //! Compute per-component division by scale factor.
213 NCollection_Vec2& operator/= (const Element_t theInvFactor)
215 v[0] /= theInvFactor;
216 v[1] /= theInvFactor;
220 //! Compute per-component multiplication by scale factor.
221 NCollection_Vec2 operator* (const Element_t theFactor) const
223 return Multiplied (theFactor);
226 //! Compute per-component division by scale factor.
227 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
229 return NCollection_Vec2(v[0] / theInvFactor,
230 v[1] / theInvFactor);
233 //! Computes the dot product.
234 Element_t Dot (const NCollection_Vec2& theOther) const
236 return x() * theOther.x() + y() * theOther.y();
239 //! Computes the vector modulus (magnitude, length).
240 Element_t Modulus() const
242 return std::sqrt (x() * x() + y() * y());
245 //! Computes the square of vector modulus (magnitude, length).
246 //! This method may be used for performance tricks.
247 Element_t SquareModulus() const
249 return x() * x() + y() * y();
252 //! Constuct DX unit vector.
253 static NCollection_Vec2 DX()
255 return NCollection_Vec2 (Element_t(1), Element_t(0));
258 //! Constuct DY unit vector.
259 static NCollection_Vec2 DY()
261 return NCollection_Vec2 (Element_t(0), Element_t(1));
270 #endif // _NCollection_Vec2_H__