MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkIGTLMessage.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
13#include "mitkIGTLMessage.h"
14#include "mitkException.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
35
36mitk::IGTLMessage::IGTLMessage(igtl::MessageBase::Pointer message)
37{
38 this->SetMessage(message);
39 this->SetName(message->GetDeviceName());
40}
41
42void 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
72void 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
90void 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
102{
103 std::stringstream output;
104 this->Print(output);
105 return output.str();
106}
107
108
109void 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
135bool 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
171template < typename IGTLMessageType >
172IGTLMessageType* mitk::IGTLMessage::GetMessage() const
173{
174 return dynamic_cast<IGTLMessageType*>(this->m_Message);
175}
A wrapper for the OpenIGTLink message type.
igtl::MessageBase::Pointer m_Message
holds the actual OpenIGTLink message
void CopyInformation(const DataObject *data) override
copy meta data of a IGTLMessage object
virtual bool IsDataValid() const
returns true if the object contains valid data
std::string ToString() const
void Graft(const DataObject *data) override
Graft the data and information from one IGTLMessage to another.
const char * GetIGTLMessageType() const
IGTLMessageType * GetMessage() const
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Prints the object information to the given stream os.
void SetMessage(igtl::MessageBase::Pointer msg)
Sets the OpenIGTLink message.
MITKIGTBASE_EXPORT bool Equal(const mitk::NavigationData &leftHandSide, const mitk::NavigationData &rightHandSide, ScalarType eps=mitk::eps, bool verbose=false)
Equal A function comparing two navigation data objects for beeing equal in meta- and imagedata.