OpenIGTLink/Tutorial/igtlutil

From NAMIC Wiki
Revision as of 21:10, 15 July 2008 by Tokuda (talk | contribs)
Jump to: navigation, search
Home < OpenIGTLink < Tutorial < igtlutil

<< OpenIGTLink Tutorial Home


Serializing Open IGT Link message for linear transform data

To generate a transform message using the igtlutil library, first we include following header files:

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

Then, prepare a transform:

 /********** 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 */

Once the transform data has been prepared, we create a general header:

 /********** 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 */

Now it's time to send the serialized data. Since igtlutil library doesn't have a socket implementation, we need to some socket libraries or API, available in the environment we are working on. In case of BSD socket, call:

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

after establishing the connection.

Deserializing Open IGT Link message for linear transform data


First of all, we receive the header data from the sender. It may look like the following code, in case of using BSD socket API:

 igtl_header header;
 int n;
 
 n = recv(sock, header, IGTL_HEADER_SIZE);
 if (n != IGTL_HEADER_SIZE)
   {
     /*** do something for error handling ***/
   }  

Then convert byte order of each element:

 igtl_header_convert_byte_order(&header);

Now the header is ready to be read. Header fields e.g. header version, size, CRC etc. can be access as members of igtl_header structure.

The following code shows how to detect which type of message is in the body part.

 char deviceType[13];
 deviceType[12] = 0;
 memcpy((void*)deviceType, header.name, 12);
 
 if (strcmp(deviceType, "TRANSFORM") == 0)
   {
     /*** code to read TRANSFORM data ***/
   }
 else if (strcmp(deviceType, "IMAGE") == 0)
   {
     /*** code to read IMAGE data ***/
   }
 
   /*** .... other types ...  ***/
 
 else
   {
     /*** Unknown type: read the rest of the message and discard it. ***/
   }