Open Inventor Release 2024.2.1
 
Loading...
Searching...
No Matches
MbVec3.h
1/*=======================================================================
2 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
3 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
4 *** ***
5 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
6 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
7 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
8 *** ***
9 *** RESTRICTED RIGHTS LEGEND ***
10 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
11 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
12 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
13 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
14 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
15 *** ***
16 *** COPYRIGHT (C) 1996-2017 BY FEI S.A.S, ***
17 *** BORDEAUX, FRANCE ***
18 *** ALL RIGHTS RESERVED ***
19**=======================================================================*/
20/*=======================================================================
21** Author : VSG (MMM YYYY)
22**=======================================================================*/
23#ifndef _MBVEC3_H
24#define _MBVEC3_H
25
26#ifdef _WIN32
27# pragma warning( push )
28# pragma warning(disable:4250)
29#endif
30
31
32#include <Inventor/STL/iostream>
33#include <Inventor/STL/limits>
34#include <Inventor/SbMathHelper.h>
35
36#include <cmath>
37
50template <typename _T>
51class MbVec3
52{
53public:
54 typedef _T ValueType;
55
58 {
59 return MbVec3<_T>((std::numeric_limits<_T>::max)());
60 }
61
64 {
65 vec[0] = vec[1] = vec[2] = 0;
66 }
68 MbVec3(const _T v[3])
69 {
70 vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2];
71 }
72
74 MbVec3(_T x, _T y, _T z)
75 {
76 vec[0] = x; vec[1] = y; vec[2] = z;
77 }
78
79 template < typename _Type >
80 MbVec3(_Type x, _Type y, _Type z)
81 {
82 vec[0] = (_T) x; vec[1] = (_T) y; vec[2] = (_T) z;
83 }
84
86 explicit MbVec3(char v)
87 {
88 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
89 }
90
92 explicit MbVec3(unsigned char v)
93 {
94 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
95 }
96
98 explicit MbVec3(short v)
99 {
100 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
101 }
102
104 explicit MbVec3(unsigned short v)
105 {
106 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
107 }
108
110 explicit MbVec3(int v)
111 {
112 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
113 }
114
116 explicit MbVec3(size_t v)
117 {
118 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
119 }
120
122 explicit MbVec3(long v)
123 {
124 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
125 }
126
127#if defined(_WIN32)
129 explicit MbVec3(unsigned long v)
130 {
131 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
132 }
133#endif
134
136 explicit MbVec3(float v)
137 {
138 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
139 }
140
142 explicit MbVec3(double v)
143 {
144 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
145 }
146
148 explicit MbVec3(long double v)
149 {
150 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
151 }
152
154 template<typename _MbVec3T>
155 explicit MbVec3(const _MbVec3T& v)
156 {
157 vec[0] = (_T)v[0];
158 vec[1] = (_T)v[1];
159 vec[2] = (_T)v[2];
160 }
161
165 MbVec3 cross(const MbVec3<_T> &v) const
166 {
167 return MbVec3<_T>(vec[1] * v.vec[2] - vec[2] * v.vec[1],
168 vec[2] * v.vec[0] - vec[0] * v.vec[2],
169 vec[0] * v.vec[1] - vec[1] * v.vec[0]);
170 }
171
175 _T dot(const MbVec3<_T> &v) const
176 {
177 return (vec[0] * v.vec[0] + vec[1] * v.vec[1] + vec[2] * v.vec[2]);
178 }
179
183 const _T *getValue() const { return vec; }
184
188 void getValue(_T &x, _T &y, _T &z) const
189 {
190 x = vec[0]; y = vec[1]; z = vec[2];
191 }
192
196 _T length() const
197 {
198 return _T(sqrt(double(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2])));
199 }
200
205
209 void negate()
210 {
211 vec[0] = -vec[0]; vec[1] = -vec[1]; vec[2] = -vec[2];
212 }
213
217 MbVec3 mult(const MbVec3<_T> &v) const
218 {
219 return MbVec3<_T>(vec[0] * v.vec[0], vec[1] * v.vec[1],vec[2] * v.vec[2]) ;
220 }
221
225 MbVec3 div(const MbVec3<_T> &v) const
226 {
227 return MbVec3<_T>(vec[0] / v.vec[0], vec[1] / v.vec[1],vec[2] / v.vec[2]) ;
228 }
229
233 MbVec3<_T>& setValue(const _T v[3])
234 {
235 vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2]; return *this;
236 }
237
241 MbVec3<_T> &setValue(_T x, _T y, _T z)
242 {
243 vec[0] = x; vec[1] = y; vec[2] = z; return *this;
244 }
245
247
250 _T& operator [](int i) { return (vec[i]); }
251 const _T & operator [](int i) const { return (vec[i]); }
252 _T& operator [](size_t i) { return (vec[i]); }
253 const _T & operator [](size_t i) const { return (vec[i]); }
255
260 {
261 vec[0] = vec[1] = vec[2] = d;
262 return *this;
263 }
264
269 {
270 vec[0] *= d;
271 vec[1] *= d;
272 vec[2] *= d;
273 return *this;
274 }
275
280 {
281 vec[0] /= d;
282 vec[1] /= d;
283 vec[2] /= d;
284 return *this;
285 }
286
291 {
292 vec[0] += v.vec[0];
293 vec[1] += v.vec[1];
294 vec[2] += v.vec[2];
295 return *this;
296 }
297
302 {
303 vec[0] -= v.vec[0];
304 vec[1] -= v.vec[1];
305 vec[2] -= v.vec[2];
306 return *this;
307 }
308
309
314 {
315 return MbVec3<_T>(-vec[0],-vec[1],-vec[2]);
316 }
317
321 friend MbVec3 operator *(const MbVec3 &v1, const MbVec3 &v2)
322 {
323 return MbVec3(v1.vec[0] * v2.vec[0], v1.vec[1] * v2.vec[1], v1.vec[2] * v2.vec[2]);
324 }
325
329 friend MbVec3 operator *(const MbVec3 &v, _T d)
330 {
331 return MbVec3<_T>(v.vec[0] * d, v.vec[1] * d, v.vec[2] * d);
332 }
333
337 friend MbVec3 operator *(_T d, const MbVec3 &v)
338 { return v * d; }
339
343 friend MbVec3 operator /(const MbVec3 &v, _T d)
344 {
345 return MbVec3<_T>(v.vec[0] / d, v.vec[1] / d, v.vec[2] / d);
346 }
347
351 friend MbVec3 operator /(const MbVec3 &v1, const MbVec3 &v2)
352 {
353 return MbVec3(v1.vec[0] / v2.vec[0], v1.vec[1] / v2.vec[1], v1.vec[2] / v2.vec[2]);
354 }
355
359 friend MbVec3 operator +(const MbVec3 &v1, const MbVec3 &v2)
360 {
361 return MbVec3(v1.vec[0] + v2.vec[0], v1.vec[1] + v2.vec[1], v1.vec[2] + v2.vec[2]);
362 }
363
367 friend MbVec3 operator +(const MbVec3 &v, _T d)
368 {
369 return MbVec3(v.vec[0] + d, v.vec[1] + d, v.vec[2] + d);
370 }
371
375 friend MbVec3 operator +(_T d, const MbVec3 &v)
376 {
377 return MbVec3(v.vec[0] + d, v.vec[1] + d, v.vec[2] + d);
378 }
379
383 friend MbVec3 operator -(const MbVec3 &v1, const MbVec3 &v2)
384 {
385 return MbVec3(v1.vec[0] - v2.vec[0], v1.vec[1] - v2.vec[1], v1.vec[2] - v2.vec[2]);
386 }
387
391 friend MbVec3 operator -(const MbVec3 &v, _T d)
392 {
393 return MbVec3(v.vec[0] - d, v.vec[1] - d, v.vec[2] - d);
394 }
395
399 friend MbVec3 sqrt(const MbVec3 &v)
400 {
401 return MbVec3(sqrt(v.vec[0]), sqrt(v.vec[1]), sqrt(v.vec[2]));
402 }
403
407 friend bool operator ==(const MbVec3 &v1, const MbVec3 &v2)
408 {
409 return (v1.vec[0] == v2.vec[0] && v1.vec[1] == v2.vec[1] && v1.vec[2] == v2.vec[2]);
410 }
411
415 friend bool operator !=(const MbVec3 &v1, const MbVec3 &v2)
416 {
417 return !(v1 == v2);
418 }
419
423 friend bool operator <(const MbVec3 &v1, const MbVec3 &v2)
424 {
425 return v1.length() < v2.length();
426 }
427
431 friend bool operator <=(const MbVec3 &v1, const MbVec3 &v2)
432 {
433 return v1.length() <= v2.length();
434 }
435
439 friend bool operator >(const MbVec3 &v1, const MbVec3 &v2)
440 {
441 return v1.length() > v2.length();
442 }
443
447 friend bool operator >=(const MbVec3 &v1, const MbVec3 &v2)
448 {
449 return v1.length() >= v2.length();
450 }
451
456 bool equals(const MbVec3 &v, _T tolerance) const
457 {
458 MbVec3 diff = *this - v;
459 return diff.dot(diff) <= tolerance;
460 }
461
466 bool equals(const MbVec3 &v) const
467 {
468 return this->equals(v,std::numeric_limits<_T>::epsilon());
469 }
470
474 friend bool areCollinear(const MbVec3<_T> &v1, const MbVec3<_T> &v2, _T tol)
475 {
476 return SbMathHelper::isCoinc(v1[0]*v2[1],v1[1]*v2[0],tol) && SbMathHelper::isCoinc(v1[0]*v2[2],v1[2]*v2[0],tol)
477 && SbMathHelper::isCoinc(v1[1]*v2[2],v1[2]*v2[1],tol);
478 }
479
484 friend bool areCollinear(const MbVec3<_T> &v1, const MbVec3<_T> &v2)
485 {
486 return areCollinear(v1,v2,std::numeric_limits<_T>::epsilon());
487 }
488
492 friend inline std::ostream& operator << (std::ostream& os, const MbVec3& v)
493 {
494 return os << "(" << v.vec[0] << "," << v.vec[1] << "," << v.vec[2] << ")";
495 }
496
497private:
498 _T vec[3];
499};
500
501template <typename _T>
502_T
504{
505 _T len = length();
506
507 if (len != 0)
508 (*this) /= len;
509 else
510 setValue(0,0,0);
511 return len;
512}
513
520
533
534#ifdef _WIN32
535# pragma warning( pop )
536#endif
537
538#endif
539
540
541
542
543
544
545
<a href="IconLegend.html"><img src="extMV.gif" alt="MeshViz" border="0"></a> Class defining a vector ...
Definition MbVec3.h:52
MbVec3< _T > & setValue(_T x, _T y, _T z)
Sets the vector components.
Definition MbVec3.h:241
bool equals(const MbVec3 &v) const
Equality comparison within the machine epsilon given by the numeric_limits<_T> Square of the length o...
Definition MbVec3.h:466
friend bool operator>(const MbVec3 &v1, const MbVec3 &v2)
Length comparison operator.
Definition MbVec3.h:439
bool equals(const MbVec3 &v, _T tolerance) const
Equality comparison within given tolerance - the square of the length of the maximum distance between...
Definition MbVec3.h:456
friend bool operator==(const MbVec3 &v1, const MbVec3 &v2)
Equality comparison operator.
Definition MbVec3.h:407
MbVec3(const _MbVec3T &v)
Constructor.
Definition MbVec3.h:155
MbVec3(_Type x, _Type y, _Type z)
Definition MbVec3.h:80
friend bool operator<=(const MbVec3 &v1, const MbVec3 &v2)
Length comparison operator.
Definition MbVec3.h:431
void negate()
Negates each component of vector in place.
Definition MbVec3.h:209
MbVec3 & operator=(_T d)
Component-wise setting.
Definition MbVec3.h:259
MbVec3 & operator-=(const MbVec3 &v)
Component-wise vector subtraction operator.
Definition MbVec3.h:301
_T length() const
Returns geometric length of vector.
Definition MbVec3.h:196
friend MbVec3 operator*(const MbVec3 &v1, const MbVec3 &v2)
Component-wise vector multiplication operator.
Definition MbVec3.h:321
void getValue(_T &x, _T &y, _T &z) const
Returns vector components.
Definition MbVec3.h:188
const _T * getValue() const
Returns pointer to vector components.
Definition MbVec3.h:183
MbVec3 div(const MbVec3< _T > &v) const
Component-wise vector division.
Definition MbVec3.h:225
friend std::ostream & operator<<(std::ostream &os, const MbVec3 &v)
Writes the vector to the specified output stream.
Definition MbVec3.h:492
MbVec3(long v)
Constructor.
Definition MbVec3.h:122
MbVec3 cross(const MbVec3< _T > &v) const
Returns right-handed cross product of vector and another vector.
Definition MbVec3.h:165
MbVec3< _T > & setValue(const _T v[3])
Sets the vector components.
Definition MbVec3.h:233
MbVec3 mult(const MbVec3< _T > &v) const
Component-wise vector multiplication.
Definition MbVec3.h:217
MbVec3(double v)
Constructor.
Definition MbVec3.h:142
MbVec3(char v)
Constructor.
Definition MbVec3.h:86
static MbVec3 numeric_limit_max()
Returns the maximum value for a vector.
Definition MbVec3.h:57
_T dot(const MbVec3< _T > &v) const
Returns dot (inner) product of vector and another vector.
Definition MbVec3.h:175
MbVec3 & operator/=(_T d)
Component-wise scalar division operator.
Definition MbVec3.h:279
friend bool areCollinear(const MbVec3< _T > &v1, const MbVec3< _T > &v2, _T tol)
Returns true if vectors are collinear within the given tolerance.
Definition MbVec3.h:474
MbVec3 operator-() const
Nondestructive unary negation - returns a new vector.
Definition MbVec3.h:313
friend bool operator!=(const MbVec3 &v1, const MbVec3 &v2)
Inequality comparison operator.
Definition MbVec3.h:415
friend MbVec3 operator+(const MbVec3 &v1, const MbVec3 &v2)
Component-wise binary vector addition operator.
Definition MbVec3.h:359
MbVec3 & operator+=(const MbVec3 &v)
Component-wise vector addition operator.
Definition MbVec3.h:290
friend MbVec3 sqrt(const MbVec3 &v)
Component-wise vector square root operator.
Definition MbVec3.h:399
_T ValueType
Definition MbVec3.h:54
_T & operator[](int i)
Accesses indexed component of vector.
Definition MbVec3.h:250
MbVec3()
Constructor (vector is initialized to zero values)
Definition MbVec3.h:63
_T normalize()
Changes vector to be unit length, returning the length before normalization.
Definition MbVec3.h:503
friend MbVec3 operator/(const MbVec3 &v, _T d)
Component-wise binary scalar division operator.
Definition MbVec3.h:343
MbVec3(unsigned short v)
Constructor.
Definition MbVec3.h:104
MbVec3(int v)
Constructor.
Definition MbVec3.h:110
MbVec3(const _T v[3])
Constructor.
Definition MbVec3.h:68
friend bool operator>=(const MbVec3 &v1, const MbVec3 &v2)
Length comparison operator.
Definition MbVec3.h:447
friend bool operator<(const MbVec3 &v1, const MbVec3 &v2)
Length comparison operator.
Definition MbVec3.h:423
MbVec3(long double v)
Constructor.
Definition MbVec3.h:148
MbVec3(short v)
Constructor.
Definition MbVec3.h:98
MbVec3 & operator*=(_T d)
Component-wise scalar multiplication operator.
Definition MbVec3.h:268
MbVec3(unsigned char v)
Constructor.
Definition MbVec3.h:92
MbVec3(_T x, _T y, _T z)
Constructor.
Definition MbVec3.h:74
friend bool areCollinear(const MbVec3< _T > &v1, const MbVec3< _T > &v2)
Returns true if vectors are collinear within the machine epsilon given by the numeric_limits<_T>
Definition MbVec3.h:484
MbVec3(size_t v)
Constructor.
Definition MbVec3.h:116
MbVec3(float v)
Constructor.
Definition MbVec3.h:136
MbVec3< double > MbVec3d
Vector of 3 double coordinates.
Definition MbVec3.h:532
MbVec3< float > MbVec3f
Vector of 3 float coordinates.
Definition MbVec3.h:526
MbVec3< size_t > MbVec3ui
Vector of 3 unsigned int.
Definition MbVec3.h:519
bool isCoinc(const T &x, const T &y, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Coincidence test using given tolerance.