File: TestOBJWriterMultiTexture.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 (146 lines) | stat: -rw-r--r-- 4,175 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
137
138
139
140
141
142
143
144
145
146
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkCellData.h"
#include "vtkDebugLeaks.h"
#include "vtkNew.h"
#include "vtkOBJReader.h"
#include "vtkOBJWriter.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkTestUtilities.h"

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

  // The OBJ file has 3 cells and 8 points.
  // 4 of those points have 2 textures associated, thus the reader output
  // must have 12 points.
  if (data->GetNumberOfPoints() != 12 && data->GetNumberOfCells() == 3)
  {
    std::cerr << "Invalid number of points or cells" << std::endl;
    return EXIT_FAILURE;
  }

  // The output must have 2 arrays, texture_0 & texture_1. texture_0 has
  // (-1, -1) for indices 4 to 7. texture_1 has (-1, -1) for every indices but
  // those between 4 and 7

  // Check the number of arrays
  if (data->GetPointData()->GetNumberOfArrays() != 2)
  {
    std::cerr << "Invalid number of arrays" << std::endl;
    return EXIT_FAILURE;
  }

  vtkDataArray* texture0 = data->GetPointData()->GetArray("texture_0");
  vtkDataArray* texture1 = data->GetPointData()->GetArray("texture_1");
  // Check if the arrays are named correctly
  if (!texture0)
  {
    std::cerr << "Could not find texture_0 array" << std::endl;
    return EXIT_FAILURE;
  }

  if (!texture1)
  {
    std::cerr << "Could not find texture_1 array" << std::endl;
    return EXIT_FAILURE;
  }

  // Check the texture values
  for (int i = 0; i < 12; ++i)
  {
    double* currentTCoord0 = texture0->GetTuple2(i);
    double* currentTCoord1 = texture1->GetTuple2(i);

    // Testing values < 4
    if (i < 4)
    {
      if (!(currentTCoord0[0] == -1 && currentTCoord0[1] == -1) ||
        !(currentTCoord1[0] == -1 && currentTCoord1[1] == -1))
      {
        std::cerr << "Unexpected texture values" << std::endl;
        return EXIT_FAILURE;
      }
    }
    // Testing values inside [4, 7]
    else if (i >= 4 && i <= 7)
    {
      if (!(currentTCoord0[0] == -1 && currentTCoord0[1] == -1) ||
        (currentTCoord1[0] == -1 && currentTCoord1[1] == -1))
      {
        std::cerr << "Unexpected texture values" << std::endl;
        return EXIT_FAILURE;
      }
    }
    // Testing values > 7
    else if (i > 7)
    {
      if ((currentTCoord0[0] == -1 && currentTCoord0[1] == -1) ||
        !(currentTCoord1[0] == -1 && currentTCoord1[1] == -1))
      {
        std::cerr << "Unexpected texture values" << std::endl;
        return EXIT_FAILURE;
      }
    }
  }

  vtkIntArray* materialIds =
    vtkIntArray::SafeDownCast(data->GetCellData()->GetArray("MaterialIds"));
  if (!(materialIds && materialIds->GetNumberOfTuples() == 3 && materialIds->GetValue(0) == 0 &&
        materialIds->GetValue(1) && materialIds->GetValue(2) == 2))
  {
    std::cerr << "Invalid MaterialIds cell array" << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

//------------------------------------------------------------------------------
int TestOBJWriterMultiTexture(int argc, char* argv[])
{
  // Create the reader.
  char* fname =
    vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/obj_multitexture_notexture.obj");

  vtkNew<vtkOBJReader> reader;
  reader->SetFileName(fname);
  reader->Update();
  vtkPolyData* data = reader->GetOutput();
  delete[] fname;

  if (CheckData(data) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }

  char* tname =
    vtkTestUtilities::GetArgOrEnvOrDefault("-T", argc, argv, "VTK_TEMP_DIR", "Testing/Temporary");
  std::string tmpDir(tname);
  delete[] tname;
  std::string filename = tmpDir + "/TestOBJWriterMultiTexture_write.obj";

  vtkNew<vtkOBJWriter> writer;
  writer->SetFileName(filename.c_str());
  writer->SetInputConnection(0, reader->GetOutputPort());
  writer->Write();

  // read this file and compare with input
  vtkNew<vtkOBJReader> reader2;
  reader2->SetFileName(filename.c_str());
  reader2->Update();
  data = reader2->GetOutput();

  if (CheckData(data) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}