OpenWalnut  1.3.1
WSelectionManager.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 <vector>
26 
27 #include <osg/Matrix>
28 
29 #include "../common/math/WLinearAlgebraFunctions.h"
30 #include "../graphicsEngine/WGEViewer.h"
31 #include "../graphicsEngine/WGEZoomTrackballManipulator.h"
32 #include "../graphicsEngine/WGraphicsEngine.h"
33 #include "../graphicsEngine/WPickHandler.h"
34 #include "WKernel.h"
35 
36 #include "WSelectionManager.h"
37 
38 
40  m_paintMode( PAINTMODE_NONE ),
41  m_textureOpacity( 1.0 ),
42  m_useTexture( false )
43 {
44  m_crosshair = boost::shared_ptr< WCrosshair >( new WCrosshair() );
45 
46  m_sliceGroup = boost::shared_ptr< WProperties >( new WProperties( "Slice Properties",
47  "Properties relating to the Axial,Coronal and Sagittal Slices." ) );
48 
49  // create dummy properties for slices. Get updated by modules.
50  m_axialPos = m_sliceGroup->addProperty( "Axial Position", "Slice X position.", 0.0, true );
51  m_coronalPos = m_sliceGroup->addProperty( "Coronal Position", "Slice Y position.", 0.0, true );
52  m_sagittalPos = m_sliceGroup->addProperty( "Sagittal Position", "Slice Z position.", 0.0, true );
53 
54  // visibility flags
55  m_axialShow = m_sliceGroup->addProperty( "Show Axial Slice", "Slice visible?", true, true );
56  m_coronalShow = m_sliceGroup->addProperty( "Show Coronal Slice", "Slice visible?", true, true );
57  m_sagittalShow = m_sliceGroup->addProperty( "Show Sagittal Slice", "Slice visible?", true, true );
58 
59  // until now, no bbox information is available.
60  m_axialPos->setMin( 0.0 );
61  m_coronalPos->setMin( 0.0 );
62  m_sagittalPos->setMin( 0.0 );
63  m_axialPos->setMax( 0.0 );
64  m_coronalPos->setMax( 0.0 );
65  m_sagittalPos->setMax( 0.0 );
66 
67  m_axialUpdateConnection = m_axialPos->getUpdateCondition()->subscribeSignal(
68  boost::bind( &WSelectionManager::updateCrosshairPosition, this )
69  );
70  m_coronalUpdateConnection = m_coronalPos->getUpdateCondition()->subscribeSignal(
71  boost::bind( &WSelectionManager::updateCrosshairPosition, this )
72  );
73  m_sagittalUpdateConnection = m_sagittalPos->getUpdateCondition()->subscribeSignal(
74  boost::bind( &WSelectionManager::updateCrosshairPosition, this )
75  );
76 }
77 
79 {
80 }
81 
82 boost::shared_ptr< WCrosshair >WSelectionManager::getCrosshair()
83 {
84  return m_crosshair;
85 }
86 
88 {
89  boost::shared_ptr< WGEViewer > viewer;
90  viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "Main View" );
91  viewer->getCamera()->getViewMatrix();
92  osg::Matrix rm = viewer->getCamera()->getViewMatrix();
93 
94  WMatrix< double > rotMat( 4, 4 );
95  for( size_t i = 0; i < 4; ++i )
96  {
97  for( size_t j = 0; j < 4; ++j )
98  {
99  rotMat( i, j ) = rm( i, j );
100  }
101  }
102  WPosition v1( 0, 0, 1 );
103  WPosition view;
104  view = transformPosition3DWithMatrix4D( rotMat, v1 );
105 
106  std::vector<float> dots( 8 );
107  WPosition v2( 1, 1, 1 );
108  dots[0] = dot( v2, view );
109 
110  v2[2] = -1;
111  dots[1] = dot( v2, view );
112 
113  v2[1] = -1;
114  dots[2] = dot( v2, view );
115 
116  v2[2] = 1;
117  dots[3] = dot( v2, view );
118 
119  v2[0] = -1;
120  dots[4] = dot( v2, view );
121 
122  v2[2] = -1;
123  dots[5] = dot( v2, view );
124 
125  v2[1] = 1;
126  dots[6] = dot( v2, view );
127 
128  v2[2] = 1;
129  dots[7] = dot( v2, view );
130 
131  float max = 0.0;
132  int quadrant = 0;
133  for( int i = 0; i < 8; ++i )
134  {
135  if( dots[i] > max )
136  {
137  max = dots[i];
138  quadrant = i;
139  }
140  }
141  return quadrant;
142 }
143 
144 void WSelectionManager::setPaintMode( WPaintMode mode )
145 {
146  m_paintMode = mode;
147 
148  osg::static_pointer_cast<WGEZoomTrackballManipulator>(
149  WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getCameraManipulator() )->setPaintMode( mode );
150  WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getPickHandler()->setPaintMode( mode );
151 }
152 
154 {
155  return m_paintMode;
156 }
157 
158 void WSelectionManager::setTexture( osg::ref_ptr< osg::Texture3D > texture, boost::shared_ptr< WGridRegular3D >grid )
159 {
160  m_texture = texture;
161  m_textureGrid = grid;
162 }
163 
164 
165 boost::shared_ptr< WGridRegular3D >WSelectionManager::getGrid()
166 {
167  return m_textureGrid;
168 }
169 
171 {
172  m_useTexture = flag;
173 }
174 
176 {
177  return m_useTexture;
178 }
179 
180 
182 {
183  return m_textureOpacity;
184 }
185 
187 {
188  if( value < 0.0 )
189  {
190  value = 0.0;
191  }
192  if( value > 1.0 )
193  {
194  value = 1.0;
195  }
196  m_textureOpacity = value;
197 }
198 
200 {
201  return m_axialPos;
202 }
203 
205 {
206  return m_coronalPos;
207 }
208 
210 {
211  return m_sagittalPos;
212 }
213 
215 {
216  return m_axialShow;
217 }
218 
220 {
221  return m_coronalShow;
222 }
223 
225 {
226  return m_sagittalShow;
227 }
228 
230 {
231  m_shader = shader;
232 }
233 
235 {
236  return m_shader;
237 }
238 
240 {
241  m_crosshair->setPosition( WPosition( m_sagittalPos->get(), m_coronalPos->get(), m_axialPos->get() ) );
242 }
243