OpenWalnut  1.5.0dev
WHeadSurfaceCallback.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 <cstddef>
26 #include <memory>
27 #include <vector>
28 
29 #include <osg/Array>
30 #include <osg/Drawable>
31 #include <osg/Geometry>
32 
33 #include "WEEGEvent.h"
34 #include "WHeadSurfaceCallback.h"
35 #include "core/common/WFlag.h"
36 #include "core/common/WPropertyTypes.h"
37 #include "core/common/WPropertyVariable.h"
38 
39 WHeadSurfaceCallback::WHeadSurfaceCallback( const std::vector< std::size_t >& channelIDs,
40  WPropDouble colorSensitivity,
41  std::shared_ptr< WFlag< std::shared_ptr< WEEGEvent > > > event )
42  : m_currentColorSensitivity( 1.0 ),
43  m_currentTime( -1.0 ),
44  m_channelIDs( channelIDs ),
45  m_colorSensitivity( colorSensitivity ),
46  m_event( event )
47 {
48 }
49 
50 void WHeadSurfaceCallback::update( osg::NodeVisitor* /*nv*/, osg::Drawable* drawable )
51 {
52  const double colorSensitivity = m_colorSensitivity->get();
53  std::shared_ptr< WEEGEvent > event = m_event->get();
54  const double time = event->getTime();
55  if( colorSensitivity != m_currentColorSensitivity || time != m_currentTime )
56  {
57  osg::Geometry* geometry = static_cast< osg::Geometry* >( drawable );
58  if( geometry )
59  {
60  osg::FloatArray* texCoords = static_cast< osg::FloatArray* >( geometry->getTexCoordArray( 0 ) );
61  if( 0.0 <= time )
62  {
63  const std::size_t size = 256u;
64  const std::vector< double >& values = event->getValues();
65  for( std::size_t vertexID = 0; vertexID < texCoords->size(); ++vertexID )
66  {
67  (*texCoords)[vertexID] = values[m_channelIDs[vertexID]] * ( 0.5 - 0.5 / size ) / colorSensitivity + 0.5;
68  }
69  }
70  else
71  {
72  for( std::size_t vertexID = 0; vertexID < texCoords->size(); ++vertexID )
73  {
74  (*texCoords)[vertexID] = 0.5f;
75  }
76  }
77 
78  geometry->setTexCoordArray( 0, texCoords );
79 
80  osg::Vec4Array* colors = new osg::Vec4Array;
81  colors->push_back( osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
82  geometry->setColorArray( colors );
83  geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
84  }
85 
86  m_currentColorSensitivity = colorSensitivity;
87  m_currentTime = time;
88  }
89 }
Class to have a simple notification/condition system for simple flags.
Definition: WFlag.h:39
virtual void update(osg::NodeVisitor *, osg::Drawable *drawable)
Callback method called by the NodeVisitor.
WHeadSurfaceCallback(const std::vector< std::size_t > &channelIDs, WPropDouble colorSensitivity, std::shared_ptr< WFlag< std::shared_ptr< WEEGEvent > > > event)
Constructor.
const std::vector< std::size_t > m_channelIDs
the mapping from vertex indices to channel IDs
WPropDouble m_colorSensitivity
The sensitivity of the color map as property.
std::shared_ptr< WFlag< std::shared_ptr< WEEGEvent > > > m_event
event marking a special time position as WFlag
double m_currentColorSensitivity
the sensitivity of the color map which is currently used
double m_currentTime
The time position which is currently used.