CMS 3D CMS Logo

Public Member Functions | Private Attributes

npstat::Interval< Numeric > Class Template Reference

#include <Interval.h>

List of all members.

Public Member Functions

void getBounds (Numeric *pmin, Numeric *pmax) const
 Interval ()
 Interval (const Numeric max)
 Interval (const Numeric min, const Numeric max, const bool swapIfOutOfOrder=false)
bool isInside (const Numeric value) const
bool isInsideLower (const Numeric value) const
bool isInsideUpper (const Numeric value) const
bool isInsideWithBounds (const Numeric value) const
Numeric length () const
template<typename Num2 >
void linearMap (const Interval< Num2 > &r, double *a, double *b) const
const Numeric max () const
Numeric midpoint () const
const Numeric min () const
IntervalmoveMidpointTo0 ()
Interval overlap (const Interval &r) const
double overlapFraction (const Interval &r) const
Numeric overlapLength (const Interval &r) const
void setBounds (const Numeric minval, const Numeric maxval, const bool swapIfOutOfOrder=false)
void setMax (const Numeric value)
void setMin (const Numeric value)
Intervaloperator*= (double r)
Intervaloperator/= (double r)
Intervaloperator+= (const Numeric value)
Intervaloperator-= (const Numeric value)
Intervalexpand (double r)

Private Attributes

Numeric max_
Numeric min_

Detailed Description

template<typename Numeric>
class npstat::Interval< Numeric >

Representation of 1-d intervals. The following invariant is maintained: min() will not exceed max().

See BoxND class for rectangles, boxes, and hyperboxes.

Definition at line 25 of file Interval.h.


Constructor & Destructor Documentation

template<typename Numeric>
npstat::Interval< Numeric >::Interval ( ) [inline]

Both lower and upper interval bounds are set to Numeric()

Definition at line 29 of file Interval.h.

: min_(Numeric()), max_(Numeric()) {}
template<typename Numeric>
npstat::Interval< Numeric >::Interval ( const Numeric  max) [inline]

Minimum is set to Numeric(), maximum to the given argument. An exception is thrown if the argument is smaller than Numeric().

Definition at line 35 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

            : min_(Numeric()), max_(max)
        {
            if (min_ > max_) throw npstat::NpstatInvalidArgument(
                "In npstat::Interval constructor: invalid limits");
        }
template<typename Numeric>
npstat::Interval< Numeric >::Interval ( const Numeric  min,
const Numeric  max,
const bool  swapIfOutOfOrder = false 
) [inline]

Constructor from both bounds. Set "swapIfOutOfOrder" argument to "true" if the minimum can be larger than the maximum (in this case the bounds will be swapped internally).

Definition at line 47 of file Interval.h.

References npstat::Interval< Numeric >::max_, npstat::Interval< Numeric >::min_, and swap().

            : min_(min), max_(max)
        {
            if (min_ > max_)
            {
                if (swapIfOutOfOrder)
                    std::swap(min_, max_);
                else
                    throw npstat::NpstatInvalidArgument(
                        "In npstat::Interval constructor: invalid limits");
            }
        }

Member Function Documentation

template<typename Numeric >
Interval< Numeric > & npstat::Interval< Numeric >::expand ( double  r) [inline]

Scaling the bounds by a constant in such a way that the midpoint remains unchanged

Definition at line 257 of file Interval.h.

References alignCSCRings::r.

    {
        const double r = fabs(ir);
        if (r != 1.0)
        {
            const Numeric center(static_cast<Numeric>((max_ + min_)*0.5));
            min_ = center + (min_ - center)*r;
            max_ = center + (max_ - center)*r;
        }
        return *this;
    }
template<typename Numeric>
void npstat::Interval< Numeric >::getBounds ( Numeric *  pmin,
Numeric *  pmax 
) const [inline]

Return both bounds

Definition at line 97 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

            {*pmin = min_; *pmax = max_;}
template<typename Numeric>
bool npstat::Interval< Numeric >::isInside ( const Numeric  value) const [inline]

Is the point completely inside the interval (and does not coincide with any bound)?

Definition at line 123 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

            {return value > min_ && value < max_;}
template<typename Numeric>
bool npstat::Interval< Numeric >::isInsideLower ( const Numeric  value) const [inline]

Is the point inside the interval or on the lower boundary?

Definition at line 108 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

            {return value >= min_ && value < max_;}
template<typename Numeric>
bool npstat::Interval< Numeric >::isInsideUpper ( const Numeric  value) const [inline]

Is the point inside the interval or on the upper boundary?

Definition at line 112 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

            {return value > min_ && value <= max_;}
template<typename Numeric>
bool npstat::Interval< Numeric >::isInsideWithBounds ( const Numeric  value) const [inline]

Is the point inside the interval or on one of the boundaries?

Definition at line 116 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

            {return value >= min_ && value <= max_;}
template<typename Numeric>
Numeric npstat::Interval< Numeric >::length ( ) const [inline]

Interval length

Definition at line 101 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

{return max_ - min_;}
template<typename Numeric >
template<typename Num2 >
void npstat::Interval< Numeric >::linearMap ( const Interval< Num2 > &  r,
double *  a,
double *  b 
) const

Derive the coefficients a and b such that the linear mapping y = a*x + b maps the lower limit of this interval into the lower limit of the argument interval and the upper limit of this interval into the upper limit of the argument interval

Definition at line 299 of file Interval.h.

References npstat::Interval< Numeric >::max(), and npstat::Interval< Numeric >::min().

    {
        if (max_ == min_) throw npstat::NpstatDomainError(
            "In npstat::Interval::linearMap: zero length interval");
        assert(a);
        assert(b);
        const Num2 rmax(r.max());
        const Num2 rmin(r.min());
        *a = static_cast<double>((rmax - rmin)*1.0/(max_ - min_));
        *b = static_cast<double>((rmax + rmin) - *a*(max_ + min_))/2.0;
    }
template<typename Numeric>
const Numeric npstat::Interval< Numeric >::max ( ) const [inline]
template<typename Numeric>
Numeric npstat::Interval< Numeric >::midpoint ( ) const [inline]

The middle point of the interval

Definition at line 104 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

            {return static_cast<Numeric>((max_ + min_)*0.5);}
template<typename Numeric>
const Numeric npstat::Interval< Numeric >::min ( ) const [inline]
template<typename Numeric >
Interval< Numeric > & npstat::Interval< Numeric >::moveMidpointTo0 ( ) [inline]

Move the interval so that the midpoint ends up at 0

Definition at line 248 of file Interval.h.

    {
        const Numeric len = max_ - min_;
        max_ = len/static_cast<Numeric>(2);
        min_ = -max_;
        return *this;
    }
template<typename Numeric >
Interval< Numeric > & npstat::Interval< Numeric >::operator*= ( double  r) [inline]

Scaling of both the minimum and the maximum by a constant. Minimum and maximum will be swapped internally in case the constant is negative.

Definition at line 238 of file Interval.h.

References alignCSCRings::r, and swap().

    {
        min_ *= r;
        max_ *= r;
        if (max_ < min_)
            std::swap(min_, max_);
        return *this;
    }
template<typename Numeric >
Interval< Numeric > & npstat::Interval< Numeric >::operator+= ( const Numeric  value) [inline]

Shift both interval bounds by a constant

Definition at line 282 of file Interval.h.

References alignCSCRings::r.

    {
        min_ += r;
        max_ += r;
        return *this;
    }
template<typename Numeric >
Interval< Numeric > & npstat::Interval< Numeric >::operator-= ( const Numeric  value) [inline]

Definition at line 290 of file Interval.h.

References alignCSCRings::r.

    {
        min_ -= r;
        max_ -= r;
        return *this;
    }
template<typename Numeric >
Interval< Numeric > & npstat::Interval< Numeric >::operator/= ( double  r) [inline]

Definition at line 270 of file Interval.h.

References alignCSCRings::r, and swap().

    {
        if (!r) throw npstat::NpstatDomainError(
            "In npstat::Interval::operator/=: division by zero");
        min_ /= r;
        max_ /= r;
        if (max_ < min_)
            std::swap(min_, max_);
        return *this;
    }
template<typename Numeric >
Interval< Numeric > npstat::Interval< Numeric >::overlap ( const Interval< Numeric > &  r) const [inline]

The following function returns default-constructed empty interval in case this interval and the argument interval do not overlap

Definition at line 195 of file Interval.h.

References npstat::Interval< Numeric >::max_, npstat::Interval< Numeric >::min_, and npstat::Interval< Numeric >::setBounds().

    {
        Interval<Numeric> over;
        if (max_ == r.min_)
            over.setBounds(max_, max_);
        else if (r.max_ == min_)
            over.setBounds(min_, min_);
        else if (max_ > r.min_ && r.max_ > min_)
        {
            over.min_ = min_ < r.min_ ? r.min_ : min_;
            over.max_ = max_ < r.max_ ? max_ : r.max_;
        }
        return over;
    }
template<typename Numeric >
double npstat::Interval< Numeric >::overlapFraction ( const Interval< Numeric > &  r) const [inline]

Same as overlapLength(r)/length() but a tad faster

Definition at line 225 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

    {
        if (max_ > r.min_ && r.max_ > min_)
        {
            const Numeric mn = min_ < r.min_ ? r.min_ : min_;
            const Numeric mx = max_ < r.max_ ? max_ : r.max_;
            return (mx - mn)*1.0/(max_ - min_);
        }
        else
            return 0.0;
    }
template<typename Numeric >
Numeric npstat::Interval< Numeric >::overlapLength ( const Interval< Numeric > &  r) const [inline]

Same as overlap.length() but a tad faster

Definition at line 212 of file Interval.h.

References npstat::Interval< Numeric >::max_, and npstat::Interval< Numeric >::min_.

    {
        if (max_ > r.min_ && r.max_ > min_)
        {
            const Numeric mn = min_ < r.min_ ? r.min_ : min_;
            const Numeric mx = max_ < r.max_ ? max_ : r.max_;
            return mx - mn;
        }
        else
            return Numeric();
    }
template<typename Numeric>
void npstat::Interval< Numeric >::setBounds ( const Numeric  minval,
const Numeric  maxval,
const bool  swapIfOutOfOrder = false 
) [inline]

Set both interval bounds

Definition at line 78 of file Interval.h.

References npstat::Interval< Numeric >::max_, npstat::Interval< Numeric >::min_, and swap().

Referenced by npstat::Interval< Numeric >::overlap().

        {
            if (maxval < minval && !swapIfOutOfOrder)
                throw npstat::NpstatInvalidArgument(
                    "In npstat::Interval::setBounds: invalid limits");
            min_ = minval;
            max_ = maxval;
            if (swapIfOutOfOrder && min_ > max_)
                std::swap(min_, max_);
        }
template<typename Numeric>
void npstat::Interval< Numeric >::setMax ( const Numeric  value) [inline]

Set the upper interval bound

Definition at line 70 of file Interval.h.

References npstat::Interval< Numeric >::max_, npstat::Interval< Numeric >::min_, and relativeConstraints::value.

        {
            if (value < min_) throw npstat::NpstatInvalidArgument(
                "In npstat::Interval::setMax: argument below min");
            max_ = value;
        }
template<typename Numeric>
void npstat::Interval< Numeric >::setMin ( const Numeric  value) [inline]

Set the lower interval bound

Definition at line 62 of file Interval.h.

References npstat::Interval< Numeric >::max_, npstat::Interval< Numeric >::min_, and relativeConstraints::value.

        {
            if (value > max_) throw npstat::NpstatInvalidArgument(
                "In npstat::Interval::setMin: argument above max");
            min_ = value; 
        }

Member Data Documentation

template<typename Numeric>
Numeric npstat::Interval< Numeric >::max_ [private]
template<typename Numeric>
Numeric npstat::Interval< Numeric >::min_ [private]