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_Vec2_H__
20 #define _NCollection_Vec2_H__
22 //! Auxiliary macros to define couple of similar access components as vector methods.
23 //! @return 2 components by their names in specified order
24 #define NCOLLECTION_VEC_COMPONENTS_2D(theX, theY) \
25 const NCollection_Vec2<Element_t> theX##theY##() const { return NCollection_Vec2<Element_t>(theX##(), theY##()); } \
26 const NCollection_Vec2<Element_t> theY##theX##() const { return NCollection_Vec2<Element_t>(theY##(), theX##()); }
28 //! Defines the 2D-vector template.
29 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
30 template<typename Element_t>
31 class NCollection_Vec2
36 //! Returns the number of components.
42 //! Empty constructor. Construct the zero vector.
45 v[0] = v[1] = Element_t(0);
48 //! Initialize ALL components of vector within specified value.
49 explicit NCollection_Vec2 (const Element_t theXY)
54 //! Per-component constructor.
55 explicit NCollection_Vec2 (const Element_t theX,
63 NCollection_Vec2 (const NCollection_Vec2& theVec2)
69 //! Assignment operator.
70 const NCollection_Vec2& operator= (const NCollection_Vec2& theVec2)
77 //! Alias to 1st component as X coordinate in XY.
78 Element_t x() const { return v[0]; }
80 //! Alias to 2nd component as Y coordinate in XY.
81 Element_t y() const { return v[1]; }
83 //! @return 2 components by their names in specified order (in GLSL-style)
84 NCOLLECTION_VEC_COMPONENTS_2D(x, y);
86 //! Alias to 1st component as X coordinate in XY.
87 Element_t& x() { return v[0]; }
89 //! Alias to 2nd component as Y coordinate in XY.
90 Element_t& y() { return v[1]; }
92 //! Raw access to the data (to simplify OpenGL exchange).
93 const Element_t* GetData() const { return v; }
94 operator const Element_t*() const { return v; }
95 operator Element_t*() { return v; }
97 //! Compute per-component summary.
98 NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
105 //! Compute per-component summary.
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]);
113 //! Compute per-component subtraction.
114 NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
121 //! Compute per-component subtraction.
122 friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
123 const NCollection_Vec2& theRight)
125 return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
126 theLeft.v[1] - theRight.v[1]);
130 NCollection_Vec2 operator-() const
132 return NCollection_Vec2 (-x(), -y());
135 //! Compute per-component multiplication.
136 NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
138 v[0] *= theRight.v[0];
139 v[1] *= theRight.v[1];
143 //! Compute per-component multiplication.
144 friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
145 const NCollection_Vec2& theRight)
147 return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
148 theLeft.v[1] * theRight.v[1]);
151 //! Compute per-component multiplication by scale factor.
152 void Multiply (const Element_t theFactor)
158 //! Compute per-component multiplication by scale factor.
159 NCollection_Vec2 Multiplied (const Element_t theFactor) const
161 return NCollection_Vec2 (v[0] * theFactor,
165 //! Compute per-component multiplication by scale factor.
166 NCollection_Vec2& operator*= (const Element_t theFactor)
168 Multiply (theFactor);
172 //! Compute per-component division by scale factor.
173 NCollection_Vec2& operator/= (const Element_t theInvFactor)
175 v[0] /= theInvFactor;
176 v[1] /= theInvFactor;
180 //! Compute per-component multiplication by scale factor.
181 NCollection_Vec2 operator* (const Element_t theFactor) const
183 return Multiplied (theFactor);
186 //! Compute per-component division by scale factor.
187 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
189 return NCollection_Vec2(v[0] / theInvFactor,
190 v[1] / theInvFactor);
193 //! Computes the dot product.
194 Element_t Dot (const NCollection_Vec2& theOther) const
196 return x() * theOther.x() + y() * theOther.y();
199 //! Computes the vector modulus (magnitude, length).
200 Element_t Modulus() const
202 return std::sqrt (x() * x() + y() * y());
205 //! Computes the square of vector modulus (magnitude, length).
206 //! This method may be used for performance tricks.
207 Element_t SquareModulus() const
209 return x() * x() + y() * y();
212 //! Constuct DX unit vector.
213 static NCollection_Vec2 DX()
215 return NCollection_Vec2 (Element_t(1), Element_t(0));
218 //! Constuct DY unit vector.
219 static NCollection_Vec2 DY()
221 return NCollection_Vec2 (Element_t(0), Element_t(1));
230 #endif // _NCollection_Vec2_H__