WModuleFactory.cpp 9.58 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 <set>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
26
#include <string>
27 28
#include <iostream>
#include <typeinfo>
29 30

#include "../common/WLogger.h"
31
#include "../modules/template/WMTemplate.h"
32
#include "../modules/data/WMData.h" // this is the ONLY module with a special meaning.
33
#include "../modules/coordinateSystem/WMCoordinateSystem.h"
34
#include "../modules/boundingBox/WMBoundingBox.h"
35
#include "../modules/distanceMap/WMDistanceMapIsosurface.h"
36
#include "../modules/distanceMap/WMDistanceMap.h"
37
#include "../modules/connectomeView/WMConnectomeView.h"
38
#include "../modules/eegView/WMEEGView.h"
39 40
#include "../modules/fiberClustering/WMFiberClustering.h"
#include "../modules/fiberCulling/WMFiberCulling.h"
Mathias Goldau's avatar
Mathias Goldau committed
41
#include "../modules/fiberDisplay/WMFiberDisplay.h"
42
#include "../modules/gaussFiltering/WMGaussFiltering.h"
43 44
#include "../modules/hud/WMHud.h"
#include "../modules/marchingCubes/WMMarchingCubes.h"
45
#include "../modules/directVolumeRendering/WMDirectVolumeRendering.h"
46
#include "../modules/navSlices/WMNavSlices.h"
47
#include "../modules/prototypeBoxManipulation/WMPrototypeBoxManipulation.h"
48
#include "../modules/voxelizer/WMVoxelizer.h"
49
#include "../modules/writeNIfTI/WMWriteNIfTI.h"
50
#include "WModuleFactory.h"
51 52
#include "exceptions/WPrototypeNotUnique.h"
#include "exceptions/WPrototypeUnknown.h"
53 54 55 56 57 58 59 60 61 62 63 64 65 66

// factory instance as singleton
boost::shared_ptr< WModuleFactory > WModuleFactory::m_instance = boost::shared_ptr< WModuleFactory >();

WModuleFactory::WModuleFactory()
{
    // initialize members
}

WModuleFactory::~WModuleFactory()
{
    // cleanup
}

67 68 69 70 71 72 73 74 75
void WModuleFactory::load()
{
    // load modules
    WLogger::getLogger()->addLogMessage( "Loading Modules", "ModuleFactory", LL_INFO );

    // operation must be exclusive
    boost::unique_lock< boost::shared_mutex > lock = boost::unique_lock< boost::shared_mutex >( m_prototypesLock );

    // currently the prototypes are added by hand. This will be done automatically later.
76
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMTemplate() ) );
77
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMBoundingBox() ) );
78
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMData() ) );
79
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMNavSlices() ) );
Mathias Goldau's avatar
Mathias Goldau committed
80
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMFiberDisplay() ) );
81 82 83 84
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMFiberCulling() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMFiberClustering() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMCoordinateSystem() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMMarchingCubes() ) );
85
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMDistanceMapIsosurface() ) );
86
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMDistanceMap() ) );
87
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMGaussFiltering() ) );
schurade's avatar
schurade committed
88
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMHud() ) );
89
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMEEGView() ) );
90
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMPrototypeBoxManipulation() ) );
91
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMVoxelizer() ) );
92
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMDirectVolumeRendering() ) );
93
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMWriteNIfTI() ) );
94
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMConnectomeView() ) );
95 96 97 98 99 100 101

    lock.unlock();

    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

    // initialize every module in the set
102 103 104
    std::set< std::string > names;  // helper to find duplicates
    for( std::set< boost::shared_ptr< WModule > >::iterator listIter = m_prototypes.begin(); listIter != m_prototypes.end();
            ++listIter )
105
    {
106
        WLogger::getLogger()->addLogMessage( "Loading module: \"" + ( *listIter )->getName() + "\"", "ModuleFactory", LL_INFO );
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
107

108 109 110
        // that should not happen. Names should not occur multiple times since they are unique
        if ( names.count( ( *listIter )->getName() ) )
        {
111
            throw WPrototypeNotUnique( "Module \"" + ( *listIter )->getName() + "\" is not unique. Modules have to have a unique name." );
112 113 114
        }
        names.insert( ( *listIter )->getName() );

115
        initializeModule( ( *listIter ) );
116 117 118 119 120
    }

    slock.unlock();
}

121 122
boost::shared_ptr< WModule > WModuleFactory::create( boost::shared_ptr< WModule > prototype )
{
123 124 125 126 127 128
    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

    // ensure this one is a prototype and nothing else
    if ( m_prototypes.count( prototype ) == 0 )
    {
129
        throw WPrototypeUnknown( "Could not clone module \"" + prototype->getName() + "\" since it is no prototype." );
130 131 132 133
    }

    slock.unlock();

134 135
    // call prototypes factory function
    boost::shared_ptr< WModule > clone = boost::shared_ptr< WModule >( prototype->factory() );
136
    initializeModule( clone );
137 138 139 140

    return clone;
}

141 142 143 144 145
void WModuleFactory::initializeModule( boost::shared_ptr< WModule > module )
{
    module->initialize();
}

146 147 148 149 150 151 152 153 154 155
boost::shared_ptr< WModuleFactory > WModuleFactory::getModuleFactory()
{
    if ( !m_instance )
    {
        m_instance = boost::shared_ptr< WModuleFactory >( new WModuleFactory() );
    }

    return m_instance;
}

156

157
const boost::shared_ptr< WModule > WModuleFactory::isPrototypeAvailable( std::string name )
158 159 160 161
{
    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
162
    // find first and only prototype (ensured during load())
163 164 165 166 167 168 169 170 171 172 173
    boost::shared_ptr< WModule > ret = boost::shared_ptr< WModule >();
    for( std::set< boost::shared_ptr< WModule > >::iterator listIter = m_prototypes.begin(); listIter != m_prototypes.end();
            ++listIter )
    {
        if ( ( *listIter )->getName() == name )
        {
            ret = ( *listIter );
            break;
        }
    }

Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
174
    slock.unlock();
175

176 177 178 179 180 181 182
    return ret;
}

const boost::shared_ptr< WModule > WModuleFactory::getPrototypeByName( std::string name )
{
    boost::shared_ptr< WModule > ret = isPrototypeAvailable( name );

183 184 185
    // if not found -> throw
    if ( ret == boost::shared_ptr< WModule >() )
    {
186
        throw WPrototypeUnknown( "Could not find prototype \"" + name + "\"." );
187 188 189 190 191
    }

    return ret;
}

192
const boost::shared_ptr< WModule > WModuleFactory::getPrototypeByInstance( boost::shared_ptr< WModule > instance )
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
    return getPrototypeByName( instance->getName() );
}

std::set< boost::shared_ptr< WModule > > WModuleFactory::getCompatiblePrototypes( boost::shared_ptr< WModule > module )
{
    std::set< boost::shared_ptr < WModule > > compatibles;

    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

    // get offered outputs
    std::set<boost::shared_ptr<WModuleOutputConnector> > cons = module->getOutputConnectors();
    if ( cons.size() == 0 )
    {
        // in this case return the empty list
        return compatibles;
    }
    // TODO(ebaum): see ticket #178 for this
    if ( cons.size() > 1 )
    {
        WLogger::getLogger()->addLogMessage( "Can not find compatibles for modules with more than 1 output connector. Using "
                + ( *cons.begin() )->getCanonicalName() + " for compatibility check.", "ModuleFactory", LL_WARNING );
    }

    // go through every prototype
    for( std::set< boost::shared_ptr< WModule > >::iterator listIter = m_prototypes.begin(); listIter != m_prototypes.end();
            ++listIter )
    {
        // get connectors of this prototype
        std::set<boost::shared_ptr<WModuleInputConnector> > pcons = ( *listIter )->getInputConnectors();

        // ensure we have 1 connector
        if ( pcons.size() == 0 )
        {
            continue;
        }
        if ( pcons.size() > 1 )
        {
            WLogger::getLogger()->addLogMessage( "Can not find compatibles for modules with more than 1 input connector. Using "
                    + ( *pcons.begin() )->getCanonicalName() + " for compatibility check.", "ModuleFactory", LL_WARNING );
        }

        // check whether the outputs are compatible with the inputs of the prototypes
237
        if (   ( *cons.begin() )->connectable( *pcons.begin() )  &&  ( *pcons.begin() )->connectable( *cons.begin() ) )
238 239 240 241 242 243 244
        {
            // it is compatible -> add to list
            compatibles.insert( *listIter );
        }
    }

    slock.unlock();
245

246
    return compatibles;
247 248
}