File: TestSurfaceWriter2.cxx

package info (click to toggle)
gdcm 3.0.24-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 27,560 kB
  • sloc: cpp: 203,722; ansic: 76,471; xml: 48,131; python: 3,473; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 97
file content (242 lines) | stat: -rw-r--r-- 7,167 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html 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 notice for more information.

=========================================================================*/
#include "gdcmTesting.h"
#include "gdcmSurfaceWriter.h"
#include "gdcmSurfaceReader.h"
#include "gdcmFileMetaInformation.h"
#include "gdcmFilename.h"
#include "gdcmSystem.h"
#include "gdcmAttribute.h"

#include <ctime>

namespace gdcm
{

/**
  * Test surface reading and writing method.
  */
int TestSurfaceWriter2(const char *subdir, const char* filename)
{
  SurfaceReader reader;
  reader.SetFileName( filename );
  if ( !reader.Read() )
    {
    int ret = 1;
    gdcm::MediaStorage ms;
    if( ms.SetFromFile( reader.GetFile() ) )
      {
      if( ms == gdcm::MediaStorage::SurfaceSegmentationStorage )
        {
        ret = 0;
        }
      }
    if( !ret )
      {
      std::cerr << "Failed to read: " << filename << std::endl;
      std::cerr << "MediaStorage is: " << ms.GetString() << std::endl;
      }
    return !ret;
    }
  //  std::cout << "success to read" << std::endl;

  if (reader.GetNumberOfSurfaces() < 2)
    {
    std::cerr << "File with not enough surfaces (min : 2): " << filename << std::endl;
    return 1;
    }

  // Modify data to test other writing/reading way
  SmartPointer< Segment >         segment = new Segment;
  srand( (unsigned int)time(nullptr));
  SegmentHelper::BasicCodedEntry  processingAlgo("123", "TEST", "Test123");
    {
    SegmentReader::SegmentVector  segments = reader.GetSegments();
    Segment::SurfaceVector        tmp;
    SegmentReader::SegmentVector::iterator itSegments    = segments.begin();
    SegmentReader::SegmentVector::iterator itEndSegments = segments.end();
    for (; itSegments != itEndSegments; itSegments++)
      {
      tmp = (*itSegments)->GetSurfaces();
      Segment::SurfaceVector::iterator itSurfaces    = tmp.begin();
      Segment::SurfaceVector::iterator itEndSurfaces = tmp.end();
      for (; itSurfaces != itEndSurfaces; itSurfaces++)
        {
        SmartPointer< Surface > surf = *itSurfaces;
        surf->SetSurfaceProcessing( true );
        surf->SetSurfaceProcessingRatio( 0.42f );
        surf->SetSurfaceProcessingDescription( "Test processing" );
        surf->SetProcessingAlgorithm( processingAlgo );

        if (surf->GetNumberOfVectors() > 0)
          surf->SetNumberOfVectors( 42 );

        surf->SetSurfaceNumber( rand()%1000 );

        segment->AddSurface(surf);
        }
      }

    segment->SetSegmentDescription("Surface test");
    //  segment.SetSurfaceCount( segment.GetSurfaces().size() );
    }

  // Create directory first:
  std::string tmpdir = Testing::GetTempDirectory( subdir );
  if( !System::FileIsDirectory( tmpdir.c_str() ) )
    {
    System::MakeDirectory( tmpdir.c_str() );
    //return 1;
    }
  std::string outfilename = Testing::GetTempFilename( filename, subdir );

  // Write file from content reader
    {
    SurfaceWriter writer;
    writer.SetFileName( outfilename.c_str() );
    writer.AddSegment( segment );
    //  writer.SetNumberOfSurfaces( segment.GetSurfaces().size() );

    // Set same header to write file
    const FileMetaInformation & header = reader.GetFile().GetHeader();
    writer.GetFile().SetHeader( header );

    if( !writer.Write() )
      {
      std::cerr << "Failed to write: " << outfilename << std::endl;
      return 1;
      }
    //  std::cout << "success to write" << std::endl;
    }

  SurfaceReader reader2;
  reader2.SetFileName( outfilename.c_str() );
  if ( !reader2.Read() )
    {
    std::cerr << "Failed to read again: " << outfilename << std::endl;
    return 1;
    }
  //  std::cout << "success to read again" << std::endl;

  SegmentReader::SegmentVector  segments2 = reader2.GetSegments();
  if ( strcmp(segment->GetSegmentDescription(), segments2[0]->GetSegmentDescription()) != 0)
    {
    std::cerr << "Find different segment description"<< std::endl;
    return 1;
    }

  Segment::SurfaceVector        surfaces2 = segments2[0]->GetSurfaces();
  if ( segment->GetSurfaces().size() != surfaces2.size() )
    {
    std::cerr << "Find different surface count"<< std::endl;
    return 1;
    }

  Segment::SurfaceVector::iterator itSurfaces2    = surfaces2.begin();
  Segment::SurfaceVector::iterator itEndSurfaces2 = surfaces2.end();
  SegmentHelper::BasicCodedEntry processingAlgo2;

  Segment::SurfaceVector           surfaces      = segment->GetSurfaces();
  Segment::SurfaceVector::iterator itSurfaces    = surfaces.begin();
  Segment::SurfaceVector::iterator itEndSurfaces = surfaces.end();
  for (; itSurfaces2 != itEndSurfaces2; itSurfaces2++)
    {
    SmartPointer< Surface > surf = *itSurfaces2;

    if (!surf->GetSurfaceProcessing())
      {
      std::cerr << "Find different surface processing"<< std::endl;
      return 1;
      }

    if ( 0.41f > surf->GetSurfaceProcessingRatio() || surf->GetSurfaceProcessingRatio() > 0.43f)
      {
      std::cerr << "Find different surface processing ratio"<< std::endl;
      return 1;
      }

    String<> str;
    str = surf->GetSurfaceProcessingDescription();
    if (str.Trim() != "Test processing")
      {
      std::cerr << "Find different surface processing description"<< std::endl;
      return 1;
      }

    processingAlgo2 = surf->GetProcessingAlgorithm();
    str = processingAlgo2.CV;
    processingAlgo2.CV = str.Trim();
    str = processingAlgo2.CSD;
    processingAlgo2.CSD = str.Trim();
    str = processingAlgo2.CM;
    processingAlgo2.CM = str.Trim();

    if (processingAlgo2.CV != processingAlgo.CV
      || processingAlgo2.CSD != processingAlgo.CSD
      || processingAlgo2.CM != processingAlgo.CM )
      {
      std::cerr << "Find different surface processing algorithm"<< std::endl;
      return 1;
      }

    if (itSurfaces != itEndSurfaces)
      {
      if (surf->GetSurfaceNumber() != (*itSurfaces)->GetSurfaceNumber())
        {
        std::cerr << "Find different surface number"<< std::endl;
        return 1;
        }

      if (surf->GetNumberOfVectors() > 0)
        {
        std::cerr << "Find different number of vectors"<< std::endl;
        return 1;
        }

      itSurfaces++;
      }
    else
      {
      std::cerr << "Find different surface organization"<< std::endl;
      return 1;
      }
    }

  return 0;
}

}

int TestSurfaceWriter2(int argc, char *argv[])
{
  if( argc == 2 )
    {
    const char *filename = argv[1];
    return gdcm::TestSurfaceWriter2(argv[0],filename);
    }

  // else
  gdcm::Trace::DebugOff();
  gdcm::Trace::WarningOff();
  int r = 0, i = 0;
  const char *filename;
  const char * const *filenames = gdcm::Testing::GetFileNames();
  while( (filename = filenames[i]) )
    {
    r += gdcm::TestSurfaceWriter2(argv[0], filename );
    ++i;
    }

  return r;
}