5e27df78 |
1 | // Created by: Kirill GAVRILOV |
2 | // Copyright (c) 2012 OPEN CASCADE SAS |
3 | // |
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. |
8 | // |
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. |
11 | // |
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. |
18 | |
19 | #ifndef _NCollection_Vec2_H__ |
20 | #define _NCollection_Vec2_H__ |
21 | |
bf75be98 |
22 | #include <cmath> // std::sqrt() |
23 | |
5e27df78 |
24 | //! Auxiliary macros to define couple of similar access components as vector methods. |
25 | //! @return 2 components by their names in specified order |
26 | #define NCOLLECTION_VEC_COMPONENTS_2D(theX, theY) \ |
bf75be98 |
27 | const NCollection_Vec2<Element_t> theX##theY() const { return NCollection_Vec2<Element_t>(theX(), theY()); } \ |
28 | const NCollection_Vec2<Element_t> theY##theX() const { return NCollection_Vec2<Element_t>(theY(), theX()); } |
5e27df78 |
29 | |
30 | //! Defines the 2D-vector template. |
31 | //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.). |
32 | template<typename Element_t> |
33 | class NCollection_Vec2 |
34 | { |
35 | |
36 | public: |
37 | |
38 | //! Returns the number of components. |
39 | static int Length() |
40 | { |
41 | return 2; |
42 | } |
43 | |
44 | //! Empty constructor. Construct the zero vector. |
45 | NCollection_Vec2() |
46 | { |
47 | v[0] = v[1] = Element_t(0); |
48 | } |
49 | |
50 | //! Initialize ALL components of vector within specified value. |
51 | explicit NCollection_Vec2 (const Element_t theXY) |
52 | { |
53 | v[0] = v[1] = theXY; |
54 | } |
55 | |
56 | //! Per-component constructor. |
57 | explicit NCollection_Vec2 (const Element_t theX, |
58 | const Element_t theY) |
59 | { |
60 | v[0] = theX; |
61 | v[1] = theY; |
62 | } |
63 | |
64 | //! Copy constructor. |
65 | NCollection_Vec2 (const NCollection_Vec2& theVec2) |
66 | { |
67 | v[0] = theVec2[0]; |
68 | v[1] = theVec2[1]; |
69 | } |
70 | |
71 | //! Assignment operator. |
72 | const NCollection_Vec2& operator= (const NCollection_Vec2& theVec2) |
73 | { |
74 | v[0] = theVec2[0]; |
75 | v[1] = theVec2[1]; |
76 | return *this; |
77 | } |
78 | |
79 | //! Alias to 1st component as X coordinate in XY. |
80 | Element_t x() const { return v[0]; } |
81 | |
82 | //! Alias to 2nd component as Y coordinate in XY. |
83 | Element_t y() const { return v[1]; } |
84 | |
85 | //! @return 2 components by their names in specified order (in GLSL-style) |
86 | NCOLLECTION_VEC_COMPONENTS_2D(x, y); |
87 | |
88 | //! Alias to 1st component as X coordinate in XY. |
89 | Element_t& x() { return v[0]; } |
90 | |
91 | //! Alias to 2nd component as Y coordinate in XY. |
92 | Element_t& y() { return v[1]; } |
93 | |
94 | //! Raw access to the data (to simplify OpenGL exchange). |
95 | const Element_t* GetData() const { return v; } |
96 | operator const Element_t*() const { return v; } |
97 | operator Element_t*() { return v; } |
98 | |
99 | //! Compute per-component summary. |
100 | NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd) |
101 | { |
102 | v[0] += theAdd.v[0]; |
103 | v[1] += theAdd.v[1]; |
104 | return *this; |
105 | } |
106 | |
107 | //! Compute per-component summary. |
108 | friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft, |
109 | const NCollection_Vec2& theRight) |
110 | { |
111 | return NCollection_Vec2 (theLeft.v[0] + theRight.v[0], |
112 | theLeft.v[1] + theRight.v[1]); |
113 | } |
114 | |
115 | //! Compute per-component subtraction. |
116 | NCollection_Vec2& operator-= (const NCollection_Vec2& theDec) |
117 | { |
118 | v[0] -= theDec.v[0]; |
119 | v[1] -= theDec.v[1]; |
120 | return *this; |
121 | } |
122 | |
123 | //! Compute per-component subtraction. |
124 | friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft, |
125 | const NCollection_Vec2& theRight) |
126 | { |
127 | return NCollection_Vec2 (theLeft.v[0] - theRight.v[0], |
128 | theLeft.v[1] - theRight.v[1]); |
129 | } |
130 | |
131 | //! Unary -. |
132 | NCollection_Vec2 operator-() const |
133 | { |
134 | return NCollection_Vec2 (-x(), -y()); |
135 | } |
136 | |
137 | //! Compute per-component multiplication. |
138 | NCollection_Vec2& operator*= (const NCollection_Vec2& theRight) |
139 | { |
140 | v[0] *= theRight.v[0]; |
141 | v[1] *= theRight.v[1]; |
142 | return *this; |
143 | } |
144 | |
145 | //! Compute per-component multiplication. |
146 | friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft, |
147 | const NCollection_Vec2& theRight) |
148 | { |
149 | return NCollection_Vec2 (theLeft.v[0] * theRight.v[0], |
150 | theLeft.v[1] * theRight.v[1]); |
151 | } |
152 | |
153 | //! Compute per-component multiplication by scale factor. |
154 | void Multiply (const Element_t theFactor) |
155 | { |
156 | v[0] *= theFactor; |
157 | v[1] *= theFactor; |
158 | } |
159 | |
160 | //! Compute per-component multiplication by scale factor. |
161 | NCollection_Vec2 Multiplied (const Element_t theFactor) const |
162 | { |
163 | return NCollection_Vec2 (v[0] * theFactor, |
164 | v[1] * theFactor); |
165 | } |
166 | |
167 | //! Compute per-component multiplication by scale factor. |
168 | NCollection_Vec2& operator*= (const Element_t theFactor) |
169 | { |
170 | Multiply (theFactor); |
171 | return *this; |
172 | } |
173 | |
174 | //! Compute per-component division by scale factor. |
175 | NCollection_Vec2& operator/= (const Element_t theInvFactor) |
176 | { |
177 | v[0] /= theInvFactor; |
178 | v[1] /= theInvFactor; |
179 | return *this; |
180 | } |
181 | |
182 | //! Compute per-component multiplication by scale factor. |
183 | NCollection_Vec2 operator* (const Element_t theFactor) const |
184 | { |
185 | return Multiplied (theFactor); |
186 | } |
187 | |
188 | //! Compute per-component division by scale factor. |
189 | NCollection_Vec2 operator/ (const Element_t theInvFactor) const |
190 | { |
191 | return NCollection_Vec2(v[0] / theInvFactor, |
192 | v[1] / theInvFactor); |
193 | } |
194 | |
195 | //! Computes the dot product. |
196 | Element_t Dot (const NCollection_Vec2& theOther) const |
197 | { |
198 | return x() * theOther.x() + y() * theOther.y(); |
199 | } |
200 | |
201 | //! Computes the vector modulus (magnitude, length). |
202 | Element_t Modulus() const |
203 | { |
204 | return std::sqrt (x() * x() + y() * y()); |
205 | } |
206 | |
207 | //! Computes the square of vector modulus (magnitude, length). |
208 | //! This method may be used for performance tricks. |
209 | Element_t SquareModulus() const |
210 | { |
211 | return x() * x() + y() * y(); |
212 | } |
213 | |
214 | //! Constuct DX unit vector. |
215 | static NCollection_Vec2 DX() |
216 | { |
217 | return NCollection_Vec2 (Element_t(1), Element_t(0)); |
218 | } |
219 | |
220 | //! Constuct DY unit vector. |
221 | static NCollection_Vec2 DY() |
222 | { |
223 | return NCollection_Vec2 (Element_t(0), Element_t(1)); |
224 | } |
225 | |
226 | private: |
227 | |
228 | Element_t v[2]; |
229 | |
230 | }; |
231 | |
232 | #endif // _NCollection_Vec2_H__ |