WCsvConverter.cpp 13.4 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
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

25
#include <string>
26
#include <vector>
27

28
#include "WCsvConverter.h"
29

30
WCsvConverter::WCsvConverter( WProtonData::SPtr protonData, boost::shared_ptr< WPropertyStatus > propertyStatus,
31 32
                                WModule::SPtr colorBar )
{
33 34 35 36 37
    if( protonData == nullptr || propertyStatus == nullptr || colorBar == nullptr )
    {
        throw WException( "Could not convert CSV data! Proton data, property status or color bar are missing!" );
    }

38 39
    m_protonData = protonData;
    m_propertyStatus = propertyStatus;
40 41
    m_vectors = WConverterVectors::SPtr( new WConverterVectors() );
    m_indexes = WConverterIndexes::SPtr( new WConverterIndexes() );
42 43 44 45 46
    m_colorBar = colorBar;

    setOutputFromCSV( );
}

47
boost::shared_ptr< WDataSetFibers > WCsvConverter::getFibers()
48 49 50 51
{
    return m_fibers;
}

52
boost::shared_ptr< WDataSetPoints > WCsvConverter::getPoints()
53 54 55 56
{
    return m_points;
}

57
boost::shared_ptr< WDataSetPointsAndEventID > WCsvConverter::getPointsAndIDs()
58 59 60 61
{
    return m_selectedEventIDs;
}

62
void WCsvConverter::setOutputFromCSV( )
63
{
64 65 66
    if( !m_protonData->isColumnAvailable( WSingleSelectorName::getX() ) ||
        !m_protonData->isColumnAvailable( WSingleSelectorName::getY() ) ||
        !m_protonData->isColumnAvailable( WSingleSelectorName::getZ() ) )
67 68 69 70 71 72
    {
        return;
    }

    WDataSetCSV::ContentSPtr data = m_protonData->getCSVData();

73
    WColor plainColor = m_propertyStatus->getVisualizationPropertyHandler()->getColorSelection()->get( true );
74 75 76 77 78 79 80 81

    m_vectors->clear();
    m_indexes->update( m_protonData );

    float maxEdep = 0.0;

    for( WDataSetCSV::Content::iterator dataRow = data->begin(); dataRow < data->end(); dataRow++ )
    {
82
        if( !checkConditionToPass( dataRow ) )
83 84 85 86
        {
            continue;
        }

87 88 89 90 91 92 93
        float edep = stringToDouble( dataRow->at( m_indexes->getEdep() ) );

        if( getClusterSize( edep ) < 1.0 || getClusterSize( edep ) > 35.0 )
        {
            continue;
        }

94
        addVertex( dataRow );
95
        addColor( plainColor );
96 97 98 99 100
        addEdepAndSize( dataRow, &maxEdep );
        addEventID( dataRow );
    }

    if( checkIfOutputIsNull() )
101
    {
102 103 104
        return;
    }

Joshua Santana's avatar
Joshua Santana committed
105
    normalizeEdeps( m_vectors->getEdeps(), m_vectors->getColors(),  maxEdep );
106

107 108
    createOutputPoints();
    createOutputFibers();
109
    createOutputPointsAndEventIDs();
110 111
}

112
boost::shared_ptr< WDataSetSingle > WCsvConverter::getTransferFunction()
113 114 115 116
{
    return m_transferFunction;
}

117
boost::shared_ptr< std::vector<unsigned char> > WCsvConverter::sampleTransferFunction()
118
{
119
    boost::shared_ptr< std::vector<unsigned char> > data( new std::vector<unsigned char>( 50 * 4 ) );
120

121
    WTransferFunction tf = m_propertyStatus->getVisualizationPropertyHandler()->getTransferFunction()->get( true );
122

123
    tf.sample1DTransferFunction( &( *data )[ 0 ], 50, 0.0, 1.0 );
124 125 126 127

    return data;
}

128
void WCsvConverter::normalizeEdeps( SPFloatVector edeps, SPFloatVector colorArray, float maxEdep )
129
{
130
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getEdep() ) )
131
    {
132
        boost::shared_ptr< std::vector< unsigned char > > data = sampleTransferFunction();
133

134
        setTransferFunction( data );
135

136
        float maxClusterSize = getClusterSize( maxEdep );
137

138 139 140 141 142
        for( std::vector< float >::iterator currentEdep = edeps->begin();
            currentEdep != edeps->end();
            currentEdep++ )
        {
            float clusterSizeNormalized = getClusterSize( *currentEdep ) / maxClusterSize;
143

144
            m_vectors->getSizes()->push_back( clusterSizeNormalized );
145

146 147
            if( m_propertyStatus->getVisualizationPropertyHandler()->getColorFromEdep()->get() )
            {
148
                clusterSizeNormalized = static_cast< int >( 49 * clusterSizeNormalized );
149

150 151 152 153
                for( int i = 0; i < 4; i++ )
                {
                    colorArray->push_back( data->at( clusterSizeNormalized * 4 + i ) / 255.0 );
                }
154 155
            }
        }
156

157 158 159 160
        m_colorBar->getProperties()->getProperty( "Max scale value" )->set( 0.0 );
        m_colorBar->getProperties()->getProperty( "Max scale value" )->set( maxClusterSize );
        m_colorBar->getProperties()->getProperty( "Description" )->set( std::string( "Clustersize " ) );

161 162 163
        bool activated = m_propertyStatus->getVisualizationPropertyHandler()->getColorFromEdep()->get();

        m_colorBar->getProperties()->getProperty( "active" )->toPropBool()->set( activated );
164 165 166
    }
}

167
bool WCsvConverter::checkConditionToPass( WDataSetCSV::Content::iterator dataRow )
168
{
169
    if( dataRow->empty() )
170
    {
171
        return false;
172 173
    }

174
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getParentId() ) )
175
    {
176
        int PrimaryValue = stringToInt( dataRow->at( m_indexes->getParentID() ) );
177 178

        if( !m_propertyStatus->getFilterPropertyHandler()->getShowPrimaries()->get() && PrimaryValue == 0 )
179 180 181
        {
            return false;
        }
182

183
        if( !m_propertyStatus->getFilterPropertyHandler()->getShowSecondaries()->get() && PrimaryValue != 0 )
184 185 186
        {
            return false;
        }
187
    }
188

189
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getPDG() ) )
190
    {
191
        if( !m_propertyStatus->getFilterPropertyHandler()->isPDGTypeSelected(
hungdangquoc's avatar
hungdangquoc committed
192
           ( int )stringToDouble( dataRow->at( m_indexes->getPDGEncoding( ) ) ) ) )
193
        {
194 195
            return false;
        }
196
    }
197

198
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getEventId() ) )
199
    {
200 201 202 203 204
        if(dataRow->at( m_indexes->getEventID() ) == "NULL")
        {
            return true;
        }

205
        int eventID = stringToInt( dataRow->at( m_indexes->getEventID() ) );
206 207 208 209 210
        if( eventID < m_propertyStatus->getEventIDLimitationPropertyHandler()->getMinCap()->get() ||
            eventID > m_propertyStatus->getEventIDLimitationPropertyHandler()->getMaxCap()->get() )
        {
            return false;
        }
211
    }
212

213 214
    return true;
}
215

216
void WCsvConverter::addVertex( WDataSetCSV::Content::iterator dataRow )
217
{
hungdangquoc's avatar
hungdangquoc committed
218 219 220
    m_vectors->getVertices()->push_back( stringToDouble( dataRow->at( m_indexes->getPosX() ) ) );
    m_vectors->getVertices()->push_back( stringToDouble( dataRow->at( m_indexes->getPosY() ) ) );
    m_vectors->getVertices()->push_back( stringToDouble( dataRow->at( m_indexes->getPosZ() ) ) );
221
}
222

223
void WCsvConverter::addColor( WColor plainColor )
224
{
225
    if( !m_protonData->isColumnAvailable( WSingleSelectorName::getEdep() ) ||
226
        !m_propertyStatus->getVisualizationPropertyHandler()->getColorFromEdep()->get() )
227
    {
228 229 230
        m_vectors->getColors()->push_back( plainColor[0] );
        m_vectors->getColors()->push_back( plainColor[1] );
        m_vectors->getColors()->push_back( plainColor[2] );
231
    }
232
}
233

234
void WCsvConverter::addEdepAndSize( WDataSetCSV::Content::iterator dataRow, float* maxEdep )
235
{
236
    if( !m_protonData->isColumnAvailable( WSingleSelectorName::getEdep() ) )
237 238 239 240
    {
        return;
    }

hungdangquoc's avatar
hungdangquoc committed
241
    float edep = stringToDouble( dataRow->at( m_indexes->getEdep() ) );
242
    if( edep > *maxEdep )
243
    {
244
        *maxEdep = edep;
245
    }
246

247 248
    m_vectors->getEdeps()->push_back( edep );
}
249

250
void WCsvConverter::calculateFibers()
251
{
252
    if( !m_protonData->isColumnAvailable( WSingleSelectorName::getEventId() ) )
253 254 255 256
    {
        return;
    }

257 258 259
    int fiberLength = 0;
    int fiberStartIndex = 0;
    int reversePos = 0;
260
    size_t currentEventID = m_vectors->getEventIDs()->at( 0 );
261

262
    m_vectors->getFiberStartIndexes()->push_back( fiberStartIndex );
263

264
    for( size_t eID : *( m_vectors->getEventIDs() ) )
265
    {
266
        if( currentEventID != eID )
267
        {
268 269 270 271
            m_vectors->getFiberStartIndexes()->push_back( fiberStartIndex );
            m_vectors->getFiberLengths()->push_back( fiberLength );

            currentEventID = eID;
272
            fiberLength = 0;
273

274 275 276 277
            reversePos++;
        }
        fiberLength++;
        fiberStartIndex++;
278
        m_vectors->getVerticesReverse()->push_back( reversePos );
279 280
    }

281 282
    m_vectors->getFiberLengths()->push_back( fiberLength );
}
283

284
void WCsvConverter::createOutputPoints()
285
{
286
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getEdep() ) )
287
    {
288
        if( m_propertyStatus->getVisualizationPropertyHandler()->getSizesFromEdep()->get() )
289 290 291 292 293 294 295 296
        {
            m_points = boost::shared_ptr< WDataSetPointsAndSizes >(
                    new WDataSetPointsAndSizes(
                            m_vectors->getVertices(),
                            m_vectors->getColors(),
                            m_vectors->getSizes()
                    )
            );
297
            return;
298
        }
299
    }
300
    m_points = boost::shared_ptr < WDataSetPoints >(
301
                    new WDataSetPoints(
302 303 304 305
                    m_vectors->getVertices(),
                    m_vectors->getColors()
            )
    );
306 307
}

308
void WCsvConverter::createOutputFibers()
309
{
310
    calculateFibers();
311
    if( !m_protonData->isColumnAvailable( WSingleSelectorName::getEventId() ) )
312
    {
313 314 315 316 317 318 319 320
        m_fibers = boost::shared_ptr< WDataSetFibers >(
            new WDataSetFibers(
                    SPFloatVector( new std::vector< float >() ),
                    SPSizeVector( new std::vector< size_t >() ),
                    SPSizeVector( new std::vector< size_t >() ),
                    SPSizeVector( new std::vector< size_t >() )
            )
        );
321

322
        return;
323
    }
324
    else
325
    {
326 327 328 329 330 331 332 333
        m_fibers = boost::shared_ptr< WDataSetFibers >(
            new WDataSetFibers(
                    m_vectors->getVertices(),
                    m_vectors->getFiberStartIndexes(),
                    m_vectors->getFiberLengths(),
                    m_vectors->getVerticesReverse()
            )
        );
334 335
    }

336
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getEdep() ) )
337
    {
338 339 340 341 342 343 344 345
        if( m_propertyStatus->getVisualizationPropertyHandler()->getColorFromEdep()->get() )
        {
            m_fibers->removeColorScheme( m_fibers->getColorScheme( "Global Color" )->getColor() );
            m_fibers->removeColorScheme( m_fibers->getColorScheme( "Local Color" )->getColor() );
            m_fibers->removeColorScheme( m_fibers->getColorScheme( "Custom Color" )->getColor() );
            m_fibers->addColorScheme( m_vectors->getColors(), "Energy deposition",
                                      "Color fibers based on their energy." );
        }
346
    }
347
}
348

349
void WCsvConverter::createOutputPointsAndEventIDs()
350
{
351
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getEventId() ) )
352 353 354 355 356 357 358 359
    {
        m_selectedEventIDs = boost::shared_ptr < WDataSetPointsAndEventID >(
                new WDataSetPointsAndEventID(
                        m_vectors->getVertices(),
                        m_vectors->getColors(),
                        m_vectors->getEventIDs()
                )
        );
360
    }
361 362
}

363
void WCsvConverter::addEventID( WDataSetCSV::Content::iterator dataRow )
364
{
365
    if( m_protonData->isColumnAvailable( WSingleSelectorName::getEventId() ) )
366 367
    {
        if(dataRow->at( m_indexes->getEventID() ) == "NULL")
368
        {
369
            return;
370
        }
371 372 373

        m_vectors->getEventIDs()->push_back( stringToInt( dataRow->at( m_indexes->getEventID() ) ) );
    }
374
}
375

376
void WCsvConverter::setTransferFunction( boost::shared_ptr< std::vector<unsigned char> > data )
377 378 379 380
{
    boost::shared_ptr< WValueSetBase > newValueSet( new WValueSet<unsigned char>( 1, 4, data, W_DT_UNSIGNED_CHAR ) );

    WGridTransformOrtho transform;
381
    boost::shared_ptr< WGridRegular3D > newGrid( new WGridRegular3D( 50, 1, 1, transform ) );
382 383 384 385
    boost::shared_ptr< WDataSetSingle > newData( new WDataSetSingle( newValueSet, newGrid ) );

    m_transferFunction = newData;
}
386

387
bool WCsvConverter::checkIfOutputIsNull()
388 389
{
    if( m_vectors->getVertices()->empty() )
390
    {
391 392
        m_points = NULL;
        m_fibers = NULL;
393
        m_selectedEventIDs = NULL;
394
        return true;
395
    }
396
    return false;
397
}
398

399
float WCsvConverter::getClusterSize( float edep )
400
{
401
    return 7.6626 * pow( edep * 1000 / 25, 0.420307 );
402
}
403

hungdangquoc's avatar
hungdangquoc committed
404
float WCsvConverter::stringToDouble( std::string str )
405 406 407
{
    try
    {
hungdangquoc's avatar
hungdangquoc committed
408
        return boost::lexical_cast< double >( str );
409
    }
Robin Eschbach's avatar
Robin Eschbach committed
410
    catch( const boost::bad_lexical_cast &e )
411
    {
hungdangquoc's avatar
hungdangquoc committed
412 413 414
        std::string errorMessage = "The selected column has an incorrect format. Received: " + str + ". " +
                                        "Required: Numbers are expected. " +
                                        std::string( e.what() );
415

hungdangquoc's avatar
hungdangquoc committed
416
        throw WException( errorMessage );
417 418 419
    }
}

420
int WCsvConverter::stringToInt( std::string str )
421
{
hungdangquoc's avatar
hungdangquoc committed
422 423 424
    //lexical_cast <int> cannot cast numbers as exponential notation, so we take this way.
    float numAsDouble = stringToDouble( str );
    return ( int )numAsDouble;
425
}