0023544: Texture management in TKOpenGl should be redesigned
[occt.git] / src / NCollection / NCollection_Vec3.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_Vec3_H__
20#define _NCollection_Vec3_H__
21
22#include <cstring>
23#include <cmath>
24#include <NCollection_Vec2.hxx>
25
26//! Auxiliary macros to define couple of similar access components as vector methods
27#define NCOLLECTION_VEC_COMPONENTS_3D(theX, theY, theZ) \
bf75be98 28 const NCollection_Vec3<Element_t> theX##theY##theZ() const { return NCollection_Vec3<Element_t>(theX(), theY(), theZ()); } \
29 const NCollection_Vec3<Element_t> theX##theZ##theY() const { return NCollection_Vec3<Element_t>(theX(), theZ(), theY()); } \
30 const NCollection_Vec3<Element_t> theY##theX##theZ() const { return NCollection_Vec3<Element_t>(theY(), theX(), theZ()); } \
31 const NCollection_Vec3<Element_t> theY##theZ##theX() const { return NCollection_Vec3<Element_t>(theY(), theZ(), theX()); } \
32 const NCollection_Vec3<Element_t> theZ##theY##theX() const { return NCollection_Vec3<Element_t>(theZ(), theY(), theX()); } \
33 const NCollection_Vec3<Element_t> theZ##theX##theY() const { return NCollection_Vec3<Element_t>(theZ(), theX(), theY()); }
5e27df78 34
35//! Generic 3-components vector.
36//! To be used as RGB color pixel or XYZ 3D-point.
37//! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
38template<typename Element_t>
39class NCollection_Vec3
40{
41
42public:
43
44 //! Returns the number of components.
45 static int Length()
46 {
47 return 3;
48 }
49
50 //! Empty constructor. Construct the zero vector.
51 NCollection_Vec3()
52 {
53 std::memset (this, 0, sizeof(NCollection_Vec3));
54 }
55
56 //! Initialize ALL components of vector within specified value.
57 explicit NCollection_Vec3 (Element_t theValue)
58 {
59 v[0] = v[1] = v[2] = theValue;
60 }
61
62 //! Per-component constructor.
63 explicit NCollection_Vec3 (const Element_t theX,
64 const Element_t theY,
65 const Element_t theZ)
66 {
67 v[0] = theX;
68 v[1] = theY;
69 v[2] = theZ;
70 }
71
72 //! Constructor from 2-components vector.
73 explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2)
74 {
75 v[0] = theVec2[0];
76 v[1] = theVec2[1];
77 v[2] = Element_t(0);
78 }
79
80 //! Copy constructor.
81 NCollection_Vec3 (const NCollection_Vec3& theVec3)
82 {
83 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3));
84 }
85
86 //! Assignment operator.
87 const NCollection_Vec3& operator= (const NCollection_Vec3& theVec3)
88 {
89 std::memcpy (this, &theVec3, sizeof(NCollection_Vec3));
90 return *this;
91 }
92
93 //! Alias to 1st component as X coordinate in XYZ.
94 Element_t x() const { return v[0]; }
95
96 //! Alias to 1st component as RED channel in RGB.
97 Element_t r() const { return v[0]; }
98
99 //! Alias to 2nd component as Y coordinate in XYZ.
100 Element_t y() const { return v[1]; }
101
102 //! Alias to 2nd component as GREEN channel in RGB.
103 Element_t g() const { return v[1]; }
104
105 //! Alias to 3rd component as Z coordinate in XYZ.
106 Element_t z() const { return v[2]; }
107
108 //! Alias to 3rd component as BLUE channel in RGB.
109 Element_t b() const { return v[2]; }
110
111 //! @return 2 components by their names in specified order (in GLSL-style)
112 NCOLLECTION_VEC_COMPONENTS_2D(x, y);
113 NCOLLECTION_VEC_COMPONENTS_2D(x, z);
114 NCOLLECTION_VEC_COMPONENTS_2D(y, z);
115
116 //! @return 3 components by their names in specified order (in GLSL-style)
117 NCOLLECTION_VEC_COMPONENTS_3D(x, y, z);
118
119 //! Alias to 1st component as X coordinate in XYZ.
120 Element_t& x() { return v[0]; }
121
122 //! Alias to 1st component as RED channel in RGB.
123 Element_t& r() { return v[0]; }
124
125 //! Alias to 2nd component as Y coordinate in XYZ.
126 Element_t& y() { return v[1]; }
127
128 //! Alias to 2nd component as GREEN channel in RGB.
129 Element_t& g() { return v[1]; }
130
131 //! Alias to 3rd component as Z coordinate in XYZ.
132 Element_t& z() { return v[2]; }
133
134 //! Alias to 3rd component as BLUE channel in RGB.
135 Element_t& b() { return v[2]; }
136
137 //! @return XY-components modifiable vector
138 NCollection_Vec2<Element_t>& xy()
139 {
140 return *((NCollection_Vec2<Element_t>* )&v[0]);
141 }
142
143 //! @return YZ-components modifiable vector
144 NCollection_Vec2<Element_t>& yz()
145 {
146 return *((NCollection_Vec2<Element_t>* )&v[1]);
147 }
148
149 //! Raw access to the data (for OpenGL exchange).
150 const Element_t* GetData() const { return v; }
151 operator const Element_t*() const { return v; }
152 operator Element_t*() { return v; }
153
154 //! Compute per-component summary.
155 NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
156 {
157 v[0] += theAdd.v[0];
158 v[1] += theAdd.v[1];
159 v[2] += theAdd.v[2];
160 return *this;
161 }
162
163 //! Compute per-component summary.
164 friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
165 const NCollection_Vec3& theRight)
166 {
167 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
168 return aSumm += theRight;
169 }
170
171 //! Unary -.
172 NCollection_Vec3 operator-() const
173 {
174 return NCollection_Vec3 (-x(), -y(), -z());
175 }
176
177 //! Compute per-component subtraction.
178 NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
179 {
180 v[0] -= theDec.v[0];
181 v[1] -= theDec.v[1];
182 v[2] -= theDec.v[2];
183 return *this;
184 }
185
186 //! Compute per-component subtraction.
187 friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
188 const NCollection_Vec3& theRight)
189 {
190 NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
191 return aSumm -= theRight;
192 }
193
194 //! Compute per-component multiplication by scale factor.
195 void Multiply (const Element_t theFactor)
196 {
197 v[0] *= theFactor;
198 v[1] *= theFactor;
199 v[2] *= theFactor;
200 }
201
202 //! Compute per-component multiplication.
203 NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
204 {
205 v[0] *= theRight.v[0];
206 v[1] *= theRight.v[1];
207 v[2] *= theRight.v[2];
208 return *this;
209 }
210
211 //! Compute per-component multiplication.
212 friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
213 const NCollection_Vec3& theRight)
214 {
215 NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
216 return aResult *= theRight;
217 }
218
219 //! Compute per-component multiplication by scale factor.
220 NCollection_Vec3& operator*= (const Element_t theFactor)
221 {
222 Multiply (theFactor);
223 return *this;
224 }
225
226 //! Compute per-component multiplication by scale factor.
227 NCollection_Vec3 operator* (const Element_t theFactor) const
228 {
229 return Multiplied (theFactor);
230 }
231
232 //! Compute per-component multiplication by scale factor.
233 NCollection_Vec3 Multiplied (const Element_t theFactor) const
234 {
235 NCollection_Vec3 aCopyVec3 (*this);
236 aCopyVec3 *= theFactor;
237 return aCopyVec3;
238 }
239
240 //! Compute per-component division by scale factor.
241 NCollection_Vec3& operator/= (const Element_t theInvFactor)
242 {
243 v[0] /= theInvFactor;
244 v[1] /= theInvFactor;
245 v[2] /= theInvFactor;
246 return *this;
247 }
248
249 //! Compute per-component division by scale factor.
250 NCollection_Vec3 operator/ (const Element_t theInvFactor)
251 {
252 NCollection_Vec3 aResult (this);
253 return aResult /= theInvFactor;
254 }
255
256 //! Computes the dot product.
257 Element_t Dot (const NCollection_Vec3& theOther) const
258 {
259 return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
260 }
261
262 //! Computes the vector modulus (magnitude, length).
263 Element_t Modulus() const
264 {
265 return std::sqrt (x() * x() + y() * y() + z() * z());
266 }
267
268 //! Computes the square of vector modulus (magnitude, length).
269 //! This method may be used for performance tricks.
270 Element_t SquareModulus() const
271 {
272 return x() * x() + y() * y() + z() * z();
273 }
274
275 //! Normalize the vector.
276 void Normalize()
277 {
278 Element_t aModulus = Modulus();
279 if (aModulus != Element_t(0)) // just avoid divide by zero
280 {
281 x() = x() / aModulus;
282 y() = y() / aModulus;
283 z() = z() / aModulus;
284 }
285 }
286
287 //! Normalize the vector.
288 NCollection_Vec3 Normalized() const
289 {
290 NCollection_Vec3 aCopy (*this);
291 aCopy.Normalize();
292 return aCopy;
293 }
294
295 //! Computes the cross product.
296 static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
297 const NCollection_Vec3& theVec2)
298 {
299 return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
300 theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
301 theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
302 }
303
304 //! Compute linear interpolation between to vectors.
305 //! @param theT - interpolation coefficient 0..1;
306 //! @return interpolation result.
307 static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
308 const NCollection_Vec3& theTo,
309 const Element_t theT)
310 {
311 return theFrom * (Element_t(1) - theT) + theTo * theT;
312 }
313
314 //! Constuct DX unit vector.
315 static NCollection_Vec3 DX()
316 {
317 return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
318 }
319
320 //! Constuct DY unit vector.
321 static NCollection_Vec3 DY()
322 {
323 return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
324 }
325
326 //! Constuct DZ unit vector.
327 static NCollection_Vec3 DZ()
328 {
329 return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
330 }
331
332private:
333
334 Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
335
336};
337
338//! Optimized concretization for float type.
339template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
340{
341 Multiply (1.0f / theInvFactor);
342 return *this;
343}
344
345//! Optimized concretization for double type.
346template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
347{
348 Multiply (1.0 / theInvFactor);
349 return *this;
350}
351
352#endif // _NCollection_Vec3_H__