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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
/*=========================================================================
Program: Image Guided Surgery Software Toolkit
Module: $RCSfile: igstkObjectRepresentation.cxx,v $
Language: C++
Date: $Date: 2008-12-30 07:34:23 $
Version: $Revision: 1.25 $
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 "igstkObjectRepresentation.h"
#include "vtkMatrix4x4.h"
#include "igstkEvents.h"
#include "vtkActor.h"
#include "vtkProp3D.h"
#include "vtkProperty.h"
/** Convenience macro for adding Inputs to the State Machine */
#define igstkAddInputMacro2( statemachine, inputname ) \
this->m_##statemachine.AddInput( this->m_##inputname##Input, \
#inputname"Input" );
/** Convenience macro for adding States to the State Machine */
#define igstkAddStateMacro2( statemachine, statename ) \
this->m_##statemachine.AddState( this->m_##statename##State,\
#statename"State" );
/** Convenience macro for adding Transitions to the State Machine */
#define igstkAddTransitionMacro2(statemachine,state1,input,state2,action )\
this->m_##statemachine.AddTransition( this->m_##state1##State, \
this->m_##input##Input, \
this->m_##state2##State, \
& Self::action##Processing );
namespace igstk
{
/** Constructor */
ObjectRepresentation::ObjectRepresentation():
m_StateMachine(this),m_VisibilityStateMachine(this)
{
this->m_Color[0] = 1.0;
this->m_Color[1] = 1.0;
this->m_Color[2] = 1.0;
this->m_Opacity = 1.0;
this->m_SpatialObject = NULL;
igstkAddInputMacro( ValidSpatialObject );
igstkAddInputMacro( NullSpatialObject );
igstkAddInputMacro( UpdateRepresentation );
igstkAddInputMacro( SpatialObjectTransform );
igstkAddInputMacro( TransformNotAvailable );
igstkAddStateMacro( NullSpatialObject );
igstkAddStateMacro( ValidSpatialObject );
igstkAddStateMacro( AttemptingGetTransform );
igstkAddTransitionMacro( NullSpatialObject,
NullSpatialObject,
NullSpatialObject,
No );
igstkAddTransitionMacro( NullSpatialObject,
ValidSpatialObject,
ValidSpatialObject,
SetSpatialObject );
igstkAddTransitionMacro( NullSpatialObject,
UpdateRepresentation,
NullSpatialObject,
No );
igstkAddTransitionMacro( NullSpatialObject,
SpatialObjectTransform,
NullSpatialObject,
ReportInvalidRequest );
igstkAddTransitionMacro( NullSpatialObject,
TransformNotAvailable,
NullSpatialObject,
ReportInvalidRequest );
igstkAddTransitionMacro( ValidSpatialObject,
NullSpatialObject,
NullSpatialObject,
No );
igstkAddTransitionMacro( ValidSpatialObject,
ValidSpatialObject,
ValidSpatialObject,
ReportInvalidRequest );
igstkAddTransitionMacro( ValidSpatialObject,
UpdateRepresentation,
AttemptingGetTransform,
RequestGetTransform );
igstkAddTransitionMacro( ValidSpatialObject,
SpatialObjectTransform,
ValidSpatialObject,
ReportInvalidRequest );
igstkAddTransitionMacro( ValidSpatialObject,
TransformNotAvailable,
ValidSpatialObject,
ReportInvalidRequest );
igstkAddTransitionMacro( AttemptingGetTransform,
NullSpatialObject,
AttemptingGetTransform,
ReportInvalidRequest );
igstkAddTransitionMacro( AttemptingGetTransform,
ValidSpatialObject,
AttemptingGetTransform,
ReportInvalidRequest );
igstkAddTransitionMacro( AttemptingGetTransform,
UpdateRepresentation,
AttemptingGetTransform,
No );
igstkAddTransitionMacro( AttemptingGetTransform,
SpatialObjectTransform,
ValidSpatialObject,
ReceiveSpatialObjectTransform );
igstkAddTransitionMacro( AttemptingGetTransform,
TransformNotAvailable,
ValidSpatialObject,
ReceiveTransformNotAvailable );
igstkSetInitialStateMacro( NullSpatialObject );
this->m_StateMachine.SetReadyToRun();
// Configure the auxiliary State Machine that controls the Visibility of the
// objects.
igstkAddInputMacro2( VisibilityStateMachine, ValidTimeStamp );
igstkAddInputMacro2( VisibilityStateMachine, InvalidTimeStamp );
igstkAddInputMacro2( VisibilityStateMachine, SetActorVisibility );
igstkAddStateMacro2( VisibilityStateMachine, Invisible );
igstkAddStateMacro2( VisibilityStateMachine, Visible );
igstkAddTransitionMacro2( VisibilityStateMachine,
Invisible, InvalidTimeStamp,
Invisible, No);
igstkAddTransitionMacro2( VisibilityStateMachine,
Invisible, ValidTimeStamp,
Visible, MakeObjectsVisible);
igstkAddTransitionMacro2( VisibilityStateMachine,
Visible, InvalidTimeStamp,
Invisible, MakeObjectsInvisible );
igstkAddTransitionMacro2( VisibilityStateMachine,
Visible, ValidTimeStamp,
Visible, No );
igstkAddTransitionMacro2( VisibilityStateMachine,
Visible, SetActorVisibility,
Visible, SetActorVisible );
igstkAddTransitionMacro2( VisibilityStateMachine,
Invisible, SetActorVisibility,
Invisible, SetActorInvisible );
this->m_VisibilitySetActor = NULL;
this->m_VisibilityStateMachine.SelectInitialState( this->m_InvisibleState );
this->m_VisibilityStateMachine.SetReadyToRun();
}
/** Destructor */
ObjectRepresentation::~ObjectRepresentation()
{
// This must be invoked in order to prevent Memory Leaks.
this->DeleteActors();
}
/** Get the red color component */
ObjectRepresentation::ColorScalarType
ObjectRepresentation::GetRed() const
{
return this->m_Color[0];
}
/** Get the green color component */
ObjectRepresentation::ColorScalarType
ObjectRepresentation::GetGreen() const
{
return this->m_Color[1];
}
/** Get the blue color component */
ObjectRepresentation::ColorScalarType
ObjectRepresentation::GetBlue() const
{
return this->m_Color[2];
}
/** Add an actor to the actors list */
void ObjectRepresentation::AddActor( vtkProp * actor )
{
// Initialize objects based on the visibility state machine.
this->RequestSetActorVisibility( actor );
this->m_Actors.push_back( actor );
}
/** Empty the list of actors */
void ObjectRepresentation::DeleteActors()
{
ActorsListType::iterator it = this->m_Actors.begin();
while(it != this->m_Actors.end())
{
(*it)->Delete();
it++;
}
// Reset the list of actors
this->m_Actors.clear();
}
/** Set the Spatial Object */
void ObjectRepresentation
::RequestSetSpatialObject( const SpatialObject * spatialObject )
{
// This const_cast is done because the ObjectRepresentation class invoke
// Request methods in the SpatialObject, and those methods modify the state
// of its internal StateMachine. It is however desirable to keep the outside
// API of this class refering to a const object.
this->m_SpatialObjectToAdd = const_cast< SpatialObject *>( spatialObject );
if( !this->m_SpatialObjectToAdd )
{
igstkPushInputMacro( NullSpatialObject );
this->m_StateMachine.ProcessInputs();
}
else
{
igstkPushInputMacro( ValidSpatialObject );
this->m_StateMachine.ProcessInputs();
}
}
/** Set the Spatial Object */
void ObjectRepresentation::SetSpatialObjectProcessing()
{
this->m_SpatialObject = this->m_SpatialObjectToAdd;
this->ObserveSpatialObjectTransformInput( this->m_SpatialObject );
}
/** Set the color */
void ObjectRepresentation::SetColor(
ColorScalarType r, ColorScalarType g, ColorScalarType b)
{
if( this->m_Color[0] == r &&
this->m_Color[1] == g &&
this->m_Color[2] == b )
{
return;
}
this->m_Color[0] = r;
this->m_Color[1] = g;
this->m_Color[2] = b;
// 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 != NULL )
{
va->GetProperty()->SetColor(
this->m_Color[0], this->m_Color[1], this->m_Color[2] );
}
it++;
}
}
/** Set the opacity */
void ObjectRepresentation::SetOpacity(OpacityType alpha)
{
if( this->m_Opacity == alpha )
{
return;
}
this->m_Opacity = alpha;
// 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->GetProperty()->SetOpacity(this->m_Opacity);
}
it++;
}
}
/** Request Update the object representation (i.e vtkActors). */
void ObjectRepresentation::RequestUpdateRepresentation(
const TimeStamp & time,
const CoordinateSystem* cs )
{
igstkLogMacro( DEBUG, "RequestUpdateRepresentation at time"
<< time );
this->m_TimeToRender = time;
this->m_TargetCoordinateSystem = cs;
igstkPushInputMacro( UpdateRepresentation );
this->m_StateMachine.ProcessInputs();
this->m_TargetCoordinateSystem = NULL; // Break reference.
}
/** Process the request for updating the transform from the SpatialObject. */
void ObjectRepresentation::RequestGetTransformProcessing()
{
igstkLogMacro( DEBUG, "RequestGetTransformProcessing called ....");
// The response to this request is part of the internal dialog between the
// ObjectRepresentation and the SpatialObject. There is no need to report the
// answer outside of the ObjectRepresentation.
this->m_SpatialObject->RequestComputeTransformTo(
this->m_TargetCoordinateSystem );
}
/** Receive the Transform from the SpatialObject via a transduction macro. */
void ObjectRepresentation::ReceiveSpatialObjectTransformProcessing()
{
this->m_SpatialObjectTransform =
this->m_SpatialObjectTransformInputToBeSet.GetTransform();
igstkLogMacro( DEBUG,
"Received SpatialObject Transform " << this->m_SpatialObjectTransform );
vtkMatrix4x4* vtkMatrix = vtkMatrix4x4::New();
this->m_SpatialObjectTransform.ExportTransform( *vtkMatrix );
// Update all the actors
ActorsListType::iterator it = this->m_Actors.begin();
while( it != this->m_Actors.end() )
{
vtkProp3D::SafeDownCast(*it)->SetUserMatrix(vtkMatrix);
it++;
}
vtkMatrix->Delete();
this->RequestVerifyTimeStampAndUpdateVisibility();
}
/** Receive No Transform Available message from the SpatialObject via a
* transduction macro. */
void ObjectRepresentation::ReceiveTransformNotAvailableProcessing()
{
// No new transform for us to use.
//
// Check if the old one has not expired.
//
this->RequestVerifyTimeStampAndUpdateVisibility();
}
/** This method checks the time stamp of the transform
* and modifies the visibility of the objects accordingly. */
void ObjectRepresentation::RequestVerifyTimeStampAndUpdateVisibility()
{
if( ! this->VerifyTimeStamp() )
{
this->m_VisibilityStateMachine.PushInput( this->m_InvalidTimeStampInput );
this->m_VisibilityStateMachine.ProcessInputs();
}
else
{
this->m_VisibilityStateMachine.PushInput( this->m_ValidTimeStampInput );
this->m_VisibilityStateMachine.ProcessInputs();
}
this->UpdateRepresentationProcessing();
}
/** Verify time stamp */
bool ObjectRepresentation::VerifyTimeStamp() const
{
if( this->m_TimeToRender.GetExpirationTime() <
this->m_SpatialObjectTransform.GetStartTime() ||
this->m_TimeToRender.GetStartTime() >
this->m_SpatialObjectTransform.GetExpirationTime() )
{
return false;
}
else
{
return true;
}
}
/** Null operation for a State Machine transition */
void ObjectRepresentation::NoProcessing()
{
}
/** Make Objects Invisible. This method is called when the Transform time stamp
* has expired with respect to the requested rendering time. */
void ObjectRepresentation::MakeObjectsInvisibleProcessing()
{
igstkLogMacro( WARNING,
"MakeObjectsInvisibleProcessing at " << this->m_TimeToRender );
this->InvokeEvent( TransformExpiredErrorEvent() );
ActorsListType::iterator it = this->m_Actors.begin();
while(it != this->m_Actors.end())
{
(*it)->VisibilityOff();
it++;
}
}
/** Make Objects Visible. This method is called when the Transform time stamp
* is valid with respect to the requested rendering time. */
void ObjectRepresentation::MakeObjectsVisibleProcessing()
{
igstkLogMacro( WARNING,
"MakeObjectsVisibleProcessing at " << this->m_TimeToRender );
ActorsListType::iterator it = this->m_Actors.begin();
while(it != this->m_Actors.end())
{
(*it)->VisibilityOn();
it++;
}
}
/** Report an invalid request made to the State Machine */
void ObjectRepresentation::ReportInvalidRequestProcessing()
{
igstkLogMacro( WARNING, "Invalid request in ObjectRepresentation\n");
InvalidRequestErrorEvent event;
this->InvokeEvent( event );
}
/** Print Self function */
void ObjectRepresentation
::PrintSelf( std::ostream& os, itk::Indent indent ) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Color: " << this->m_Color[0] << " : ";
os << this->m_Color[1] << " : " << this->m_Color[2] << std::endl;
os << indent << "Opacity: " << this->m_Opacity << std::endl;
}
void ObjectRepresentation
::RequestSetActorVisibility( vtkProp* p )
{
this->m_VisibilitySetActor = p;
this->m_VisibilityStateMachine.PushInput( this->m_SetActorVisibilityInput );
this->m_VisibilityStateMachine.ProcessInputs();
}
void ObjectRepresentation
::SetActorVisibleProcessing()
{
this->m_VisibilitySetActor->VisibilityOn();
}
void ObjectRepresentation
::SetActorInvisibleProcessing()
{
this->m_VisibilitySetActor->VisibilityOff();
}
TimeStamp ObjectRepresentation
::GetRenderTimeStamp() const
{
return this->m_TimeToRender;
}
} // end namespace igstk
|