The interval is a standard 1D real-valued interval. The class implements a representation and operations on the interval type and guarantees interval validity on construction. Basic operations and accessors are implemented, as well as other common operations. See 'Example Usage' below.
{c++}
#include "geometry/interval.hpp"
#include <iostream>
// using-directive is just for illustration; don't do this in practice
using namespace autoware::common::geometry;
// bounds for example interval
constexpr auto MIN = 0.0;
constexpr auto MAX = 1.0;
//
// Try to construct an invalid interval. This will give the following error:
// 'Attempted to construct an invalid interval: {"min": 1.0, "max": 0.0}'
//
try {
const auto i = Interval_d(MAX, MIN);
} catch (const std::runtime_error& e) {
std::cerr << e.what();
}
//
// Construct a double precision interval from 0 to 1
//
const auto i = Interval_d(MIN, MAX);
//
// Test accessors and properties
//
std::cout << Interval_d::min(i) << " " << Interval_d::max(i) << "\n";
// Prints: 0.0 1.0
std::cout << Interval_d::empty(i) << " " << Interval_d::length(i) << "\n";
// Prints: false 1.0
std::cout << Interval_d::contains(i, 0.3) << "\n";
// Prints: true
std::cout << Interval_d::is_subset_eq(Interval_d(0.2, 0.4), i) << "\n";
// Prints: true
//
// Test operations.
//
std::cout << Interval_d::intersect(i, Interval(-1.0, 0.3)) << "\n";
// Prints: {"min": 0.0, "max": 0.3}
std::cout << Interval_d::project_to_interval(i, 0.5) << " "
<< Interval_d::project_to_interval(i, -1.3) << "\n";
// Prints: 0.5 0.0
//
// Distinguish empty/zero measure
//
const auto i_empty = Interval();
const auto i_zero_length = Interval(0.0, 0.0);
std::cout << Interval_d::empty(i_empty) << " "
<< Interval_d::empty(i_zero_length) << "\n";
// Prints: true false
std::cout << Interval_d::zero_measure(i_empty) << " "
<< Interval_d::zero_measure(i_zero_length) << "\n";
// Prints: false false