Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkIGTLMessage.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkIGTLMessage.h"
18 #include "mitkException.h"
19 #include "mitkIGTLMessageCommon.h"
20 
22 m_DataValid(false), m_IGTTimeStamp(0.0), m_Name()
23 {
25 }
26 
27 
29  itk::DataObject()
30 {
31  // TODO SW: Graft does the same, remove code duplications, set Graft to
32  // deprecated, remove duplication in tescode
33  this->Graft(&toCopy);
34 }
35 
37 {
38 }
39 
41  std::string name)
42 {
43  this->SetMessage(message);
44  this->SetName(name);
45 }
46 
47 void mitk::IGTLMessage::Graft( const DataObject *data )
48 {
49  // Attempt to cast data to an IGTLMessage
50  const Self* msg;
51  try
52  {
53  msg = dynamic_cast<const Self *>(data);
54  }
55  catch( ... )
56  {
57  itkExceptionMacro( << "mitk::IGTLMessage::Graft cannot cast "
58  << typeid(data).name() << " to "
59  << typeid(const Self *).name() );
60  return;
61  }
62  if (!msg)
63  {
64  // pointer could not be cast back down
65  itkExceptionMacro( << "mitk::IGTLMessage::Graft cannot cast "
66  << typeid(data).name() << " to "
67  << typeid(const Self *).name() );
68  return;
69  }
70  // Now copy anything that is needed
71  this->SetMessage(msg->GetMessage());
72  this->SetDataValid(msg->IsDataValid());
73  this->SetIGTTimeStamp(msg->GetIGTTimeStamp());
74  this->SetName(msg->GetName());
75 }
76 
78 {
79  m_Message = msg;
80  unsigned int ts = 0;
81  unsigned int frac = 0;
82  m_Message->GetTimeStamp(&ts, &frac);
83  this->SetIGTTimeStamp((double)ts + (double)frac/1000.0);
84  this->SetDataValid(true);
85 }
86 
88 {
89  return m_DataValid;
90 }
91 
92 
93 void mitk::IGTLMessage::PrintSelf(std::ostream& os, itk::Indent indent) const
94 {
95  this->Superclass::PrintSelf(os, indent);
96  os << indent << "name: " << this->GetName() << std::endl;
97  os << indent << "data valid: " << this->IsDataValid() << std::endl;
98  os << indent << "TimeStamp: " << this->GetIGTTimeStamp() << std::endl;
99  os << indent << "OpenIGTLinkMessage: " << std::endl;
100  m_Message->Print(os);
101 }
102 
103 
104 void mitk::IGTLMessage::CopyInformation( const DataObject* data )
105 {
106  this->Superclass::CopyInformation( data );
107 
108  const Self * nd = nullptr;
109  try
110  {
111  nd = dynamic_cast<const Self*>(data);
112  }
113  catch( ... )
114  {
115  // data could not be cast back down
116  itkExceptionMacro(<< "mitk::IGTLMessage::CopyInformation() cannot cast "
117  << typeid(data).name() << " to "
118  << typeid(Self*).name() );
119  }
120  if ( !nd )
121  {
122  // pointer could not be cast back down
123  itkExceptionMacro(<< "mitk::IGTLMessage::CopyInformation() cannot cast "
124  << typeid(data).name() << " to "
125  << typeid(Self*).name() );
126  }
127  /* copy all meta data */
128 }
129 
130 bool mitk::Equal(const mitk::IGTLMessage& leftHandSide,
131  const mitk::IGTLMessage& rightHandSide,
132  ScalarType /*eps*/, bool verbose)
133 {
134  bool returnValue = true;
135 
136  if( std::string(rightHandSide.GetName()) != std::string(leftHandSide.GetName()) )
137  {
138  if(verbose)
139  {
140  MITK_INFO << "[( IGTLMessage )] Name differs.";
141  MITK_INFO << "leftHandSide is " << leftHandSide.GetName()
142  << "rightHandSide is " << rightHandSide.GetName();
143  }
144  returnValue = false;
145  }
146 
147  if( rightHandSide.GetIGTTimeStamp() != leftHandSide.GetIGTTimeStamp() )
148  {
149  if(verbose)
150  {
151  MITK_INFO << "[( IGTLMessage )] IGTTimeStamp differs.";
152  MITK_INFO << "leftHandSide is " << leftHandSide.GetIGTTimeStamp()
153  << "rightHandSide is " << rightHandSide.GetIGTTimeStamp();
154  }
155  returnValue = false;
156  }
157 
158  return returnValue;
159 }
160 
162 {
163  return this->m_Message->GetDeviceType();
164 }
165 
166 template < typename IGTLMessageType >
167 IGTLMessageType* mitk::IGTLMessage::GetMessage() const
168 {
169  return dynamic_cast<IGTLMessageType*>(this->m_Message);
170 }
virtual bool IsDataValid() const
returns true if the object contains valid data
virtual igtl::MessageBase::Pointer GetMessage() const
returns the OpenIGTLink message
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
void SetMessage(igtl::MessageBase::Pointer msg)
Sets the OpenIGTLink message.
double ScalarType
const char * GetIGTLMessageType() const
A wrapper for the OpenIGTLink message type.
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Prints the object information to the given stream os.
virtual const char * GetName() const
returns the name of the IGTLMessage object
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static std::string GetName(std::string fileName, std::string suffix)
igtl::MessageBase::Pointer m_Message
holds the actual OpenIGTLink message
virtual void Graft(const DataObject *data) override
Graft the data and information from one IGTLMessage to another.
virtual void CopyInformation(const DataObject *data) override
copy meta data of a IGTLMessage object
virtual TimeStampType GetIGTTimeStamp() const
gets the IGT timestamp of the IGTLMessage object
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.