File: igtlPointMessageTest.cxx

package info (click to toggle)
openigtlink 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 20,076; ansic: 6,704; sh: 227; perl: 74; makefile: 46
file content (134 lines) | stat: -rw-r--r-- 5,230 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*=========================================================================
 
 Program:   OpenIGTLink Library
 Language:  C++
 
 Copyright (c) Insight Software Consortium. All rights reserved.
 
 This software is distributed WITHOUT ANY WARRANTY; without even
 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 PURPOSE.  See the above copyright notices for more information.
 
 =========================================================================*/

#include "igtlPointMessage.h"
#include "igtlutil/igtl_test_data_point.h"
#include "igtl_point.h"
#include "igtl_header.h"
#include "igtlTestConfig.h"
#include "string.h"

igtl::PointMessage::Pointer pointSendMsg = igtl::PointMessage::New();
igtl::PointMessage::Pointer pointReceiveMsg = igtl::PointMessage::New();

igtl::PointElement::Pointer pointElement0 = igtl::PointElement::New();
igtl::PointElement::Pointer pointElement1 = igtl::PointElement::New();
igtl::PointElement::Pointer pointElement2 = igtl::PointElement::New();

void BuildUpElements()
{
  pointElement0->SetName("POINT_DESCRIPTION_0");
  pointElement0->SetGroupName("Landmark");
  pointElement0->SetRGBA(255,0,0,255);
  pointElement0->SetPosition(10.0, 15.0, 20.0);
  pointElement0->SetRadius(5.0);
  pointElement0->SetOwner("IMAGE_0");
  pointElement1->SetName("POINT_DESCRIPTION_1");
  pointElement1->SetGroupName("Landmark");
  pointElement1->SetRGBA(0,255,0,255);
  pointElement1->SetPosition(25.0, 30.0, 35.0);
  pointElement1->SetRadius(3.0);
  pointElement1->SetOwner("IMAGE_0");
  pointElement2->SetName("POINT_DESCRIPTION_2");
  pointElement2->SetGroupName("Landmark");
  pointElement2->SetRGBA(0,0,255,255);
  pointElement2->SetPosition(40.0, 45.0, 50.0);
  pointElement2->SetRadius(1.0);
  pointElement2->SetOwner("IMAGE_0");
  pointSendMsg = igtl::PointMessage::New();
  pointSendMsg->SetDeviceName("DeviceName");
  pointSendMsg->SetHeaderVersion(IGTL_HEADER_VERSION_1);
  pointSendMsg->SetTimeStamp(0, 1234567892);
  pointSendMsg->AddPointElement(pointElement0);
  pointSendMsg->AddPointElement(pointElement1);
  pointSendMsg->AddPointElement(pointElement2);
  pointSendMsg->Pack();
}

TEST(PointMessageTest, Pack)
{
  BuildUpElements();
  int r = memcmp((const void*)pointSendMsg->GetPackPointer(), (const void*)test_point_message,
                 (size_t)(IGTL_HEADER_SIZE));
  EXPECT_EQ(r, 0);
  r = memcmp((const void*)pointSendMsg->GetPackBodyPointer(), (const void*)(test_point_message+(size_t)(IGTL_HEADER_SIZE)), IGTL_POINT_ELEMENT_SIZE*3 );
  EXPECT_EQ(r, 0);
}


TEST(PointMessageTest, Unpack)
{
  BuildUpElements();
  igtl::MessageHeader::Pointer headerMsg = igtl::MessageHeader::New();
  headerMsg->AllocatePack();
  memcpy(headerMsg->GetPackPointer(), pointSendMsg->GetPackPointer(), IGTL_HEADER_SIZE);
  headerMsg->Unpack();
  pointReceiveMsg = igtl::PointMessage::New();
  pointReceiveMsg->SetMessageHeader(headerMsg);
  pointReceiveMsg->AllocatePack();
  memcpy(pointReceiveMsg->GetPackBodyPointer(), pointSendMsg->GetPackBodyPointer(), IGTL_POINT_ELEMENT_SIZE*3);
  pointReceiveMsg->Unpack();
  
  igtl_header *messageHeader = (igtl_header *)pointReceiveMsg->GetPackPointer();
  EXPECT_STREQ(messageHeader->device_name, "DeviceName");
  EXPECT_STREQ(messageHeader->name, "POINT");
  EXPECT_EQ(messageHeader->header_version, 1);
  EXPECT_EQ(messageHeader->timestamp, 1234567892);
  EXPECT_EQ(messageHeader->body_size, IGTL_POINT_ELEMENT_SIZE*3);
  
  std::vector<std::vector<igtlUint8> > groundTruthRGBA(3,std::vector<igtlUint8>(4));
  igtlUint8 tempIni1[4] = {255,0,0,255};
  groundTruthRGBA[0].assign(tempIni1,tempIni1+4);
  igtlUint8 tempIni2[4] = {0,255,0,255};
  groundTruthRGBA[1].assign(tempIni2,tempIni2+4);
  igtlUint8 tempIni3[4] = {0,0,255,255};
  groundTruthRGBA[2].assign(tempIni3,tempIni3+4);
  
  std::vector<std::vector<igtlFloat32> > groundTruthPos(3,std::vector<igtlFloat32>(3));
  igtlFloat32 tempFloat1[3] = {10.0,15.0,20.0};
  groundTruthPos[0].assign(tempFloat1,tempFloat1+3);
  igtlFloat32 tempFloat2[3] = {25.0,30.0,35.0};
  groundTruthPos[1].assign(tempFloat2,tempFloat2+3);
  igtlFloat32 tempFloat3[3] = {40.0,45.0,50.0};
  groundTruthPos[2].assign(tempFloat3,tempFloat3+3);
  
  igtlFloat32 groundTruthRadius[3] = {5.0,3.0,1.0};
  
  std::vector<char*> pointDescription;
  pointDescription.push_back((char*)"POINT_DESCRIPTION_0");
  pointDescription.push_back((char*)"POINT_DESCRIPTION_1");
  pointDescription.push_back((char*)"POINT_DESCRIPTION_2");
  
  for (int i = 0; i<3;++i)
    {
    igtl::PointElement::Pointer elem = igtl::PointElement::New();
    pointReceiveMsg->GetPointElement(i, elem);
    EXPECT_EQ(strncmp((char*)(elem->GetName()), pointDescription[i], 19),0);
    igtlUint8 returnedRGBA[4] ={0,0,0,0};
    elem->GetRGBA(returnedRGBA);
    EXPECT_THAT(returnedRGBA, testing::ElementsAreArray(groundTruthRGBA[i]));
    igtlFloat32 returnedPosition[3] ={0,0,0};
    elem->GetPosition(returnedPosition);
    EXPECT_THAT(returnedPosition, testing::ElementsAreArray(groundTruthPos[i]));
    EXPECT_EQ(elem->GetRadius(), groundTruthRadius[i]);
    EXPECT_EQ(strncmp((char*)elem->GetOwner(), "IMAGE_0", 7),0);
    }
}


int main(int argc, char **argv)
{
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}