File: igstkImageSpatialObjectRepresentationTest2.cxx

package info (click to toggle)
igstk 4.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,980 kB
  • sloc: cpp: 86,267; xml: 96; makefile: 75; python: 38
file content (356 lines) | stat: -rwxr-xr-x 10,957 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: igstkImageSpatialObjectRepresentationTest2.cxx,v $
  Language:  C++
  Date:      $Date: 2009-06-09 20:10:35 $
  Version:   $Revision: 1.8 $

  Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include "igstkConfigure.h"
#include "igstkAxesObject.h"
#include "igstkCTImageReader.h"
#include "igstkCTImageSpatialObjectRepresentation.h"
#include "igstkBoxObject.h"
#include "igstkBoxObjectRepresentation.h"
#include "igstkView2D.h"
#include "igstkFLTKWidget.h"
#include "igstkVTKLoggerOutput.h"
#include "igstkLogger.h"
#include "itkStdStreamLogOutput.h"


namespace ImageSpatialObjectRepresentationTest2
{
igstkObserverObjectMacro(CTImage,
    ::igstk::CTImageReader::ImageModifiedEvent,::igstk::CTImageSpatialObject)
}


int igstkImageSpatialObjectRepresentationTest2( int argc, char* argv[] )
{

  igstk::RealTimeClock::Initialize();


  if( argc < 3 )
    {
    std::cerr << "Usage: " << argv[0] << "  CTImage  "
              << "Output image file for a screenshot" << std::endl;
    return EXIT_FAILURE;
    }
  
  typedef igstk::Object::LoggerType     LoggerType;
  typedef itk::StdStreamLogOutput       LogOutputType;
  
  // logger object created for logging mouse activities
  LoggerType::Pointer   logger = LoggerType::New();
  LogOutputType::Pointer logOutput = LogOutputType::New();
  logOutput->SetStream( std::cout );
  logger->AddLogOutput( logOutput );
  logger->SetPriorityLevel( LoggerType::CRITICAL );

  // Create an igstk::VTKLoggerOutput and then test it.
  igstk::VTKLoggerOutput::Pointer vtkLoggerOutput
                                            = igstk::VTKLoggerOutput::New();
  vtkLoggerOutput->OverrideVTKWindow();
  vtkLoggerOutput->SetLogger(logger);

  igstk::AxesObject::Pointer worldReference = igstk::AxesObject::New();

  typedef igstk::CTImageReader         ReaderType;

  ReaderType::Pointer   reader = ReaderType::New();

  reader->SetLogger( logger );

  /* Read in a DICOM series */
  std::cout << "Reading CT image : " << argv[1] << std::endl;
  ReaderType::DirectoryNameType directoryName = argv[1];
  reader->RequestSetDirectory( directoryName );

  
  // Attach an observer
  typedef ImageSpatialObjectRepresentationTest2::CTImageObserver 
                                                          CTImageObserverType;
  CTImageObserverType::Pointer ctImageObserver = CTImageObserverType::New();
  reader->AddObserver(::igstk::CTImageReader::ImageModifiedEvent(),
                            ctImageObserver);

  /* set up the image spatial object representation */ 
  typedef igstk::CTImageSpatialObjectRepresentation ImageRepresentationType;
  ImageRepresentationType::Pointer imageRepresentation =
    ImageRepresentationType::New();
  imageRepresentation->SetLogger( logger );

  //
  try
    {
    reader->RequestReadImage();
    }
  catch( ... )
    {
    std::cerr << "ERROR: An exception was thrown while reading the CT dataset"
              << std::endl;
    std::cerr << "This should not have happened. The State Machine should have"
              << std::endl;
    std::cerr << "catched that exception and converted it into a SM Input " 
              << std::endl;
    return EXIT_FAILURE;
    }

  /* Set up the box spatial object that will be added to each 
   * each corners of the image */
  
  // Top Left corner
  typedef igstk::BoxObject BoxObjectType;

  BoxObjectType::Pointer  boxObject = BoxObjectType::New();
  boxObject->SetLogger ( logger );
  
  typedef igstk::BoxObjectRepresentation BoxRepresentationType;
  BoxRepresentationType::Pointer 
       boxObjectRepresentation = BoxRepresentationType::New();
  boxObjectRepresentation->SetLogger ( logger );

  double size_X;
  double size_Y;
  double size_Z;
  
  size_X = 20.0;
  size_Y = 20.0;
  size_Z = 2.0;

  boxObject->SetSize( size_X, size_Y, size_Z );

  igstk::Transform                          transform;
  igstk::Transform::VectorType              translation;

  translation[0] = -153.66;
  translation[1] = -307.66;
  translation[2] = -176.0;
 
  igstk::Transform::ErrorType            errorValue = 1e-20;
  igstk::Transform::TimePeriodType       validtyTime = 1e20;
  
  transform.SetTranslation( translation, errorValue, validtyTime ); 

  boxObject->RequestSetTransformAndParent( transform, worldReference );

  boxObjectRepresentation->RequestSetBoxObject( boxObject );
  boxObjectRepresentation->SetColor( 1.0, 1.0, 1.0 );
  boxObjectRepresentation->SetOpacity( 1.0 );

  //Top Right corner
  BoxObjectType::Pointer  boxObject2 = BoxObjectType::New();
  boxObject2->SetLogger ( logger );
  
  BoxRepresentationType::Pointer 
       boxObjectRepresentation2 = BoxRepresentationType::New();
  boxObjectRepresentation2->SetLogger ( logger );

  double size_X2;
  double size_Y2;
  double size_Z2;

  size_X2 = 30.0;
  size_Y2 = 30.0;
  size_Z2 = 2.0;

  boxObject2->SetSize( size_X2, size_Y2, size_Z2 );

  igstk::Transform                          transform2;
  igstk::Transform::VectorType              translation2;

  translation2[0] = -153.66 + 512*0.6718;
  translation2[1] = -307.66;
  translation2[2] = -176.0;
 
  igstk::Transform::ErrorType            errorValue2 = 1e-20;
  igstk::Transform::TimePeriodType       validtyTime2 = 1e20;
  
  transform2.SetTranslation( translation2, errorValue2, validtyTime2 ); 

  boxObject2->RequestSetTransformAndParent( transform2, worldReference );

  boxObjectRepresentation2->RequestSetBoxObject( boxObject2 );
  boxObjectRepresentation2->SetColor( 1.0, 1.0, 1.0 );
  boxObjectRepresentation2->SetOpacity( 1.0 );

 //Bottom Left corner
  BoxObjectType::Pointer  boxObject3 = BoxObjectType::New();
  boxObject3->SetLogger ( logger );
  
  BoxRepresentationType::Pointer 
       boxObjectRepresentation3 = BoxRepresentationType::New();
  boxObjectRepresentation3->SetLogger ( logger );

  double size_X3;
  double size_Y3;
  double size_Z3;

  size_X3 = 40.0;
  size_Y3 = 40.0;
  size_Z3 = 2.0;

  boxObject3->SetSize( size_X3, size_Y3, size_Z3 );

  igstk::Transform                          transform3;
  igstk::Transform::VectorType              translation3;

  translation3[0] = -153.66;
  translation3[1] = -307.66 + 512*0.6718;
  translation3[2] = -176.0;
 
  igstk::Transform::ErrorType            errorValue3 = 1e-20;
  igstk::Transform::TimePeriodType       validtyTime3 = 1e20;
  
  transform3.SetTranslation( translation3, errorValue3, validtyTime3 ); 
      
  boxObject3->RequestSetTransformAndParent( transform3, worldReference );

  boxObjectRepresentation3->RequestSetBoxObject( boxObject3 );
  boxObjectRepresentation3->SetColor( 1.0, 1.0, 1.0 );
  boxObjectRepresentation3->SetOpacity( 1.0 );

  // Bottom right corner
  BoxObjectType::Pointer  boxObject4 = BoxObjectType::New();
  boxObject4->SetLogger ( logger );
  
  BoxRepresentationType::Pointer 
       boxObjectRepresentation4 = BoxRepresentationType::New();
  boxObjectRepresentation4->SetLogger ( logger );

  double size_X4;
  double size_Y4;
  double size_Z4;

  size_X4 = 50.0;
  size_Y4 = 50.0;
  size_Z4 = 2.0;

  boxObject4->SetSize( size_X4, size_Y4, size_Z4 );

  igstk::Transform                          transform4;
  igstk::Transform::VectorType              translation4;

  translation4[0] = -153.66 + 512*0.6718;
  translation4[1] = -307.66 + 512*0.6718;
  translation4[2] = -176.0;
 
  igstk::Transform::ErrorType            errorValue4 = 1e-20;
  igstk::Transform::TimePeriodType       validtyTime4 = 1e20;
  
  transform4.SetTranslation( translation4, errorValue4, validtyTime4 ); 
      
  boxObject4->RequestSetTransformAndParent( transform4, worldReference );

  boxObjectRepresentation4->RequestSetBoxObject( boxObject4 );
  boxObjectRepresentation4->SetColor( 1.0, 1.0, 1.0 );
  boxObjectRepresentation4->SetOpacity( 1.0 );

  // Create an FLTK minimal GUI
  Fl_Window * form = new Fl_Window(532,532,"CT Read View Test");
    
  typedef igstk::View2D  View2DType;
  // Create an FLTK minimal GUI
  typedef igstk::FLTKWidget      WidgetType;

  View2DType::Pointer view2D = View2DType::New();

  // instantiate FLTK widget 
  WidgetType * widget2D = new WidgetType( 10,10,512,512,"2D View");
  widget2D->RequestSetView( view2D );
  widget2D->SetLogger( logger );
  
  form->end();
  form->show();

  view2D->SetLogger( logger ); 

  igstk::Transform identityTransform;
  identityTransform.SetToIdentity( igstk::TimeStamp::GetLongestPossibleTime() );

  view2D->RequestSetTransformAndParent( identityTransform, worldReference );

  // Add the image object representation to the view
  view2D->RequestAddObject( imageRepresentation );

  // Add the box object representation to the view 
  view2D->RequestAddObject( boxObjectRepresentation );
  view2D->RequestAddObject( boxObjectRepresentation2 );
  view2D->RequestAddObject( boxObjectRepresentation3 );
  view2D->RequestAddObject( boxObjectRepresentation4 );

  reader->RequestGetImage();
  if(!ctImageObserver->GotCTImage())
    {
    std::cout << "No CTImage!" << std::endl;
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
    }

  typedef igstk::CTImageSpatialObject::Pointer ImagePointer;
  ImagePointer imageSpatialObject = ctImageObserver->GetCTImage(); 

  imageSpatialObject->SetLogger( logger );

  transform.SetToIdentity( validtyTime );

  imageSpatialObject->RequestSetTransformAndParent( transform, worldReference );

  imageRepresentation->RequestSetImageSpatialObject( imageSpatialObject );

  imageRepresentation->RequestSetOrientation( ImageRepresentationType::Axial );
  
  view2D->RequestSetOrientation( igstk::View2D::Axial );

  imageRepresentation->RequestSetSliceNumber( 0 );
  
  view2D->RequestResetCamera();
  view2D->SetRefreshRate( 30 );
  view2D->RequestStart();

  Fl::wait(1.0);  
  igstk::PulseGenerator::CheckTimeouts();

  for(unsigned int i=0; i<10; i++)
    {
    igstk::PulseGenerator::CheckTimeouts();
    Fl::check();
    }

  //Request refreshing stop to take a screenshot
  view2D->RequestStop();
      
  /* Save screenshots in a file */
  std::string filename;
  filename = argv[2]; 
  std::cout << "Saving a screen shot in file:" << argv[2] << std::endl;
  igstk::PulseGenerator::Sleep(500);
  view2D->RequestStop();
  igstk::PulseGenerator::Sleep(500);
  view2D->RequestSaveScreenShot( filename );

  delete widget2D;
  delete form;
 
  if( vtkLoggerOutput->GetNumberOfErrorMessages()  > 0 )
    {
    return EXIT_FAILURE;
    }
  
  return EXIT_SUCCESS;
}