1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
10
// OpenWalnut is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
15
// OpenWalnut is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public License
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
23
//---------------------------------------------------------------------------
28
#include <osg/ref_ptr>
30
#include "core/common/exceptions/WTypeMismatch.h"
31
#include "core/common/WLogger.h"
32
#include "core/graphicsEngine/WGEGroupNode.h"
33
#include "core/dataHandler/WDataSetScalar.h"
34
#include "core/dataHandler/WGridRegular3D.h"
35
#include "WSPSliceBuilder.h"
37
WSPSliceBuilder::WSPSliceBuilder( ProbTractList probTracts, WPropGroup sliceGroup, std::vector< WPropGroup > colorMap )
39
m_probTracts( probTracts ),
41
m_colorMap( colorMap ) // yes this is a COPY of the vector but WPropGroup is a boost::shared_ptr so updates will propagate!
43
m_slicePos[2] = sliceGroup->findProperty( "Axial Position" )->toPropDouble();
44
m_slicePos[1] = sliceGroup->findProperty( "Coronal Position" )->toPropDouble();
45
m_slicePos[0] = sliceGroup->findProperty( "Sagittal Position" )->toPropDouble();
47
checkAndExtractGrids();
48
computeSliceBB(); // just to be sure those are initialized, since they may change due to m_slicePos[0], et al. anyway
51
WSPSliceBuilder::~WSPSliceBuilder()
53
// since we are having virtual member functions we also need a virtual destructor
56
// helper functions only to be DRY
60
* Try a cast to WGridRegular3D, and return the cast result if it was successful, otherwise throw an exception of
63
* \param dataset The dataset of which the grid is taken from to check.
65
* \return The grid of the dataset casted to WGridRegular3D.
67
boost::shared_ptr< const WGridRegular3D > ensureWGridRegular3D( boost::shared_ptr< const WDataSetScalar > dataset )
69
boost::shared_ptr< const WGridRegular3D > result = boost::shared_dynamic_cast< WGridRegular3D >( dataset->getGrid() );
72
wlog::error( "WSPSliceBuilder" ) << "Cast to WGridRegular3D failed.";
73
throw WTypeMismatch( "WSPSliceBuilder::extractGrid(): WGridRegular3D expected, but cast failed." );
79
void WSPSliceBuilder::checkAndExtractGrids()
81
if( m_probTracts.empty() )
89
m_grid = ensureWGridRegular3D( m_probTracts.front() );
91
for( ProbTractList::const_iterator cit = m_probTracts.begin(); cit != m_probTracts.end(); ++cit )
93
boost::shared_ptr< const WGridRegular3D > grid = ensureWGridRegular3D( *cit );
94
// TODO(math): ensure that each WGridRegular3D is the same once the operator== is available for WGridRegular3D
98
catch( const WTypeMismatch& e )
100
wlog::error( "WSPSliceBuilder" ) << "At least one probabilistic tractogram has a grid which is not castable to WGridRegluar3D";
106
WColor WSPSliceBuilder::colorMap( size_t probTractNum ) const
108
std::string dataSetFileName = m_probTracts[probTractNum]->getFileName();
110
for( size_t i = 0; i < m_colorMap.size(); ++i )
112
std::string colorMapFileName = m_colorMap[i]->findProperty( "Filename" )->toPropString()->get();
113
if( colorMapFileName == dataSetFileName )
115
return m_colorMap[i]->findProperty( "Color" )->toPropColor()->get();
119
// keep old behaviour
120
return m_colorMap.at( probTractNum )->findProperty( "Color" )->toPropColor()->get();
123
bool WSPSliceBuilder::alphaBelowThreshold( const WColor& c, const double threshold ) const
125
return c[3] < threshold;
128
WColor WSPSliceBuilder::lookUpColor( const WPosition& pos, size_t tractID ) const
130
WColor c = colorMap( tractID );
131
bool success = false;
132
double probability = m_probTracts.at( tractID )->interpolate( pos, &success );
133
if( m_probTracts.at( tractID )->getMax() > 10 )
135
probability /= 255.0;
140
c[3] = ( success ? probability : -1.0 );
142
// // sinusiodal mapping
143
// double pi2 = 2*3.14159265358979323846;
144
// c[3] = ( success ? ( pi2*probability - std::sin(pi2*probability) ) / ( pi2 ) : -1.0 );
146
// // square root mapping
147
// c[3] = ( success ? std::sqrt( probability ) : -1.0 );
153
osg::ref_ptr< osg::Vec4Array > WSPSliceBuilder::computeColorsFor( const osg::Vec3& pos ) const
155
osg::ref_ptr< osg::Vec4Array > result( new osg::Vec4Array );
156
result->reserve( m_probTracts.size() );
158
// for each probabilisitc tractogram look up if its probability at this vertex is below a certain threshold or not
159
for( size_t tractID = 0; tractID < m_probTracts.size(); ++tractID )
161
WColor c = lookUpColor( WPosition( pos ), tractID );
164
result->push_back( c );
171
void WSPSliceBuilder::computeSliceBB()
175
wlog::warn( "WSPSliceBuilder" ) << "Invalid grid while BB computation!";
178
m_sliceBB[0] = WBoundingBox( m_grid->getOrigin() + m_slicePos[0]->get() * m_grid->getDirectionX(),
179
m_grid->getOrigin() + m_slicePos[0]->get() * m_grid->getDirectionX() + m_grid->getNbCoordsY() * m_grid->getDirectionY() +
180
m_grid->getNbCoordsZ() * m_grid->getDirectionZ() );
181
m_sliceBB[1] = WBoundingBox( m_grid->getOrigin() + m_slicePos[1]->get() * m_grid->getDirectionY(),
182
m_grid->getOrigin() + m_slicePos[1]->get() * m_grid->getDirectionY() + m_grid->getNbCoordsX() * m_grid->getDirectionX() +
183
m_grid->getNbCoordsZ() * m_grid->getDirectionZ() );
184
m_sliceBB[2] = WBoundingBox( m_grid->getOrigin() + m_slicePos[2]->get() * m_grid->getDirectionZ(),
185
m_grid->getOrigin() + m_slicePos[2]->get() * m_grid->getDirectionZ() + m_grid->getNbCoordsY() * m_grid->getDirectionY() +
186
m_grid->getNbCoordsX() * m_grid->getDirectionX() );