WTransferFunctionHistogram.cpp 2.99 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 26 27
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include <algorithm>
#include <iostream>
#include <cmath>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
28
#include <vector>
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

#include "WTransferFunctionHistogram.h"
#include "WTransferFunctionWidget.h"

#include "QtGui/QPainter"
#include "QtGui/QStyleOption"
#include "QtGui/QGraphicsSceneMouseEvent"


WTransferFunctionHistogram::WTransferFunctionHistogram( WTransferFunctionWidget * /*parent*/ ) : BaseClass()
{
    setOpacity( 0.3 );
    setZValue( 2 );
}

WTransferFunctionHistogram::~WTransferFunctionHistogram()
{
}

QRectF WTransferFunctionHistogram::boundingRect() const
{
    return scene()->sceneRect();
}

void WTransferFunctionHistogram::paint( QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget* )
{
55
    const int steps = m_data.size();
56
    if( steps > 0 )
57
    {
58
        double maxval = *std::max_element( m_data.begin(), m_data.end() );
59
        if( maxval > 0.0 )
60 61 62 63 64 65 66 67 68 69 70 71 72
        {
            QRadialGradient gradient( 0.0, 0.0, 10 );
            painter->setBrush( gradient );
            gradient.setColorAt( 0, Qt::white );
            gradient.setColorAt( 1, Qt::black );

            painter->setBrush( gradient );

            QPolygon histogram;
            QRectF bb( this->scene()->sceneRect() );
            histogram << QPoint( bb.right(), bb.bottom() );
            histogram << QPoint( bb.left(), bb.bottom() );

73
            for( int i = 0; i < steps; ++i )
74
            {
75 76
                // logarithmic mapping of histogram to values
                // the added 1.001 is to avoid numerical problems but should work for most data sets
77
                histogram << QPoint( bb.left()+ ( double )bb.width()*( double )i/( double )steps,
78
                          bb.bottom() - ( double )bb.height() * std::log( m_data[ i ]+1 )/std::log( maxval+1.001 ) );
79 80 81 82 83
            }
            painter->drawPolygon( histogram );
        }
    }
}
84 85 86 87 88 89 90 91 92 93 94

const std::vector< double >& WTransferFunctionHistogram::getData() const
{
    return m_data;
}

std::vector< double >& WTransferFunctionHistogram::getData()
{
    return m_data;
}