qwt_interval.cpp 8.24 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_interval.h"
#include "qwt_math.h"
#include <qalgorithms.h>

/*!
  \brief Normalize the limits of the interval

  If maxValue() < minValue() the limits will be inverted.
  \return Normalized interval

  \sa isValid(), inverted()
*/
QwtInterval QwtInterval::normalized() const
{
    if ( d_minValue > d_maxValue )
    {
        return inverted();
    }
    if ( d_minValue == d_maxValue && d_borderFlags == ExcludeMinimum )
    {
        return inverted();
    }

    return *this;
}

/*!
  Invert the limits of the interval
  \return Inverted interval
  \sa normalized()
*/
QwtInterval QwtInterval::inverted() const
{
    BorderFlags borderFlags = IncludeBorders;
    if ( d_borderFlags & ExcludeMinimum )
        borderFlags |= ExcludeMaximum;
    if ( d_borderFlags & ExcludeMaximum )
        borderFlags |= ExcludeMinimum;

    return QwtInterval( d_maxValue, d_minValue, borderFlags );
}

/*!
  Test if a value is inside an interval

  \param value Value
  \return true, if value >= minValue() && value <= maxValue()
*/
bool QwtInterval::contains( double value ) const
{
    if ( !isValid() )
        return false;

    if ( value < d_minValue || value > d_maxValue )
        return false;

    if ( value == d_minValue && d_borderFlags & ExcludeMinimum )
        return false;

    if ( value == d_maxValue && d_borderFlags & ExcludeMaximum )
        return false;

    return true;
}

//! Unite 2 intervals
QwtInterval QwtInterval::unite( const QwtInterval &other ) const
{
    /*
     If one of the intervals is invalid return the other one.
     If both are invalid return an invalid default interval
     */
    if ( !isValid() )
    {
        if ( !other.isValid() )
            return QwtInterval();
        else
            return other;
    }
    if ( !other.isValid() )
        return *this;

    QwtInterval united;
    BorderFlags flags = IncludeBorders;

    // minimum
    if ( d_minValue < other.minValue() )
    {
        united.setMinValue( d_minValue );
        flags &= d_borderFlags & ExcludeMinimum;
    }
    else if ( other.minValue() < d_minValue )
    {
        united.setMinValue( other.minValue() );
        flags &= other.borderFlags() & ExcludeMinimum;
    }
    else // d_minValue == other.minValue()
    {
        united.setMinValue( d_minValue );
        flags &= ( d_borderFlags & other.borderFlags() ) & ExcludeMinimum;
    }

    // maximum
    if ( d_maxValue > other.maxValue() )
    {
        united.setMaxValue( d_maxValue );
        flags &= d_borderFlags & ExcludeMaximum;
    }
    else if ( other.maxValue() > d_maxValue )
    {
        united.setMaxValue( other.maxValue() );
        flags &= other.borderFlags() & ExcludeMaximum;
    }
    else // d_maxValue == other.maxValue() )
    {
        united.setMaxValue( d_maxValue );
        flags &= d_borderFlags & other.borderFlags() & ExcludeMaximum;
    }

    united.setBorderFlags( flags );
    return united;
}

/*! 
  \brief Intersect 2 intervals
  
  \param other Interval to be intersect with
  \return Intersection
 */
QwtInterval QwtInterval::intersect( const QwtInterval &other ) const
{
    if ( !other.isValid() || !isValid() )
        return QwtInterval();

    QwtInterval i1 = *this;
    QwtInterval i2 = other;

    // swap i1/i2, so that the minimum of i1
    // is smaller then the minimum of i2

    if ( i1.minValue() > i2.minValue() )
    {
        qSwap( i1, i2 );
    }
    else if ( i1.minValue() == i2.minValue() )
    {
        if ( i1.borderFlags() & ExcludeMinimum )
            qSwap( i1, i2 );
    }

    if ( i1.maxValue() < i2.minValue() )
    {
        return QwtInterval();
    }

    if ( i1.maxValue() == i2.minValue() )
    {
        if ( i1.borderFlags() & ExcludeMaximum ||
            i2.borderFlags() & ExcludeMinimum )
        {
            return QwtInterval();
        }
    }

    QwtInterval intersected;
    BorderFlags flags = IncludeBorders;

    intersected.setMinValue( i2.minValue() );
    flags |= i2.borderFlags() & ExcludeMinimum;

    if ( i1.maxValue() < i2.maxValue() )
    {
        intersected.setMaxValue( i1.maxValue() );
        flags |= i1.borderFlags() & ExcludeMaximum;
    }
    else if ( i2.maxValue() < i1.maxValue() )
    {
        intersected.setMaxValue( i2.maxValue() );
        flags |= i2.borderFlags() & ExcludeMaximum;
    }
    else // i1.maxValue() == i2.maxValue()
    {
        intersected.setMaxValue( i1.maxValue() );
        flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
    }

    intersected.setBorderFlags( flags );
    return intersected;
}

/*! 
  \brief Unite this interval with the given interval.

  \param other Interval to be united with
  \return This interval
 */
QwtInterval& QwtInterval::operator|=( const QwtInterval &other )
{
    *this = *this | other;
    return *this;
}

/*! 
  \brief Intersect this interval with the given interval.

  \param other Interval to be intersected with
  \return This interval
 */
QwtInterval& QwtInterval::operator&=( const QwtInterval &other )
{
    *this = *this & other;
    return *this;
}

/*!
  \brief Test if two intervals overlap

  \param other Interval
  \return True, when the intervals are intersecting
*/
bool QwtInterval::intersects( const QwtInterval &other ) const
{
    if ( !isValid() || !other.isValid() )
        return false;

    QwtInterval i1 = *this;
    QwtInterval i2 = other;

    // swap i1/i2, so that the minimum of i1
    // is smaller then the minimum of i2

    if ( i1.minValue() > i2.minValue() )
    {
        qSwap( i1, i2 );
    }
    else if ( i1.minValue() == i2.minValue() &&
              i1.borderFlags() & ExcludeMinimum )
    {
        qSwap( i1, i2 );
    }

    if ( i1.maxValue() > i2.minValue() )
    {
        return true;
    }
    if ( i1.maxValue() == i2.minValue() )
    {
        return !( ( i1.borderFlags() & ExcludeMaximum ) ||
            ( i2.borderFlags() & ExcludeMinimum ) );
    }
    return false;
}

/*!
  Adjust the limit that is closer to value, so that value becomes
  the center of the interval.

  \param value Center
  \return Interval with value as center
*/
QwtInterval QwtInterval::symmetrize( double value ) const
{
    if ( !isValid() )
        return *this;

    const double delta =
        qMax( qAbs( value - d_maxValue ), qAbs( value - d_minValue ) );

    return QwtInterval( value - delta, value + delta );
}

/*!
  Limit the interval, keeping the border modes

  \param lowerBound Lower limit
  \param upperBound Upper limit

  \return Limited interval
*/
QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
{
    if ( !isValid() || lowerBound > upperBound )
        return QwtInterval();

    double minValue = qMax( d_minValue, lowerBound );
    minValue = qMin( minValue, upperBound );

    double maxValue = qMax( d_maxValue, lowerBound );
    maxValue = qMin( maxValue, upperBound );

    return QwtInterval( minValue, maxValue, d_borderFlags );
}

/*!
  \brief Extend the interval

  If value is below minValue(), value becomes the lower limit.
  If value is above maxValue(), value becomes the upper limit.

  extend() has no effect for invalid intervals

  \param value Value
  \return extended interval

  \sa isValid()
*/
QwtInterval QwtInterval::extend( double value ) const
{
    if ( !isValid() )
        return *this;

    return QwtInterval( qMin( value, d_minValue ),
        qMax( value, d_maxValue ), d_borderFlags );
}

/*!
  Extend an interval

  \param value Value
  \return Reference of the extended interval

  \sa extend()
*/
QwtInterval& QwtInterval::operator|=( double value )
{
    *this = *this | value;
    return *this;
}

#ifndef QT_NO_DEBUG_STREAM

QDebug operator<<( QDebug debug, const QwtInterval &interval )
{
    const int flags = interval.borderFlags();

    debug.nospace() << "QwtInterval("
        << ( ( flags & QwtInterval::ExcludeMinimum ) ? "]" : "[" )
        << interval.minValue() << "," << interval.maxValue()
        << ( ( flags & QwtInterval::ExcludeMaximum ) ? "[" : "]" )
        << ")";

    return debug.space();
}

#endif