Detect rooms/enclosed spaces from List of Boxes

68 Views Asked by At

Im using three.js to develop a website in which the user can place boxes/walls. After placing a wall im trying to detect all rooms that are formed by the collection of boxes. For this im assuming all of this to be 2D.

enter image description here

This is an example of how the boxes could be places. Im working with the bounding boxes of the actual objects. The points where boxes meat each other are equal.

My goal is to get all the red and purple points in the image to later draw a polygon which represents the outline of the room. The amount of boxes and formed rooms is not limited in any way.

I already tried a few things. But im only getting to a point where around 60-70% of cases work.

My current approach is creating a shape a little bigger than the total bounds. And using the bounding box points of every box to create a hole in that shape. After that im creating a mesh from this shape and get all polygons and group polygons that share 2 coordinates until i have in my above example 2 groups.

This works for a lot of cases. But the triangulation doesn't seem to always produce a valid result.

How could i modify my approach to work better or what is a better way to achieve this?

1

There are 1 best solutions below

0
Nail Sharipov On

In other words you wish to find all holes(rooms) in the polygon. If that the easiest way to do this is to use any of existing poly-bool libraries.

You will need to union all holes.