[IGSTK-Users] Trying to add axes to the four views display, but its not working!!!!
Arefin Shamsil
marefin at uwo.ca
Wed Jul 20 22:13:23 EDT 2011
Hi there,
I am trying to add axes to four different views. I have practically tried every way possible except those that I cant think of. Can someone please tell me what I am doing wrong.
The code is added below. The idea is that when the function StartTracking() is called, it will start the tracking and also load the axes on all four views as well as the real-time moving object. But no matter however I am adding the graphic objects and object representations to the four views, nothing is showing-- not the moving object, not the axes, nothing.
I know that I am missing something, and I tried following all the examples provided but I cant figure out what is wrong.
Any help would be greatly appreciated.
PS. Code is added here. Please look in the void StartTracking() function definition where I am adding all the graphic objects.
Regards,
Arefin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/igstk-users/attachments/20110720/d2f4fb9e/attachment.html>
-------------- next part --------------
/*=========================================================================
Program: Image Guided Surgery Software Toolkit
Module: $RCSfile: igstkTrackerDataLogger.h,v $
Language: C++
Date: $Date: 2011-02-04 22:42:16 $
Version: $Revision: 1.1 $
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.
=========================================================================*/
#ifndef __igstkTrackerDataLogger_h
#define __igstkTrackerDataLogger_h
// BeginLatex
//
// This example illustrates how to export tracking data to a file.
// The example program supports output to several files at once.
//
// EndLatex
//#include "igstkSandboxConfigure.h"
#include "igstkPolarisTrackerGUI.h"
#include "FL/Fl_File_Chooser.H"
#include "TrackingVolumeViewerQuadrantViews.h"
#include "igstkStateMachine.h"
#include "igstkMacros.h"
#include "igstkObject.h"
#include "igstkEvents.h"
#include "igstkTracker.h"
#include "igstkTrackerTool.h"
#include "igstkBoxObject.h"
#include "igstkBoxObjectRepresentation.h"
#include "igstkMeshReader.h"
#include "igstkMeshObjectRepresentation.h"
#include "igstkAxesObject.h"
#include "igstkAxesObjectRepresentation.h"
#include "itkStdStreamLogOutput.h"
#include "igstkReslicerPlaneSpatialObject.h"
#include "igstkMeshResliceObjectRepresentation.h"
#include "igstkTrackerConfiguration.h"
#include "igstkTrackerController.h"
#include "igstkTransformObserver.h"
#include "igstkTrackerDataLoggerConfigurationFileReader.h"
#include "igstkTrackerDataLoggerConfigurationXMLFileReader.h"
#include <fstream>
#include <FL/Fl_Output.H>
#include "igstkPolarisVicraConfigurationXMLFileReader.h"
#include "igstkPolarisSpectraConfigurationXMLFileReader.h"
#include "igstkPolarisHybridConfigurationXMLFileReader.h"
#include "igstkAuroraConfigurationXMLFileReader.h"
#include "igstkMicronConfigurationXMLFileReader.h"
#include "igstkAscensionConfigurationXMLFileReader.h"
#include "igstkAscension3DGConfigurationXMLFileReader.h"
#include "FL/Fl_Input.H"
#define VIEW_2D_REFRESH_RATE 60
#define VIEW_3D_REFRESH_RATE 60
//#include "TrackingVolumeViewerQuadrantViews.h"
/**
* \class TrackerDataLogger
* \brief This class performs output of tracking data to output file.
*
* The class is instantiated with the name of the xml file containing the
* tracker. The user can then start and stop the tracking.
*
*/
class TrackerDataLogger : public igstkPolarisTrackerGUI //implementing the functions in igstkPolarisTrackerGUI
{
public:
/**
* \class This class was created due to the need for platform independence.
* In windows the std::exception class has a constructor which has a string as
* payload, sadly in linux/unix this constructor does not exist.
*/
class ExceptionWithMessage : public std::exception
{
public:
ExceptionWithMessage (const std::string & str) throw(){
this->m_Str = str;
}
virtual ~ExceptionWithMessage() throw () {}
virtual const char *what() const throw (){
return this->m_Str.c_str();
}
private:
std::string m_Str;
};
TrackerDataLogger(){
//igstkPolarisTrackerGUI *application = new igstkPolarisTrackerGUI;
//application->mainWindow->show();
this->Display3D = ViewType3D::New();
this->DisplayAxial = ViewType2D::New();
this->DisplayCoronal = ViewType2D::New();
this->DisplaySagittal = ViewType2D::New();
this->Display3DWidget->RequestSetView( this->Display3D );
this->DisplayAxialWidget->RequestSetView( this->DisplayAxial );
this->DisplayCoronalWidget->RequestSetView( this->DisplayCoronal );
this->DisplaySagittalWidget->RequestSetView( this->DisplaySagittal );
// Set up the four quadrant views
this->Display3DWidget->RequestEnableInteractions();
this->Display3D->SetRefreshRate( 60 ); // 60 Hz
this->Display3D->RequestStart();
//this->DisplayAxialWidget->RequestEnableInteractions();
this->DisplayAxial->SetRefreshRate( 60 ); // 60 Hz
this->DisplayAxial->RequestStart();
this->DisplayAxial->RequestSetOrientation( ViewType2D::Axial );
//this->DisplayCoronalWidget->RequestEnableInteractions();
this->DisplayCoronal->SetRefreshRate( 60 ); // 60 Hz
this->DisplayCoronal->RequestStart();
this->DisplayCoronal->RequestSetOrientation( ViewType2D::Coronal );
this->DisplaySagittalWidget->RequestEnableInteractions();
this->DisplaySagittal->SetRefreshRate( 60 ); // 60 Hz
this->DisplaySagittal->RequestStart();
this->DisplaySagittal->RequestSetOrientation( ViewType2D::Sagittal );
m_Logger = NULL;
} //Default constructor
~TrackerDataLogger(){
this->Display3D->RequestStop();
this->DisplayAxial->RequestStop();
this->DisplaySagittal->RequestStop();
this->DisplayCoronal->RequestStop();
} //Destructor of the object
//constructor for constructing a TrackerDataLogger object
void TrackerConfiguration(std::string &trackerXMLConfigurationFileName){ //throw (ExceptionWithMessage){
//TrackerDataLogger( std::string &trackerXMLConfigurationFileName ) throw ( ExceptionWithMessage ){
// Calling the File reader function
igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType *trackerConfiguration =
new igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType();
const unsigned int NUM_TRACKER_TYPES = 7;
igstk::TrackerConfigurationXMLFileReaderBase::Pointer trackerConfigurationXMLReaders[NUM_TRACKER_TYPES];
trackerConfigurationXMLReaders[0] = igstk::PolarisVicraConfigurationXMLFileReader::New();
trackerConfigurationXMLReaders[1] = igstk::PolarisSpectraConfigurationXMLFileReader::New();
trackerConfigurationXMLReaders[2] = igstk::PolarisHybridConfigurationXMLFileReader::New();
trackerConfigurationXMLReaders[3] = igstk::AuroraConfigurationXMLFileReader::New();
trackerConfigurationXMLReaders[4] = igstk::MicronConfigurationXMLFileReader::New();
trackerConfigurationXMLReaders[5] = igstk::AscensionConfigurationXMLFileReader::New();
trackerConfigurationXMLReaders[6] = igstk::Ascension3DGConfigurationXMLFileReader::New();
igstk::TrackerDataLoggerConfigurationFileReader::Pointer trackerConfigReader = igstk::TrackerDataLoggerConfigurationFileReader::New();
//need to observe if the request read succeeds or fails
//there is a third option that the read is invalid, if the
//file name or xml reader weren't set
igstk::TrackerDataLoggerConfigurationFileReader::ReadFailSuccessObserver::Pointer
rfso = igstk::TrackerDataLoggerConfigurationFileReader::ReadFailSuccessObserver::New();
trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::ReadSuccessEvent(),rfso);
trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::ReadFailureEvent(),rfso);
trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::UnexpectedTrackerTypeEvent(),rfso );
//setting the file name and reader always succeeds so I don't
//observe for success event
trackerConfigReader->RequestSetFileName( trackerXMLConfigurationFileName );
TrackerConfigurationObserver::Pointer tco = TrackerConfigurationObserver::New();
for( unsigned int i=0; i<NUM_TRACKER_TYPES; i++ )
{
//setting the xml reader always succeeds so I don't
//observe the success event
trackerConfigReader->RequestSetReader( trackerConfigurationXMLReaders[i]);
trackerConfigReader->RequestRead();
if( rfso->GotUnexpectedTrackerType())
{
rfso->Reset();
}
else if( rfso->GotFailure() && !rfso->GotUnexpectedTrackerType() )//<<<<<<<<
{
throw ExceptionWithMessage( std::string( "Failed to reset tracker." ) );
//throw ExceptionWithMessage( rfso->GetFailureMessage() );
}
else if( rfso->GotSuccess() )
{
//get the configuration data from the reader
trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataEvent(),tco);
trackerConfigReader->RequestGetData();
if( tco->GotTrackerConfiguration() )
{
igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType *result = tco->GetTrackerConfiguration();
trackerConfiguration->m_TrackerConfiguration = result->m_TrackerConfiguration;
trackerConfiguration->m_ToolNamesAndOutputFileNames.insert(result->m_ToolNamesAndOutputFileNames.begin(),
result->m_ToolNamesAndOutputFileNames.end());
}
}
}
// Checking if tracker configuration is of proper type
if( !trackerConfiguration ){throw ExceptionWithMessage(std::string( "Unknown tracker type." ));}
this->m_TrackerController = igstk::TrackerController::New(); // Initializing tracker controller object
InitializeErrorObserver::Pointer ieo = InitializeErrorObserver::New();
this->m_TrackerController->AddObserver(igstk::TrackerController::InitializeErrorEvent(), ieo);
this->m_TrackerController->RequestInitialize(trackerConfiguration->m_TrackerConfiguration);
if( ieo->GotInitializeError())
{
//throw ExceptionWithMessage( ieo->GetInitializeError() ); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< CHANGE
throw ExceptionWithMessage( std::string( "Initialization error" ) );
}
//igstk::SpatialObject::Pointer world = igstk::SpatialObject::New();
//igstk::Transform I;
igstk::Transform identity;
identity.SetToIdentity(igstk::TimeStamp::GetLongestPossibleTime());
//check if we have a reference and setup the observers
ToolObserver::Pointer requestReferenceToolObserver = ToolObserver::New();
this->m_TrackerController->AddObserver(igstk::TrackerController::RequestToolEvent(), requestReferenceToolObserver );
this->m_TrackerController->RequestGetReferenceTool();
//we have a reference
if( requestReferenceToolObserver->GotTool() ) {
igstk::TrackerController::ToolEntryType referenceToolData = requestReferenceToolObserver->GetTool();
//referenceToolData.second->RequestSetTransformAndParent( I, world );
// Added new code from here,,,,,,,,,
/*m_WorldReferenceRepresentation = AxesRepresentationType::New();
m_WorldReference->SetSize(50,50,50);
m_WorldReferenceRepresentation->RequestSetAxesObject(m_WorldReference);
this->DisplayAxial->RequestAddObject(m_WorldReferenceRepresentation);
this->DisplaySagittal->RequestAddObject(m_WorldReferenceRepresentation);
this->DisplayCoronal->RequestAddObject(m_WorldReferenceRepresentation);
this->Display3D->RequestAddObject(m_WorldReferenceRepresentation);*/
referenceToolData.second->RequestSetTransformAndParent( identity, m_WorldReference );
}
else {
//this->m_TrackerController->RequestSetParentSpatialObject( I, world );
/*m_WorldReferenceRepresentation = AxesRepresentationType::New();
m_WorldReference->SetSize(50,50,50);
m_WorldReferenceRepresentation->RequestSetAxesObject(m_WorldReference);
this->DisplayAxial->RequestAddObject(m_WorldReferenceRepresentation);
this->DisplaySagittal->RequestAddObject(m_WorldReferenceRepresentation->Copy());
this->DisplayCoronal->RequestAddObject(m_WorldReferenceRepresentation->Copy());
this->Display3D->RequestAddObject(m_WorldReferenceRepresentation->Copy());*/
this->m_TrackerController->RequestSetParentSpatialObject( identity, m_WorldReference );
}
ToolListObserver::Pointer requestNonReferenceToolsObserver = ToolListObserver::New();
this->m_TrackerController->AddObserver(igstk::TrackerController::RequestToolsEvent(), requestNonReferenceToolsObserver );
this->m_TrackerController->RequestGetNonReferenceToolList();
if( requestNonReferenceToolsObserver->GotToolList() )
{ //get the tools from the tracker controller
igstk::TrackerController::ToolContainerType toolContainer = requestNonReferenceToolsObserver->GetToolList();
igstk::TrackerController::ToolContainerType::iterator toolNamesAndToolsIt;
//pair the tracker tools with the oigtlink data
igstk::TrackerDataLoggerConfigurationFileReader::LoggerDataType::iterator toolNamesAndOutputFileNamesIt,
toolNamesAndOutputFileNamesEnd;
toolNamesAndOutputFileNamesIt = trackerConfiguration->m_ToolNamesAndOutputFileNames.begin();
toolNamesAndOutputFileNamesEnd = trackerConfiguration->m_ToolNamesAndOutputFileNames.end();
for(; toolNamesAndOutputFileNamesIt!=toolNamesAndOutputFileNamesEnd; toolNamesAndOutputFileNamesIt++ ) {
toolNamesAndToolsIt = toolContainer.find(toolNamesAndOutputFileNamesIt->first );
if( toolNamesAndToolsIt != toolContainer.end() ) {
ToolUpdatedObserver::Pointer updateObserver = ToolUpdatedObserver::New();
//this method can throw an exception,
//the user is responsible for dealing with this
updateObserver->Initialize(this, toolNamesAndOutputFileNamesIt->first, toolNamesAndToolsIt->second, m_WorldReference,
toolNamesAndOutputFileNamesIt->second );
toolNamesAndToolsIt->second->AddObserver(igstk::TrackerToolTransformUpdateEvent(), updateObserver);
}
}
}
//Michel Temp
m_InitialTimeStamp = -1.0;
m_TimeLimit = -1.0;
}
//private:
public:
// igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType *GetTrackerConfiguration( std::string &configurationFileName)
// throw ( ExceptionWithMessage ){
// const unsigned int NUM_TRACKER_TYPES = 7;
// igstk::TrackerConfigurationXMLFileReaderBase::Pointer trackerConfigurationXMLReaders[NUM_TRACKER_TYPES];
// trackerConfigurationXMLReaders[0] = igstk::PolarisVicraConfigurationXMLFileReader::New();
// trackerConfigurationXMLReaders[1] = igstk::PolarisSpectraConfigurationXMLFileReader::New();
// trackerConfigurationXMLReaders[2] = igstk::PolarisHybridConfigurationXMLFileReader::New();
// trackerConfigurationXMLReaders[3] = igstk::AuroraConfigurationXMLFileReader::New();
// trackerConfigurationXMLReaders[4] = igstk::MicronConfigurationXMLFileReader::New();
// trackerConfigurationXMLReaders[5] = igstk::AscensionConfigurationXMLFileReader::New();
// trackerConfigurationXMLReaders[6] = igstk::Ascension3DGConfigurationXMLFileReader::New();
//
// igstk::TrackerDataLoggerConfigurationFileReader::Pointer trackerConfigReader = igstk::TrackerDataLoggerConfigurationFileReader::New();
//
// //need to observe if the request read succeeds or fails
// //there is a third option that the read is invalid, if the
// //file name or xml reader weren't set
// igstk::TrackerDataLoggerConfigurationFileReader::ReadFailSuccessObserver::Pointer
// rfso = igstk::TrackerDataLoggerConfigurationFileReader::ReadFailSuccessObserver::New();
//
// trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::ReadSuccessEvent(),rfso);
// trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::ReadFailureEvent(),rfso);
// trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::UnexpectedTrackerTypeEvent(),rfso );
//
// //setting the file name and reader always succeeds so I don't
// //observe for success event
// trackerConfigReader->RequestSetFileName( configurationFileName );
//
// TrackerConfigurationObserver::Pointer tco = TrackerConfigurationObserver::New();
//
// for( unsigned int i=0; i<NUM_TRACKER_TYPES; i++ )
// {
// //setting the xml reader always succeeds so I don't
// //observe the success event
// trackerConfigReader->RequestSetReader( trackerConfigurationXMLReaders[i]);
// trackerConfigReader->RequestRead();
//
// if( rfso->GotUnexpectedTrackerType())
// {
// rfso->Reset();
// }
// else if( rfso->GotFailure() && !rfso->GotUnexpectedTrackerType() )//<<<<<<<<
// {
// throw ExceptionWithMessage( std::string( "Failed to reset tracker." ) );
// //throw ExceptionWithMessage( rfso->GetFailureMessage() );
// }
// else if( rfso->GotSuccess() )
// {
// //get the configuration data from the reader
// trackerConfigReader->AddObserver(igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataEvent(),tco);
// trackerConfigReader->RequestGetData();
//
// if( tco->GotTrackerConfiguration() )
// {
// igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType *returnedResult =
// new igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType();
// igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType *result = tco->GetTrackerConfiguration();
//
// returnedResult->m_TrackerConfiguration = result->m_TrackerConfiguration;
// returnedResult->m_ToolNamesAndOutputFileNames.insert(
// result->m_ToolNamesAndOutputFileNames.begin(), result->m_ToolNamesAndOutputFileNames.end());
// return returnedResult;
// }
// }
// }
// return NULL;
//}
igstk::TrackerController::Pointer m_TrackerController;
double m_InitialTimeStamp;
double m_TimeLimit;
/* Declarations for Mesh Reader (added functions in igstkTrackerDataLogger.cxx file) */
typedef igstk::MeshReader MeshReaderType; /*typedef for mesh readers */
typedef MeshReaderType::MeshObjectType MeshObjectType; /** typedef for mesh spatial object */
typedef igstk::MeshObjectRepresentation MeshRepresentationType; /*typedef for mesh spatial object representation */
MeshObjectType::Pointer m_MeshSpatialObject;
MeshRepresentationType::Pointer m_MeshRepresentation;
//__________________________________________________________________________________________________________________
/* typedef for axes spatial objects */
typedef igstk::AxesObject AxesObjectType;
typedef igstk::AxesObjectRepresentation AxesRepresentationType;
/** Define a initial world coordinate system */
AxesObjectType::Pointer m_WorldReference;
AxesRepresentationType::Pointer m_WorldReferenceRepresentation;
//__________________________________________________________________________________________________________________
//Declaration for the real-time box object marker (Added code from me)
typedef igstk::BoxObject BoxType;
typedef igstk::BoxObjectRepresentation BoxObjectRepresentationType;
typedef std::vector <BoxType::Pointer> BoxSpatialObjectVectorType;
typedef std::vector <BoxObjectRepresentationType::Pointer> BoxRepresentationVectorType;
BoxSpatialObjectVectorType m_TipSpatialObjectVector;
BoxRepresentationVectorType m_TipRepresentationVector;
//__________________________________________________________________________________________________________________
// Graphics related declaration.
typedef igstk::View2D ViewType2D;
typedef igstk::View3D ViewType3D;
ViewType2D::Pointer DisplayAxial;
ViewType2D::Pointer DisplayCoronal;
ViewType2D::Pointer DisplaySagittal;
ViewType3D::Pointer Display3D;
/*Declaration of the plane slicer*/
typedef igstk::ReslicerPlaneSpatialObject ReslicerPlaneType; /** reslicer plane spatial object */
typedef igstk::MeshResliceObjectRepresentation MeshResliceRepresentationType; /** mesh reslice representation */
ReslicerPlaneType::Pointer m_AxialPlaneSpatialObject;
ReslicerPlaneType::Pointer m_SagittalPlaneSpatialObject;
ReslicerPlaneType::Pointer m_CoronalPlaneSpatialObject;
MeshResliceRepresentationType::Pointer m_AxialMeshResliceRepresentation;
MeshResliceRepresentationType::Pointer m_SagittalMeshResliceRepresentation;
MeshResliceRepresentationType::Pointer m_CoronalMeshResliceRepresentation;
//_______________________________________________________________________________________________________________________
/** typedef for the vector of tracker tools */
typedef std::vector < igstk::TrackerTool::Pointer > ToolVectorType;
//_______________________________________________________________________________________________________________________
igstk::Object::LoggerType::Pointer m_Logger;
//igstk::TrackerController::Pointer m_TrackerController;
//TrackerControllerObserver::Pointer m_TrackerControllerObserver;
ToolVectorType m_ToolVector;
typedef igstk::PolarisTrackerTool pTrackerToolType;
//----------------------------------------------------------------------------
//TrackerDataLogger(){}; //Default constructor
//virtual ~TrackerDataLogger(){ } //Destructor of the object
void SetLogger(igstk::Object::LoggerType * logger) { m_Logger = logger; }
void StartTracking(){
StartTrackingErrorObserver::Pointer steo= StartTrackingErrorObserver::New();
unsigned long observerID = this->m_TrackerController->AddObserver(igstk::TrackerStartTrackingErrorEvent(), steo );
this->m_TrackerController->RequestStartTracking();
this->m_TrackerController->RemoveObserver( observerID );
if( steo->GotStartTrackingError() ){
throw ExceptionWithMessage( std::string( "Failed to start tracking." ) );
}
// create a world reference system
igstk::Transform identity;
identity.SetToIdentity(igstk::TimeStamp::GetLongestPossibleTime());
//m_TrackerController->RequestSetParentSpatialObject( identity, m_WorldReference );
//m_WorldReferenceRepresentation = AxesRepresentationType::New();
/*m_WorldReferenceRepresentation->RequestSetAxesObject( m_WorldReference );
m_WorldReference->SetSize(50,50,50);*/
/*m_WorldReferenceRepresentation = AxesRepresentationType::New();
m_WorldReference->SetSize(50,50,50);
m_WorldReferenceRepresentation->RequestSetAxesObject(m_WorldReference);*/
/*this->DisplayAxial->RequestAddObject(m_WorldReferenceRepresentation);
this->DisplaySagittal->RequestAddObject(m_WorldReferenceRepresentation);
this->DisplayCoronal->RequestAddObject(m_WorldReferenceRepresentation);
this->Display3D->RequestAddObject(m_WorldReferenceRepresentation);*/
ToolVectorType::iterator iter = m_ToolVector.begin();
typedef ::itk::Vector<double, 3> VectorType;
typedef ::itk::Versor<double> VersorType;
typedef pTrackerToolType::TransformType transformType;
int toolCounter = 0;
//transformType identityTransform; // added code
// identityTransform.SetToIdentity(igstk::TimeStamp::GetLongestPossibleTime()); // added code
for ( ; iter != m_ToolVector.end(); iter++, toolCounter++ ){
BoxType::Pointer boxToolType = BoxType::New();///////////////////////////////////////////////////////////////////
boxToolType->SetSize(80,40,40); //SetRadius(10,10,10);
boxToolType->RequestDetachFromParent();
boxToolType->RequestSetTransformAndParent(identity, (*iter));
// //coordSystemAObserver->ObserveTransformEventsFrom( *iter ); ////////// added code June 10th, 2011, 5:01
// transformType transform;
// VectorType position;
// //coordSystemAObserver->Clear();
m_TipSpatialObjectVector.push_back( boxToolType );
BoxObjectRepresentationType::Pointer rep = BoxObjectRepresentationType::New();
rep->RequestSetBoxObject(boxToolType);
double r = ( ( ( double ) ( std::rand( ) ) ) / ( ( double ) ( RAND_MAX ) ) );
double g = ( ( ( double ) ( std::rand( ) ) ) / ( ( double ) ( RAND_MAX ) ) );
double b = ( ( ( double ) ( std::rand( ) ) ) / ( ( double ) ( RAND_MAX ) ) );
rep->SetColor(r, g, b);
m_TipRepresentationVector.push_back( rep );
this->DisplayAxial->RequestAddObject(rep->Copy());
this->DisplaySagittal->RequestAddObject(rep->Copy());
this->DisplayCoronal->RequestAddObject(rep->Copy());
this->Display3D->RequestAddObject(rep->Copy());
/*this->DisplayAxial->RequestSetTransformAndParent(identity, rep);
this->DisplaySagittal->RequestSetTransformAndParent(identity, rep);
this->DisplayCoronal->RequestSetTransformAndParent(identity, rep);
this->Display3D->RequestSetTransformAndParent(identity, rep);*/
}
//// set views' background colors
igstk::AxesObject::Pointer axes = igstk::AxesObject::New();
axes->SetSize(30, 30, 30);
igstk::AxesObjectRepresentation::Pointer trackerAxesRepresentation = igstk::AxesObjectRepresentation::New();
trackerAxesRepresentation->RequestSetAxesObject( axes );
/*this->DisplayAxial->SetRendererBackgroundColor(0,0,0);
this->DisplaySagittal->SetRendererBackgroundColor(0,0,0);
this->DisplayCoronal->SetRendererBackgroundColor(0,0,0);
this->Display3D->SetRendererBackgroundColor(1,1,1); */
//// build scene graph
/*this->DisplayAxial->RequestSetTransformAndParent(identity, rep);
this->DisplaySagittal->RequestSetTransformAndParent(identity, m_WorldReference);
this->DisplayCoronal->RequestSetTransformAndParent(identity, m_WorldReference);
this->Display3D->RequestSetTransformAndParent(identity, this->DisplayAxial);*/
//// set up view parameters
// this->DisplayAxial->SetRefreshRate( 60 );
// this->DisplayAxial->RequestStart();
//this->Display3DWidget->RequestEnableInteractions(); //m_ViewerGroup->m_AxialWidget->RequestEnableInteractions();
// this->DisplaySagittal->SetRefreshRate( 60 );
// this->DisplaySagittal->RequestStart();
//this->DisplaySagittalWidget->RequestEnableInteractions();
// this->DisplayCoronal->SetRefreshRate( 60 );
// this->DisplayCoronal->RequestStart();
//this->DisplayCoronalWidget->RequestEnableInteractions();
//this->Display3D->SetRefreshRate( 60 );
// this->Display3D->RequestStart();
// // add world reference to the views
this->DisplayAxial->RequestAddObject(trackerAxesRepresentation);
this->DisplaySagittal->RequestAddObject(trackerAxesRepresentation);
this->DisplayCoronal->RequestAddObject(trackerAxesRepresentation);
this->Display3D->RequestAddObject(trackerAxesRepresentation);
axes->RequestSetTransformAndParent(identity, this->Display3D);
axes->RequestSetTransformAndParent(identity, this->DisplayAxial);
axes->RequestSetTransformAndParent(identity, this->DisplayCoronal);
axes->RequestSetTransformAndParent(identity, this->DisplaySagittal);
//this->DisplayCoronal->RequestSetTransformAndParent(identity, axes);
// // reset cameras in all the views
this->DisplayAxial->RequestResetCamera();
this->DisplaySagittal->RequestResetCamera();
this->DisplayCoronal->RequestResetCamera();
this->Display3D->RequestResetCamera();
//return;
}
void StopTracking(){
//std::cerr << " stop tracking ...\n";
StopTrackingErrorObserver::Pointer steo= StopTrackingErrorObserver::New();
unsigned long observerID = this->m_TrackerController->AddObserver(igstk::TrackerStopTrackingErrorEvent(), steo );
this->m_TrackerController->RequestStopTracking();
this->m_TrackerController->RemoveObserver( observerID );
if( steo->GotStopTrackingError() ){
throw ExceptionWithMessage( std::string( "Failed to stop tracking." ) );
}
}
void LoadWorkingVolumeMesh(){ ///////////// Added function from TrackerVolumeViewer
const char* fileName = fl_file_chooser("Select the mesh file","*.msh", "");
if (!fileName){
throw ExceptionWithMessage( std::string( "No mesh file found!\n" ) );
}
MeshReaderType::Pointer reader = MeshReaderType::New();
reader->RequestSetFileName( fileName );
reader->RequestReadObject();
MeshObjectObserver::Pointer observer = MeshObjectObserver::New();
reader->AddObserver( igstk::MeshReader::MeshModifiedEvent(), observer);
reader->RequestGetOutput();
if (!observer->GotMeshObject()){
throw ExceptionWithMessage( std::string( "Cannot read mesh file!\n" ) );
}
m_MeshSpatialObject = observer->GetMeshObject();
if ( m_MeshSpatialObject.IsNull() ){
throw ExceptionWithMessage( std::string( "Cannot read mesh file!\n" ) );
}
igstk::Transform identity;
identity.SetToIdentity( igstk::TimeStamp::GetLongestPossibleTime() );
// we want the mesh spatial object to be a child of the tracker
m_MeshSpatialObject->RequestDetachFromParent();
m_TrackerController->RequestAddChildSpatialObject( identity, m_MeshSpatialObject );
// build the mesh representation
MeshRepresentationType::Pointer m_MeshRepresentation = MeshRepresentationType::New();
m_MeshRepresentation->RequestSetMeshObject( m_MeshSpatialObject );
m_MeshRepresentation->SetOpacity(0.3);
m_MeshRepresentation->SetColor(0, 0, 1);
// add the mesh representation only to the 3D view
this->Display3D->RequestAddObject(m_MeshRepresentation);
//
// // create reslice plane spatial object for axial view
m_AxialPlaneSpatialObject = ReslicerPlaneType::New();
m_AxialPlaneSpatialObject->RequestSetReslicingMode( ReslicerPlaneType::Orthogonal );
m_AxialPlaneSpatialObject->RequestSetOrientationType( ReslicerPlaneType::Axial );
m_AxialPlaneSpatialObject->RequestSetBoundingBoxProviderSpatialObject( m_MeshSpatialObject );
m_AxialPlaneSpatialObject->RequestSetToolSpatialObject( m_TipSpatialObjectVector[0] );
m_AxialPlaneSpatialObject->RequestSetTransformAndParent( identity, m_WorldReference );
//// create reslice plane spatial object for sagittal view
m_SagittalPlaneSpatialObject = ReslicerPlaneType::New();
m_SagittalPlaneSpatialObject->RequestSetReslicingMode( ReslicerPlaneType::Orthogonal );
m_SagittalPlaneSpatialObject->RequestSetOrientationType( ReslicerPlaneType::Sagittal );
m_SagittalPlaneSpatialObject->RequestSetBoundingBoxProviderSpatialObject( m_MeshSpatialObject );
m_SagittalPlaneSpatialObject->RequestSetToolSpatialObject( m_TipSpatialObjectVector[0] );
m_SagittalPlaneSpatialObject->RequestSetTransformAndParent( identity, m_WorldReference );
// create reslice plane spatial object for coronal view
m_CoronalPlaneSpatialObject = ReslicerPlaneType::New();
m_CoronalPlaneSpatialObject->RequestSetReslicingMode( ReslicerPlaneType::Orthogonal );
m_CoronalPlaneSpatialObject->RequestSetOrientationType( ReslicerPlaneType::Coronal );
m_CoronalPlaneSpatialObject->RequestSetBoundingBoxProviderSpatialObject( m_MeshSpatialObject );
m_CoronalPlaneSpatialObject->RequestSetToolSpatialObject( m_TipSpatialObjectVector[0] );
m_CoronalPlaneSpatialObject->RequestSetTransformAndParent( identity, m_WorldReference );
// create a mesh reslice representation for axial view
MeshResliceRepresentationType::Pointer m_AxialMeshResliceRepresentation = MeshResliceRepresentationType::New();
m_AxialMeshResliceRepresentation->RequestSetMeshObject( m_MeshSpatialObject );
m_AxialMeshResliceRepresentation->RequestSetReslicePlaneSpatialObject( m_AxialPlaneSpatialObject );
m_AxialMeshResliceRepresentation->SetOpacity(1.0);
m_AxialMeshResliceRepresentation->SetLineWidth(1.0);
m_AxialMeshResliceRepresentation->SetColor(1, 1, 0);
// create a mesh reslice representation for sagittal view
MeshResliceRepresentationType::Pointer m_SagittalMeshResliceRepresentation = MeshResliceRepresentationType::New();
m_SagittalMeshResliceRepresentation->RequestSetMeshObject( m_MeshSpatialObject );
m_SagittalMeshResliceRepresentation->RequestSetReslicePlaneSpatialObject( m_SagittalPlaneSpatialObject );
m_SagittalMeshResliceRepresentation->SetOpacity(1.0);
m_SagittalMeshResliceRepresentation->SetLineWidth(1.0);
m_SagittalMeshResliceRepresentation->SetColor(1, 1, 0);
// create a mesh reslice representation for coronal view
MeshResliceRepresentationType::Pointer m_CoronalMeshResliceRepresentation = MeshResliceRepresentationType::New();
m_CoronalMeshResliceRepresentation->RequestSetMeshObject( m_MeshSpatialObject );
m_CoronalMeshResliceRepresentation->RequestSetReslicePlaneSpatialObject( m_CoronalPlaneSpatialObject );
m_CoronalMeshResliceRepresentation->SetOpacity(1.0);
m_CoronalMeshResliceRepresentation->SetLineWidth(1.0);
m_CoronalMeshResliceRepresentation->SetColor(1, 1, 0);
//
// // todo: see the camera issue within childs and parents for the case of
// // a View and a ReslicerPlaneSpatialObjet, respectively.
this->DisplayAxial->RequestDetachFromParent();
this->DisplayAxial->RequestSetTransformAndParent(identity, m_AxialPlaneSpatialObject );
//
this->DisplaySagittal->RequestDetachFromParent();
this->DisplaySagittal->RequestSetTransformAndParent(identity, m_SagittalPlaneSpatialObject);
//
this->DisplayCoronal->RequestDetachFromParent();
this->DisplayCoronal->RequestSetTransformAndParent(identity, m_CoronalPlaneSpatialObject );
// // add axial mesh reslice representation to the 2D and 3D views
this->DisplayAxial->RequestAddObject( m_AxialMeshResliceRepresentation );
this->Display3D->RequestAddObject( m_AxialMeshResliceRepresentation->Copy() );
//
// // add sagittal mesh reslice representation to the 2D and 3D views
this->DisplaySagittal->RequestAddObject( m_SagittalMeshResliceRepresentation );
this->Display3D->RequestAddObject( m_SagittalMeshResliceRepresentation->Copy() );
//
// // add coronal mesh reslice representation to the 2D and 3D views
this->DisplayCoronal->RequestAddObject( m_CoronalMeshResliceRepresentation );
this->Display3D->RequestAddObject( m_CoronalMeshResliceRepresentation->Copy() );
//
// // set background color to the views
this->DisplayAxial->SetRendererBackgroundColor(0,0,0);
this->DisplaySagittal->SetRendererBackgroundColor(0,0,0);
this->DisplayCoronal->SetRendererBackgroundColor(0,0,0);
this->Display3D->SetRendererBackgroundColor(1,1,1);
//
// // set parallel projection to the camera
this->DisplayAxial->SetCameraParallelProjection(true);
this->DisplaySagittal->SetCameraParallelProjection(true);
this->DisplayCoronal->SetCameraParallelProjection(true);
// // reset cameras in the different views
this->DisplayAxial->RequestResetCamera();
this->DisplaySagittal->RequestResetCamera();
this->DisplayCoronal->RequestResetCamera();
this->Display3D->RequestResetCamera();
}
void LoadTrackerMesh(){ ///////////// Added function from TrackerVolumeViewer
const char* fileName = fl_file_chooser("Select the mesh file","*.msh", "");
if ( fileName != NULL )
{
MeshReaderType::Pointer reader = MeshReaderType::New();
reader->RequestSetFileName( fileName );
reader->RequestReadObject();
MeshObjectObserver::Pointer observer = MeshObjectObserver::New();
reader->AddObserver( igstk::MeshReader::MeshModifiedEvent(), observer);
reader->RequestGetOutput();
if(!observer->GotMeshObject())
{
throw ExceptionWithMessage( std::string( "Cannot read mesh file!\n" ) );
}
m_MeshSpatialObject = observer->GetMeshObject();
if ( m_MeshSpatialObject.IsNull() )
{
throw ExceptionWithMessage( std::string( "Cannot read mesh!\n" ) );
}
igstk::Transform identity;
identity.SetToIdentity( igstk::TimeStamp::GetLongestPossibleTime() );
// we want the mesh to be a child of the tracker
m_MeshSpatialObject->RequestDetachFromParent();
m_TrackerController->RequestAddChildSpatialObject( identity, m_MeshSpatialObject );
MeshRepresentationType::Pointer m_MeshRepresentation = MeshRepresentationType::New();
m_MeshRepresentation->RequestSetMeshObject( m_MeshSpatialObject );
m_MeshRepresentation->SetOpacity(0.3);
m_MeshRepresentation->SetColor(1,1,0);
this->DisplayAxial->RequestAddObject( m_MeshRepresentation->Copy() );
this->DisplaySagittal->RequestAddObject( m_MeshRepresentation->Copy() );
this->DisplayCoronal->RequestAddObject( m_MeshRepresentation->Copy() );
this->Display3D->RequestAddObject( m_MeshRepresentation->Copy() );
this->DisplayAxial->RequestResetCamera();
this->DisplaySagittal->RequestResetCamera();
this->DisplayCoronal->RequestResetCamera();
this->Display3D->RequestResetCamera();
return;
}
else{
throw ExceptionWithMessage( std::string( "No directory is selected\n" ) );
}
}
void ExitSuccessfully(){
exit( EXIT_SUCCESS );
}
igstkGetMacro( InitialTimeStamp, double );
igstkGetMacro( TimeLimit, double );
igstkSetMacro( InitialTimeStamp, double );
igstkSetMacro( TimeLimit, double );
private:
/**
* Observer for the event generated by
* TrackerConfigurationLoggerFileReader->RequestGetData() method.
*/
igstkObserverMacro( TrackerConfiguration, igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataEvent,
igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataPointerType )
/**
* Observer for the TrackerController->RequestInitialize() failure.
*/
igstkObserverMacro( InitializeError, igstk::TrackerController::InitializeErrorEvent, std::string )
/**
* Observer for the TrackerController->RequestStartTracking() failure.
*/
igstkObserverMacro( StartTrackingError, igstk::TrackerStartTrackingErrorEvent, std::string )
/**
* Observer for the TrackerController->RequestStopTracking() failure.
*/
igstkObserverMacro( StopTrackingError, igstk::TrackerStopTrackingErrorEvent, std::string )
/**
* Observer for the TrackerController->RequestGetNonReferenceToolList()
*/
igstkObserverMacro( ToolList, igstk::TrackerController::RequestToolsEvent, igstk::TrackerController::ToolContainerType )
/**
* Observer for the TrackerController->RequestGetTool() and TrackerController->RequestGetReferenceTool()
*/
igstkObserverMacro( Tool, igstk::TrackerController::RequestToolEvent, igstk::TrackerController::ToolEntryType )
/** Define observers for event communication */ //Newly Added ---------------------------<<<<<<<<
igstkObserverObjectMacro(MeshObject, igstk::MeshReader::MeshModifiedEvent, igstk::MeshObject);
igstkEventMacro( InvalidRequestErrorEvent, igstk::IGSTKErrorEvent );
/**
* This class observes the TrackerToolTransformUpdateEvent for a specific tool.
* It checks that the event is for the relevant tool and then gets the tool's
* transform w.r.t. the "world" coordinate system. The transform is then
* broadcasted to all destinations.
*/
// BeginLatex
// \code{ToolUpdatedObserver} class observes the
// TrackerToolTransformUpdateEvent for a specific tool. It checks that
// the event is for the relevant tool and then gets the tool's
// transform
// to its parent and sends it via socket.
// EndLatex
class ToolUpdatedObserver : public ::itk::Command
{
public:
typedef ToolUpdatedObserver Self;
typedef ::itk::Command Superclass;
typedef ::itk::SmartPointer<Self> Pointer;
itkNewMacro( Self );
//typedef ofstream* OfstreamPointer;
private:
std::string m_HostName;
unsigned int m_PortNumber;
//we are interested in the tool location relative to the world's
//coordinate system
igstk::SpatialObject::Pointer m_World;
igstk::TransformObserver::Pointer m_TransformObserver;
igstk::TrackerTool::Pointer m_Tool;
TrackerDataLogger *m_Logger;
//send data to these ofstreams
std::vector< ofstream* > m_Ofstreams;
std::string m_PositionMessage;
protected:
// BeginLatex
// In the \code{ToolUpdatedObserver} constructor we instantiate a
// PositionMessage, which is updated with the transformation data.
// EndLatex
ToolUpdatedObserver()
{
this->m_TransformObserver = igstk::TransformObserver::New();
// BeginCodeSnippet
//this->m_PositionMessage = igtl::PositionMessage::New();
// EndCodeSnippet
}
~ToolUpdatedObserver()
{
std::vector< ofstream* >::iterator it;
for (it = this->m_Ofstreams.begin(); it != this->m_Ofstreams.end(); ++it)
{
(*it)->close();
delete (*it);
}
this->m_Ofstreams.clear();
}
public:
//void Initialize(TrackerDataLogger *logger, const std::string toolName, igstk::TrackerTool::Pointer trackerTool,
//igstk::SpatialObject::Pointer world, std::vector< std::string > & outputFileNames)
void Initialize(TrackerDataLogger *logger, const std::string toolName, igstk::TrackerTool::Pointer trackerTool,
AxesObjectType::Pointer m_WorldReference, std::vector< std::string > & outputFileNames) {
this->m_Logger = logger;
this->m_Tool = trackerTool;
this->m_World = m_WorldReference;
this->m_TransformObserver->ObserveTransformEventsFrom( this->m_Tool );
this->m_PositionMessage.assign( toolName.c_str() );
std::vector< ofstream* >::iterator it;
for (it = this->m_Ofstreams.begin(); it != this->m_Ofstreams.end(); ++it)
{
(*it)->close();
delete (*it);
}
this->m_Ofstreams.clear();
// BeginLatex
// We create a list of output files to write to.
// Then we open each file for writing.
// EndLatex
// BeginCodeSnippet
std::vector< std::string >::iterator outputFileNamesIt;
for ( outputFileNamesIt = outputFileNames.begin(); outputFileNamesIt != outputFileNames.end(); ++outputFileNamesIt)
{
ofstream *outputFile = new ofstream ( outputFileNamesIt->c_str() , ios::out );
//outputFile.open();
if ( !outputFile->is_open() )
{
for (it = this->m_Ofstreams.begin(); it != this->m_Ofstreams.end(); ++it)
{
(*it)->close();
delete (*it);
}
this->m_Ofstreams.clear();
std::ostringstream msg;
msg<<" Failed to open file " << (*outputFileNamesIt);
throw ExceptionWithMessage( msg.str() );
}
this->m_Ofstreams.push_back(outputFile);
}
// EndCodeSnippet
}
void Execute(itk::Object *caller, const itk::EventObject & event)
{
const itk::Object * constCaller = caller;
this->Execute( constCaller, event );
}
// BeginLatex
// In \emph{ToolUpdatedObserver::Excecute()}, we define the event
// handler to receive a transform, fill the Logger message, and
// send it out.
// EndLatex
void Execute(const itk::Object *caller, const itk::EventObject & event)
{
//if no outputFiles open for writing, just return
if( m_Ofstreams.empty() )
{
return;
}
//do something only for the correct tool
if( this->m_Tool.GetPointer() == caller )
{ //the tool transform has been updated, get it
if( dynamic_cast<const igstk::TrackerToolTransformUpdateEvent *>( &event))
{ //request to get the transform
this->m_Tool->RequestComputeTransformTo( this->m_World );
//check that we got it
if ( this->m_TransformObserver->GotTransform() )
{
// BeginCodeSnippet
char buffer[100];
double timeStamp = igstk::RealTimeClock::GetTimeStamp();
if (this->m_Logger->GetInitialTimeStamp() < 0.0)
{
this->m_Logger->SetInitialTimeStamp( timeStamp );
}
double timeDifference = timeStamp - this->m_Logger->GetInitialTimeStamp();
if (this->m_Logger->GetTimeLimit() > 0.0)
{
//std::cerr << " timeDifference " << timeDifference << "\n";
if (timeDifference > this->m_Logger->GetTimeLimit() )
{
//std::cerr << " want to stop tracking \n";
this->m_Logger->StopTracking();
this->m_Logger->ExitSuccessfully();
}
}
igstk::Transform transform = this->m_TransformObserver->GetTransform();
igstk::Transform::VectorType t = transform.GetTranslation();
igstk::Transform::VersorType r = transform.GetRotation();
sprintf(buffer, "%f %f %f %f %f %f %f %f \n", timeDifference , t[0], t[1], t[2],
r.GetX(), r.GetY(), r.GetZ(), r.GetW());
//std::cerr << buffer;
this->m_PositionMessage.assign(buffer);
std::vector< ofstream * >::iterator it;
for (it = this->m_Ofstreams.begin(); it != this->m_Ofstreams.end(); ++it)
{
(*(*it)) << this->m_PositionMessage;
}
// EndCodeSnippet
std::cout << this->m_PositionMessage;
}
}
}
}
//private:
//std::string m_HostName;
//unsigned int m_PortNumber;
////we are interested in the tool location relative to the world's
////coordinate system
//igstk::SpatialObject::Pointer m_World;
//igstk::TransformObserver::Pointer m_TransformObserver;
//igstk::TrackerTool::Pointer m_Tool;
//TrackerDataLogger *m_Logger;
////send data to these ofstreams
//std::vector< ofstream* > m_Ofstreams;
//std::string m_PositionMessage;
};
};
#endif //__igstkTrackerDataLogger_h
More information about the IGSTK-Users
mailing list