qwt_double_interval.h 4.84 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_DOUBLE_INTERVAL_H
#define QWT_DOUBLE_INTERVAL_H

#include "qwt_global.h"

/*!
  \brief A class representing an interval

18
  The interval is represented by 2 doubles, the lower and the upper limit.
pixhawk's avatar
pixhawk committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
*/

class QWT_EXPORT QwtDoubleInterval
{
public:
    inline QwtDoubleInterval();
    inline QwtDoubleInterval(double minValue, double maxValue);

    inline void setInterval(double minValue, double maxValue);

    QwtDoubleInterval normalized() const;
    QwtDoubleInterval inverted() const;
    QwtDoubleInterval limited(double minValue, double maxValue) const;

    inline int operator==(const QwtDoubleInterval &) const;
    inline int operator!=(const QwtDoubleInterval &) const;

    inline double minValue() const;
    inline double maxValue() const;
38

pixhawk's avatar
pixhawk committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    inline double width() const;

    inline void setMinValue(double);
    inline void setMaxValue(double);

    bool contains(double value) const;

    bool intersects(const QwtDoubleInterval &) const;
    QwtDoubleInterval intersect(const QwtDoubleInterval &) const;
    QwtDoubleInterval unite(const QwtDoubleInterval &) const;

    inline QwtDoubleInterval operator|(const QwtDoubleInterval &) const;
    inline QwtDoubleInterval operator&(const QwtDoubleInterval &) const;

    QwtDoubleInterval &operator|=(const QwtDoubleInterval &);
    QwtDoubleInterval &operator&=(const QwtDoubleInterval &);

    QwtDoubleInterval extend(double value) const;
    inline QwtDoubleInterval operator|(double) const;
    QwtDoubleInterval &operator|=(double);

    inline bool isValid() const;
    inline bool isNull() const;
    inline void invalidate();

    QwtDoubleInterval symmetrize(double value) const;

private:
    double d_minValue;
    double d_maxValue;
};

/*!
  \brief Default Constructor

  Creates an invalid interval [0.0, -1.0]
  \sa setInterval, isValid
*/
inline QwtDoubleInterval::QwtDoubleInterval():
    d_minValue(0.0),
    d_maxValue(-1.0)
{
}

/*!
   Constructor

   \param minValue Minimum value
   \param maxValue Maximum value
*/
inline QwtDoubleInterval::QwtDoubleInterval(double minValue, double maxValue):
    d_minValue(minValue),
    d_maxValue(maxValue)
{
}

/*!
   Assign the limits of the interval

   \param minValue Minimum value
   \param maxValue Maximum value
*/
inline void QwtDoubleInterval::setInterval(double minValue, double maxValue)
{
    d_minValue = minValue;
    d_maxValue = maxValue;
}

/*!
   Assign the lower limit of the interval

   \param minValue Minimum value
*/
inline void QwtDoubleInterval::setMinValue(double minValue)
113
{
pixhawk's avatar
pixhawk committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
    d_minValue = minValue;
}

/*!
   Assign the upper limit of the interval

   \param maxValue Maximum value
*/
inline void QwtDoubleInterval::setMaxValue(double maxValue)
{
    d_maxValue = maxValue;
}

//! \return Lower limit of the interval
128 129 130
inline double QwtDoubleInterval::minValue() const
{
    return d_minValue;
pixhawk's avatar
pixhawk committed
131 132 133
}

//! \return Upper limit of the interval
134 135 136
inline double QwtDoubleInterval::maxValue() const
{
    return d_maxValue;
pixhawk's avatar
pixhawk committed
137 138 139 140 141 142 143 144 145
}

/*!
   Return the width of an interval
   The width of invalid intervals is 0.0, otherwise the result is
   maxValue() - minValue().

   \sa isValid
*/
146 147 148
inline double QwtDoubleInterval::width() const
{
    return isValid() ? (d_maxValue - d_minValue) : 0.0;
pixhawk's avatar
pixhawk committed
149 150
}

151
/*!
pixhawk's avatar
pixhawk committed
152 153 154 155 156 157 158 159 160
   Intersection of two intervals
   \sa intersect
*/
inline QwtDoubleInterval QwtDoubleInterval::operator&(
    const QwtDoubleInterval &interval ) const
{
    return intersect(interval);
}

161
/*!
pixhawk's avatar
pixhawk committed
162 163 164 165 166 167 168 169 170 171 172 173 174
   Union of two intervals
   \sa unite
*/
inline QwtDoubleInterval QwtDoubleInterval::operator|(
    const QwtDoubleInterval &interval) const
{
    return unite(interval);
}

//! Compare two intervals
inline int QwtDoubleInterval::operator==(const QwtDoubleInterval &other) const
{
    return (d_minValue == other.d_minValue) &&
175
           (d_maxValue == other.d_maxValue);
pixhawk's avatar
pixhawk committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
}

//! Compare two intervals
inline int QwtDoubleInterval::operator!=(const QwtDoubleInterval &other) const
{
    return (!(*this == other));
}

/*!
   Extend an interval
   \sa extend
*/
inline QwtDoubleInterval QwtDoubleInterval::operator|(double value) const
{
    return extend(value);
}

//! \return true, if minValue() >= maxValue()
inline bool QwtDoubleInterval::isNull() const
{
    return d_minValue >= d_maxValue;
}

//! \return true, if minValue() <= maxValue()
inline bool QwtDoubleInterval::isValid() const
{
    return d_minValue <= d_maxValue;
}

/*!
  Invalidate the interval

  The limits are set to interval [0.0, -1.0]
  \sa isValid
*/
inline void QwtDoubleInterval::invalidate()
{
    d_minValue = 0.0;
    d_maxValue = -1.0;
}

#endif