OpenWalnut  1.5.0dev
WRoiProjectFileIO.cpp
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 <map>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include <boost/regex.hpp>
31 
32 #include "../common/WLogger.h"
33 #include "../common/WProperties.h"
34 #include "../common/WPropertyBase.h"
35 #include "../common/WPropertyTypes.h"
36 #include "../common/WPropertyVariable.h"
37 #include "../common/WStringUtils.h"
38 #include "../common/exceptions/WFileNotFound.h"
39 #include "../graphicsEngine/WROI.h"
40 #include "../graphicsEngine/WROIBox.h"
41 #include "WKernel.h"
42 #include "WProjectFile.h"
43 #include "WROIManager.h"
44 #include "WRoiProjectFileIO.h"
45 
48 {
49  // initialize members
50 }
51 
53 {
54  // cleanup
55 }
56 
58 {
59  // nothing special. Simply create new instance.
61  p->setProject( project );
62  return p;
63 }
64 
65 bool WRoiProjectFileIO::parse( std::string line, unsigned int lineNumber )
66 {
67  // this is the proper regular expression for modules
68  static const boost::regex branchRe( "^ *SELECTOR_BRANCH:([0-9]*)$" );
69  static const boost::regex roiBoxRe( "^ *SELECTOR_ROIBOX:([0-9]*):SELECTOR_BRANCH([0-9]*)$" );
70  static const boost::regex branchPropRe( "^ *PROPERTY:\\(SELECTOR_BRANCH([0-9]*),(.*)\\)=(.*)$" );
71  static const boost::regex roiBoxPropRe( "^ *PROPERTY:\\(SELECTOR_ROIBOX([0-9]*),(.*)\\)=(.*)$" );
72 
73  boost::smatch matches; // the list of matches
74  if( boost::regex_match( line, matches, branchRe ) )
75  {
76  Branch branch = string_utils::fromString< Branch >( matches[1] );
77  wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": Branch with ID " << branch;
78 
79  // store info
80  m_branches.push_back( branch );
81  }
82  else if( boost::regex_match( line, matches, roiBoxRe ) )
83  {
84  Branch parentBranch = string_utils::fromString< Branch >( matches[2] );
85  RoiID roiID = string_utils::fromString< RoiID >( matches[1] );
86  wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": ROI with ID " << roiID << " in Branch " << parentBranch;
87 
88  // store info
89  m_rois.push_back( Roi( roiID, parentBranch ) );
90  }
91  else if( boost::regex_match( line, matches, branchPropRe ) )
92  {
93  Branch branch = string_utils::fromString< Branch >( matches[1] );
94  std::string prop = matches[2];
95  std::string propValue = matches[3];
96  wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": Property \"" << prop << "\" of Branch " << branch
97  << " set to " << propValue;
98  // store info
99  m_branchProperties[ branch ].push_back( Property( prop, propValue ) );
100  }
101  else if( boost::regex_match( line, matches, roiBoxPropRe ) )
102  {
103  RoiID roiID = string_utils::fromString< RoiID >( matches[1] );
104  std::string prop = matches[2];
105  std::string propValue = matches[3];
106  wlog::debug( "Project Loader [Parser]" ) << "Line " << lineNumber << ": Property \"" << prop << "\" of ROI " << roiID
107  << " set to " << propValue;
108  // store info
109  m_roiProperties[ roiID ].push_back( Property( prop, propValue ) );
110  }
111  else
112  {
113  return false;
114  }
115 
116  // read something
117  return true;
118 }
119 
121 {
122  std::shared_ptr< WROIManager> rm = WKernel::getRunningKernel()->getRoiManager();
123 
124  std::map< Branch, std::shared_ptr< WRMBranch > > branches;
125  // add all branches
126  for( std::vector< Branch >::const_iterator i = m_branches.begin(); i != m_branches.end(); ++i )
127  {
128  std::shared_ptr< WRMBranch > branch = rm->addBranch();
129  branches[ *i ] = branch;
130 
131  // set the properties
132  for( Properties::const_iterator propI = m_branchProperties[ *i ].begin(); propI != m_branchProperties[ *i ].end();
133  ++propI )
134  {
135  std::string name = ( *propI ).get< 0 >();
136  std::string value = ( *propI ).get< 1 >();
137 
138  WPropertyBase::SPtr prop = branch->getProperties()->findProperty( name );
139  if( prop )
140  {
141  prop->setAsString( value );
142  }
143  else
144  {
145  addError( "The ROI does not have a property named \"" + name + "\". Skipping." );
146  }
147  }
148  }
149 
150  // add ROIs to the branches
151  for( std::vector< Roi >::const_iterator i = m_rois.begin(); i != m_rois.end(); ++i )
152  {
153  // get branch of this ROI
154  std::shared_ptr< WRMBranch > branch = branches[ ( *i ).get< 1 >() ];
155 
156  // add ROI to branch
157  osg::ref_ptr< WROI > roi( new WROIBox( WPosition(), WPosition() ) );
158  rm->addRoi( roi, branch );
159 
160  // set the properties
161  for( Properties::const_iterator propI = m_roiProperties[ ( *i ).get< 0 >() ].begin(); propI != m_roiProperties[ ( *i ).get< 0 >() ].end();
162  ++propI )
163  {
164  std::string name = ( *propI ).get< 0 >();
165  std::string value = ( *propI ).get< 1 >();
166 
167  WPropertyBase::SPtr prop = roi->getProperties()->findProperty( name );
168  if( prop )
169  {
170  prop->setAsString( value );
171  }
172  else
173  {
174  addError( "The ROI does not have a property named \"" + name + "\". Skipping." );
175  }
176  }
177  }
178 
179  // clear everything
180  m_branches.clear();
181  m_rois.clear();
182  m_roiProperties.clear();
183  m_branchProperties.clear();
184 }
185 
186 void WRoiProjectFileIO::save( std::ostream& output ) // NOLINT
187 {
188  // save here
189  output << "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////" << std::endl <<
190  "// ROI Structure" << std::endl <<
191  "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////" << std::endl <<
192  std::endl;
193 
194  // write all branches
195  WROIManager::Branches branches =WKernel::getRunningKernel()->getRoiManager()->getBranches();
196  unsigned int branchIndex = 0;
197  unsigned int roiIndex = 0;
198  for( WROIManager::Branches::const_iterator branchIter = branches.begin(); branchIter != branches.end(); ++branchIter )
199  {
200  // get the branch
201  WRMBranch::SPtr branch = *branchIter;
202 
203  // write this branch's properties
204  output << "SELECTOR_BRANCH:" << branchIndex << std::endl;
205  printProperties( output, branch->getProperties(), "", "", branchIndex, "SELECTOR_BRANCH" );
206  output << std::endl;
207 
208  // handle this branch's ROIs
209  WROIManager::ROIs rois = branch->getRois();
210  for( WROIManager::ROIs::const_iterator roiIter = rois.begin(); roiIter != rois.end(); ++roiIter )
211  {
212  // differentiate the type of roi, currently we will support only WROiBox
213  WROIBox* roiBox = dynamic_cast< WROIBox* >( ( *roiIter ).get() );
214 
215  output << "SELECTOR_ROIBOX:" << roiIndex << ":SELECTOR_BRANCH" << branchIndex << std::endl;
216  printProperties( output, roiBox->getProperties(), "", "", roiIndex, "SELECTOR_ROIBOX" );
217  output << std::endl;
218 
219  // done
220  roiIndex++;
221  }
222 
223  // done
224  branchIndex++;
225  }
226 }
227 
static WKernel * getRunningKernel()
Returns pointer to the currently running kernel.
Definition: WKernel.cpp:117
std::shared_ptr< WROIManager > getRoiManager()
get for roi manager
Definition: WKernel.cpp:209
This only is a 3d double vector.
A base class for all parts of OpenWalnut which can be serialized to a project file.
std::shared_ptr< WProjectFileIO > SPtr
Abbreviation for a shared pointer.
void addError(std::string description)
Add an error.
void printProperties(std::ostream &output, std::shared_ptr< WProperties > props, std::string indent, std::string prefix, unsigned int index, std::string indexPrefix="")
Recursively prints the properties and nested properties.
Class loading project files.
Definition: WProjectFile.h:50
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
std::shared_ptr< WRMBranch > SPtr
Convenience type for a shared pointer of this type.
Definition: WRMBranch.h:49
A box representing a region of interest.
Definition: WROIBox.h:49
std::vector< std::shared_ptr< WRMBranch > > Branches
Branches list.
Definition: WROIManager.h:194
std::vector< osg::ref_ptr< WROI > > ROIs
ROI list.
Definition: WROIManager.h:183
std::shared_ptr< WProperties > getProperties()
Getter.
Definition: WROI.cpp:92
unsigned int Branch
Branch by ID.
WRoiProjectFileIO()
Default constructor.
virtual void save(std::ostream &output)
Saves the state to the specified stream.
virtual SPtr clone(WProjectFile *project) const
Create a clone of the IO.
boost::tuple< std::string, std::string > Property
Property for branch/roi with ID.
std::map< Branch, Properties > m_branchProperties
Properties of each branch.
boost::tuple< RoiID, Branch > Roi
ROI by ID, second is parent branch ID.
virtual bool parse(std::string line, unsigned int lineNumber)
This method parses the specified line and interprets it.
virtual ~WRoiProjectFileIO()
Destructor.
unsigned int RoiID
ID of a ROI.
virtual void done()
Called whenever the end of the project file has been reached.
std::vector< Roi > m_rois
All loaded rois.
std::vector< Branch > m_branches
All loaded branch IDs.
std::map< RoiID, Properties > m_roiProperties
Properties of each branch.
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331