[Python] CGAL python bindings: Triangulations with vertex info

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to CGAL bindings and am trying to implement mesh-based interpolation. I have a triangulation with values stored at each vertex, and I have a query point that I want to interpolate the values on. This requires storing the values inside the triangulation using the Triangulation_vertex_base_with_info_3 CGAL class. In effect, I'm trying to reproduce the below CGAL tutorial ( https://doc.cgal.org/latest/Triangulation_3/Triangulation_3_2color_8cpp-example.html ) within the context of CGAL python bindings:

    #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>

    #include <CGAL/Delaunay_triangulation_3.h>
    #include <CGAL/Delaunay_triangulation_cell_base_3.h>
    #include <CGAL/Triangulation_vertex_base_with_info_3.h>

    #include <CGAL/IO/Color.h>

    typedef CGAL::Exact_predicates_inexact_constructions_kernel K;

    typedef CGAL::Triangulation_vertex_base_with_info_3<CGAL::IO::Color, K> Vb;
    typedef CGAL::Delaunay_triangulation_cell_base_3<K> Cb;
    typedef CGAL::Triangulation_data_structure_3<Vb, Cb> Tds;
    typedef CGAL::Delaunay_triangulation_3<K, Tds> Delaunay;

    typedef Delaunay::point Point;

    int main()
    {
    Delaunay T;

    T.insert(Point(0,0,0));
    T.insert(Point(1,0,0));
    T.insert(Point(0,1,0));
    T.insert(Point(0,0,1));
    T.insert(Point(2,2,2));
    T.insert(Point(-1,0,1));

    // Set the color of finite vertices of degree 6 to red.
    Delaunay::Finite_vertices_iterator vit;
    for (Delaunay::Vertex_handle v : T.finite_vertex_handles())
    if (T.degree(v) == 6)
    v->info() = CGAL::IO::red();

    return 0;
    }


    Just with the vertex info being something like np.float32 instead of CGAL::IO::Color.

    I'm doing this through the bindings because this is in the context of a script that otherwise works best in Python.

    What's shown below is my attempted implementation of the bindings. There's a few things I'm unsure about, like if a Python tuple is the equivalent of the command std::make_pair(point(x,y,z),value) or if I'm setting up the triangulation data structure correctly. However, these are hypothetical concerns, because the real problem is that the script fails in the import commands due to it not finding Delaunay_triangulation_cell_base_3 or Triangulation_vertex_base_with_info_3 in the compiled CGAL kernel. I've also tried importing from CGAL.CGAL_Triangulation_3, and that also fails. I'm not sure where these entities would be stored in the bindings, if not in these places. The tutorial quoted above indicates that these should be imported right from the kernel. Am I misunderstanding where these are located or what their syntax is, or does this look right and I'm experiencing an installation issue?

    from CGAL.CGAL_Kernel import Point_3
    from CGAL.CGAL_Kernel import Delaunay_triangulation_cell_base_3
    from CGAL.CGAL_Kernel import Triangulation_vertex_base_with_info_3
    from CGAL.CGAL_Triangulation_3 import Triangulation_data_structure_3
    from CGAL.CGAL_Triangulation_3 import Delaunay_triangulation_3
    from CGAL.CGAL_Triangulation_3 import Ref_Locate_type_3
    from CGAL.CGAL_Kernel import Ref_int
    import numpy as np

    # construct triangulation with info at each node
    vertex_base=Triangulation_vertex_base_with_info_3(np.float32,CGAL.CGAL_Kernel)
    dataStructure=Triangulation_data_structure_3(vertex_base,Delaunay_triangulation_cell_base)
    T=Delaunay_triangulation_3(dataStructure)


    NUMNODES=170000
    NODES=np.random.rand(NUMNODES,3)
    NODE_VALUES=np.random.rand(NUMNODES) # values stored at each node
    L=[]
    for ii in range(np.shape(NODES)[0]):
    # not entirely sure if tuple is equivalent of
    # std::make_pair(point(x,y,z),value)
    temp=(Point_3(NODES[ii,0],NODES[ii,1],NODES[ii,2]),NODE_VALUES[ii])
    T.insert(temp)
    print(f'Triangulation made, # vertices: {T.number_of_vertices()}')

    # T.locate() can't seem to query more than one point at a time
    query_point=Point_3(0.5,0.5,0.5)
    enclosingTets=T.locate(query_point,Ref_Locate_type_3(),Ref_int(),Ref_int())
    for jj in range(1):
    print(dir(enclosingTets.vertex(jj).point()))
    print(enclosingTets.vertex(jj).point())


    Using std::make_pair() to place the vertex values comes from this StackOverflow post CGAL - Retrieve Vertex Index After Delaunay Triangulation

    Continue reading...

Compartilhe esta Página