~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

Viewing changes to src/modules/schmahmannPandyaSlices/probTractDisplaySP/WSPSliceBuilder.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//---------------------------------------------------------------------------
2
 
//
3
 
// Project: OpenWalnut ( http://www.openwalnut.org )
4
 
//
5
 
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
 
// For more information see http://www.openwalnut.org/copying
7
 
//
8
 
// This file is part of OpenWalnut.
9
 
//
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.
14
 
//
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.
19
 
//
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/>.
22
 
//
23
 
//---------------------------------------------------------------------------
24
 
 
25
 
#include <cmath>
26
 
#include <vector>
27
 
 
28
 
#include <osg/ref_ptr>
29
 
 
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"
36
 
 
37
 
WSPSliceBuilder::WSPSliceBuilder( ProbTractList probTracts, WPropGroup sliceGroup, std::vector< WPropGroup > colorMap )
38
 
    : m_slicePos( 3 ),
39
 
      m_probTracts( probTracts ),
40
 
      m_sliceBB( 3 ),
41
 
      m_colorMap( colorMap ) // yes this is a COPY of the vector but WPropGroup is a boost::shared_ptr so updates will propagate!
42
 
{
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();
46
 
 
47
 
    checkAndExtractGrids();
48
 
    computeSliceBB(); // just to be sure those are initialized, since they may change due to m_slicePos[0], et al. anyway
49
 
}
50
 
 
51
 
WSPSliceBuilder::~WSPSliceBuilder()
52
 
{
53
 
    // since we are having virtual member functions we also need a virtual destructor
54
 
}
55
 
 
56
 
// helper functions only to be DRY
57
 
namespace
58
 
{
59
 
    /**
60
 
     * Try a cast to WGridRegular3D, and return the cast result if it was successful, otherwise throw an exception of
61
 
     * WTypeMismatched.
62
 
     *
63
 
     * \param dataset The dataset of which the grid is taken from to check.
64
 
     *
65
 
     * \return The grid of the dataset casted to WGridRegular3D.
66
 
     */
67
 
    boost::shared_ptr< const WGridRegular3D > ensureWGridRegular3D( boost::shared_ptr< const WDataSetScalar > dataset )
68
 
    {
69
 
        boost::shared_ptr< const WGridRegular3D > result = boost::shared_dynamic_cast< WGridRegular3D >( dataset->getGrid() );
70
 
        if( !result )
71
 
        {
72
 
            wlog::error( "WSPSliceBuilder" ) << "Cast to WGridRegular3D failed.";
73
 
            throw WTypeMismatch( "WSPSliceBuilder::extractGrid(): WGridRegular3D expected, but cast failed." );
74
 
        }
75
 
        return result;
76
 
    }
77
 
}
78
 
 
79
 
void WSPSliceBuilder::checkAndExtractGrids()
80
 
{
81
 
    if( m_probTracts.empty() )
82
 
    {
83
 
        m_grid.reset();
84
 
    }
85
 
    else
86
 
    {
87
 
        try
88
 
        {
89
 
            m_grid = ensureWGridRegular3D( m_probTracts.front() );
90
 
 
91
 
            for( ProbTractList::const_iterator cit = m_probTracts.begin(); cit != m_probTracts.end(); ++cit )
92
 
            {
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
95
 
                // grid == m_grid
96
 
            }
97
 
        }
98
 
        catch( const WTypeMismatch& e )
99
 
        {
100
 
            wlog::error( "WSPSliceBuilder" ) << "At least one probabilistic tractogram has a grid which is not castable to WGridRegluar3D";
101
 
            throw e;
102
 
        }
103
 
    }
104
 
}
105
 
 
106
 
WColor WSPSliceBuilder::colorMap( size_t probTractNum ) const
107
 
{
108
 
    std::string dataSetFileName = m_probTracts[probTractNum]->getFileName();
109
 
 
110
 
    for( size_t i = 0; i < m_colorMap.size(); ++i )
111
 
    {
112
 
        std::string colorMapFileName = m_colorMap[i]->findProperty( "Filename" )->toPropString()->get();
113
 
        if( colorMapFileName == dataSetFileName )
114
 
        {
115
 
            return m_colorMap[i]->findProperty( "Color" )->toPropColor()->get();
116
 
        }
117
 
    }
118
 
 
119
 
    // keep old behaviour
120
 
    return m_colorMap.at( probTractNum )->findProperty( "Color" )->toPropColor()->get();
121
 
}
122
 
 
123
 
bool WSPSliceBuilder::alphaBelowThreshold( const WColor& c, const double threshold ) const
124
 
{
125
 
    return c[3] < threshold;
126
 
}
127
 
 
128
 
WColor WSPSliceBuilder::lookUpColor( const WPosition& pos, size_t tractID ) const
129
 
{
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 )
134
 
    {
135
 
        probability /= 255.0;
136
 
    }
137
 
    if( c[3] != 0.0 )
138
 
    {
139
 
        // linear mapping
140
 
        c[3] = ( success ? probability : -1.0 );
141
 
 
142
 
        // // sinusiodal mapping
143
 
        // double pi2 = 2*3.14159265358979323846;
144
 
        // c[3] = ( success ? ( pi2*probability - std::sin(pi2*probability) ) / ( pi2 ) : -1.0 );
145
 
 
146
 
        // // square root mapping
147
 
        // c[3] = ( success ? std::sqrt( probability ) : -1.0 );
148
 
    }
149
 
 
150
 
    return c;
151
 
}
152
 
 
153
 
osg::ref_ptr< osg::Vec4Array > WSPSliceBuilder::computeColorsFor( const osg::Vec3& pos ) const
154
 
{
155
 
    osg::ref_ptr< osg::Vec4Array > result( new osg::Vec4Array );
156
 
    result->reserve( m_probTracts.size() );
157
 
 
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 )
160
 
    {
161
 
        WColor c = lookUpColor( WPosition( pos ), tractID );
162
 
        if( c[3] != -1.0 )
163
 
        {
164
 
            result->push_back( c );
165
 
        }
166
 
    }
167
 
 
168
 
    return result;
169
 
}
170
 
 
171
 
void WSPSliceBuilder::computeSliceBB()
172
 
{
173
 
    if( !m_grid )
174
 
    {
175
 
        wlog::warn( "WSPSliceBuilder" ) << "Invalid grid while BB computation!";
176
 
        return;
177
 
    }
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() );
187
 
}