~ubuntu-branches/ubuntu/wily/ginkgocadx/wily-proposed

« back to all changes in this revision

Viewing changes to src/cadxcore/vtk/command/ginkgoimagecommand.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Tille
  • Date: 2011-05-02 08:09:26 UTC
  • Revision ID: james.westby@ubuntu.com-20110502080926-bql5wep49c7hg91t
Tags: upstream-2.4.1.1
ImportĀ upstreamĀ versionĀ 2.4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  
 
3
 *  $Id$
 
4
 *  Ginkgo CADx Project
 
5
 *
 
6
 *  Code adapted from vtkINRIA3D
 
7
=========================================================================
 
8
 
 
9
Program:   vtkINRIA3D
 
10
Module:    $Id: GinkgoImageCommand.cpp 1297 2010-02-16 12:30:41Z carlos $
 
11
Language:  C++
 
12
Author:    $Author: filus $
 
13
Date:      $Date: 2008-06-06 18:10:57 +0200 (vie, 06 jun 2008) $
 
14
Version:   $Revision: 880 $
 
15
 
 
16
Copyright (c) 2007 INRIA - Asclepios Project. All rights reserved.
 
17
See Copyright.txt for details.
 
18
 
 
19
This software is distributed WITHOUT ANY WARRANTY; without even
 
20
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
21
PURPOSE.  See the above copyright notices for more information.
 
22
 
 
23
=========================================================================*/
 
24
#ifdef __DEPRECATED
 
25
#undef __DEPRECATED
 
26
#endif
 
27
#ifdef _MSC_VER
 
28
#  pragma warning (disable : 4018)
 
29
#endif
 
30
 
 
31
#include <string.h>
 
32
#include "../interactor/ginkgointeractorstyleimage2d.h"
 
33
#include <vtkginkgoimageviewer.h>
 
34
 
 
35
#include <vtkAbstractPropPicker.h>
 
36
#include <vtkRenderer.h>
 
37
#include <vtkRenderWindow.h>
 
38
#include <vtkRenderWindowInteractor.h>
 
39
#include <vtkImageData.h>
 
40
 
 
41
 
 
42
#include "ginkgoimagecommand.h"
 
43
 
 
44
 
 
45
GinkgoImageCommand::GinkgoImageCommand() :
 
46
  View(NULL), InitialWindow(0), InitialLevel(0),
 
47
  WindowEventStatus(false)
 
48
{
 
49
}
 
50
 
 
51
void
 
52
GinkgoImageCommand::Execute(vtkObject*    caller,
 
53
                                 unsigned long event,
 
54
                                 void*         /*callData*/)
 
55
{
 
56
  if (this->View->GetInput() == NULL){
 
57
    return;
 
58
  }
 
59
 
 
60
  if (event == vtkCommand::KeyPressEvent)
 
61
  {
 
62
 
 
63
    vtkRenderWindowInteractor *rwi = this->View->GetRenderWindow()->GetInteractor();
 
64
    char key = rwi->GetKeyCode();
 
65
 
 
66
    if ( key=='i' || key=='I')
 
67
    {
 
68
      //int mode;
 
69
      //mode = this->View->GetInterpolationMode();
 
70
      //this->View->SetInterpolationMode((++mode)%2);
 
71
      //this->View->Render();
 
72
      return;
 
73
    }
 
74
 
 
75
    return;
 
76
  }
 
77
 
 
78
  if (event == vtkCommand::StartWindowLevelEvent) {
 
79
    this->StartWindowing();
 
80
    return;
 
81
  }
 
82
  if (event == vtkCommand::EndWindowLevelEvent) {
 
83
    this->EndWindowing();
 
84
    return;
 
85
  }
 
86
  if (event == vtkCommand::WindowLevelEvent) {
 
87
    this->Windowing(GinkgoInteractorStyleImage2D::SafeDownCast(caller));
 
88
    return;
 
89
  }
 
90
  if (event == vtkCommand::ResetWindowLevelEvent) {
 
91
    //this->View->SyncResetWindowLevel();
 
92
    //std::cout << "GinkgoImageCommand::ResetWindowLevelEvent()" << std::endl;
 
93
    this->View->ResetToDefaultWindowLevel();
 
94
    this->View->SyncSetWindow ( this->View->GetWindow() );
 
95
    this->View->SyncSetLevel ( this->View->GetLevel() );
 
96
    this->View->Render();
 
97
    return;
 
98
  }
 
99
  if (event == GinkgoImageCommand::ResetPositionEvent) {
 
100
         //std::cout << "GinkgoImageCommand::ResetPositionEvent()" << std::endl;
 
101
    //this->View->SyncResetCurrentPoint();
 
102
    //this->View->Render();
 
103
    return;
 
104
  }
 
105
  if (event == GinkgoImageCommand::ZoomEvent) {
 
106
         //std::cout << "GinkgoImageCommand::ZoomEvent()" << std::endl;
 
107
    this->Zoom(GinkgoInteractorStyleImage2D::SafeDownCast(caller));
 
108
    return;
 
109
  }
 
110
  if (event == GinkgoImageCommand::ResetZoomEvent) {
 
111
         //std::cout << "GinkgoImageCommand::ResetZoomEvent()" << std::endl;
 
112
    //this->View->SyncResetZoom();
 
113
    //this->View->Render();
 
114
    return;
 
115
  }
 
116
  if (event == vtkCommand::StartPickEvent) {
 
117
    this->StartPicking(vtkInteractorStyleImage::SafeDownCast(caller));
 
118
    return;
 
119
  }
 
120
  if (event == vtkCommand::EndPickEvent) {
 
121
    this->EndPicking(/*static_cast<vtkInteractorStyleImage *>(caller)*/);
 
122
    return;
 
123
  }
 
124
  if (event == GinkgoImageCommand::StartMeasureEvent) {
 
125
    //std::cout<<"StartMeasureEvent\n";
 
126
    return;
 
127
  }
 
128
  if (event == GinkgoImageCommand::MeasureEvent) {
 
129
    //std::cout<<"MeasureEvent\n";
 
130
    return;
 
131
  }
 
132
  if (event == GinkgoImageCommand::EndMeasureEvent) {
 
133
    //std::cout<<"EndMeasureEvent\n";
 
134
    return;
 
135
  }
 
136
  if (event == GinkgoImageCommand::StartZSliceMoveEvent) {
 
137
    return;
 
138
  }
 
139
  if (event == GinkgoImageCommand::ZSliceMoveEvent) {
 
140
    this->ChangeZSlice(GinkgoInteractorStyleImage2D::SafeDownCast(caller));
 
141
    return;
 
142
  }
 
143
  if (event == GinkgoImageCommand::EndZSliceMoveEvent) {
 
144
    return;
 
145
  }
 
146
  if (event == GinkgoImageCommand::FullPageEvent) {
 
147
    return;
 
148
  }
 
149
 
 
150
}
 
151
 
 
152
void
 
153
GinkgoImageCommand::StartWindowing()
 
154
{
 
155
  this->InitialWindow = this->View->GetColorWindow();
 
156
  this->InitialLevel  = this->View->GetColorLevel();
 
157
}
 
158
 
 
159
void
 
160
GinkgoImageCommand::Windowing(GinkgoInteractorStyleImage2D* p_isi)
 
161
{
 
162
 
 
163
  if( !p_isi )
 
164
  {
 
165
    return;
 
166
  }
 
167
 
 
168
  double window = this->InitialWindow;
 
169
  double level  = this->InitialLevel;
 
170
  double EPS    = 0.01;
 
171
 
 
172
 
 
173
  double dx = p_isi->GetWindowStep();
 
174
  double dy = p_isi->GetLevelStep();
 
175
 
 
176
  // Scale by current values
 
177
  if (fabs(window) > EPS)
 
178
  {
 
179
    dx = dx * window;
 
180
  }
 
181
  else
 
182
  {
 
183
    dx = dx * (window < 0 ? -EPS : EPS);
 
184
  }
 
185
 
 
186
  if (fabs(level) > EPS)
 
187
  {
 
188
    dy = dy * level;
 
189
  }
 
190
  else
 
191
  {
 
192
    dy = dy * (level < 0 ? -EPS : EPS);
 
193
  }
 
194
 
 
195
  // Abs so that direction does not flip
 
196
  if (window < 0.0f)
 
197
  {
 
198
    dx = -1*dx;
 
199
  }
 
200
  if (level < 0.0f)
 
201
  {
 
202
    dy = -1*dy;
 
203
  }
 
204
 
 
205
  // Compute new window level
 
206
  double newWindow = dx + window;
 
207
  double newLevel  = level - dy;
 
208
 
 
209
  // Stay away from zero and really
 
210
  if (fabs(newWindow) < EPS)
 
211
  {
 
212
    newWindow = EPS * (newWindow < 0 ? -1 : 1);
 
213
  }
 
214
 
 
215
  if (fabs(newLevel) < EPS)
 
216
  {
 
217
    newLevel = EPS * (newLevel < 0 ? -1 : 1);
 
218
  }
 
219
 
 
220
  this->View->SyncSetWindow(newWindow);
 
221
  this->View->SyncSetLevel(newLevel);
 
222
  this->View->Modified();
 
223
}
 
224
 
 
225
void
 
226
GinkgoImageCommand::EndWindowing()
 
227
{
 
228
}
 
229
 
 
230
void
 
231
GinkgoImageCommand::StartPicking(vtkInteractorStyleImage* p_isi)
 
232
{
 
233
  vtkRenderWindowInteractor *rwi = p_isi->GetInteractor();
 
234
  vtkAssemblyPath *path=NULL;
 
235
  p_isi->FindPokedRenderer(rwi->GetEventPosition()[0],
 
236
                           rwi->GetEventPosition()[1]);
 
237
  rwi->GetPicker()->Pick(rwi->GetEventPosition()[0],
 
238
                         rwi->GetEventPosition()[1], 0.0,
 
239
                         p_isi->GetCurrentRenderer());
 
240
  vtkAbstractPropPicker *picker;
 
241
  if ( (picker=vtkAbstractPropPicker::SafeDownCast(rwi->GetPicker())) )
 
242
  {
 
243
    path = picker->GetPath();
 
244
  }
 
245
 
 
246
  if ( path != NULL )
 
247
  {
 
248
    double* world = picker->GetPickPosition();
 
249
        
 
250
        double  pos[3] = {0.0, 0.0, 0.0};
 
251
    //this->View->GetCurrentPoint(pos);
 
252
 
 
253
    //double* imBounds = this->View->GetImage()->GetBounds();
 
254
 
 
255
    switch(this->View->GetOrientation())
 
256
    {
 
257
        case vtkGinkgoImageViewer::SAGITTAL_ID:
 
258
 
 
259
          pos [1] = world[0];//*1.0;
 
260
          pos [2] = world[1];//*1.0;
 
261
          break;
 
262
 
 
263
 
 
264
        case vtkGinkgoImageViewer::CORONAL_ID:
 
265
 
 
266
          if( this->View->GetConventions() == vtkGinkgoImageViewer::RADIOLOGIC )
 
267
          {
 
268
            pos [0] = world[0];//*-1.0;
 
269
          }
 
270
          else
 
271
          {
 
272
            pos [0] = world[0]*-1.0;
 
273
          }
 
274
          pos [2] = world[1];
 
275
          break;
 
276
 
 
277
 
 
278
        case vtkGinkgoImageViewer::AXIAL_ID:
 
279
 
 
280
          if( this->View->GetConventions() == vtkGinkgoImageViewer::RADIOLOGIC )
 
281
          {
 
282
            pos [0] = world[0];
 
283
          }
 
284
          else
 
285
          {
 
286
            pos [0] = world[0]*-1.0;
 
287
          }
 
288
          pos [1] = world[1]*-1.0;
 
289
          break;
 
290
 
 
291
    }
 
292
 
 
293
    // Treat extrem positions
 
294
        int range[3][2] = {{0, 0}, {0, 0}, {0, 0}};
 
295
        this->View->GetSliceRange(vtkGinkgoImageViewer::X, range[0]);
 
296
        this->View->GetSliceRange(vtkGinkgoImageViewer::Y, range[1]);
 
297
        this->View->GetSliceRange(vtkGinkgoImageViewer::Z, range[2]);
 
298
 
 
299
    for (unsigned int i=0; i<3; i++)
 
300
    {
 
301
                if (pos[i] < range[i][0]) {
 
302
                        pos[i] = range[i][0] + 0.0005;
 
303
                }
 
304
                if (pos[i] > range[i][1]) {
 
305
                        pos[i] = range[i][1] - 0.0005;
 
306
                }
 
307
    }
 
308
 
 
309
    // Set the position
 
310
    //this->View->SyncSetCurrentPoint(pos);
 
311
    //this->View->Modified();
 
312
  }
 
313
 
 
314
}
 
315
 
 
316
void
 
317
GinkgoImageCommand::EndPicking()
 
318
{
 
319
}
 
320
 
 
321
void
 
322
GinkgoImageCommand::ChangeZSlice(GinkgoInteractorStyleImage2D* /*p_isi*/)
 
323
{
 
324
 
 
325
        /*
 
326
  if( !p_isi )
 
327
  {
 
328
    return;
 
329
  }
 
330
 
 
331
  int p_nbSlices = p_isi->GetZSliceStep();
 
332
 
 
333
  int current_slice = 0;
 
334
  int dest_slice    = current_slice + p_nbSlices;
 
335
  //int min_slice     = this->View->GetWholeZMin();
 
336
  //int max_slice     = this->View->GetWholeZMax();
 
337
 
 
338
  double pos[3];
 
339
  this->View->GetPositionForSlice ( dest_slice, this->View->GetOrientation(), pos);
 
340
  //this->View->SyncSetPosition (pos);
 
341
 
 
342
  if ((dest_slice > min_slice) && (dest_slice < max_slice)) {
 
343
    this->View->GetPositionForSlice ( dest_slice, this->View->GetOrientation(), pos);
 
344
    //this->View->SyncSetSlice(this->View->GetOrientation(), dest_slice);
 
345
  }
 
346
  if ((dest_slice >= max_slice) && (current_slice != max_slice)) {
 
347
    this->View->GetPositionForSlice ( max_slice, this->View->GetOrientation(), pos);
 
348
    //this->View->SyncSetSlice(this->View->GetOrientation(), max_slice);
 
349
  }
 
350
  if ((dest_slice <= min_slice) && (current_slice != min_slice)) {
 
351
    this->View->GetPositionForSlice ( min_slice, this->View->GetOrientation(), pos);
 
352
    //this->View->SyncSetSlice(this->View->GetOrientation(), min_slice);
 
353
  }
 
354
 
 
355
  //this->View->Modified();
 
356
  */
 
357
}
 
358
 
 
359
 
 
360
void
 
361
GinkgoImageCommand::SetView(vtkGinkgoImageViewer *p_view)
 
362
{
 
363
  this->View = p_view;
 
364
}
 
365
 
 
366
 
 
367
 
 
368
void
 
369
GinkgoImageCommand::Zoom (vtkInteractorStyleImage* p_isi)
 
370
{
 
371
  if( !p_isi )
 
372
  {
 
373
    return;
 
374
  }
 
375
 
 
376
  vtkRenderWindowInteractor *rwi = p_isi->GetInteractor();
 
377
 
 
378
  // vtkAssemblyPath *path=NULL;
 
379
  p_isi->FindPokedRenderer(rwi->GetEventPosition()[0],
 
380
                           rwi->GetEventPosition()[1]);
 
381
 
 
382
  double *center = p_isi->GetCurrentRenderer()->GetCenter();
 
383
  int dy = rwi->GetEventPosition()[1] - rwi->GetLastEventPosition()[1];
 
384
 
 
385
  double factor = 10.0 * (double)(dy) / (double)(center[1]);
 
386
 
 
387
  //this->View->SyncSetZoom ( pow((double)1.1, factor) );
 
388
  this->View->SyncSetZoom ( pow((double)1.1, factor)*this->View->GetZoom() );
 
389
  this->View->Modified();
 
390
}