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