WSelectionManager.cpp 6.16 KB
Newer Older
schurade's avatar
schurade committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25 26 27
#include <vector>

#include <osg/Matrix>
28
#include <osg/Texture3D>
29 30

#include "../common/math/WLinearAlgebraFunctions.h"
31
#include "../graphicsEngine/WGEViewer.h"
32
#include "../graphicsEngine/WGEZoomTrackballManipulator.h"
33
#include "../graphicsEngine/WGraphicsEngine.h"
34
#include "../graphicsEngine/WPickHandler.h"
35
#include "WKernel.h"
36

schurade's avatar
schurade committed
37 38
#include "WSelectionManager.h"

39

40
WSelectionManager::WSelectionManager() :
41 42 43
    m_paintMode( PAINTMODE_NONE ),
    m_textureOpacity( 1.0 ),
    m_useTexture( false )
schurade's avatar
schurade committed
44 45
{
    m_crosshair = boost::shared_ptr< WCrosshair >( new WCrosshair() );
46 47 48 49 50

    m_sliceGroup = boost::shared_ptr< WProperties >( new WProperties( "Slice Properties",
                    "Properties relating to the Axial,Coronal and Sagittal Slices." ) );

    // create dummy properties for slices. Get updated by modules.
51 52 53 54 55 56 57 58
    m_axialPos = m_sliceGroup->addProperty( "Axial Position", "Slice X position.", 0.0, true );
    m_coronalPos = m_sliceGroup->addProperty( "Coronal Position", "Slice Y position.", 0.0, true );
    m_sagittalPos = m_sliceGroup->addProperty( "Sagittal Position", "Slice Z position.", 0.0, true );

    // visibility flags
    m_axialShow = m_sliceGroup->addProperty( "Show Axial Slice", "Slice visible?", true, true );
    m_coronalShow = m_sliceGroup->addProperty( "Show Coronal Slice", "Slice visible?", true, true );
    m_sagittalShow = m_sliceGroup->addProperty( "Show Sagittal Slice", "Slice visible?", true, true );
59 60 61 62 63 64 65 66 67 68

    m_axialUpdateConnection = m_axialPos->getUpdateCondition()->subscribeSignal(
        boost::bind( &WSelectionManager::updateCrosshairPosition, this )
    );
    m_coronalUpdateConnection = m_coronalPos->getUpdateCondition()->subscribeSignal(
        boost::bind( &WSelectionManager::updateCrosshairPosition, this )
    );
    m_sagittalUpdateConnection = m_sagittalPos->getUpdateCondition()->subscribeSignal(
        boost::bind( &WSelectionManager::updateCrosshairPosition, this )
    );
schurade's avatar
schurade committed
69 70 71 72 73 74 75 76 77 78
}

WSelectionManager::~WSelectionManager()
{
}

boost::shared_ptr< WCrosshair >WSelectionManager::getCrosshair()
{
    return m_crosshair;
}
79 80 81 82

int WSelectionManager::getFrontSector()
{
    boost::shared_ptr< WGEViewer > viewer;
83
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "Main View" );
84 85 86 87 88 89 90 91 92 93 94
    viewer->getCamera()->getViewMatrix();
    osg::Matrix rm = viewer->getCamera()->getViewMatrix();

    WMatrix< double > rotMat( 4, 4 );
    for( size_t i = 0; i < 4; ++i )
    {
        for( size_t j = 0; j < 4; ++j )
        {
            rotMat( i, j ) = rm( i, j );
        }
    }
95 96
    WPosition v1( 0, 0, 1 );
    WPosition view;
97 98 99
    view = transformPosition3DWithMatrix4D( rotMat, v1 );

    std::vector<float> dots( 8 );
100
    WPosition v2( 1, 1, 1 );
101
    dots[0] = dot( v2, view );
102 103

    v2[2] = -1;
104
    dots[1] = dot( v2, view );
105 106

    v2[1] = -1;
107
    dots[2] = dot( v2, view );
108 109

    v2[2] = 1;
110
    dots[3] = dot( v2, view );
111 112

    v2[0] = -1;
113
    dots[4] = dot( v2, view );
114 115

    v2[2] = -1;
116
    dots[5] = dot( v2, view );
117 118

    v2[1] = 1;
119
    dots[6] = dot( v2, view );
120 121

    v2[2] = 1;
122
    dots[7] = dot( v2, view );
123 124 125

    float max = 0.0;
    int quadrant = 0;
126
    for( int i = 0; i < 8; ++i )
127
    {
128
        if( dots[i] > max )
129 130 131 132 133 134 135
        {
            max = dots[i];
            quadrant = i;
        }
    }
    return quadrant;
}
136 137 138 139 140 141 142

void WSelectionManager::setPaintMode( WPaintMode mode )
{
    m_paintMode = mode;

    osg::static_pointer_cast<WGEZoomTrackballManipulator>(
            WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getCameraManipulator() )->setPaintMode( mode );
143
    WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getPickHandler()->setPaintMode( mode == PAINTMODE_PAINT );
144 145 146 147 148 149 150
}

WPaintMode WSelectionManager::getPaintMode()
{
    return m_paintMode;
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
void WSelectionManager::setTexture( osg::ref_ptr< osg::Texture3D > texture, boost::shared_ptr< WGridRegular3D >grid )
{
    m_texture = texture;
    m_textureGrid = grid;
}


boost::shared_ptr< WGridRegular3D >WSelectionManager::getGrid()
{
    return m_textureGrid;
}

void WSelectionManager::setUseTexture( bool flag )
{
    m_useTexture = flag;
}

bool WSelectionManager::getUseTexture()
{
    return m_useTexture;
}


float WSelectionManager::getTextureOpacity()
{
    return m_textureOpacity;
}

void WSelectionManager::setTextureOpacity( float value )
{
181
    if( value < 0.0 )
182 183 184
    {
        value = 0.0;
    }
185
    if( value > 1.0 )
186 187 188 189 190
    {
        value = 1.0;
    }
    m_textureOpacity = value;
}
191

192
WPropDouble WSelectionManager::getPropAxialPos()
193 194 195 196
{
    return m_axialPos;
}

197
WPropDouble WSelectionManager::getPropCoronalPos()
198 199 200 201
{
    return m_coronalPos;
}

202
WPropDouble WSelectionManager::getPropSagittalPos()
203 204 205 206
{
    return m_sagittalPos;
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
WPropBool WSelectionManager::getPropAxialShow()
{
    return m_axialShow;
}

WPropBool WSelectionManager::getPropCoronalShow()
{
    return m_coronalShow;
}

WPropBool WSelectionManager::getPropSagittalShow()
{
    return m_sagittalShow;
}

222 223 224 225 226 227 228 229 230
void WSelectionManager::setShader( int shader )
{
    m_shader = shader;
}

int WSelectionManager::getShader()
{
    return m_shader;
}
231 232 233

void WSelectionManager::updateCrosshairPosition()
{
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
234
    m_crosshair->setPosition( WPosition( m_sagittalPos->get(), m_coronalPos->get(), m_axialPos->get() ) );
235 236
}