简体   繁体   中英

Finding Edges and Vertices Within Heightmap Array

Lets say I have generated a heightmap array, which for simplicity's sake will only have three height values within it: 0 , 1 , and 2 . For example, it may look like this:

000000111111110000000000000000000000000000000000
001111111111111111000000000001111111111111000000
000011111111111111100000000000000011122111110000
000000000111111100000000000000000111221110000000
000000000000000000000000000001111122222111111100
000000000000000000000000111111111111111111000000
000000000000000000000111111111100000000000001111
000000000001111111111110000000000000000001111111
000000001111222222211100000000000000000000001110
000000000011112221100000000000000011111000000000
000000000000111111100000000000001111111110000000
000000001111111110000000000000000011111000000000
000000000000000000000000000000000000000000000000

What I'm trying to do is find the 'vertices' in this heightmap, and to output them in a logical order (so I can draw a line going to each consecutive point that will eventually trace the outline of the shape). For example, for the first 'group' of 1 s in the top right corner:

000000111111110000000           .1------2       
001111111111111111000        8-'         `--3   
000011111111111111100  -->    `7---.        .4  
000000000111111100000               6-----5'    
000000000000000000000                           

The numbers in the second diagram are the coordinates I need to find (in the correct order), and the dots and dashes are the lines I would trace from each point to get the outline of the shape.

Is there any sort of algorithm I can use to find these vertices? If not, what is the most efficient method of finding them?

What I am currently doing is using recursion to find each of the 'islands' or 'groups' of numbers, and then taking all the outer points of this shape as the vertices. However, my method is quite slow and the vertices are not in the correct order.

Thank you for your help and I hope this all makes sense.

Edit: I realize from the comments that using vertices would make me lose some areas of the shape. I think that instead of vertices then what I need to find is all the points on the edge of the shape, but in the correct order. So my example should be:

000000111111110000000             12345678       
001111111111111111000         17          9,10   
000011111111111111100  -->     16,15         11  
000000000111111100000               14,13,12     
000000000000000000000                            

Sorry for the confusion.

(Answer to the edited question:) A certain form of graph traversal should do for this, I will only give a very high-level description of it. Start at the highest level of the height map, and for any island, start at some interior point. Go north until you reach the edge of the island or the edge of the map. In a clockwise (or counter-clockwise, whichever it is you need) fashion, explore only those cells that can belong to the edge of the island.

For lower levels, start from cells neighboring the higher-level islands and do the same.


(Answer to question before clarifying edit:) Sounds to me like you want the convex hull of every "island". Find contiguous groups (your "islands") with graph traversal (I like BFS best, but tastes differ; consider two cell to belong to the same island whenever they contain the same value and are adjacent), then apply some convex hull algorithm .

You can try alpha shapes. Alpha shapes is defined as a delaunay triangulation without edges exceeding alpha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM