OpenWalnut  1.5.0dev
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 <memory>
26 #include <sstream>
27 #include <string>
28 #include <utility>
29 
30 #include <osg/ShapeDrawable>
31 
32 #include "WGEUtils.h"
33 #include "WGraphicsEngine.h"
34 #include "WROISphere.h"
35 #include "callbacks/WGEFunctorCallback.h"
36 
37 size_t WROISphere::maxSphereId = 0;
38 
39 
40 WROISphere::WROISphere( WPosition position, float radius ) :
41  WROI(),
42  sphereId( maxSphereId++ ),
43  m_position( position ),
44  m_originalPosition( position ),
45  m_radius( radius ),
46  m_pickNormal( WVector3d() ),
47  m_oldPixelPosition( WVector2d::zero() ),
48  m_color( osg::Vec4( 0.f, 1.f, 1.f, 0.4f ) ),
49  m_notColor( osg::Vec4( 1.0f, 0.0f, 0.0f, 0.4f ) ),
50  m_lockPoint( WVector3d( 0.0, 0.0, 0.0 ) ),
51  m_lockVector( WVector3d( 1.0, 1.0, 1.0 ) ),
52  m_lockOnVector( false ),
53  m_lockX( false ),
54  m_lockY( false ),
55  m_lockZ( false )
56 {
57  std::shared_ptr< WGraphicsEngine > ge = WGraphicsEngine::getGraphicsEngine();
58  assert( ge );
59  std::shared_ptr< WGEViewer > viewer = ge->getViewerByName( "Main View" );
60  assert( viewer );
61  m_viewer = viewer;
62  m_pickHandler = m_viewer->getPickHandler();
63  m_pickHandler->getPickSignal()->connect( boost::bind( &WROISphere::registerRedrawRequest, this, boost::placeholders::_1 ) );
64 
65  redrawSphere();
66  //**********************************************************
67  m_dirty->set( true );
68 
69  setUserData( this );
70 
71  setUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROISphere::updateGFX, this ) ) );
72 }
73 
75 {
76  removeDrawables( 0 );
77 
78  osg::ShapeDrawable* shape = new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( m_position[0], m_position[1], m_position[2] ), m_radius ) );
79  shape->setColor( m_color );
80 
81  std::stringstream ss;
82  ss << "ROISphere" << sphereId;
83 
84  setName( ss.str() );
85  shape->setName( ss.str() );
86 
87  addDrawable( shape );
88 }
89 
91 {
92 // std::cout << "destructor called" << std::endl;
93 // std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl;
94 //
95 // WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode );
96 }
97 
99 {
100  return m_position;
101 }
102 
104 {
105  m_position = position;
106  m_lockPoint = position;
107  m_originalPosition = position;
108  m_dirty->set( true );
109 }
110 
111 void WROISphere::setPosition( float x, float y, float z )
112 {
113  m_position = WPosition( x, y, z );
114  m_lockPoint = WPosition( x, y, z );
115  m_originalPosition = WPosition( x, y, z );
116  m_dirty->set( true );
117 }
118 
119 
120 void WROISphere::setX( float x )
121 {
122  m_position.x() = x;
124  m_dirty->set( true );
125 }
126 
127 void WROISphere::setY( float y )
128 {
129  m_position.y() = y;
131  m_dirty->set( true );
132 }
133 
134 void WROISphere::setZ( float z )
135 {
136  m_position.z() = z;
138  m_dirty->set( true );
139 }
140 
141 
143 {
144  m_pickInfo = pickInfo;
145 }
146 
148 {
149  std::stringstream ss;
150  ss << "ROISphere" << sphereId << "";
151  bool mouseMove = false;
152 
153  if( m_pickInfo.getName() == ss.str() )
154  {
155  WVector2d newPixelPos( m_pickInfo.getPickPixel() );
156  if( m_isPicked )
157  {
158  osg::Vec3 in( newPixelPos.x(), newPixelPos.y(), 0.0 );
159  osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() );
160 
161  WPosition newPixelWorldPos( world[0], world[1], world[2] );
162  WPosition oldPixelWorldPos;
163  if( m_oldPixelPosition.x() == 0 && m_oldPixelPosition.y() == 0 )
164  {
165  oldPixelWorldPos = newPixelWorldPos;
166  }
167  else
168  {
169  osg::Vec3 in( m_oldPixelPosition.x(), m_oldPixelPosition.y(), 0.0 );
170  osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() );
171  oldPixelWorldPos = WPosition( world[0], world[1], world[2] );
172  }
173 
174  WVector3d moveVec = newPixelWorldPos - oldPixelWorldPos;
175 
176  osg::ref_ptr<osg::Vec3Array> vertices = osg::ref_ptr<osg::Vec3Array>( new osg::Vec3Array );
177 
178  // move sphere
179  if( m_pickInfo.getModifierKey() == WPickInfo::NONE )
180  {
181  moveSphere( moveVec );
182  mouseMove = true;
183  }
184  }
185 
186  m_oldPixelPosition = newPixelPos;
187  m_dirty->set( true );
188  m_isPicked = true;
189  }
191  {
192  // Perform all actions necessary for finishing a pick
194  m_isPicked = false;
195  }
196 
197  if( m_dirty->get() )
198  {
199  redrawSphere();
200  m_dirty->set( false );
201  }
202 
203  if( mouseMove )
204  {
205  signalRoiChange();
206  }
207 }
208 
210 {
211  m_position += offset;
212 
213  if( m_lockX )
214  {
215  m_position[0] = m_lockPoint[0];
216  }
217 
218  if( m_lockY )
219  {
220  m_position[1] = m_lockPoint[1];
221  }
222 
223  if( m_lockZ )
224  {
225  m_position[2] = m_lockPoint[2];
226  }
227 
228  if( m_lockOnVector )
229  {
230  float k = ( ( m_lockPoint[0] * m_lockVector[0] ) - ( m_position.x() * m_lockVector[0] ) +
231  ( m_lockPoint[1] * m_lockVector[1] ) - ( m_position.y() * m_lockVector[1] ) +
232  ( m_lockPoint[2] * m_lockVector[2] ) - ( m_position.z() * m_lockVector[2] ) ) /
233  ( m_lockVector[0] * m_lockVector[0] + m_lockVector[1] * m_lockVector[1] + m_lockVector[2] * m_lockVector[2] ) * -1.0;
234  m_position = m_lockPoint + ( m_lockVector * k );
235  }
236 }
237 
238 void WROISphere::setLockX( bool value )
239 {
240  m_lockX = value;
242 }
243 
244 void WROISphere::setLockY( bool value )
245 {
246  m_lockY = value;
248 }
249 
250 void WROISphere::setLockZ( bool value )
251 {
252  m_lockZ = value;
254 }
255 
256 
257 void WROISphere::setColor( osg::Vec4 color )
258 {
259  m_color = color;
260  redrawSphere();
261 }
262 
263 void WROISphere::setNotColor( osg::Vec4 color )
264 {
265  m_notColor = color;
266 }
267 
269 {
270  m_lockVector = vector;
272 }
273 
274 void WROISphere::setLockOnVector( bool value )
275 {
276  m_lockOnVector = value;
277 }
This callback allows you a simple usage of callbacks in your module.
static std::shared_ptr< WGraphicsEngine > getGraphicsEngine()
Returns instance of the graphics engine.
ValueT & y()
Access y element of vector.
ValueT & x()
Access x element of vector.
static const std::string unpickString
The string indicating picking has stopped.
Definition: WPickHandler.h:104
Encapsulates info for pick action.
Definition: WPickInfo.h:42
WVector2d getPickPixel() const
Returns the picked pixel coordinates in screen-space.
Definition: WPickInfo.h:275
std::string getName() const
Get name of picked object.
Definition: WPickInfo.h:243
modifierKey getModifierKey() const
Get the modifier key associated with the pick.
Definition: WPickInfo.h:223
This only is a 3d double vector.
bool m_lockX
flag indicatin wether the movement of the sphere is restricted
Definition: WROISphere.h:187
virtual ~WROISphere()
standard destructor
Definition: WROISphere.cpp:90
void redrawSphere()
removes the old drawable from the osg geode and adds a new one at the current position and size
Definition: WROISphere.cpp:74
osg::Vec4 m_notColor
the color of the box when negated
Definition: WROISphere.h:179
virtual void updateGFX()
updates the graphics
Definition: WROISphere.cpp:147
void setZ(float z)
setter
Definition: WROISphere.cpp:134
WVector3d m_pickNormal
Store the normal that occured when the pick action was started.
Definition: WROISphere.h:169
static size_t maxSphereId
Current maximum boxId over all spheres.
Definition: WROISphere.h:156
void setY(float y)
setter
Definition: WROISphere.cpp:127
void setLockY(bool value=true)
sets the flag that allows or disallows movement along the y axis
Definition: WROISphere.cpp:244
osg::Vec4 m_color
the color of the box
Definition: WROISphere.h:177
void setLockVector(WVector3d vector)
setter
Definition: WROISphere.cpp:268
WPosition getPosition() const
getter
Definition: WROISphere.cpp:98
void registerRedrawRequest(WPickInfo pickInfo)
note that there was a pick
Definition: WROISphere.cpp:142
WVector3d m_lockVector
stores the lock vector
Definition: WROISphere.h:183
void setPosition(WPosition position)
setter
Definition: WROISphere.cpp:103
void setLockZ(bool value=true)
sets the flag that allows or disallows movement along the z axis
Definition: WROISphere.cpp:250
std::shared_ptr< WGEViewer > m_viewer
makes viewer available all over this class.
Definition: WROISphere.h:175
void setLockOnVector(bool value=true)
setter
Definition: WROISphere.cpp:274
bool m_lockZ
flag indicatin wether the movement of the sphere is restricted
Definition: WROISphere.h:189
bool m_isPicked
Indicates whether the box is currently picked or not.
Definition: WROISphere.h:165
float m_radius
The radius of the sphere.
Definition: WROISphere.h:163
void setLockX(bool value=true)
sets the flag that allows or disallows movement along the x axis
Definition: WROISphere.cpp:238
WPickInfo m_pickInfo
Stores the pick information for potential redraw.
Definition: WROISphere.h:173
size_t sphereId
Id of the current sphere.
Definition: WROISphere.h:157
WPosition m_position
The position of the sphere.
Definition: WROISphere.h:159
bool m_lockOnVector
flag indicatin wether the movement of the sphere is restricted
Definition: WROISphere.h:185
void setNotColor(osg::Vec4 color)
Setter for color in negated state.
Definition: WROISphere.cpp:263
bool m_lockY
flag indicatin wether the movement of the sphere is restricted
Definition: WROISphere.h:188
WROISphere(WPosition position, float radius=5.0)
Yields sphere with desired center point and radius.
Definition: WROISphere.cpp:40
WVector2d m_oldPixelPosition
Caches the old picked position to a allow for cmoparison.
Definition: WROISphere.h:171
void moveSphere(WVector3d offset)
move the sphere with a given offset
Definition: WROISphere.cpp:209
WVector3d m_lockPoint
stores to point of origin of the lock vector
Definition: WROISphere.h:181
WPosition m_originalPosition
The position of the sphere when created, used for locking.
Definition: WROISphere.h:161
void setX(float x)
setter
Definition: WROISphere.cpp:120
void setColor(osg::Vec4 color)
Setter for standard color.
Definition: WROISphere.cpp:257
Superclass for different ROI (region of interest) types.
Definition: WROI.h:45
osg::ref_ptr< WPickHandler > m_pickHandler
A pointer to the pick handler used to get gui events for moving the box.
Definition: WROI.h:172
void signalRoiChange()
signals a roi change to all subscribers
Definition: WROI.cpp:140
WPropBool m_dirty
dirty flag, indicating the graphics needs updating, it is no longer used for bitfield updating since ...
Definition: WROI.h:183
osg::Vec3 unprojectFromScreen(const osg::Vec3 screen, osg::ref_ptr< WGECamera > camera)
Converts screen coordinates into Camera coordinates.
Definition: WGEUtils.cpp:52