How to identify each polyshape independently.

1 view (last 30 days)
The scope is to identify each grain (polyshape) independently in order to find the centroids of each one of them (centroid-polyshape function). I have all the vertices and connectivity matrix between these vertices but I need to arrange these vertices by grain so I can apply the centroid function (polyshape)
polyin = polyshape({x1,x2},{y1,y2});
[x,y] = centroid(polyin);

Answers (2)

Matt J
Matt J on 1 Jun 2022
Edited: Matt J on 1 Jun 2022
If you download spatialgraph2D,
you can use its polyshape() method to obtain a polyshape vector from the graph, e.g.,
s = [1 1 1 2 3 3 4 4 5 6 7 6 8 9 10 10 12 12 13 14 15 16 17 17 18 19 20 21 20 25];
t = [2 8 18 3 4 23 5 21 6 7 8 11 9 10 11 12 14 13 15 18 16 17 18 25 19 20 1 22 24 26];
G = graph(s,t);
x = [0.5 0 0 0 0.5 1 1.5 2 3 3 3 5.5 6 4 6 6 4 3 2 0.5 -1 -2 -1 1.5 4.5 4.5];
y = [0 0.5 1 1.5 2 2 1.5 1 1 1.5 2 1 0.5 0.5 0 -1 -1 -0.5 -1 -1 1 0.5 0.5 -0.5 -0.5 0];
obj=spatialgraph2D(G,x,y);
obj.plot
warning off
pgon=obj.polyshape %Get the polyshape vector
pgon =
1×5 polyshape array with properties: Vertices NumRegions NumHoles
figure;
plot(obj); hold on
plot(pgon);hold off

William Rose
William Rose on 1 Jun 2022
Please share the matrix of vertex coordinates and the connectivity matrix that accompany the figure you posted. Does every vertex have only three line segments meeting at it? SOme vertices appear to have four lines, but maybe this is really two vertices with three lines each, which are very close together.
We want a list of the vertices of each polygon that does not include any smaller polygons. We can do this by proceeding clockwise (or counter CW) around each polygon, and whenever there is a choice about which path to follow, follow the path that makes the "tightest turn", i.e. which results in the smallest angle between the segments. We need to make sure we do not overlook any polygons. If there are only three line segments per polygon, then each vertex participates in two or three polygons, and this bit of information can help us make sure we have not missed any polygons.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by