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

Nesting result in a circle but not a rectangle #28

Open
jindili opened this issue Aug 3, 2023 · 8 comments
Open

Nesting result in a circle but not a rectangle #28

jindili opened this issue Aug 3, 2023 · 8 comments

Comments

@jindili
Copy link

jindili commented Aug 3, 2023

I try to Nesting some random Items (100 rectangle of random size), and get this result:
image

But supposed to get something like this:
image

I use pynest2d.pyd from UltiMaker Cura 5.4.0.

main part of code related:

from pynest2d import *

volume = Box(2000, 2000)

config = NfpConfig()
config.accuracy = 1.0

# config.alignment = NfpConfig.Alignment.BOTTOM_LEFT
# config.alignment = NfpConfig.Alignment.BOTTOM_RIGHT
# config.alignment = NfpConfig.Alignment.CENTER
# config.alignment = NfpConfig.Alignment.DONT_ALIGN
config.alignment = NfpConfig.Alignment.TOP_LEFT
# config.alignment = NfpConfig.Alignment.TOP_RIGHT

# config.starting_point = NfpConfig.Alignment.BOTTOM_LEFT
# config.starting_point = NfpConfig.Alignment.BOTTOM_RIGHT
# config.starting_point = NfpConfig.Alignment.CENTER
# config.starting_point = NfpConfig.Alignment.DONT_ALIGN
config.starting_point = NfpConfig.Alignment.TOP_LEFT
# config.starting_point = NfpConfig.Alignment.TOP_RIGHT

spacing = 1

num_bins = nest(List_Items, volume, spacing, config)

Had tried all options of alignment, starting_point, get some strange result:
image

@jindili
Copy link
Author

jindili commented Aug 4, 2023

After search through Google and Github, I try Ask Claude AI
https://claude.ai/
Q: use libnest2d to nesting 100 random rectrangle, the nesting result is a circle, but supposed to nest in a rectangle shape, help me debug

A:
There are a few things that could cause the nesting result to be a circle rather than a rectangle when using libnest2d to nest 100 random rectangles:

  • The bounding box is not being set correctly - libnest2d will nest shapes within the bounding box provided. Make sure you are setting an appropriate rectangular bounding box, not a circular one.

  • Shapes are being rotated - libnest2d will rotate shapes to find the optimal nesting. If you don't want rotation, disable it with the no_rotation option.

  • Order of input shapes - Try changing the order of the input rectangle list to get different nesting output. Some orders may lend better to a circular result.

  • Not enough space for proper nesting - If the bounding box is too small to fit all the rectangles efficiently, it may result in a circular-looking layout. Try increasing the bounding box size.

  • Bad initial placement - The initial placement seed can affect nesting. Try changing the config place_seeds option.

  • Sub-optimal packing algorithm - Libnest2d has different packing algorithms. Test different ones like bottom-left or contact-point to see if it improves the rectilinear nesting.

To properly debug, I would start by printing out the bounding box coordinates and enabling visual output to see exactly what is happening. Also try nesting just a few rectangles first to check the baseline behavior. With some targeted debugging and experimentation, you should be able to get the desired rectangular nesting.

@jindili
Copy link
Author

jindili commented Aug 4, 2023

Trying PrusaSlicer-2.6.0
seems arrange Items in circle is the default behavior
image

@jindili
Copy link
Author

jindili commented Aug 4, 2023

and it's the default arrange behavior of Cura 5.4
image

@jindili
Copy link
Author

jindili commented Aug 4, 2023

Come across a similar question on libnest2d:

Strange "starting_point" behavior in NfpPlacer · Issue #52 · tamasmeszaros/libnest2d
tamasmeszaros/libnest2d#52

@jindili
Copy link
Author

jindili commented Aug 4, 2023

try nest() with BottomLeft Placer

config_rect = BottomLeftConfig()
config_rect.allow_rotations = False
num_bins = nest(List_Items, volume, spacing, config_rect)

get this error

TypeError: nest(): argument 4 has unexpected type 'BottomLeftConfig'

help(nest)

Help on built-in function nest in module pynest2d:
nest(...)
    Arrange a number of items in bins, packing them closely with the goal to
    minimise the number of bins necessary to pack all given items.
    
    This function uses the FirstFit selector and NfpPPlacer in its algorithms,
    and Clipper as geometry back-end.
    
    The result of the packing is stored in the transformation of the input
    items. You can get the transformed items by calling transformedShape() on
    each of the items, or by looking at their rotation and translation. Items
    may also be placed on alternative bins if the first bin is not big enough to
    fit all items.
    :param items: An iterable of Item instances which are the items that need to
    be placed in the bins.
    :param bin: The shape in which to place these items.
    :param distance: (Default 1.) The distance to hold between adjacent items in
    the final pack result. Items will not get placed closer than this distance.
    Must be at least 1 unit.
    :param config: An NfpConfig instance that contains configuration options to
    specialize how objects get placed. Defaults will be used if this is not
    provided.
    :return: The number of bins necessary to fit all items.

:param config: An NfpConfig instance that contains configuration options

seems only NfpConfig is supported.

@jindili
Copy link
Author

jindili commented Aug 4, 2023

look into source code of libnest2d, seems BottomLeftPlacer is implemented as well.

libnest2d/src/libnest2d.cpp at 359d71fb4e5a951c17228f4be75b432187ca4993 · Ultimaker/libnest2d
https://github.com/Ultimaker/libnest2d/blob/359d71fb4e5a951c17228f4be75b432187ca4993/src/libnest2d.cpp

image

@jindili
Copy link
Author

jindili commented Aug 4, 2023

[Doubt] - Using BottomLeftConfig · Issue #25 · Ultimaker/pynest2d
#25

@jindili
Copy link
Author

jindili commented Aug 4, 2023

So to use BottomLeftPlacer, seems I have to change some code and build a new pynest2d.pyd

image

pynest2d/python/pynest2d.sip at main · Ultimaker/pynest2d
https://github.com/Ultimaker/pynest2d/blob/main/python/pynest2d.sip

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

No branches or pull requests

1 participant