Momentum Scripting v1
Loading...
Searching...
No Matches
Vec4.h
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material without having a written signed agreement with Algoryx Simulation AB.
9
10Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
11from using this software, unless otherwise stated in written agreements with
12Algoryx Simulation AB.
13*/
14
15#ifndef MOMENTUM_VEC4_H
16#define MOMENTUM_VEC4_H
17
18#include "momentum_namespace.h"
19#include "momentum_math.h"
20
21#include <iosfwd>
22
23#include <agx/agx.h>
24#include "Vec3.h"
25
26
27namespace MOMENTUM_NAMESPACE
28{
32 class Vec4
33 {
34 public:
35 typedef double Type;
36
37 public:
38
40 Vec4(const Vec4& copy );
41
45 Vec4();
46
48 explicit Vec4( double r );
49
51 Vec4( double x, double y, double z, double w );
52
54 explicit Vec4(const double v[4] );
55
57 Vec4( const Vec3& v3, double w );
58
60 static Vec4 random(double min, double max);
61
63 static Vec4 random(const Vec4& min, const Vec4& max);
64
68 bool operator == ( const Vec4& v ) const;
69
73 bool operator != ( const Vec4& v ) const;
74
78 static Vec4 componentMin(const Vec4& v1, const Vec4& v2);
79
83 static Vec4 componentMax(const Vec4& v1, const Vec4& v2);
84
88 double minComponent() const;
89
93 double maxComponent() const;
94
98 int minElement() const;
99
103 int maxElement() const;
104
108 void clamp(const Vec4& min, const Vec4& max);
109
113 bool equalsZero() const;
114
116 void set( double x, double y, double z, double w );
117
119 void set( double value );
120
122 void set( const Vec4& rhs );
123
124#ifndef SWIG
126 double& operator [] ( int i );
127
129 const double& operator [] ( int i ) const;
130#endif
132 double x() const;
133
135 double y() const;
136
138 double z() const;
139
141 double w() const;
142
143 void setX(double value);
144
145 void setY(double value);
146
147 void setZ(double value);
148
149 void setW(double value);
150
152 bool isValid() const;
153
155 bool isNaN() const;
156
158 bool isFinite() const;
159
163 double operator * ( const Vec4& rhs ) const;
164
168 const Vec4 operator | ( const Vec4& rhs ) const;
169
173 const Vec4 operator * ( double rhs ) const;
174
178 Vec4& operator *= ( double rhs );
179
183 const Vec4 operator / ( double rhs ) const;
184
188 Vec4& operator /= ( double rhs );
189
194 const Vec4 operator + ( const Vec4& rhs ) const;
195
200 Vec4& operator += ( const Vec4& rhs );
201
205 const Vec4 operator - ( const Vec4& rhs ) const;
206
210 Vec4& operator -= ( const Vec4& rhs );
211
215 const Vec4 operator + ( const double& rhs ) const;
216
221 Vec4& operator += ( const double& rhs );
222
226 const Vec4 operator - ( const double& rhs ) const;
227
231 Vec4& operator -= ( const double& rhs );
232
236 const Vec4 operator - () const;
237
241 double length() const;
242
246 double length2() const;
247
251 double distance2(const Vec4& v2) const;
252
256 double distance(const Vec4& v2) const;
257
262 double normalize();
263
267 Vec3 asVec3() const;
268
270 std::string __str__() const;
272
273 private:
274 double m_data[4];
275 }; // end of class Vec4
276
277
278#ifndef SWIG
279 /* Implementation */
280
281
282 inline Vec4::Vec4(const Vec4& copy )
283 {
284 m_data[ 0 ] = copy.m_data[ 0 ];
285 m_data[ 1 ] = copy.m_data[ 1 ];
286 m_data[ 2 ] = copy.m_data[ 2 ];
287 m_data[ 3 ] = copy.m_data[ 3 ];
288 }
289
290
291 inline Vec4::Vec4()
292 {
293 m_data[0] = double();
294 m_data[1] = double();
295 m_data[2] = double();
296 m_data[3] = double();
297 }
298
299
300 inline Vec4::Vec4( double r )
301 {
302 m_data[0] = m_data[1] = m_data[2] = m_data[3] = r;
303 }
304
305
306 inline Vec4::Vec4( double x, double y, double z, double w )
307 {
308 m_data[0] = x;
309 m_data[1] = y;
310 m_data[2] = z;
311 m_data[3] = w;
312 }
313
314
315 inline Vec4::Vec4(const double v[4] )
316 {
317 m_data[0] = v[0];
318 m_data[1] = v[1];
319 m_data[2] = v[2];
320 m_data[3] = v[3];
321 }
322
323
324 inline Vec4::Vec4( const Vec3& v3, double w )
325 {
326 m_data[0] = double(v3[0]);
327 m_data[1] = double(v3[1]);
328 m_data[2] = double(v3[2]);
329 m_data[3] = w;
330 }
331
332
333
334 inline bool Vec4::operator == ( const Vec4& v ) const
335 {
336 return m_data[0] == v.m_data[0] && m_data[1] == v.m_data[1] && m_data[2] == v.m_data[2] && m_data[3] == v.m_data[3];
337 }
338
339
340 inline bool Vec4::operator != ( const Vec4& v ) const
341 {
342 return m_data[0] != v.m_data[0] || m_data[1] != v.m_data[1] || m_data[2] != v.m_data[2] || m_data[3] != v.m_data[3];
343 }
344
345
346
347 inline Vec4 Vec4::componentMin(const Vec4& v1, const Vec4& v2)
348 {
349 return Vec4(std::min(v1[0], v2[0]), std::min(v1[1], v2[1]), std::min(v1[2], v2[2]), std::min(v1[3], v2[3]));
350 }
351
352
353 inline Vec4 Vec4::componentMax(const Vec4& v1, const Vec4& v2)
354 {
355 return Vec4(std::max(v1[0], v2[0]), std::max(v1[1], v2[1]), std::max(v1[2], v2[2]), std::max(v1[3], v2[3]));
356 }
357
358
359 inline double Vec4::minComponent() const
360 {
361 return std::min(std::min(std::min(m_data[0], m_data[1]), m_data[2]), m_data[3]);
362 }
363
364
365 inline double Vec4::maxComponent() const
366 {
367 return std::max(std::max(std::max(m_data[0], m_data[1]), m_data[2]), m_data[3]);
368 }
369
370
371 inline int Vec4::minElement() const
372 {
373 double m = std::numeric_limits<double>::infinity();
374 int idx = 0;
375 for(int i = 0; i < 4; i++) {
376 double a = agx::absolute(m_data[i]);
377 if ( a < m) {
378 idx = i;
379 m = a;
380 }
381 }
382 return idx;
383 }
384
385
386 inline int Vec4::maxElement() const
387 {
388 double m = 0;
389 int idx = 0;
390 for(int i = 0; i < 4; i++) {
391 double a = agx::absolute(m_data[i]);
392 if ( a > m) {
393 idx = i;
394 m = a;
395 }
396 }
397 return idx;
398 }
399
400
401 inline void Vec4::clamp(const Vec4& min, const Vec4& max)
402 {
403 m_data[0] = agx::clamp( m_data[0], min.m_data[0], max.m_data[0] );
404 m_data[1] = agx::clamp( m_data[1], min.m_data[1], max.m_data[1] );
405 m_data[2] = agx::clamp( m_data[2], min.m_data[2], max.m_data[2] );
406 m_data[3] = agx::clamp( m_data[3], min.m_data[3], max.m_data[3] );
407 }
408
409
410
411 inline bool Vec4::equalsZero() const
412 {
413 return (agx::equalsZero(m_data[0]) && agx::equalsZero(m_data[1]) && agx::equalsZero(m_data[2]) && agx::equalsZero(m_data[3]));
414 }
415
416
417
418
419 inline void Vec4::set( double x, double y, double z, double w )
420 {
421 m_data[0] = x;
422 m_data[1] = y;
423 m_data[2] = z;
424 m_data[3] = w;
425 }
426
427
428 inline void Vec4::set( double value )
429 {
430 m_data[0] = m_data[1] = m_data[2] = m_data[3] = value;
431 }
432
433
434 inline void Vec4::set( const Vec4& rhs )
435 {
436 m_data[0] = rhs.m_data[0];
437 m_data[1] = rhs.m_data[1];
438 m_data[2] = rhs.m_data[2];
439 m_data[3] = rhs.m_data[3];
440 }
441
442
443 inline double& Vec4::operator [] ( int i )
444 {
445 return m_data[i];
446 }
447
448
449 inline const double& Vec4::operator [] ( int i ) const
450 {
451 return m_data[i];
452 }
453
454
455 inline double Vec4::x() const
456 {
457 return m_data[0];
458 }
459
460
461 inline double Vec4::y() const
462 {
463 return m_data[1];
464 }
465
466 inline double Vec4::z() const
467 {
468 return m_data[2];
469 }
470
471
472 inline double Vec4::w() const
473 {
474 return m_data[3];
475 }
476
477
478 inline void Vec4::setX(double value)
479 {
480 m_data[0] = value;
481 }
482
483
484 inline void Vec4::setY(double value)
485 {
486 m_data[1] = value;
487 }
488
489
490 inline void Vec4::setZ(double value)
491 {
492 m_data[2] = value;
493 }
494
495
496 inline void Vec4::setW(double value)
497 {
498 m_data[3] = value;
499 }
500
501
502 inline bool Vec4::isValid() const
503 {
504 return !isNaN();
505 }
506
507
508 inline bool Vec4::isNaN() const
509 {
510 return agx::isNaN( m_data[0] ) || agx::isNaN( m_data[1] ) || agx::isNaN( m_data[2] ) || agx::isNaN( m_data[3] );
511 }
512
513
514 inline bool Vec4::isFinite() const
515 {
516 return agx::isFinite( m_data[0] ) && agx::isFinite( m_data[1] ) && agx::isFinite( m_data[2] ) && agx::isFinite( m_data[3] );
517 }
518
519
520 inline double Vec4::operator * ( const Vec4& rhs ) const
521 {
522 return m_data[0] * rhs.m_data[0] + m_data[1] * rhs.m_data[1] + m_data[2] * rhs.m_data[2] + m_data[3] * rhs.m_data[3];
523 }
524
525
526 inline const Vec4 Vec4::operator | ( const Vec4& rhs ) const
527 {
528 return Vec4( m_data[0] * rhs.m_data[0],
529 m_data[1] * rhs.m_data[1],
530 m_data[2] * rhs.m_data[2],
531 m_data[3] * rhs.m_data[3] );
532 }
533
534
535 inline const Vec4 Vec4::operator * ( double rhs ) const
536 {
537 return Vec4( m_data[0] * rhs, m_data[1] * rhs, m_data[2] * rhs, m_data[3] * rhs );
538 }
539
540
541
542 inline Vec4& Vec4::operator *= ( double rhs )
543 {
544 m_data[0] = m_data[0] * rhs;
545 m_data[1] = m_data[1] * rhs;
546 m_data[2] = m_data[2] * rhs;
547 m_data[3] = m_data[3] * rhs;
548 return *this;
549 }
550
551
552 inline const Vec4 Vec4::operator / ( double rhs ) const
553 {
554 return Vec4( m_data[0] / rhs, m_data[1] / rhs, m_data[2] / rhs, m_data[3] / rhs );
555 }
556
557
558 inline Vec4& Vec4::operator /= ( double rhs )
559 {
560 m_data[0] = m_data[0] / rhs;
561 m_data[1] = m_data[1] / rhs;
562 m_data[2] = m_data[2] / rhs;
563 m_data[3] = m_data[3] / rhs;
564 return *this;
565 }
566
567
568
569 inline const Vec4 Vec4::operator + ( const Vec4& rhs ) const
570 {
571 return Vec4( m_data[0] + rhs.m_data[0], m_data[1] + rhs.m_data[1], m_data[2] + rhs.m_data[2], m_data[3] + rhs.m_data[3] );
572 }
573
574
575 inline Vec4& Vec4::operator += ( const Vec4& rhs )
576 {
577 m_data[0] += rhs.m_data[0];
578 m_data[1] += rhs.m_data[1];
579 m_data[2] += rhs.m_data[2];
580 m_data[3] += rhs.m_data[3];
581 return *this;
582 }
583
584
585 inline const Vec4 Vec4::operator - ( const Vec4& rhs ) const
586 {
587 return Vec4( m_data[0] - rhs.m_data[0], m_data[1] - rhs.m_data[1], m_data[2] - rhs.m_data[2], m_data[3] - rhs.m_data[3] );
588 }
589
590
591 inline Vec4& Vec4::operator -= ( const Vec4& rhs )
592 {
593 m_data[0] -= rhs.m_data[0];
594 m_data[1] -= rhs.m_data[1];
595 m_data[2] -= rhs.m_data[2];
596 m_data[3] -= rhs.m_data[3];
597 return *this;
598 }
599
600
601 inline const Vec4 Vec4::operator + ( const double& rhs ) const
602 {
603 return Vec4( m_data[0] + rhs, m_data[1] + rhs, m_data[2] + rhs, m_data[3] + rhs );
604 }
605
606
607 inline Vec4& Vec4::operator += ( const double& rhs )
608 {
609 m_data[0] += rhs;
610 m_data[1] += rhs;
611 m_data[2] += rhs;
612 m_data[3] += rhs;
613 return *this;
614 }
615
616
617 inline const Vec4 Vec4::operator - ( const double& rhs ) const
618 {
619 return Vec4( m_data[0] - rhs, m_data[1] - rhs, m_data[2] - rhs, m_data[3] - rhs );
620 }
621
622
623 inline Vec4& Vec4::operator -= ( const double& rhs )
624 {
625 m_data[0] -= rhs;
626 m_data[1] -= rhs;
627 m_data[2] -= rhs;
628 m_data[3] -= rhs;
629 return *this;
630 }
631
632
633 inline const Vec4 Vec4::operator - () const
634 {
635 return Vec4 ( -m_data[0], -m_data[1], -m_data[2], -m_data[3] );
636 }
637
638
639 inline double Vec4::length() const
640 {
641 return std::sqrt( length2() );
642 }
643
644
645 inline double Vec4::length2() const
646 {
647 return double(m_data[0] * m_data[0] + m_data[1] * m_data[1] + m_data[2] * m_data[2] + m_data[3] * m_data[3]);
648 }
649
650
651
652 inline double Vec4::distance(const Vec4& v2) const
653 {
654 return std::sqrt(distance2(v2));
655 }
656
657
658 inline double Vec4::normalize()
659 {
660 double norm = Vec4::length();
661 if ( norm > 0.0 ) {
662 double inv = 1.0 / norm;
663 m_data[0] = double(m_data[0] * inv);
664 m_data[1] = double(m_data[1] * inv);
665 m_data[2] = double(m_data[2] * inv);
666 m_data[3] = double(m_data[3] * inv);
667 }
668 return( norm );
669 }
670
671
672
673
674 inline Vec3 Vec4::asVec3() const
675 {
676 return Vec3( m_data[0], m_data[1], m_data[2] );
677 }
678
679
680 inline Vec4 Vec4::random(double min, double max)
681 {
682 return Vec4(agx::random(min, max), agx::random(min, max), agx::random(min, max), agx::random(min, max));
683 }
684
685
686 inline Vec4 Vec4::random(const Vec4& min, const Vec4& max)
687 {
688 return Vec4(agx::random(min[0], max[0]), agx::random(min[1], max[1]), agx::random(min[2], max[2]), agx::random(min[3], max[3]));
689 }
690
691
692 inline std::ostream& operator << ( std::ostream& output, const Vec4& v )
693 {
694 output << v[0] << " " << v[1] << " " << v[2] << " " << v[3];
695 return output;
696 }
697
698
699 inline std::string Vec4::__str__() const
700 {
701 std::ostringstream stream;
702 stream << *this;
703 return stream.str();
704 }
705
706
708 inline double operator * ( const Vec3& lhs, const Vec4& rhs )
709 {
710 return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2] + rhs[3];
711 }
712
714 inline double operator * ( const Vec4& lhs, const Vec3& rhs )
715 {
716 return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2] + lhs[3];
717 }
718
719 inline double Vec4::distance2(const Vec4& v2) const
720 {
721 Vec4 diff(m_data[0] - v2.m_data[0], m_data[1] - v2.m_data[1], m_data[2] - v2.m_data[2], m_data[3] - v2.m_data[3]);
722 return diff.length2();
723 }
724#endif // "#ifndef SWIG"
725
726
727 inline bool equivalent(const Vec4& a, const Vec4& b, double epsilon = double(MOMENTUM_EQUIVALENT_EPSILON))
728 {
729 return agx::equivalent(a[0], b[0], epsilon) &&
730 agx::equivalent(a[1], b[1], epsilon) &&
731 agx::equivalent(a[2], b[2], epsilon) &&
732 agx::equivalent(a[3], b[3], epsilon);
733 }
734
735
736 inline Vec4 absolute(const Vec4& a)
737 {
738 return Vec4(std::abs(a[0]),
739 std::abs(a[1]),
740 std::abs(a[2]),
741 std::abs(a[3]));
742 }
743
744
745 inline Vec4 clamp(const Vec4& vec, const Vec4& minimum, const Vec4& maximum)
746 {
747 Vec4 result = vec;
748 result.clamp(minimum, maximum);
749 return result;
750 }
751
752} // end of namespace momentum
753
754#endif
A 3 dimensional vector which can be used to define a point or a vector and contains basic arithmetic.
Definition: Vec3.h:40
A 4 dimensional vector and contains basic arithmetic.
Definition: Vec4.h:33
void setY(double value)
Definition: Vec4.h:484
double distance2(const Vec4 &v2) const
Definition: Vec4.h:719
bool isNaN() const
Definition: Vec4.h:508
double Type
Definition: Vec4.h:35
void setX(double value)
Definition: Vec4.h:478
Vec4 & operator-=(const Vec4 &rhs)
Unary vector subtract.
Definition: Vec4.h:591
const Vec4 operator|(const Vec4 &rhs) const
Definition: Vec4.h:526
const Vec4 operator/(double rhs) const
Divide by scalar.
Definition: Vec4.h:552
Vec4()
Default constructor.
Definition: Vec4.h:291
static Vec4 componentMax(const Vec4 &v1, const Vec4 &v2)
Definition: Vec4.h:353
double minComponent() const
Definition: Vec4.h:359
Vec3 asVec3() const
Definition: Vec4.h:674
const Vec4 operator-() const
Negation operator.
Definition: Vec4.h:633
double z() const
Definition: Vec4.h:466
int maxElement() const
Definition: Vec4.h:386
void set(double x, double y, double z, double w)
Set the elements of the vector.
Definition: Vec4.h:419
bool operator==(const Vec4 &v) const
Equality test.
Definition: Vec4.h:334
Vec4 & operator/=(double rhs)
Definition: Vec4.h:558
void clamp(const Vec4 &min, const Vec4 &max)
Clamp a vector between a lower and upper bound (per component).
Definition: Vec4.h:401
int minElement() const
Definition: Vec4.h:371
double maxComponent() const
Definition: Vec4.h:365
bool equalsZero() const
Definition: Vec4.h:411
Vec4 & operator+=(const Vec4 &rhs)
Unary vector add.
Definition: Vec4.h:575
void setZ(double value)
Definition: Vec4.h:490
bool operator!=(const Vec4 &v) const
In-equality test.
Definition: Vec4.h:340
double w() const
Definition: Vec4.h:472
bool isFinite() const
Definition: Vec4.h:514
double y() const
Definition: Vec4.h:461
bool isValid() const
Definition: Vec4.h:502
void setW(double value)
Definition: Vec4.h:496
double length() const
Definition: Vec4.h:639
static Vec4 componentMin(const Vec4 &v1, const Vec4 &v2)
Definition: Vec4.h:347
double normalize()
Normalize the vector so that it has length unity.
Definition: Vec4.h:658
static Vec4 random(double min, double max)
Definition: Vec4.h:680
double x() const
Definition: Vec4.h:455
const Vec4 operator+(const Vec4 &rhs) const
Binary vector add.
Definition: Vec4.h:569
double & operator[](int i)
Definition: Vec4.h:443
Vec4 & operator*=(double rhs)
Definition: Vec4.h:542
double operator*(const Vec4 &rhs) const
Definition: Vec4.h:520
double length2() const
Definition: Vec4.h:645
double distance(const Vec4 &v2) const
Definition: Vec4.h:652
Namespace for Momentum Scripting API.
Definition: AffineMatrix4x4.h:29
const double MOMENTUM_EQUIVALENT_EPSILON
Definition: momentum_math.h:30
std::ostream & operator<<(std::ostream &os, const EulerAngles &e)
Definition: EulerAngles.h:358
Vec3 max(const Vec3 &lhs, const Vec3 &rhs)
Definition: Vec3.h:804
Vec3 operator*(const Vec3 &v, const Matrix3x3 &m)
Definition: Matrix3x3.h:746
Vec3 min(const Vec3 &lhs, const Vec3 &rhs)
Definition: Vec3.h:810
Vec3 absolute(const Vec3 &a)
Definition: Vec3.h:775
Vec3 clamp(const Vec3 &vec, const Vec3 &minimum, const Vec3 &maximum)
Definition: Vec3.h:841
bool equivalent(const Matrix3x3 &a, const Matrix3x3 &b, double epsilon=1e-6)
Definition: Matrix3x3.h:905
Definition: Statistic.h:23