OpenWalnut 1.3.1
WSelectionManager.cpp
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 <vector>
00026 
00027 #include <osg/Matrix>
00028 
00029 #include "../common/math/WLinearAlgebraFunctions.h"
00030 #include "../graphicsEngine/WGEViewer.h"
00031 #include "../graphicsEngine/WGEZoomTrackballManipulator.h"
00032 #include "../graphicsEngine/WGraphicsEngine.h"
00033 #include "../graphicsEngine/WPickHandler.h"
00034 #include "WKernel.h"
00035 
00036 #include "WSelectionManager.h"
00037 
00038 
00039 WSelectionManager::WSelectionManager() :
00040     m_paintMode( PAINTMODE_NONE ),
00041     m_textureOpacity( 1.0 ),
00042     m_useTexture( false )
00043 {
00044     m_crosshair = boost::shared_ptr< WCrosshair >( new WCrosshair() );
00045 
00046     m_sliceGroup = boost::shared_ptr< WProperties >( new WProperties( "Slice Properties",
00047                     "Properties relating to the Axial,Coronal and Sagittal Slices." ) );
00048 
00049     // create dummy properties for slices. Get updated by modules.
00050     m_axialPos = m_sliceGroup->addProperty( "Axial Position", "Slice X position.", 0.0, true );
00051     m_coronalPos = m_sliceGroup->addProperty( "Coronal Position", "Slice Y position.", 0.0, true );
00052     m_sagittalPos = m_sliceGroup->addProperty( "Sagittal Position", "Slice Z position.", 0.0, true );
00053 
00054     // visibility flags
00055     m_axialShow = m_sliceGroup->addProperty( "Show Axial Slice", "Slice visible?", true, true );
00056     m_coronalShow = m_sliceGroup->addProperty( "Show Coronal Slice", "Slice visible?", true, true );
00057     m_sagittalShow = m_sliceGroup->addProperty( "Show Sagittal Slice", "Slice visible?", true, true );
00058 
00059     // until now, no bbox information is available.
00060     m_axialPos->setMin( 0.0 );
00061     m_coronalPos->setMin( 0.0 );
00062     m_sagittalPos->setMin( 0.0 );
00063     m_axialPos->setMax( 0.0 );
00064     m_coronalPos->setMax( 0.0 );
00065     m_sagittalPos->setMax( 0.0 );
00066 
00067     m_axialUpdateConnection = m_axialPos->getUpdateCondition()->subscribeSignal(
00068         boost::bind( &WSelectionManager::updateCrosshairPosition, this )
00069     );
00070     m_coronalUpdateConnection = m_coronalPos->getUpdateCondition()->subscribeSignal(
00071         boost::bind( &WSelectionManager::updateCrosshairPosition, this )
00072     );
00073     m_sagittalUpdateConnection = m_sagittalPos->getUpdateCondition()->subscribeSignal(
00074         boost::bind( &WSelectionManager::updateCrosshairPosition, this )
00075     );
00076 }
00077 
00078 WSelectionManager::~WSelectionManager()
00079 {
00080 }
00081 
00082 boost::shared_ptr< WCrosshair >WSelectionManager::getCrosshair()
00083 {
00084     return m_crosshair;
00085 }
00086 
00087 int WSelectionManager::getFrontSector()
00088 {
00089     boost::shared_ptr< WGEViewer > viewer;
00090     viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "Main View" );
00091     viewer->getCamera()->getViewMatrix();
00092     osg::Matrix rm = viewer->getCamera()->getViewMatrix();
00093 
00094     WMatrix< double > rotMat( 4, 4 );
00095     for( size_t i = 0; i < 4; ++i )
00096     {
00097         for( size_t j = 0; j < 4; ++j )
00098         {
00099             rotMat( i, j ) = rm( i, j );
00100         }
00101     }
00102     WPosition v1( 0, 0, 1 );
00103     WPosition view;
00104     view = transformPosition3DWithMatrix4D( rotMat, v1 );
00105 
00106     std::vector<float> dots( 8 );
00107     WPosition v2( 1, 1, 1 );
00108     dots[0] = dot( v2, view );
00109 
00110     v2[2] = -1;
00111     dots[1] = dot( v2, view );
00112 
00113     v2[1] = -1;
00114     dots[2] = dot( v2, view );
00115 
00116     v2[2] = 1;
00117     dots[3] = dot( v2, view );
00118 
00119     v2[0] = -1;
00120     dots[4] = dot( v2, view );
00121 
00122     v2[2] = -1;
00123     dots[5] = dot( v2, view );
00124 
00125     v2[1] = 1;
00126     dots[6] = dot( v2, view );
00127 
00128     v2[2] = 1;
00129     dots[7] = dot( v2, view );
00130 
00131     float max = 0.0;
00132     int quadrant = 0;
00133     for( int i = 0; i < 8; ++i )
00134     {
00135         if( dots[i] > max )
00136         {
00137             max = dots[i];
00138             quadrant = i;
00139         }
00140     }
00141     return quadrant;
00142 }
00143 
00144 void WSelectionManager::setPaintMode( WPaintMode mode )
00145 {
00146     m_paintMode = mode;
00147 
00148     osg::static_pointer_cast<WGEZoomTrackballManipulator>(
00149             WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getCameraManipulator() )->setPaintMode( mode );
00150     WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getPickHandler()->setPaintMode( mode );
00151 }
00152 
00153 WPaintMode WSelectionManager::getPaintMode()
00154 {
00155     return m_paintMode;
00156 }
00157 
00158 void WSelectionManager::setTexture( osg::ref_ptr< osg::Texture3D > texture, boost::shared_ptr< WGridRegular3D >grid )
00159 {
00160     m_texture = texture;
00161     m_textureGrid = grid;
00162 }
00163 
00164 
00165 boost::shared_ptr< WGridRegular3D >WSelectionManager::getGrid()
00166 {
00167     return m_textureGrid;
00168 }
00169 
00170 void WSelectionManager::setUseTexture( bool flag )
00171 {
00172     m_useTexture = flag;
00173 }
00174 
00175 bool WSelectionManager::getUseTexture()
00176 {
00177     return m_useTexture;
00178 }
00179 
00180 
00181 float WSelectionManager::getTextureOpacity()
00182 {
00183     return m_textureOpacity;
00184 }
00185 
00186 void WSelectionManager::setTextureOpacity( float value )
00187 {
00188     if( value < 0.0 )
00189     {
00190         value = 0.0;
00191     }
00192     if( value > 1.0 )
00193     {
00194         value = 1.0;
00195     }
00196     m_textureOpacity = value;
00197 }
00198 
00199 WPropDouble WSelectionManager::getPropAxialPos()
00200 {
00201     return m_axialPos;
00202 }
00203 
00204 WPropDouble WSelectionManager::getPropCoronalPos()
00205 {
00206     return m_coronalPos;
00207 }
00208 
00209 WPropDouble WSelectionManager::getPropSagittalPos()
00210 {
00211     return m_sagittalPos;
00212 }
00213 
00214 WPropBool WSelectionManager::getPropAxialShow()
00215 {
00216     return m_axialShow;
00217 }
00218 
00219 WPropBool WSelectionManager::getPropCoronalShow()
00220 {
00221     return m_coronalShow;
00222 }
00223 
00224 WPropBool WSelectionManager::getPropSagittalShow()
00225 {
00226     return m_sagittalShow;
00227 }
00228 
00229 void WSelectionManager::setShader( int shader )
00230 {
00231     m_shader = shader;
00232 }
00233 
00234 int WSelectionManager::getShader()
00235 {
00236     return m_shader;
00237 }
00238 
00239 void WSelectionManager::updateCrosshairPosition()
00240 {
00241     m_crosshair->setPosition( WPosition( m_sagittalPos->get(), m_coronalPos->get(), m_axialPos->get() ) );
00242 }
00243