132b6db9be074acb353dd27d552a0b85d87840a9
[occt.git] / src / NCollection / NCollection_Vec2.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _NCollection_Vec2_H__
16 #define _NCollection_Vec2_H__
17
18 #include <cmath> // std::sqrt()
19
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()); }
25
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
30 {
31
32 public:
33
34   //! Returns the number of components.
35   static int Length()
36   {
37     return 2;
38   }
39
40   //! Empty constructor. Construct the zero vector.
41   NCollection_Vec2()
42   {
43     v[0] = v[1] = Element_t(0);
44   }
45
46   //! Initialize ALL components of vector within specified value.
47   explicit NCollection_Vec2 (const Element_t theXY)
48   {
49     v[0] = v[1] = theXY;
50   }
51
52   //! Per-component constructor.
53   explicit NCollection_Vec2 (const Element_t theX,
54                              const Element_t theY)
55   {
56     v[0] = theX;
57     v[1] = theY;
58   }
59
60   //! Alias to 1st component as X coordinate in XY.
61   Element_t x() const { return v[0]; }
62
63   //! Alias to 2nd component as Y coordinate in XY.
64   Element_t y() const { return v[1]; }
65
66   //! @return 2 components by their names in specified order (in GLSL-style)
67   NCOLLECTION_VEC_COMPONENTS_2D(x, y)
68
69   //! Alias to 1st component as X coordinate in XY.
70   Element_t& x() { return v[0]; }
71
72   //! Alias to 2nd component as Y coordinate in XY.
73   Element_t& y() { return v[1]; }
74
75   //! Check this vector with another vector for equality (without tolerance!).
76   bool IsEqual (const NCollection_Vec2& theOther) const
77   {
78     return v[0] == theOther.v[0]
79         && v[1] == theOther.v[1];
80   }
81
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); }
85
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); }
89
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; }
95
96   //! Compute per-component summary.
97   NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
98   {
99     v[0] += theAdd.v[0];
100     v[1] += theAdd.v[1];
101     return *this;
102   }
103
104   //! Compute per-component summary.
105   friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft,
106                                      const NCollection_Vec2& theRight)
107   {
108     return NCollection_Vec2 (theLeft.v[0] + theRight.v[0],
109                              theLeft.v[1] + theRight.v[1]);
110   }
111
112   //! Compute per-component subtraction.
113   NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
114   {
115     v[0] -= theDec.v[0];
116     v[1] -= theDec.v[1];
117     return *this;
118   }
119
120   //! Compute per-component subtraction.
121   friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
122                                      const NCollection_Vec2& theRight)
123   {
124     return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
125                              theLeft.v[1] - theRight.v[1]);
126   }
127
128   //! Unary -.
129   NCollection_Vec2 operator-() const
130   {
131     return NCollection_Vec2 (-x(), -y());
132   }
133
134   //! Compute per-component multiplication.
135   NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
136   {
137     v[0] *= theRight.v[0];
138     v[1] *= theRight.v[1];
139     return *this;
140   }
141
142   //! Compute per-component multiplication.
143   friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
144                                      const NCollection_Vec2& theRight)
145   {
146     return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
147                              theLeft.v[1] * theRight.v[1]);
148   }
149
150   //! Compute per-component multiplication by scale factor.
151   void Multiply (const Element_t theFactor)
152   {
153     v[0] *= theFactor;
154     v[1] *= theFactor;
155   }
156
157   //! Compute per-component multiplication by scale factor.
158   NCollection_Vec2 Multiplied (const Element_t theFactor) const
159   {
160     return NCollection_Vec2 (v[0] * theFactor,
161                              v[1] * theFactor);
162   }
163
164   //! Compute component-wise minimum of two vectors.
165   NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
166   {
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]);
169   }
170
171   //! Compute component-wise maximum of two vectors.
172   NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
173   {
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]);
176   }
177
178   //! Compute component-wise modulus of the vector.
179   NCollection_Vec2 cwiseAbs() const
180   {
181     return NCollection_Vec2 (std::abs (v[0]),
182                              std::abs (v[1]));
183   }
184
185   //! Compute maximum component of the vector.
186   Element_t maxComp() const
187   {
188     return v[0] > v[1] ? v[0] : v[1];
189   }
190
191   //! Compute minimum component of the vector.
192   Element_t minComp() const
193   {
194     return v[0] < v[1] ? v[0] : v[1];
195   }
196
197   //! Compute per-component multiplication by scale factor.
198   NCollection_Vec2& operator*= (const Element_t theFactor)
199   {
200     Multiply (theFactor);
201     return *this;
202   }
203
204   //! Compute per-component division by scale factor.
205   NCollection_Vec2& operator/= (const Element_t theInvFactor)
206   {
207     v[0] /= theInvFactor;
208     v[1] /= theInvFactor;
209     return *this;
210   }
211
212   //! Compute per-component multiplication by scale factor.
213   NCollection_Vec2 operator* (const Element_t theFactor) const
214   {
215     return Multiplied (theFactor);
216   }
217
218   //! Compute per-component division by scale factor.
219   NCollection_Vec2 operator/ (const Element_t theInvFactor) const
220   {
221     return NCollection_Vec2(v[0] / theInvFactor,
222             v[1] / theInvFactor);
223   }
224
225   //! Computes the dot product.
226   Element_t Dot (const NCollection_Vec2& theOther) const
227   {
228     return x() * theOther.x() + y() * theOther.y();
229   }
230
231   //! Computes the vector modulus (magnitude, length).
232   Element_t Modulus() const
233   {
234     return std::sqrt (x() * x() + y() * y());
235   }
236
237   //! Computes the square of vector modulus (magnitude, length).
238   //! This method may be used for performance tricks.
239   Element_t SquareModulus() const
240   {
241     return x() * x() + y() * y();
242   }
243
244   //! Constuct DX unit vector.
245   static NCollection_Vec2 DX()
246   {
247     return NCollection_Vec2 (Element_t(1), Element_t(0));
248   }
249
250   //! Constuct DY unit vector.
251   static NCollection_Vec2 DY()
252   {
253     return NCollection_Vec2 (Element_t(0), Element_t(1));
254   }
255
256 private:
257
258   Element_t v[2];
259
260 };
261
262 #endif // _NCollection_Vec2_H__