Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disallowed areas #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Ghostkeeper
Copy link

Hi! We've made some changes to this library to fit the needs of Cura. These changes are to fix a particular issue we've had with using the library in Cura because we seem to have a different way of dealing with the prime tower and bed clips than the application this library was originally designed for (PrusaSlicer). See also the pull request being reviewed by my colleagues: Ultimaker#1

This change adds a new concept to libnest2d: Disallowed areas.

There are several places in the bin where no objects can be placed. This includes places where objects have been placed before (pre-loaded items) but also some places where there are no objects at all, but objects that should still be avoided. In Cura's case this includes the prime tower and bed clips and a few other printer-defined places. In libnest2d these are currently all "fixed" items.
The problem we faced: We want objects to group together, but we don't want to group them together with disallowed areas that are not actual printed objects. Previously placed objects and disallowed areas need to be marked as "fixed" in order to prevent the nester from moving them. However newly placed items are then still packed close together with all fixed items. In our case, we want to pack them close together with normal fixed objects, but not with non-objects such as the bed clips.

This change allows the consumer of the library to set some items to be disallowed areas. Those items are fixed, but other items won't try to pack closely together with them. In effect, just the first item doesn't need to get packed closely together since other items will then pack with the first item.

Before this change, items get placed down by the side which is a disallowed area due to the difference between nozzle positions in this case:
image
After this change, items get placed down in the middle as the NfpPConfig::alignment indicates:
image

I don't know if this is something you'd like to add upstream as well. Maybe it's not useful for you or not considered in scope of this library. Even if it's not useful, take it as a sign of goodwill.

Disallowed areas have slightly different behaviour from fixed items: Other items won't get packed closely around them. Implementation of that pending.

Contributes to issue CURA-7754.
If you set the bin to -1 or set the item to be a simple fixed item afterwards, it'll no longer be a disallowed area.

Contributes to issue CURA-7754.
…owed

We shouldn't align items to disallowed areas. So place them in the starting position according to the alignment property.

Lot of work to investigate. But very little code changes!

Contributes to issue CURA-7754.
@Ghostkeeper
Copy link
Author

It seems the tests failed because the CI server couldn't download Clipper:

Performing download step (git clone) for 'rp_Clipper'
  -- Avoiding repeated git clone, stamp file is up to date: 'C:/projects/libnest2d/build-x64/rp_packages_build/+Clipper/rp_Clipper-prefix/src/rp_Clipper-stamp/rp_Clipper-gitclone-lastrun.txt'
  Performing update step for 'rp_Clipper'
  fatal: unable to access 'https://github.com/tamasmeszaros/libpolyclipping.git/': Could not resolve host: github.com
  CMake Error at C:/projects/libnest2d/build-x64/rp_packages_build/+Clipper/rp_Clipper-prefix/tmp/rp_Clipper-gitupdate.cmake:55 (message):
    Failed to fetch repository
    'https://github.com/tamasmeszaros/libpolyclipping.git'

This has nothing to do with these code changes. May just be a hiccup in the CI system.

@Ghostkeeper
Copy link
Author

Should mention that we've also made Python bindings for this library here: https://github.com/Ultimaker/pynest2d/

These Python bindings have some documentation on the public functions that goes beyond the documentation of libnest2d itself. The bindings are a bit more extensive than the previously existing bindings of nest2d.

@tamasmeszaros
Copy link
Owner

Many thanks! I will need to find some time to deal with the PR and also to review the tests, they might not be up to date.

@tamasmeszaros
Copy link
Owner

tamasmeszaros commented Nov 27, 2020

Hello @Ghostkeeper!

So I've looked closely and I've found some problems:

What if the first object being arranged collides with some disallowed area in the initial position. This is a deeper problem. The initial position is already a bit of an inconsistent feature if you take into account an irregular bin. Naturally, the disallowed areas would be the holes in a bin. You can see the issue in Cura as well if you enlarge some object enough to touch a disallowed area if placed in the center, the arrange will ignore the collision. The nesting could still snap to a disallowed area if it was not somewhere at the edge of the bin but closer to the center.

Don't get me wrong, this is a reasonable workaround especially in Cura's use-case. It's just not robust for all the cases.

I think if you where to center the first object manually and then add that and all the disallowed areas as fixed objects, it would have the same effect.

@Ghostkeeper
Copy link
Author

I think if you where to center the first object manually and then add that and all the disallowed areas as fixed objects, it would have the same effect.

This is impossible from outside the library though (as far as I could find) since it's unknown which object is placed first.

@tamasmeszaros
Copy link
Owner

tamasmeszaros commented Dec 1, 2020

It only depends on the selection algorithm, which is the one called "first fit" by default. It sorts items by the area in descending order. So in case you use the default selection algorithm, you can just center the biggest item and mark it as fixed.

There is a catch, that if you want to have multiple merged piles, then you really can't no in advance, which items will be placed first on any of the bins with ID > 0.

Let's just say that the library has many missing parts, one of which is the support for irregular bins. I will try to add that as soon as I can, but I can't make any time estimations on that.

@tamasmeszaros
Copy link
Owner

BTW, you can influence the selection order with the priority() setter and getter method of items. The implemented first-fit selection algorithm will respect this property. It could be useful in the future in Cura or PrusaSlicer, for example to put taller objects nearer to the center of the print bed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants