Skip to content

Commit ab04691

Browse files
authored
Add subscript operator optimizations, remove usage (#451)
* Normalize Vector Implementations Adds routines to vec4 to match others Adds a pre-defined half type for future specialization Adds C++23 logic for known subscript values Signed-off-by: Kimball Thurston <[email protected]> * Favor using direct member access avoid dynamic subscript usage by using x, y, z, w access directly Signed-off-by: Kimball Thurston <[email protected]> * Prefer using getValue In preparation for changing subscript operators, change python bindings where appropriate which get the pointers. Signed-off-by: Kimball Thurston <[email protected]> --------- Signed-off-by: Kimball Thurston <[email protected]>
1 parent bb6348b commit ab04691

File tree

10 files changed

+271
-140
lines changed

10 files changed

+271
-140
lines changed

src/Imath/ImathBox.h

Lines changed: 56 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ typedef Box<V2i> Box2i;
150150
/// 2D box of base type `int64_t`.
151151
typedef Box<V2i64> Box2i64;
152152

153+
/// 2D box of base type `half`.
154+
typedef Box<V2h> Box2h;
155+
153156
/// 2D box of base type `float`.
154157
typedef Box<V2f> Box2f;
155158

@@ -165,6 +168,9 @@ typedef Box<V3i> Box3i;
165168
/// 3D box of base type `int64_t`.
166169
typedef Box<V3i64> Box3i64;
167170

171+
/// 3D box of base type `half`.
172+
typedef Box<V3h> Box3h;
173+
168174
/// 3D box of base type `float`.
169175
typedef Box<V3f> Box3f;
170176

@@ -515,48 +521,36 @@ template <class T>
515521
IMATH_HOSTDEVICE inline void
516522
Box<Vec2<T>>::extendBy (const Vec2<T>& point) IMATH_NOEXCEPT
517523
{
518-
if (point[0] < min[0]) min[0] = point[0];
519-
520-
if (point[0] > max[0]) max[0] = point[0];
521-
522-
if (point[1] < min[1]) min[1] = point[1];
523-
524-
if (point[1] > max[1]) max[1] = point[1];
524+
min.x = std::min (min.x, point.x);
525+
max.x = std::max (max.x, point.x);
526+
min.y = std::min (min.y, point.y);
527+
max.y = std::max (max.y, point.y);
525528
}
526529

527530
template <class T>
528531
IMATH_HOSTDEVICE inline void
529532
Box<Vec2<T>>::extendBy (const Box<Vec2<T>>& box) IMATH_NOEXCEPT
530533
{
531-
if (box.min[0] < min[0]) min[0] = box.min[0];
532-
533-
if (box.max[0] > max[0]) max[0] = box.max[0];
534-
535-
if (box.min[1] < min[1]) min[1] = box.min[1];
536-
537-
if (box.max[1] > max[1]) max[1] = box.max[1];
534+
min.x = std::min (min.x, box.min.x);
535+
max.x = std::max (max.x, box.max.x);
536+
min.y = std::min (min.y, box.min.y);
537+
max.y = std::max (max.y, box.max.y);
538538
}
539539

540540
template <class T>
541541
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
542542
Box<Vec2<T>>::intersects (const Vec2<T>& point) const IMATH_NOEXCEPT
543543
{
544-
if (point[0] < min[0] || point[0] > max[0] || point[1] < min[1] ||
545-
point[1] > max[1])
546-
return false;
547-
548-
return true;
544+
return (point.x >= min.x) && (point.x <= max.x) &&
545+
(point.y >= min.y) && (point.y <= max.y);
549546
}
550547

551548
template <class T>
552549
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
553550
Box<Vec2<T>>::intersects (const Box<Vec2<T>>& box) const IMATH_NOEXCEPT
554551
{
555-
if (box.max[0] < min[0] || box.min[0] > max[0] || box.max[1] < min[1] ||
556-
box.min[1] > max[1])
557-
return false;
558-
559-
return true;
552+
return (box.min.x <= max.x) && (box.max.x >= min.x) &&
553+
(box.min.y <= max.y) && (box.max.y >= min.y);
560554
}
561555

562556
template <class T>
@@ -579,19 +573,17 @@ template <class T>
579573
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
580574
Box<Vec2<T>>::isEmpty () const IMATH_NOEXCEPT
581575
{
582-
if (max[0] < min[0] || max[1] < min[1]) return true;
583-
584-
return false;
576+
return (max.x < min.x || max.y < min.y);
585577
}
586578

587579
template <class T>
588580
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
589581
Box<Vec2<T>>::isInfinite () const IMATH_NOEXCEPT
590582
{
591-
if (min[0] != std::numeric_limits<T>::lowest () ||
592-
max[0] != std::numeric_limits<T>::max () ||
593-
min[1] != std::numeric_limits<T>::lowest () ||
594-
max[1] != std::numeric_limits<T>::max ())
583+
if (min.x != std::numeric_limits<T>::lowest () ||
584+
max.x != std::numeric_limits<T>::max () ||
585+
min.y != std::numeric_limits<T>::lowest () ||
586+
max.y != std::numeric_limits<T>::max ())
595587
return false;
596588

597589
return true;
@@ -601,7 +593,7 @@ template <class T>
601593
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
602594
Box<Vec2<T>>::hasVolume () const IMATH_NOEXCEPT
603595
{
604-
if (max[0] <= min[0] || max[1] <= min[1]) return false;
596+
if (max.x <= min.x || max.y <= min.y) return false;
605597

606598
return true;
607599
}
@@ -610,12 +602,9 @@ template <class T>
610602
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int
611603
Box<Vec2<T>>::majorAxis () const IMATH_NOEXCEPT
612604
{
613-
unsigned int major = 0;
614-
Vec2<T> s = size ();
615-
616-
if (s[1] > s[major]) major = 1;
605+
Vec2<T> s = size ();
617606

618-
return major;
607+
return (s.x >= s.y) ? 0 : 1;
619608
}
620609

621610
///
@@ -769,56 +758,42 @@ template <class T>
769758
IMATH_HOSTDEVICE inline void
770759
Box<Vec3<T>>::extendBy (const Vec3<T>& point) IMATH_NOEXCEPT
771760
{
772-
if (point[0] < min[0]) min[0] = point[0];
773-
774-
if (point[0] > max[0]) max[0] = point[0];
775-
776-
if (point[1] < min[1]) min[1] = point[1];
777-
778-
if (point[1] > max[1]) max[1] = point[1];
779-
780-
if (point[2] < min[2]) min[2] = point[2];
781-
782-
if (point[2] > max[2]) max[2] = point[2];
761+
min.x = std::min (min.x, point.x);
762+
min.y = std::min (min.y, point.y);
763+
min.z = std::min (min.z, point.z);
764+
max.x = std::max (max.x, point.x);
765+
max.y = std::max (max.y, point.y);
766+
max.z = std::max (max.z, point.z);
783767
}
784768

785769
template <class T>
786770
IMATH_HOSTDEVICE inline void
787771
Box<Vec3<T>>::extendBy (const Box<Vec3<T>>& box) IMATH_NOEXCEPT
788772
{
789-
if (box.min[0] < min[0]) min[0] = box.min[0];
790-
791-
if (box.max[0] > max[0]) max[0] = box.max[0];
792-
793-
if (box.min[1] < min[1]) min[1] = box.min[1];
794-
795-
if (box.max[1] > max[1]) max[1] = box.max[1];
796-
797-
if (box.min[2] < min[2]) min[2] = box.min[2];
798-
799-
if (box.max[2] > max[2]) max[2] = box.max[2];
773+
min.x = std::min (min.x, box.min.x);
774+
min.y = std::min (min.y, box.min.y);
775+
min.z = std::min (min.z, box.min.z);
776+
max.x = std::max (max.x, box.max.x);
777+
max.y = std::max (max.y, box.max.y);
778+
max.z = std::max (max.z, box.max.z);
800779
}
801780

802781
template <class T>
803782
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
804783
Box<Vec3<T>>::intersects (const Vec3<T>& point) const IMATH_NOEXCEPT
805784
{
806-
if (point[0] < min[0] || point[0] > max[0] || point[1] < min[1] ||
807-
point[1] > max[1] || point[2] < min[2] || point[2] > max[2])
808-
return false;
809-
810-
return true;
785+
return (point.x >= min.x) && (point.x <= max.x) &&
786+
(point.y >= min.y) && (point.y <= max.y) &&
787+
(point.z >= min.z) && (point.z <= max.z);
811788
}
812789

813790
template <class T>
814791
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
815792
Box<Vec3<T>>::intersects (const Box<Vec3<T>>& box) const IMATH_NOEXCEPT
816793
{
817-
if (box.max[0] < min[0] || box.min[0] > max[0] || box.max[1] < min[1] ||
818-
box.min[1] > max[1] || box.max[2] < min[2] || box.min[2] > max[2])
819-
return false;
820-
821-
return true;
794+
return (box.min.x <= max.x) && (box.max.x >= min.x) &&
795+
(box.min.y <= max.y) && (box.max.y >= min.y) &&
796+
(box.min.z <= max.z) && (box.max.z >= min.z);
822797
}
823798

824799
template <class T>
@@ -841,21 +816,19 @@ template <class T>
841816
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
842817
Box<Vec3<T>>::isEmpty () const IMATH_NOEXCEPT
843818
{
844-
if (max[0] < min[0] || max[1] < min[1] || max[2] < min[2]) return true;
845-
846-
return false;
819+
return (max.x < min.x || max.y < min.y || max.z < min.z);
847820
}
848821

849822
template <class T>
850823
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
851824
Box<Vec3<T>>::isInfinite () const IMATH_NOEXCEPT
852825
{
853-
if (min[0] != std::numeric_limits<T>::lowest () ||
854-
max[0] != std::numeric_limits<T>::max () ||
855-
min[1] != std::numeric_limits<T>::lowest () ||
856-
max[1] != std::numeric_limits<T>::max () ||
857-
min[2] != std::numeric_limits<T>::lowest () ||
858-
max[2] != std::numeric_limits<T>::max ())
826+
if (min.x != std::numeric_limits<T>::lowest () ||
827+
max.x != std::numeric_limits<T>::max () ||
828+
min.y != std::numeric_limits<T>::lowest () ||
829+
max.y != std::numeric_limits<T>::max () ||
830+
min.z != std::numeric_limits<T>::lowest () ||
831+
max.z != std::numeric_limits<T>::max ())
859832
return false;
860833

861834
return true;
@@ -865,7 +838,7 @@ template <class T>
865838
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
866839
Box<Vec3<T>>::hasVolume () const IMATH_NOEXCEPT
867840
{
868-
if (max[0] <= min[0] || max[1] <= min[1] || max[2] <= min[2]) return false;
841+
if (max.x <= min.x || max.y <= min.y || max.z <= min.z) return false;
869842

870843
return true;
871844
}
@@ -874,14 +847,11 @@ template <class T>
874847
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int
875848
Box<Vec3<T>>::majorAxis () const IMATH_NOEXCEPT
876849
{
877-
unsigned int major = 0;
878-
Vec3<T> s = size ();
879-
880-
if (s[1] > s[major]) major = 1;
850+
Vec3<T> s = size ();
881851

882-
if (s[2] > s[major]) major = 2;
883-
884-
return major;
852+
if (s.x >= s.y)
853+
return (s.x >= s.z) ? 0 : 2;
854+
return (s.y >= s.z) ? 1 : 2;
885855
}
886856

887857
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT

src/Imath/ImathEuler.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -877,18 +877,16 @@ IMATH_HOSTDEVICE Quat<T>
877877
if (_initialRepeated)
878878
{
879879
a[i] = cj * (cs + sc);
880-
a[j] = sj * (cc + ss) *
881-
parity, // NOSONAR - suppress SonarCloud bug report.
882-
a[k] = sj * (cs - sc);
883-
q.r = cj * (cc - ss);
880+
a[j] = sj * (cc + ss) * parity;
881+
a[k] = sj * (cs - sc);
882+
q.r = cj * (cc - ss);
884883
}
885884
else
886885
{
887-
a[i] = cj * sc - sj * cs,
888-
a[j] = (cj * ss + sj * cc) *
889-
parity, // NOSONAR - suppress SonarCloud bug report.
890-
a[k] = cj * cs - sj * sc;
891-
q.r = cj * cc + sj * ss;
886+
a[i] = cj * sc - sj * cs;
887+
a[j] = (cj * ss + sj * cc) * parity;
888+
a[k] = cj * cs - sj * sc;
889+
q.r = cj * cc + sj * ss;
892890
}
893891

894892
q.v = a;
@@ -984,9 +982,9 @@ Euler<T>::simpleXYZRotation (Vec3<T>& xyzRot, const Vec3<T>& targetXyzRot)
984982
IMATH_NOEXCEPT
985983
{
986984
Vec3<T> d = xyzRot - targetXyzRot;
987-
xyzRot[0] = targetXyzRot[0] + angleMod (d[0]);
988-
xyzRot[1] = targetXyzRot[1] + angleMod (d[1]);
989-
xyzRot[2] = targetXyzRot[2] + angleMod (d[2]);
985+
xyzRot.x = targetXyzRot.x + angleMod (d.x);
986+
xyzRot.y = targetXyzRot.y + angleMod (d.y);
987+
xyzRot.z = targetXyzRot.z + angleMod (d.z);
990988
}
991989

992990
template <class T>

src/Imath/ImathFrame.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ Matrix44<T> constexpr firstFrame (
8989
n.normalize ();
9090
if (n.length () == 0.0f)
9191
{
92-
int i = fabs (t[0]) < fabs (t[1]) ? 0 : 1;
93-
if (fabs (t[2]) < fabs (t[i])) i = 2;
94-
9592
Vec3<T> v (0.0, 0.0, 0.0);
93+
94+
int i = fabs (t.x) < fabs (t.y) ? 0 : 1;
95+
if (fabs (t.z) < fabs (t[i])) i = 2;
96+
9697
v[i] = 1.0;
9798
n = t.cross (v);
9899
n.normalize ();
@@ -102,18 +103,21 @@ Matrix44<T> constexpr firstFrame (
102103

103104
Matrix44<T> M;
104105

105-
M[0][0] = t[0];
106-
M[0][1] = t[1];
107-
M[0][2] = t[2];
108-
M[0][3] = 0.0, M[1][0] = n[0];
109-
M[1][1] = n[1];
110-
M[1][2] = n[2];
111-
M[1][3] = 0.0, M[2][0] = b[0];
112-
M[2][1] = b[1];
113-
M[2][2] = b[2];
114-
M[2][3] = 0.0, M[3][0] = pi[0];
115-
M[3][1] = pi[1];
116-
M[3][2] = pi[2];
106+
M[0][0] = t.x;
107+
M[0][1] = t.y;
108+
M[0][2] = t.z;
109+
M[0][3] = 0.0;
110+
M[1][0] = n.x;
111+
M[1][1] = n.y;
112+
M[1][2] = n.z;
113+
M[1][3] = 0.0;
114+
M[2][0] = b.x;
115+
M[2][1] = b.y;
116+
M[2][2] = b.z;
117+
M[2][3] = 0.0;
118+
M[3][0] = pi.x;
119+
M[3][1] = pi.y;
120+
M[3][2] = pi.z;
117121
M[3][3] = 1.0;
118122

119123
return M;

src/Imath/ImathMatrixAlgo.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,10 @@ twoSidedJacobiSVD (
834834

835835
// The off-diagonal entries are (effectively) 0, so whatever's left on the
836836
// diagonal are the singular values:
837-
S[0] = A[0][0];
838-
S[1] = A[1][1];
839-
S[2] = A[2][2];
840-
S[3] = A[3][3];
837+
S.x = A[0][0];
838+
S.y = A[1][1];
839+
S.z = A[2][2];
840+
S.w = A[3][3];
841841

842842
// Nothing thus far has guaranteed that the singular values are positive,
843843
// so let's go back through and flip them if not (since by contract we are
@@ -901,14 +901,14 @@ twoSidedJacobiSVD (
901901
{
902902
for (int i = 0; i < 4; ++i)
903903
U[i][3] = -U[i][3];
904-
S[3] = -S[3];
904+
S.w = -S.w;
905905
}
906906

907907
if (V.determinant () < 0)
908908
{
909909
for (int i = 0; i < 4; ++i)
910910
V[i][3] = -V[i][3];
911-
S[3] = -S[3];
911+
S.w = -S.w;
912912
}
913913
}
914914
}

0 commit comments

Comments
 (0)