OpenWalnut  1.4.0
WRoiProjectFileIO.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <map>
00027 #include <vector>
00028 
00029 #include <boost/regex.hpp>
00030 
00031 #include "../common/WStringUtils.h"
00032 #include "../common/WProperties.h"
00033 #include "../common/WPropertyBase.h"
00034 #include "../common/WPropertyVariable.h"
00035 #include "../common/WPropertyTypes.h"
00036 #include "../common/exceptions/WFileNotFound.h"
00037 #include "../common/WLogger.h"
00038 #include "../graphicsEngine/WROI.h"
00039 #include "../graphicsEngine/WROIBox.h"
00040 #include "WROIManager.h"
00041 #include "WKernel.h"
00042 #include "WProjectFile.h"
00043 
00044 #include "WRoiProjectFileIO.h"
00045 
00046 WRoiProjectFileIO::WRoiProjectFileIO():
00047     WProjectFileIO()
00048 {
00049     // initialize members
00050 }
00051 
00052 WRoiProjectFileIO::~WRoiProjectFileIO()
00053 {
00054     // cleanup
00055 }
00056 
00057 WProjectFileIO::SPtr WRoiProjectFileIO::clone( WProjectFile* project ) const
00058 {
00059     // nothing special. Simply create new instance.
00060     WProjectFileIO::SPtr p( new WRoiProjectFileIO() );
00061     p->setProject( project );
00062     return p;
00063 }
00064 
00065 bool WRoiProjectFileIO::parse( std::string line, unsigned int lineNumber )
00066 {
00067     // this is the proper regular expression for modules
00068     static const boost::regex branchRe( "^ *SELECTOR_BRANCH:([0-9]*)$" );
00069     static const boost::regex roiBoxRe( "^ *SELECTOR_ROIBOX:([0-9]*):SELECTOR_BRANCH([0-9]*)$" );
00070     static const boost::regex branchPropRe( "^ *PROPERTY:\\(SELECTOR_BRANCH([0-9]*),(.*)\\)=(.*)$" );
00071     static const boost::regex roiBoxPropRe( "^ *PROPERTY:\\(SELECTOR_ROIBOX([0-9]*),(.*)\\)=(.*)$" );
00072 
00073     boost::smatch matches;  // the list of matches
00074     if( boost::regex_match( line, matches, branchRe ) )
00075     {
00076         Branch branch =  string_utils::fromString< Branch >( matches[1] );
00077         wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": Branch with ID " << branch;
00078 
00079         // store info
00080         m_branches.push_back( branch );
00081     }
00082     else if( boost::regex_match( line, matches, roiBoxRe ) )
00083     {
00084         Branch parentBranch = string_utils::fromString< Branch >( matches[2] );
00085         RoiID roiID = string_utils::fromString< RoiID >( matches[1] );
00086         wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": ROI with ID " << roiID << " in Branch " << parentBranch;
00087 
00088         // store info
00089         m_rois.push_back( Roi( roiID, parentBranch ) );
00090     }
00091     else if( boost::regex_match( line, matches, branchPropRe ) )
00092     {
00093         Branch branch = string_utils::fromString< Branch >( matches[1] );
00094         std::string prop = matches[2];
00095         std::string propValue = matches[3];
00096         wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": Property \"" << prop << "\" of Branch " << branch
00097                                                                           << " set to " << propValue;
00098         // store info
00099         m_branchProperties[ branch ].push_back( Property( prop, propValue ) );
00100     }
00101     else if( boost::regex_match( line, matches, roiBoxPropRe ) )
00102     {
00103         RoiID roiID = string_utils::fromString< RoiID >( matches[1] );
00104         std::string prop = matches[2];
00105         std::string propValue = matches[3];
00106         wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": Property \"" << prop << "\" of ROI " << roiID
00107                                                                           << " set to " << propValue;
00108         // store info
00109         m_roiProperties[ roiID ].push_back( Property( prop, propValue ) );
00110     }
00111     else
00112     {
00113         return false;
00114     }
00115 
00116     // read something
00117     return true;
00118 }
00119 
00120 void WRoiProjectFileIO::done()
00121 {
00122     boost::shared_ptr< WROIManager> rm = WKernel::getRunningKernel()->getRoiManager();
00123 
00124     std::map< Branch, boost::shared_ptr< WRMBranch > > branches;
00125     // add all branches
00126     for( std::vector< Branch >::const_iterator i = m_branches.begin(); i != m_branches.end(); ++i )
00127     {
00128         boost::shared_ptr< WRMBranch > branch = rm->addBranch();
00129         branches[ *i ] = branch;
00130 
00131         // set the properties
00132         for( Properties::const_iterator propI = m_branchProperties[ *i ].begin(); propI != m_branchProperties[ *i ].end();
00133              ++propI )
00134         {
00135             std::string name = ( *propI ).get< 0 >();
00136             std::string value = ( *propI ).get< 1 >();
00137 
00138             WPropertyBase::SPtr prop = branch->getProperties()->findProperty( name );
00139             if( prop )
00140             {
00141                 prop->setAsString( value );
00142             }
00143             else
00144             {
00145                 addError( "The ROI does not have a property named \"" + name + "\". Skipping." );
00146             }
00147         }
00148     }
00149 
00150     // add ROIs to the branches
00151     for( std::vector< Roi >::const_iterator i = m_rois.begin(); i != m_rois.end(); ++i )
00152     {
00153         // get branch of this ROI
00154         boost::shared_ptr< WRMBranch > branch = branches[ ( *i ).get< 1 >() ];
00155 
00156         // add ROI to branch
00157         osg::ref_ptr< WROI > roi( new WROIBox( WPosition(), WPosition() ) );
00158         rm->addRoi( roi, branch );
00159 
00160         // set the properties
00161         for( Properties::const_iterator propI = m_roiProperties[ ( *i ).get< 0 >() ].begin(); propI != m_roiProperties[ ( *i ).get< 0 >() ].end();
00162              ++propI )
00163         {
00164             std::string name = ( *propI ).get< 0 >();
00165             std::string value = ( *propI ).get< 1 >();
00166 
00167             WPropertyBase::SPtr prop = roi->getProperties()->findProperty( name );
00168             if( prop )
00169             {
00170                 prop->setAsString( value );
00171             }
00172             else
00173             {
00174                 addError( "The ROI does not have a property named \"" + name + "\". Skipping." );
00175             }
00176         }
00177     }
00178 
00179     // clear everything
00180     m_branches.clear();
00181     m_rois.clear();
00182     m_roiProperties.clear();
00183     m_branchProperties.clear();
00184 }
00185 
00186 void WRoiProjectFileIO::save( std::ostream& output )   // NOLINT
00187 {
00188     // save here
00189     output << "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////" << std::endl <<
00190               "// ROI Structure" << std::endl <<
00191               "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////" << std::endl <<
00192               std::endl;
00193 
00194     // write all branches
00195     WROIManager::Branches branches =WKernel::getRunningKernel()->getRoiManager()->getBranches();
00196     unsigned int branchIndex = 0;
00197     unsigned int roiIndex = 0;
00198     for( WROIManager::Branches::const_iterator branchIter = branches.begin(); branchIter != branches.end(); ++branchIter )
00199     {
00200         // get the branch
00201         WRMBranch::SPtr branch = *branchIter;
00202 
00203         // write this branch's properties
00204         output << "SELECTOR_BRANCH:" << branchIndex << std::endl;
00205         printProperties( output, branch->getProperties(), "", "", branchIndex, "SELECTOR_BRANCH" );
00206         output << std::endl;
00207 
00208         // handle this branch's ROIs
00209         WROIManager::ROIs rois = branch->getRois();
00210         for( WROIManager::ROIs::const_iterator roiIter = rois.begin(); roiIter != rois.end(); ++roiIter )
00211         {
00212             // differentiate the type of roi, currently we will support only WROiBox
00213             WROIBox* roiBox = dynamic_cast< WROIBox* >( ( *roiIter ).get() );
00214 
00215             output << "SELECTOR_ROIBOX:" << roiIndex << ":SELECTOR_BRANCH" << branchIndex << std::endl;
00216             printProperties( output, roiBox->getProperties(), "", "", roiIndex, "SELECTOR_ROIBOX" );
00217             output << std::endl;
00218 
00219             // done
00220             roiIndex++;
00221         }
00222 
00223         // done
00224         branchIndex++;
00225     }
00226 }
00227