OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #include <string> 00026 #include <sstream> 00027 #include <utility> 00028 00029 #include <osg/ShapeDrawable> 00030 00031 #include "callbacks/WGEFunctorCallback.h" 00032 00033 #include "WROISphere.h" 00034 #include "WGraphicsEngine.h" 00035 #include "WGEUtils.h" 00036 00037 size_t WROISphere::maxSphereId = 0; 00038 00039 00040 WROISphere::WROISphere( WPosition position, float radius ) : 00041 WROI(), 00042 sphereId( maxSphereId++ ), 00043 m_position( position ), 00044 m_originalPosition( position ), 00045 m_radius( radius ), 00046 m_pickNormal( WVector3d() ), 00047 m_oldPixelPosition( WVector2d::zero() ), 00048 m_color( osg::Vec4( 0.f, 1.f, 1.f, 0.4f ) ), 00049 m_notColor( osg::Vec4( 1.0f, 0.0f, 0.0f, 0.4f ) ), 00050 m_lockPoint( WVector3d( 0.0, 0.0, 0.0 ) ), 00051 m_lockVector( WVector3d( 1.0, 1.0, 1.0 ) ), 00052 m_lockOnVector( false ), 00053 m_lockX( false ), 00054 m_lockY( false ), 00055 m_lockZ( false ) 00056 { 00057 boost::shared_ptr< WGraphicsEngine > ge = WGraphicsEngine::getGraphicsEngine(); 00058 assert( ge ); 00059 boost::shared_ptr< WGEViewer > viewer = ge->getViewerByName( "Main View" ); 00060 assert( viewer ); 00061 m_viewer = viewer; 00062 m_pickHandler = m_viewer->getPickHandler(); 00063 m_pickHandler->getPickSignal()->connect( boost::bind( &WROISphere::registerRedrawRequest, this, _1 ) ); 00064 00065 redrawSphere(); 00066 //********************************************************** 00067 m_dirty->set( true ); 00068 00069 setUserData( this ); 00070 00071 setUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROISphere::updateGFX, this ) ) ); 00072 } 00073 00074 void WROISphere::redrawSphere() 00075 { 00076 removeDrawables( 0 ); 00077 00078 osg::ShapeDrawable* shape = new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( m_position[0], m_position[1], m_position[2] ), m_radius ) ); 00079 shape->setColor( m_color ); 00080 00081 std::stringstream ss; 00082 ss << "ROISphere" << sphereId; 00083 00084 setName( ss.str() ); 00085 shape->setName( ss.str() ); 00086 00087 addDrawable( shape ); 00088 } 00089 00090 WROISphere::~WROISphere() 00091 { 00092 // std::cout << "destructor called" << std::endl; 00093 // std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl; 00094 // 00095 // WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode ); 00096 } 00097 00098 WPosition WROISphere::getPosition() const 00099 { 00100 return m_position; 00101 } 00102 00103 void WROISphere::setPosition( WPosition position ) 00104 { 00105 m_position = position; 00106 m_lockPoint = position; 00107 m_originalPosition = position; 00108 m_dirty->set( true ); 00109 } 00110 00111 void WROISphere::setPosition( float x, float y, float z ) 00112 { 00113 m_position = WPosition( x, y, z ); 00114 m_lockPoint = WPosition( x, y, z ); 00115 m_originalPosition = WPosition( x, y, z ); 00116 m_dirty->set( true ); 00117 } 00118 00119 00120 void WROISphere::setX( float x ) 00121 { 00122 m_position.x() = x; 00123 m_originalPosition = m_position; 00124 m_dirty->set( true ); 00125 } 00126 00127 void WROISphere::setY( float y ) 00128 { 00129 m_position.y() = y; 00130 m_originalPosition = m_position; 00131 m_dirty->set( true ); 00132 } 00133 00134 void WROISphere::setZ( float z ) 00135 { 00136 m_position.z() = z; 00137 m_originalPosition = m_position; 00138 m_dirty->set( true ); 00139 } 00140 00141 00142 void WROISphere::registerRedrawRequest( WPickInfo pickInfo ) 00143 { 00144 m_pickInfo = pickInfo; 00145 } 00146 00147 void WROISphere::updateGFX() 00148 { 00149 std::stringstream ss; 00150 ss << "ROISphere" << sphereId << ""; 00151 bool mouseMove = false; 00152 00153 if( m_pickInfo.getName() == ss.str() ) 00154 { 00155 WVector2d newPixelPos( m_pickInfo.getPickPixel() ); 00156 if( m_isPicked ) 00157 { 00158 osg::Vec3 in( newPixelPos.x(), newPixelPos.y(), 0.0 ); 00159 osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() ); 00160 00161 WPosition newPixelWorldPos( world[0], world[1], world[2] ); 00162 WPosition oldPixelWorldPos; 00163 if( m_oldPixelPosition.x() == 0 && m_oldPixelPosition.y() == 0 ) 00164 { 00165 oldPixelWorldPos = newPixelWorldPos; 00166 } 00167 else 00168 { 00169 osg::Vec3 in( m_oldPixelPosition.x(), m_oldPixelPosition.y(), 0.0 ); 00170 osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() ); 00171 oldPixelWorldPos = WPosition( world[0], world[1], world[2] ); 00172 } 00173 00174 WVector3d moveVec = newPixelWorldPos - oldPixelWorldPos; 00175 00176 osg::ref_ptr<osg::Vec3Array> vertices = osg::ref_ptr<osg::Vec3Array>( new osg::Vec3Array ); 00177 00178 // move sphere 00179 if( m_pickInfo.getModifierKey() == WPickInfo::NONE ) 00180 { 00181 moveSphere( moveVec ); 00182 mouseMove = true; 00183 } 00184 } 00185 00186 m_oldPixelPosition = newPixelPos; 00187 m_dirty->set( true ); 00188 m_isPicked = true; 00189 } 00190 if( m_isPicked && m_pickInfo.getName() == "unpick" ) 00191 { 00192 // Perform all actions necessary for finishing a pick 00193 m_pickNormal = WVector3d(); 00194 m_isPicked = false; 00195 } 00196 00197 if( m_dirty->get() ) 00198 { 00199 redrawSphere(); 00200 m_dirty->set( false ); 00201 } 00202 00203 if( mouseMove ) 00204 { 00205 signalRoiChange(); 00206 } 00207 } 00208 00209 void WROISphere::moveSphere( WVector3d offset ) 00210 { 00211 m_position += offset; 00212 00213 if( m_lockX ) 00214 { 00215 m_position[0] = m_lockPoint[0]; 00216 } 00217 00218 if( m_lockY ) 00219 { 00220 m_position[1] = m_lockPoint[1]; 00221 } 00222 00223 if( m_lockZ ) 00224 { 00225 m_position[2] = m_lockPoint[2]; 00226 } 00227 00228 if( m_lockOnVector ) 00229 { 00230 float k = ( ( m_lockPoint[0] * m_lockVector[0] ) - ( m_position.x() * m_lockVector[0] ) + 00231 ( m_lockPoint[1] * m_lockVector[1] ) - ( m_position.y() * m_lockVector[1] ) + 00232 ( m_lockPoint[2] * m_lockVector[2] ) - ( m_position.z() * m_lockVector[2] ) ) / 00233 ( m_lockVector[0] * m_lockVector[0] + m_lockVector[1] * m_lockVector[1] + m_lockVector[2] * m_lockVector[2] ) * -1.0; 00234 m_position = m_lockPoint + ( m_lockVector * k ); 00235 } 00236 } 00237 00238 void WROISphere::setLockX( bool value ) 00239 { 00240 m_lockX = value; 00241 m_lockPoint = m_position; 00242 } 00243 00244 void WROISphere::setLockY( bool value ) 00245 { 00246 m_lockY = value; 00247 m_lockPoint = m_position; 00248 } 00249 00250 void WROISphere::setLockZ( bool value ) 00251 { 00252 m_lockZ = value; 00253 m_lockPoint = m_position; 00254 } 00255 00256 00257 void WROISphere::setColor( osg::Vec4 color ) 00258 { 00259 m_color = color; 00260 redrawSphere(); 00261 } 00262 00263 void WROISphere::setNotColor( osg::Vec4 color ) 00264 { 00265 m_notColor = color; 00266 } 00267 00268 void WROISphere::setLockVector( WVector3d vector ) 00269 { 00270 m_lockVector = vector; 00271 m_lockPoint = m_position; 00272 } 00273 00274 void WROISphere::setLockOnVector( bool value ) 00275 { 00276 m_lockOnVector = value; 00277 }