OpenWalnut
1.2.5
|
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 <utility> 00027 00028 #include <osg/LightModel> 00029 00030 #include "callbacks/WGEFunctorCallback.h" 00031 00032 #include "WROISphere.h" 00033 #include "WGraphicsEngine.h" 00034 #include "WGEUtils.h" 00035 00036 size_t WROISphere::maxSphereId = 0; 00037 00038 00039 WROISphere::WROISphere( WPosition position, float radius ) : 00040 WROI(), 00041 sphereId( maxSphereId++ ), 00042 m_position( position ), 00043 m_originalPosition( position ), 00044 m_radius( radius ), 00045 m_pickNormal( WVector3d() ), 00046 m_oldPixelPosition( WVector2d::zero() ), 00047 m_color( osg::Vec4( 0.f, 1.f, 1.f, 0.4f ) ), 00048 m_notColor( osg::Vec4( 1.0f, 0.0f, 0.0f, 0.4f ) ), 00049 m_lockPoint( WVector3d( 0.0, 0.0, 0.0 ) ), 00050 m_lockVector( WVector3d( 1.0, 1.0, 1.0 ) ), 00051 m_lockOnVector( false ), 00052 m_lockX( false ), 00053 m_lockY( false ), 00054 m_lockZ( false ) 00055 { 00056 boost::shared_ptr< WGraphicsEngine > ge = WGraphicsEngine::getGraphicsEngine(); 00057 assert( ge ); 00058 boost::shared_ptr< WGEViewer > viewer = ge->getViewerByName( "main" ); 00059 assert( viewer ); 00060 m_viewer = viewer; 00061 m_pickHandler = m_viewer->getPickHandler(); 00062 m_pickHandler->getPickSignal()->connect( boost::bind( &WROISphere::registerRedrawRequest, this, _1 ) ); 00063 00064 redrawSphere(); 00065 //********************************************************** 00066 m_dirty->set( true ); 00067 00068 setUserData( this ); 00069 00070 setUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROISphere::updateGFX, this ) ) ); 00071 } 00072 00073 void WROISphere::redrawSphere() 00074 { 00075 removeDrawables( 0 ); 00076 00077 osg::ShapeDrawable* shape = new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( m_position[0], m_position[1], m_position[2] ), m_radius ) ); 00078 shape->setColor( m_color ); 00079 00080 std::stringstream ss; 00081 ss << "ROISphere" << sphereId; 00082 00083 setName( ss.str() ); 00084 shape->setName( ss.str() ); 00085 00086 addDrawable( shape ); 00087 } 00088 00089 WROISphere::~WROISphere() 00090 { 00091 // std::cout << "destructor called" << std::endl; 00092 // std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl; 00093 // 00094 // WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode ); 00095 } 00096 00097 WPosition WROISphere::getPosition() const 00098 { 00099 return m_position; 00100 } 00101 00102 void WROISphere::setPosition( WPosition position ) 00103 { 00104 m_position = position; 00105 m_lockPoint = position; 00106 m_originalPosition = position; 00107 m_dirty->set( true ); 00108 } 00109 00110 void WROISphere::setPosition( float x, float y, float z ) 00111 { 00112 m_position = WPosition( x, y, z ); 00113 m_lockPoint = WPosition( x, y, z ); 00114 m_originalPosition = WPosition( x, y, z ); 00115 m_dirty->set( true ); 00116 } 00117 00118 00119 void WROISphere::setX( float x ) 00120 { 00121 m_position.x() = x; 00122 m_originalPosition = m_position; 00123 m_dirty->set( true ); 00124 } 00125 00126 void WROISphere::setY( float y ) 00127 { 00128 m_position.y() = y; 00129 m_originalPosition = m_position; 00130 m_dirty->set( true ); 00131 } 00132 00133 void WROISphere::setZ( float z ) 00134 { 00135 m_position.z() = z; 00136 m_originalPosition = m_position; 00137 m_dirty->set( true ); 00138 } 00139 00140 00141 void WROISphere::registerRedrawRequest( WPickInfo pickInfo ) 00142 { 00143 m_pickInfo = pickInfo; 00144 } 00145 00146 void WROISphere::updateGFX() 00147 { 00148 std::stringstream ss; 00149 ss << "ROISphere" << sphereId << ""; 00150 bool mouseMove = false; 00151 00152 if( m_pickInfo.getName() == ss.str() ) 00153 { 00154 WVector2d newPixelPos( m_pickInfo.getPickPixel() ); 00155 if( m_isPicked ) 00156 { 00157 osg::Vec3 in( newPixelPos.x(), newPixelPos.y(), 0.0 ); 00158 osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() ); 00159 00160 WPosition newPixelWorldPos( world[0], world[1], world[2] ); 00161 WPosition oldPixelWorldPos; 00162 if( m_oldPixelPosition.x() == 0 && m_oldPixelPosition.y() == 0 ) 00163 { 00164 oldPixelWorldPos = newPixelWorldPos; 00165 } 00166 else 00167 { 00168 osg::Vec3 in( m_oldPixelPosition.x(), m_oldPixelPosition.y(), 0.0 ); 00169 osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() ); 00170 oldPixelWorldPos = WPosition( world[0], world[1], world[2] ); 00171 } 00172 00173 WVector3d moveVec = newPixelWorldPos - oldPixelWorldPos; 00174 00175 osg::ref_ptr<osg::Vec3Array> vertices = osg::ref_ptr<osg::Vec3Array>( new osg::Vec3Array ); 00176 00177 // move sphere 00178 if( m_pickInfo.getModifierKey() == WPickInfo::NONE ) 00179 { 00180 moveSphere( moveVec ); 00181 mouseMove = true; 00182 } 00183 } 00184 00185 m_oldPixelPosition = newPixelPos; 00186 m_dirty->set( true ); 00187 m_isPicked = true; 00188 } 00189 if( m_isPicked && m_pickInfo.getName() == "unpick" ) 00190 { 00191 // Perform all actions necessary for finishing a pick 00192 m_pickNormal = WVector3d(); 00193 m_isPicked = false; 00194 } 00195 00196 if( m_dirty->get() ) 00197 { 00198 redrawSphere(); 00199 m_dirty->set( false ); 00200 } 00201 00202 if( mouseMove ) 00203 { 00204 signalRoiChange(); 00205 } 00206 } 00207 00208 void WROISphere::moveSphere( WVector3d offset ) 00209 { 00210 m_position += offset; 00211 00212 if( m_lockX ) 00213 { 00214 m_position[0] = m_lockPoint[0]; 00215 } 00216 00217 if( m_lockY ) 00218 { 00219 m_position[1] = m_lockPoint[1]; 00220 } 00221 00222 if( m_lockZ ) 00223 { 00224 m_position[2] = m_lockPoint[2]; 00225 } 00226 00227 if( m_lockOnVector ) 00228 { 00229 float k = ( ( m_lockPoint[0] * m_lockVector[0] ) - ( m_position.x() * m_lockVector[0] ) + 00230 ( m_lockPoint[1] * m_lockVector[1] ) - ( m_position.y() * m_lockVector[1] ) + 00231 ( m_lockPoint[2] * m_lockVector[2] ) - ( m_position.z() * m_lockVector[2] ) ) / 00232 ( m_lockVector[0] * m_lockVector[0] + m_lockVector[1] * m_lockVector[1] + m_lockVector[2] * m_lockVector[2] ) * -1.0; 00233 m_position = m_lockPoint + ( m_lockVector * k ); 00234 } 00235 } 00236 00237 void WROISphere::setLockX( bool value ) 00238 { 00239 m_lockX = value; 00240 m_lockPoint = m_position; 00241 } 00242 00243 void WROISphere::setLockY( bool value ) 00244 { 00245 m_lockY = value; 00246 m_lockPoint = m_position; 00247 } 00248 00249 void WROISphere::setLockZ( bool value ) 00250 { 00251 m_lockZ = value; 00252 m_lockPoint = m_position; 00253 } 00254 00255 00256 void WROISphere::setColor( osg::Vec4 color ) 00257 { 00258 m_color = color; 00259 redrawSphere(); 00260 } 00261 00262 void WROISphere::setNotColor( osg::Vec4 color ) 00263 { 00264 m_notColor = color; 00265 } 00266 00267 void WROISphere::setLockVector( WVector3d vector ) 00268 { 00269 m_lockVector = vector; 00270 m_lockPoint = m_position; 00271 } 00272 00273 void WROISphere::setLockOnVector( bool value ) 00274 { 00275 m_lockOnVector = value; 00276 }