OpenWalnut  1.5.0dev
WROIArbitrary.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 <iostream>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include <osg/Geometry>
32 #include <osg/LightModel>
33 #include <osg/LineWidth>
34 
35 #include "WGraphicsEngine.h"
36 #include "WROIArbitrary.h"
37 #include "callbacks/WGEFunctorCallback.h"
38 #include "core/common/algorithms/WMarchingLegoAlgorithm.h"
39 
40 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
41  const WMatrix< double >& mat,
42  const std::vector< float >& vals,
43  std::shared_ptr< WTriangleMesh > triMesh,
44  float threshold,
45  float maxThreshold,
46  WColor color ) :
47  WROI(),
48  m_nbCoordsVec( 3 ),
49  m_matrix( mat ),
50  m_vals( vals ),
51  m_triMesh( triMesh ),
52  m_color( color )
53 {
54  m_nbCoordsVec[0] = nbCoordsX;
55  m_nbCoordsVec[1] = nbCoordsY;
56  m_nbCoordsVec[2] = nbCoordsZ;
57 
58  properties();
59 
60  m_threshold->set( threshold );
61  m_threshold->setMax( maxThreshold );
62 
63  updateGFX();
64 
65  WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
66  addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
67 
68  setDirty();
69 }
70 
71 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
72  const WMatrix< double >& mat,
73  const std::vector< float >& vals,
74  float maxThreshold,
75  WColor color ) :
76  WROI(),
77  m_nbCoordsVec( 3 ),
78  m_matrix( mat ),
79  m_vals( vals ),
80  m_color( color )
81 {
82  m_nbCoordsVec[0] = nbCoordsX;
83  m_nbCoordsVec[1] = nbCoordsY;
84  m_nbCoordsVec[2] = nbCoordsZ;
85 
86  properties();
87 
88  m_threshold->set( 0.01 );
89  m_threshold->setMax( maxThreshold );
90 
91  updateGFX();
92 
93  WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
94  addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
95 
96  setDirty();
97 }
98 
100 {
101 // std::cout << "destructor called" << std::endl;
102 // std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl;
103 //
104 // WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode );
105 }
106 
108 {
109  m_threshold = m_properties->addProperty( "Threshold", "description", 0. , boost::bind( &WROIArbitrary::propertyChanged, this ) );
110 }
111 
113 {
114  setDirty();
115 }
116 
117 void WROIArbitrary::setThreshold( double threshold )
118 {
119  m_threshold->set( threshold );
120  setDirty();
121 }
122 
124 {
125  return m_threshold->get();
126 }
127 
128 std::vector< size_t > WROIArbitrary::getCoordDimensions()
129 {
130  return m_nbCoordsVec;
131 }
132 
133 std::vector< double > WROIArbitrary::getCoordOffsets()
134 {
135  std::vector< double > vec( 3 );
136  vec[0] = m_matrix( 0, 0 );
137  vec[1] = m_matrix( 1, 1 );
138  vec[2] = m_matrix( 2, 2 );
139  return vec;
140 }
141 
142 float WROIArbitrary::getValue( size_t i )
143 {
144  return m_vals[i];
145 }
146 
148 {
149  if( m_dirty->get() )
150  {
151  WMarchingLegoAlgorithm mlAlgo;
153  m_matrix,
154  &m_vals,
155  m_threshold->get() );
156 
157  osg::Geometry* surfaceGeometry = new osg::Geometry();
158  setName( "roi" );
159 
160  surfaceGeometry->setVertexArray( m_triMesh->getVertexArray() );
161 
162  // ------------------------------------------------
163  // normals
164  surfaceGeometry->setNormalArray( m_triMesh->getVertexNormalArray() );
165  surfaceGeometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
166 
167  // ------------------------------------------------
168  // colors
169  osg::Vec4Array* colors = new osg::Vec4Array;
170  colors->push_back( m_color );
171  surfaceGeometry->setColorArray( colors );
172  surfaceGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
173 
174  osg::DrawElementsUInt* surfaceElement = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
175 
176  std::vector< size_t >tris = m_triMesh->getTriangles();
177  surfaceElement->reserve( tris.size() );
178 
179  for( unsigned int vertId = 0; vertId < tris.size(); ++vertId )
180  {
181  surfaceElement->push_back( tris[vertId] );
182  }
183  surfaceGeometry->addPrimitiveSet( surfaceElement );
184  removeDrawables( 0 );
185  addDrawable( surfaceGeometry );
186 
187  osg::StateSet* state = getOrCreateStateSet();
188  osg::ref_ptr<osg::LightModel> lightModel = new osg::LightModel();
189  lightModel->setTwoSided( true );
190  state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
191 
192  state->setMode( GL_BLEND, osg::StateAttribute::ON );
193 
194  // {
195  // osg::ref_ptr< osg::Material > material = new osg::Material();
196  // material->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
197  // material->setSpecular( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
198  // material->setAmbient( osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
199  // material->setEmission( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
200  // material->setShininess( osg::Material::FRONT, 25.0 );
201  // state->setAttribute( material );
202  // }
203 
204  m_dirty->set( false );
205  }
206 }
This callback allows you a simple usage of callbacks in your module.
static std::shared_ptr< WGraphicsEngine > getGraphicsEngine()
Returns instance of the graphics engine.
Creates a non interpolated triangulation of an isosurface.
std::shared_ptr< WTriangleMesh > generateSurface(size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ, const WMatrix< double > &mat, const std::vector< T > *vals, double isoValue, std::shared_ptr< WProgressCombiner > mainProgress=std::shared_ptr< WProgressCombiner >())
Generate the triangles for the surface on the given dataSet (inGrid, vals).
double getThreshold()
getter
float getValue(size_t i)
Get the i-th value of the data defining the ROI.
void properties()
initalizes the properties
virtual void updateGFX()
updates the graphics
void setThreshold(double threshold)
setter
WMatrix< double > m_matrix
The 4x4 transformation matrix for the vertices.
std::vector< size_t > getCoordDimensions()
Get the number of vertices in the three coordinate directions.
std::vector< double > getCoordOffsets()
Get the vertex offsets in the three coordinate directions.
void propertyChanged()
Used as callback to mark a change in the ROI.
WROIArbitrary(size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ, const WMatrix< double > &mat, const std::vector< float > &vals, std::shared_ptr< WTriangleMesh > triMesh, float threshold, float maxThreshold, WColor color)
constructor
std::vector< size_t > m_nbCoordsVec
The data's number of vertices in X, Y and Z direction.
const std::vector< float > m_vals
The data at the vertices.
WPropDouble m_threshold
the threshold
virtual ~WROIArbitrary()
destructor
std::shared_ptr< WTriangleMesh > m_triMesh
This triangle mesh is provided as output through the connector.
WColor m_color
The ROI color.
Superclass for different ROI (region of interest) types.
Definition: WROI.h:45
std::shared_ptr< WProperties > m_properties
the property object for the module
Definition: WROI.h:177
void setDirty()
sets the dirty flag
Definition: WROI.cpp:119
WPropBool m_dirty
dirty flag, indicating the graphics needs updating, it is no longer used for bitfield updating since ...
Definition: WROI.h:183