20 #include "PlusConfigure.h" 27 #include "igsioCommon.h" 29 #include "igtlImageMessage.h" 30 #include "igtlMessageFactory.h" 31 #include "igtlMessageHeader.h" 32 #include "igtlMultiThreader.h" 33 #include "igtlOSUtil.h" 34 #include "igtlServerSocket.h" 35 #include "igtlTrackingDataMessage.h" 41 igtl::MutexLock::Pointer glock;
42 igtl::Socket::Pointer socket;
58 orientation[1]=0.6666666666*cos(theta);
59 orientation[2]=0.577350269189626;
60 orientation[3]=0.6666666666*sin(theta);
63 igtl::QuaternionToMatrix(orientation, matrix);
73 void SendTrackingData(igtl::Socket::Pointer& socket, igtl::TrackingDataMessage::Pointer& trackingMsg)
75 static float phi0 = 0.0;
76 static float theta0 = 0.0;
79 for (
int trackingDataElementIndex=0; trackingDataElementIndex<trackingMsg->GetNumberOfTrackingDataElements(); trackingDataElementIndex++)
81 igtl::TrackingDataElement::Pointer ptr=NULL;
82 trackingMsg->GetTrackingDataElement(trackingDataElementIndex, ptr);
83 igtl::Matrix4x4 matrix;
85 ptr->SetMatrix(matrix);
88 static igtlUint32 messageID = 0;
89 trackingMsg->SetMessageID(messageID++);
91 socket->Send(trackingMsg->GetBufferPointer(), trackingMsg->GetBufferSize());
101 igtl::MultiThreader::ThreadInfo* info = static_cast<igtl::MultiThreader::ThreadInfo*>(ptr);
102 int id = info->ThreadID;
103 std::cerr <<
"Thread #" <<
id <<
": start" << std::endl;
106 ThreadData* td = static_cast<ThreadData*>(info->UserData);
107 igtl::MutexLock::Pointer glock = td->glock;
108 long interval = td->interval;
109 std::cerr <<
"Interval = " << interval <<
" (ms)" << std::endl;
111 igtl::Socket::Pointer& socket = td->socket;
115 igtl::TrackingDataMessage::Pointer trackingMsg;
116 trackingMsg = igtl::TrackingDataMessage::New();
117 trackingMsg->SetDeviceName(
"Tracker");
118 igtl::TrackingDataElement::Pointer trackElement0;
119 trackElement0 = igtl::TrackingDataElement::New();
120 trackElement0->SetName(
"ReferenceStar");
121 trackElement0->SetType(igtl::TrackingDataElement::TYPE_TRACKER);
122 igtl::TrackingDataElement::Pointer trackElement1;
123 trackElement1 = igtl::TrackingDataElement::New();
124 trackElement1->SetName(
"Ultrasound");
125 trackElement1->SetType(igtl::TrackingDataElement::TYPE_6D);
126 igtl::TrackingDataElement::Pointer trackElement2;
127 trackElement2 = igtl::TrackingDataElement::New();
128 trackElement2->SetName(
"Pointer");
129 trackElement2->SetType(igtl::TrackingDataElement::TYPE_5D);
133 std::cout <<
"Sending tool data: ReferenceStar, Ultrasound..." << std::endl;
135 trackingMsg->AddTrackingDataElement(trackElement0);
136 trackingMsg->AddTrackingDataElement(trackElement1);
137 for (
int i=0;
i<500;
i++)
142 igtl::Sleep(interval);
147 std::cout <<
"Sending tool data: ReferenceStar, Ultrasound, Pointer..." << std::endl;
149 trackingMsg->AddTrackingDataElement(trackElement2);
155 igtl::Sleep(interval);
159 std::cerr <<
"Thread #" <<
id <<
": end" << std::endl;
166 int main(
int argc,
char* argv[])
175 if (argc > 2 ||
port==0)
178 std::cerr <<
"Usage: " << argv[0] <<
" <port>" << std::endl;
179 std::cerr <<
" <port> : Server port number (default: 22222)" << std::endl;
184 igtl::MessageFactory::Pointer igtlMessageFactory = igtl::MessageFactory::New();
186 std::cout <<
"Started BrainLab tracker simulator server at port " <<
port << std::endl;
188 igtl::ServerSocket::Pointer serverSocket;
189 serverSocket = igtl::ServerSocket::New();
190 int r = serverSocket->CreateServer(
port);
193 std::cerr <<
"Cannot create a server socket." << std::endl;
197 igtl::MultiThreader::Pointer threader = igtl::MultiThreader::New();
198 igtl::MutexLock::Pointer glock = igtl::MutexLock::New();
201 std::cout <<
"Waiting for connections..." << std::endl;
207 igtl::Socket::Pointer socket;
208 socket = serverSocket->WaitForConnection(1000);
210 if (socket.IsNotNull())
212 std::cerr <<
"A client is connected." << std::endl;
215 igtl::MessageHeader::Pointer headerMsg;
216 headerMsg = igtl::MessageHeader::New();
220 headerMsg->InitPack();
224 int rs = socket->Receive(headerMsg->GetBufferPointer(), headerMsg->GetBufferSize(), timeout);
230 threader->TerminateThread(threadID);
233 std::cerr <<
"Disconnecting the client." << std::endl;
235 socket->CloseSocket();
238 if (rs != headerMsg->GetBufferSize())
246 igtl::MessageBase::Pointer bodyMsg = igtlMessageFactory->CreateReceiveMessage(headerMsg);
247 if( bodyMsg.IsNull() )
249 LOG_ERROR(
"Unable to receive message of type: " << headerMsg->GetMessageType());
254 if (dynamic_cast<igtl::StartTrackingDataMessage*>(bodyMsg.GetPointer()))
256 std::cerr <<
"Received a STT_TDATA message." << std::endl;
258 igtl::StartTrackingDataMessage::Pointer startTracking = dynamic_cast<igtl::StartTrackingDataMessage*>(bodyMsg.GetPointer());
261 socket->Receive(startTracking->GetBufferBodyPointer(), startTracking->GetBufferBodySize(), timeout);
262 const int enableCrcCheck=1;
263 int unpackSuccess = startTracking->Unpack(enableCrcCheck);
264 if (unpackSuccess & igtl::MessageHeader::UNPACK_BODY)
266 td.interval = startTracking->GetResolution();
270 threadID = threader->SpawnThread((igtl::ThreadFunctionType) &
ThreadFunction, &td);
273 else if (dynamic_cast<igtl::StopTrackingDataMessage*>(bodyMsg.GetPointer()))
275 socket->Skip(headerMsg->GetBodySizeToRead(), 0);
276 std::cerr <<
"Received a STP_TDATA message." << std::endl;
280 threader->TerminateThread(threadID);
282 std::cerr <<
"Disconnecting the client." << std::endl;
284 socket->CloseSocket();
290 std::cerr <<
"Receiving : " << headerMsg->GetMessageType() << std::endl;
291 socket->Skip(headerMsg->GetBodySizeToRead(), 0);
298 serverSocket->CloseSocket();
void SendTrackingData(igtl::Socket::Pointer &socket, igtl::TrackingDataMessage::Pointer &trackingMsg)
int main(int argc, char *argv[])
void GetRandomTestMatrix(igtl::Matrix4x4 &matrix, float phi, float theta)
void * ThreadFunction(void *ptr)