MDL SDK API nvidia_logo_transpbg.gif Up
vector.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2024 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#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 typedef mi::base::numeric_traits<T> Traits;
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
339#if (__cplusplus >= 201103L)
341 Vector( const Vector<T,DIM>& vec ) = default;
342#endif
343
345 inline Vector( const Vector_struct<T,DIM>& vec )
346 {
347 for( Size i(0u); i < DIM; ++i)
348 (*this)[i] = mi::math::vector_base_ptr(vec)[i];
349 }
350
352 inline explicit Vector(T v)
353 {
354 for( Size i(0u); i < DIM; ++i)
355 (*this)[i] = v;
356 }
357
371 template <typename Iterator>
372 inline Vector(From_iterator_tag, Iterator p)
373 {
374 for( Size i(0u); i < DIM; ++i, ++p)
375 (*this)[i] = *p;
376 }
377
389 template <typename T2>
390 inline explicit Vector( T2 const (& array)[DIM])
391 {
392 for( Size i(0u); i < DIM; ++i)
393 (*this)[i] = array[i];
394 }
395
398 template <typename T2>
399 inline explicit Vector( const Vector<T2,DIM>& other)
400 {
401 for( Size i(0u); i < DIM; ++i)
402 (*this)[i] = T(other[i]);
403 }
404
407 template <typename T2>
408 inline explicit Vector( const Vector_struct<T2,DIM>& other)
409 {
410 for( Size i(0u); i < DIM; ++i)
411 (*this)[i] = T(mi::math::vector_base_ptr(other)[i]);
412 }
413
418 inline Vector(T v1, T v2)
419 {
420 mi_static_assert(DIM == 2);
421 begin()[0] = v1;
422 begin()[1] = v2;
423 }
424
429 inline Vector(T v1, T v2, T v3)
430 {
431 mi_static_assert(DIM == 3);
432 begin()[0] = v1;
433 begin()[1] = v2;
434 begin()[2] = v3;
435 }
436
441 inline Vector(T v1, const Vector<T,2>& v2)
442 {
443 mi_static_assert(DIM == 3);
444 begin()[0] = v1;
445 begin()[1] = v2.x;
446 begin()[2] = v2.y;
447 }
448
453 inline Vector(const Vector<T,2>& v1, T v2)
454 {
455 mi_static_assert(DIM == 3);
456 begin()[0] = v1.x;
457 begin()[1] = v1.y;
458 begin()[2] = v2;
459 }
460
465 inline Vector(T v1, T v2, T v3, T v4)
466 {
467 mi_static_assert(DIM == 4);
468 begin()[0] = v1;
469 begin()[1] = v2;
470 begin()[2] = v3;
471 begin()[3] = v4;
472 }
473
478 inline Vector(T v1, T v2, const Vector<T,2>& v3)
479 {
480 mi_static_assert(DIM == 4);
481 begin()[0] = v1;
482 begin()[1] = v2;
483 begin()[2] = v3.x;
484 begin()[3] = v3.y;
485 }
486
487
492 inline Vector(T v1, const Vector<T,2>& v2, T v3)
493 {
494 mi_static_assert(DIM == 4);
495 begin()[0] = v1;
496 begin()[1] = v2.x;
497 begin()[2] = v2.y;
498 begin()[3] = v3;
499 }
500
505 inline Vector(const Vector<T,2>& v1, T v2, T v3)
506 {
507 mi_static_assert(DIM == 4);
508 begin()[0] = v1.x;
509 begin()[1] = v1.y;
510 begin()[2] = v2;
511 begin()[3] = v3;
512 }
513
518 inline Vector(const Vector<T,2>& v1, const Vector<T,2>& v2)
519 {
520 mi_static_assert(DIM == 4);
521 begin()[0] = v1.x;
522 begin()[1] = v1.y;
523 begin()[2] = v2.x;
524 begin()[3] = v2.y;
525 }
526
531 inline Vector(T v1, const Vector<T,3>& v2)
532 {
533 mi_static_assert(DIM == 4);
534 begin()[0] = v1;
535 begin()[1] = v2.x;
536 begin()[2] = v2.y;
537 begin()[3] = v2.z;
538 }
539
544 inline Vector(const Vector<T,3>& v1, T v2)
545 {
546 mi_static_assert(DIM == 4);
547 begin()[0] = v1.x;
548 begin()[1] = v1.y;
549 begin()[2] = v1.z;
550 begin()[3] = v2;
551 }
552
557 inline explicit Vector( const Color_struct& color)
558 {
559 mi_static_assert(DIM == 4);
560 this->x = color.r;
561 this->y = color.g;
562 this->z = color.b;
563 this->w = color.a;
564 }
565
567#if (__cplusplus >= 201103L)
568 Vector& operator=( const Vector& other) = default;
569#else
570 inline Vector& operator=( const Vector& other)
571 {
572 for( Size i(0u); i < DIM; ++i)
573 (*this)[i] = other[i];
574 return *this;
575 }
576#endif
577
579 inline Vector& operator=( T s)
580 {
581 for( Size i(0u); i < DIM; ++i)
582 (*this)[i] = s;
583 return *this;
584 }
589 inline Vector& operator=( const Color_struct& color)
590 {
591 mi_static_assert(DIM == 4);
592 this->x = color.r;
593 this->y = color.g;
594 this->z = color.b;
595 this->w = color.a;
596 return *this;
597 }
598
602 inline T& operator[](Size i)
603 {
604 mi_math_assert_msg(i < DIM, "precondition");
605 return begin()[i];
606 }
607
611 inline const T& operator[](Size i) const
612 {
613 mi_math_assert_msg(i < DIM, "precondition");
614 return begin()[i];
615 }
616
620 inline const T& get(Size i) const
621 {
622 mi_math_assert_msg(i < DIM, "precondition");
623 return begin()[i];
624 }
625
629 inline void set(Size i, T value)
630 {
631 mi_math_assert_msg(i < DIM, "precondition");
632 begin()[i] = value;
633 }
634
635
636
643 inline bool normalize()
644 {
645 const T rec_length = T(1) / length( *this);
646 const bool result = isfinite( rec_length); //-V601 PVS
647 if( result)
648 (*this) *= rec_length;
649 return result;
650 }
651
652
653 //------ Free comparison operators ==, !=, <, <=, >, >= for vectors --------
654
656 inline bool operator==( Vector<T,DIM> rhs) const
657 {
658 return is_equal( *this, rhs);
659 }
660
662 inline bool operator!=( Vector<T,DIM> rhs) const
663 {
664 return is_not_equal( *this, rhs);
665 }
666
670 inline bool operator<( Vector<T,DIM> rhs) const
671 {
672 return lexicographically_less( *this, rhs);
673 }
674
678 inline bool operator<=( Vector<T,DIM> rhs) const
679 {
680 return lexicographically_less_or_equal( *this, rhs);
681 }
682
686 inline bool operator>( Vector<T,DIM> rhs) const
687 {
688 return lexicographically_greater( *this, rhs);
689 }
690
694 inline bool operator>=( Vector<T,DIM> rhs) const
695 {
696 return lexicographically_greater_or_equal( *this, rhs);
697 }
698};
699
700
701//------ Free operators +=, -=, *=, /=, +, -, *, and / for vectors -------------
702
704template <typename T, Size DIM>
706 Vector<T,DIM>& lhs,
707 const Vector_struct<T,DIM>& rhs)
708{
709 for( Size i(0u); i < DIM; ++i)
710 vector_base_ptr(lhs)[i] += vector_base_ptr(rhs)[i];
711 return lhs;
712}
713
715template <typename T, Size DIM>
717 Vector<T,DIM>& lhs,
718 const Vector_struct<T,DIM>& rhs)
719{
720 for( Size i(0u); i < DIM; ++i)
721 vector_base_ptr(lhs)[i] -= vector_base_ptr(rhs)[i];
722 return lhs;
723}
724
726template <typename T, Size DIM>
728 Vector<T,DIM>& lhs,
729 const Vector_struct<T,DIM>& rhs)
730{
731 for( Size i(0u); i < DIM; ++i)
732 vector_base_ptr(lhs)[i] *= vector_base_ptr(rhs)[i];
733 return lhs;
734}
735
738template <typename T, Size DIM>
740 Vector<T,DIM>& lhs,
741 const Vector_struct<T,DIM>& rhs)
742{
743 for( Size i(0u); i < DIM; ++i)
744 vector_base_ptr(lhs)[i] %= vector_base_ptr(rhs)[i];
745 return lhs;
746}
747
749template <typename T, typename U, Size DIM>
751 Vector<T,DIM>& lhs,
752 const Vector_struct<U,DIM>& rhs)
753{
754 for( Size i(0u); i < DIM; ++i)
755 vector_base_ptr(lhs)[i] = T(vector_base_ptr(lhs)[i] / vector_base_ptr(rhs)[i]);
756 return lhs;
757}
758
760template <typename T, Size DIM>
762 const Vector_struct<T,DIM>& lhs,
763 const Vector_struct<T,DIM>& rhs)
764{
765 Vector<T,DIM> tmp( lhs);
766 return tmp += rhs;
767}
768
770template <typename T, Size DIM>
772 const Vector_struct<T,DIM>& lhs,
773 const Vector_struct<T,DIM>& rhs)
774{
775 Vector<T,DIM> tmp( lhs);
776 return tmp -= rhs;
777}
778
780template <typename T, Size DIM>
782 const Vector_struct<T,DIM>& lhs,
783 const Vector_struct<T,DIM>& rhs)
784{
785 Vector<T,DIM> tmp( lhs);
786 return tmp *= rhs;
787}
788
791template <typename T, Size DIM>
793 const Vector_struct<T,DIM>& lhs,
794 const Vector_struct<T,DIM>& rhs)
795{
796 Vector<T,DIM> tmp( lhs);
797 return tmp %= rhs;
798}
799
801template <typename T, typename U, Size DIM>
803 const Vector_struct<T,DIM>& lhs,
804 const Vector_struct<U,DIM>& rhs)
805{
806 Vector<T,DIM> tmp(lhs);
807 return tmp /= rhs;
808}
809
811template <typename T, Size DIM>
813{
814 Vector<T,DIM> tmp;
815 for( Size i(0u); i < DIM; ++i)
816 vector_base_ptr(tmp)[i] = -vector_base_ptr(v)[i];
817 return tmp;
818}
819
820
821//------ Resolve ambiguity with hybrid operators -------------
822// (Those operators should really be restricted to scalar types but that requires
823// modern C++ which some users of this headers don't enable.)
824
826template <typename T, Size DIM>
828 Vector<T,DIM>& lhs,
829 const Vector<T,DIM>& rhs)
830{
831 return lhs += static_cast<const Vector_struct<T,DIM>&>(rhs);
832}
833
835template <typename T, Size DIM>
837 Vector<T,DIM>& lhs,
838 const Vector<T,DIM>& rhs)
839{
840 return lhs -= static_cast<const Vector_struct<T,DIM>&>(rhs);
841}
842
844template <typename T, Size DIM>
846 Vector<T,DIM>& lhs,
847 const Vector<T,DIM>& rhs)
848{
849 return lhs *= static_cast<const Vector_struct<T,DIM>&>(rhs);
850}
851
854template <typename T, Size DIM>
856 Vector<T,DIM>& lhs,
857 const Vector<T,DIM>& rhs)
858{
859 return lhs %= static_cast<const Vector_struct<T,DIM>&>(rhs);
860}
861
863template <typename T, typename U, Size DIM>
865 Vector<T,DIM>& lhs,
866 const Vector<U,DIM>& rhs)
867{
868 return lhs /= static_cast<const Vector_struct<U,DIM>&>(rhs);
869}
870
872template <typename T, Size DIM>
874 const Vector<T,DIM>& lhs,
875 const Vector<T,DIM>& rhs)
876{
877 return lhs + static_cast<const Vector_struct<T,DIM>&>(rhs);
878}
879
881template <typename T, Size DIM>
883 const Vector<T,DIM>& lhs,
884 const Vector<T,DIM>& rhs)
885{
886 return lhs - static_cast<const Vector_struct<T,DIM>&>(rhs);
887}
888
890template <typename T, Size DIM>
892 const Vector<T,DIM>& lhs,
893 const Vector<T,DIM>& rhs)
894{
895 return lhs * static_cast<const Vector_struct<T,DIM>&>(rhs);
896}
897
900template <typename T, Size DIM>
902 const Vector<T,DIM>& lhs,
903 const Vector<T,DIM>& rhs)
904{
905 return lhs % static_cast<const Vector_struct<T,DIM>&>(rhs);
906}
907
909template <typename T, typename U, Size DIM>
911 const Vector<T,DIM>& lhs,
912 const Vector<U,DIM>& rhs)
913{
914 return lhs / static_cast<const Vector_struct<U,DIM>&>(rhs);
915}
916
918template <typename T, Size DIM>
920{
921 return -static_cast<const Vector_struct<T,DIM>&>(v);
922}
923
924
925//------ Free operator *=, /=, *, and / definitions for scalars ---------------
926
929template <typename T, typename TT, Size DIM>
931 Vector<T,DIM>& v,
932 TT s)
933{
934 for( Size i(0u); i < DIM; ++i)
935 vector_base_ptr(v)[i] = T(vector_base_ptr(v)[i] * s);
936 return v;
937}
938
942template <typename T, typename TT, Size DIM>
944 Vector<T,DIM>& v,
945 TT s)
946{
947 for( Size i(0u); i < DIM; ++i)
948 vector_base_ptr(v)[i] = T(vector_base_ptr(v)[i] % s);
949 return v;
950}
951
953template <typename T, typename TT, Size DIM>
955 Vector<T,DIM>& v,
956 TT s)
957{
958 for( Size i(0u); i < DIM; ++i)
959 vector_base_ptr(v)[i] = T(vector_base_ptr(v)[i] / s);
960 return v;
961}
962
964template <typename T, typename TT, Size DIM>
966 const Vector_struct<T,DIM>& v,
967 TT s)
968{
969 Vector<T,DIM> tmp( v);
970 return tmp *= s;
971}
972
974template <typename T, typename TT, Size DIM>
976 TT s,
977 const Vector_struct<T,DIM>& v)
978{
979 Vector<T,DIM> tmp(v);
980 return tmp *= s;
981}
982
986template <typename T, typename TT, Size DIM>
988 const Vector_struct<T,DIM>& v,
989 TT s)
990{
991 Vector<T,DIM> tmp(v);
992 return tmp %= s;
993}
994
996template <typename T, typename TT, Size DIM>
998 const Vector_struct<T,DIM>& v,
999 TT s)
1000{
1001 Vector<T,DIM> tmp( v);
1002 return tmp /= s;
1003}
1004
1005
1006//------ Free operator ++, -- for vectors -------------------------------------
1007
1009template <typename T, Size DIM>
1011{
1013 return vec;
1014}
1015
1017template <typename T, Size DIM>
1019{
1021 return vec;
1022}
1023
1024
1025//------ Free operators !, &&, ||, ^ for bool vectors and bool scalars ---------
1026
1028template <Size DIM>
1030 const Vector<bool,DIM>& lhs,
1031 const Vector<bool,DIM>& rhs)
1032{
1033 Vector<bool,DIM> result;
1034 general::transform( lhs, rhs, result, functor::Operator_and_and());
1035 return result;
1036}
1037
1039template <Size DIM>
1041 bool lhs,
1042 const Vector<bool,DIM>& rhs)
1043{
1044 Vector<bool,DIM> result;
1046 return result;
1047}
1048
1050template <Size DIM>
1052 const Vector<bool,DIM>& lhs,
1053 bool rhs)
1054{
1055 Vector<bool,DIM> result;
1057 return result;
1058}
1059
1061template <Size DIM>
1063 const Vector<bool,DIM>& lhs,
1064 const Vector<bool,DIM>& rhs)
1065{
1066 Vector<bool,DIM> result;
1067 general::transform(lhs, rhs, result, functor::Operator_or_or());
1068 return result;
1069}
1070
1072template <Size DIM>
1074 bool lhs,
1075 const Vector<bool,DIM>& rhs)
1076{
1077 Vector<bool,DIM> result;
1079 return result;
1080}
1081
1083template <Size DIM>
1085 const Vector<bool,DIM>& lhs,
1086 bool rhs)
1087{
1088 Vector<bool,DIM> result;
1090 return result;
1091}
1092
1094template <Size DIM>
1096 const Vector<bool,DIM>& lhs,
1097 const Vector<bool,DIM>& rhs)
1098{
1099 Vector<bool,DIM> result;
1100 general::transform( lhs, rhs, result, functor::Operator_xor());
1101 return result;
1102}
1103
1105template <Size DIM>
1107 bool lhs,
1108 const Vector<bool,DIM>& rhs)
1109{
1110 Vector<bool,DIM> result;
1112 return result;
1113}
1114
1116template <Size DIM>
1118 const Vector<bool,DIM>& lhs,
1119 bool rhs)
1120{
1121 Vector<bool,DIM> result;
1123 return result;
1124}
1125
1127template <Size DIM>
1129 const Vector<bool,DIM>& vec)
1130{
1131 Vector<bool,DIM> result;
1133 return result;
1134}
1135
1136
1137//------ Elementwise comparison operators returning a bool vector. ------------
1138
1140template <typename T, Size DIM>
1142 const Vector<T,DIM>& lhs,
1143 const Vector<T,DIM>& rhs)
1144{
1145 Vector<bool,DIM> result;
1147 return result;
1148}
1149
1151template <typename T, Size DIM>
1153 const Vector<T,DIM>& lhs,
1154 const Vector<T,DIM>& rhs)
1155{
1156 Vector<bool,DIM> result;
1158 return result;
1159}
1160
1162template <typename T, Size DIM>
1164 const Vector<T,DIM>& lhs,
1165 const Vector<T,DIM>& rhs)
1166{
1167 Vector<bool,DIM> result;
1168 general::transform( lhs, rhs, result,functor::Operator_less());
1169 return result;
1170}
1171
1173template <typename T, Size DIM>
1175 const Vector<T,DIM>& lhs,
1176 const Vector<T,DIM>& rhs)
1177{
1178 Vector<bool,DIM> result;
1180 return result;
1181}
1182
1184template <typename T, Size DIM>
1186 const Vector<T,DIM>& lhs,
1187 const Vector<T,DIM>& rhs)
1188{
1189 Vector<bool,DIM> result;
1190 general::transform( lhs, rhs, result,functor::Operator_greater());
1191 return result;
1192}
1193
1195template <typename T, Size DIM>
1197 const Vector<T,DIM>& lhs,
1198 const Vector<T,DIM>& rhs)
1199{
1200 Vector<bool,DIM> result;
1202 return result;
1203}
1204
1205
1206//------ Function Overloads for Vector Algorithms -----------------------------
1207
1209template <typename T, Size DIM>
1211{
1212 Vector<T,DIM> result;
1213 for( Size i = 0; i != DIM; ++i)
1214 result[i] = abs( vector_base_ptr(v)[i]);
1215 return result;
1216}
1217
1219template <typename T, Size DIM>
1221{
1222 Vector<T,DIM> result;
1223 for( Size i = 0; i != DIM; ++i)
1224 result[i] = acos( vector_base_ptr(v)[i]);
1225 return result;
1226}
1227
1229template <typename T, Size DIM>
1230inline bool all( const Vector_struct<T,DIM>& v)
1231{
1232 for( Size i = 0; i != DIM; ++i)
1233 if( !all(vector_base_ptr(v)[i]))
1234 return false;
1235 return true;
1236}
1237
1239template <typename T, Size DIM>
1240inline bool any( const Vector_struct<T,DIM>& v)
1241{
1242 for( Size i = 0; i != DIM; ++i)
1243 if( any(vector_base_ptr(v)[i]))
1244 return true;
1245 return false;
1246}
1247
1249template <typename T, Size DIM>
1251{
1252 Vector<T,DIM> result;
1253 for( Size i = 0; i != DIM; ++i)
1254 result[i] = asin( vector_base_ptr(v)[i]);
1255 return result;
1256}
1257
1259template <typename T, Size DIM>
1261{
1262 Vector<T,DIM> result;
1263 for( Size i = 0; i != DIM; ++i)
1264 result[i] = atan( vector_base_ptr(v)[i]);
1265 return result;
1266}
1267
1271template <typename T, Size DIM>
1273{
1274 Vector<T,DIM> result;
1275 for( Size i = 0; i != DIM; ++i)
1276 result[i] = atan2( vector_base_ptr(v)[i], vector_base_ptr(w)[i]);
1277 return result;
1278}
1279
1282template <typename T, Size DIM>
1284{
1285 Vector<T,DIM> result;
1286 for( Size i = 0; i != DIM; ++i)
1287 result[i] = ceil( vector_base_ptr(v)[i]);
1288 return result;
1289}
1290
1292template <typename T, Size DIM>
1294 const Vector_struct<T,DIM>& v,
1295 const Vector_struct<T,DIM>& low,
1296 const Vector_struct<T,DIM>& high)
1297{
1298 Vector<T,DIM> result;
1299 for( Size i = 0u; i < DIM; ++i)
1300 result[i] = clamp(
1301 vector_base_ptr(v)[i], vector_base_ptr(low)[i], vector_base_ptr(high)[i]);
1302 return result;
1303}
1304
1306template <typename T, Size DIM>
1308 const Vector_struct<T,DIM>& v,
1309 const Vector_struct<T,DIM>& low,
1310 T high)
1311{
1312 Vector<T,DIM> result;
1313 for( Size i = 0u; i < DIM; ++i)
1314 result[i] = clamp( vector_base_ptr(v)[i], vector_base_ptr(low)[i], high);
1315 return result;
1316}
1317
1319template <typename T, Size DIM>
1321 const Vector_struct<T,DIM>& v,
1322 T low,
1323 const Vector_struct<T,DIM>& high)
1324{
1325 Vector<T,DIM> result;
1326 for( Size i = 0u; i < DIM; ++i)
1327 result[i] = clamp( vector_base_ptr(v)[i], low, vector_base_ptr(high)[i]);
1328 return result;
1329}
1330
1332template <typename T, Size DIM>
1334 const Vector_struct<T,DIM>& v,
1335 T low,
1336 T high)
1337{
1338 Vector<T,DIM> result;
1339 for( Size i = 0u; i < DIM; ++i)
1340 result[i] = clamp( vector_base_ptr(v)[i], low, high);
1341 return result;
1342}
1343
1345template <typename T, Size DIM>
1347{
1348 Vector<T,DIM> result;
1349 for( Size i = 0; i != DIM; ++i)
1350 result[i] = cos( vector_base_ptr(v)[i]);
1351 return result;
1352}
1353
1355template <typename T, Size DIM>
1357{
1358 Vector<T,DIM> result;
1359 for( Size i = 0; i != DIM; ++i)
1360 result[i] = degrees( vector_base_ptr(v)[i]);
1361 return result;
1362}
1363
1365template <typename T, Size DIM>
1367 const Vector_struct<T,DIM>& lhs,
1368 const Vector_struct<T,DIM>& rhs)
1369{
1370 Vector<T,DIM> r;
1371 for( Size i(0u); i < Vector<T,DIM>::DIMENSION; ++i)
1372 vector_base_ptr(r)[i] = base::max MI_PREVENT_MACRO_EXPAND (
1373 vector_base_ptr(lhs)[i], vector_base_ptr(rhs)[i] );
1374 return r;
1375}
1376
1378template <typename T, Size DIM>
1380 const Vector_struct<T,DIM>& lhs,
1381 const Vector_struct<T,DIM>& rhs)
1382{
1383 Vector<T,DIM> r;
1384 for( Size i(0u); i < Vector<T,DIM>::DIMENSION; ++i)
1385 vector_base_ptr(r)[i] = base::min MI_PREVENT_MACRO_EXPAND (
1386 vector_base_ptr(lhs)[i], vector_base_ptr(rhs)[i] );
1387 return r;
1388}
1389
1391template <typename T, Size DIM>
1393{
1394 Vector<T,DIM> result;
1395 for( Size i = 0; i != DIM; ++i)
1396 result[i] = exp( vector_base_ptr(v)[i]);
1397 return result;
1398}
1399
1401template <typename T, Size DIM>
1403{
1404 Vector<T,DIM> result;
1405 for( Size i = 0; i != DIM; ++i)
1406 result[i] = exp2( vector_base_ptr(v)[i]);
1407 return result;
1408}
1409
1412template <typename T, Size DIM>
1414{
1415 Vector<T,DIM> result;
1416 for( Size i = 0; i != DIM; ++i)
1417 result[i] = floor( vector_base_ptr(v)[i]);
1418 return result;
1419}
1420
1424template <typename T, Size DIM>
1426{
1427 Vector<T,DIM> result;
1428 for( Size i = 0; i != DIM; ++i)
1429 result[i] = fmod( vector_base_ptr(a)[i], vector_base_ptr(b)[i]);
1430 return result;
1431}
1432
1436template <typename T, Size DIM>
1438{
1439 Vector<T,DIM> result;
1440 for( Size i = 0; i != DIM; ++i)
1441 result[i] = fmod( vector_base_ptr(a)[i], b);
1442 return result;
1443}
1444
1446template <typename T, Size DIM>
1448{
1449 Vector<T,DIM> result;
1450 for( Size i = 0; i != DIM; ++i)
1451 result[i] = frac( vector_base_ptr(v)[i]);
1452 return result;
1453}
1454
1456template <typename T, Size DIM>
1458 const Vector_struct<T,DIM>& left,
1459 const Vector_struct<T,DIM>& right,
1460 T e)
1461{
1462 for( Size i = 0u; i < DIM; ++i)
1463 if( !is_approx_equal( vector_base_ptr(left)[i], vector_base_ptr(right)[i], e))
1464 return false;
1465 return true;
1466}
1467
1470template <typename T, Size DIM>
1472 const Vector_struct<T,DIM>& v1,
1473 const Vector_struct<T,DIM>& v2,
1474 const Vector_struct<T,DIM>& t)
1475{
1476 Vector<T,DIM> result;
1477 for( Size i = 0; i != DIM; ++i)
1478 result[i] = vector_base_ptr(v1)[i] * (T(1)-vector_base_ptr(t)[i])
1479 + vector_base_ptr(v2)[i] * vector_base_ptr(t)[i];
1480 return result;
1481}
1482
1485template <typename T, Size DIM>
1487 const Vector_struct<T,DIM>& v1,
1488 const Vector_struct<T,DIM>& v2,
1489 T t)
1490{
1491 // equivalent to: return v1 * (T(1)-t) + v2 * t;
1492 Vector<T,DIM> result;
1493 T t2 = T(1) - t;
1494 for( Size i = 0; i != DIM; ++i)
1495 result[i] = vector_base_ptr(v1)[i] * t2 + vector_base_ptr(v2)[i] * t;
1496 return result;
1497}
1498
1500template <typename T, Size DIM>
1502{
1503 Vector<T,DIM> result;
1504 for( Size i = 0; i != DIM; ++i)
1505 result[i] = log( vector_base_ptr(v)[i]);
1506 return result;
1507}
1508
1510template <typename T, Size DIM>
1512{
1513 Vector<T,DIM> result;
1514 for( Size i = 0; i != DIM; ++i)
1515 result[i] = log2 MI_PREVENT_MACRO_EXPAND ( vector_base_ptr(v)[i]);
1516 return result;
1517}
1518
1520template <typename T, Size DIM>
1522{
1523 Vector<T,DIM> result;
1524 for( Size i = 0; i != DIM; ++i)
1525 result[i] = log10( vector_base_ptr(v)[i]);
1526 return result;
1527}
1528
1533template <typename T, Size DIM>
1535{
1536 Vector<T,DIM> result;
1537 for( Size j = 0; j != DIM; ++j)
1538 result[j] = modf( vector_base_ptr(v)[j], vector_base_ptr(i)[j]);
1539 return result;
1540}
1541
1543template <typename T, Size DIM>
1545{
1546 Vector<T,DIM> result;
1547 for( Size i = 0; i != DIM; ++i)
1548 result[i] = pow( vector_base_ptr(a)[i], vector_base_ptr(b)[i]);
1549 return result;
1550}
1551
1553template <typename T, Size DIM>
1555{
1556 Vector<T,DIM> result;
1557 for( Size i = 0; i != DIM; ++i)
1558 result[i] = pow( vector_base_ptr(a)[i], b);
1559 return result;
1560}
1561
1563template <typename T, Size DIM>
1565{
1566 Vector<T,DIM> result;
1567 for( Size i = 0; i != DIM; ++i)
1568 result[i] = radians( vector_base_ptr(v)[i]);
1569 return result;
1570}
1571
1573template <typename T, Size DIM>
1575{
1576 Vector<T,DIM> result;
1577 for( Size i = 0; i != DIM; ++i)
1578 result[i] = round( vector_base_ptr(v)[i]);
1579 return result;
1580}
1581
1583template <typename T, Size DIM>
1585{
1586 Vector<T,DIM> result;
1587 for( Size i = 0; i != DIM; ++i)
1588 result[i] = rsqrt( vector_base_ptr(v)[i]);
1589 return result;
1590}
1591
1593template <typename T, Size DIM>
1595{
1596 Vector<T,DIM> result;
1597 for( Size i = 0; i != DIM; ++i)
1598 result[i] = saturate( vector_base_ptr(v)[i]);
1599 return result;
1600}
1601
1603template <typename T, Size DIM>
1605{
1606 Vector<T,DIM> result;
1607 for( Size i = 0; i != DIM; ++i)
1608 result[i] = sign( vector_base_ptr(v)[i]);
1609 return result;
1610}
1611
1613template <typename T, Size DIM>
1615{
1616 Vector<T,DIM> result;
1617 for( Size i = 0; i != DIM; ++i)
1618 result[i] = sin( vector_base_ptr(v)[i]);
1619 return result;
1620}
1621
1625template <typename T, Size DIM>
1627{
1628 for( Size i = 0; i != DIM; ++i)
1630}
1631
1637template <typename T, Size DIM>
1639 const Vector_struct<T,DIM>& a,
1640 const Vector_struct<T,DIM>& b,
1641 const Vector_struct<T,DIM>& v)
1642{
1643 Vector<T,DIM> result;
1644 for( Size i = 0; i != DIM; ++i)
1645 result[i] = smoothstep(
1646 vector_base_ptr(a)[i], vector_base_ptr(b)[i], vector_base_ptr(v)[i]);
1647 return result;
1648}
1649
1655template <typename T, Size DIM>
1657 const Vector_struct<T,DIM>& a,
1658 const Vector_struct<T,DIM>& b,
1659 T x)
1660{
1661 Vector<T,DIM> result;
1662 for( Size i = 0; i != DIM; ++i)
1663 result[i] = smoothstep( vector_base_ptr(a)[i], vector_base_ptr(b)[i], x);
1664 return result;
1665}
1666
1668template <typename T, Size DIM>
1670{
1671 Vector<T,DIM> result;
1672 for( Size i = 0; i != DIM; ++i)
1673 result[i] = sqrt( vector_base_ptr(v)[i]);
1674 return result;
1675}
1676
1678template <typename T, Size DIM>
1680{
1681 Vector<T,DIM> result;
1682 for( Size i = 0; i != DIM; ++i)
1683 result[i] = step( vector_base_ptr(a)[i], vector_base_ptr(v)[i]);
1684 return result;
1685}
1686
1688template <typename T, Size DIM>
1690{
1691 Vector<T,DIM> result;
1692 for( Size i = 0; i != DIM; ++i)
1693 result[i] = tan( vector_base_ptr(v)[i]);
1694 return result;
1695}
1696
1697
1698//------ Geometric Vector Algorithms ------------------------------------------
1699
1701template <typename T>
1702inline T cross(
1703 const Vector_struct<T,2>& lhs,
1704 const Vector_struct<T,2>& rhs)
1705{
1706 return lhs.x * rhs.y - lhs.y * rhs.x;
1707}
1708
1710template <typename T>
1712 const Vector_struct<T,3>& lhs,
1713 const Vector_struct<T,3>& rhs)
1714{
1715 return Vector<T,3>( lhs.y * rhs.z - lhs.z * rhs.y,
1716 lhs.z * rhs.x - lhs.x * rhs.z,
1717 lhs.x * rhs.y - lhs.y * rhs.x);
1718}
1719
1725template <typename T>
1726inline void make_basis(
1727 const Vector<T,3>& n,
1728 Vector<T,3>* u,
1729 Vector<T,3>* v)
1730{
1731#ifdef mi_base_assert_enabled
1732 const T eps = 1e-6f; // smallest resolvable factor
1733#endif
1734
1735 mi_math_assert_msg( u != 0, "precondition");
1736 mi_math_assert_msg( v != 0, "precondition");
1737 // Sanity check: the normal vector must be unit length.
1738 mi_math_assert_msg( abs( length(n) - 1.0f) < eps, "precondition");
1739
1740 // Compute u.
1741 if( abs(n.x) < abs(n.y)) {
1742 // u = cross(x, n), x = (1, 0, 0)
1743 u->x = T(0);
1744 u->y = -n.z;
1745 u->z = n.y;
1746 } else {
1747 // u = cross(y, n), y = (0, 1, 0)
1748 u->x = n.z;
1749 u->y = T(0);
1750 u->z = -n.x;
1751 }
1752 u->normalize();
1753
1754 // Compute v. Since *u and n are orthogonal and unit-length,
1755 // there is no need to normalize *v.
1756 *v = cross( *u, n);
1757
1758 // Sanity check: make sure (u, n, v) is an orthogonal basis.
1759 mi_math_assert_msg( abs( dot( *u, n)) < eps, "postcondition");
1760 mi_math_assert_msg( abs( dot( *u, *v)) < eps, "postcondition");
1761 mi_math_assert_msg( abs( dot( n, *v)) < eps, "postcondition");
1762 // Sanity check: make sure u and v are unit length.
1763 mi_math_assert_msg( abs( length( *u) - T(1)) < eps, "postcondition");
1764 mi_math_assert_msg( abs( length( *v) - T(1)) < eps, "postcondition");
1765}
1766
1773template <typename T>
1774inline void make_basis(
1775 const Vector<T,3>& n,
1776 const Vector<T,3>& u,
1777 const Vector<T,3>& v,
1778 Vector<T,3>* t,
1779 Vector<T,3>* b)
1780{
1781 const T eps = 1e-6f; // smallest resolvable factor
1782 (void)eps;
1783
1784 mi_math_assert_msg( t != 0, "precondition");
1785 mi_math_assert_msg( b != 0, "precondition");
1786 // Sanity check: the normal vector must be unit length.
1787 mi_math_assert_msg( abs( length( n) - 1.0f) < eps, "precondition");
1788 // Sanity check: the other vector lengths should be finite and non-zero
1789 mi_math_assert_msg( length( u) > 0., "precondition");
1790 mi_math_assert_msg( length( v) > 0., "precondition");
1791 mi_math_assert_msg( isfinite( length( u)), "precondition");
1792 mi_math_assert_msg( isfinite( length( v)), "precondition");
1793
1794 // Compute b
1795 *b = cross(u,n);
1796 b->normalize();
1797
1798 // Compute t. Since *b and n are orthogonal and unit-length,
1799 // there is no need to normalize *t.
1800 *t = cross(n,*b);
1801
1802 // Check that b has the same orientation of v
1803 if( dot( *b,v) < T(0))
1804 *b = -*b;
1805
1806 // Sanity check: make sure *u and t have the same orientation.
1807 mi_math_assert_msg( dot( u, *t) > T(0), "postcondition");
1808 // Sanity check: make sure (t, n, b) is an orthogonal basis.
1809 // We use a scaled epsilon in order to avoid false positives.
1810 mi_math_assert_msg( abs( dot( *t, n)) < 20*eps, "postcondition");
1811 mi_math_assert_msg( abs( dot( *t, *b)) < 20*eps, "postcondition");
1812 mi_math_assert_msg( abs( dot( n, *b)) < 20*eps, "postcondition");
1813 // Sanity check: make sure t and b are unit length.
1814 mi_math_assert_msg( abs( length( *t) - T(1)) < eps, "postcondition");
1815 mi_math_assert_msg( abs( length( *b) - T(1)) < eps, "postcondition");
1816}
1817
1823template <typename T2, Size DIM2, typename T1, Size DIM1>
1825 const Vector<T1, DIM1>& v,
1826 const T2& fill = T2(0))
1827{
1828 const Size dim_min = base::min MI_PREVENT_MACRO_EXPAND ( DIM1, DIM2 );
1829 Vector<T2, DIM2> result;
1830 for( Size i = 0; i < dim_min; ++i)
1831 result[i] = T2(v[i]);
1832 for( Size i = dim_min; i < DIM2; ++i)
1833 result[i] = fill;
1834 return result;
1835}
1836 // end group mi_math_vector
1838
1839} // namespace math
1840
1841} // namespace mi
1842
1843#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
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:1174
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:1190
Sint32 dot(Sint32 a, Sint32 b)
Returns the inner product (a.k.a. dot or scalar product) of two integers.
Definition: function.h:1081
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:1222
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:1161
Float32 length(Float32 a)
Returns the Euclidean norm of the scalar a (its absolute value).
Definition: function.h:1107
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:1206
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:1150
#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:625
Color sqrt(const Color &c)
Returns the square root of each element of c.
Definition: color.h:804
Color acos(const Color &c)
Returns a color with the elementwise arc cosine of the color c.
Definition: color.h:477
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:516
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:522
Color abs(const Color &c)
Returns a color with the elementwise absolute values of the color c.
Definition: color.h:471
Color round(const Color &c)
Returns a color with the elements of color c rounded to nearest integers.
Definition: color.h:737
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:713
Color atan(const Color &c)
Returns a color with the elementwise arc tangent of the color c.
Definition: color.h:501
bool isfinite(const Color &c)
Indicates whether all components of the color are finite.
Definition: color.h:822
Color step(const Color &a, const Color &c)
Returns elementwise 0 if c is less than a and 1 otherwise.
Definition: color.h:810
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:581
Color tan(const Color &c)
Returns a color with the elementwise tangent of the color c.
Definition: color.h:816
Color atan2(const Color &c, const Color &d)
Returns a color with the elementwise arc tangent of the color c / d.
Definition: color.h:509
Color sin(const Color &c)
Returns a color with the elementwise sine of the color c.
Definition: color.h:761
Color degrees(const Color &c)
Converts elementwise radians in c to degrees.
Definition: color.h:564
Color saturate(const Color &c)
Returns the color c clamped elementwise to the range [0,1].
Definition: color.h:749
Color rsqrt(const Color &c)
Returns the reciprocal of the square root of each element of c.
Definition: color.h:743
bool any(const Color &c)
Returns true if any element of c is not equal to zero.
Definition: color.h:489
Color radians(const Color &c)
Converts elementwise degrees in c to radians.
Definition: color.h:731
Color exp2(const Color &c)
Returns a color with elementwise 2 to the power of the element in the color c.
Definition: color.h:596
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:571
Color log(const Color &c)
Returns a color with elementwise natural logarithm of the color c.
Definition: color.h:689
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:603
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:782
Color sign(const Color &c)
Returns the elementwise sign of color c.
Definition: color.h:755
Color fmod(const Color &a, const Color &b)
Returns elementwise a modulo b, in other words, the remainder of a/b.
Definition: color.h:611
Color cos(const Color &c)
Returns a color with the elementwise cosine of the color c.
Definition: color.h:558
Color pow(const Color &a, const Color &b)
Returns the color a elementwise to the power of b.
Definition: color.h:719
bool all(const Color &c)
Returns true if all elements of c are not equal to zero.
Definition: color.h:483
Color log10(const Color &c)
Returns a color with elementwise base 10 logarithm of the color c.
Definition: color.h:704
Color log2(const Color &c)
Returns a color with elementwise base 2 logarithm of the color c.
Definition: color.h:695
Color exp(const Color &c)
Returns a color with elementwise e to the power of the element in the color c.
Definition: color.h:590
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:650
void sincos(const Color &a, Color &s, Color &c)
Computes elementwise the sine s and cosine c of angles a simultaneously.
Definition: color.h:769
Color asin(const Color &c)
Returns a color with the elementwise arc sine of the color c.
Definition: color.h:495
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:399
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:1095
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:629
Vector(T2 const (&array)[DIM])
Constructor initializes the vector elements from an array of dimension DIM.
Definition: vector.h:390
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:1702
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:531
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:1185
Vector(const Color_struct &color)
Dedicated constructor, for dimension 4 only, that initializes the vector elements from a color interp...
Definition: vector.h:557
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:1196
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:518
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:372
bool operator<=(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically less than or equal to rhs.
Definition: vector.h:678
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:544
bool operator>=(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically greater than or equal to rhs.
Definition: vector.h:694
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:686
Vector(const Vector_struct<T, DIM> &vec)
Constructor from underlying storage type.
Definition: vector.h:345
Vector<T, DIM> & operator++(Vector<T, DIM> &vec)
Pre-increments all elements of vec and returns the result. Modifies vec.
Definition: vector.h:1010
Vector<T, DIM> & operator--(Vector<T, DIM> &vec)
Pre-decrements all elements of vec and returns the result. Modifies vec.
Definition: vector.h:1018
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:453
Vector(T v)
Constructor initializes all vector elements to the value v.
Definition: vector.h:352
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:1152
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:465
T & operator[](Size i)
Accesses the i-th vector element.
Definition: vector.h:602
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:408
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:1824
const T & get(Size i) const
Returns the i-th vector element.
Definition: vector.h:620
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:739
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:478
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:656
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:505
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:1174
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:429
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:792
Float32 r
Red color component.
Definition: vector.h:69
bool normalize()
Normalizes this vector to unit length.
Definition: vector.h:643
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:1029
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:1128
bool operator<(Vector<T, DIM> rhs) const
Returns true if lhs is lexicographically less than rhs.
Definition: vector.h:670
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:1062
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:418
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:1141
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:441
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:1726
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:1163
bool operator!=(Vector<T, DIM> rhs) const
Returns true if lhs is elementwise not equal to rhs.
Definition: vector.h:662
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:492
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: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.