OpenWalnut  1.4.0
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 <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <osg/LineWidth>
31 #include <osg/LightModel>
32 #include <osg/Geometry>
33 
34 #include "core/common/algorithms/WMarchingLegoAlgorithm.h"
35 
36 #include "callbacks/WGEFunctorCallback.h"
37 #include "WGraphicsEngine.h"
38 
39 #include "WROIArbitrary.h"
40 
41 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
42  const WMatrix< double >& mat,
43  const std::vector< float >& vals,
44  boost::shared_ptr< WTriangleMesh > triMesh,
45  float threshold,
46  float maxThreshold,
47  WColor color ) :
48  WROI(),
49  m_nbCoordsVec( 3 ),
50  m_matrix( mat ),
51  m_vals( vals ),
52  m_triMesh( triMesh ),
53  m_color( color )
54 {
55  m_nbCoordsVec[0] = nbCoordsX;
56  m_nbCoordsVec[1] = nbCoordsY;
57  m_nbCoordsVec[2] = nbCoordsZ;
58 
59  properties();
60 
61  m_threshold->set( threshold );
62  m_threshold->setMax( maxThreshold );
63 
64  updateGFX();
65 
66  WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
67  addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
68 
69  setDirty();
70 }
71 
72 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
73  const WMatrix< double >& mat,
74  const std::vector< float >& vals,
75  float maxThreshold,
76  WColor color ) :
77  WROI(),
78  m_nbCoordsVec( 3 ),
79  m_matrix( mat ),
80  m_vals( vals ),
81  m_color( color )
82 {
83  m_nbCoordsVec[0] = nbCoordsX;
84  m_nbCoordsVec[1] = nbCoordsY;
85  m_nbCoordsVec[2] = nbCoordsZ;
86 
87  properties();
88 
89  m_threshold->set( 0.01 );
90  m_threshold->setMax( maxThreshold );
91 
92  updateGFX();
93 
94  WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
95  addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
96 
97  setDirty();
98 }
99 
101 {
102 // std::cout << "destructor called" << std::endl;
103 // std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl;
104 //
105 // WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode );
106 }
107 
109 {
110  m_threshold = m_properties->addProperty( "Threshold", "description", 0. , boost::bind( &WROIArbitrary::propertyChanged, this ) );
111 }
112 
114 {
115  setDirty();
116 }
117 
118 void WROIArbitrary::setThreshold( double threshold )
119 {
120  m_threshold->set( threshold );
121  setDirty();
122 }
123 
125 {
126  return m_threshold->get();
127 }
128 
129 std::vector< size_t > WROIArbitrary::getCoordDimensions()
130 {
131  return m_nbCoordsVec;
132 }
133 
134 std::vector< double > WROIArbitrary::getCoordOffsets()
135 {
136  std::vector< double > vec( 3 );
137  vec[0] = m_matrix( 0, 0 );
138  vec[1] = m_matrix( 1, 1 );
139  vec[2] = m_matrix( 2, 2 );
140  return vec;
141 }
142 
143 float WROIArbitrary::getValue( size_t i )
144 {
145  return m_vals[i];
146 }
147 
149 {
150  if( m_dirty->get() )
151  {
152  WMarchingLegoAlgorithm mlAlgo;
154  m_matrix,
155  &m_vals,
156  m_threshold->get() );
157 
158  osg::Geometry* surfaceGeometry = new osg::Geometry();
159  setName( "roi" );
160 
161  surfaceGeometry->setVertexArray( m_triMesh->getVertexArray() );
162 
163  // ------------------------------------------------
164  // normals
165  surfaceGeometry->setNormalArray( m_triMesh->getVertexNormalArray() );
166  surfaceGeometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
167 
168  // ------------------------------------------------
169  // colors
170  osg::Vec4Array* colors = new osg::Vec4Array;
171  colors->push_back( m_color );
172  surfaceGeometry->setColorArray( colors );
173  surfaceGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
174 
175  osg::DrawElementsUInt* surfaceElement = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
176 
177  std::vector< size_t >tris = m_triMesh->getTriangles();
178  surfaceElement->reserve( tris.size() );
179 
180  for( unsigned int vertId = 0; vertId < tris.size(); ++vertId )
181  {
182  surfaceElement->push_back( tris[vertId] );
183  }
184  surfaceGeometry->addPrimitiveSet( surfaceElement );
185  removeDrawables( 0 );
186  addDrawable( surfaceGeometry );
187 
188  osg::StateSet* state = getOrCreateStateSet();
189  osg::ref_ptr<osg::LightModel> lightModel = new osg::LightModel();
190  lightModel->setTwoSided( true );
191  state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
192 
193  state->setMode( GL_BLEND, osg::StateAttribute::ON );
194 
195  // {
196  // osg::ref_ptr< osg::Material > material = new osg::Material();
197  // material->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
198  // material->setSpecular( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
199  // material->setAmbient( osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
200  // material->setEmission( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
201  // material->setShininess( osg::Material::FRONT, 25.0 );
202  // state->setAttribute( material );
203  // }
204 
205  m_dirty->set( false );
206  }
207 }