# Why do obj files read more vertices?

**URL:** <https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204>\
**Category:** Development\
**Created:** [September 13, 2020, 7:54am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204 "2020-09-13T07:54:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DreamLFairy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@DreamLFairy](https://discourse.vtk.org/u/DreamLFairy)\
**Post date:** [September 13, 2020, 7:54am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/1 "2020-09-13T07:54:06Z")

</div>

I use it meshlab.exe Open an obj file, or use Notepad to open an obj file. There are only 26408 vertices in it。  
The obj file format is as follows (I removed the annotation symbol#):

` Wavefront OBJ mtllib model.mtl 26408 vertices v -0.0495170839 0.7720808848 0.4657617399 v -0.0456603417 0.7741941402 0.4641847307 v -0.1665510523 0.7487052447 -0.0761576362 v 0.0980114390 0.7601768368 -0.0284780685 v 0.0954305225 0.7593402284 -0.0304180109 v 0.2112363794 0.7507016730 0.0587138140 v 0.2117039923 0.7513025276 0.0564567301 v 0.2442548111 0.7516214668 0.0573516367 v -0.1019155675 0.7483195137 -0.0658163599 v 0.3240373672 0.6416865419 -0.0256790367 v 0.2156865962 0.7512316044 0.0407874909 v 0.2910738839 0.7496361757 0.0008729120 v 0.2914005528 0.7498561777 -0.0001805026 v -0.1002491918 0.7556102703 0.0010426163 v -0.0954618129 0.7603561897 0.0031562183 v -0.0957103628 0.7601207650 0.0028731435 v 0.2860406632 0.7503417712 0.0227865344 v 0.2859714883 0.7505213540 0.0225769585 v -0.1583670350 0.7480347469 -0.1123186190 v -0.0569953775 0.7926299248 -0.0253003932 v -0.0781332995 0.7749295899 0.6390007532 v 0.1873167278 0.7809761373 0.6831041962 v 0.1824798420 0.7812302528 0.6822939733 v 0.0946332339 0.7807375689 0.6676606828 v 0.1271371529 0.7810844599 0.6730712102 v 0.0919379506 0.7801290763 0.6672108222 v 0.2039010933 0.7810425682 0.6858654135 v -0.2348067498 0.7754054320 0.6128682305 v -0.2638290460 0.7761159476 0.6080135198 ......`

The code is as follows:

`

```
   auto reader =vtkSmartPointer<vtkOBJReader>::New();

reader->SetFileName(fileName.c_str());

reader->Update();

polyData = reader->GetOutput();

    std::cout << "There are " << polyData->GetNumberOfPoints() << " GetNumberOfPoints." << std::endl;

```

`

but the output is  
`There are 156909 GetNumberOfPoints.`

How to make the number of points output with vtkobjreader as many as the number of vertices?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [September 17, 2020, 3:18am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/2 "2020-09-17T03:18:36Z")

</div>

The OBJ reader may duplicate some vertices:

> <https://github.com/Kitware/VTK/blob/2c0b936a7676ae4b83b606d2aabec8296a47c6de/IO/Geometry/vtkOBJReader.cxx#L95-L96>

You may run vtkCleanPolyData to remove merge coincident points. If you need exact point ID correspondence then you may try to use another mesh file format (VTK, VTP, PLY, …).

---

<div class="post-metadata">

**Author:** ![DreamLFairy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@DreamLFairy](https://discourse.vtk.org/u/DreamLFairy)\
**Post date:** [September 17, 2020, 4:31am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/4 "2020-09-17T04:31:17Z")

</div>

Thank you very much!  
You have solved my problem!

But can I ask you another problem?

My purpose is to downsample .obj file because there are too many points and faces in .obj files. So I import obj file with vtkOBJImporter, then use vtkCleanPolyData to reduce points, and then use vtkDecimatePro to reduce points and faces(I use 0.5 reduction）. Finally, I use vtkOBJExporter to export obj file.

The texture effect of the exported obj file is poor. How can I make downsampling obj files with texture better?

original:

 ![sm2](https://discourse.vtk.org/uploads/default/original/2X/e/ee028b6503f6df537f6bdc1f1ed3ac122e7b892f.png)

output:

 ![sm](https://discourse.vtk.org/uploads/default/original/2X/f/f841169fe69e7742e61447299097ab5129a19ba1.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [September 17, 2020, 4:35am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/5 "2020-09-17T04:35:58Z")

</div>

DecimatePro results are usually not very nice. I mostly use quadric decimation instead.

---

<div class="post-metadata">

**Author:** ![DreamLFairy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@DreamLFairy](https://discourse.vtk.org/u/DreamLFairy)\
**Post date:** [September 17, 2020, 4:47am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/7 "2020-09-17T04:47:23Z")

</div>

I use the following code for down sampling, and then the error as shown in the figure below appears, and the figure after down sampling is as follows.

```
	vtkSmartPointer<vtkQuadricDecimation> decimate =
	vtkSmartPointer<vtkQuadricDecimation>::New();
decimate->SetInputData(pd);
decimate->AttributeErrorMetricOn();
decimate->SetTargetReduction(.5);
decimate->VolumePreservationOn();

decimate->Update();
vtkSmartPointer<vtkPolyData> decimated =
	vtkSmartPointer<vtkPolyData>::New();
decimated->ShallowCopy(decimate->GetOutput());

```

 ![error](https://discourse.vtk.org/uploads/default/original/2X/d/d77dbdcef9a5d9437189eeab67e0a1b7549479e2.png)

 ![sm3](https://discourse.vtk.org/uploads/default/original/2X/c/c0b13b318491c4502a725562d76be58c470af09d.png)

---

<div class="post-metadata">

**Author:** ![DreamLFairy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@DreamLFairy](https://discourse.vtk.org/u/DreamLFairy)\
**Post date:** [September 17, 2020, 5:27am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/8 "2020-09-17T05:27:20Z")

</div>

The complete code is as follows:

```
void Reduction(string fileNameObj, string fileNameMtl,
string fileNametexture, string exportFileName) {

/*fileNameObj = "F:\\AIData\\test1\\model1.obj";
fileNameMtl = "F:\\AIData\\test1\\model1.mtl";
fileNametexture = "F:\\AIData\\test1\\";*/

std::cout << "Reduction " << std::endl;
auto importer = vtkSmartPointer<vtkOBJImporter>::New();
importer->SetFileName(fileNameObj.c_str());
importer->SetFileNameMTL(fileNameMtl.c_str());
importer->SetTexturePath(fileNametexture.c_str());
auto backgroundRenderer = vtkSmartPointer<vtkRenderer>::New();
auto renderer = vtkSmartPointer<vtkRenderer>::New();
auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();

renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
importer->SetRenderWindow(renderWindow);
importer->Update();
auto actors = renderer->GetActors();
actors->InitTraversal();
vtkActor* actor;
vtkPolyData* pd;
vtkPolyDataMapper* mapper;
for (vtkIdType a = 0; a < actors->GetNumberOfItems(); ++a)
{
	actor = actors->GetNextActor();
	if (actor->GetTexture())
	{
		//std::cout << "Has texture\n";
		actor->GetTexture()->InterpolateOn();
	}
	pd = dynamic_cast<vtkPolyData*>(actor->GetMapper()->GetInput());
}
std::cout << "Input cube has " << pd->GetNumberOfPoints()
	<< " vertices." << std::endl;

vtkSmartPointer<vtkCleanPolyData> cleanPolyData =
	vtkSmartPointer<vtkCleanPolyData>::New();
//cleanPolyData->SetInputConnection(cubeSource->GetOutputPort());
cleanPolyData->SetInputData(pd);
cleanPolyData->Update();

std::cout << "after cube has " << cleanPolyData->GetOutput()->GetNumberOfPoints()
	<< " vertices." << std::endl;

vtkSmartPointer<vtkQuadricDecimation> decimate =
	vtkSmartPointer<vtkQuadricDecimation>::New();
decimate->SetInputData(pd);
decimate->AttributeErrorMetricOn();
decimate->SetTargetReduction(.5);
decimate->VolumePreservationOn();

decimate->Update();
vtkSmartPointer<vtkPolyData> decimated =
	vtkSmartPointer<vtkPolyData>::New();
decimated->ShallowCopy(decimate->GetOutput());

//vtkSmartPointer<vtkDecimatePro> decimate =
//	vtkSmartPointer<vtkDecimatePro>::New();
//
//decimate->SetInputConnection(cleanPolyData->GetOutputPort());
//decimate->SetTargetReduction(0.5);
//decimate->PreserveTopologyOn();
//decimate->Update();

vtkSmartPointer<vtkPolyDataMapper> decimatedMapper =
	vtkSmartPointer<vtkPolyDataMapper>::New();

decimatedMapper->SetInputData(decimated);

vtkSmartPointer<vtkActor> decimatedActor =
	vtkSmartPointer<vtkActor>::New();
decimatedActor->SetMapper(decimatedMapper);
//renderer->AddActor(actor);

renderer->RemoveActor(actor);
renderer->AddActor(decimatedActor);
renderWindow->AddRenderer(renderer);

auto exporter = vtkSmartPointer<vtkOBJExporter>::New();
exporter->SetActiveRenderer(renderer);
exporter->SetRenderWindow(renderWindow);

exporter->SetFilePrefix(exportFileName.c_str());

std::cout << "Writing " << exportFileName << std::endl;
exporter->Write();
std::cout << "success " << std::endl;

```

}

---

<div class="post-metadata">

**Author:** ![DreamLFairy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@DreamLFairy](https://discourse.vtk.org/u/DreamLFairy)\
**Post date:** [September 17, 2020, 5:30am UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/9 "2020-09-17T05:30:24Z")

</div>

The complete OBJ files is as follows（

Because the original obj file is too large, What I uploaded is an obj file which has been simplified with wrap software. The effect after the simplification of wrap software is what I want.）:  
[model1.mtl](https://discourse.vtk.org/uploads/short-url/8nj0gLYVDIKjTFC1KHNzkcYBkgs.mtl) (436 Bytes)

 ![model1material_0](https://discourse.vtk.org/uploads/default/original/2X/d/d288e756f553d08b895f85a0fc30d26d138e3bde.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [September 17, 2020, 7:53pm UTC](https://discourse.vtk.org/t/why-do-obj-files-read-more-vertices/4204/10 "2020-09-17T19:53:44Z")

</div>

Those errors logged by vtkQuadricDecimation are usually harmless. However, I’m not sure that the class is prepared to process texture data.

If you upload the full file somewhere and post the link here then maybe someone who has experience with texture-mapped surfaces can have a look at it (I don’t know that part of VTK well, so I won’t be able to investigate).
