This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Extract mesh line segments

0 votes

Hi all,

I try to plot a fenics mesh using a different plotting library (plotly), in order to plot 3D meshes inline in ipython notebook. In order to do that, I need all line segments forming the mesh, ordered in three arrays (x, y and z) in pairs of line start and end points like [line1_startpoint_x, line1_endpoint_x, line2_startpoint_x, line2_endpoint_x ...] and same for y and z.
I know that the node coordinates can be extracted with mesh.coordinates(), but how to order them in line start and end points like this?

Thanks in advance!

Adriaan

asked Feb 9, 2016 by Adriaan FEniCS Novice (660 points)

1 Answer

0 votes

Try something like this, but update the code to follow the cell vertex numbering used in 3D, the example below is for 2D. You can find the numbering in the UFC manual/FEniCS book (I believe ...), or you can just inspect a random cell in the mesh and figure it out on your own.

import dolfin as df

def get_triangulation(mesh):
    """
    Get the mesh triangulation in a format suitable for matplotlib (2D)
    """
    coords = mesh.coordinates()
    triangles = []
    for cell in df.cells(mesh):
        cell_vertices = cell.entities(0)
        triangles.append(cell_vertices)
    return coords, triangles
answered Feb 9, 2016 by Tormod Landet FEniCS User (5,130 points)

It was brought to my attention that you may be looking for one continuous line made up of line segments that together form the entire mesh. If you do not want to include each line segment more than once then this is Euler's classical bridges of Köningsberg problem, which very likely does not have a solution on your mesh (your mesh is probably not Eulerian).

If you can visit each edge more than once then you have a variant of the Chinese postman problem.

On the practical side, you probably will want to plot each edge separately. This can be slow in some plotting libraries where the plotting routines are O(n) where n is the number of distinct lines plotted. Matplotlib has a library of collection that are used to speed things up. Maybe plotly has something similar, you can at least use the network graph features to get this working if plotting one and one line is too slow.

...