OpenWalnut  1.4.0
WSelectorRoi.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 <vector>
26 
27 #include "../graphicsEngine/WROIBox.h"
28 #include "../graphicsEngine/WROIArbitrary.h"
29 #include "WKdTree.h"
30 
31 #include "WSelectorRoi.h"
32 
33 
34 WSelectorRoi::WSelectorRoi( osg::ref_ptr< WROI > roi, boost::shared_ptr< const WDataSetFibers > fibers, boost::shared_ptr< WKdTree> kdTree ) :
35  m_roi( roi ),
36  m_fibers( fibers ),
37  m_kdTree( kdTree ),
38  m_size( fibers->size() ),
39  m_dirty( true )
40 {
41  m_bitField = boost::shared_ptr< std::vector<bool> >( new std::vector<bool>( m_size, false ) );
42 
43  m_currentArray = m_fibers->getVertices();
44  m_currentReverse = m_fibers->getVerticesReverse();
45 
47  = boost::shared_ptr< boost::function< void() > >( new boost::function< void() >( boost::bind( &WSelectorRoi::setDirty, this ) ) );
48  m_roi->addROIChangeNotifier( m_changeRoiSignal );
49 }
50 
52 {
53  m_roi->removeROIChangeNotifier( m_changeRoiSignal );
54 }
55 
57 {
58  m_dirty = true;
59 }
60 
62 {
63  m_workerBitfield = boost::shared_ptr< std::vector< bool > >( new std::vector< bool >( m_size, false ) );
64 
65  if( osg::dynamic_pointer_cast<WROIBox>( m_roi ).get() )
66  {
67  m_boxMin.resize( 3 );
68  m_boxMax.resize( 3 );
69 
70  osg::ref_ptr<WROIBox> box = osg::dynamic_pointer_cast<WROIBox>( m_roi );
71 
72  m_boxMin[0] = box->getMinPos()[0];
73  m_boxMax[0] = box->getMaxPos()[0];
74  m_boxMin[1] = box->getMinPos()[1];
75  m_boxMax[1] = box->getMaxPos()[1];
76  m_boxMin[2] = box->getMinPos()[2];
77  m_boxMax[2] = box->getMaxPos()[2];
78 
79  boxTest( 0, m_currentArray->size() / 3 - 1, 0 );
80  }
81 
82  if( osg::dynamic_pointer_cast<WROIArbitrary>( m_roi ).get() )
83  {
84  osg::ref_ptr<WROIArbitrary>roi = osg::dynamic_pointer_cast<WROIArbitrary>( m_roi );
85 
86  float threshold = static_cast<float>( roi->getThreshold() );
87 
88  size_t nx = roi->getCoordDimensions()[0];
89  size_t ny = roi->getCoordDimensions()[1];
90 
91  double dx = roi->getCoordOffsets()[0];
92  double dy = roi->getCoordOffsets()[1];
93  double dz = roi->getCoordOffsets()[2];
94 
95  for( size_t i = 0; i < m_currentArray->size()/3; ++i )
96  {
97  size_t x = static_cast<size_t>( ( *m_currentArray )[i * 3 ] / dx );
98  size_t y = static_cast<size_t>( ( *m_currentArray )[i * 3 + 1] / dy );
99  size_t z = static_cast<size_t>( ( *m_currentArray )[i * 3 + 2] / dz );
100  int index = x + y * nx + z * nx * ny;
101 
102  if( static_cast<float>( roi->getValue( index ) ) - threshold > 0.1 )
103  {
104  ( *m_workerBitfield )[getLineForPoint( i )] = 1;
105  }
106  }
107  }
108  m_dirty = false;
110 }
111 
112 void WSelectorRoi::boxTest( int left, int right, int axis )
113 {
114  // abort condition
115  if( left > right )
116  return;
117 
118  int root = left + ( ( right - left ) / 2 );
119  int axis1 = ( axis + 1 ) % 3;
120  int pointIndex = m_kdTree->m_tree[root] * 3;
121 
122  if( ( *m_currentArray )[pointIndex + axis] < m_boxMin[axis] )
123  {
124  boxTest( root + 1, right, axis1 );
125  }
126  else if( ( *m_currentArray )[pointIndex + axis] > m_boxMax[axis] )
127  {
128  boxTest( left, root - 1, axis1 );
129  }
130  else
131  {
132  int axis2 = ( axis + 2 ) % 3;
133  if( ( *m_currentArray )[pointIndex + axis1] <= m_boxMax[axis1] && ( *m_currentArray )[pointIndex + axis1]
134  >= m_boxMin[axis1] && ( *m_currentArray )[pointIndex + axis2] <= m_boxMax[axis2]
135  && ( *m_currentArray )[pointIndex + axis2] >= m_boxMin[axis2] )
136  {
137  ( *m_workerBitfield )[getLineForPoint( m_kdTree->m_tree[root] )] = 1;
138  }
139  boxTest( left, root - 1, axis1 );
140  boxTest( root + 1, right, axis1 );
141  }
142 }
void boxTest(int left, int right, int axis)
recursive function to check for intersections with the roi
~WSelectorRoi()
destructor
osg::ref_ptr< WROI > m_roi
pointer to the roi
Definition: WSelectorRoi.h:101
void setDirty()
setter sets the dirty flag
boost::shared_ptr< std::vector< float > > m_currentArray
pointer to the array that is used for updating this is used for the recurse update function...
Definition: WSelectorRoi.h:137
void recalculate()
updates the output bitfiel when something with the rois has changed
boost::shared_ptr< std::vector< bool > > m_bitField
the bitfield that is given to the outside world
Definition: WSelectorRoi.h:126
boost::shared_ptr< WKdTree > m_kdTree
Stores a pointer to the kdTree used for fiber selection.
Definition: WSelectorRoi.h:111
size_t m_size
size of the fiber dataset, stored for convinience
Definition: WSelectorRoi.h:116
A box representing a region of interest.
Definition: WROIBox.h:47
size_t getLineForPoint(size_t point)
getter
Definition: WSelectorRoi.h:160
boost::shared_ptr< const WDataSetFibers > m_fibers
Pointer to the fiber data set.
Definition: WSelectorRoi.h:106
A box containing information on an arbitrarily shaped a region of interest.
Definition: WROIArbitrary.h:51
std::vector< float > m_boxMin
lower boundary of the box, used for boxtest
Definition: WSelectorRoi.h:145
std::vector< float > m_boxMax
upper boundary of the box, used for boxtest
Definition: WSelectorRoi.h:146
boost::shared_ptr< boost::function< void() > > m_changeRoiSignal
Signal that can be used to update the selector ROI.
Definition: WSelectorRoi.h:148
boost::shared_ptr< std::vector< size_t > > m_currentReverse
pointer to the reverse array that is used for updating this is used for the recurse update function...
Definition: WSelectorRoi.h:143
boost::shared_ptr< std::vector< bool > > m_workerBitfield
the bitfield we work on
Definition: WSelectorRoi.h:131
bool m_dirty
dirty flag
Definition: WSelectorRoi.h:121
std::vector< size_t > getCoordDimensions()
Get the number of vertices in the three coordinate directions.
WSelectorRoi(osg::ref_ptr< WROI > roi, boost::shared_ptr< const WDataSetFibers > fibers, boost::shared_ptr< WKdTree > kdTree)
constructor