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