0025617: Avoid classes with a copy constructor and the default destructor or assignme...
[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   //! 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; }
80
81   //! Compute per-component summary.
82   NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
83   {
84     v[0] += theAdd.v[0];
85     v[1] += theAdd.v[1];
86     return *this;
87   }
88
89   //! Compute per-component summary.
90   friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft,
91                                      const NCollection_Vec2& theRight)
92   {
93     return NCollection_Vec2 (theLeft.v[0] + theRight.v[0],
94                              theLeft.v[1] + theRight.v[1]);
95   }
96
97   //! Compute per-component subtraction.
98   NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
99   {
100     v[0] -= theDec.v[0];
101     v[1] -= theDec.v[1];
102     return *this;
103   }
104
105   //! Compute per-component subtraction.
106   friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
107                                      const NCollection_Vec2& theRight)
108   {
109     return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
110                              theLeft.v[1] - theRight.v[1]);
111   }
112
113   //! Unary -.
114   NCollection_Vec2 operator-() const
115   {
116     return NCollection_Vec2 (-x(), -y());
117   }
118
119   //! Compute per-component multiplication.
120   NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
121   {
122     v[0] *= theRight.v[0];
123     v[1] *= theRight.v[1];
124     return *this;
125   }
126
127   //! Compute per-component multiplication.
128   friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
129                                      const NCollection_Vec2& theRight)
130   {
131     return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
132                              theLeft.v[1] * theRight.v[1]);
133   }
134
135   //! Compute per-component multiplication by scale factor.
136   void Multiply (const Element_t theFactor)
137   {
138     v[0] *= theFactor;
139     v[1] *= theFactor;
140   }
141
142   //! Compute per-component multiplication by scale factor.
143   NCollection_Vec2 Multiplied (const Element_t theFactor) const
144   {
145     return NCollection_Vec2 (v[0] * theFactor,
146                              v[1] * theFactor);
147   }
148
149   //! Compute component-wise minimum of two vectors.
150   NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
151   {
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]);
154   }
155
156   //! Compute component-wise maximum of two vectors.
157   NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
158   {
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]);
161   }
162
163   //! Compute component-wise modulus of the vector.
164   NCollection_Vec2 cwiseAbs() const
165   {
166     return NCollection_Vec2 (std::abs (v[0]),
167                              std::abs (v[1]));
168   }
169
170   //! Compute maximum component of the vector.
171   Element_t maxComp() const
172   {
173     return v[0] > v[1] ? v[0] : v[1];
174   }
175
176   //! Compute minimum component of the vector.
177   Element_t minComp() const
178   {
179     return v[0] < v[1] ? v[0] : v[1];
180   }
181
182   //! Compute per-component multiplication by scale factor.
183   NCollection_Vec2& operator*= (const Element_t theFactor)
184   {
185     Multiply (theFactor);
186     return *this;
187   }
188
189   //! Compute per-component division by scale factor.
190   NCollection_Vec2& operator/= (const Element_t theInvFactor)
191   {
192     v[0] /= theInvFactor;
193     v[1] /= theInvFactor;
194     return *this;
195   }
196
197   //! Compute per-component multiplication by scale factor.
198   NCollection_Vec2 operator* (const Element_t theFactor) const
199   {
200     return Multiplied (theFactor);
201   }
202
203   //! Compute per-component division by scale factor.
204   NCollection_Vec2 operator/ (const Element_t theInvFactor) const
205   {
206     return NCollection_Vec2(v[0] / theInvFactor,
207             v[1] / theInvFactor);
208   }
209
210   //! Computes the dot product.
211   Element_t Dot (const NCollection_Vec2& theOther) const
212   {
213     return x() * theOther.x() + y() * theOther.y();
214   }
215
216   //! Computes the vector modulus (magnitude, length).
217   Element_t Modulus() const
218   {
219     return std::sqrt (x() * x() + y() * y());
220   }
221
222   //! Computes the square of vector modulus (magnitude, length).
223   //! This method may be used for performance tricks.
224   Element_t SquareModulus() const
225   {
226     return x() * x() + y() * y();
227   }
228
229   //! Constuct DX unit vector.
230   static NCollection_Vec2 DX()
231   {
232     return NCollection_Vec2 (Element_t(1), Element_t(0));
233   }
234
235   //! Constuct DY unit vector.
236   static NCollection_Vec2 DY()
237   {
238     return NCollection_Vec2 (Element_t(0), Element_t(1));
239   }
240
241 private:
242
243   Element_t v[2];
244
245 };
246
247 #endif // _NCollection_Vec2_H__