File: itkQuadEdgeMeshCellInterfaceTest.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (460 lines) | stat: -rw-r--r-- 14,027 bytes parent folder | download | duplicates (6)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include <iostream>

#include "itkQuadEdgeMesh.h"

#include "itkHexahedronCell.h"
#include "itkTetrahedronCell.h"
#include "itkQuadraticTriangleCell.h"
#include "itkPolygonCell.h"


/**
 * Define a mesh type that stores a PixelType of "int".  Use the defaults
 * for the other template parameters.
 */
typedef int                                             PixelType;
typedef itk::QuadEdgeMesh<PixelType, 3>                 MeshType;
typedef MeshType::CellTraits                            CellTraits;
typedef CellTraits::QuadEdgeType                        QEType;
typedef itk::CellInterface< int, CellTraits >           CellInterfaceType;
typedef itk::QuadEdgeMeshLineCell<CellInterfaceType>    QELineCellType;
typedef itk::QuadEdgeMeshPolygonCell<CellInterfaceType> QEPolygonCellType;

/**
 * Typedef the generic cell type for the mesh.  It is an abstract class,
 * so we can only use information from it, like get its pointer type.
 */
typedef MeshType::CellType              CellType;
typedef CellType::CellAutoPointer       CellAutoPointer;

class CustomQELineVisitor
{
public:
  void Visit(unsigned long cellId, QELineCellType * t )
    {
    (void)cellId;
    (void)t;
    }
  virtual ~CustomQELineVisitor() {}
};

class CustomQEPolyVisitor
{
public:
  void Visit(unsigned long cellId, QEPolygonCellType * t )
    {
    (void)cellId;
    (void)t;
    }
  virtual ~CustomQEPolyVisitor() {}
};

// Test the cell interface
template<typename TCell> int TestCellInterface(std::string name, TCell *aCell)
{

  CellAutoPointer cell(aCell,true);
  const TCell * cell2 = aCell;

  std::cout << "-------- " << name << "("
            << aCell->GetNameOfClass() << ")" << std::endl;
  std::cout << "    Type: " << cell->GetType() << std::endl;
  std::cout << "    Dimension: " << cell->GetDimension() << std::endl;
  std::cout << "    NumberOfPoints: " << cell->GetNumberOfPoints() << std::endl;
  std::cout << "    NumberOfBoundaryFeatures:" << std::endl;
  // Note the <= is here to test the default case
  for (unsigned int i = 0; i <= cell->GetDimension(); i++)
    {
    std::cout << "      " << i << ": " << cell->GetNumberOfBoundaryFeatures(i)
              << std::endl;
    for (unsigned int j = 0; j < cell->GetNumberOfBoundaryFeatures(i); j++)
      {
      CellAutoPointer feature;
      cell->GetBoundaryFeature(i, j, feature);
      }
    }

  std::cout << "    Iterator test: PointIds for empty cell: ";
  typename TCell::PointIdIterator pointId = cell->PointIdsBegin();
  typename TCell::PointIdIterator endId = cell->PointIdsEnd();
  while (pointId != endId)
    {
    std::cout << *pointId << ", ";
    pointId++;
    }
  std::cout << std::endl;

  std::cout << "    ConstIterator test: PointIds for empty cell: ";
  typename TCell::PointIdConstIterator cpointId = cell2->PointIdsBegin();
  typename TCell::PointIdConstIterator cendId = cell2->PointIdsEnd();
  while (cpointId != cendId)
    {
    std::cout << *cpointId << ", ";
    cpointId++;
    }
  std::cout << std::endl;

  // Add point ids
  std::cout << "    SetPointIds" << std::endl;

  typedef MeshType::PointIdentifier  PointIdentifier;

  PointIdentifier *pointIds = new PointIdentifier[cell->GetNumberOfPoints() * 2];
  for (unsigned int i = 0; i < cell->GetNumberOfPoints() * 2; i++)
    {
    pointIds[i] = i;
    }

  cell->SetPointIds(pointIds);
  // exercing the const GetPointIds() method
  // null for QE Cells
  if(cell2->GetPointIds())
    {
    cell->SetPointIds(cell2->GetPointIds());
    }
  if (cell->GetNumberOfPoints() > 0)
    {
    cell->SetPointId(0, 100);
    }

  std::cout << "    ConstIterator test: PointIds for populated cell: ";
  typename TCell::PointIdConstIterator ppointId = cell2->PointIdsBegin();
  typename TCell::PointIdConstIterator pendId = cell2->PointIdsEnd();
  while (ppointId != pendId)
    {
    std::cout << *ppointId << ", ";
    ppointId++;
    }
  std::cout << std::endl;

  cell->SetPointIds(&pointIds[cell->GetNumberOfPoints()],
                    &pointIds[cell->GetNumberOfPoints() * 2]);
  std::cout << "    Iterator test: PointIds for populated cell: ";
  typename TCell::PointIdIterator pxpointId = cell->PointIdsBegin();
  typename TCell::PointIdIterator pxendId   = cell->PointIdsEnd();
  while (pxpointId != pxendId)
    {
    std::cout << *pxpointId << ", ";
    pxpointId++;
    }
  std::cout << std::endl;

  // Make a copy
  CellAutoPointer copyOfCell;
  cell->MakeCopy(copyOfCell);
  std::cout << "    PointIds for copied cell: ";
  typename TCell::PointIdConstIterator xpointId = copyOfCell->PointIdsBegin();
  typename TCell::PointIdConstIterator xendId = copyOfCell->PointIdsEnd();
  while (xpointId != xendId)
    {
    std::cout << *xpointId << ", ";
    xpointId++;
    }
  std::cout << std::endl;


  delete[] pointIds;
  return EXIT_SUCCESS;
}

// Test the QEcell interface

template<typename TCell> int TestQECellInterface(std::string name, TCell *aCell)
{
  std::cout << "-------- " << name << "("
            << aCell->GetNameOfClass() << ")" << std::endl;

  TCell *  cell = aCell;
  const TCell * cell2 = aCell;

  std::cout << "    QE Iterator test: PointIds for empty cell: ";
  typename TCell::PointIdInternalIterator pointId
    = cell->InternalPointIdsBegin();
  typename TCell::PointIdInternalIterator endId
    = cell->InternalPointIdsEnd();
  while (pointId != endId)
    {
    std::cout << *pointId << ", ";
    pointId++;
    }
  std::cout << std::endl;

  std::cout << "    ConstIterator test: PointIds for empty cell: ";
  typename TCell::PointIdInternalConstIterator cpointId
    = cell2->InternalPointIdsBegin();
  typename TCell::PointIdInternalConstIterator cendId
    = cell2->InternalPointIdsEnd();

  while (cpointId != cendId)
    {
    std::cout << *cpointId << ", ";
    cpointId++;
    }
  std::cout << std::endl;

  // Add point ids
  std::cout << "    SetPointIds" << std::endl;

  typedef typename TCell::PointIdentifier  PointIdentifier;

  PointIdentifier *pointIds = new PointIdentifier[cell->GetNumberOfPoints() * 2];
  for (unsigned int i = 0; i < cell->GetNumberOfPoints() * 2; i++)
    {
    pointIds[i] = i;
    }

  // actually populate
  cell->SetPointIds(pointIds);
  // exercing the non const internal equivalent.
  cell->InternalSetPointIds( cell->InternalGetPointIds( ) );
  // exercing the const internal equivalent
  cell->InternalSetPointIds( cell2->InternalGetPointIds( ));


  std::cout << "    ConstIterator test: PointIds for populated cell: ";
  typename TCell::PointIdInternalConstIterator ppointId
    = cell2->InternalPointIdsBegin();
  typename TCell::PointIdInternalConstIterator pendId
    = cell2->InternalPointIdsEnd();
  while (ppointId != pendId)
    {
    std::cout << *ppointId << ", ";
    ppointId++;
    }
  std::cout << std::endl;

  cell->InternalSetPointIds( cell2->InternalPointIdsBegin(),
                             cell2->InternalPointIdsEnd());
  std::cout << "    Iterator test: PointIds for populated cell: ";
  typename TCell::PointIdInternalIterator pxpointId
    = cell->InternalPointIdsBegin();
  typename TCell::PointIdInternalIterator pxendId
    = cell->InternalPointIdsEnd();
  while (pxpointId != pxendId)
    {
    std::cout << *pxpointId << ", ";
    pxpointId++;
    }
  std::cout << std::endl;

  delete[] pointIds;
  return EXIT_SUCCESS;
}


int itkQuadEdgeMeshCellInterfaceTest(int, char* [] )
{
  int status;

  /*
   * ITK NATIVE CELLS
   */
  typedef itk::VertexCell<CellInterfaceType> VertexCellType;
  status = TestCellInterface("Vertex", new VertexCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::LineCell<CellInterfaceType> LineCellType;
  status = TestCellInterface("Line", new LineCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::TriangleCell<CellInterfaceType> TriangleCellType;
  status = TestCellInterface("Triangle", new TriangleCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::HexahedronCell<CellInterfaceType> HexahedronCellType;
  status = TestCellInterface("HexahedronCell", new HexahedronCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::TetrahedronCell<CellInterfaceType> TetrahedronCellType;
  status = TestCellInterface("TetrahedronCell", new TetrahedronCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::QuadraticEdgeCell<CellInterfaceType> QuadraticEdgeCellType;
  status = TestCellInterface("QuadraticEdgeCell", new QuadraticEdgeCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::QuadraticTriangleCell<CellInterfaceType>
    QuadraticTriangleCellType;
  status = TestCellInterface("QuadraticTriangleCell",
                             new QuadraticTriangleCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::QuadrilateralCell<CellInterfaceType> QuadrilateralCellType;
  status = TestCellInterface("QuadrilateralCell",
                             new QuadrilateralCellType);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::PolygonCell<CellInterfaceType> PolygonCellType;
  status = TestCellInterface("PolygonCell with 0 vertices",
                             new PolygonCellType());
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  typedef itk::PolygonCell<CellInterfaceType> PolygonCellType;
  status = TestCellInterface("PolygonCell with 5 vertices",
                             new PolygonCellType(5));
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  // ITK QuadEdgeMesh CELLS - Standard cell API test

  QEPolygonCellType* tempQEPolygon = new QEPolygonCellType;
  status = TestCellInterface("QuadEdgePolygonCell with 0 vertices",
                             tempQEPolygon);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  tempQEPolygon = new QEPolygonCellType(5);
  status = TestCellInterface("QuadEdgePolygonCell with 5 vertices",
                             tempQEPolygon);
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  status = TestCellInterface("QuadEdgeLineCell", new QELineCellType());
  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  // ITK QuadEdgeMesh CELLS - Specific QEcell API test

  tempQEPolygon = new QEPolygonCellType();

  status = TestQECellInterface("QuadEdgePolygonCell with 0 vertices",
                             tempQEPolygon);
  delete tempQEPolygon;

  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  tempQEPolygon = new QEPolygonCellType(5);
  status = TestQECellInterface("QuadEdgePolygonCell with 5 vertices",
                             tempQEPolygon);
  delete tempQEPolygon;

  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  QELineCellType* tempQELine = new QELineCellType;
  status = TestQECellInterface("QuadEdgeLineCell", tempQELine);
  delete tempQELine;

  if (status != 0)
    {
    return EXIT_FAILURE;
    }

  // test the visitor API
  typedef itk::CellInterfaceVisitorImplementation<
                              PixelType, MeshType::CellTraits,
                              QELineCellType, CustomQELineVisitor
                                                 > QELineVisitorInterfaceType;
  QELineVisitorInterfaceType::Pointer QELineVisitor =
                                   QELineVisitorInterfaceType::New();

  typedef itk::CellInterfaceVisitorImplementation<
                              PixelType, MeshType::CellTraits,
                              QEPolygonCellType, CustomQEPolyVisitor
                                                 > QEPolyVisitorInterfaceType;
  QEPolyVisitorInterfaceType::Pointer QEPolyVisitor =
                                   QEPolyVisitorInterfaceType::New();


  typedef CellType::MultiVisitor CellMultiVisitorType;
  CellMultiVisitorType::Pointer multiVisitor = CellMultiVisitorType::New();
  multiVisitor->AddVisitor( QELineVisitor );
  multiVisitor->AddVisitor( QEPolyVisitor );

  MeshType::Pointer mesh = MeshType::New( );
  MeshType::PointType pts[3];
  pts[0][0] = 0; pts[0][1] = 0; pts[0][2] = 0;
  pts[1][0] = 0; pts[1][1] = 0; pts[1][2] = 1;
  pts[2][0] = 0; pts[2][1] = 1; pts[2][2] = 0;
  mesh->SetPoint( 0, pts[0] );
  mesh->SetPoint( 1, pts[1] );
  mesh->SetPoint( 2, pts[2] );
  mesh->AddFaceTriangle( 0, 1, 2 );

  mesh->Accept(   multiVisitor );

  // test 4 very specific QELineCell destructor cases
  QELineCellType* test = new QELineCellType();
  QEType* m_QuadEdgeGeom = test->GetQEGeom( );
  delete m_QuadEdgeGeom->GetRot( )->GetRot( )->GetRot( );
  m_QuadEdgeGeom->GetRot( )->GetRot( )->SetRot( ITK_NULLPTR );
  delete test;

  test = new QELineCellType();
  m_QuadEdgeGeom = test->GetQEGeom( );
  delete m_QuadEdgeGeom->GetRot( )->GetRot( )->GetRot( );
  m_QuadEdgeGeom->GetRot( )->GetRot( )->SetRot( ITK_NULLPTR );
  delete m_QuadEdgeGeom->GetRot( )->GetRot( );
  m_QuadEdgeGeom->GetRot( )->SetRot( ITK_NULLPTR );
  delete test;

  test = new QELineCellType();
  m_QuadEdgeGeom = test->GetQEGeom( );
  delete m_QuadEdgeGeom->GetRot( )->GetRot( )->GetRot( );
  m_QuadEdgeGeom->GetRot( )->GetRot( )->SetRot( ITK_NULLPTR );
  delete m_QuadEdgeGeom->GetRot( )->GetRot( );
  m_QuadEdgeGeom->GetRot( )->SetRot( ITK_NULLPTR );
  delete m_QuadEdgeGeom->GetRot( );
  m_QuadEdgeGeom->SetRot( ITK_NULLPTR );
  delete test;

  return status;
}