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