Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkIGTLMessage.h"
14 #include "mitkException.h"
15 #include "mitkIGTLMessageCommon.h"
16 
18  m_DataValid(false), m_IGTTimeStamp(0), m_Name()
19 {
20  m_Message = igtl::MessageBase::New();
21 }
22 
23 
25  itk::DataObject()
26 {
27  // TODO SW: Graft does the same, remove code duplications, set Graft to
28  // deprecated, remove duplication in tescode
29  this->Graft(&toCopy);
30 }
31 
33 {
34 }
35 
36 mitk::IGTLMessage::IGTLMessage(igtl::MessageBase::Pointer message)
37 {
38  this->SetMessage(message);
39  this->SetName(message->GetDeviceName());
40 }
41 
42 void mitk::IGTLMessage::Graft( const DataObject *data )
43 {
44  // Attempt to cast data to an IGTLMessage
45  const Self* msg;
46  try
47  {
48  msg = dynamic_cast<const Self *>(data);
49  }
50  catch( ... )
51  {
52  itkExceptionMacro( << "mitk::IGTLMessage::Graft cannot cast "
53  << typeid(data).name() << " to "
54  << typeid(const Self *).name() );
55  return;
56  }
57  if (!msg)
58  {
59  // pointer could not be cast back down
60  itkExceptionMacro( << "mitk::IGTLMessage::Graft cannot cast "
61  << typeid(data).name() << " to "
62  << typeid(const Self *).name() );
63  return;
64  }
65  // Now copy anything that is needed
66  this->SetMessage(msg->GetMessage());
67  this->SetDataValid(msg->IsDataValid());
68  this->SetIGTTimeStamp(msg->GetIGTTimeStamp());
69  this->SetName(msg->GetName());
70 }
71 
72 void mitk::IGTLMessage::SetMessage(igtl::MessageBase::Pointer msg)
73 {
74  m_Message = msg;
75  unsigned int ts = 0;
76  unsigned int frac = 0;
77  m_Message->GetTimeStamp(&ts, &frac); //ts = seconds / frac = nanoseconds
78  this->SetName(m_Message->GetDeviceName());
79  double timestamp = ts * 1000.0 + frac;
80  this->SetIGTTimeStamp(timestamp);
81  this->SetDataValid(true);
82 }
83 
85 {
86  return m_DataValid;
87 }
88 
89 
90 void mitk::IGTLMessage::PrintSelf(std::ostream& os, itk::Indent indent) const
91 {
92  os << indent << "name: " << this->GetName() << std::endl;
93  os << indent << "type: " << this->GetIGTLMessageType() << std::endl;
94  os << indent << "valid: " << this->IsDataValid() << std::endl;
95  os << indent << "timestamp: " << this->GetIGTTimeStamp() << std::endl;
96  os << indent << "OpenIGTLinkMessage: " << std::endl;
97  m_Message->Print(os);
98  this->Superclass::PrintSelf(os, indent);
99 }
100 
101 std::string mitk::IGTLMessage::ToString() const
102 {
103  std::stringstream output;
104  this->Print(output);
105  return output.str();
106 }
107 
108 
109 void mitk::IGTLMessage::CopyInformation( const DataObject* data )
110 {
111  this->Superclass::CopyInformation( data );
112 
113  const Self * nd = nullptr;
114  try
115  {
116  nd = dynamic_cast<const Self*>(data);
117  }
118  catch( ... )
119  {
120  // data could not be cast back down
121  itkExceptionMacro(<< "mitk::IGTLMessage::CopyInformation() cannot cast "
122  << typeid(data).name() << " to "
123  << typeid(Self*).name() );
124  }
125  if ( !nd )
126  {
127  // pointer could not be cast back down
128  itkExceptionMacro(<< "mitk::IGTLMessage::CopyInformation() cannot cast "
129  << typeid(data).name() << " to "
130  << typeid(Self*).name() );
131  }
132  /* copy all meta data */
133 }
134 
135 bool mitk::Equal(const mitk::IGTLMessage& leftHandSide,
136  const mitk::IGTLMessage& rightHandSide,
137  ScalarType /*eps*/, bool verbose)
138 {
139  bool returnValue = true;
140 
141  if( std::string(rightHandSide.GetName()) != std::string(leftHandSide.GetName()) )
142  {
143  if(verbose)
144  {
145  MITK_INFO << "[( IGTLMessage )] Name differs.";
146  MITK_INFO << "leftHandSide is " << leftHandSide.GetName()
147  << "rightHandSide is " << rightHandSide.GetName();
148  }
149  returnValue = false;
150  }
151 
152  if( rightHandSide.GetIGTTimeStamp() != leftHandSide.GetIGTTimeStamp() )
153  {
154  if(verbose)
155  {
156  MITK_INFO << "[( IGTLMessage )] IGTTimeStamp differs.";
157  MITK_INFO << "leftHandSide is " << leftHandSide.GetIGTTimeStamp()
158  << "rightHandSide is " << rightHandSide.GetIGTTimeStamp();
159  }
160  returnValue = false;
161  }
162 
163  return returnValue;
164 }
165 
167 {
168  return this->m_Message->GetDeviceType();
169 }
170 
171 template < typename IGTLMessageType >
172 IGTLMessageType* mitk::IGTLMessage::GetMessage() const
173 {
174  return dynamic_cast<IGTLMessageType*>(this->m_Message);
175 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
void SetMessage(igtl::MessageBase::Pointer msg)
Sets the OpenIGTLink message.
double ScalarType
bool m_DataValid
defines if the object contains valid values
virtual void SetName(const char *_arg)
set the name of the IGTLMessage object
virtual bool IsDataValid() const
returns true if the object contains valid data
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.
bool verbose(false)
const char * GetIGTLMessageType() const
std::string ToString() const
virtual igtl::MessageBase::Pointer GetMessage() const
returns the OpenIGTLink message
virtual void SetDataValid(bool _arg)
sets the dataValid flag of the IGTLMessage object indicating if the object contains valid data ...
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.
virtual const char * GetName() const
returns the name of the IGTLMessage object
virtual TimeStampType GetIGTTimeStamp() const
gets the IGT timestamp of the IGTLMessage object
igtl::MessageBase::Pointer m_Message
holds the actual OpenIGTLink message
void Graft(const DataObject *data) override
Graft the data and information from one IGTLMessage to another.
void CopyInformation(const DataObject *data) override
copy meta data of a IGTLMessage object