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 | ||
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 | ||
5e27df78 | 76 | //! Alias to 1st component as X coordinate in XYZ. |
77 | Element_t x() const { return v[0]; } | |
78 | ||
79 | //! Alias to 1st component as RED channel in RGB. | |
80 | Element_t r() const { return v[0]; } | |
81 | ||
82 | //! Alias to 2nd component as Y coordinate in XYZ. | |
83 | Element_t y() const { return v[1]; } | |
84 | ||
85 | //! Alias to 2nd component as GREEN channel in RGB. | |
86 | Element_t g() const { return v[1]; } | |
87 | ||
88 | //! Alias to 3rd component as Z coordinate in XYZ. | |
89 | Element_t z() const { return v[2]; } | |
90 | ||
91 | //! Alias to 3rd component as BLUE channel in RGB. | |
92 | Element_t b() const { return v[2]; } | |
93 | ||
94 | //! @return 2 components by their names in specified order (in GLSL-style) | |
5640d653 DB |
95 | NCOLLECTION_VEC_COMPONENTS_2D(x, y) |
96 | NCOLLECTION_VEC_COMPONENTS_2D(x, z) | |
97 | NCOLLECTION_VEC_COMPONENTS_2D(y, z) | |
5e27df78 | 98 | |
99 | //! @return 3 components by their names in specified order (in GLSL-style) | |
5640d653 | 100 | NCOLLECTION_VEC_COMPONENTS_3D(x, y, z) |
5e27df78 | 101 | |
102 | //! Alias to 1st component as X coordinate in XYZ. | |
103 | Element_t& x() { return v[0]; } | |
104 | ||
105 | //! Alias to 1st component as RED channel in RGB. | |
106 | Element_t& r() { return v[0]; } | |
107 | ||
108 | //! Alias to 2nd component as Y coordinate in XYZ. | |
109 | Element_t& y() { return v[1]; } | |
110 | ||
111 | //! Alias to 2nd component as GREEN channel in RGB. | |
112 | Element_t& g() { return v[1]; } | |
113 | ||
114 | //! Alias to 3rd component as Z coordinate in XYZ. | |
115 | Element_t& z() { return v[2]; } | |
116 | ||
117 | //! Alias to 3rd component as BLUE channel in RGB. | |
118 | Element_t& b() { return v[2]; } | |
119 | ||
120 | //! @return XY-components modifiable vector | |
121 | NCollection_Vec2<Element_t>& xy() | |
122 | { | |
123 | return *((NCollection_Vec2<Element_t>* )&v[0]); | |
124 | } | |
125 | ||
126 | //! @return YZ-components modifiable vector | |
127 | NCollection_Vec2<Element_t>& yz() | |
128 | { | |
129 | return *((NCollection_Vec2<Element_t>* )&v[1]); | |
130 | } | |
131 | ||
8613985b | 132 | //! Check this vector with another vector for equality (without tolerance!). |
133 | bool IsEqual (const NCollection_Vec3& theOther) const | |
134 | { | |
135 | return v[0] == theOther.v[0] | |
136 | && v[1] == theOther.v[1] | |
137 | && v[2] == theOther.v[2]; | |
138 | } | |
139 | ||
140 | //! Check this vector with another vector for equality (without tolerance!). | |
141 | bool operator== (const NCollection_Vec3& theOther) { return IsEqual (theOther); } | |
142 | bool operator== (const NCollection_Vec3& theOther) const { return IsEqual (theOther); } | |
143 | ||
144 | //! Check this vector with another vector for non-equality (without tolerance!). | |
145 | bool operator!= (const NCollection_Vec3& theOther) { return !IsEqual (theOther); } | |
146 | bool operator!= (const NCollection_Vec3& theOther) const { return !IsEqual (theOther); } | |
147 | ||
5e27df78 | 148 | //! Raw access to the data (for OpenGL exchange). |
938d4544 | 149 | const Element_t* GetData() const { return v; } |
150 | Element_t* ChangeData() { return v; } | |
151 | operator const Element_t*() const { return v; } | |
152 | operator Element_t*() { return v; } | |
5e27df78 | 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 | ||
3c4e78f2 | 240 | //! Compute component-wise minimum of two vectors. |
241 | NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const | |
242 | { | |
200ed755 | 243 | return NCollection_Vec3 (v[0] < theVec.v[0] ? v[0] : theVec.v[0], |
244 | v[1] < theVec.v[1] ? v[1] : theVec.v[1], | |
245 | v[2] < theVec.v[2] ? v[2] : theVec.v[2]); | |
3c4e78f2 | 246 | } |
247 | ||
248 | //! Compute component-wise maximum of two vectors. | |
249 | NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const | |
250 | { | |
200ed755 | 251 | return NCollection_Vec3 (v[0] > theVec.v[0] ? v[0] : theVec.v[0], |
252 | v[1] > theVec.v[1] ? v[1] : theVec.v[1], | |
253 | v[2] > theVec.v[2] ? v[2] : theVec.v[2]); | |
3c4e78f2 | 254 | } |
255 | ||
91c60b57 | 256 | //! Compute component-wise modulus of the vector. |
257 | NCollection_Vec3 cwiseAbs() const | |
258 | { | |
259 | return NCollection_Vec3 (std::abs (v[0]), | |
260 | std::abs (v[1]), | |
261 | std::abs (v[2])); | |
262 | } | |
263 | ||
264 | //! Compute maximum component of the vector. | |
265 | Element_t maxComp() const | |
266 | { | |
267 | return v[0] > v[1] ? (v[0] > v[2] ? v[0] : v[2]) | |
268 | : (v[1] > v[2] ? v[1] : v[2]); | |
269 | } | |
270 | ||
271 | //! Compute minimum component of the vector. | |
272 | Element_t minComp() const | |
273 | { | |
274 | return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2]) | |
275 | : (v[1] < v[2] ? v[1] : v[2]); | |
276 | } | |
277 | ||
5e27df78 | 278 | //! Compute per-component division by scale factor. |
279 | NCollection_Vec3& operator/= (const Element_t theInvFactor) | |
280 | { | |
281 | v[0] /= theInvFactor; | |
282 | v[1] /= theInvFactor; | |
283 | v[2] /= theInvFactor; | |
284 | return *this; | |
285 | } | |
286 | ||
287 | //! Compute per-component division by scale factor. | |
288 | NCollection_Vec3 operator/ (const Element_t theInvFactor) | |
289 | { | |
cc5f85f8 | 290 | NCollection_Vec3 aResult (*this); |
5e27df78 | 291 | return aResult /= theInvFactor; |
292 | } | |
293 | ||
294 | //! Computes the dot product. | |
295 | Element_t Dot (const NCollection_Vec3& theOther) const | |
296 | { | |
297 | return x() * theOther.x() + y() * theOther.y() + z() * theOther.z(); | |
298 | } | |
299 | ||
300 | //! Computes the vector modulus (magnitude, length). | |
301 | Element_t Modulus() const | |
302 | { | |
303 | return std::sqrt (x() * x() + y() * y() + z() * z()); | |
304 | } | |
305 | ||
306 | //! Computes the square of vector modulus (magnitude, length). | |
307 | //! This method may be used for performance tricks. | |
308 | Element_t SquareModulus() const | |
309 | { | |
310 | return x() * x() + y() * y() + z() * z(); | |
311 | } | |
312 | ||
313 | //! Normalize the vector. | |
314 | void Normalize() | |
315 | { | |
316 | Element_t aModulus = Modulus(); | |
317 | if (aModulus != Element_t(0)) // just avoid divide by zero | |
318 | { | |
319 | x() = x() / aModulus; | |
320 | y() = y() / aModulus; | |
321 | z() = z() / aModulus; | |
322 | } | |
323 | } | |
324 | ||
325 | //! Normalize the vector. | |
326 | NCollection_Vec3 Normalized() const | |
327 | { | |
328 | NCollection_Vec3 aCopy (*this); | |
329 | aCopy.Normalize(); | |
330 | return aCopy; | |
331 | } | |
332 | ||
333 | //! Computes the cross product. | |
334 | static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1, | |
335 | const NCollection_Vec3& theVec2) | |
336 | { | |
337 | return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(), | |
338 | theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(), | |
339 | theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x()); | |
340 | } | |
341 | ||
342 | //! Compute linear interpolation between to vectors. | |
343 | //! @param theT - interpolation coefficient 0..1; | |
344 | //! @return interpolation result. | |
345 | static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom, | |
346 | const NCollection_Vec3& theTo, | |
347 | const Element_t theT) | |
348 | { | |
349 | return theFrom * (Element_t(1) - theT) + theTo * theT; | |
350 | } | |
351 | ||
352 | //! Constuct DX unit vector. | |
353 | static NCollection_Vec3 DX() | |
354 | { | |
355 | return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0)); | |
356 | } | |
357 | ||
358 | //! Constuct DY unit vector. | |
359 | static NCollection_Vec3 DY() | |
360 | { | |
361 | return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0)); | |
362 | } | |
363 | ||
364 | //! Constuct DZ unit vector. | |
365 | static NCollection_Vec3 DZ() | |
366 | { | |
367 | return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1)); | |
368 | } | |
369 | ||
370 | private: | |
371 | ||
372 | Element_t v[3]; //!< define the vector as array to avoid structure alignment issues | |
373 | ||
374 | }; | |
375 | ||
376 | //! Optimized concretization for float type. | |
377 | template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor) | |
378 | { | |
379 | Multiply (1.0f / theInvFactor); | |
380 | return *this; | |
381 | } | |
382 | ||
383 | //! Optimized concretization for double type. | |
384 | template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor) | |
385 | { | |
386 | Multiply (1.0 / theInvFactor); | |
387 | return *this; | |
388 | } | |
389 | ||
390 | #endif // _NCollection_Vec3_H__ |