OpenWalnut  1.4.0
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  m_axialUpdateConnection = m_axialPos->getUpdateCondition()->subscribeSignal(
60  boost::bind( &WSelectionManager::updateCrosshairPosition, this )
61  );
62  m_coronalUpdateConnection = m_coronalPos->getUpdateCondition()->subscribeSignal(
63  boost::bind( &WSelectionManager::updateCrosshairPosition, this )
64  );
65  m_sagittalUpdateConnection = m_sagittalPos->getUpdateCondition()->subscribeSignal(
66  boost::bind( &WSelectionManager::updateCrosshairPosition, this )
67  );
68 }
69 
71 {
72 }
73 
74 boost::shared_ptr< WCrosshair >WSelectionManager::getCrosshair()
75 {
76  return m_crosshair;
77 }
78 
80 {
81  boost::shared_ptr< WGEViewer > viewer;
82  viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "Main View" );
83  viewer->getCamera()->getViewMatrix();
84  osg::Matrix rm = viewer->getCamera()->getViewMatrix();
85 
86  WMatrix< double > rotMat( 4, 4 );
87  for( size_t i = 0; i < 4; ++i )
88  {
89  for( size_t j = 0; j < 4; ++j )
90  {
91  rotMat( i, j ) = rm( i, j );
92  }
93  }
94  WPosition v1( 0, 0, 1 );
95  WPosition view;
96  view = transformPosition3DWithMatrix4D( rotMat, v1 );
97 
98  std::vector<float> dots( 8 );
99  WPosition v2( 1, 1, 1 );
100  dots[0] = dot( v2, view );
101 
102  v2[2] = -1;
103  dots[1] = dot( v2, view );
104 
105  v2[1] = -1;
106  dots[2] = dot( v2, view );
107 
108  v2[2] = 1;
109  dots[3] = dot( v2, view );
110 
111  v2[0] = -1;
112  dots[4] = dot( v2, view );
113 
114  v2[2] = -1;
115  dots[5] = dot( v2, view );
116 
117  v2[1] = 1;
118  dots[6] = dot( v2, view );
119 
120  v2[2] = 1;
121  dots[7] = dot( v2, view );
122 
123  float max = 0.0;
124  int quadrant = 0;
125  for( int i = 0; i < 8; ++i )
126  {
127  if( dots[i] > max )
128  {
129  max = dots[i];
130  quadrant = i;
131  }
132  }
133  return quadrant;
134 }
135 
136 void WSelectionManager::setPaintMode( WPaintMode mode )
137 {
138  m_paintMode = mode;
139 
140  osg::static_pointer_cast<WGEZoomTrackballManipulator>(
141  WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getCameraManipulator() )->setPaintMode( mode );
142  WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getPickHandler()->setPaintMode( mode );
143 }
144 
146 {
147  return m_paintMode;
148 }
149 
150 void WSelectionManager::setTexture( osg::ref_ptr< osg::Texture3D > texture, boost::shared_ptr< WGridRegular3D >grid )
151 {
152  m_texture = texture;
153  m_textureGrid = grid;
154 }
155 
156 
157 boost::shared_ptr< WGridRegular3D >WSelectionManager::getGrid()
158 {
159  return m_textureGrid;
160 }
161 
163 {
164  m_useTexture = flag;
165 }
166 
168 {
169  return m_useTexture;
170 }
171 
172 
174 {
175  return m_textureOpacity;
176 }
177 
179 {
180  if( value < 0.0 )
181  {
182  value = 0.0;
183  }
184  if( value > 1.0 )
185  {
186  value = 1.0;
187  }
188  m_textureOpacity = value;
189 }
190 
192 {
193  return m_axialPos;
194 }
195 
197 {
198  return m_coronalPos;
199 }
200 
202 {
203  return m_sagittalPos;
204 }
205 
207 {
208  return m_axialShow;
209 }
210 
212 {
213  return m_coronalShow;
214 }
215 
217 {
218  return m_sagittalShow;
219 }
220 
222 {
223  m_shader = shader;
224 }
225 
227 {
228  return m_shader;
229 }
230 
232 {
233  m_crosshair->setPosition( WPosition( m_sagittalPos->get(), m_coronalPos->get(), m_axialPos->get() ) );
234 }
235