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