FunctionGraphWidget.cpp 11.1 KB
Newer Older
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
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Gnevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "FunctionGraphWidget.h"




const float FunctionGraphWidget::RADIUS_DEFAULT_VALUE[PREDEFINED_FUNC_COUNT] = { 0.0f, 0.2f, 0.5f, 10.0f, 0.5f, 0.1f };
const float FunctionGraphWidget::RADIUS_UPDATE_FACTOR[PREDEFINED_FUNC_COUNT] = { 0.0f, 0.05f, 0.05f, -0.1f, 0.05f, 0.05f };


FunctionGraphWidget::FunctionGraphWidget( const QString& label, QWidget *parent ) :
    QWidget(parent),
    m_Label(label),
    m_CurrentMode(LINEAR)
{
    for( int i=0; i<PREDEFINED_FUNC_COUNT; ++i )
        m_FunctionParam[i].set( 0.0f, 0.5f, RADIUS_DEFAULT_VALUE[i], 1.0f );

26
#if QT_VERSION < QT_VERSION_CHECK(5,11,0)
27
    int minWidth = 8 + QFontMetrics(font()).width( m_Label );
28 29 30
#else
    int minWidth = 8 + QFontMetrics(font()).horizontalAdvance( m_Label );
#endif
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    setMinimumSize( std::max(200,minWidth), 100 );
}


void FunctionGraphWidget::resizeEvent( QResizeEvent *evt )
{
    if( m_CurrentMode == USER )
    {
        QVector<float> resizedFuncValues( evt->size().width() );
        getUserFunction( resizedFuncValues );
        m_FuncValues = resizedFuncValues;
    }
    else
    {
        m_FuncValues.resize( evt->size().width() );
        getFunction( m_FuncValues );
    }
}


void FunctionGraphWidget::paintEvent( QPaintEvent *evt )
{
    if( !m_FuncValues.empty() )
    {
55 56
        QColor color = this->palette().windowText().color();

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        QPolygonF polyPoints( m_FuncValues.size()+2 );
        for( int i=0; i<m_FuncValues.size(); ++i )
        {
            polyPoints[i].setX( float(i) );
            polyPoints[i].setY( (height()-1)*(1.0f-m_FuncValues[i]) );
        }
        polyPoints[m_FuncValues.size()+0].setX( width()-1 );
        polyPoints[m_FuncValues.size()+0].setY( height()-1 );
        polyPoints[m_FuncValues.size()+1].setX( 0 );
        polyPoints[m_FuncValues.size()+1].setY( height()-1 );

        QPainterPath polyPath;
        polyPath.addPolygon( polyPoints );

        QPainter painter;
        painter.begin( this );
            // Background grid.
            painter.setOpacity( 0.1f );
75
            painter.setPen( QPen(QBrush(color),1.0f) );
76 77 78 79 80 81 82 83 84 85
            for( int i=0; i<4; ++i )
            {
                float y = 0.25f*i * (height()-1);
                painter.drawLine( 0, int(y), width()-1, int(y) );
                float x = 0.25f*(i+1) * (width()-1);
                painter.drawLine( int(x), 0, int(x), height()-1 );
            }

            // Function curve.
            painter.setOpacity( 0.1f );
86
            painter.fillPath( polyPath, QBrush(color) );
87 88

            painter.setOpacity( 0.5f );
89
            painter.setPen( QPen(QBrush(color),1.0f) );
90 91 92 93
            painter.drawPolyline( &polyPoints[0], m_FuncValues.size() );

            // Axis.
            painter.setOpacity( 1.0f );
94
            painter.setPen( QPen(QBrush(color),1.0f) );
95 96 97 98 99 100 101 102 103 104 105 106 107 108
            painter.drawLine( 0, 0, 0, height()-1 );
            painter.drawLine( 0, height()-1, width()-1, height()-1 );

            // Vertical arrow head.
            painter.drawLine( 1, 2, 1, 5 );
            painter.drawLine( 2, 4, 2, 5 );
            painter.drawLine( 1, 6, 3, 6 );

            // Horizontal arrow head.
            painter.drawLine( width()-6, height()-2, width()-3, height()-2 );
            painter.drawLine( width()-6, height()-3, width()-5, height()-3 );
            painter.drawLine( width()-7, height()-2, width()-7, height()-4 );

            QFontMetrics fm( font() );
109
#if QT_VERSION < QT_VERSION_CHECK(5,11,0)
110
            int labelW = fm.width( m_Label );
111 112 113
#else
            int labelW = fm.horizontalAdvance( m_Label );
#endif
114

115
            painter.setPen( QPen(QBrush(color),1.0f) );
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
            painter.drawText( (width()-labelW)>>1, 2+fm.height(), m_Label );
        painter.end();
    }
}


void FunctionGraphWidget::mouseDoubleClickEvent( QMouseEvent *evt )
{
    if( m_CurrentMode != USER )
    {
        FunctionMode m = baseMode();
        m_FunctionParam[m].set( 0.0f, 0.5f, RADIUS_DEFAULT_VALUE[m], 1.0f );
        getFunction( m_FuncValues );
    }
}


void FunctionGraphWidget::mousePressEvent( QMouseEvent *evt )
{
    m_CurrentMouseMotion = NONE;

    m_PrevMouseX = evt->x();
    m_PrevMouseY = evt->y();
}


void FunctionGraphWidget::mouseMoveEvent( QMouseEvent *evt )
{
    if( evt->buttons() & Qt::LeftButton )
    {
        if( (evt->x() >= 0  &&  evt->x() < width())  ||  (m_PrevMouseX >= 0  &&  m_PrevMouseY < width()) )
        {
            int xMin, xMax, yMin, yMax;
            if( evt->x() < m_PrevMouseX )
            {
                xMin = evt->x();
                xMax = m_PrevMouseX;
                yMin = evt->y();
                yMax = m_PrevMouseY;
            }
            else
            {
                xMin = m_PrevMouseX;
                xMax = evt->x();
                yMin = m_PrevMouseY;
                yMax = evt->y();
            }

            float y = yMin;
            float dy = float(yMax - yMin) / (xMax - xMin);

            for( int x=xMin; x<=xMax; ++x, y+=dy )
                if( x >= 0  &&  x < width() )
                {
                    float v = 1.0f - y / (height()-1);
                    m_FuncValues[x] = std::min( std::max(v,0.0f), 1.0f );
                }

            m_CurrentMode = USER;
            emit functionModified();
            update();
        }
    }
    else if( (evt->buttons() & Qt::RightButton)  &&  m_CurrentMode != USER )
    {
        float dx = evt->x() - m_PrevMouseX;
        float dy = (evt->y() - m_PrevMouseY) * (m_CurrentMode & INV_FLAG? -1 : 1);

        if( m_CurrentMouseMotion == NONE )
            m_CurrentMouseMotion = std::abs(dx)>std::abs(dy)?  HORIZONTAL  :  VERTICAL;

        if( evt->modifiers() & Qt::ShiftModifier )
        {
            dx *= 0.1f;
            dy *= 0.1f;
        }

        dx /= width()  - 1;
        dy /= height() - 1;

        FunctionMode m = baseMode();
        if( m_CurrentMouseMotion == HORIZONTAL )
            m_FunctionParam[m].center += dx;
        else
            m_FunctionParam[m].zero += dy;

        getFunction( m_FuncValues );
    }

    m_PrevMouseX = evt->x();
    m_PrevMouseY = evt->y();
}


void FunctionGraphWidget::wheelEvent( QWheelEvent *evt )
{
    FunctionMode m = baseMode();
    if( m == USER )
        return;

    float deltaSign = (float) evt->delta() / std::abs(evt->delta());

    if( (evt->modifiers() & Qt::ShiftModifier)  ||  m == LINEAR )
    {
        float newHeight = m_FunctionParam[m].height + 2.0f * deltaSign / (height()-1);
        if( newHeight >= 0.0f )
        {
            m_FunctionParam[m].zero += 0.5f*(newHeight - m_FunctionParam[m].height);
            m_FunctionParam[m].height = newHeight;
            getFunction( m_FuncValues );
        }
    }
    else
    {
        float newRadius = m_FunctionParam[m].radius * (1.0f + RADIUS_UPDATE_FACTOR[m]*deltaSign);
        if( newRadius > 0.0f )
        {
            m_FunctionParam[m].radius = newRadius;
            getFunction( m_FuncValues );
        }
    }
}


void FunctionGraphWidget::getFunction( QVector<float> &funcSamples )
{
    switch( baseMode() )
    {
        case LINEAR:        getLinearFunction(funcSamples); break;
        case BOX:           getBoxFunction(funcSamples); break;
        case GAUSSIAN:      getGaussianFunction(funcSamples); break;
        case SIGMOID:       getSigmoidFunction(funcSamples); break;
        case POWER:         getPowerFunction(funcSamples); break;
        case GEMANMCLURE:   getGemanMcLureFunction(funcSamples); break;
        case USER:          getUserFunction( funcSamples ); break;
    }

    if( &funcSamples == &m_FuncValues )
    {
        emit functionModified();
        update();
    }
}


void FunctionGraphWidget::getUserFunction( QVector<float> &funcSamples )
{
    const float step = (float) (m_FuncValues.size()-1) / funcSamples.size();
    float x = 0.5f * step;

    for( int i=0; i<funcSamples.size(); ++i, x+=step )
    {
        // Get the two closest function samples.
        float x1 = m_FuncValues[ (int)x   ];
        float x2 = m_FuncValues[ (int)x+1 ];

        // New value is the linear interpolation of these two samples.
        float t = x - (int) x;
        funcSamples[i] = (1.0f-t)*x1 + t*x2;
    }
}


void FunctionGraphWidget::getLinearFunction( QVector<float> &funcSamples )
{
    struct FuncLinear {
        inline FuncLinear( FunctionParam &p ) {}
        inline float operator()( FunctionParam &p, float x ) { return x+0.5f; }
    };
    m_FunctionParam[baseMode()].apply<FuncLinear>( m_CurrentMode, funcSamples );
}


void FunctionGraphWidget::getBoxFunction( QVector<float> &funcSamples )
{
    struct FuncBox {
        inline FuncBox( FunctionParam &p ) {}
        inline float operator()( FunctionParam &p, float x ) { return std::abs(x)<p.radius? 1.0f : 0.0f; }
    };
    m_FunctionParam[baseMode()].apply<FuncBox>( m_CurrentMode, funcSamples );
}


void FunctionGraphWidget::getGaussianFunction( QVector<float> &funcSamples )
{
    struct FuncGaussian {
        float B;
        inline FuncGaussian( FunctionParam &p ) { float sigma = p.radius / 3.0f;  B = 1.0f / (-2.0f * sigma * sigma); }
        inline float operator()( FunctionParam &p, float x ) { return std::exp( B*x*x ); }
    };
    m_FunctionParam[baseMode()].apply<FuncGaussian>( m_CurrentMode, funcSamples );
}


void FunctionGraphWidget::getSigmoidFunction( QVector<float> &funcSamples )
{
    struct FuncSigmoid {
        inline FuncSigmoid( FunctionParam &p ) {}
        inline float operator()( FunctionParam &p, float x ) { return 1.0f / (1.0f + std::exp( -p.radius*x )); }
    };
    m_FunctionParam[baseMode()].apply<FuncSigmoid>( m_CurrentMode, funcSamples );
}


void FunctionGraphWidget::getPowerFunction( QVector<float> &funcSamples )
{
    struct FuncPower {
        inline FuncPower( FunctionParam &p ) {}
        inline float operator()( FunctionParam &p, float x ) { return std::pow(std::abs(2.0f*x),p.radius); }
    };
    m_FunctionParam[baseMode()].apply<FuncPower>( m_CurrentMode, funcSamples );
}


void FunctionGraphWidget::getGemanMcLureFunction( QVector<float> &funcSamples )
{
    struct FuncGM {
        float rSq;
        inline FuncGM( FunctionParam &p ) { rSq = p.radius * p.radius; }
        inline float operator()( FunctionParam &p, float x ) { float xSq = 0.25f*x*x;  return xSq / (rSq+xSq); }
    };
    m_FunctionParam[baseMode()].apply<FuncGM>( m_CurrentMode, funcSamples );
}


void FunctionGraphWidget::setLinear()
{
    m_CurrentMode = (m_CurrentMode == LINEAR)?  LINEAR_INV  :  LINEAR;
    getFunction( m_FuncValues );
}


void FunctionGraphWidget::setBox()
{
    m_CurrentMode = (m_CurrentMode == BOX)?  BOX_INV  :  BOX;
    getFunction( m_FuncValues );
}


void FunctionGraphWidget::setGaussian()
{
    m_CurrentMode = (m_CurrentMode == GAUSSIAN)?  GAUSSIAN_INV  :  GAUSSIAN;
    getFunction( m_FuncValues );
}


void FunctionGraphWidget::setSigmoid()
{
    m_CurrentMode = (m_CurrentMode == SIGMOID)?  SIGMOID_INV  :  SIGMOID;
    getFunction( m_FuncValues );
}


void FunctionGraphWidget::setPower()
{
    m_CurrentMode = (m_CurrentMode == POWER)?  POWER_INV  :  POWER;
    getFunction( m_FuncValues );
}


void FunctionGraphWidget::setGemanMcLure()
{
    m_CurrentMode = (m_CurrentMode == GEMANMCLURE)?  GEMANMCLURE_INV  :  GEMANMCLURE;
    getFunction( m_FuncValues );
}