MDL SDK API nvidia_logo_transpbg.gif Up
vector.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2025 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
9
10#ifndef MI_MATH_VECTOR_H
11#define MI_MATH_VECTOR_H
12
13#include <mi/base/types.h>
14#include <mi/math/assert.h>
15#include <mi/math/function.h>
16
17namespace mi {
18
19namespace math {
20
38};
39
40
41// Color and Vector can be converted into each other. To avoid cyclic dependencies among the
42// headers, the Color_struct class is already defined here.
43
44//------- POD struct that provides storage for color elements --------
45
67{
76};
77
78
105//------- POD struct that provides storage for vector elements --------
106
133template <typename T, Size DIM>
135{
136 T elements[DIM];
137};
138
140template <typename T> struct Vector_struct<T, 1>
141{
142 T x;
143};
144
146template <typename T> struct Vector_struct<T, 2>
147{
148 T x;
149 T y;
150};
151
153template <typename T> struct Vector_struct<T, 3>
154{
155 T x;
156 T y;
157 T z;
158};
159
161template <typename T> struct Vector_struct<T, 4>
162{
163 T x;
164 T y;
165 T z;
166 T w;
167};
168
169
170//------ Indirect access to vector storage base ptr to keep Vector_struct a POD --
171
173template <typename T, Size DIM>
175{
176 return vec.elements;
177}
178
180template <typename T, Size DIM>
181inline const T* vector_base_ptr( const Vector_struct<T,DIM>& vec)
182{
183 return vec.elements;
184}
185
187template <typename T>
189{
190 return &vec.x;
191}
192
194template <typename T>
195inline const T* vector_base_ptr( const Vector_struct<T,1>& vec)
196{
197 return &vec.x;
198}
199
201template <typename T>
203{
204 return &vec.x;
205}
206
208template <typename T>
209inline const T* vector_base_ptr( const Vector_struct<T,2>& vec)
210{
211 return &vec.x;
212}
213
215template <typename T>
217{
218 return &vec.x;
219}
220
222template <typename T>
223inline const T* vector_base_ptr( const Vector_struct<T,3>& vec)
224{
225 return &vec.x;
226}
227
229template <typename T>
231{
232 return &vec.x;
233}
234
236template <typename T>
237inline const T* vector_base_ptr( const Vector_struct<T,4>& vec)
238{
239 return &vec.x;
240}
241
242 // end group mi_math_vector_struct
244
245
246//------ Generic Vector Class -------------------------------------------------
247
284template < class T, Size DIM>
285class Vector : public Vector_struct<T, DIM> //-V690 PVS
286{
287public:
290
291 using value_type = T;
292 using size_type = Size;
294 using pointer = T*;
295 using const_pointer = const T*;
296 using reference = T&;
297 using const_reference = const T&;
298
299 static constexpr Size DIMENSION = DIM;
300 static constexpr Size SIZE = DIM;
301
303 static constexpr inline Size size() { return SIZE; }
304
306 static constexpr inline Size max_size() { return SIZE; }
307
309 inline T* begin() { return vector_base_ptr( *this); }
310
312 inline const T* begin() const { return vector_base_ptr( *this); }
313
317 inline T* end() { return begin() + DIM; }
318
322 inline const T* end() const { return begin() + DIM; }
323
325 inline Vector()
326 {
327#ifndef NDEBUG
328 // In debug mode, default-constructed vectors are initialized with signaling NaNs or, if not
329 // applicable, with a maximum value to increase the chances of diagnosing incorrect use of
330 // an uninitialized vector.
331 using Traits = mi::base::numeric_traits<T>;
332 T v = (Traits::has_signaling_NaN) ? Traits::signaling_NaN()
333 : Traits::max MI_PREVENT_MACRO_EXPAND ();
334 for( Size i(0u); i < DIM; ++i)
335 (*this)[i] = v;
336#endif
337 }
338
340 Vector( const Vector<T,DIM>& vec ) = default;
341
343 inline Vector( const Vector_struct<T,DIM>& vec )
344 {
345 for( Size i(0u); i < DIM; ++i)
346 begin()[i] = vector_base_ptr(vec)[i];
347 }
348
349#if (__cplusplus >= 201402L)
350private:
351 template<Size s>
352 constexpr inline void init( std::integral_constant<Size,s>, T v)
353 {
354 for( Size i = 0; i < DIM; ++i)
355 Pod_type::elements[i] = v;
356 }
357
358 constexpr inline void init( std::integral_constant<Size,1>, T v)
359 { Pod_type::x = v; }
360
361 constexpr inline void init( std::integral_constant<Size,2>, T v)
362 { Pod_type::x = Pod_type::y = v; }
363
364 constexpr inline void init( std::integral_constant<Size,3>, T v)
365 { Pod_type::x = Pod_type::y = Pod_type::z = v; }
366
367 constexpr inline void init( std::integral_constant<Size,4>, T v)
368 { Pod_type::x = Pod_type::y = Pod_type::z = Pod_type::w = v; }
369
370public:
372 constexpr inline explicit Vector(T v)
373 {
374 init( std::integral_constant<Size,DIM>(), v);
375 }
376#else
377 inline explicit Vector(T v)
378 {
379 for( Size i(0u); i < DIM; ++i)
380 begin()[i] = v;
381 }
382#endif
383
397 template <typename Iterator>
398 inline Vector(From_iterator_tag, Iterator p)
399 {
400 for( Size i(0u); i < DIM; ++i, ++p)
401 begin()[i] = *p;
402 }
403
415 template <typename T2>
416 inline explicit Vector( T2 const (& array)[DIM])
417 {
418 for( Size i(0u); i < DIM; ++i)
419 begin()[i] = array[i];
420 }
421
424 template <typename T2>
425 inline explicit Vector( const Vector<T2,DIM>& other)
426 {
427 for( Size i(0u); i < DIM; ++i)
428 begin()[i] = T(other[i]);
429 }
430
433 template <typename T2>
434 inline explicit Vector( const Vector_struct<T2,DIM>& other)
435 {
436 for( Size i(0u); i < DIM; ++i)
437 begin()[i] = T(vector_base_ptr(other)[i]);
438 }
439
444#if (__cplusplus >= 201402L)
445 constexpr
446#endif
447 inline Vector(T v1, T v2) : Vector_struct<T, DIM>{v1,v2}
448 {
449 mi_static_assert(DIM == 2);
450 }
451
456#if (__cplusplus >= 201402L)
457 constexpr
458#endif
459 inline Vector(T v1, T v2, T v3) : Vector_struct<T, DIM>{v1,v2,v3}
460 {
461 mi_static_assert(DIM == 3);
462 }
463
468#if (__cplusplus >= 201402L)
469 constexpr
470#endif
471 inline Vector(T v1, const Vector<T,2>& v2) : Vector_struct<T, DIM>{v1,v2.x,v2.y}
472 {
473 mi_static_assert(DIM == 3);
474 }
475
480#if (__cplusplus >= 201402L)
481 constexpr
482#endif
483 inline Vector(const Vector<T,2>& v1, T v2) : Vector_struct<T, DIM>{v1.x,v1.y,v2}
484 {
485 mi_static_assert(DIM == 3);
486 }
487
492#if (__cplusplus >= 201402L)
493 constexpr
494#endif
495 inline Vector(T v1, T v2, T v3, T v4) : Vector_struct<T, DIM>{v1,v2,v3,v4}
496 {
497 mi_static_assert(DIM == 4);
498 }
499
504#if (__cplusplus >= 201402L)
505 constexpr
506#endif
507 inline Vector(T v1, T v2, const Vector<T,2>& v3) : Vector_struct<T, DIM>{v1,v2,v3.x,v3.y}
508 {
509 mi_static_assert(DIM == 4);
510 }
511
516#if (__cplusplus >= 201402L)
517 constexpr
518#endif
519 inline Vector(T v1, const Vector<T,2>& v2, T v3) : Vector_struct<T, DIM>{v1,v2.x,v2.y,v3}
520 {
521 mi_static_assert(DIM == 4);
522 }
523
528#if (__cplusplus >= 201402L)
529 constexpr
530#endif
531 inline Vector(const Vector<T,2>& v1, T v2, T v3) : Vector_struct<T, DIM>{v1.x,v1.y,v2,v3}
532 {
533 mi_static_assert(DIM == 4);
534 }
535
540#if (__cplusplus >= 201402L)
541 constexpr
542#endif
543 inline Vector(const Vector<T,2>& v1, const Vector<T,2>& v2) : Vector_struct<T, DIM>{v1.x,v1.y,v2.x,v2.y}
544 {
545 mi_static_assert(DIM == 4);
546 }
547
552#if (__cplusplus >= 201402L)
553 constexpr
554#endif
555 inline Vector(T v1, const Vector<T,3>& v2) : Vector_struct<T, DIM>{v1,v2.x,v2.y,v2.z}
556 {
557 mi_static_assert(DIM == 4);
558 }
559
564#if (__cplusplus >= 201402L)
565 constexpr
566#endif
567 inline Vector(const Vector<T,3>& v1, T v2) : Vector_struct<T, DIM>{v1.x,v1.y,v1.z,v2}
568 {
569 mi_static_assert(DIM == 4);
570 }
571
576#if (__cplusplus >= 201402L)
577 constexpr
578#endif
579 inline explicit Vector( const Color_struct& color) : Vector_struct<T, DIM>{static_cast<T>(color.r),static_cast<T>(color.g),static_cast<T>(color.b),static_cast<T>(color.a)}
580 {
581 mi_static_assert(DIM == 4);
582 }
583
585 Vector& operator=( const Vector& other) = default;
586
588#if (__cplusplus >= 201402L)
589 constexpr
590#endif
591 inline Vector& operator=( T s)
592 {
593#if (__cplusplus >= 201402L)
594 init(std::integral_constant<Size,DIM>(), s);
595#else
596 for( Size i(0u); i < DIM; ++i)
597 begin()[i] = s;
598#endif
599 return *this;
600 }
605#if (__cplusplus >= 201402L)
606 constexpr
607#endif
608 inline Vector& operator=( const Color_struct& color)
609 {
610 mi_static_assert(DIM == 4);
611 Pod_type::x = color.r;
612 Pod_type::y = color.g;
613 Pod_type::z = color.b;
614 Pod_type::w = color.a;
615 return *this;
616 }
617
621 inline T& operator[](Size i)
622 {
623 mi_math_assert_msg(i < DIM, "precondition");
624 return begin()[i];
625 }
626
630 inline const T& operator[](Size i) const
631 {
632 mi_math_assert_msg(i < DIM, "precondition");
633 return begin()[i];
634 }
635
639 inline const T& get(Size i) const
640 {
641 mi_math_assert_msg(i < DIM, "precondition");
642 return begin()[i];
643 }
644
648 inline void set(Size i, T value)
649 {
650 mi_math_assert_msg(i < DIM, "precondition");
651 begin()[i] = value;
652 }
653
654
655
662 inline bool normalize()
663 {
664 const T rec_length = T(1) / length( *this);
665 const bool result = isfinite( rec_length); //-V601 PVS
666 if( result)
667 (*this) *= rec_length;
668 return result;
669 }
670
671
672 //------ Free comparison operators ==, !=, <, <=, >, >= for vectors --------
673
675 inline bool operator==( Vector<T,DIM> rhs) const
676 {
677 return is_equal( *this, rhs);
678 }
679
681 inline bool operator!=( Vector<T,DIM> rhs) const
682 {
683 return is_not_equal( *this, rhs);
684 }
685
689 inline bool operator<( Vector<T,DIM> rhs) const
690 {
691 return lexicographically_less( *this, rhs);
692 }
693
697 inline bool operator<=( Vector<T,DIM> rhs) const
698 {
699 return lexicographically_less_or_equal( *this, rhs);
700 }
701
705 inline bool operator>( Vector<T,DIM> rhs) const
706 {
707 return lexicographically_greater( *this, rhs);
708 }
709
713 inline bool operator>=( Vector<T,DIM> rhs) const
714 {
715 return lexicographically_greater_or_equal( *this, rhs);
716 }
717};
718
719
720//------ Free operators +=, -=, *=, /=, +, -, *, and / for vectors -------------
721
723template <typename T, Size DIM>
725 Vector<T,DIM>& lhs,
726 const Vector_struct<T,DIM>& rhs)
727{
728 for( Size i(0u); i < DIM; ++i)
729 lhs.elements[i] += rhs.elements[i];
730 return lhs;
731}
732template <typename T>
733inline Vector<T,1>& operator+=(
734 Vector<T,1>& lhs,
735 const Vector_struct<T,1>& rhs)
736{
737 lhs.x += rhs.x;
738 return lhs;
739}
740template <typename T>
741inline Vector<T,2>& operator+=(
742 Vector<T,2>& lhs,
743 const Vector_struct<T,2>& rhs)
744{
745 lhs.x += rhs.x;
746 lhs.y += rhs.y;
747 return lhs;
748}
749template <typename T>
750inline Vector<T,3>& operator+=(
751 Vector<T,3>& lhs,
752 const Vector_struct<T,3>& rhs)
753{
754 lhs.x += rhs.x;
755 lhs.y += rhs.y;
756 lhs.z += rhs.z;
757 return lhs;
758}
759template <typename T>
760inline Vector<T,4>& operator+=(
761 Vector<T,4>& lhs,
762 const Vector_struct<T,4>& rhs)
763{
764 lhs.x += rhs.x;
765 lhs.y += rhs.y;
766 lhs.z += rhs.z;
767 lhs.w += rhs.w;
768 return lhs;
769}
770
772template <typename T, Size DIM>
774 Vector<T,DIM>& lhs,
775 const Vector_struct<T,DIM>& rhs)
776{
777 for( Size i(0u); i < DIM; ++i)
778 lhs.elements[i] -= rhs.elements[i];
779 return lhs;
780}
781template <typename T>
782inline Vector<T,1>& operator-=(
783 Vector<T,1>& lhs,
784 const Vector_struct<T,1>& rhs)
785{
786 lhs.x -= rhs.x;
787 return lhs;
788}
789template <typename T>
790inline Vector<T,2>& operator-=(
791 Vector<T,2>& lhs,
792 const Vector_struct<T,2>& rhs)
793{
794 lhs.x -= rhs.x;
795 lhs.y -= rhs.y;
796 return lhs;
797}
798template <typename T>
799inline Vector<T,3>& operator-=(
800 Vector<T,3>& lhs,
801 const Vector_struct<T,3>& rhs)
802{
803 lhs.x -= rhs.x;
804 lhs.y -= rhs.y;
805 lhs.z -= rhs.z;
806 return lhs;
807}
808template <typename T>
809inline Vector<T,4>& operator-=(
810 Vector<T,4>& lhs,
811 const Vector_struct<T,4>& rhs)
812{
813 lhs.x -= rhs.x;
814 lhs.y -= rhs.y;
815 lhs.z -= rhs.z;
816 lhs.w -= rhs.w;
817 return lhs;
818}
819
821template <typename T, Size DIM>
823 Vector<T,DIM>& lhs,
824 const Vector_struct<T,DIM>& rhs)
825{
826 for( Size i(0u); i < DIM; ++i)
827 lhs.elements[i] *= rhs.elements[i];
828 return lhs;
829}
830template <typename T>
831inline Vector<T,1>& operator*=(
832 Vector<T,1>& lhs,
833 const Vector_struct<T,1>& rhs)
834{
835 lhs.x *= rhs.x;
836 return lhs;
837}
838template <typename T>
839inline Vector<T,2>& operator*=(
840 Vector<T,2>& lhs,
841 const Vector_struct<T,2>& rhs)
842{
843 lhs.x *= rhs.x;
844 lhs.y *= rhs.y;
845 return lhs;
846}
847template <typename T>
848inline Vector<T,3>& operator*=(
849 Vector<T,3>& lhs,
850 const Vector_struct<T,3>& rhs)
851{
852 lhs.x *= rhs.x;
853 lhs.y *= rhs.y;
854 lhs.z *= rhs.z;
855 return lhs;
856}
857template <typename T>
858inline Vector<T,4>& operator*=(
859 Vector<T,4>& lhs,
860 const Vector_struct<T,4>& rhs)
861{
862 lhs.x *= rhs.x;
863 lhs.y *= rhs.y;
864 lhs.z *= rhs.z;
865 lhs.w *= rhs.w;
866 return lhs;
867}
868
871template <typename T, Size DIM>
873 Vector<T,DIM>& lhs,
874 const Vector_struct<T,DIM>& rhs)
875{
876 for( Size i(0u); i < DIM; ++i)
877 lhs[i] %= vector_base_ptr(rhs)[i];
878 return lhs;
879}
880
882template <typename T, typename U, Size DIM>
884 Vector<T,DIM>& lhs,
885 const Vector_struct<U,DIM>& rhs)
886{
887 for( Size i(0u); i < DIM; ++i)
888 lhs[i] = T(lhs[i] / vector_base_ptr(rhs)[i]);
889 return lhs;
890}
891
893template <typename T, Size DIM>
895 const Vector_struct<T,DIM>& lhs,
896 const Vector_struct<T,DIM>& rhs)
897{
898 Vector<T,DIM> tmp( lhs);
899 return tmp += rhs;
900}
901
903template <typename T, Size DIM>
905 const Vector_struct<T,DIM>& lhs,
906 const Vector_struct<T,DIM>& rhs)
907{
908 Vector<T,DIM> tmp( lhs);
909 return tmp -= rhs;
910}
911
913template <typename T, Size DIM>
915 const Vector_struct<T,DIM>& lhs,
916 const Vector_struct<T,DIM>& rhs)
917{
918 Vector<T,DIM> tmp( lhs);
919 return tmp *= rhs;
920}
921
924template <typename T, Size DIM>
926 const Vector_struct<T,DIM>& lhs,
927 const Vector_struct<T,DIM>& rhs)
928{
929 Vector<T,DIM> tmp( lhs);
930 return tmp %= rhs;
931}
932
934template <typename T, typename U, Size DIM>
936 const Vector_struct<T,DIM>& lhs,
937 const Vector_struct<U,DIM>& rhs)
938{
939 Vector<T,DIM> tmp(lhs);
940 return tmp /= rhs;
941}
942
944template <typename T, Size DIM>
946{
947 Vector<T,DIM> tmp;
948 for( Size i(0u); i < DIM; ++i)
949 tmp.elements[i] = -v.elements[i];
950 return tmp;
951}
952template <typename T>
953inline Vector<T,1> operator-( const Vector_struct<T,1>& v)
954{
955 return Vector<T,1>( -v.x);
956}
957template <typename T>
958inline Vector<T,2> operator-( const Vector_struct<T,2>& v)
959{
960 return Vector<T,2>( -v.x, -v.y);
961}
962template <typename T>
963inline Vector<T,3> operator-( const Vector_struct<T,3>& v)
964{
965 return Vector<T,3>( -v.x, -v.y, -v.z);
966}
967template <typename T>
968inline Vector<T,4> operator-( const Vector_struct<T,4>& v)
969{
970 return Vector<T,4>( -v.x, -v.y, -v.z, -v.w);
971}
972
973
974//------ Resolve ambiguity with hybrid operators -------------
975// (Those operators should really be restricted to scalar types but that requires
976// modern C++ which some users of this headers don't enable.)
977
979template <typename T, Size DIM>
981 Vector<T,DIM>& lhs,
982 const Vector<T,DIM>& rhs)
983{
984 return lhs += static_cast<const Vector_struct<T,DIM>&>(rhs);
985}
986
988template <typename T, Size DIM>
990 Vector<T,DIM>& lhs,
991 const Vector<T,DIM>& rhs)
992{
993 return lhs -= static_cast<const Vector_struct<T,DIM>&>(rhs);
994}
995
997template <typename T, Size DIM>
999 Vector<T,DIM>& lhs,
1000 const Vector<T,DIM>& rhs)
1001{
1002 return lhs *= static_cast<const Vector_struct<T,DIM>&>(rhs);
1003}
1004template <typename T>
1005inline Vector<T,1>& operator*=(
1006 Vector<T,1>& lhs,
1007 const Vector<T,1>& rhs)
1008{
1009 return lhs *= static_cast<const Vector_struct<T,1>&>(rhs);
1010}
1011template <typename T>
1012inline Vector<T,2>& operator*=(
1013 Vector<T,2>& lhs,
1014 const Vector<T,2>& rhs)
1015{
1016 return lhs *= static_cast<const Vector_struct<T,2>&>(rhs);
1017}
1018template <typename T>
1019inline Vector<T,3>& operator*=(
1020 Vector<T,3>& lhs,
1021 const Vector<T,3>& rhs)
1022{
1023 return lhs *= static_cast<const Vector_struct<T,3>&>(rhs);
1024}
1025template <typename T>
1026inline Vector<T,4>& operator*=(
1027 Vector<T,4>& lhs,
1028 const Vector<T,4>& rhs)
1029{
1030 return lhs *= static_cast<const Vector_struct<T,4>&>(rhs);
1031}
1032
1035template <typename T, Size DIM>
1037 Vector<T,DIM>& lhs,
1038 const Vector<T,DIM>& rhs)
1039{
1040 return lhs %= static_cast<const Vector_struct<T,DIM>&>(rhs);
1041}
1042
1044template <typename T, typename U, Size DIM>
1046 Vector<T,DIM>& lhs,
1047 const Vector<U,DIM>& rhs)
1048{
1049 return lhs /= static_cast<const Vector_struct<U,DIM>&>(rhs);
1050}
1051
1053template <typename T, Size DIM>
1055 const Vector<T,DIM>& lhs,
1056 const Vector<T,DIM>& rhs)
1057{
1058 return lhs + static_cast<const Vector_struct<T,DIM>&>(rhs);
1059}
1060
1062template <typename T, Size DIM>
1064 const Vector<T,DIM>& lhs,
1065 const Vector<T,DIM>& rhs)
1066{
1067 return lhs - static_cast<const Vector_struct<T,DIM>&>(rhs);
1068}
1069
1071template <typename T, Size DIM>
1073 const Vector<T,DIM>& lhs,
1074 const Vector<T,DIM>& rhs)
1075{
1076 return lhs * static_cast<const Vector_struct<T,DIM>&>(rhs);
1077}
1078
1081template <typename T, Size DIM>
1083 const Vector<T,DIM>& lhs,
1084 const Vector<T,DIM>& rhs)
1085{
1086 return lhs % static_cast<const Vector_struct<T,DIM>&>(rhs);
1087}
1088
1090template <typename T, typename U, Size DIM>
1092 const Vector<T,DIM>& lhs,
1093 const Vector<U,DIM>& rhs)
1094{
1095 return lhs / static_cast<const Vector_struct<U,DIM>&>(rhs);
1096}
1097
1099template <typename T, Size DIM>
1101{
1102 return -static_cast<const Vector_struct<T,DIM>&>(v);
1103}
1104
1105
1106//------ Free operator *=, /=, *, and / definitions for scalars ---------------
1107
1110template <typename T, typename TT, Size DIM>
1112 Vector<T,DIM>& v,
1113 TT s)
1114{
1115 for( Size i(0u); i < DIM; ++i)
1116 v.elements[i] = T(v.elements[i] * s);
1117 return v;
1118}
1119template <typename T, typename TT>
1120inline Vector<T,1>& operator*=(
1121 Vector<T,1>& v,
1122 TT s)
1123{
1124 v.x = T(v.x * s);
1125 return v;
1126}
1127template <typename T, typename TT>
1128inline Vector<T,2>& operator*=(
1129 Vector<T,2>& v,
1130 TT s)
1131{
1132 v.x = T(v.x * s);
1133 v.y = T(v.y * s);
1134 return v;
1135}
1136template <typename T, typename TT>
1137inline Vector<T,3>& operator*=(
1138 Vector<T,3>& v,
1139 TT s)
1140{
1141 v.x = T(v.x * s);
1142 v.y = T(v.y * s);
1143 v.z = T(v.z * s);
1144 return v;
1145}
1146template <typename T, typename TT>
1147inline Vector<T,4>& operator*=(
1148 Vector<T,4>& v,
1149 TT s)
1150{
1151 v.x = T(v.x * s);
1152 v.y = T(v.y * s);
1153 v.z = T(v.z * s);
1154 v.w = T(v.w * s);
1155 return v;
1156}
1157
1161template <typename T, typename TT, Size DIM>
1163 Vector<T,DIM>& v,
1164 TT s)
1165{
1166 for( Size i(0u); i < DIM; ++i)
1167 v[i] = T(v[i] % s);
1168 return v;
1169}
1170
1172template <typename T, typename TT, Size DIM>
1174 Vector<T,DIM>& v,
1175 TT s)
1176{
1177 for( Size i(0u); i < DIM; ++i)
1178 v[i] = T(v[i] / s);
1179 return v;
1180}
1181
1183template <typename T, typename TT, Size DIM>
1185 const Vector_struct<T,DIM>& v,
1186 TT s)
1187{
1188 Vector<T,DIM> tmp( v);
1189 return tmp *= s;
1190}
1191
1193template <typename T, typename TT, Size DIM>
1195 TT s,
1196 const Vector_struct<T,DIM>& v)
1197{
1198 Vector<T,DIM> tmp( v);
1199 return tmp *= s;
1200}
1201
1205template <typename T, typename TT, Size DIM>
1207 const Vector_struct<T,DIM>& v,
1208 TT s)
1209{
1210 Vector<T,DIM> tmp( v);
1211 return tmp %= s;
1212}
1213
1215template <typename T, typename TT, Size DIM>
1217 const Vector_struct<T,DIM>& v,
1218 TT s)
1219{
1220 Vector<T,DIM> tmp( v);
1221 return tmp /= s;
1222}
1223
1224
1225//------ Free operator ++, -- for vectors -------------------------------------
1226
1228template <typename T, Size DIM>
1230{
1232 return vec;
1233}
1234
1236template <typename T, Size DIM>
1238{
1240 return vec;
1241}
1242
1243
1244//------ Free operators !, &&, ||, ^ for bool vectors and bool scalars ---------
1245
1247template <Size DIM>
1249 const Vector<bool,DIM>& lhs,
1250 const Vector<bool,DIM>& rhs)
1251{
1252 Vector<bool,DIM> result;
1253 general::transform( lhs, rhs, result, functor::Operator_and_and());
1254 return result;
1255}
1256
1258template <Size DIM>
1260 bool lhs,
1261 const Vector<bool,DIM>& rhs)
1262{
1263 Vector<bool,DIM> result;
1265 return result;
1266}
1267
1269template <Size DIM>
1271 const Vector<bool,DIM>& lhs,
1272 bool rhs)
1273{
1274 Vector<bool,DIM> result;
1276 return result;
1277}
1278
1280template <Size DIM>
1282 const Vector<bool,DIM>& lhs,
1283 const Vector<bool,DIM>& rhs)
1284{
1285 Vector<bool,DIM> result;
1286 general::transform(lhs, rhs, result, functor::Operator_or_or());
1287 return result;
1288}
1289
1291template <Size DIM>
1293 bool lhs,
1294 const Vector<bool,DIM>& rhs)
1295{
1296 Vector<bool,DIM> result;
1298 return result;
1299}
1300
1302template <Size DIM>
1304 const Vector<bool,DIM>& lhs,
1305 bool rhs)
1306{
1307 Vector<bool,DIM> result;
1309 return result;
1310}
1311
1313template <Size DIM>
1315 const Vector<bool,DIM>& lhs,
1316 const Vector<bool,DIM>& rhs)
1317{
1318 Vector<bool,DIM> result;
1319 general::transform( lhs, rhs, result, functor::Operator_xor());
1320 return result;
1321}
1322
1324template <Size DIM>
1326 bool lhs,
1327 const Vector<bool,DIM>& rhs)
1328{
1329 Vector<bool,DIM> result;
1331 return result;
1332}
1333
1335template <Size DIM>
1337 const Vector<bool,DIM>& lhs,
1338 bool rhs)
1339{
1340 Vector<bool,DIM> result;
1342 return result;
1343}
1344
1346template <Size DIM>
1348 const Vector<bool,DIM>& vec)
1349{
1350 Vector<bool,DIM> result;
1352 return result;
1353}
1354
1355
1356//------ Elementwise comparison operators returning a bool vector. ------------
1357
1359template <typename T, Size DIM>
1361 const Vector<T,DIM>& lhs,
1362 const Vector<T,DIM>& rhs)
1363{
1364 Vector<bool,DIM> result;
1366 return result;
1367}
1368
1370template <typename T, Size DIM>
1372 const Vector<T,DIM>& lhs,
1373 const Vector<T,DIM>& rhs)
1374{
1375 Vector<bool,DIM> result;
1377 return result;
1378}
1379
1381template <typename T, Size DIM>
1383 const Vector<T,DIM>& lhs,
1384 const Vector<T,DIM>& rhs)
1385{
1386 Vector<bool,DIM> result;
1387 general::transform( lhs, rhs, result,functor::Operator_less());
1388 return result;
1389}
1390
1392template <typename T, Size DIM>
1394 const Vector<T,DIM>& lhs,
1395 const Vector<T,DIM>& rhs)
1396{
1397 Vector<bool,DIM> result;
1399 return result;
1400}
1401
1403template <typename T, Size DIM>
1405 const Vector<T,DIM>& lhs,
1406 const Vector<T,DIM>& rhs)
1407{
1408 Vector<bool,DIM> result;
1409 general::transform( lhs, rhs, result,functor::Operator_greater());
1410 return result;
1411}
1412
1414template <typename T, Size DIM>
1416 const Vector<T,DIM>& lhs,
1417 const Vector<T,DIM>& rhs)
1418{
1419 Vector<bool,DIM> result;
1421 return result;
1422}
1423
1424
1425//------ Function Overloads for Vector Algorithms -----------------------------
1426
1428template <typename T, Size DIM>
1430{
1431 Vector<T,DIM> result;
1432 for( Size i = 0; i != DIM; ++i)
1433 result[i] = abs( vector_base_ptr(v)[i]);
1434 return result;
1435}
1436
1438template <typename T, Size DIM>
1440{
1441 Vector<T,DIM> result;
1442 for( Size i = 0; i != DIM; ++i)
1443 result[i] = acos( vector_base_ptr(v)[i]);
1444 return result;
1445}
1446
1448template <typename T, Size DIM>
1449inline bool all( const Vector_struct<T,DIM>& v)
1450{
1451 for( Size i = 0; i != DIM; ++i)
1452 if( !all( vector_base_ptr(v)[i]))
1453 return false;
1454 return true;
1455}
1456
1458template <typename T, Size DIM>
1459inline bool any( const Vector_struct<T,DIM>& v)
1460{
1461 for( Size i = 0; i != DIM; ++i)
1462 if( any( vector_base_ptr(v)[i]))
1463 return true;
1464 return false;
1465}
1466
1468template <typename T, Size DIM>
1470{
1471 Vector<T,DIM> result;
1472 for( Size i = 0; i != DIM; ++i)
1473 result[i] = asin( vector_base_ptr(v)[i]);
1474 return result;
1475}
1476
1478template <typename T, Size DIM>
1480{
1481 Vector<T,DIM> result;
1482 for( Size i = 0; i != DIM; ++i)
1483 result[i] = atan( vector_base_ptr(v)[i]);
1484 return result;
1485}
1486
1490template <typename T, Size DIM>
1492{
1493 Vector<T,DIM> result;
1494 for( Size i = 0; i != DIM; ++i)
1495 result[i] = atan2( vector_base_ptr(v)[i], vector_base_ptr(w)[i]);
1496 return result;
1497}
1498
1501template <typename T, Size DIM>
1503{
1504 Vector<T,DIM> result;
1505 for( Size i = 0; i != DIM; ++i)
1506 result[i] = ceil( vector_base_ptr(v)[i]);
1507 return result;
1508}
1509
1511template <typename T, Size DIM>
1513 const Vector_struct<T,DIM>& v,
1514 const Vector_struct<T,DIM>& low,
1515 const Vector_struct<T,DIM>& high)
1516{
1517 Vector<T,DIM> result;
1518 for( Size i = 0u; i < DIM; ++i)
1519 result[i] = clamp(
1520 vector_base_ptr(v)[i], vector_base_ptr(low)[i], vector_base_ptr(high)[i]);
1521 return result;
1522}
1523
1525template <typename T, Size DIM>
1527 const Vector_struct<T,DIM>& v,
1528 const Vector_struct<T,DIM>& low,
1529 T high)
1530{
1531 Vector<T,DIM> result;
1532 for( Size i = 0u; i < DIM; ++i)
1533 result[i] = clamp( vector_base_ptr(v)[i], vector_base_ptr(low)[i], high);
1534 return result;
1535}
1536
1538template <typename T, Size DIM>
1540 const Vector_struct<T,DIM>& v,
1541 T low,
1542 const Vector_struct<T,DIM>& high)
1543{
1544 Vector<T,DIM> result;
1545 for( Size i = 0u; i < DIM; ++i)
1546 result[i] = clamp( vector_base_ptr(v)[i], low, vector_base_ptr(high)[i]);
1547 return result;
1548}
1549
1551template <typename T, Size DIM>
1553 const Vector_struct<T,DIM>& v,
1554 T low,
1555 T high)
1556{
1557 Vector<T,DIM> result;
1558 for( Size i = 0u; i < DIM; ++i)
1559 result[i] = clamp( vector_base_ptr(v)[i], low, high);
1560 return result;
1561}
1562
1564template <typename T, Size DIM>
1566{
1567 Vector<T,DIM> result;
1568 for( Size i = 0; i != DIM; ++i)
1569 result[i] = cos( vector_base_ptr(v)[i]);
1570 return result;
1571}
1572
1574template <typename T, Size DIM>
1576{
1577 Vector<T,DIM> result;
1578 for( Size i = 0; i != DIM; ++i)
1579 result[i] = degrees( vector_base_ptr(v)[i]);
1580 return result;
1581}
1582
1584template <typename T, Size DIM>
1586 const Vector_struct<T,DIM>& lhs,
1587 const Vector_struct<T,DIM>& rhs)
1588{
1589 Vector<T,DIM> r;
1590 for( Size i(0u); i < Vector<T,DIM>::DIMENSION; ++i)
1591 r[i] = base::max MI_PREVENT_MACRO_EXPAND (
1592 vector_base_ptr(lhs)[i], vector_base_ptr(rhs)[i] );
1593 return r;
1594}
1595
1597template <typename T, Size DIM>
1599 const Vector_struct<T,DIM>& lhs,
1600 const Vector_struct<T,DIM>& rhs)
1601{
1602 Vector<T,DIM> r;
1603 for( Size i(0u); i < Vector<T,DIM>::DIMENSION; ++i)
1604 r[i] = base::min MI_PREVENT_MACRO_EXPAND (
1605 vector_base_ptr(lhs)[i], vector_base_ptr(rhs)[i] );
1606 return r;
1607}
1608
1610template <typename T, Size DIM>
1612{
1613 Vector<T,DIM> result;
1614 for( Size i = 0; i != DIM; ++i)
1615 result[i] = exp( vector_base_ptr(v)[i]);
1616 return result;
1617}
1618
1620template <typename T, Size DIM>
1622{
1623 Vector<T,DIM> result;
1624 for( Size i = 0; i != DIM; ++i)
1625 result[i] = exp2( vector_base_ptr(v)[i]);
1626 return result;
1627}
1628
1631template <typename T, Size DIM>
1633{
1634 Vector<T,DIM> result;
1635 for( Size i = 0; i != DIM; ++i)
1636 result[i] = floor( vector_base_ptr(v)[i]);
1637 return result;
1638}
1639
1643template <typename T, Size DIM>
1645{
1646 Vector<T,DIM> result;
1647 for( Size i = 0; i != DIM; ++i)
1648 result[i] = fmod( vector_base_ptr(a)[i], vector_base_ptr(b)[i]);
1649 return result;
1650}
1651
1655template <typename T, Size DIM>
1657{
1658 Vector<T,DIM> result;
1659 for( Size i = 0; i != DIM; ++i)
1660 result[i] = fmod( vector_base_ptr(a)[i], b);
1661 return result;
1662}
1663
1665template <typename T, Size DIM>
1667{
1668 Vector<T,DIM> result;
1669 for( Size i = 0; i != DIM; ++i)
1670 result[i] = frac( vector_base_ptr(v)[i]);
1671 return result;
1672}
1673
1675template <typename T, Size DIM>
1677 const Vector_struct<T,DIM>& left,
1678 const Vector_struct<T,DIM>& right,
1679 T e)
1680{
1681 for( Size i = 0u; i < DIM; ++i)
1682 if( !is_approx_equal( vector_base_ptr(left)[i], vector_base_ptr(right)[i], e))
1683 return false;
1684 return true;
1685}
1686
1689template <typename T, Size DIM>
1691 const Vector_struct<T,DIM>& v1,
1692 const Vector_struct<T,DIM>& v2,
1693 const Vector_struct<T,DIM>& t)
1694{
1695 Vector<T,DIM> result;
1696 for( Size i = 0; i != DIM; ++i)
1697 result[i] = vector_base_ptr(v1)[i] * (T(1)-vector_base_ptr(t)[i])
1698 + vector_base_ptr(v2)[i] * vector_base_ptr(t)[i];
1699 return result;
1700}
1701
1704template <typename T, Size DIM>
1706 const Vector_struct<T,DIM>& v1,
1707 const Vector_struct<T,DIM>& v2,
1708 T t)
1709{
1710 // equivalent to: return v1 * (T(1)-t) + v2 * t;
1711 Vector<T,DIM> result;
1712 T t2 = T(1) - t;
1713 for( Size i = 0; i != DIM; ++i)
1714 result[i] = vector_base_ptr(v1)[i] * t2 + vector_base_ptr(v2)[i] * t;
1715 return result;
1716}
1717
1719template <typename T, Size DIM>
1721{
1722 Vector<T,DIM> result;
1723 for( Size i = 0; i != DIM; ++i)
1724 result[i] = log( vector_base_ptr(v)[i]);
1725 return result;
1726}
1727
1729template <typename T, Size DIM>
1731{
1732 Vector<T,DIM> result;
1733 for( Size i = 0; i != DIM; ++i)
1734 result[i] = log2 MI_PREVENT_MACRO_EXPAND ( vector_base_ptr(v)[i]);
1735 return result;
1736}
1737
1739template <typename T, Size DIM>
1741{
1742 Vector<T,DIM> result;
1743 for( Size i = 0; i != DIM; ++i)
1744 result[i] = log10( vector_base_ptr(v)[i]);
1745 return result;
1746}
1747
1752template <typename T, Size DIM>
1754{
1755 Vector<T,DIM> result;
1756 for( Size j = 0; j != DIM; ++j)
1757 result[j] = modf( vector_base_ptr(v)[j], i[j]);
1758 return result;
1759}
1760
1762template <typename T, Size DIM>
1764{
1765 Vector<T,DIM> result;
1766 for( Size i = 0; i != DIM; ++i)
1767 result[i] = pow( vector_base_ptr(a)[i], vector_base_ptr(b)[i]);
1768 return result;
1769}
1770
1772template <typename T, Size DIM>
1774{
1775 Vector<T,DIM> result;
1776 for( Size i = 0; i != DIM; ++i)
1777 result[i] = pow( vector_base_ptr(a)[i], b);
1778 return result;
1779}
1780
1782template <typename T, Size DIM>
1784{
1785 Vector<T,DIM> result;
1786 for( Size i = 0; i != DIM; ++i)
1787 result[i] = radians( vector_base_ptr(v)[i]);
1788 return result;
1789}
1790
1792template <typename T, Size DIM>
1794{
1795 Vector<T,DIM> result;
1796 for( Size i = 0; i != DIM; ++i)
1797 result[i] = round( vector_base_ptr(v)[i]);
1798 return result;
1799}
1800
1802template <typename T, Size DIM>
1804{
1805 Vector<T,DIM> result;
1806 for( Size i = 0; i != DIM; ++i)
1807 result[i] = rsqrt( vector_base_ptr(v)[i]);
1808 return result;
1809}
1810
1812template <typename T, Size DIM>
1814{
1815 Vector<T,DIM> result;
1816 for( Size i = 0; i != DIM; ++i)
1817 result[i] = saturate( vector_base_ptr(v)[i]);
1818 return result;
1819}
1820
1822template <typename T, Size DIM>
1824{
1825 Vector<T,DIM> result;
1826 for( Size i = 0; i != DIM; ++i)
1827 result[i] = sign( vector_base_ptr(v)[i]);
1828 return result;
1829}
1830
1832template <typename T, Size DIM>
1834{
1835 Vector<T,DIM> result;
1836 for( Size i = 0; i != DIM; ++i)
1837 result[i] = sin( vector_base_ptr(v)[i]);
1838 return result;
1839}
1840
1844template <typename T, Size DIM>
1846{
1847 for( Size i = 0; i != DIM; ++i)
1848 sincos( vector_base_ptr(a)[i], s[i], c[i]);
1849}
1850
1856template <typename T, Size DIM>
1858 const Vector_struct<T,DIM>& a,
1859 const Vector_struct<T,DIM>& b,
1860 const Vector_struct<T,DIM>& v)
1861{
1862 Vector<T,DIM> result;
1863 for( Size i = 0; i != DIM; ++i)
1864 result[i] = smoothstep(
1865 vector_base_ptr(a)[i], vector_base_ptr(b)[i], vector_base_ptr(v)[i]);
1866 return result;
1867}
1868
1874template <typename T, Size DIM>
1876 const Vector_struct<T,DIM>& a,
1877 const Vector_struct<T,DIM>& b,
1878 T x)
1879{
1880 Vector<T,DIM> result;
1881 for( Size i = 0; i != DIM; ++i)
1882 result[i] = smoothstep( vector_base_ptr(a)[i], vector_base_ptr(b)[i], x);
1883 return result;
1884}
1885
1887template <typename T, Size DIM>
1889{
1890 Vector<T,DIM> result;
1891 for( Size i = 0; i != DIM; ++i)
1892 result[i] = sqrt( vector_base_ptr(v)[i]);
1893 return result;
1894}
1895
1897template <typename T, Size DIM>
1899{
1900 Vector<T,DIM> result;
1901 for( Size i = 0; i != DIM; ++i)
1902 result[i] = step( vector_base_ptr(a)[i], vector_base_ptr(v)[i]);
1903 return result;
1904}
1905
1907template <typename T, Size DIM>
1909{
1910 Vector<T,DIM> result;
1911 for( Size i = 0; i != DIM; ++i)
1912 result[i] = tan( vector_base_ptr(v)[i]);
1913 return result;
1914}
1915
1916
1917//------ Geometric Vector Algorithms ------------------------------------------
1918
1920template <typename T>
1921inline T cross(
1922 const Vector_struct<T,2>& lhs,
1923 const Vector_struct<T,2>& rhs)
1924{
1925 return lhs.x * rhs.y - lhs.y * rhs.x;
1926}
1927
1929template <typename T>
1931 const Vector_struct<T,3>& lhs,
1932 const Vector_struct<T,3>& rhs)
1933{
1934 return Vector<T,3>( lhs.y * rhs.z - lhs.z * rhs.y,
1935 lhs.z * rhs.x - lhs.x * rhs.z,
1936 lhs.x * rhs.y - lhs.y * rhs.x);
1937}
1938
1944template <typename T>
1945inline void make_basis(
1946 const Vector<T,3>& n,
1947 Vector<T,3>* const u,
1948 Vector<T,3>* const v)
1949{
1950#ifdef mi_base_assert_enabled
1951 constexpr T eps = 1e-6f; // smallest resolvable factor
1952#endif
1953
1954 mi_math_assert_msg( u != 0, "precondition");
1955 mi_math_assert_msg( v != 0, "precondition");
1956 // Sanity check: the normal vector must be unit length.
1957 mi_math_assert_msg( abs( length(n) - T(1)) < eps, "precondition");
1958
1959 // Compute u.
1960 if( abs(n.x) < abs(n.y)) {
1961 // u = cross(x, n), x = (1, 0, 0)
1962 u->x = T(0);
1963 u->y = -n.z;
1964 u->z = n.y;
1965 } else {
1966 // u = cross(y, n), y = (0, 1, 0)
1967 u->x = n.z;
1968 u->y = T(0);
1969 u->z = -n.x;
1970 }
1971 u->normalize();
1972
1973 // Compute v. Since *u and n are orthogonal and unit-length,
1974 // there is no need to normalize *v.
1975 *v = cross( *u, n);
1976
1977 // Sanity check: make sure (u, n, v) is an orthogonal basis.
1978 mi_math_assert_msg( abs( dot( *u, n)) < eps, "postcondition");
1979 mi_math_assert_msg( abs( dot( *u, *v)) < eps, "postcondition");
1980 mi_math_assert_msg( abs( dot( n, *v)) < eps, "postcondition");
1981 // Sanity check: make sure u and v are unit length.
1982 mi_math_assert_msg( abs( length( *u) - T(1)) < eps, "postcondition");
1983 mi_math_assert_msg( abs( length( *v) - T(1)) < eps, "postcondition");
1984}
1985
1992template <typename T>
1993inline void make_basis(
1994 const Vector<T,3>& n,
1995 const Vector<T,3>& u,
1996 const Vector<T,3>& v,
1997 Vector<T,3>* const t,
1998 Vector<T,3>* const b)
1999{
2000#ifdef mi_base_assert_enabled
2001 constexpr T eps = 1e-6f; // smallest resolvable factor
2002#endif
2003
2004 mi_math_assert_msg( t != 0, "precondition");
2005 mi_math_assert_msg( b != 0, "precondition");
2006 // Sanity check: the normal vector must be unit length.
2007 mi_math_assert_msg( abs( length( n) - T(1)) < eps, "precondition");
2008 // Sanity check: the other vector lengths should be finite and non-zero
2009 mi_math_assert_msg( length( u) > 0, "precondition");
2010 mi_math_assert_msg( length( v) > 0, "precondition");
2011 mi_math_assert_msg( isfinite( length( u)), "precondition");
2012 mi_math_assert_msg( isfinite( length( v)), "precondition");
2013
2014 // Compute b
2015 *b = cross(u,n);
2016 b->normalize();
2017
2018 // Compute t. Since *b and n are orthogonal and unit-length,
2019 // there is no need to normalize *t.
2020 *t = cross(n,*b);
2021
2022 // Check that b has the same orientation of v
2023 if( dot( *b,v) < T(0))
2024 *b = -*b;
2025
2026 // Sanity check: make sure *u and t have the same orientation.
2027 mi_math_assert_msg( dot( u, *t) > T(0), "postcondition");
2028 // Sanity check: make sure (t, n, b) is an orthogonal basis.
2029 // We use a scaled epsilon in order to avoid false positives.
2030 mi_math_assert_msg( abs( dot( *t, n)) < 20*eps, "postcondition");
2031 mi_math_assert_msg( abs( dot( *t, *b)) < 20*eps, "postcondition");
2032 mi_math_assert_msg( abs( dot( n, *b)) < 20*eps, "postcondition");
2033 // Sanity check: make sure t and b are unit length.
2034 mi_math_assert_msg( abs( length( *t) - T(1)) < eps, "postcondition");
2035 mi_math_assert_msg( abs( length( *b) - T(1)) < eps, "postcondition");
2036}
2037
2043template <typename T2, Size DIM2, typename T1, Size DIM1>
2045 const Vector<T1, DIM1>& v,
2046 const T2& fill = T2(0))
2047{
2048 const Size dim_min = base::min MI_PREVENT_MACRO_EXPAND ( DIM1, DIM2 );
2049 Vector<T2, DIM2> result;
2050 for( Size i = 0; i < dim_min; ++i)
2051 result[i] = T2(v[i]);
2052 for( Size i = dim_min; i < DIM2; ++i)
2053 result[i] = fill;
2054 return result;
2055}
2056 // end group mi_math_vector
2058
2059} // namespace math
2060
2061} // namespace mi
2062
2063#endif // MI_MATH_VECTOR_H
Fixed-size math vector class template with generic operations.
Definition: vector.h:286
Math functions and function templates on simple types or generic container and vector concepts.
#define mi_static_assert(expr)
Compile time assertion that raises a compilation error if the constant expression expr evaluates to f...
Definition: assert.h:58
#define MI_PREVENT_MACRO_EXPAND
Empty macro that can be used after function names to prevent macro expansion that happen to have the ...
Definition: config.h:97
float Float32
32-bit float.
Definition: types.h:51
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
Sint64 Difference
Signed integral type that is large enough to hold the difference of two pointers.
Definition: types.h:122
bool lexicographically_less(const V &lhs, const V &rhs)
Returns true if vector lhs is lexicographically less than vector rhs, and false otherwise.
Definition: function.h:1114
bool lexicographically_less_or_equal(const V &lhs, const V &rhs)
Returns true if vector lhs is lexicographically less than or equal to vector rhs, and false otherwise...
Definition: function.h:1130
Sint32 dot(Sint32 a, Sint32 b)
Returns the inner product (a.k.a. dot or scalar product) of two integers.
Definition: function.h:1021
bool lexicographically_greater_or_equal(const V &lhs, const V &rhs)
Returns true if vector lhs is lexicographically greater than or equal to vector rhs,...
Definition: function.h:1162
bool is_not_equal(const V &lhs, const V &rhs)
Returns true if vector lhs is elementwise not equal to vector rhs, and false otherwise.
Definition: function.h:1101
Float32 length(Float32 a)
Returns the Euclidean norm of the scalar a (its absolute value).
Definition: function.h:1047
bool lexicographically_greater(const V &lhs, const V &rhs)
Returns true if vector lhs is lexicographically greater than vector rhs, and false otherwise.
Definition: function.h:1146
bool is_equal(const V &lhs, const V &rhs)
Returns true if vector lhs is elementwise equal to vector rhs, and false otherwise.
Definition: function.h:1090
#define mi_math_assert_msg(expr, msg)
Math API assertion macro (with message).
Definition: assert.h:80
Bbox<T, DIM> operator+(const Bbox<T, DIM> &bbox, T value)
Returns a bounding box that is the bbox increased by a constant value at each face,...
Definition: bbox.h:468
Bbox<T, DIM> & operator*=(Bbox<T, DIM> &bbox, T factor)
Scales bbox by factor, i.e., bbox.max and bbox.min are multiplied by factor.
Definition: bbox.h:562
Bbox<T, DIM> & operator+=(Bbox<T, DIM> &bbox, T value)
Increases bbox by a constant value at each face, i.e., value is added to bbox.max and subtracted from...
Definition: bbox.h:533
Bbox<T, DIM> & operator/=(Bbox<T, DIM> &bbox, T divisor)
Divide bbox by divisor, i.e., bbox.max and bbox.min are divided by divisor.
Definition: bbox.h:576
Bbox<T, DIM> operator/(const Bbox<T, DIM> &bbox, T divisor)
Returns a bounding box that is a version of bbox divided by divisor, i.e., bbox.max and bbox....
Definition: bbox.h:516
Bbox<T, DIM> & operator-=(Bbox<T, DIM> &bbox, T value)
Shrinks bbox by a constant value at each face, i.e., value is subtracted from bbox....
Definition: bbox.h:548
Bbox<T, DIM> operator-(const Bbox<T, DIM> &bbox, T value)
Returns a bounding box that is the bbox shrunk by a constant value at each face, i....
Definition: bbox.h:484
Bbox<T, DIM> operator*(const Bbox<T, DIM> &bbox, T factor)
Returns a bounding box that is a version of bbox scaled by factor, i.e., bbox.max and bbox....
Definition: bbox.h:500
Bbox<T, DIM> lerp(const Bbox<T, DIM> &bbox1, const Bbox<T, DIM> &bbox2, T t)
Returns the linear interpolation between bbox1 and bbox2, i.e., it returns (1-t) * bbox1 + t * bbox2.
Definition: bbox.h:670
Color frac(const Color &c)
Returns a color with the elementwise positive fractional part of the color c.
Definition: color.h:613
Color sqrt(const Color &c)
Returns the square root of each element of c.
Definition: color.h:792
Color acos(const Color &c)
Returns a color with the elementwise arc cosine of the color c.
Definition: color.h:465
Color ceil(const Color &c)
Returns a color with the elementwise smallest integral value that is not less than the element in col...
Definition: color.h:504
Color clamp(const Color &c, const Color &low, const Color &high)
Returns the color c elementwise clamped to the range [low, high].
Definition: color.h:510
Color abs(const Color &c)
Returns a color with the elementwise absolute values of the color c.
Definition: color.h:459
Color round(const Color &c)
Returns a color with the elements of color c rounded to nearest integers.
Definition: color.h:725
Color modf(const Color &c, Color &i)
Returns the elementwise fractional part of c and stores the elementwise integral part of c in i.
Definition: color.h:701
Color atan(const Color &c)
Returns a color with the elementwise arc tangent of the color c.
Definition: color.h:489
bool isfinite(const Color &c)
Indicates whether all components of the color are finite.
Definition: color.h:810
Color step(const Color &a, const Color &c)
Returns elementwise 0 if c is less than a and 1 otherwise.
Definition: color.h:798
Color elementwise_min(const Color &lhs, const Color &rhs)
Returns elementwise min for each element in color lhs that is less than the corresponding element in ...
Definition: color.h:569
Color tan(const Color &c)
Returns a color with the elementwise tangent of the color c.
Definition: color.h:804
Color atan2(const Color &c, const Color &d)
Returns a color with the elementwise arc tangent of the color c / d.
Definition: color.h:497
Color sin(const Color &c)
Returns a color with the elementwise sine of the color c.
Definition: color.h:749
Color degrees(const Color &c)
Converts elementwise radians in c to degrees.
Definition: color.h:552
Color saturate(const Color &c)
Returns the color c clamped elementwise to the range [0,1].
Definition: color.h:737
Color rsqrt(const Color &c)
Returns the reciprocal of the square root of each element of c.
Definition: color.h:731
bool any(const Color &c)
Returns true if any element of c is not equal to zero.
Definition: color.h:477
Color radians(const Color &c)
Converts elementwise degrees in c to radians.
Definition: color.h:719
Color exp2(const Color &c)
Returns a color with elementwise 2 to the power of the element in the color c.
Definition: color.h:584
Color elementwise_max(const Color &lhs, const Color &rhs)
Returns elementwise max for each element in color lhs that is less than the corresponding element in ...
Definition: color.h:559
Color log(const Color &c)
Returns a color with elementwise natural logarithm of the color c.
Definition: color.h:677
Color floor(const Color &c)
Returns a color with the elementwise largest integral value that is not greater than the element in c...
Definition: color.h:591
Color smoothstep(const Color &a, const Color &b, const Color &c)
Returns 0 if c is less than a and 1 if c is greater than b in an elementwise fashion.
Definition: color.h:770
Color sign(const Color &c)
Returns the elementwise sign of color c.
Definition: color.h:743
Color fmod(const Color &a, const Color &b)
Returns elementwise a modulo b, in other words, the remainder of a/b.
Definition: color.h:599
Color cos(const Color &c)
Returns a color with the elementwise cosine of the color c.
Definition: color.h:546
Color pow(const Color &a, const Color &b)
Returns the color a elementwise to the power of b.
Definition: color.h:707
bool all(const Color &c)
Returns true if all elements of c are not equal to zero.
Definition: color.h:471
Color log10(const Color &c)
Returns a color with elementwise base 10 logarithm of the color c.
Definition: color.h:692
Color log2(const Color &c)
Returns a color with elementwise base 2 logarithm of the color c.
Definition: color.h:683
Color exp(const Color &c)
Returns a color with elementwise e to the power of the element in the color c.
Definition: color.h:578
bool is_approx_equal(const Color &lhs, const Color &rhs, Float32 e)
Compares the two given values elementwise for equality within the given epsilon.
Definition: color.h:638
void sincos(const Color &a, Color &s, Color &c)
Computes elementwise the sine s and cosine c of angles a simultaneously.
Definition: color.h:757
Color asin(const Color &c)
Returns a color with the elementwise arc sine of the color c.
Definition: color.h:483
void transform(const Vector &vec, ResultVector &result, UnaryFunctor f)
Generic transform function that applies a unary functor (return value).
Definition: function.h:236
void for_each(Vector &vec, UnaryFunctor f)
Generic transform function that applies a unary functor (in-place).
Definition: function.h:304
void transform_right_scalar(const Vector &vec, const Scalar &s, ResultVector &result, BinaryFunctor f)
Generic transform function that applies a binary functor (return value, RHS scalar).
Definition: function.h:289
void transform_left_scalar(const Scalar &s, const Vector &vec, ResultVector &result, BinaryFunctor f)
Generic transform function that applies a binary functor (return value, LHS scalar).
Definition: function.h:271
T y
y-coordinate.
Definition: vector.h:164
Vector(const Vector<T2, DIM> &other)
Template constructor that allows explicit conversions from other vectors with assignment compatible e...
Definition: vector.h:425
Vector<bool, DIM> operator^(const Vector<bool, DIM> &lhs, const Vector<bool, DIM> &rhs)
Returns the elementwise logical xor of two boolean vectors.
Definition: vector.h:1314
void set(Size i, T value)
Sets the i-th vector element to value.
Definition: vector.h:648
Vector(T2 const (&array)[DIM])
Constructor initializes the vector elements from an array of dimension DIM.
Definition: vector.h:416
T x
x-coordinate.
Definition: vector.h:163
T cross(const Vector_struct<T, 2> &lhs, const Vector_struct<T, 2> &rhs)
Returns the two-times-two determinant result for the two vectors lhs and rhs.
Definition: vector.h:1921
Vector(T v1, const Vector<T, 3> &v2)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four eleme...
Definition: vector.h:555
const T * end() const
Returns the past-the-end pointer.
Definition: vector.h:322
T elements[DIM]
coordinates.
Definition: vector.h:136
T w
w-coordinate.
Definition: vector.h:166
Vector<bool, DIM> elementwise_is_greater_than(const Vector<T, DIM> &lhs, const Vector<T, DIM> &rhs)
Returns the boolean vector result of an elementwise greater-than comparison.
Definition: vector.h:1404
Vector(const Color_struct &color)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from a color interp...
Definition: vector.h:579
Vector<bool, DIM> elementwise_is_greater_than_or_equal(const Vector<T, DIM> &lhs, const Vector<T, DIM> &rhs)
Returns the boolean vector result of an elementwise greater-than-or-equal comparison.
Definition: vector.h:1415
Float32 g
Green color component.
Definition: vector.h:71
Vector(const Vector<T, 2> &v1, const Vector<T, 2> &v2)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four eleme...
Definition: vector.h:543
Vector(From_iterator_tag, Iterator p)
Constructor requires the mi::math::FROM_ITERATOR tag as first argument and initializes the vector ele...
Definition: vector.h:398
const T & const_reference
Const reference to element.
Definition: vector.h:297
bool operator<=(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically less than or equal to rhs.
Definition: vector.h:697
T & reference
Mutable reference to element.
Definition: vector.h:296
Vector(const Vector<T, 3> &v1, T v2)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four eleme...
Definition: vector.h:567
bool operator>=(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically greater than or equal to rhs.
Definition: vector.h:713
bool operator>(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically greater than rhs.
Definition: vector.h:705
T * pointer
Mutable pointer to element.
Definition: vector.h:294
Vector(const Vector_struct<T, DIM> &vec)
Constructor from underlying storage type.
Definition: vector.h:343
Vector<T, DIM> & operator++(Vector<T, DIM> &vec)
Pre-increments all elements of vec and returns the result. Modifies vec.
Definition: vector.h:1229
Vector<T, DIM> & operator--(Vector<T, DIM> &vec)
Pre-decrements all elements of vec and returns the result. Modifies vec.
Definition: vector.h:1237
static constexpr Size max_size()
Constant maximum size of the vector.
Definition: vector.h:306
Vector(const Vector<T, 2> &v1, T v2)
Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elem...
Definition: vector.h:483
Vector<bool, DIM> elementwise_is_not_equal(const Vector<T, DIM> &lhs, const Vector<T, DIM> &rhs)
Returns the boolean vector result of an elementwise inequality comparison.
Definition: vector.h:1371
Vector(T v1, T v2, T v3, T v4)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four eleme...
Definition: vector.h:495
static constexpr Size SIZE
Constant size of the vector.
Definition: vector.h:300
static constexpr Size size()
Constant size of the vector.
Definition: vector.h:303
T & operator[](Size i)
Accesses the i-th vector element.
Definition: vector.h:621
const T * begin() const
Returns the pointer to the first vector element.
Definition: vector.h:312
Vector(const Vector_struct<T2, DIM> &other)
Template constructor that allows explicit conversions from underlying storage type with assignment co...
Definition: vector.h:434
Size size_type
Size type, unsigned.
Definition: vector.h:292
Vector<T2, DIM2> convert_vector(const Vector<T1, DIM1> &v, const T2 &fill=T2(0))
Converts the vector v of type Vector<T1, DIM1> to a vector of type Vector<T2, DIM2>.
Definition: vector.h:2044
const T & get(Size i) const
Returns the i-th vector element.
Definition: vector.h:639
T x
x-coordinate.
Definition: vector.h:148
Vector<T, DIM> & operator%=(Vector<T, DIM> &lhs, const Vector_struct<T, DIM> &rhs)
Computes lhs modulo rhs elementwise and returns the modified lhs.
Definition: vector.h:872
Difference difference_type
Difference type, signed.
Definition: vector.h:293
void make_basis(const Vector<T, 3> &n, Vector<T, 3> *const u, Vector<T, 3> *const v)
Computes a basis of 3D space with one given vector.
Definition: vector.h:1945
T * vector_base_ptr(Vector_struct<T, DIM> &vec)
Returns the base pointer to the vector data.
Definition: vector.h:174
Vector(const Vector<T, DIM> &vec)=default
Default copy constructor.
Vector(T v1, T v2, const Vector<T, 2> &v3)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four eleme...
Definition: vector.h:507
Vector & operator=(const Vector &other)=default
Assignment.
Vector()
The default constructor leaves the vector elements uninitialized.
Definition: vector.h:325
T y
y-coordinate.
Definition: vector.h:156
bool operator==(Vector<T, DIM> rhs) const
Returns true if lhs is elementwise equal to rhs.
Definition: vector.h:675
Vector(const Vector<T, 2> &v1, T v2, T v3)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four eleme...
Definition: vector.h:531
T z
z-coordinate.
Definition: vector.h:165
const T * const_pointer
Const pointer to element.
Definition: vector.h:295
Vector<bool, DIM> elementwise_is_less_than_or_equal(const Vector<T, DIM> &lhs, const Vector<T, DIM> &rhs)
Returns the boolean vector result of an elementwise less-than-or-equal comparison.
Definition: vector.h:1393
T z
z-coordinate.
Definition: vector.h:157
T value_type
Element type.
Definition: vector.h:291
Float32 a
Alpha value, 0.0 is fully transparent and 1.0 is opaque; value can lie outside that range.
Definition: vector.h:75
Vector(T v1, T v2, T v3)
Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elem...
Definition: vector.h:459
Vector<T, DIM> operator%(const Vector_struct<T, DIM> &lhs, const Vector_struct<T, DIM> &rhs)
Computes lhs modulo rhs elementwise and returns the new result.
Definition: vector.h:925
Float32 r
Red color component.
Definition: vector.h:69
bool normalize()
Normalizes this vector to unit length.
Definition: vector.h:662
Vector<bool, DIM> operator&&(const Vector<bool, DIM> &lhs, const Vector<bool, DIM> &rhs)
Returns the elementwise logical and of two boolean vectors.
Definition: vector.h:1248
T * end()
Returns the past-the-end pointer.
Definition: vector.h:317
Vector<bool, DIM> operator!(const Vector<bool, DIM> &vec)
Returns the elementwise logical not of a boolean vector.
Definition: vector.h:1347
bool operator<(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically less than rhs.
Definition: vector.h:689
T y
y-coordinate.
Definition: vector.h:149
T * begin()
Returns the pointer to the first vector element.
Definition: vector.h:309
Vector<bool, DIM> operator||(const Vector<bool, DIM> &lhs, const Vector<bool, DIM> &rhs)
Returns the elementwise logical or of two boolean vectors.
Definition: vector.h:1281
static constexpr Size DIMENSION
Constant dimension of the vector.
Definition: vector.h:299
T x
x-coordinate.
Definition: vector.h:155
Float32 b
Blue color component.
Definition: vector.h:73
Vector(T v1, T v2)
Dedicated constructor, for dimension 2 only, that initializes the vector elements from the two elemen...
Definition: vector.h:447
Vector<bool, DIM> elementwise_is_equal(const Vector<T, DIM> &lhs, const Vector<T, DIM> &rhs)
Returns the boolean vector result of an elementwise equality comparison.
Definition: vector.h:1360
Vector(T v1, const Vector<T, 2> &v2)
Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elem...
Definition: vector.h:471
Vector<bool, DIM> elementwise_is_less_than(const Vector<T, DIM> &lhs, const Vector<T, DIM> &rhs)
Returns the boolean vector result of an elementwise less-than comparison.
Definition: vector.h:1382
bool operator!=(Vector<T, DIM> rhs) const
Returns true if lhs is elementwise not equal to rhs.
Definition: vector.h:681
T x
x-coordinate.
Definition: vector.h:142
Vector(T v1, const Vector<T, 2> &v2, T v3)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four eleme...
Definition: vector.h:519
From_iterator_tag
Enum used for initializing a vector from an iterator.
Definition: vector.h:36
@ FROM_ITERATOR
Unique enumerator of From_iterator_tag.
Definition: vector.h:37
Assertions and compile-time assertions.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
Helper class to deduce properties of numeric types defined in this API.
Definition: types.h:428
Generic storage class template for an RGBA color representation storing four floating points elements...
Definition: vector.h:67
Specialization for dimension 1 to create x member.
Definition: vector.h:141
Specialization for dimension 2 to create x and y member.
Definition: vector.h:147
Specialization for dimension 3 to create x, y, and z members.
Definition: vector.h:154
Specialization for dimension 4 to create x, y, z, and w members.
Definition: vector.h:162
Generic storage class template for math vector representations storing DIM elements of type T.
Definition: vector.h:135
Functor for the logical and operator, &&.
Definition: function.h:151
Functor for the equality comparison operator, ==.
Definition: function.h:78
Functor for the greater-than-or-equal comparison operator, >=.
Definition: function.h:113
Functor for the greater-than comparison operator, >.
Definition: function.h:106
Functor for the less-than-or-equal comparison operator, <=.
Definition: function.h:99
Functor for the less-than comparison operator, <.
Definition: function.h:92
Functor for the inequality comparison operator, !=.
Definition: function.h:85
Functor for the logical not operator, !.
Definition: function.h:172
Functor for the logical or operator, ||.
Definition: function.h:158
Functor for the pre-decrement operator, --.
Definition: function.h:193
Functor for the pre-increment operator, ++.
Definition: function.h:179
Functor for the xor operator, ^.
Definition: function.h:165
Basic types.