artem-ogre / CDT

Constrained Delaunay Triangulation (C++)
https://artem-ogre.github.io/CDT/
Mozilla Public License 2.0
1.08k stars 136 forks source link

example how to build geometry from processed data #83

Closed Meldryt closed 2 years ago

Meldryt commented 2 years ago

Hi, I am currently trying to figure out how to use processed triangle and vertices to draw them as triangles, but i get weird results. Are the indices and vertices ready to use or do i have to go through neighbors or something? Can you show an example how you to access the processed data?

CDT::Triangulation<double> cdt;
cdt.insertVertices(
    points.begin(),
    points.end(),
    [](const CDT::V2d<double>& p) { return p.x; },
    [](const CDT::V2d<double>& p) { return p.y; }
);
cdt.insertEdges(
    edges.begin(),
    edges.end(),
    [](const CDT::Edge& e) { return e.v1(); },
    [](const CDT::Edge& e) { return e.v2(); }
);

cdt.eraseOuterTrianglesAndHoles();

for (uint64_t i = 0; i < cdt.vertices.size(); ++i)
{
    CDT::V2d vertex = cdt.vertices[i];
    double height = 0.0;
    geometryData.positions.push_back(glm::vec3(vertex.x, vertex.y, height));
}

for (uint64_t i = 0; i < cdt.triangles.size(); ++i)
{
    CDT::Triangle triangle = cdt.triangles[i];
    CDT::VerticesArr3 vertices = triangle.vertices;
    for (uint8_t j = 0; j < vertices.size(); ++j)
    {
        geometryData.indices.push_back(vertices[j]);
    }
    geometryData.indices.push_back(globals::restartIndex);
}
artem-ogre commented 2 years ago

You could look into visualizer code to see how triangles can be retrieved and rendered: https://github.com/artem-ogre/CDT/blob/master/visualizer/main.cpp For example, here's the loop that draws triangles: https://github.com/artem-ogre/CDT/blob/master/visualizer/main.cpp#L296

artem-ogre commented 2 years ago

Each triangle is represented by three indices into the cdt.vertices array.

Meldryt commented 2 years ago

thx. its working now.