Good afternoon.
It is necessary to place the models when loading next to the already loaded models. The first model should be loaded in the center of the plane, and the rest should be located next to it.
Also, they should not overlap each other.
Can someone share an example of how this can be implemented?
Or thoughts in which direction to move
I don’t have an example, and I don’t have the time to write some. Generally, my suggestion would be the following:
when placing a model, take its bounding box and try to fit it – this will make computations much easier, at the cost of leaving some gaps (I hope you do not need compact and perfect arrangement)
having this done, the problem is reduced to placing rectangles in a plane – there are several algorithms for this, collectively they are called “2D nesting algorithms”
option 1: find some existing JS library that implements such algorithm
option 2: implement by yourself one of the 2D nesting algorithms (this might be hard to do)
option 3: implement some naive form of nesting, which will be fine if you do not have too many objects. One naive approach is this: split the plane into squares. Each object occupies several full squares (i.e. no objects share the same square). When you place an object, find a place with enough free squares. After placing the object mark its squares as occupied.
option 4: put all objects in groups depending on their size. Arrange objects from each group into a grid adjusted to their size.
option 5: use physics engine, put the models in a sparse grid, make horizontal gravitation force, and the objects will pack “automatically”
Thanks, bro. I’ve sketched out a very simple option for loading stl models with subsequent positioning. Perhaps you will have the time and desire to watch.
You won’t need anything except the nest2DArrange function here.
If I load models of the same size, then there are no problems, but if they are different, then overlap begins. An example is shown in the screenshot.
There is some kind of problem with loading specifically into codesandbox, the first model is loaded only after loading the second, and the second after loading the third, and so on in order.
I think when you say the box is w x h, potpack thinks it means min.xyz are all 0. if they are negative, this fails. so you should call geometry.translate to fix it