File: itkPolygonGroupSpatialObjectXMLFileTest.cxx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (215 lines) | stat: -rw-r--r-- 5,948 bytes parent folder | download | duplicates (2)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*=========================================================================

Program:   Insight Segmentation & Registration Toolkit
Module:    itkPolygonGroupSpatialObjectXMLFileTest.cxx
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

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.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include "itkPolygonGroupSpatialObject.h"
#include "itkPolygonSpatialObject.h"
#include <iostream>
#include "itkPolygonGroupSpatialObjectXMLFile.h"
#include <itksys/SystemTools.hxx>

static float strandPoints[11][2] = 
  {
    {1,1},{1,2},{1.25,2},{1.25,1.25},{1.75,1.25},
    {1.75,1.5},{1.5,1.5},{1.5,2},{2,2},{2,1},{1,1}
  };
typedef itk::PolygonSpatialObject<3> Polygon3DType;
typedef itk::PolygonGroupSpatialObject<3> PolygonGroup3DType;
typedef PolygonGroup3DType::Pointer PolygonGroup3DPointer;

int
buildPolygonGroup(PolygonGroup3DPointer &PolygonGroup)
{
  try
    {
    for(float z = 0.0; z <= 10.0; z += 1.0)
      {      
      itk::PolygonSpatialObject<3>::Pointer strand
        = itk::PolygonSpatialObject<3>::New();

      if(!PolygonGroup->AddStrand(strand))
        {
        std::cerr << "Error adding point" << std::endl;
        return EXIT_FAILURE;
        }
      strand->SetThickness(1.0);
      //
      // add all points to this strand.
      for(int i = 0; i < 11; i++)
        {
        double pos[3];
        pos[0] = strandPoints[i][0];
        pos[1] = strandPoints[i][1];
        pos[2] = z;
        itk::PolygonSpatialObject<3>::PointType curpoint(pos);
        if(!strand->AddPoint(curpoint)) 
          {
          std::cerr << "Error adding point" << std::endl;
          return EXIT_FAILURE;
          }
        }
      }
    }
  catch(itk::ExceptionObject &)
    {
    std::cerr << "Error creating PolygonGroup" << std::endl;
    return EXIT_FAILURE;
    }
  return EXIT_SUCCESS;
}

int testPolygonGroupEquivalence(PolygonGroup3DPointer &p1,
                                PolygonGroup3DPointer &p2)
{
  //
  // Write out polygondata
  PolygonGroup3DType::ChildrenListType *children1 =
    p1->GetChildren(0,NULL);
  PolygonGroup3DType::ChildrenListType::iterator it1 =
    children1->begin();
  PolygonGroup3DType::ChildrenListType::iterator end1 =
    children1->end();

  PolygonGroup3DType::ChildrenListType *children2 =
    p2->GetChildren(0,NULL);
  PolygonGroup3DType::ChildrenListType::iterator it2 =
    children2->begin();
  PolygonGroup3DType::ChildrenListType::iterator end2 =
    children2->end();

  while(it1 != end1)
    {
    if(it2 == end2) // premature end of list
      {
      delete children1;
      delete children2;
      return EXIT_FAILURE;
      }
    Polygon3DType *curstrand1 =
      dynamic_cast<Polygon3DType *>((*it1).GetPointer());
    Polygon3DType *curstrand2 =
      dynamic_cast<Polygon3DType *>((*it2).GetPointer());

    Polygon3DType::PointListType &points1 =
      curstrand1->GetPoints();
    Polygon3DType::PointListType &points2 =
      curstrand2->GetPoints();

    Polygon3DType::PointListType::iterator pointIt1 
      = points1.begin();
    Polygon3DType::PointListType::iterator pointItEnd1
      = points1.end();

    Polygon3DType::PointListType::iterator pointIt2 
      = points2.begin();
    Polygon3DType::PointListType::iterator pointItEnd2
      = points2.end();

    while(pointIt1 != pointItEnd1) 
      {
      if(pointIt2 == pointItEnd2)
        {
        delete children1;
        delete children2;
        return EXIT_FAILURE;
        }
      Polygon3DType::PointType curpoint1 = 
        (*pointIt1).GetPosition();
      Polygon3DType::PointType curpoint2 = 
        (*pointIt2).GetPosition();
      pointIt1++;
      pointIt2++;
      }
    if(pointIt2 != pointItEnd2)
      {
      delete children1;
      delete children2;
      return EXIT_FAILURE;
      }
    it1++;
    it2++;
    }
  
  if(it2 != end2)
    {
    delete children1;
    delete children2;
    return EXIT_FAILURE;
    }
  
  delete children1;
  delete children2;
  return EXIT_SUCCESS;
}
int itkPolygonGroupSpatialObjectXMLFileTest(int ac, char *av[])
{
  if(ac < 2)
    {
    std::cerr << "Usage: " << av[0] << " XMLfile\n";
    return EXIT_FAILURE;
    }

  PolygonGroup3DPointer PolygonGroup = PolygonGroup3DType::New();

  PolygonGroup3DPointer PGroupFromFile;
  if(buildPolygonGroup(PolygonGroup) != 0 || PolygonGroup.IsNull())
    {
    std::cerr << "Error building polygon group" << std::endl;
    return EXIT_FAILURE;
    }

  std::string xmlfilename(av[1]);
  xmlfilename = xmlfilename + "/PolygonGroupSpatialObjectXMLFileTest.xml";
  try
    {
    itk::PolygonGroupSpatialObjectXMLFileWriter::Pointer pw =
      itk::PolygonGroupSpatialObjectXMLFileWriter::New();
  
    pw->SetFilename(xmlfilename.c_str());
    pw->SetObject(&(*PolygonGroup));
    pw->WriteFile();
    }
  catch(itk::ExceptionObject &)
    {
    std::cerr << "Error Creating file" << std::endl;
    return EXIT_FAILURE;
    }

  try
    {
    itk::PolygonGroupSpatialObjectXMLFileReader::Pointer p = 
      itk::PolygonGroupSpatialObjectXMLFileReader::New();
    p->SetFilename(xmlfilename.c_str());
    p->GenerateOutputInformation();
    PGroupFromFile = p->GetOutputObject();
    if(PGroupFromFile.IsNull())
      {
      std::cerr << "Error retrieving object pointer" << std::endl;
      return EXIT_FAILURE;
      }
    }
  catch(itk::ExceptionObject &)
    {
    std::cerr << "Error Reading file" << std::endl;
    return EXIT_FAILURE;
    }
  itksys::SystemTools::RemoveFile(xmlfilename.c_str());

  return testPolygonGroupEquivalence(PolygonGroup,PGroupFromFile);
}