Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkIGTLMessageFactory.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 "mitkIGTLMessageFactory.h"
18 
19 // IGT message types
20 #include "igtlImageMessage.h"
21 #include "igtlTransformMessage.h"
22 #include "igtlPositionMessage.h"
23 #include "igtlStatusMessage.h"
24 #include "igtlImageMetaMessage.h"
25 #include "igtlPointMessage.h"
26 #include "igtlTrajectoryMessage.h"
27 #include "igtlStringMessage.h"
28 #include "igtlSensorMessage.h"
29 #include "igtlBindMessage.h"
30 #include "igtlPolyDataMessage.h"
31 #include "igtlQuaternionTrackingDataMessage.h"
32 #include "igtlCapabilityMessage.h"
33 #include "igtlNDArrayMessage.h"
34 #include "igtlTrackingDataMessage.h"
35 #include "igtlColorTableMessage.h"
36 #include "igtlLabelMetaMessage.h"
37 
38 //own types
39 #include "mitkIGTLDummyMessage.h"
40 
41 #include "mitkIGTLMessageCommon.h"
42 
43 #include "itksys/SystemTools.hxx"
44 
45 //------------------------------------------------------------
46 // Define message clone classes
47 // igtlMessageHandlerClassMacro() defines a child class of
48 // igtl::MessageHandler to handle OpenIGTLink messages for
49 // the message type specified as the first argument. The
50 // second argument will be used for the name of this
51 // message handler class, while the third argument specifies
52 // a type of data that will be shared with the message functions
53 // of this handler class.
54 
55 mitkIGTMessageCloneClassMacro(igtl::TransformMessage, TransformMsgCloneHandler);
56 
64 {
65  bool copySuccess = false;
67 
68  //initialize the clone
69  // clone = igtl::MessageBase::New();
70 
71  igtl::TransformMessage* original = (igtl::TransformMessage*)original_;
72 
73  //copy all meta data
74  copySuccess = clone_->Copy(original);
75 
76  if (!copySuccess)
77  return nullptr;
78 
79  //copy all data that is important for this class
80  //copy the matrix
81  igtl::Matrix4x4 mat;
82  original->GetMatrix(mat);
83  clone_->SetMatrix(mat);
84 
85  //copy the normals
86  float normals[3][3];
87  original->GetNormals(normals);
88  clone_->SetNormals(normals);
89 
90  //copy the position
91  float position[3];
92  original->GetPosition(position);
93  clone_->SetPosition(position);
94 
95  return igtl::MessageBase::Pointer(clone_.GetPointer());
96 }
97 
99 {
100  //create clone handlers
101  // mitk::IGTLMessageCloneHandler::Pointer tmch = ;
102 
103  this->AddMessageNewMethod("NONE", nullptr);
104 
105  //OpenIGTLink Types V1
111 
114  //this->AddMessageNewMethod("GET_POS", (PointerToMessageBaseNew)&igtl::GetPositionMessage::New); //not available???
117 
118  // //OpenIGTLink Types V2
131 
137  // this->AddMessageNewMethod("GET_TDATA", (PointerToMessageBaseNew)&igtl::GetTrackingDataMessage::New); //not available???
138  // this->AddMessageNewMethod("GET_QTDATA", (PointerToMessageBaseNew)&igtl::GetQuaternionTrackingDataMessage::New); //not available???
139  // this->AddMessageNewMethod("GET_SENSOR", (PointerToMessageBaseNew)&igtl::GetSensorMessage::New); //not available???
140  // this->AddMessageNewMethod("GET_STRING", (PointerToMessageBaseNew)&igtl::GetStringMessage::New); //not available???
141  // this->AddMessageNewMethod("GET_NDARRAY", (PointerToMessageBaseNew)&igtl::GetNDArrayMessage::New); //not available???
144 
148  //todo: check if there are more RTS messages
149 
153  //todo: check if there are more STT messages
154 
158  //todo: check if there are more STP messages
159 
160  //Own Types
162 }
163 
165 {
166 }
167 
168 void mitk::IGTLMessageFactory::AddMessageType(std::string messageTypeName,
169  IGTLMessageFactory::PointerToMessageBaseNew messageTypeNewPointer,
171 {
172  this->AddMessageNewMethod(messageTypeName, messageTypeNewPointer);
173  this->AddMessageCloneHandler(messageTypeName, cloneHandler);
174 }
175 
176 void mitk::IGTLMessageFactory::AddMessageNewMethod(std::string messageTypeName,
177  IGTLMessageFactory::PointerToMessageBaseNew messageTypeNewPointer)
178 {
179  this->m_NewMethods[messageTypeName] = messageTypeNewPointer;
180 }
181 
184 {
185  this->m_CloneHandlers[msgTypeName] = cloneHandler;
186 }
187 
189 mitk::IGTLMessageFactory::GetCloneHandler(std::string messageTypeName)
190 {
191  if (this->m_CloneHandlers.find(messageTypeName) !=
192  this->m_CloneHandlers.end())
193  {
194  return m_CloneHandlers[messageTypeName];
195  }
196 
197  MITK_ERROR("IGTLMessageFactory") << messageTypeName <<
198  " message type is not registered to factory!";
199 
200  mitkThrow() << messageTypeName << " message type is not registered to factory!";
201 
202  return nullptr;
203 }
204 
207 {
208  return this->GetCloneHandler(msg->GetDeviceType())->Clone(msg);
209 }
210 
213 {
214  if (this->m_NewMethods.find(messageTypeName) != this->m_NewMethods.end())
215  {
216  return m_NewMethods[messageTypeName];
217  }
218 
219  MITK_ERROR("IGTLMessageFactory") << messageTypeName <<
220  " message type is not registered to factory!";
221  return nullptr;
222 }
223 
225 mitk::IGTLMessageFactory::CreateInstance(std::string messageTypeName)
226 {
228  this->GetMessageTypeNewPointer(messageTypeName);
229  if (newPointer != nullptr)
230  {
231  return newPointer();
232  }
233  else
234  {
235  return nullptr;
236  }
237 }
238 
239 std::list<std::string>
241 {
242  std::list<std::string> allGetMessages;
243  for (std::map<std::string, PointerToMessageBaseNew>::const_iterator it =
244  this->m_NewMethods.begin();
245  it != this->m_NewMethods.end(); ++it)
246  {
247  if (it->first.find("GET_") != std::string::npos ||
248  it->first.find("STT_") != std::string::npos ||
249  it->first.find("STP_") != std::string::npos ||
250  it->first.find("RTS_") != std::string::npos)
251  {
252  allGetMessages.push_back(it->first);
253  }
254  }
255 
256  return allGetMessages;
257 }
258 
261 {
262  std::string messageType;
263 
264  //check the header
265  if (msgHeader.IsNull())
266  {
267  messageType = "NONE";
268  }
269  else
270  {
271  messageType = msgHeader->GetDeviceType();
272  }
273 
274  //make message type uppercase
275  messageType = itksys::SystemTools::UpperCase(messageType);
276 
277  //find the according new method
278  if (this->m_NewMethods.find(messageType)
279  != this->m_NewMethods.end())
280  {
281  if (this->m_NewMethods[messageType] != nullptr)
282  {
283  // Call tracker New() function if tracker not NULL
284  return (*this->m_NewMethods[messageType])();
285  }
286  else
287  return nullptr;
288  }
289  else
290  {
291  MITK_ERROR("IGTLMessageFactory") << "Unknown IGT message type: "
292  << messageType;
293  return nullptr;
294  }
295 }
virtual IGTLMessageFactory::PointerToMessageBaseNew GetMessageTypeNewPointer(std::string messageTypeName)
Get pointer to message type new function, or NULL if the message type not registered Usage: igtl::Mes...
std::list< std::string > GetAvailableMessageRequestTypes()
Returns available get messages.
itk::SmartPointer< Self > Pointer
Pointer Clone() const
Pointer Clone() const
#define MITK_ERROR
Definition: mitkLogMacros.h:24
igtl::MessageBase::Pointer CreateInstance(std::string messageTypeName)
Creates a new message instance fitting to the given type.
void AddMessageType(std::string messageTypeName, IGTLMessageFactory::PointerToMessageBaseNew messageTypeNewPointer, mitk::IGTLMessageCloneHandler::Pointer cloneHandler)
Add message type name and pointer to IGTL message new function and the clone handler.
#define mitkIGTMessageCloneClassMacro(messagetype, classname)
virtual void AddMessageNewMethod(std::string messageTypeName, IGTLMessageFactory::PointerToMessageBaseNew messageTypeNewPointer)
Add message type name and pointer to IGTL message new function Usage: AddMessageType("IMAGE", (PointerToMessageBaseNew)&igtl::ImageMessage::New);.
virtual void AddMessageCloneHandler(std::string msgTypeName, mitk::IGTLMessageCloneHandler::Pointer msgCloneHandler)
Adds a clone function for the specified message type.
igtl::MessageBase::Pointer(* PointerToMessageBaseNew)()
Function pointer for storing New() static methods of igtl::MessageBase classes.
#define mitkThrow()
virtual mitk::IGTLMessageCloneHandler::Pointer GetCloneHandler(std::string messageTypeName)
Get pointer to message type clone function, or NULL if the message type is not registered Usage: igtl...
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.