0030131: Foundation Classes - support of Linear builder for 2D BVH trees
[occt.git] / src / NCollection / NCollection_Vec2.hxx
... / ...
CommitLineData
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.).
28template<typename Element_t>
29class NCollection_Vec2
30{
31
32public:
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 //! Assign new values to the vector.
61 void SetValues (const Element_t theX,
62 const Element_t theY)
63 {
64 v[0] = theX;
65 v[1] = theY;
66 }
67
68 //! Alias to 1st component as X coordinate in XY.
69 Element_t x() const { return v[0]; }
70
71 //! Alias to 2nd component as Y coordinate in XY.
72 Element_t y() const { return v[1]; }
73
74 //! @return 2 components by their names in specified order (in GLSL-style)
75 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
76
77 //! Alias to 1st component as X coordinate in XY.
78 Element_t& x() { return v[0]; }
79
80 //! Alias to 2nd component as Y coordinate in XY.
81 Element_t& y() { return v[1]; }
82
83 //! Check this vector with another vector for equality (without tolerance!).
84 bool IsEqual (const NCollection_Vec2& theOther) const
85 {
86 return v[0] == theOther.v[0]
87 && v[1] == theOther.v[1];
88 }
89
90 //! Check this vector with another vector for equality (without tolerance!).
91 bool operator== (const NCollection_Vec2& theOther) { return IsEqual (theOther); }
92 bool operator== (const NCollection_Vec2& theOther) const { return IsEqual (theOther); }
93
94 //! Check this vector with another vector for non-equality (without tolerance!).
95 bool operator!= (const NCollection_Vec2& theOther) { return !IsEqual (theOther); }
96 bool operator!= (const NCollection_Vec2& theOther) const { return !IsEqual (theOther); }
97
98 //! Raw access to the data (for OpenGL exchange).
99 const Element_t* GetData() const { return v; }
100 Element_t* ChangeData() { return v; }
101 operator const Element_t*() const { return v; }
102 operator Element_t*() { return v; }
103
104 //! Compute per-component summary.
105 NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
106 {
107 v[0] += theAdd.v[0];
108 v[1] += theAdd.v[1];
109 return *this;
110 }
111
112 //! Compute per-component summary.
113 friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft,
114 const NCollection_Vec2& theRight)
115 {
116 return NCollection_Vec2 (theLeft.v[0] + theRight.v[0],
117 theLeft.v[1] + theRight.v[1]);
118 }
119
120 //! Compute per-component subtraction.
121 NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
122 {
123 v[0] -= theDec.v[0];
124 v[1] -= theDec.v[1];
125 return *this;
126 }
127
128 //! Compute per-component subtraction.
129 friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
130 const NCollection_Vec2& theRight)
131 {
132 return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
133 theLeft.v[1] - theRight.v[1]);
134 }
135
136 //! Unary -.
137 NCollection_Vec2 operator-() const
138 {
139 return NCollection_Vec2 (-x(), -y());
140 }
141
142 //! Compute per-component multiplication.
143 NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
144 {
145 v[0] *= theRight.v[0];
146 v[1] *= theRight.v[1];
147 return *this;
148 }
149
150 //! Compute per-component multiplication.
151 friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
152 const NCollection_Vec2& theRight)
153 {
154 return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
155 theLeft.v[1] * theRight.v[1]);
156 }
157
158 //! Compute per-component multiplication by scale factor.
159 void Multiply (const Element_t theFactor)
160 {
161 v[0] *= theFactor;
162 v[1] *= theFactor;
163 }
164
165 //! Compute per-component multiplication by scale factor.
166 NCollection_Vec2 Multiplied (const Element_t theFactor) const
167 {
168 return NCollection_Vec2 (v[0] * theFactor,
169 v[1] * theFactor);
170 }
171
172 //! Compute component-wise minimum of two vectors.
173 NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
174 {
175 return NCollection_Vec2 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
176 v[1] < theVec.v[1] ? v[1] : theVec.v[1]);
177 }
178
179 //! Compute component-wise maximum of two vectors.
180 NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
181 {
182 return NCollection_Vec2 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
183 v[1] > theVec.v[1] ? v[1] : theVec.v[1]);
184 }
185
186 //! Compute component-wise modulus of the vector.
187 NCollection_Vec2 cwiseAbs() const
188 {
189 return NCollection_Vec2 (std::abs (v[0]),
190 std::abs (v[1]));
191 }
192
193 //! Compute maximum component of the vector.
194 Element_t maxComp() const
195 {
196 return v[0] > v[1] ? v[0] : v[1];
197 }
198
199 //! Compute minimum component of the vector.
200 Element_t minComp() const
201 {
202 return v[0] < v[1] ? v[0] : v[1];
203 }
204
205 //! Compute per-component multiplication by scale factor.
206 NCollection_Vec2& operator*= (const Element_t theFactor)
207 {
208 Multiply (theFactor);
209 return *this;
210 }
211
212 //! Compute per-component division by scale factor.
213 NCollection_Vec2& operator/= (const Element_t theInvFactor)
214 {
215 v[0] /= theInvFactor;
216 v[1] /= theInvFactor;
217 return *this;
218 }
219
220 //! Compute per-component division.
221 NCollection_Vec2& operator/= (const NCollection_Vec2& theRight)
222 {
223 v[0] /= theRight.v[0];
224 v[1] /= theRight.v[1];
225 return *this;
226 }
227
228 //! Compute per-component multiplication by scale factor.
229 NCollection_Vec2 operator* (const Element_t theFactor) const
230 {
231 return Multiplied (theFactor);
232 }
233
234 //! Compute per-component division by scale factor.
235 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
236 {
237 return NCollection_Vec2(v[0] / theInvFactor,
238 v[1] / theInvFactor);
239 }
240
241 //! Compute per-component division.
242 friend NCollection_Vec2 operator/ (const NCollection_Vec2& theLeft,
243 const NCollection_Vec2& theRight)
244 {
245 return NCollection_Vec2 (theLeft.v[0] / theRight.v[0],
246 theLeft.v[1] / theRight.v[1]);
247 }
248
249 //! Computes the dot product.
250 Element_t Dot (const NCollection_Vec2& theOther) const
251 {
252 return x() * theOther.x() + y() * theOther.y();
253 }
254
255 //! Computes the vector modulus (magnitude, length).
256 Element_t Modulus() const
257 {
258 return std::sqrt (x() * x() + y() * y());
259 }
260
261 //! Computes the square of vector modulus (magnitude, length).
262 //! This method may be used for performance tricks.
263 Element_t SquareModulus() const
264 {
265 return x() * x() + y() * y();
266 }
267
268 //! Constuct DX unit vector.
269 static NCollection_Vec2 DX()
270 {
271 return NCollection_Vec2 (Element_t(1), Element_t(0));
272 }
273
274 //! Constuct DY unit vector.
275 static NCollection_Vec2 DY()
276 {
277 return NCollection_Vec2 (Element_t(0), Element_t(1));
278 }
279
280private:
281
282 Element_t v[2];
283
284};
285
286#endif // _NCollection_Vec2_H__