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

How to access Polygon.vertices() from python?

+1 vote

I'd like to access the vertex list of a mshr Polygon but I can't work out how to do so. My first attempt:

from dolfin import *
from mshr import *

poly = Polygon([Point(0,0), Point(1,0), Point(1,1), Point(0,0)])
[v for v in poly.vertices()]

results in

TypeError: 'SwigPyObject' object is not iterable

Attempting "poly.vertices()[0]" also fails:

TypeError: 'SwigPyObject' object has no attribute '__getitem__'

The only reasonable thing I can find on the object returned by vertices() is a next() function, but it just returns "None".

How can I get the vertices out of a polygon?

asked Apr 1, 2017 by steve FEniCS Novice (340 points)

1 Answer

+1 vote
 
Best answer

Hi steve, the polygon object is not a mesh, so it does not have what you need. For this, you might first create a mesh with

resolution = 10
mesh = generate_mesh(poly, resolution)
vertices = mesh.coordinates()

Best regards!

answered Apr 11, 2017 by nabarnaf FEniCS User (2,940 points)
selected Apr 15, 2017 by steve

Is there no way to get the vertices which define the Polygon from mshr prior to mesh generation?

None that I know of, and as steve mentioned the methods that seems reasonable give only nulls. Maybe if he gives some details on the problem we could find another way.

Best regs

Thanks for confirming my fears :-) In the end, however, I solved my problem by avoiding it!

For my application, I have boundaries generated by another system that I am converting into fenics to solve the Poisson equation (an electrostatics problem). The reason I want the vertices is to set up the boundary conditions. In the end, I just went back to the original input polylines for the boundary vertices.

Incidentally, my first attempt was to do as you suggest -- generate a mesh and obtain the boundaries from it. For inner "hole" boundaries, this works fine since the part that is meshed is outside the problem domain: I was able to essentially do a set intersection to find the (common) boundary edges. But this doesn't work well for the outer domain boundary -- the "rough mesh for boundary creation" also contains vertices interior to the boundary and gets in the way of my naive approach.

...