Skip to content

Commit

Permalink
Fix problems with ImathInterval, and add test (#52)
Browse files Browse the repository at this point in the history
The test for Imath::Interval was missing and it's not included
anywhere else in the library, so builds were not compiling it and thus
not catching syntax errors. These changes also bring it more in line
with the Box<T> class.

* misspelling of "constexpr"
* replace constexpr with IMATH_CONSTEXPR14
* add makeInfinite()/isInfinite() for similarity with Box<T>
* add operator<<
* add ImathTest/testInterval.cpp, based on testBox.cpp

Signed-off-by: Cary Phillips <[email protected]>
  • Loading branch information
cary-ilm authored Sep 5, 2020
1 parent 3b7953e commit 9c864c5
Show file tree
Hide file tree
Showing 5 changed files with 847 additions and 19 deletions.
77 changes: 59 additions & 18 deletions src/Imath/ImathInterval.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ template <class T> class Interval
// Constructors - an "empty" Interval is created by default
//-----------------------------------------------------

IMATH_HOSTDEVICE Interval();
IMATH_HOSTDEVICE constexpr Interval (const T& point);
IMATH_HOSTDEVICE constexpr Interval (const T& minT, const T& maxT);
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval();
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval (const T& point);
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval (const T& minT, const T& maxT);

//--------------------------------
// Operators: we get != from STL
//--------------------------------

IMATH_HOSTDEVICE constexpr bool operator== (const Interval<T>& src) const;
IMATH_HOSTDEVICE constexpr bool operator!= (const Interval<T>& src) const;

//------------------
// Interval manipulation
Expand All @@ -82,24 +83,28 @@ template <class T> class Interval
IMATH_HOSTDEVICE void makeEmpty();
IMATH_HOSTDEVICE void extendBy (const T& point);
IMATH_HOSTDEVICE void extendBy (const Interval<T>& interval);
IMATH_HOSTDEVICE void makeInfinite();

//---------------------------------------------------
// Query functions - these compute results each time
//---------------------------------------------------

IMATH_HOSTDEVICE constexpr T size() const;
IMATH_HOSTDEVICE constexpr T center() const;
IMATH_HOSTDEVICE constexpr bool intersects (const T& point) const;
IMATH_HOSTDEVICE constexpr bool intersects (const Interval<T>& interval) const;
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T size() const;
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T center() const;
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const T& point) const;
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Interval<T>& interval) const;

//----------------
// Classification
//----------------

IMATH_HOSTDEVICE constexpr bool hasVolume() const;
IMATH_HOSTDEVICE constexpr bool isEmpty() const;
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const;
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const;
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const;
};

template <class T> std::ostream& operator<< (std::ostream& s, const Interval<T>& v);

//--------------------
// Convenient typedefs
//--------------------
Expand All @@ -113,18 +118,18 @@ typedef Interval<int> Intervali;
// Implementation
//----------------

template <class T> inline Interval<T>::Interval()
template <class T> inline IMATH_CONSTEXPR14 Interval<T>::Interval()
{
makeEmpty();
}

template <class T> constexpr inline Interval<T>::Interval (const T& point)
template <class T> IMATH_CONSTEXPR14 inline Interval<T>::Interval (const T& point)
{
min = point;
max = point;
}

template <class T> constexpr inline Interval<T>::Interval (const T& minV, const T& maxV)
template <class T> IMATH_CONSTEXPR14 inline Interval<T>::Interval (const T& minV, const T& maxV)
{
min = minV;
max = maxV;
Expand All @@ -137,6 +142,13 @@ Interval<T>::operator== (const Interval<T>& src) const
return (min == src.min && max == src.max);
}

template <class T>
constexpr inline bool
Interval<T>::operator!= (const Interval<T>& src) const
{
return (min != src.min || max != src.max);
}

template <class T>
inline void
Interval<T>::makeEmpty()
Expand All @@ -145,6 +157,15 @@ Interval<T>::makeEmpty()
max = limits<T>::min();
}

template <class T>
inline void
Interval<T>::makeInfinite()
{
min = limits<T>::min();
max = limits<T>::max();
}


template <class T>
inline void
Interval<T>::extendBy (const T& point)
Expand All @@ -168,47 +189,67 @@ Interval<T>::extendBy (const Interval<T>& interval)
}

template <class T>
constexpr inline bool
IMATH_CONSTEXPR14 inline bool
Interval<T>::intersects (const T& point) const
{
return point >= min && point <= max;
}

template <class T>
constexpr inline bool
IMATH_CONSTEXPR14 inline bool
Interval<T>::intersects (const Interval<T>& interval) const
{
return interval.max >= min && interval.min <= max;
}

template <class T>
constexpr inline T
IMATH_CONSTEXPR14 inline T
Interval<T>::size() const
{
if (isEmpty())
return T(0);

return max - min;
}

template <class T>
cosntexpr inline T
IMATH_CONSTEXPR14 inline T
Interval<T>::center() const
{
return (max + min) / 2;
}

template <class T>
constexpr inline bool
IMATH_CONSTEXPR14 inline bool
Interval<T>::isEmpty() const
{
return max < min;
}

template <class T>
constexpr inline bool
IMATH_CONSTEXPR14 inline bool
Interval<T>::hasVolume() const
{
return max > min;
}

template <class T>
IMATH_CONSTEXPR14 inline bool
Interval<T>::isInfinite() const
{
if (min != limits<T>::min() || max != limits<T>::max())
return false;

return true;
}

template <class T>
std::ostream&
operator<< (std::ostream& s, const Interval<T>& v)
{
return s << '(' << v.min << ' ' << v.max << ')';
}

IMATH_INTERNAL_NAMESPACE_HEADER_EXIT

#endif // INCLUDED_IMATHINTERVAL_H
1 change: 1 addition & 0 deletions src/ImathTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_executable(ImathTest
testFrustum.cpp
testFrustumTest.cpp
testFun.cpp
testInterval.cpp
testInvert.cpp
testJacobiEigenSolver.cpp
testLineAlgo.cpp
Expand Down
4 changes: 3 additions & 1 deletion src/ImathTest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <testFrustum.h>
#include <testFrustumTest.h>
#include <testFun.h>
#include <testInterval.h>
#include <testInvert.h>
#include <testJacobiEigenSolver.h>
#include <testLineAlgo.h>
Expand Down Expand Up @@ -76,6 +77,7 @@ main (int argc, char* argv[])
TEST (testRoots);
TEST (testFun);
TEST (testInvert);
TEST (testInterval);
TEST (testFrustum);
TEST (testRandom);
TEST (testExtractEuler);
Expand All @@ -90,6 +92,6 @@ main (int argc, char* argv[])
TEST (testTinySVD);
TEST (testJacobiEigenSolver);
TEST (testFrustumTest);

return 0;
}
Loading

0 comments on commit 9c864c5

Please sign in to comment.