File: TestOFFReader.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (136 lines) | stat: -rw-r--r-- 4,492 bytes parent folder | download | duplicates (4)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
// .NAME Test of vtkOFFReader to read in a simple OFF file
// .SECTION Description
//

#include <vtkCellData.h>
#include <vtkMemoryResourceStream.h>
#include <vtkNew.h>
#include <vtkOFFReader.h>
#include <vtkPointData.h>
#include <vtkTestUtilities.h>

#include <cmath>

//------------------------------------------------------------------------------
int TestOFFReader(int /*argc*/, char** const /*argv*/)
{
  // a simple unit cube mesh with 3 quadrilateral and 6 triangular faces
  static const char* off_file_contents = "OFF\n"
                                         "# a simple unit cube mesh with 9 faces\n"
                                         "8 9 0\n"
                                         "# the set of 8 vertex\n"
                                         "0 0 0\n"
                                         "1 0 0\n"
                                         "0 1 0\n"
                                         "1 1 0\n"
                                         "0 0 1\n"
                                         "1 0 1\n"
                                         "0 1 1\n"
                                         "1 1 1\n"
                                         "# 3 quadrilateral faces\n"
                                         "4 0 1 3 2\n"
                                         "4 4 5 7 6\n"
                                         "4 0 1 5 4\n"
                                         "# 6 triangular faces\n"
                                         "3 2 3 7\n"
                                         "3 7 6 2\n"
                                         "3 0 2 6\n"
                                         "3 6 4 0\n"
                                         "3 1 3 7\n"
                                         "3 7 5 1\n";

  // create a memory resource stream for the file contents
  auto memStream = vtkSmartPointer<vtkMemoryResourceStream>::New();
  memStream->SetBuffer(std::string(off_file_contents));

  // create the reader.
  vtkNew<vtkOFFReader> reader;
  reader->SetStream(memStream);
  reader->Update();

  // get output
  vtkPolyData* data = reader->GetOutput();

  if (!data)
  {
    std::cerr << "Could not read data" << std::endl;
    return EXIT_FAILURE;
  }

  // we should have 8 points
  if (data->GetNumberOfPoints() != 8)
  {
    std::cerr << "Invalid number of points" << std::endl;
    return EXIT_FAILURE;
  }

  // explicitly test a few points
  double x[3];
  data->GetPoint(3, x); // 1 1 0
  x[0] -= 1.0;
  x[1] -= 1.0;
  if (x[0] * x[0] + x[1] * x[1] + x[2] * x[2] > 1E-5)
  {
    std::cerr << "Invalid point coordinates for point 3" << std::endl;
    return EXIT_FAILURE;
  }
  data->GetPoint(5, x); // 1 0 1
  x[0] -= 1.0;
  x[2] -= 1.0;
  if (x[0] * x[0] + x[1] * x[1] + x[2] * x[2] > 1E-5)
  {
    std::cerr << "Invalid point coordinates for point 5" << std::endl;
    return EXIT_FAILURE;
  }
  data->GetPoint(7, x); // 1 1 1
  x[0] -= 1.0;
  x[1] -= 1.0;
  x[2] -= 1.0;
  if (x[0] * x[0] + x[1] * x[1] + x[2] * x[2] > 1E-5)
  {
    std::cerr << "Invalid point coordinates for point 7" << std::endl;
    return EXIT_FAILURE;
  }

  // we should have 9 polygons
  if (data->GetNumberOfPolys() != 9)
  {
    std::cerr << "Invalid number of polygons" << std::endl;
    return EXIT_FAILURE;
  }

  // explicitly test two quads
  vtkNew<vtkIdList> ids;
  data->GetPolys()->GetCellAtId(0, ids);
  if (ids->GetNumberOfIds() != 4 || ids->GetId(0) != 0 || ids->GetId(1) != 1 ||
    ids->GetId(2) != 3 || ids->GetId(3) != 2)
  {
    std::cerr << "Invalid indices for polygon 0" << std::endl;
    return EXIT_FAILURE;
  }
  data->GetPolys()->GetCellAtId(2, ids);
  if (ids->GetNumberOfIds() != 4 || ids->GetId(0) != 0 || ids->GetId(1) != 1 ||
    ids->GetId(2) != 5 || ids->GetId(3) != 4)
  {
    std::cerr << "Invalid indices for polygon 2" << std::endl;
    return EXIT_FAILURE;
  }

  // test two triangles
  data->GetPolys()->GetCellAtId(4, ids);
  if (ids->GetNumberOfIds() != 3 || ids->GetId(0) != 7 || ids->GetId(1) != 6 || ids->GetId(2) != 2)
  {
    std::cerr << "Invalid indices for polygon 4" << std::endl;
    return EXIT_FAILURE;
  }
  data->GetPolys()->GetCellAtId(7, ids);
  if (ids->GetNumberOfIds() != 3 || ids->GetId(0) != 1 || ids->GetId(1) != 3 || ids->GetId(2) != 7)
  {
    std::cerr << "Invalid indices for polygon 7" << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}