OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WROISphere.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 <string>
26 #include <utility>
27 
28 #include <osg/LightModel>
29 
30 #include "callbacks/WGEFunctorCallback.h"
31 
32 #include "WROISphere.h"
33 #include "WGraphicsEngine.h"
34 #include "WGEUtils.h"
35 
36 size_t WROISphere::maxSphereId = 0;
37 
38 
39 WROISphere::WROISphere( WPosition position, float radius ) :
40  WROI(),
41  sphereId( maxSphereId++ ),
42  m_position( position ),
43  m_originalPosition( position ),
44  m_radius( radius ),
45  m_pickNormal( WVector3d() ),
46  m_oldPixelPosition( WVector2d::zero() ),
47  m_color( osg::Vec4( 0.f, 1.f, 1.f, 0.4f ) ),
48  m_notColor( osg::Vec4( 1.0f, 0.0f, 0.0f, 0.4f ) ),
49  m_lockPoint( WVector3d( 0.0, 0.0, 0.0 ) ),
50  m_lockVector( WVector3d( 1.0, 1.0, 1.0 ) ),
51  m_lockOnVector( false ),
52  m_lockX( false ),
53  m_lockY( false ),
54  m_lockZ( false )
55 {
56  boost::shared_ptr< WGraphicsEngine > ge = WGraphicsEngine::getGraphicsEngine();
57  assert( ge );
58  boost::shared_ptr< WGEViewer > viewer = ge->getViewerByName( "main" );
59  assert( viewer );
60  m_viewer = viewer;
61  m_pickHandler = m_viewer->getPickHandler();
62  m_pickHandler->getPickSignal()->connect( boost::bind( &WROISphere::registerRedrawRequest, this, _1 ) );
63 
64  redrawSphere();
65  //**********************************************************
66  m_dirty->set( true );
67 
68  setUserData( this );
69 
70  setUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROISphere::updateGFX, this ) ) );
71 }
72 
74 {
75  removeDrawables( 0 );
76 
77  osg::ShapeDrawable* shape = new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( m_position[0], m_position[1], m_position[2] ), m_radius ) );
78  shape->setColor( m_color );
79 
80  std::stringstream ss;
81  ss << "ROISphere" << sphereId;
82 
83  setName( ss.str() );
84  shape->setName( ss.str() );
85 
86  addDrawable( shape );
87 }
88 
90 {
91 // std::cout << "destructor called" << std::endl;
92 // std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl;
93 //
94 // WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode );
95 }
96 
98 {
99  return m_position;
100 }
101 
103 {
104  m_position = position;
105  m_lockPoint = position;
106  m_originalPosition = position;
107  m_dirty->set( true );
108 }
109 
110 void WROISphere::setPosition( float x, float y, float z )
111 {
112  m_position = WPosition( x, y, z );
113  m_lockPoint = WPosition( x, y, z );
114  m_originalPosition = WPosition( x, y, z );
115  m_dirty->set( true );
116 }
117 
118 
119 void WROISphere::setX( float x )
120 {
121  m_position.x() = x;
123  m_dirty->set( true );
124 }
125 
126 void WROISphere::setY( float y )
127 {
128  m_position.y() = y;
130  m_dirty->set( true );
131 }
132 
133 void WROISphere::setZ( float z )
134 {
135  m_position.z() = z;
137  m_dirty->set( true );
138 }
139 
140 
142 {
143  m_pickInfo = pickInfo;
144 }
145 
147 {
148  std::stringstream ss;
149  ss << "ROISphere" << sphereId << "";
150  bool mouseMove = false;
151 
152  if( m_pickInfo.getName() == ss.str() )
153  {
154  WVector2d newPixelPos( m_pickInfo.getPickPixel() );
155  if( m_isPicked )
156  {
157  osg::Vec3 in( newPixelPos.x(), newPixelPos.y(), 0.0 );
158  osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() );
159 
160  WPosition newPixelWorldPos( world[0], world[1], world[2] );
161  WPosition oldPixelWorldPos;
162  if( m_oldPixelPosition.x() == 0 && m_oldPixelPosition.y() == 0 )
163  {
164  oldPixelWorldPos = newPixelWorldPos;
165  }
166  else
167  {
168  osg::Vec3 in( m_oldPixelPosition.x(), m_oldPixelPosition.y(), 0.0 );
169  osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() );
170  oldPixelWorldPos = WPosition( world[0], world[1], world[2] );
171  }
172 
173  WVector3d moveVec = newPixelWorldPos - oldPixelWorldPos;
174 
175  osg::ref_ptr<osg::Vec3Array> vertices = osg::ref_ptr<osg::Vec3Array>( new osg::Vec3Array );
176 
177  // move sphere
178  if( m_pickInfo.getModifierKey() == WPickInfo::NONE )
179  {
180  moveSphere( moveVec );
181  mouseMove = true;
182  }
183  }
184 
185  m_oldPixelPosition = newPixelPos;
186  m_dirty->set( true );
187  m_isPicked = true;
188  }
189  if( m_isPicked && m_pickInfo.getName() == "unpick" )
190  {
191  // Perform all actions necessary for finishing a pick
193  m_isPicked = false;
194  }
195 
196  if( m_dirty->get() )
197  {
198  redrawSphere();
199  m_dirty->set( false );
200  }
201 
202  if( mouseMove )
203  {
204  signalRoiChange();
205  }
206 }
207 
209 {
210  m_position += offset;
211 
212  if( m_lockX )
213  {
214  m_position[0] = m_lockPoint[0];
215  }
216 
217  if( m_lockY )
218  {
219  m_position[1] = m_lockPoint[1];
220  }
221 
222  if( m_lockZ )
223  {
224  m_position[2] = m_lockPoint[2];
225  }
226 
227  if( m_lockOnVector )
228  {
229  float k = ( ( m_lockPoint[0] * m_lockVector[0] ) - ( m_position.x() * m_lockVector[0] ) +
230  ( m_lockPoint[1] * m_lockVector[1] ) - ( m_position.y() * m_lockVector[1] ) +
231  ( m_lockPoint[2] * m_lockVector[2] ) - ( m_position.z() * m_lockVector[2] ) ) /
232  ( m_lockVector[0] * m_lockVector[0] + m_lockVector[1] * m_lockVector[1] + m_lockVector[2] * m_lockVector[2] ) * -1.0;
233  m_position = m_lockPoint + ( m_lockVector * k );
234  }
235 }
236 
237 void WROISphere::setLockX( bool value )
238 {
239  m_lockX = value;
241 }
242 
243 void WROISphere::setLockY( bool value )
244 {
245  m_lockY = value;
247 }
248 
249 void WROISphere::setLockZ( bool value )
250 {
251  m_lockZ = value;
253 }
254 
255 
256 void WROISphere::setColor( osg::Vec4 color )
257 {
258  m_color = color;
259  redrawSphere();
260 }
261 
262 void WROISphere::setNotColor( osg::Vec4 color )
263 {
264  m_notColor = color;
265 }
266 
268 {
269  m_lockVector = vector;
271 }
272 
273 void WROISphere::setLockOnVector( bool value )
274 {
275  m_lockOnVector = value;
276 }