0024530: TKMesh - remove unused package IntPoly
[occt.git] / src / NCollection / NCollection_Vec2.hxx
CommitLineData
5e27df78 1// Created by: Kirill GAVRILOV
973c2be1 2// Copyright (c) 2014 OPEN CASCADE SAS
5e27df78 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
5e27df78 5//
973c2be1 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.
5e27df78 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
5e27df78 14
15#ifndef _NCollection_Vec2_H__
16#define _NCollection_Vec2_H__
17
bf75be98 18#include <cmath> // std::sqrt()
19
5e27df78 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) \
bf75be98 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()); }
5e27df78 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 //! Copy constructor.
61 NCollection_Vec2 (const NCollection_Vec2& theVec2)
62 {
63 v[0] = theVec2[0];
64 v[1] = theVec2[1];
65 }
66
67 //! Assignment operator.
68 const NCollection_Vec2& operator= (const NCollection_Vec2& theVec2)
69 {
70 v[0] = theVec2[0];
71 v[1] = theVec2[1];
72 return *this;
73 }
74
75 //! Alias to 1st component as X coordinate in XY.
76 Element_t x() const { return v[0]; }
77
78 //! Alias to 2nd component as Y coordinate in XY.
79 Element_t y() const { return v[1]; }
80
81 //! @return 2 components by their names in specified order (in GLSL-style)
5640d653 82 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
5e27df78 83
84 //! Alias to 1st component as X coordinate in XY.
85 Element_t& x() { return v[0]; }
86
87 //! Alias to 2nd component as Y coordinate in XY.
88 Element_t& y() { return v[1]; }
89
938d4544 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; }
5e27df78 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
3c4e78f2 164 //! Compute component-wise minimum of two vectors.
165 NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
166 {
200ed755 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]);
3c4e78f2 169 }
170
171 //! Compute component-wise maximum of two vectors.
172 NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
173 {
200ed755 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]);
3c4e78f2 176 }
177
5e27df78 178 //! Compute per-component multiplication by scale factor.
179 NCollection_Vec2& operator*= (const Element_t theFactor)
180 {
181 Multiply (theFactor);
182 return *this;
183 }
184
185 //! Compute per-component division by scale factor.
186 NCollection_Vec2& operator/= (const Element_t theInvFactor)
187 {
188 v[0] /= theInvFactor;
189 v[1] /= theInvFactor;
190 return *this;
191 }
192
193 //! Compute per-component multiplication by scale factor.
194 NCollection_Vec2 operator* (const Element_t theFactor) const
195 {
196 return Multiplied (theFactor);
197 }
198
199 //! Compute per-component division by scale factor.
200 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
201 {
202 return NCollection_Vec2(v[0] / theInvFactor,
203 v[1] / theInvFactor);
204 }
205
206 //! Computes the dot product.
207 Element_t Dot (const NCollection_Vec2& theOther) const
208 {
209 return x() * theOther.x() + y() * theOther.y();
210 }
211
212 //! Computes the vector modulus (magnitude, length).
213 Element_t Modulus() const
214 {
215 return std::sqrt (x() * x() + y() * y());
216 }
217
218 //! Computes the square of vector modulus (magnitude, length).
219 //! This method may be used for performance tricks.
220 Element_t SquareModulus() const
221 {
222 return x() * x() + y() * y();
223 }
224
225 //! Constuct DX unit vector.
226 static NCollection_Vec2 DX()
227 {
228 return NCollection_Vec2 (Element_t(1), Element_t(0));
229 }
230
231 //! Constuct DY unit vector.
232 static NCollection_Vec2 DY()
233 {
234 return NCollection_Vec2 (Element_t(0), Element_t(1));
235 }
236
237private:
238
239 Element_t v[2];
240
241};
242
243#endif // _NCollection_Vec2_H__