polylinevertexmeshlabpymeshlab

Edge data from generated polyline in Meshlab


I am generating polylines in Meshlab through the 'Compute Planar Section' filter, with code as seen here.

for z_value in np.arange(0, 5, 1):
    ms.set_current_mesh(0)
    planeoffset = float(z_value)
    ms.compute_planar_section(planeaxis = 'Z Axis', planeoffset = planeoffset)
    m = ms.current_mesh()
    m.compact()
    print(m.vertex_number(), "vertices in Planar Section Z =", planeoffset)

What I would like to be able to do is obtain the data that is used to connect one point to another. Meshlab holds this data, as when I export my polyline to DXF, the edges are present, correctly joined together.

I imagine a list, where each edge has a start and endpoint (potentially vertex ID), as seen in the DXF would be the most help.

Any guidance in helping obtain this information would be greatly appreciated.


Solution

  • Update: Pymeshlab developpers have already included the method m.edge_matrix() in the current version of pymeshlab to expose edge data. Since then, this is the recommended way to solve your problem if you have a modern version of pymeshlab.

    I have to bring bad news. At current day (October 2021) the edge information that you request is stored internally in the VCG meshes but it is not exposed to python API, so you can't read it using pymeshlab. You can only read the number of edges using the m.edge_number() method.

    If you need to continue with your project, yours options are:

    1. Write one issue at https://github.com/cnr-isti-vclab/PyMeshLab/issues/ kindly asking the developers to expose the edge information to pymeshlab api.
    2. If your surfaces are convex, you can rebuild the edge data computing the convex hull of the vertex, or by sorting the vertex by angle around the centroid of the vertex.
    3. If your surfaces are complex, you can still store the mesh into a dxf file, and then parse the dxf to read the info back

    The option 3 seems to be the most easy to achieve. The DXF files written by meshlab have a lot of sections

    LINE
    8
    0
    10
    40.243473   -> this is coordinate X of point 1
    20
    -40.981182  -> this is coordinate Y of point 1
    30
    0.000000    -> this is coordinate Z of point 1
    11
    40.887867    -> this is coordinate X of point 2
    21
    -42.090389   -> this is coordinate Y of point 2
    31
    0.000000    -> this is coordinate Z of point 2
    0
    

    so you can parse the dxf file with this piece of python code

    edges=[]
    with open("contour.dxf") as f:
        line = f.readline().strip()
        while line:
            if line == "LINE" :
                f.readline()
                f.readline()
                f.readline()
                x1 = float(f.readline())
                f.readline()
                y1 = float(f.readline())
                f.readline()
                z1 = float(f.readline())
                f.readline()
                x2 = float(f.readline())
                f.readline()
                y2 = float(f.readline())
                f.readline()
                z2 = float(f.readline())
                print("edge from", (x1,y1,z1), "to", (x2,y2,z2))
                edges.append( ( (x1,y1,z1), (x2,y2,z2) ) )
            line = f.readline().strip()