1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestOBJReaderMultiTexture.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkOBJReader.h"
16 #include "vtkDebugLeaks.h"
17 
18 #include "vtkPointData.h"
19 #include "vtkNew.h"
20 #include "vtkTestUtilities.h"
21 
22 //------------------------------------------------------------------------------
TestOBJReaderMultiTexture(int argc,char * argv[])23 int TestOBJReaderMultiTexture(int argc, char *argv[])
24 {
25   // Create the reader.
26   char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv,
27     "Data/obj_multitexture.obj");
28 
29   vtkNew<vtkOBJReader> reader;
30   reader->SetFileName(fname);
31   reader->Update();
32 
33   vtkPolyData *data = reader->GetOutput();
34 
35   delete[] fname;
36 
37   if (!data)
38   {
39     std::cerr << "Could not read data" << std::endl;
40     return EXIT_FAILURE;
41   }
42 
43   // The OBJ file has 3 cells and 8 points.
44   // 4 of those points have 2 textures associated, thus the reader output
45   // must have 12 points.
46   if (data->GetNumberOfPoints() != 12 && data->GetNumberOfCells() == 3)
47   {
48     std::cerr << "Invalid number of points or cells" << std::endl;
49     return EXIT_FAILURE;
50   }
51 
52   // The output must have 2 arrays, texture_0 & texture_1. texture_0 has
53   // (-1, -1) for indices 4 to 7. texture_1 has (-1, -1) for every indices but
54   // those between 4 and 7
55 
56   // Check the number of arrays
57   if (data->GetPointData()->GetNumberOfArrays() != 2)
58   {
59     std::cerr << "Invalid number of arrays" << std::endl;
60     return EXIT_FAILURE;
61   }
62 
63   vtkDataArray* texture0 = data->GetPointData()->GetArray("texture_0");
64   vtkDataArray* texture1 = data->GetPointData()->GetArray("texture_1");
65   // Check if the arrays are named correctly
66   if (!texture0)
67   {
68     std::cerr << "Could not find texture_0 array" << std::endl;
69     return EXIT_FAILURE;
70   }
71 
72   if (!texture1)
73   {
74     std::cerr << "Could not find texture_1 array" << std::endl;
75     return EXIT_FAILURE;
76   }
77 
78   // Check the values
79   for (int i = 0; i < 12; ++i)
80   {
81     double* currentTCoord0 = texture0->GetTuple2(i);
82     double* currentTCoord1 = texture1->GetTuple2(i);
83 
84     // Testing values outside [4, 7]
85     if (i < 4 || i > 7)
86     {
87       if ((currentTCoord0[0] == -1 && currentTCoord0[1] == -1)
88         || !(currentTCoord1[0] == -1 && currentTCoord1[1] == -1))
89       {
90         std::cerr << "Unexpected texture values" << std::endl;
91         return EXIT_FAILURE;
92       }
93     }
94     // Testing values inside [4, 7]
95     else
96     {
97       if (!(currentTCoord0[0] == -1 && currentTCoord0[1] == -1)
98         || (currentTCoord1[0] == -1 && currentTCoord1[1] == -1))
99       {
100         std::cerr << "Unexpected texture values" << std::endl;
101         return EXIT_FAILURE;
102       }
103     }
104   }
105 
106   return EXIT_SUCCESS;
107 }
108