Skip to content

polygon_soup_to_polygon_mesh adds extra vertices #4331

@jasjuang

Description

@jasjuang

Issue Details

What happened was I have a particular obj file testmesh.zip, when I load it in with tinyobjloader into vertices and triangle indices, and then convert it into CGAL::Surface_mesh using polygon_soup_to_polygon_mesh, it generates an addition vertex for some reason. Please see below the minimal code to reproduce the problem. Note that the tiny_obj_loader.h can be directly copied from here, it's a header-only library.

Source Code

#include <tiny_obj_loader.h>

void ReadOBJ(const std::string &obj_file,
             std::vector<double> &vert_pos,
             std::vector<int> &tri_ind){
  tinyobj::attrib_t attrib;
  std::vector<tinyobj::shape_t> shapes;
  std::vector<tinyobj::material_t> materials;
  std::string warn;
  std::string err;

  tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, obj_file.c_str());

  for (float vertice : attrib.vertices){
    vert_pos.emplace_back(vertice);
  }

  for (auto &shape : shapes){
    for (auto &indice : shape.mesh.indices){
      tri_ind.emplace_back(indice.vertex_index);
    }
  }
}

void STLtoCGAL(const std::vector<double> &vert_pos,
               const std::vector<int> &tri_ind,
               CGAL::Surface_mesh<
                   CGAL::Exact_predicates_inexact_constructions_kernel::Point_3>
                   &mesh_cgal){
  using Pt_3    = CGAL::Exact_predicates_inexact_constructions_kernel::Point_3;
  using Polygon = std::vector<std::size_t>;

  const auto num_vert = static_cast<int>(vert_pos.size() / 3);
  const auto num_tri  = static_cast<int>(tri_ind.size() / 3);

  std::vector<Pt_3> points(num_vert);
  std::vector<Polygon> polygons(num_tri);

  for (int i = 0; i < num_vert; i++){
    points[i] =
        Pt_3(vert_pos[i * 3 + 0], vert_pos[i * 3 + 1], vert_pos[i * 3 + 2]);
  }

  for (int i = 0; i < num_tri; i++){
    for (int j = 0; j < 3; j++){
      polygons[i].emplace_back(tri_ind[i * 3 + j]);
    }
  }

  CGAL::Polygon_mesh_processing::repair_polygon_soup(points, polygons);
  CGAL::Polygon_mesh_processing::orient_polygon_soup(points, polygons);

  CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(
      points, polygons, mesh_cgal);
}

int main(){
  CGAL::Surface_mesh<
      CGAL::Exact_predicates_inexact_constructions_kernel::Point_3>
      m;

  std::vector<double> vert_pos;
  std::vector<int> tri_ind;

  ReadOBJ("testmesh.obj", vert_pos, tri_ind);

  STLtoCGAL(vert_pos, tri_ind, m);

  std::cout << " Number of vertices in stl mesh " << (vert_pos.size() / 3)
            << std::endl;
  std::cout << " Number of vertices in cgal mesh " << m.number_of_vertices()
            << std::endl;
  return 0;
}

After execution, the output looks like

 Number of vertices in stl mesh 417
 Number of vertices in cgal mesh 418

This is not ideal because if I call STLtoCGAL and then CGALtoSTL it should return the same thing. Please help.

Environment

  • Operating system: Ubuntu 18.04
  • Compiler: GCC 7
  • Release or debug mode: Release
  • CGAL version: Latest
  • Boost version: 1.68

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions