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
|
/*=========================================================================
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 "igstkTrackerConfiguration.h"
#include "igstkTrackerController.h"
#include "igstkTransformObserver.h"
#include "igstkTrackerDataLoggerConfigurationFileReader.h"
#include "igstkTrackerDataLoggerConfigurationXMLFileReader.h"
#include <fstream>
/**
* \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:
/**
* \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( std::string &trackerXMLConfigurationFileName )
throw ( ExceptionWithMessage );
virtual ~TrackerDataLogger(){ }
void StartTracking();
void StopTracking();
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 )
/**
* 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;
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)
{
this->m_Logger = logger;
this->m_Tool = trackerTool;
this->m_World = world;
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;
};
private:
igstk::TrackerDataLoggerConfigurationFileReader::ConfigurationDataType *
GetTrackerConfiguration( std::string &configurationFileName)
throw ( ExceptionWithMessage );
igstk::TrackerController::Pointer m_TrackerController;
double m_InitialTimeStamp;
double m_TimeLimit;
};
#endif //__igstkTrackerDataLogger_h
|