Math API nvidia_logo_transpbg.gif Up
vector.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2023 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 typedef T value_type;
292 typedef Size size_type;
294 typedef T * pointer;
295 typedef const T * const_pointer;
296 typedef T & reference;
297 typedef const T & const_reference;
298
299 static const Size DIMENSION = DIM;
300 static const Size SIZE = DIM;
301
303 static inline Size size() { return SIZE; }
304
306 static inline Size max_size() { return SIZE; }
307
309 inline T* begin() { return mi::math::vector_base_ptr( *this); }
310
312 inline const T* begin() const { return mi::math::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#if defined(DEBUG) || (defined(_MSC_VER) && _MSC_VER <= 1310)
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 //
332 // When compiling with Visual C++ 7.1 or earlier, this code is enabled in all variants to
333 // work around a very obscure compiler bug that causes the compiler to crash.
334 typedef mi::base::numeric_traits<T> Traits;
335 T v = (Traits::has_signaling_NaN) ? Traits::signaling_NaN()
336 : Traits::max MI_PREVENT_MACRO_EXPAND ();
337 for( Size i(0u); i < DIM; ++i)
338 (*this)[i] = v;
339#endif
340 }
341
342#if (__cplusplus >= 201103L)
344 Vector( const Vector<T,DIM>& vec ) = default;
345#endif
346
348 inline Vector( const Vector_struct<T,DIM>& vec )
349 {
350 for( Size i(0u); i < DIM; ++i)
351 (*this)[i] = mi::math::vector_base_ptr(vec)[i];
352 }
353
355 inline explicit Vector(T v)
356 {
357 for( Size i(0u); i < DIM; ++i)
358 (*this)[i] = v;
359 }
360
374 template <typename Iterator>
375 inline Vector(From_iterator_tag, Iterator p)
376 {
377 for( Size i(0u); i < DIM; ++i, ++p)
378 (*this)[i] = *p;
379 }
380
392 template <typename T2>
393 inline explicit Vector( T2 const (& array)[DIM])
394 {
395 for( Size i(0u); i < DIM; ++i)
396 (*this)[i] = array[i];
397 }
398
401 template <typename T2>
402 inline explicit Vector( const Vector<T2,DIM>& other)
403 {
404 for( Size i(0u); i < DIM; ++i)
405 (*this)[i] = T(other[i]);
406 }
407
410 template <typename T2>
411 inline explicit Vector( const Vector_struct<T2,DIM>& other)
412 {
413 for( Size i(0u); i < DIM; ++i)
414 (*this)[i] = T(mi::math::vector_base_ptr(other)[i]);
415 }
416
421 inline Vector(T v1, T v2)
422 {
423 mi_static_assert(DIM == 2);
424 begin()[0] = v1;
425 begin()[1] = v2;
426 }
427
432 inline Vector(T v1, T v2, T v3)
433 {
434 mi_static_assert(DIM == 3);
435 begin()[0] = v1;
436 begin()[1] = v2;
437 begin()[2] = v3;
438 }
439
444 inline Vector(T v1, const Vector<T,2>& v2)
445 {
446 mi_static_assert(DIM == 3);
447 begin()[0] = v1;
448 begin()[1] = v2.x;
449 begin()[2] = v2.y;
450 }
451
456 inline Vector(const Vector<T,2>& v1, T v2)
457 {
458 mi_static_assert(DIM == 3);
459 begin()[0] = v1.x;
460 begin()[1] = v1.y;
461 begin()[2] = v2;
462 }
463
468 inline Vector(T v1, T v2, T v3, T v4)
469 {
470 mi_static_assert(DIM == 4);
471 begin()[0] = v1;
472 begin()[1] = v2;
473 begin()[2] = v3;
474 begin()[3] = v4;
475 }
476
481 inline Vector(T v1, T v2, const Vector<T,2>& v3)
482 {
483 mi_static_assert(DIM == 4);
484 begin()[0] = v1;
485 begin()[1] = v2;
486 begin()[2] = v3.x;
487 begin()[3] = v3.y;
488 }
489
490
495 inline Vector(T v1, const Vector<T,2>& v2, T v3)
496 {
497 mi_static_assert(DIM == 4);
498 begin()[0] = v1;
499 begin()[1] = v2.x;
500 begin()[2] = v2.y;
501 begin()[3] = v3;
502 }
503
508 inline Vector(const Vector<T,2>& v1, T v2, T v3)
509 {
510 mi_static_assert(DIM == 4);
511 begin()[0] = v1.x;
512 begin()[1] = v1.y;
513 begin()[2] = v2;
514 begin()[3] = v3;
515 }
516
521 inline Vector(const Vector<T,2>& v1, const Vector<T,2>& v2)
522 {
523 mi_static_assert(DIM == 4);
524 begin()[0] = v1.x;
525 begin()[1] = v1.y;
526 begin()[2] = v2.x;
527 begin()[3] = v2.y;
528 }
529
534 inline Vector(T v1, const Vector<T,3>& v2)
535 {
536 mi_static_assert(DIM == 4);
537 begin()[0] = v1;
538 begin()[1] = v2.x;
539 begin()[2] = v2.y;
540 begin()[3] = v2.z;
541 }
542
547 inline Vector(const Vector<T,3>& v1, T v2)
548 {
549 mi_static_assert(DIM == 4);
550 begin()[0] = v1.x;
551 begin()[1] = v1.y;
552 begin()[2] = v1.z;
553 begin()[3] = v2;
554 }
555
560 inline explicit Vector( const Color_struct& color)
561 {
562 mi_static_assert(DIM == 4);
563 this->x = color.r;
564 this->y = color.g;
565 this->z = color.b;
566 this->w = color.a;
567 }
568
570#if (__cplusplus >= 201103L)
571 Vector& operator=( const Vector& other) = default;
572#else
573 inline Vector& operator=( const Vector& other)
574 {
575 for( Size i(0u); i < DIM; ++i)
576 (*this)[i] = other[i];
577 return *this;
578 }
579#endif
580
582 inline Vector& operator=( T s)
583 {
584 for( Size i(0u); i < DIM; ++i)
585 (*this)[i] = s;
586 return *this;
587 }
592 inline Vector& operator=( const Color_struct& color)
593 {
594 mi_static_assert(DIM == 4);
595 this->x = color.r;
596 this->y = color.g;
597 this->z = color.b;
598 this->w = color.a;
599 return *this;
600 }
601
605 inline T& operator[](Size i)
606 {
607 mi_math_assert_msg(i < DIM, "precondition");
608 return begin()[i];
609 }
610
614 inline const T& operator[](Size i) const
615 {
616 mi_math_assert_msg(i < DIM, "precondition");
617 return begin()[i];
618 }
619
623 inline const T& get(Size i) const
624 {
625 mi_math_assert_msg(i < DIM, "precondition");
626 return begin()[i];
627 }
628
632 inline void set(Size i, T value)
633 {
634 mi_math_assert_msg(i < DIM, "precondition");
635 begin()[i] = value;
636 }
637
638
639
646 inline bool normalize()
647 {
648 const T rec_length = T(1) / length( *this);
649 const bool result = isfinite( rec_length); //-V601 PVS
650 if( result)
651 (*this) *= rec_length;
652 return result;
653 }
654
655
656 //------ Free comparison operators ==, !=, <, <=, >, >= for vectors --------
657
659 inline bool operator==( Vector<T,DIM> rhs) const
660 {
661 return is_equal( *this, rhs);
662 }
663
665 inline bool operator!=( Vector<T,DIM> rhs) const
666 {
667 return is_not_equal( *this, rhs);
668 }
669
673 inline bool operator<( Vector<T,DIM> rhs) const
674 {
675 return lexicographically_less( *this, rhs);
676 }
677
681 inline bool operator<=( Vector<T,DIM> rhs) const
682 {
683 return lexicographically_less_or_equal( *this, rhs);
684 }
685
689 inline bool operator>( Vector<T,DIM> rhs) const
690 {
691 return lexicographically_greater( *this, rhs);
692 }
693
697 inline bool operator>=( Vector<T,DIM> rhs) const
698 {
699 return lexicographically_greater_or_equal( *this, rhs);
700 }
701};
702
703
704//------ Free operators +=, -=, *=, /=, +, -, *, and / for vectors -------------
705
707template <typename T, Size DIM>
709 Vector<T,DIM>& lhs,
710 const Vector_struct<T,DIM>& rhs)
711{
712 for( Size i(0u); i < DIM; ++i)
713 vector_base_ptr(lhs)[i] += vector_base_ptr(rhs)[i];
714 return lhs;
715}
716
718template <typename T, Size DIM>
720 Vector<T,DIM>& lhs,
721 const Vector_struct<T,DIM>& rhs)
722{
723 for( Size i(0u); i < DIM; ++i)
724 vector_base_ptr(lhs)[i] -= vector_base_ptr(rhs)[i];
725 return lhs;
726}
727
729template <typename T, Size DIM>
731 Vector<T,DIM>& lhs,
732 const Vector_struct<T,DIM>& rhs)
733{
734 for( Size i(0u); i < DIM; ++i)
735 vector_base_ptr(lhs)[i] *= vector_base_ptr(rhs)[i];
736 return lhs;
737}
738
741template <typename T, Size DIM>
743 Vector<T,DIM>& lhs,
744 const Vector_struct<T,DIM>& rhs)
745{
746 for( Size i(0u); i < DIM; ++i)
747 vector_base_ptr(lhs)[i] %= vector_base_ptr(rhs)[i];
748 return lhs;
749}
750
752template <typename T, typename U, Size DIM>
754 Vector<T,DIM>& lhs,
755 const Vector_struct<U,DIM>& rhs)
756{
757 for( Size i(0u); i < DIM; ++i)
758 vector_base_ptr(lhs)[i] = T(vector_base_ptr(lhs)[i] / vector_base_ptr(rhs)[i]);
759 return lhs;
760}
761
763template <typename T, Size DIM>
765 const Vector_struct<T,DIM>& lhs,
766 const Vector_struct<T,DIM>& rhs)
767{
768 Vector<T,DIM> tmp( lhs);
769 return tmp += rhs;
770}
771
773template <typename T, Size DIM>
775 const Vector_struct<T,DIM>& lhs,
776 const Vector_struct<T,DIM>& rhs)
777{
778 Vector<T,DIM> tmp( lhs);
779 return tmp -= rhs;
780}
781
783template <typename T, Size DIM>
785 const Vector_struct<T,DIM>& lhs,
786 const Vector_struct<T,DIM>& rhs)
787{
788 Vector<T,DIM> tmp( lhs);
789 return tmp *= rhs;
790}
791
794template <typename T, Size DIM>
796 const Vector_struct<T,DIM>& lhs,
797 const Vector_struct<T,DIM>& rhs)
798{
799 Vector<T,DIM> tmp( lhs);
800 return tmp %= rhs;
801}
802
804template <typename T, typename U, Size DIM>
806 const Vector_struct<T,DIM>& lhs,
807 const Vector_struct<U,DIM>& rhs)
808{
809 Vector<T,DIM> tmp(lhs);
810 return tmp /= rhs;
811}
812
814template <typename T, Size DIM>
816{
817 Vector<T,DIM> tmp;
818 for( Size i(0u); i < DIM; ++i)
819 vector_base_ptr(tmp)[i] = -vector_base_ptr(v)[i];
820 return tmp;
821}
822
823
824//------ Resolve ambiguity with hybrid operators -------------
825// (Those operators should really be restricted to scalar types but that requires
826// modern C++ which some users of this headers don't enable.)
827
829template <typename T, Size DIM>
831 Vector<T,DIM>& lhs,
832 const Vector<T,DIM>& rhs)
833{
834 return lhs += static_cast<const Vector_struct<T,DIM>&>(rhs);
835}
836
838template <typename T, Size DIM>
840 Vector<T,DIM>& lhs,
841 const Vector<T,DIM>& rhs)
842{
843 return lhs -= static_cast<const Vector_struct<T,DIM>&>(rhs);
844}
845
847template <typename T, Size DIM>
849 Vector<T,DIM>& lhs,
850 const Vector<T,DIM>& rhs)
851{
852 return lhs *= static_cast<const Vector_struct<T,DIM>&>(rhs);
853}
854
857template <typename T, Size DIM>
859 Vector<T,DIM>& lhs,
860 const Vector<T,DIM>& rhs)
861{
862 return lhs %= static_cast<const Vector_struct<T,DIM>&>(rhs);
863}
864
866template <typename T, typename U, Size DIM>
868 Vector<T,DIM>& lhs,
869 const Vector<U,DIM>& rhs)
870{
871 return lhs /= static_cast<const Vector_struct<U,DIM>&>(rhs);
872}
873
875template <typename T, Size DIM>
877 const Vector<T,DIM>& lhs,
878 const Vector<T,DIM>& rhs)
879{
880 return lhs + static_cast<const Vector_struct<T,DIM>&>(rhs);
881}
882
884template <typename T, Size DIM>
886 const Vector<T,DIM>& lhs,
887 const Vector<T,DIM>& rhs)
888{
889 return lhs - static_cast<const Vector_struct<T,DIM>&>(rhs);
890}
891
893template <typename T, Size DIM>
895 const Vector<T,DIM>& lhs,
896 const Vector<T,DIM>& rhs)
897{
898 return lhs * static_cast<const Vector_struct<T,DIM>&>(rhs);
899}
900
903template <typename T, Size DIM>
905 const Vector<T,DIM>& lhs,
906 const Vector<T,DIM>& rhs)
907{
908 return lhs % static_cast<const Vector_struct<T,DIM>&>(rhs);
909}
910
912template <typename T, typename U, Size DIM>
914 const Vector<T,DIM>& lhs,
915 const Vector<U,DIM>& rhs)
916{
917 return lhs / static_cast<const Vector_struct<U,DIM>&>(rhs);
918}
919
921template <typename T, Size DIM>
923{
924 return -static_cast<const Vector_struct<T,DIM>&>(v);
925}
926
927
928//------ Free operator *=, /=, *, and / definitions for scalars ---------------
929
932template <typename T, typename TT, Size DIM>
934 Vector<T,DIM>& v,
935 TT s)
936{
937 for( Size i(0u); i < DIM; ++i)
938 vector_base_ptr(v)[i] = T(vector_base_ptr(v)[i] * s);
939 return v;
940}
941
945template <typename T, typename TT, Size DIM>
947 Vector<T,DIM>& v,
948 TT s)
949{
950 for( Size i(0u); i < DIM; ++i)
951 vector_base_ptr(v)[i] = T(vector_base_ptr(v)[i] % s);
952 return v;
953}
954
956template <typename T, typename TT, Size DIM>
958 Vector<T,DIM>& v,
959 TT s)
960{
961 for( Size i(0u); i < DIM; ++i)
962 vector_base_ptr(v)[i] = T(vector_base_ptr(v)[i] / s);
963 return v;
964}
965
967template <typename T, typename TT, Size DIM>
969 const Vector_struct<T,DIM>& v,
970 TT s)
971{
972 Vector<T,DIM> tmp( v);
973 return tmp *= s;
974}
975
977template <typename T, typename TT, Size DIM>
979 TT s,
980 const Vector_struct<T,DIM>& v)
981{
982 Vector<T,DIM> tmp(v);
983 return tmp *= s;
984}
985
989template <typename T, typename TT, Size DIM>
991 const Vector_struct<T,DIM>& v,
992 TT s)
993{
994 Vector<T,DIM> tmp(v);
995 return tmp %= s;
996}
997
999template <typename T, typename TT, Size DIM>
1001 const Vector_struct<T,DIM>& v,
1002 TT s)
1003{
1004 Vector<T,DIM> tmp( v);
1005 return tmp /= s;
1006}
1007
1008
1009//------ Free operator ++, -- for vectors -------------------------------------
1010
1012template <typename T, Size DIM>
1014{
1016 return vec;
1017}
1018
1020template <typename T, Size DIM>
1022{
1024 return vec;
1025}
1026
1027
1028//------ Free operators !, &&, ||, ^ for bool vectors and bool scalars ---------
1029
1031template <Size DIM>
1033 const Vector<bool,DIM>& lhs,
1034 const Vector<bool,DIM>& rhs)
1035{
1036 Vector<bool,DIM> result;
1037 general::transform( lhs, rhs, result, functor::Operator_and_and());
1038 return result;
1039}
1040
1042template <Size DIM>
1044 bool lhs,
1045 const Vector<bool,DIM>& rhs)
1046{
1047 Vector<bool,DIM> result;
1049 return result;
1050}
1051
1053template <Size DIM>
1055 const Vector<bool,DIM>& lhs,
1056 bool rhs)
1057{
1058 Vector<bool,DIM> result;
1060 return result;
1061}
1062
1064template <Size DIM>
1066 const Vector<bool,DIM>& lhs,
1067 const Vector<bool,DIM>& rhs)
1068{
1069 Vector<bool,DIM> result;
1070 general::transform(lhs, rhs, result, functor::Operator_or_or());
1071 return result;
1072}
1073
1075template <Size DIM>
1077 bool lhs,
1078 const Vector<bool,DIM>& rhs)
1079{
1080 Vector<bool,DIM> result;
1082 return result;
1083}
1084
1086template <Size DIM>
1088 const Vector<bool,DIM>& lhs,
1089 bool rhs)
1090{
1091 Vector<bool,DIM> result;
1093 return result;
1094}
1095
1097template <Size DIM>
1099 const Vector<bool,DIM>& lhs,
1100 const Vector<bool,DIM>& rhs)
1101{
1102 Vector<bool,DIM> result;
1103 general::transform( lhs, rhs, result, functor::Operator_xor());
1104 return result;
1105}
1106
1108template <Size DIM>
1110 bool lhs,
1111 const Vector<bool,DIM>& rhs)
1112{
1113 Vector<bool,DIM> result;
1115 return result;
1116}
1117
1119template <Size DIM>
1121 const Vector<bool,DIM>& lhs,
1122 bool rhs)
1123{
1124 Vector<bool,DIM> result;
1126 return result;
1127}
1128
1130template <Size DIM>
1132 const Vector<bool,DIM>& vec)
1133{
1134 Vector<bool,DIM> result;
1136 return result;
1137}
1138
1139
1140//------ Elementwise comparison operators returning a bool vector. ------------
1141
1143template <typename T, Size DIM>
1145 const Vector<T,DIM>& lhs,
1146 const Vector<T,DIM>& rhs)
1147{
1148 Vector<bool,DIM> result;
1150 return result;
1151}
1152
1154template <typename T, Size DIM>
1156 const Vector<T,DIM>& lhs,
1157 const Vector<T,DIM>& rhs)
1158{
1159 Vector<bool,DIM> result;
1161 return result;
1162}
1163
1165template <typename T, Size DIM>
1167 const Vector<T,DIM>& lhs,
1168 const Vector<T,DIM>& rhs)
1169{
1170 Vector<bool,DIM> result;
1171 general::transform( lhs, rhs, result,functor::Operator_less());
1172 return result;
1173}
1174
1176template <typename T, Size DIM>
1178 const Vector<T,DIM>& lhs,
1179 const Vector<T,DIM>& rhs)
1180{
1181 Vector<bool,DIM> result;
1183 return result;
1184}
1185
1187template <typename T, Size DIM>
1189 const Vector<T,DIM>& lhs,
1190 const Vector<T,DIM>& rhs)
1191{
1192 Vector<bool,DIM> result;
1193 general::transform( lhs, rhs, result,functor::Operator_greater());
1194 return result;
1195}
1196
1198template <typename T, Size DIM>
1200 const Vector<T,DIM>& lhs,
1201 const Vector<T,DIM>& rhs)
1202{
1203 Vector<bool,DIM> result;
1205 return result;
1206}
1207
1208
1209//------ Function Overloads for Vector Algorithms -----------------------------
1210
1212template <typename T, Size DIM>
1214{
1215 Vector<T,DIM> result;
1216 for( Size i = 0; i != DIM; ++i)
1217 result[i] = abs( vector_base_ptr(v)[i]);
1218 return result;
1219}
1220
1222template <typename T, Size DIM>
1224{
1225 Vector<T,DIM> result;
1226 for( Size i = 0; i != DIM; ++i)
1227 result[i] = acos( vector_base_ptr(v)[i]);
1228 return result;
1229}
1230
1232template <typename T, Size DIM>
1233inline bool all( const Vector_struct<T,DIM>& v)
1234{
1235 for( Size i = 0; i != DIM; ++i)
1236 if( !all(vector_base_ptr(v)[i]))
1237 return false;
1238 return true;
1239}
1240
1242template <typename T, Size DIM>
1243inline bool any( const Vector_struct<T,DIM>& v)
1244{
1245 for( Size i = 0; i != DIM; ++i)
1246 if( any(vector_base_ptr(v)[i]))
1247 return true;
1248 return false;
1249}
1250
1252template <typename T, Size DIM>
1254{
1255 Vector<T,DIM> result;
1256 for( Size i = 0; i != DIM; ++i)
1257 result[i] = asin( vector_base_ptr(v)[i]);
1258 return result;
1259}
1260
1262template <typename T, Size DIM>
1264{
1265 Vector<T,DIM> result;
1266 for( Size i = 0; i != DIM; ++i)
1267 result[i] = atan( vector_base_ptr(v)[i]);
1268 return result;
1269}
1270
1274template <typename T, Size DIM>
1276{
1277 Vector<T,DIM> result;
1278 for( Size i = 0; i != DIM; ++i)
1279 result[i] = atan2( vector_base_ptr(v)[i], vector_base_ptr(w)[i]);
1280 return result;
1281}
1282
1285template <typename T, Size DIM>
1287{
1288 Vector<T,DIM> result;
1289 for( Size i = 0; i != DIM; ++i)
1290 result[i] = ceil( vector_base_ptr(v)[i]);
1291 return result;
1292}
1293
1295template <typename T, Size DIM>
1297 const Vector_struct<T,DIM>& v,
1298 const Vector_struct<T,DIM>& low,
1299 const Vector_struct<T,DIM>& high)
1300{
1301 Vector<T,DIM> result;
1302 for( Size i = 0u; i < DIM; ++i)
1303 result[i] = clamp(
1304 vector_base_ptr(v)[i], vector_base_ptr(low)[i], vector_base_ptr(high)[i]);
1305 return result;
1306}
1307
1309template <typename T, Size DIM>
1311 const Vector_struct<T,DIM>& v,
1312 const Vector_struct<T,DIM>& low,
1313 T high)
1314{
1315 Vector<T,DIM> result;
1316 for( Size i = 0u; i < DIM; ++i)
1317 result[i] = clamp( vector_base_ptr(v)[i], vector_base_ptr(low)[i], high);
1318 return result;
1319}
1320
1322template <typename T, Size DIM>
1324 const Vector_struct<T,DIM>& v,
1325 T low,
1326 const Vector_struct<T,DIM>& high)
1327{
1328 Vector<T,DIM> result;
1329 for( Size i = 0u; i < DIM; ++i)
1330 result[i] = clamp( vector_base_ptr(v)[i], low, vector_base_ptr(high)[i]);
1331 return result;
1332}
1333
1335template <typename T, Size DIM>
1337 const Vector_struct<T,DIM>& v,
1338 T low,
1339 T high)
1340{
1341 Vector<T,DIM> result;
1342 for( Size i = 0u; i < DIM; ++i)
1343 result[i] = clamp( vector_base_ptr(v)[i], low, high);
1344 return result;
1345}
1346
1348template <typename T, Size DIM>
1350{
1351 Vector<T,DIM> result;
1352 for( Size i = 0; i != DIM; ++i)
1353 result[i] = cos( vector_base_ptr(v)[i]);
1354 return result;
1355}
1356
1358template <typename T, Size DIM>
1360{
1361 Vector<T,DIM> result;
1362 for( Size i = 0; i != DIM; ++i)
1363 result[i] = degrees( vector_base_ptr(v)[i]);
1364 return result;
1365}
1366
1368template <typename T, Size DIM>
1370 const Vector_struct<T,DIM>& lhs,
1371 const Vector_struct<T,DIM>& rhs)
1372{
1373 Vector<T,DIM> r;
1374 for( Size i(0u); i < Vector<T,DIM>::DIMENSION; ++i)
1375 vector_base_ptr(r)[i] = base::max MI_PREVENT_MACRO_EXPAND (
1376 vector_base_ptr(lhs)[i], vector_base_ptr(rhs)[i] );
1377 return r;
1378}
1379
1381template <typename T, Size DIM>
1383 const Vector_struct<T,DIM>& lhs,
1384 const Vector_struct<T,DIM>& rhs)
1385{
1386 Vector<T,DIM> r;
1387 for( Size i(0u); i < Vector<T,DIM>::DIMENSION; ++i)
1388 vector_base_ptr(r)[i] = base::min MI_PREVENT_MACRO_EXPAND (
1389 vector_base_ptr(lhs)[i], vector_base_ptr(rhs)[i] );
1390 return r;
1391}
1392
1394template <typename T, Size DIM>
1396{
1397 Vector<T,DIM> result;
1398 for( Size i = 0; i != DIM; ++i)
1399 result[i] = exp( vector_base_ptr(v)[i]);
1400 return result;
1401}
1402
1404template <typename T, Size DIM>
1406{
1407 Vector<T,DIM> result;
1408 for( Size i = 0; i != DIM; ++i)
1409 result[i] = exp2( vector_base_ptr(v)[i]);
1410 return result;
1411}
1412
1415template <typename T, Size DIM>
1417{
1418 Vector<T,DIM> result;
1419 for( Size i = 0; i != DIM; ++i)
1420 result[i] = floor( vector_base_ptr(v)[i]);
1421 return result;
1422}
1423
1427template <typename T, Size DIM>
1429{
1430 Vector<T,DIM> result;
1431 for( Size i = 0; i != DIM; ++i)
1432 result[i] = fmod( vector_base_ptr(a)[i], vector_base_ptr(b)[i]);
1433 return result;
1434}
1435
1439template <typename T, Size DIM>
1441{
1442 Vector<T,DIM> result;
1443 for( Size i = 0; i != DIM; ++i)
1444 result[i] = fmod( vector_base_ptr(a)[i], b);
1445 return result;
1446}
1447
1449template <typename T, Size DIM>
1451{
1452 Vector<T,DIM> result;
1453 for( Size i = 0; i != DIM; ++i)
1454 result[i] = frac( vector_base_ptr(v)[i]);
1455 return result;
1456}
1457
1459template <typename T, Size DIM>
1461 const Vector_struct<T,DIM>& left,
1462 const Vector_struct<T,DIM>& right,
1463 T e)
1464{
1465 for( Size i = 0u; i < DIM; ++i)
1466 if( !is_approx_equal( vector_base_ptr(left)[i], vector_base_ptr(right)[i], e))
1467 return false;
1468 return true;
1469}
1470
1473template <typename T, Size DIM>
1475 const Vector_struct<T,DIM>& v1,
1476 const Vector_struct<T,DIM>& v2,
1477 const Vector_struct<T,DIM>& t)
1478{
1479 Vector<T,DIM> result;
1480 for( Size i = 0; i != DIM; ++i)
1481 result[i] = vector_base_ptr(v1)[i] * (T(1)-vector_base_ptr(t)[i])
1482 + vector_base_ptr(v2)[i] * vector_base_ptr(t)[i];
1483 return result;
1484}
1485
1488template <typename T, Size DIM>
1490 const Vector_struct<T,DIM>& v1,
1491 const Vector_struct<T,DIM>& v2,
1492 T t)
1493{
1494 // equivalent to: return v1 * (T(1)-t) + v2 * t;
1495 Vector<T,DIM> result;
1496 T t2 = T(1) - t;
1497 for( Size i = 0; i != DIM; ++i)
1498 result[i] = vector_base_ptr(v1)[i] * t2 + vector_base_ptr(v2)[i] * t;
1499 return result;
1500}
1501
1503template <typename T, Size DIM>
1505{
1506 Vector<T,DIM> result;
1507 for( Size i = 0; i != DIM; ++i)
1508 result[i] = log( vector_base_ptr(v)[i]);
1509 return result;
1510}
1511
1513template <typename T, Size DIM>
1515{
1516 Vector<T,DIM> result;
1517 for( Size i = 0; i != DIM; ++i)
1518 result[i] = log2 MI_PREVENT_MACRO_EXPAND ( vector_base_ptr(v)[i]);
1519 return result;
1520}
1521
1523template <typename T, Size DIM>
1525{
1526 Vector<T,DIM> result;
1527 for( Size i = 0; i != DIM; ++i)
1528 result[i] = log10( vector_base_ptr(v)[i]);
1529 return result;
1530}
1531
1536template <typename T, Size DIM>
1538{
1539 Vector<T,DIM> result;
1540 for( Size j = 0; j != DIM; ++j)
1541 result[j] = modf( vector_base_ptr(v)[j], vector_base_ptr(i)[j]);
1542 return result;
1543}
1544
1546template <typename T, Size DIM>
1548{
1549 Vector<T,DIM> result;
1550 for( Size i = 0; i != DIM; ++i)
1551 result[i] = pow( vector_base_ptr(a)[i], vector_base_ptr(b)[i]);
1552 return result;
1553}
1554
1556template <typename T, Size DIM>
1558{
1559 Vector<T,DIM> result;
1560 for( Size i = 0; i != DIM; ++i)
1561 result[i] = pow( vector_base_ptr(a)[i], b);
1562 return result;
1563}
1564
1566template <typename T, Size DIM>
1568{
1569 Vector<T,DIM> result;
1570 for( Size i = 0; i != DIM; ++i)
1571 result[i] = radians( vector_base_ptr(v)[i]);
1572 return result;
1573}
1574
1576template <typename T, Size DIM>
1578{
1579 Vector<T,DIM> result;
1580 for( Size i = 0; i != DIM; ++i)
1581 result[i] = round( vector_base_ptr(v)[i]);
1582 return result;
1583}
1584
1586template <typename T, Size DIM>
1588{
1589 Vector<T,DIM> result;
1590 for( Size i = 0; i != DIM; ++i)
1591 result[i] = rsqrt( vector_base_ptr(v)[i]);
1592 return result;
1593}
1594
1596template <typename T, Size DIM>
1598{
1599 Vector<T,DIM> result;
1600 for( Size i = 0; i != DIM; ++i)
1601 result[i] = saturate( vector_base_ptr(v)[i]);
1602 return result;
1603}
1604
1606template <typename T, Size DIM>
1608{
1609 Vector<T,DIM> result;
1610 for( Size i = 0; i != DIM; ++i)
1611 result[i] = sign( vector_base_ptr(v)[i]);
1612 return result;
1613}
1614
1616template <typename T, Size DIM>
1618{
1619 Vector<T,DIM> result;
1620 for( Size i = 0; i != DIM; ++i)
1621 result[i] = sin( vector_base_ptr(v)[i]);
1622 return result;
1623}
1624
1628template <typename T, Size DIM>
1630{
1631 for( Size i = 0; i != DIM; ++i)
1633}
1634
1640template <typename T, Size DIM>
1642 const Vector_struct<T,DIM>& a,
1643 const Vector_struct<T,DIM>& b,
1644 const Vector_struct<T,DIM>& v)
1645{
1646 Vector<T,DIM> result;
1647 for( Size i = 0; i != DIM; ++i)
1648 result[i] = smoothstep(
1649 vector_base_ptr(a)[i], vector_base_ptr(b)[i], vector_base_ptr(v)[i]);
1650 return result;
1651}
1652
1658template <typename T, Size DIM>
1660 const Vector_struct<T,DIM>& a,
1661 const Vector_struct<T,DIM>& b,
1662 T x)
1663{
1664 Vector<T,DIM> result;
1665 for( Size i = 0; i != DIM; ++i)
1666 result[i] = smoothstep( vector_base_ptr(a)[i], vector_base_ptr(b)[i], x);
1667 return result;
1668}
1669
1671template <typename T, Size DIM>
1673{
1674 Vector<T,DIM> result;
1675 for( Size i = 0; i != DIM; ++i)
1676 result[i] = sqrt( vector_base_ptr(v)[i]);
1677 return result;
1678}
1679
1681template <typename T, Size DIM>
1683{
1684 Vector<T,DIM> result;
1685 for( Size i = 0; i != DIM; ++i)
1686 result[i] = step( vector_base_ptr(a)[i], vector_base_ptr(v)[i]);
1687 return result;
1688}
1689
1691template <typename T, Size DIM>
1693{
1694 Vector<T,DIM> result;
1695 for( Size i = 0; i != DIM; ++i)
1696 result[i] = tan( vector_base_ptr(v)[i]);
1697 return result;
1698}
1699
1700
1701//------ Geometric Vector Algorithms ------------------------------------------
1702
1704template <typename T>
1705inline T cross(
1706 const Vector_struct<T,2>& lhs,
1707 const Vector_struct<T,2>& rhs)
1708{
1709 return lhs.x * rhs.y - lhs.y * rhs.x;
1710}
1711
1713template <typename T>
1715 const Vector_struct<T,3>& lhs,
1716 const Vector_struct<T,3>& rhs)
1717{
1718 return Vector<T,3>( lhs.y * rhs.z - lhs.z * rhs.y,
1719 lhs.z * rhs.x - lhs.x * rhs.z,
1720 lhs.x * rhs.y - lhs.y * rhs.x);
1721}
1722
1728template <typename T>
1729inline void make_basis(
1730 const Vector<T,3>& n,
1731 Vector<T,3>* u,
1732 Vector<T,3>* v)
1733{
1734#ifdef mi_base_assert_enabled
1735 const T eps = 1e-6f; // smallest resolvable factor
1736#endif
1737
1738 mi_math_assert_msg( u != 0, "precondition");
1739 mi_math_assert_msg( v != 0, "precondition");
1740 // Sanity check: the normal vector must be unit length.
1741 mi_math_assert_msg( abs( length(n) - 1.0f) < eps, "precondition");
1742
1743 // Compute u.
1744 if( abs(n.x) < abs(n.y)) {
1745 // u = cross(x, n), x = (1, 0, 0)
1746 u->x = T(0);
1747 u->y = -n.z;
1748 u->z = n.y;
1749 } else {
1750 // u = cross(y, n), y = (0, 1, 0)
1751 u->x = n.z;
1752 u->y = T(0);
1753 u->z = -n.x;
1754 }
1755 u->normalize();
1756
1757 // Compute v. Since *u and n are orthogonal and unit-length,
1758 // there is no need to normalize *v.
1759 *v = cross( *u, n);
1760
1761 // Sanity check: make sure (u, n, v) is an orthogonal basis.
1762 mi_math_assert_msg( abs( dot( *u, n)) < eps, "postcondition");
1763 mi_math_assert_msg( abs( dot( *u, *v)) < eps, "postcondition");
1764 mi_math_assert_msg( abs( dot( n, *v)) < eps, "postcondition");
1765 // Sanity check: make sure u and v are unit length.
1766 mi_math_assert_msg( abs( length( *u) - T(1)) < eps, "postcondition");
1767 mi_math_assert_msg( abs( length( *v) - T(1)) < eps, "postcondition");
1768}
1769
1776template <typename T>
1777inline void make_basis(
1778 const Vector<T,3>& n,
1779 const Vector<T,3>& u,
1780 const Vector<T,3>& v,
1781 Vector<T,3>* t,
1782 Vector<T,3>* b)
1783{
1784 const T eps = 1e-6f; // smallest resolvable factor
1785 (void)eps;
1786
1787 mi_math_assert_msg( t != 0, "precondition");
1788 mi_math_assert_msg( b != 0, "precondition");
1789 // Sanity check: the normal vector must be unit length.
1790 mi_math_assert_msg( abs( length( n) - 1.0f) < eps, "precondition");
1791 // Sanity check: the other vector lengths should be finite and non-zero
1792 mi_math_assert_msg( length( u) > 0., "precondition");
1793 mi_math_assert_msg( length( v) > 0., "precondition");
1794 mi_math_assert_msg( isfinite( length( u)), "precondition");
1795 mi_math_assert_msg( isfinite( length( v)), "precondition");
1796
1797 // Compute b
1798 *b = cross(u,n);
1799 b->normalize();
1800
1801 // Compute t. Since *b and n are orthogonal and unit-length,
1802 // there is no need to normalize *t.
1803 *t = cross(n,*b);
1804
1805 // Check that b has the same orientation of v
1806 if( dot( *b,v) < T(0))
1807 *b = -*b;
1808
1809 // Sanity check: make sure *u and t have the same orientation.
1810 mi_math_assert_msg( dot( u, *t) > T(0), "postcondition");
1811 // Sanity check: make sure (t, n, b) is an orthogonal basis.
1812 // We use a scaled epsilon in order to avoid false positives.
1813 mi_math_assert_msg( abs( dot( *t, n)) < 20*eps, "postcondition");
1814 mi_math_assert_msg( abs( dot( *t, *b)) < 20*eps, "postcondition");
1815 mi_math_assert_msg( abs( dot( n, *b)) < 20*eps, "postcondition");
1816 // Sanity check: make sure t and b are unit length.
1817 mi_math_assert_msg( abs( length( *t) - T(1)) < eps, "postcondition");
1818 mi_math_assert_msg( abs( length( *b) - T(1)) < eps, "postcondition");
1819}
1820
1826template <typename T2, Size DIM2, typename T1, Size DIM1>
1828 const Vector<T1, DIM1>& v,
1829 const T2& fill = T2(0))
1830{
1831 const Size dim_min = base::min MI_PREVENT_MACRO_EXPAND ( DIM1, DIM2 );
1832 Vector<T2, DIM2> result;
1833 for( Size i = 0; i < dim_min; ++i)
1834 result[i] = T2(v[i]);
1835 for( Size i = dim_min; i < DIM2; ++i)
1836 result[i] = fill;
1837 return result;
1838}
1839 // end group mi_math_vector
1841
1842} // namespace math
1843
1844} // namespace mi
1845
1846#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:65
#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
Sint64 Difference
Signed integral type that is large enough to hold the difference of two pointers.
Definition: types.h:122
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
float Float32
32-bit float.
Definition: types.h:51
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:1181
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:1197
Sint32 dot(Sint32 a, Sint32 b)
Returns the inner product (a.k.a. dot or scalar product) of two integers.
Definition: function.h:1088
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:1229
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:1168
Float32 length(Float32 a)
Returns the Euclidean norm of the scalar a (its absolute value).
Definition: function.h:1114
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:1213
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:1157
#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:470
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:564
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:535
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:578
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:518
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:550
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:486
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:502
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:672
Color frac(const Color &c)
Returns a color with the elementwise positive fractional part of the color c.
Definition: color.h:628
Color sqrt(const Color &c)
Returns the square root of each element of c.
Definition: color.h:807
Color acos(const Color &c)
Returns a color with the elementwise arc cosine of the color c.
Definition: color.h:480
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:519
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:525
Color abs(const Color &c)
Returns a color with the elementwise absolute values of the color c.
Definition: color.h:474
Color round(const Color &c)
Returns a color with the elements of color c rounded to nearest integers.
Definition: color.h:740
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:716
Color atan(const Color &c)
Returns a color with the elementwise arc tangent of the color c.
Definition: color.h:504
bool isfinite(const Color &c)
Indicates whether all components of the color are finite.
Definition: color.h:825
Color step(const Color &a, const Color &c)
Returns elementwise 0 if c is less than a and 1 otherwise.
Definition: color.h:813
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:584
Color tan(const Color &c)
Returns a color with the elementwise tangent of the color c.
Definition: color.h:819
Color atan2(const Color &c, const Color &d)
Returns a color with the elementwise arc tangent of the color c / d.
Definition: color.h:512
Color sin(const Color &c)
Returns a color with the elementwise sine of the color c.
Definition: color.h:764
Color degrees(const Color &c)
Converts elementwise radians in c to degrees.
Definition: color.h:567
Color saturate(const Color &c)
Returns the color c clamped elementwise to the range [0,1].
Definition: color.h:752
Color rsqrt(const Color &c)
Returns the reciprocal of the square root of each element of c.
Definition: color.h:746
bool any(const Color &c)
Returns true if any element of c is not equal to zero.
Definition: color.h:492
Color radians(const Color &c)
Converts elementwise degrees in c to radians.
Definition: color.h:734
Color exp2(const Color &c)
Returns a color with elementwise 2 to the power of the element in the color c.
Definition: color.h:599
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:574
Color log(const Color &c)
Returns a color with elementwise natural logarithm of the color c.
Definition: color.h:692
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:606
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:785
Color sign(const Color &c)
Returns the elementwise sign of color c.
Definition: color.h:758
Color fmod(const Color &a, const Color &b)
Returns elementwise a modulo b, in other words, the remainder of a/b.
Definition: color.h:614
Color cos(const Color &c)
Returns a color with the elementwise cosine of the color c.
Definition: color.h:561
Color pow(const Color &a, const Color &b)
Returns the color a elementwise to the power of b.
Definition: color.h:722
bool all(const Color &c)
Returns true if all elements of c are not equal to zero.
Definition: color.h:486
Color log10(const Color &c)
Returns a color with elementwise base 10 logarithm of the color c.
Definition: color.h:707
Color log2(const Color &c)
Returns a color with elementwise base 2 logarithm of the color c.
Definition: color.h:698
Color exp(const Color &c)
Returns a color with elementwise e to the power of the element in the color c.
Definition: color.h:593
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:653
void sincos(const Color &a, Color &s, Color &c)
Computes elementwise the sine s and cosine c of angles a simultaneously.
Definition: color.h:772
Color asin(const Color &c)
Returns a color with the elementwise arc sine of the color c.
Definition: color.h:498
void transform(const Vector &vec, ResultVector &result, UnaryFunctor f)
Generic transform function that applies a unary functor (return value).
Definition: function.h:234
void for_each(Vector &vec, UnaryFunctor f)
Generic transform function that applies a unary functor (in-place).
Definition: function.h:302
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:287
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:269
Difference difference_type
Difference type, signed.
Definition: vector.h:293
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:402
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:1098
T & reference
Mutable reference to element.
Definition: vector.h:296
void set(Size i, T value)
Sets the i-th vector element to value.
Definition: vector.h:632
Vector(T2 const (&array)[DIM])
Constructor initializes the vector elements from an array of dimension DIM.
Definition: vector.h:393
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:1705
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:534
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_struct<T, DIM> storage_type
Storage class used by this vector.
Definition: vector.h:289
const T & const_reference
Const reference to element.
Definition: vector.h:297
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:1188
Vector(const Color_struct &color)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from a color interp...
Definition: vector.h:560
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:1199
Vector_struct<T, DIM> Pod_type
POD class corresponding to this vector.
Definition: vector.h:288
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:521
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:375
bool operator<=(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically less than or equal to rhs.
Definition: vector.h:681
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:547
bool operator>=(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically greater than or equal to rhs.
Definition: vector.h:697
const T * const_pointer
Const pointer to element.
Definition: vector.h:295
bool operator>(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically greater than rhs.
Definition: vector.h:689
Vector(const Vector_struct<T, DIM> &vec)
Constructor from underlying storage type.
Definition: vector.h:348
Vector<T, DIM> & operator++(Vector<T, DIM> &vec)
Pre-increments all elements of vec and returns the result. Modifies vec.
Definition: vector.h:1013
Vector<T, DIM> & operator--(Vector<T, DIM> &vec)
Pre-decrements all elements of vec and returns the result. Modifies vec.
Definition: vector.h:1021
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:456
Vector(T v)
Constructor initializes all vector elements to the value v.
Definition: vector.h:355
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:1155
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:468
T & operator[](Size i)
Accesses the i-th vector element.
Definition: vector.h:605
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:411
static Size max_size()
Constant maximum size of the vector.
Definition: vector.h:306
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:1827
const T & get(Size i) const
Returns the i-th vector element.
Definition: vector.h:623
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:742
T value_type
Element type.
Definition: vector.h:291
Size size_type
Size type, unsigned.
Definition: vector.h:292
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.
static Size size()
Constant size of the vector.
Definition: vector.h:303
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:481
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:659
static const Size SIZE
Constant size of the vector.
Definition: vector.h:300
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:508
T z
z-coordinate.
Definition: vector.h:165
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:1177
T z
z-coordinate.
Definition: vector.h:157
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:432
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:795
Float32 r
Red color component.
Definition: vector.h:69
bool normalize()
Normalizes this vector to unit length.
Definition: vector.h:646
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:1032
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:1131
bool operator<(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically less than rhs.
Definition: vector.h:673
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:1065
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:421
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:1144
T * pointer
Mutable pointer to element.
Definition: vector.h:294
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:444
void make_basis(const Vector<T, 3> &n, Vector<T, 3> *u, Vector<T, 3> *v)
Computes a basis of 3D space with one given vector.
Definition: vector.h:1729
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:1166
bool operator!=(Vector<T, DIM> rhs) const
Returns true if lhs is elementwise not equal to rhs.
Definition: vector.h:665
T x
x-coordinate.
Definition: vector.h:142
static const Size DIMENSION
Constant dimension of the vector.
Definition: vector.h:299
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:495
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: math.h:22
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:149
Functor for the equality comparison operator, ==.
Definition: function.h:76
Functor for the greater-than-or-equal comparison operator, >=.
Definition: function.h:111
Functor for the greater-than comparison operator, >.
Definition: function.h:104
Functor for the less-than-or-equal comparison operator, <=.
Definition: function.h:97
Functor for the less-than comparison operator, <.
Definition: function.h:90
Functor for the inequality comparison operator, !=.
Definition: function.h:83
Functor for the logical not operator, !.
Definition: function.h:170
Functor for the logical or operator, ||.
Definition: function.h:156
Functor for the pre-decrement operator, --.
Definition: function.h:191
Functor for the pre-increment operator, ++.
Definition: function.h:177
Functor for the xor operator, ^.
Definition: function.h:163
Basic types.