OpenWalnut  1.5.0dev
WMVectorPlot.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 <algorithm>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include <osg/Geode>
31 #include <osg/Geometry>
32 #include <osg/Group>
33 #include <osg/Material>
34 #include <osg/ShapeDrawable>
35 #include <osg/StateAttribute>
36 
37 #include "WMVectorPlot.h"
38 #include "WMVectorPlot.xpm"
39 #include "core/common/WColor.h"
40 #include "core/common/exceptions/WTypeMismatch.h"
41 #include "core/dataHandler/WDataHandlerEnums.h"
42 #include "core/kernel/WKernel.h"
43 #include "core/kernel/WSelectionManager.h"
44 
45 // This line is needed by the module loader to actually find your module.
46 W_LOADABLE_MODULE( WMVectorPlot )
47 
49  WModule()
50 {
51 }
52 
54 {
55  // Cleanup!
56 }
57 
58 std::shared_ptr< WModule > WMVectorPlot::factory() const
59 {
60  return std::shared_ptr< WModule >( new WMVectorPlot() );
61 }
62 
63 const char** WMVectorPlot::getXPMIcon() const
64 {
65  return vectorplot_xpm;
66 }
67 
68 const std::string WMVectorPlot::getName() const
69 {
70  return "Vector Plot";
71 }
72 
73 const std::string WMVectorPlot::getDescription() const
74 {
75  return "This module displays vector data as small vector representations on navigation slices and other surfaces.";
76 }
77 
79 {
80  m_input = std::shared_ptr< WModuleInputData < WDataSetVector > >(
81  new WModuleInputData< WDataSetVector >( shared_from_this(), "in", "The dataset to display" )
82  );
83 
85 
86  // call WModules initialization
88 }
89 
91 {
92  m_xSlice = m_properties->addProperty( "X Pos of the slice", "Description.", 80. );
93  m_ySlice = m_properties->addProperty( "Y Pos of the slice", "Description.", 100. );
94  m_zSlice = m_properties->addProperty( "Z Pos of the slice", "Description.", 80. );
95 
96  // NOTE: min/max of these props are set vy buildPlotSlices
97  m_xSlice->setHidden( true );
98  m_ySlice->setHidden( true );
99  m_zSlice->setHidden( true );
100 
101  m_projectOnSlice = m_properties->addProperty( "Projection",
102  "If active, the vectors are projected into the surface "
103  "used to place them. Thus their "
104  "representation is tangential to the surface.",
105  false );
106  m_coloringMode = m_properties->addProperty( "Direction coloring",
107  "Draw each vector in a color indicating its direction. ", false );
108  m_aColor = m_properties->addProperty( "Color",
109  "This color is used if direction coloring is deactivated.",
110  WColor( 1.0, 0.0, 0.0, 1.0 ) );
111 
112  m_showOnSagittal = m_properties->addProperty( "Show sagittal", "Show vectors on sagittal slice.", true );
113  m_showOnCoronal = m_properties->addProperty( "Show coronal", "Show vectors on coronal slice.", true );
114  m_showOnAxial = m_properties->addProperty( "Show axial", "Show vectors on axial slice.", true );
115 
116  m_scale = m_properties->addProperty( "Scaling",
117  "Scaling the length of the plotted vectors",
118  1.0 );
119 
121 }
122 
124 {
125  m_moduleState.setResetable( true, true );
126  m_moduleState.add( m_input->getDataChangedCondition() );
127 
128  ready();
129 
130  while( !m_shutdownFlag() )
131  {
133 
134  if( m_shutdownFlag() )
135  {
136  break;
137  }
138 
139  std::shared_ptr< WProgress > progress( new WProgress( "Vector Plot", 2 ) );
140  m_progress->addSubProgress( progress );
141 
142  std::shared_ptr< WDataSetVector > newDataSet = m_input->getData();
143  bool dataChanged = ( m_dataSet != newDataSet );
144  bool dataValid = ( newDataSet != NULL );
145 
146  if( dataChanged && dataValid )
147  {
148  m_dataSet = newDataSet;
149  }
150 
151  if( dataValid && dataChanged )
152  {
153  debugLog() << "Building Vector Plot";
154 
155  if( m_rootNode )
156  {
157  WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->remove( m_rootNode );
158  }
159 
160  osg::ref_ptr< osg::Geode > newRootNode = new osg::Geode();
161  newRootNode->addDrawable( buildPlotSlices() );
162 
163  ++*progress;
164 
165  m_rootNode = newRootNode;
166  m_rootNode->setNodeMask( m_active->get() ? 0xFFFFFFFF : 0x0 );
167  m_rootNode->addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WMVectorPlot::updateCallback, this ) ) );
168 
169  // no light for lines
170  m_rootNode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
171 
172  WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->insert( m_rootNode );
173  }
174  progress->finish();
175  }
176 
177  WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->remove( m_rootNode );
178 }
179 
180 osg::ref_ptr<osg::Geometry> WMVectorPlot::buildPlotSlices()
181 {
182  switch( ( *m_dataSet ).getValueSet()->getDataType() )
183  {
184  case W_DT_SIGNED_INT:
185  return buildPlotSlices< DataTypeRT< W_DT_SIGNED_INT >::type >();
186  case W_DT_FLOAT:
187  return buildPlotSlices< DataTypeRT< W_DT_FLOAT >::type >();
188  case W_DT_DOUBLE:
189  return buildPlotSlices< DataTypeRT< W_DT_DOUBLE >::type >();
190  case W_DT_INT8:
191  return buildPlotSlices< DataTypeRT< W_DT_INT8 >::type >();
192  case W_DT_UINT16:
193  return buildPlotSlices< DataTypeRT< W_DT_UINT16 >::type >();
194  case W_DT_UINT8:
195  return buildPlotSlices< DataTypeRT< W_DT_UINT8 >::type >();
196  case W_DT_INT16:
197  return buildPlotSlices< DataTypeRT< W_DT_INT16 >::type >();
198  case W_DT_UINT32:
199  return buildPlotSlices< DataTypeRT< W_DT_UINT32 >::type >();
200  case W_DT_INT64:
201  return buildPlotSlices< DataTypeRT< W_DT_INT64 >::type >();
202  case W_DT_UINT64:
203  return buildPlotSlices< DataTypeRT< W_DT_UINT64 >::type >();
204  default:
205  throw WTypeMismatch( "Unknown valueset type." );
206  break;
207  }
208 }
209 
211 {
212  WPosition current = WKernel::getRunningKernel()->getSelectionManager()->getCrosshair()->getPosition();
213 
214  if( ( m_oldPos != current ) || m_coloringMode->changed() || m_aColor->changed() || m_projectOnSlice->changed() ||
215  m_showOnSagittal->changed() || m_showOnCoronal->changed() || m_showOnAxial->changed() || m_scale->changed() )
216  {
217  m_oldPos = current; // for next run
218  osg::ref_ptr<osg::Drawable> old = osg::ref_ptr<osg::Drawable>( m_rootNode->getDrawable( 0 ) );
219  m_rootNode->removeDrawable( old );
220  m_rootNode->addDrawable( buildPlotSlices() );
221  }
222 }
223 
225 {
226  if( m_rootNode ) // always ensure the root node exists
227  {
228  if( m_active->get() )
229  {
230  m_rootNode->setNodeMask( 0xFFFFFFFF );
231  }
232  else
233  {
234  m_rootNode->setNodeMask( 0x0 );
235  }
236  }
237 
238  // Always call WModule's activate!
240 }
241 
242 void WMVectorPlot::transformVerts( osg::ref_ptr< osg::Vec3Array > verts )
243 {
244  WMatrix< double > mat = std::dynamic_pointer_cast< WGridRegular3D >( m_dataSet->getGrid() )->getTransformationMatrix();
245 
246  for( size_t i = 0; i < verts->size(); ++i )
247  {
248  std::vector< double > resultPos4D( 4 );
249  resultPos4D[0] = mat( 0, 0 ) * ( *verts )[i][0] + mat( 0, 1 ) * ( *verts )[i][1] + mat( 0, 2 ) * ( *verts )[i][2] + mat( 0, 3 ) * 1;
250  resultPos4D[1] = mat( 1, 0 ) * ( *verts )[i][0] + mat( 1, 1 ) * ( *verts )[i][1] + mat( 1, 2 ) * ( *verts )[i][2] + mat( 1, 3 ) * 1;
251  resultPos4D[2] = mat( 2, 0 ) * ( *verts )[i][0] + mat( 2, 1 ) * ( *verts )[i][1] + mat( 2, 2 ) * ( *verts )[i][2] + mat( 2, 3 ) * 1;
252  resultPos4D[3] = mat( 3, 0 ) * ( *verts )[i][0] + mat( 3, 1 ) * ( *verts )[i][1] + mat( 3, 2 ) * ( *verts )[i][2] + mat( 3, 3 ) * 1;
253 
254  ( *verts )[i][0] = resultPos4D[0] / resultPos4D[3];
255  ( *verts )[i][1] = resultPos4D[1] / resultPos4D[3];
256  ( *verts )[i][2] = resultPos4D[2] / resultPos4D[3];
257  }
258 }
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
This callback allows you a simple usage of callbacks in your module.
std::shared_ptr< WSelectionManager > getSelectionManager()
get for selection manager
Definition: WKernel.cpp:214
static WKernel * getRunningKernel()
Returns pointer to the currently running kernel.
Definition: WKernel.cpp:117
std::shared_ptr< WGraphicsEngine > getGraphicsEngine() const
Returns pointer to currently running instance of graphics engine.
Definition: WKernel.cpp:122
Show an glyph plot (in this case the glyphs are arrows) of a vector data set.
Definition: WMVectorPlot.h:48
virtual void activate()
Callback for m_active.
virtual void connectors()
Initialize the connectors this module is using.
WPropBool m_showOnAxial
indicates whether the vector should be shown on axial slice
Definition: WMVectorPlot.h:169
void transformVerts(osg::ref_ptr< osg::Vec3Array > verts)
Transforms the given vertices according to m_matrix.
WPropBool m_coloringMode
indicates a set color or direction color coding.
Definition: WMVectorPlot.h:157
std::shared_ptr< WDataSetVector > m_dataSet
This is a pointer to the dataset the module is currently working on.
Definition: WMVectorPlot.h:148
WPropBool m_projectOnSlice
projects the vectors on the slice.
Definition: WMVectorPlot.h:155
WPropColor m_aColor
color
Definition: WMVectorPlot.h:172
WPropBool m_showOnSagittal
indicates whether the vector should be shown on sagittal slice
Definition: WMVectorPlot.h:165
void updateCallback()
The update callback that is called for the osg node of this module.
osg::ref_ptr< osg::Geode > m_rootNode
The root node used for this modules graphics.
Definition: WMVectorPlot.h:105
WPropDouble m_scale
Scaling of arrow length.
Definition: WMVectorPlot.h:174
WPropDouble m_xSlice
x position of the slice
Definition: WMVectorPlot.h:159
virtual void moduleMain()
Entry point after loading the module.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
WPropBool m_showOnCoronal
indicates whether the vector should be shown on coronal slice
Definition: WMVectorPlot.h:167
virtual void properties()
Initialize the properties for this module.
osg::ref_ptr< osg::Geometry > buildPlotSlices()
Creates a vector plot on the currently selected slices.
Definition: WMVectorPlot.h:182
virtual const std::string getDescription() const
Gives back a description of this module.
std::shared_ptr< WModuleInputData< WDataSetVector > > m_input
An input connector used to get datasets from other modules.
Definition: WMVectorPlot.h:143
virtual const std::string getName() const
Gives back the name of this module.
WPropDouble m_ySlice
y position of the slice
Definition: WMVectorPlot.h:161
virtual ~WMVectorPlot()
Destructor.
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
WMVectorPlot()
Default constructor.
WPosition m_oldPos
The previous position of the slides.
Definition: WMVectorPlot.h:178
WPropDouble m_zSlice
z position of the slice
Definition: WMVectorPlot.h:163
Class offering an instantiate-able data connection between modules.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
Definition: WModule.cpp:575
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
Definition: WModule.cpp:108
std::shared_ptr< WProperties > m_properties
The property object for the module.
Definition: WModule.h:640
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
virtual void activate()
Callback for m_active.
Definition: WModule.cpp:220
WPropBool m_active
True whenever the module should be active.
Definition: WModule.h:723
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
Definition: WModule.h:652
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
This only is a 3d double vector.
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Indicates invalid type of something.
Definition: WTypeMismatch.h:37