OpenIGTLink/Library/Tutorial

From NAMIC Wiki
Revision as of 22:12, 25 June 2008 by Tokuda (talk | contribs)
Jump to: navigation, search
Home < OpenIGTLink < Library < Tutorial

<< OpenIGTLink

Code snippet using igtlutil (C/C++)

The igtlutil c functions are available from Source/igtlutil in the Open IGT Link Library.

The followings are example codes to create Open IGT Link message.

Include declarations for transformation data:

 #include "igtl_util.h"
 #include "igtl_header.h"
 #include "igtl_transform.h"

Transform package creation:

 /********** pack data body **********/
 igtl_float32 transform[12];
 
 transform[0] = tx;
 transform[1] = ty;
 transform[2] = tz;
 transform[3] = sx;
 transform[4] = sy;
 transform[5] = sz;
 transform[6] = nx;
 transform[7] = ny;
 transform[8] = nz;
 transform[9] = px;
 transform[10] = py;
 transform[11] = pz;
 
 igtl_transform_convert_byte_order(transform);  /* convert endian if necessary */
 
 /********** general header **********/  
 igtl_header header;
 igtl_uint64 crc = crc64(0, 0, 0LL);          /* initial crc */
 header.version   = IGTL_HEADER_VERSION;
 header.timestamp = 0;
 header.body_size = IGTL_TRANSFORM_SIZE;
 header.crc       = crc64((unsigned char*)transform, IGTL_TRANSFORM_SIZE, crc);
 
 strncpy(header.name, "TRANSFORM", 12);       /* Device Type: should be "TRANSFORM" */
 strncpy(header.device_name, "Tracker", 20);    /* Device name */
 
 igtl_header_convert_byte_order(header);   /* convert endian if necessary */

Then send package (in case of BSD socket)

 send(sock, (void*) &header, IGTL_HEADER_SIZE, 0);
 send(sock, (void*) transform, IGTL_TRANSFORM_SIZE, 0);


Example using the Open IGT Link Library (C++)

Get the example code

The example code is available in the Examples/ directory of Open IGT Link library. The instruction is available in the Open IGT Link Library page.

Writing Tracker Client program

The code "Tracker/TrackerClient.cxx" demonstrates how to create open igt link message, establish connection with the Open IGT Link server, then repeat sending coordinate data to the server, using Open IGT Link Library.

First, establish connection with server (ip: 192.168.0.1, port number: 18944) using igtl::ClientSocket class:

 igtl::ClientSocket::Pointer socket;
 socket = igtl::ClientSocket::New();
 int r = socket->ConnectToServer("192.168.0.1", 18944);
 
 if (r != 0)
   {
   //
   // do error handling
   //
   }

Prepare a class instance for the Open IGT Link message:

 igtl::TransformMessage::Pointer transMsg;
 transMsg = igtl::TransformMessage::New();

Set Open IGT Link device name:

 transMsg->SetDeviceName("Tracker");

Substitute matrix into the Matrix4x4 class:

 igtl::Matrix4x4 matrix;
 GetRandomTestMatrix(matrix);

Set the matrix to the message class:

 transMsg->SetMatrix(matrix);

Pack the data. This member function generates byte stream formatted in Open IGT Link format.

 transMsg->Pack();

Send data through TCP/IP socket connection:

 socket->Send(transMsg->GetPackPointer(), transMsg->GetPackSize());