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 //! Raw access to the data (for OpenGL exchange).
76 const Element_t* GetData() const { return v; }
77 Element_t* ChangeData() { return v; }
78 operator const Element_t*() const { return v; }
79 operator Element_t*() { return v; }
81 //! Compute per-component summary.
82 NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
89 //! Compute per-component summary.
90 friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft,
91 const NCollection_Vec2& theRight)
93 return NCollection_Vec2 (theLeft.v[0] + theRight.v[0],
94 theLeft.v[1] + theRight.v[1]);
97 //! Compute per-component subtraction.
98 NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
105 //! Compute per-component subtraction.
106 friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
107 const NCollection_Vec2& theRight)
109 return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
110 theLeft.v[1] - theRight.v[1]);
114 NCollection_Vec2 operator-() const
116 return NCollection_Vec2 (-x(), -y());
119 //! Compute per-component multiplication.
120 NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
122 v[0] *= theRight.v[0];
123 v[1] *= theRight.v[1];
127 //! Compute per-component multiplication.
128 friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
129 const NCollection_Vec2& theRight)
131 return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
132 theLeft.v[1] * theRight.v[1]);
135 //! Compute per-component multiplication by scale factor.
136 void Multiply (const Element_t theFactor)
142 //! Compute per-component multiplication by scale factor.
143 NCollection_Vec2 Multiplied (const Element_t theFactor) const
145 return NCollection_Vec2 (v[0] * theFactor,
149 //! Compute component-wise minimum of two vectors.
150 NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
152 return NCollection_Vec2 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
153 v[1] < theVec.v[1] ? v[1] : theVec.v[1]);
156 //! Compute component-wise maximum of two vectors.
157 NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
159 return NCollection_Vec2 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
160 v[1] > theVec.v[1] ? v[1] : theVec.v[1]);
163 //! Compute component-wise modulus of the vector.
164 NCollection_Vec2 cwiseAbs() const
166 return NCollection_Vec2 (std::abs (v[0]),
170 //! Compute maximum component of the vector.
171 Element_t maxComp() const
173 return v[0] > v[1] ? v[0] : v[1];
176 //! Compute minimum component of the vector.
177 Element_t minComp() const
179 return v[0] < v[1] ? v[0] : v[1];
182 //! Compute per-component multiplication by scale factor.
183 NCollection_Vec2& operator*= (const Element_t theFactor)
185 Multiply (theFactor);
189 //! Compute per-component division by scale factor.
190 NCollection_Vec2& operator/= (const Element_t theInvFactor)
192 v[0] /= theInvFactor;
193 v[1] /= theInvFactor;
197 //! Compute per-component multiplication by scale factor.
198 NCollection_Vec2 operator* (const Element_t theFactor) const
200 return Multiplied (theFactor);
203 //! Compute per-component division by scale factor.
204 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
206 return NCollection_Vec2(v[0] / theInvFactor,
207 v[1] / theInvFactor);
210 //! Computes the dot product.
211 Element_t Dot (const NCollection_Vec2& theOther) const
213 return x() * theOther.x() + y() * theOther.y();
216 //! Computes the vector modulus (magnitude, length).
217 Element_t Modulus() const
219 return std::sqrt (x() * x() + y() * y());
222 //! Computes the square of vector modulus (magnitude, length).
223 //! This method may be used for performance tricks.
224 Element_t SquareModulus() const
226 return x() * x() + y() * y();
229 //! Constuct DX unit vector.
230 static NCollection_Vec2 DX()
232 return NCollection_Vec2 (Element_t(1), Element_t(0));
235 //! Constuct DY unit vector.
236 static NCollection_Vec2 DY()
238 return NCollection_Vec2 (Element_t(0), Element_t(1));
247 #endif // _NCollection_Vec2_H__