21 #ifndef SNARK_MATH_INTERVAL_H_
22 #define SNARK_MATH_INTERVAL_H_
24 #include <boost/optional.hpp>
25 #include <comma/base/exception.h>
26 #include <comma/math/compare.h>
29 namespace snark {
namespace math {
32 template <
typename T,
int N >
36 typedef Eigen::Matrix< T, N, 1 > vector_type;
39 closed_interval(
const vector_type&
min,
const vector_type&
max ) : m_interval( std::make_pair( get_min( min, max ), get_max( min, max ) ) ) {
if( !less_or_equal( min, max ) ) { COMMA_THROW( comma::exception,
"invalid interval" ); } }
42 closed_interval(
const vector_type& rhs ) : m_interval( std::make_pair( rhs, rhs ) ) {}
48 const std::pair< vector_type, vector_type >&
operator()()
const { assert( m_interval );
return m_interval; }
51 const vector_type&
min()
const { assert( m_interval );
return m_interval->first; }
54 const vector_type&
max()
const { assert( m_interval );
return m_interval->second; }
57 bool contains(
const vector_type& rhs )
const { assert( m_interval );
return less_or_equal( m_interval->first, rhs ) && less_or_equal( rhs, m_interval->second ); }
75 bool operator==(
const closed_interval& rhs )
const { assert( m_interval && rhs );
return m_interval->first.isApprox( rhs().first ) && m_interval->second.isApprox( rhs().second ); }
82 static bool less_or_equal(
const vector_type& lhs,
const vector_type& rhs ) {
return ( ( lhs.array() <= rhs.array() ).all() ); }
83 static vector_type get_min(
const vector_type& lhs,
const vector_type& rhs ) {
return rhs.array().min( lhs.array() ); }
84 static vector_type get_max(
const vector_type& lhs,
const vector_type& rhs ) {
return rhs.array().max( lhs.array() ); }
86 boost::optional< std::pair< vector_type, vector_type > > m_interval;
91 #endif // SNARK_MATH_INTERVAL_H_