points and texture coordinate in vtkPolyData vtkOBJReader ?

I am reading in an OBJ file that has both points and texture coordinate information, I am reading them in via vtkOBJReader

How should I go about extracting those information ? Should I go via Cell or Poly ?

#include <vtkOBJReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyData.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <string>
#include <iostream>

void printOBJInfo(vtkPolyData* const polydata) {
	std::cout << "GetNumberOfVerts: " << polydata->GetNumberOfVerts() << std::endl;
	std::cout << "GetNumberOfPolys: " << polydata->GetNumberOfPolys() << std::endl;
	std::cout << "GetNumberOfCells: " << polydata->GetNumberOfCells() << std::endl;
}

Is there an example which illustrate this sort of queries ?

Cheers

Answering my own question, I believe I need to look for Points and the texture coordinate is stored as vtkDataArray. The following seems to work

void printOBJInfo(vtkPolyData* const polydata) {
	std::cout << "GetNumberOfVerts: " << polydata->GetNumberOfVerts() << std::endl;
	std::cout << "GetNumberOfPoints: " << polydata->GetNumberOfPoints() << std::endl;
	std::cout << "GetNumberOfPolys: " << polydata->GetNumberOfPolys() << std::endl;
	std::cout << "GetNumberOfCells: " << polydata->GetNumberOfCells() << std::endl;

	vtkPoints *p = polydata->GetPoints();
	vtkPointData *pd = polydata->GetPointData();
	int numArrays = pd->GetNumberOfArrays();
	printf("numArrays %d\n",numArrays);
	for (auto i=0;i<numArrays;i++) {
		printf("Array name = '%s'\n",pd->GetArrayName(i));
	}
	for (auto i=0;i<polydata->GetNumberOfPoints();i++) {
		double x = p->GetPoint(i)[0];
		double y = p->GetPoint(i)[1];
		double z = p->GetPoint(i)[2];
		printf("v %f,%f,%f\n",x,y,z);
	}
	std::string arrayName("TCoords");
	vtkDataArray *da = pd->GetArray(arrayName.c_str());
	assert(da);
	auto numTuples = da->GetNumberOfTuples();
	auto numComponents = da->GetNumberOfComponents();
	for (auto i=0;i<numTuples;i++) {
		if (numComponents==2)
			printf("uv %f,%f\n",da->GetTuple2(i)[0],da->GetTuple2(i)[1]);
		else if (numComponents==3)
			printf("uv %f,%f,%f\n",da->GetTuple3(i)[0],da->GetTuple3(i)[1],da->GetTuple3(i)[2]);
	}
}

It seems that the array “TCoords” is not always present even when e.g. using vtkOBJReader to load in an OBJ file that has texture coordinate.

Is there a direct API call for retrieving texture coordinate reliably via VTK ?

@mwestphal hope you can share some expertise tips here :slight_smile: .

I came across the method GetTCoords(), is this the way to go ?

	vtkPointData *pd = polydata->GetPointData();
	vtkDataArray *vda = pd->GetTCoords();

I have more development around using GetTCoords() but it returns unexpected values. The tuple count looks reasonable (depends on point-varying or facevarying texture coordinates)

I now have the following test code

	// Query the poly indices
	vtkPoints *p = polydata->GetPoints();
	vtkCellData* cd = polydata->GetCellData();
	vtkCellArray *polys = polydata->GetPolys();
	assert(polys);
	vtkPointData *pd = polydata->GetPointData();
	// vtkDataArray* tcoords = cd->GetTCoords();
	vtkDataArray* tcoords = pd->GetTCoords();
	if (tcoords) {
		uv_areas->SetName("uv_areas");
		uv_areas->SetNumberOfComponents(1);
		vtkIdType numTuples = tcoords->GetNumberOfTuples();
		std::cout << boost::format("numTuples %1%") % numTuples << std::endl;

		{
		    for (auto i = 0; i < tcoords->GetNumberOfTuples(); i++)
		    {
				double *p = tcoords->GetTuple(i);
				std::cout << boost::format("vt %1% %2%") % p[0] % p[1]
						<< std::endl;
		    }

		}
	}

I see the following printed out, the whole set of -1 is unexpected.

GetNumberOfVerts  0
GetNumberOfPoints 81654
numTuples 81654
vt -1 -1
vt -1 -1
vt -1 -1
vt -1 -1

My source geometry (Wavefront OBJ file) has the following data
snippet

v -0.383699298 0.0305271503 0.765173852
v -0.392188191 0.0444016196 0.731078923
v -0.397135496 0.0690283626 0.712362707
v -0.396477133 0.0822300315 0.70755136
v -0.396951139 0.0956643596 0.706578732
v -0.401354492 0.116896018 0.712859452
v -0.40205282 0.136538953 0.7307055
vt 0.614133537 0.230135888 0
vt 0.614440203 0.229551509 0
vt 0.61633867 0.230652139 0
vt 0.616162419 0.23116152 0
vt 0.612020731 0.230625212 0
vt 0.61179179 0.230252698 0
vt 0.610773683 0.231699765 0
vt 0.610486925 0.231454507 0
vt 0.610056043 0.232428282 0
vt 0.609743118 0.232246235 0
vt 0.609679401 0.23315841 0

I am getting a little lost now.

Look for SetTCoord in vtkOBJReader. the vtkOBJReader do not set the TCoords.