File: igstkToolProjectionObjectRepresentation.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 (401 lines) | stat: -rw-r--r-- 12,252 bytes parent folder | download
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: igstkToolProjectionObjectRepresentation.cxx,v $
  Language:  C++
  Date:      $Date: 2011-02-09 10:13:37 $
  Version:   $Revision: 1.4 $

  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.

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

#include "igstkToolProjectionObjectRepresentation.h"
#include "igstkEvents.h"

#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkLineSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkPlaneSource.h>


namespace igstk
{ 

/** Constructor */
ToolProjectionObjectRepresentation
::ToolProjectionObjectRepresentation():m_StateMachine(this)
{

  m_LineSource = NULL;
  m_LineProperty = NULL;

  m_ToolProjectionSpatialObject = NULL;

  m_LineWidth = 2;

  m_ReslicerPlaneNormalObserver = ReslicerPlaneNormalObserver::New();

  this->RequestSetSpatialObject( m_ToolProjectionSpatialObject );
  
  igstkAddInputMacro( ValidToolProjectionObject );
  igstkAddInputMacro( NullToolProjectionObject  );
  igstkAddInputMacro( ValidReslicePlaneSpatialObject );
  igstkAddInputMacro( InValidReslicePlaneSpatialObject );

  igstkAddStateMacro( NullToolProjectionObject  );
  igstkAddStateMacro( ValidToolProjectionObject );
  igstkAddStateMacro( ValidReslicePlaneSpatialObject );

  //from NullToolProjectionObject

  igstkAddTransitionMacro( NullToolProjectionObject, ValidToolProjectionObject, 
                           ValidToolProjectionObject,  
                           SetToolProjectionObject );

  igstkAddTransitionMacro( NullToolProjectionObject, NullToolProjectionObject, 
                           NullToolProjectionObject,  No );

  igstkAddTransitionMacro( NullToolProjectionObject, 
                           ValidReslicePlaneSpatialObject, 
                           NullToolProjectionObject,  No );

  igstkAddTransitionMacro( NullToolProjectionObject, 
                           InValidReslicePlaneSpatialObject, 
                           NullToolProjectionObject,  No );

  //from ValidToolProjectionObject

  igstkAddTransitionMacro( ValidToolProjectionObject, 
                           ValidReslicePlaneSpatialObject, 
                           ValidReslicePlaneSpatialObject,  
                           SetReslicePlaneSpatialObject );

  igstkAddTransitionMacro( ValidToolProjectionObject, NullToolProjectionObject, 
                           NullToolProjectionObject,  No ); 

  igstkAddTransitionMacro( ValidToolProjectionObject, ValidToolProjectionObject,
                           ValidToolProjectionObject,  No );

  igstkAddTransitionMacro( ValidToolProjectionObject, 
                           InValidReslicePlaneSpatialObject, 
                           ValidToolProjectionObject,  No );


  //from ValidReslicePlaneSpatialObject

  igstkAddTransitionMacro( ValidReslicePlaneSpatialObject, 
                           ValidToolProjectionObject, 
                           ValidReslicePlaneSpatialObject,  No );

  igstkAddTransitionMacro( ValidReslicePlaneSpatialObject, 
                           NullToolProjectionObject, 
                           ValidReslicePlaneSpatialObject,  No ); 

  igstkAddTransitionMacro( ValidReslicePlaneSpatialObject, 
                           ValidReslicePlaneSpatialObject, 
                           ValidReslicePlaneSpatialObject,  No );

  igstkAddTransitionMacro( ValidReslicePlaneSpatialObject, 
                           InValidReslicePlaneSpatialObject, 
                           ValidReslicePlaneSpatialObject,  No );

  m_StateMachine.SelectInitialState( m_NullToolProjectionObjectState );

  m_StateMachine.SetReadyToRun();
} 

/** Destructor */
ToolProjectionObjectRepresentation
::~ToolProjectionObjectRepresentation()  
{
  this->DeleteActors();

  if (m_LineSource != NULL)
    {
    m_LineSource->Delete();
    m_LineSource=NULL;
    }

  if (m_LineProperty != NULL)
    {
    m_LineProperty->Delete();
    m_LineProperty=NULL;
    }
}


/** Request to Set the ToolProjectionSpatial Object */
void ToolProjectionObjectRepresentation
::RequestSetToolProjectionObject( 
                        const ToolProjectionSpatialObjectType * ToolProjection )
{
  m_ToolProjectionObjectToAdd = ToolProjection;
  if( !m_ToolProjectionObjectToAdd )
    {
    m_StateMachine.PushInput( m_NullToolProjectionObjectInput );
    m_StateMachine.ProcessInputs();
    }
  else
    {
    m_StateMachine.PushInput( m_ValidToolProjectionObjectInput );
    m_StateMachine.ProcessInputs();
    }
}

/** Set the ToolProjection Spatial Object */
void ToolProjectionObjectRepresentation
::NoProcessing()
{
}

/** Set the ToolProjection Spatial Object */
void ToolProjectionObjectRepresentation
::SetToolProjectionObjectProcessing()
{
  m_ToolProjectionSpatialObject = m_ToolProjectionObjectToAdd;
  this->RequestSetSpatialObject( m_ToolProjectionSpatialObject );
} 

/** Requests to set the ReslicePlaneSpatialObject */
void 
ToolProjectionObjectRepresentation
::RequestSetReslicePlaneSpatialObject( const ReslicerPlaneType *
reslicePlaneSpatialObject )
{  
  igstkLogMacro( DEBUG,"igstk::ToolProjectionObjectRepresentation\
                       ::RequestSetReslicePlaneSpatialObject called...\n");

  m_ReslicePlaneSpatialObjectToBeSet = const_cast< ReslicerPlaneType* >(
                                                     reslicePlaneSpatialObject);

  if( !m_ReslicePlaneSpatialObjectToBeSet )
    {
    m_StateMachine.PushInput( m_InValidReslicePlaneSpatialObjectInput );
    }
  else
    {
    m_StateMachine.PushInput( m_ValidReslicePlaneSpatialObjectInput );
    }

  m_StateMachine.ProcessInputs();
}

/** Sets the ReslicePlaneSpatialObject */
void 
ToolProjectionObjectRepresentation
::SetReslicePlaneSpatialObjectProcessing( )
{  
  igstkLogMacro( DEBUG,"igstk::ToolProjectionObjectRepresentation\
                       ::SetReslicePlaneSpatialObjectProcessing called...\n");

  m_ReslicePlaneSpatialObject = m_ReslicePlaneSpatialObjectToBeSet;

  m_ReslicePlaneSpatialObject->AddObserver( 
                                  ReslicerPlaneType::ReslicerPlaneNormalEvent(),
                                                m_ReslicerPlaneNormalObserver );

  m_ReslicePlaneSpatialObject->RequestComputeReslicingPlane();
}

/** Print Self function */
void ToolProjectionObjectRepresentation
::PrintSelf( std::ostream& os, itk::Indent indent ) const
{
  Superclass::PrintSelf(os, indent);
  os << "m_ReslicePlaneSpatialObject = " << m_ReslicePlaneSpatialObject << std::endl;
  os << "m_LineSource = " << m_LineSource << std::endl;
  os << "m_LineProperty = " << m_LineProperty << std::endl;
  os << "m_LineWidth = " << m_LineWidth << std::endl;
}

/** Update the visual representation in response to changes in the geometric
 * object */
void ToolProjectionObjectRepresentation
::UpdateRepresentationProcessing()
{
  igstkLogMacro( DEBUG, "UpdateRepresentationProcessing called ....\n");

  // we don't need to force a plane update in the reslicer plane spatial object.
  // Let's use the plane the he already has
  m_ReslicePlaneSpatialObject->RequestComputeReslicingPlane();

  if ( !m_ReslicePlaneSpatialObject->IsToolSpatialObjectSet() )
  return;

  // todo: get the tool's direction directly. do not calculate it again
  igstk::Transform toolTransform = 
                                m_ReslicePlaneSpatialObject->GetToolTransform();
  VectorType point1 = toolTransform.GetTranslation();

  VectorType point2;
  point2.Fill(0);

  // we assume the tool's long axis in the -x direction (with the tip in (0,0,0)
  igstk::Transform::VersorType rotation = toolTransform.GetRotation();
  VectorType toolAxis;
  toolAxis[0] = 1;
  toolAxis[1] = 0;
  toolAxis[2] = 0;
  toolAxis = rotation.Transform(toolAxis);
  
  m_ReslicerPlaneNormalObserver->Reset();

  m_ReslicePlaneSpatialObject->RequestGetReslicingPlaneParameters();
  
  ReslicerPlaneType::VectorType normal;
  if( m_ReslicerPlaneNormalObserver->GotReslicerPlaneNormal() )
    {
    normal = m_ReslicerPlaneNormalObserver->GetReslicerPlaneNormal();

    VectorType toolProy = itk::CrossProduct( normal, 
                                        itk::CrossProduct(toolAxis, normal) );

    point2 = point1 + toolProy*this->m_ToolProjectionSpatialObject->GetSize();
    }
  else
  return;

  if ( (point2-point1).GetNorm() > 0.1 )
    {
    m_LineSource->SetPoint1( point1[0], point1[1], point1[2] );
    m_LineSource->SetPoint2( point2[0], point2[1], point2[2] );
    }
}

/** Sets actors visibility */
void ToolProjectionObjectRepresentation
::SetVisibility(bool visibility)
{
  // Update all the actors
  ActorsListType::iterator it = this->m_Actors.begin();
  while(it != this->m_Actors.end())
    {
    vtkActor * va = dynamic_cast< vtkActor * >( *it );
    if( va )
      {
      va->SetVisibility(visibility);
      }
    it++;
    }
}

/** Verify time stamp of the attached tool */
bool
ToolProjectionObjectRepresentation
::VerifyTimeStamp( ) const
{
  igstkLogMacro( DEBUG, 
  "igstk::ImageResliceSpatialObjectRepresentation::VerifyTimeStamp called..\n");

  if( m_ReslicePlaneSpatialObject.IsNull() )
    {
    return false;
    }

  // if there is no tool spatial object attached to the reslicer plane,
  // we don't want to show the tool projection either
  if( !m_ReslicePlaneSpatialObject->IsToolSpatialObjectSet() )
    {
    return false;
    }

  // fixme: we are having severe blinking problems here
  if( this->GetRenderTimeStamp().GetExpirationTime() <
    this->m_ReslicePlaneSpatialObject->GetToolTransform().GetStartTime() ||
    this->GetRenderTimeStamp().GetStartTime() >
    this->m_ReslicePlaneSpatialObject->GetToolTransform().GetExpirationTime() )
    {

    // fixme
    double diff = 
      this->GetRenderTimeStamp().GetStartTime() - 
      this->m_ReslicePlaneSpatialObject->
                                       GetToolTransform().GetExpirationTime();

    if (diff > 250 )
      {
      return false;
      }
    else
      {
      return true;
      }

    }
  else
    {
    return true;
    }
}

/** Set the line width */
void ToolProjectionObjectRepresentation
::SetLineWidth(double width)
{
  if( this->m_LineWidth == width )
    {
    return;
    }
  this->m_LineWidth = width;

  if ( m_LineProperty != NULL )
    {
    m_LineProperty->SetLineWidth(m_LineWidth);
    }
}

/** Create the vtk Actors */
void ToolProjectionObjectRepresentation
::CreateActors()
{
  // to avoid duplicates we clean the previous actors
  this->DeleteActors();

  // build projection line
  m_LineSource = vtkLineSource::New();
  m_LineSource->SetPoint1( 0, 0, 0 );
  m_LineSource->SetPoint2( 1, 1, 1 );

  m_LineProperty = vtkProperty::New();
  m_LineProperty->SetAmbient(1);
  m_LineProperty->SetRepresentationToWireframe();
  m_LineProperty->SetInterpolationToFlat();
  m_LineProperty->SetLineWidth(m_LineWidth);
  m_LineProperty->SetColor( this->GetRed(),this->GetGreen(),this->GetBlue() );

  vtkPolyDataMapper* lineMapper = vtkPolyDataMapper::New();
  lineMapper->SetInput ( m_LineSource->GetOutput() );
  lineMapper->SetResolveCoincidentTopologyToPolygonOffset();
  lineMapper->SetResolveCoincidentTopologyPolygonOffsetParameters(10,10);

  vtkActor* lineActor = vtkActor::New();
  lineActor->SetMapper (lineMapper);
  lineActor->SetProperty(m_LineProperty);
  lineMapper->Delete(); 

  this->AddActor( lineActor );
}

/** Create a copy of the current object representation */
ToolProjectionObjectRepresentation::Pointer
ToolProjectionObjectRepresentation
::Copy() const
{
  Pointer newOR = ToolProjectionObjectRepresentation::New();
  newOR->SetColor( this->GetRed(),this->GetGreen(),this->GetBlue() );
  newOR->SetOpacity( this->GetOpacity() );
  newOR->RequestSetToolProjectionObject( this->m_ToolProjectionSpatialObject );
  newOR->RequestSetReslicePlaneSpatialObject(this->m_ReslicePlaneSpatialObject);

  return newOR;
}


} // end namespace igstk