1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 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
7 // under the terms of the GNU Lesser General Public 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,
61 NCollection_Vec2 (const NCollection_Vec2& theVec2)
67 //! Assignment operator.
68 const NCollection_Vec2& operator= (const NCollection_Vec2& theVec2)
75 //! Alias to 1st component as X coordinate in XY.
76 Element_t x() const { return v[0]; }
78 //! Alias to 2nd component as Y coordinate in XY.
79 Element_t y() const { return v[1]; }
81 //! @return 2 components by their names in specified order (in GLSL-style)
82 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
84 //! Alias to 1st component as X coordinate in XY.
85 Element_t& x() { return v[0]; }
87 //! Alias to 2nd component as Y coordinate in XY.
88 Element_t& y() { return v[1]; }
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 per-component multiplication by scale factor.
165 NCollection_Vec2& operator*= (const Element_t theFactor)
167 Multiply (theFactor);
171 //! Compute per-component division by scale factor.
172 NCollection_Vec2& operator/= (const Element_t theInvFactor)
174 v[0] /= theInvFactor;
175 v[1] /= theInvFactor;
179 //! Compute per-component multiplication by scale factor.
180 NCollection_Vec2 operator* (const Element_t theFactor) const
182 return Multiplied (theFactor);
185 //! Compute per-component division by scale factor.
186 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
188 return NCollection_Vec2(v[0] / theInvFactor,
189 v[1] / theInvFactor);
192 //! Computes the dot product.
193 Element_t Dot (const NCollection_Vec2& theOther) const
195 return x() * theOther.x() + y() * theOther.y();
198 //! Computes the vector modulus (magnitude, length).
199 Element_t Modulus() const
201 return std::sqrt (x() * x() + y() * y());
204 //! Computes the square of vector modulus (magnitude, length).
205 //! This method may be used for performance tricks.
206 Element_t SquareModulus() const
208 return x() * x() + y() * y();
211 //! Constuct DX unit vector.
212 static NCollection_Vec2 DX()
214 return NCollection_Vec2 (Element_t(1), Element_t(0));
217 //! Constuct DY unit vector.
218 static NCollection_Vec2 DY()
220 return NCollection_Vec2 (Element_t(0), Element_t(1));
229 #endif // _NCollection_Vec2_H__