Commit | Line | Data |
---|---|---|
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.). | |
34 | template<typename Element_t> | |
35 | class NCollection_Vec3 | |
36 | { | |
37 | ||
38 | public: | |
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 | ||
bc379358 | 68 | //! Constructor from 2-components vector + optional 3rd value. |
69 | explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ = Element_t(0)) | |
5e27df78 | 70 | { |
71 | v[0] = theVec2[0]; | |
72 | v[1] = theVec2[1]; | |
bc379358 | 73 | v[2] = theZ; |
5e27df78 | 74 | } |
75 | ||
f9b30c0d | 76 | //! Conversion constructor (explicitly converts some 3-component vector with other element type |
77 | //! to a new 3-component vector with the element type Element_t, | |
78 | //! whose elements are static_cast'ed corresponding elements of theOtherVec3 vector) | |
79 | //! @tparam OtherElement_t the element type of the other 3-component vector theOtherVec3 | |
80 | //! @param theOtherVec3 the 3-component vector that needs to be converted | |
81 | template <typename OtherElement_t> | |
82 | explicit NCollection_Vec3 (const NCollection_Vec3<OtherElement_t>& theOtherVec3) | |
83 | { | |
84 | v[0] = static_cast<Element_t> (theOtherVec3[0]); | |
85 | v[1] = static_cast<Element_t> (theOtherVec3[1]); | |
86 | v[2] = static_cast<Element_t> (theOtherVec3[2]); | |
87 | } | |
88 | ||
e958a649 | 89 | //! Assign new values to the vector. |
90 | void SetValues (const Element_t theX, | |
91 | const Element_t theY, | |
92 | const Element_t theZ) | |
93 | { | |
94 | v[0] = theX; | |
95 | v[1] = theY; | |
96 | v[2] = theZ; | |
97 | } | |
98 | ||
bc379358 | 99 | //! Assign new values to the vector. |
100 | void SetValues (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ) | |
101 | { | |
102 | v[0] = theVec2.x(); | |
103 | v[1] = theVec2.y(); | |
104 | v[2] = theZ; | |
105 | } | |
106 | ||
5e27df78 | 107 | //! Alias to 1st component as X coordinate in XYZ. |
108 | Element_t x() const { return v[0]; } | |
109 | ||
110 | //! Alias to 1st component as RED channel in RGB. | |
111 | Element_t r() const { return v[0]; } | |
112 | ||
113 | //! Alias to 2nd component as Y coordinate in XYZ. | |
114 | Element_t y() const { return v[1]; } | |
115 | ||
116 | //! Alias to 2nd component as GREEN channel in RGB. | |
117 | Element_t g() const { return v[1]; } | |
118 | ||
119 | //! Alias to 3rd component as Z coordinate in XYZ. | |
120 | Element_t z() const { return v[2]; } | |
121 | ||
122 | //! Alias to 3rd component as BLUE channel in RGB. | |
123 | Element_t b() const { return v[2]; } | |
124 | ||
125 | //! @return 2 components by their names in specified order (in GLSL-style) | |
5640d653 DB |
126 | NCOLLECTION_VEC_COMPONENTS_2D(x, y) |
127 | NCOLLECTION_VEC_COMPONENTS_2D(x, z) | |
128 | NCOLLECTION_VEC_COMPONENTS_2D(y, z) | |
5e27df78 | 129 | |
130 | //! @return 3 components by their names in specified order (in GLSL-style) | |
5640d653 | 131 | NCOLLECTION_VEC_COMPONENTS_3D(x, y, z) |
5e27df78 | 132 | |
133 | //! Alias to 1st component as X coordinate in XYZ. | |
134 | Element_t& x() { return v[0]; } | |
135 | ||
136 | //! Alias to 1st component as RED channel in RGB. | |
137 | Element_t& r() { return v[0]; } | |
138 | ||
139 | //! Alias to 2nd component as Y coordinate in XYZ. | |
140 | Element_t& y() { return v[1]; } | |
141 | ||
142 | //! Alias to 2nd component as GREEN channel in RGB. | |
143 | Element_t& g() { return v[1]; } | |
144 | ||
145 | //! Alias to 3rd component as Z coordinate in XYZ. | |
146 | Element_t& z() { return v[2]; } | |
147 | ||
148 | //! Alias to 3rd component as BLUE channel in RGB. | |
149 | Element_t& b() { return v[2]; } | |
150 | ||
8613985b | 151 | //! Check this vector with another vector for equality (without tolerance!). |
152 | bool IsEqual (const NCollection_Vec3& theOther) const | |
153 | { | |
154 | return v[0] == theOther.v[0] | |
155 | && v[1] == theOther.v[1] | |
156 | && v[2] == theOther.v[2]; | |
157 | } | |
158 | ||
159 | //! Check this vector with another vector for equality (without tolerance!). | |
8613985b | 160 | bool operator== (const NCollection_Vec3& theOther) const { return IsEqual (theOther); } |
161 | ||
162 | //! Check this vector with another vector for non-equality (without tolerance!). | |
8613985b | 163 | bool operator!= (const NCollection_Vec3& theOther) const { return !IsEqual (theOther); } |
164 | ||
5e27df78 | 165 | //! Raw access to the data (for OpenGL exchange). |
938d4544 | 166 | const Element_t* GetData() const { return v; } |
167 | Element_t* ChangeData() { return v; } | |
168 | operator const Element_t*() const { return v; } | |
169 | operator Element_t*() { return v; } | |
5e27df78 | 170 | |
171 | //! Compute per-component summary. | |
172 | NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd) | |
173 | { | |
174 | v[0] += theAdd.v[0]; | |
175 | v[1] += theAdd.v[1]; | |
176 | v[2] += theAdd.v[2]; | |
177 | return *this; | |
178 | } | |
179 | ||
180 | //! Compute per-component summary. | |
181 | friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft, | |
182 | const NCollection_Vec3& theRight) | |
183 | { | |
184 | NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft); | |
185 | return aSumm += theRight; | |
186 | } | |
187 | ||
188 | //! Unary -. | |
189 | NCollection_Vec3 operator-() const | |
190 | { | |
191 | return NCollection_Vec3 (-x(), -y(), -z()); | |
192 | } | |
193 | ||
194 | //! Compute per-component subtraction. | |
195 | NCollection_Vec3& operator-= (const NCollection_Vec3& theDec) | |
196 | { | |
197 | v[0] -= theDec.v[0]; | |
198 | v[1] -= theDec.v[1]; | |
199 | v[2] -= theDec.v[2]; | |
200 | return *this; | |
201 | } | |
202 | ||
203 | //! Compute per-component subtraction. | |
204 | friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft, | |
205 | const NCollection_Vec3& theRight) | |
206 | { | |
207 | NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft); | |
208 | return aSumm -= theRight; | |
209 | } | |
210 | ||
211 | //! Compute per-component multiplication by scale factor. | |
212 | void Multiply (const Element_t theFactor) | |
213 | { | |
214 | v[0] *= theFactor; | |
215 | v[1] *= theFactor; | |
216 | v[2] *= theFactor; | |
217 | } | |
218 | ||
219 | //! Compute per-component multiplication. | |
220 | NCollection_Vec3& operator*= (const NCollection_Vec3& theRight) | |
221 | { | |
222 | v[0] *= theRight.v[0]; | |
223 | v[1] *= theRight.v[1]; | |
224 | v[2] *= theRight.v[2]; | |
225 | return *this; | |
226 | } | |
227 | ||
228 | //! Compute per-component multiplication. | |
229 | friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft, | |
230 | const NCollection_Vec3& theRight) | |
231 | { | |
232 | NCollection_Vec3 aResult = NCollection_Vec3 (theLeft); | |
233 | return aResult *= theRight; | |
234 | } | |
235 | ||
236 | //! Compute per-component multiplication by scale factor. | |
237 | NCollection_Vec3& operator*= (const Element_t theFactor) | |
238 | { | |
239 | Multiply (theFactor); | |
240 | return *this; | |
241 | } | |
242 | ||
243 | //! Compute per-component multiplication by scale factor. | |
244 | NCollection_Vec3 operator* (const Element_t theFactor) const | |
245 | { | |
246 | return Multiplied (theFactor); | |
247 | } | |
248 | ||
249 | //! Compute per-component multiplication by scale factor. | |
250 | NCollection_Vec3 Multiplied (const Element_t theFactor) const | |
251 | { | |
252 | NCollection_Vec3 aCopyVec3 (*this); | |
253 | aCopyVec3 *= theFactor; | |
254 | return aCopyVec3; | |
255 | } | |
256 | ||
3c4e78f2 | 257 | //! Compute component-wise minimum of two vectors. |
258 | NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const | |
259 | { | |
200ed755 | 260 | return NCollection_Vec3 (v[0] < theVec.v[0] ? v[0] : theVec.v[0], |
261 | v[1] < theVec.v[1] ? v[1] : theVec.v[1], | |
262 | v[2] < theVec.v[2] ? v[2] : theVec.v[2]); | |
3c4e78f2 | 263 | } |
264 | ||
265 | //! Compute component-wise maximum of two vectors. | |
266 | NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const | |
267 | { | |
200ed755 | 268 | return NCollection_Vec3 (v[0] > theVec.v[0] ? v[0] : theVec.v[0], |
269 | v[1] > theVec.v[1] ? v[1] : theVec.v[1], | |
270 | v[2] > theVec.v[2] ? v[2] : theVec.v[2]); | |
3c4e78f2 | 271 | } |
272 | ||
91c60b57 | 273 | //! Compute component-wise modulus of the vector. |
274 | NCollection_Vec3 cwiseAbs() const | |
275 | { | |
276 | return NCollection_Vec3 (std::abs (v[0]), | |
277 | std::abs (v[1]), | |
278 | std::abs (v[2])); | |
279 | } | |
280 | ||
281 | //! Compute maximum component of the vector. | |
282 | Element_t maxComp() 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 | ||
288 | //! Compute minimum component of the vector. | |
289 | Element_t minComp() const | |
290 | { | |
291 | return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2]) | |
292 | : (v[1] < v[2] ? v[1] : v[2]); | |
293 | } | |
294 | ||
5e27df78 | 295 | //! Compute per-component division by scale factor. |
296 | NCollection_Vec3& operator/= (const Element_t theInvFactor) | |
297 | { | |
298 | v[0] /= theInvFactor; | |
299 | v[1] /= theInvFactor; | |
300 | v[2] /= theInvFactor; | |
301 | return *this; | |
302 | } | |
303 | ||
d0bcf7aa | 304 | //! Compute per-component division. |
305 | NCollection_Vec3& operator/= (const NCollection_Vec3& theRight) | |
306 | { | |
307 | v[0] /= theRight.v[0]; | |
308 | v[1] /= theRight.v[1]; | |
309 | v[2] /= theRight.v[2]; | |
310 | return *this; | |
311 | } | |
312 | ||
5e27df78 | 313 | //! Compute per-component division by scale factor. |
bc379358 | 314 | NCollection_Vec3 operator/ (const Element_t theInvFactor) const |
5e27df78 | 315 | { |
cc5f85f8 | 316 | NCollection_Vec3 aResult (*this); |
5e27df78 | 317 | return aResult /= theInvFactor; |
318 | } | |
319 | ||
d0bcf7aa | 320 | //! Compute per-component division. |
321 | friend NCollection_Vec3 operator/ (const NCollection_Vec3& theLeft, | |
322 | const NCollection_Vec3& theRight) | |
323 | { | |
324 | NCollection_Vec3 aResult = NCollection_Vec3 (theLeft); | |
325 | return aResult /= theRight; | |
326 | } | |
327 | ||
5e27df78 | 328 | //! Computes the dot product. |
329 | Element_t Dot (const NCollection_Vec3& theOther) const | |
330 | { | |
331 | return x() * theOther.x() + y() * theOther.y() + z() * theOther.z(); | |
332 | } | |
333 | ||
334 | //! Computes the vector modulus (magnitude, length). | |
335 | Element_t Modulus() const | |
336 | { | |
337 | return std::sqrt (x() * x() + y() * y() + z() * z()); | |
338 | } | |
339 | ||
340 | //! Computes the square of vector modulus (magnitude, length). | |
341 | //! This method may be used for performance tricks. | |
342 | Element_t SquareModulus() const | |
343 | { | |
344 | return x() * x() + y() * y() + z() * z(); | |
345 | } | |
346 | ||
347 | //! Normalize the vector. | |
348 | void Normalize() | |
349 | { | |
350 | Element_t aModulus = Modulus(); | |
351 | if (aModulus != Element_t(0)) // just avoid divide by zero | |
352 | { | |
353 | x() = x() / aModulus; | |
354 | y() = y() / aModulus; | |
355 | z() = z() / aModulus; | |
356 | } | |
357 | } | |
358 | ||
359 | //! Normalize the vector. | |
360 | NCollection_Vec3 Normalized() const | |
361 | { | |
362 | NCollection_Vec3 aCopy (*this); | |
363 | aCopy.Normalize(); | |
364 | return aCopy; | |
365 | } | |
366 | ||
367 | //! Computes the cross product. | |
368 | static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1, | |
369 | const NCollection_Vec3& theVec2) | |
370 | { | |
371 | return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(), | |
372 | theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(), | |
373 | theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x()); | |
374 | } | |
375 | ||
376 | //! Compute linear interpolation between to vectors. | |
377 | //! @param theT - interpolation coefficient 0..1; | |
378 | //! @return interpolation result. | |
379 | static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom, | |
380 | const NCollection_Vec3& theTo, | |
381 | const Element_t theT) | |
382 | { | |
383 | return theFrom * (Element_t(1) - theT) + theTo * theT; | |
384 | } | |
385 | ||
316ea293 | 386 | //! Construct DX unit vector. |
5e27df78 | 387 | static NCollection_Vec3 DX() |
388 | { | |
389 | return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0)); | |
390 | } | |
391 | ||
316ea293 | 392 | //! Construct DY unit vector. |
5e27df78 | 393 | static NCollection_Vec3 DY() |
394 | { | |
395 | return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0)); | |
396 | } | |
397 | ||
316ea293 | 398 | //! Construct DZ unit vector. |
5e27df78 | 399 | static NCollection_Vec3 DZ() |
400 | { | |
401 | return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1)); | |
402 | } | |
403 | ||
bc73b006 | 404 | //! Dumps the content of me into the stream |
405 | void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const | |
406 | { | |
407 | (void)theDepth; | |
408 | OCCT_DUMP_FIELD_VALUES_NUMERICAL (theOStream, "Vec3", 3, v[0], v[1], v[2]) | |
409 | } | |
410 | ||
5e27df78 | 411 | private: |
412 | ||
413 | Element_t v[3]; //!< define the vector as array to avoid structure alignment issues | |
414 | ||
415 | }; | |
416 | ||
417 | //! Optimized concretization for float type. | |
418 | template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor) | |
419 | { | |
420 | Multiply (1.0f / theInvFactor); | |
421 | return *this; | |
422 | } | |
423 | ||
424 | //! Optimized concretization for double type. | |
425 | template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor) | |
426 | { | |
427 | Multiply (1.0 / theInvFactor); | |
428 | return *this; | |
429 | } | |
430 | ||
431 | #endif // _NCollection_Vec3_H__ |