Code Monkey home page Code Monkey logo

svgnest's Introduction

SVGNest

SVGNest: A browser-based vector nesting tool.

Demo: http://svgnest.com

(requires SVG and webworker support). Mobile warning: running the demo is CPU intensive.

references (PDF):

What is "nesting"?

Given a square piece of material and some letters to be laser-cut:

letter nesting

We want to pack all the letters into the square, using as little material as possible. If a single square is not enough, we also want to minimize the number of squares used.

In the CNC world this is called "nesting", and software that does this is typically targeted at industrial customers and very expensive.

SVGnest is a free and open-source alternative that solves this problem with the orbital approach outlined in [E.K. Burke et al. 2006], using a genetic algorithm for global optimization. It works for arbitrary containers and concave edge cases, and performs on-par with existing commercial software.

non-rectangular shapes

It also features part-in-part support, for placing parts in the holes of other parts.

non-rectangular shapes

Usage

Make sure all parts have been converted to outlines, and that no outlines overlap. Upload the SVG file and select one of the outlines to be used as the bin.

All other outlines are automatically processed as parts for nesting.

Outline of algorithm

While good heuristics exist for the rectangular bin packing problem, in the real world we are concerned with irregular shapes.

The strategy is made of two parts:

  • the placement strategy (ie. how do I insert each part into a bin?)
  • and the optimization strategy (ie. what's the best order of insertions?)

Placing the part

The key concept here is the "No Fit Polygon".

Given polygons A and B, we want to "orbit" B around A such that they always touch but do not intersect.

No Fit Polygon example

The resulting orbit is the NFP. The NFP contains all possible placements of B that touches the previously placed parts. We can then choose a point on the NFP as the placement position using some heuristics.

Similarly we can construct an "Inner Fit Polygon" for the part and the bin. This is the same as the NFP, except the orbiting polygon is inside the stationary one.

When two or more parts have already been placed, we can take the union of the NFPs of the previously placed parts.

No Fit Polygon example

This means that we need to compute O(nlogn) NFPs to complete the first packing. While there are ways to mitigate this, we take the brute-force approach which has good properties for the optimization algo.

Optimization

Now that we can place the parts, we need to optimize the insertion order. Here's an example of a bad insertion order:

Bad insertion order

If the large "C" is placed last, the concave space inside it won't be utilized because all the parts that could have filled it have already been placed.

To solve this, we use the "first-fit-decreasing" heuristic. Larger parts are placed first, and smaller parts last. This is quite intuitive, as the smaller parts tend to act as "sand" to fill the gaps left by the larger parts.

Good insertion order

While this strategy gives us a good start, we want to explore more of the solution space. We could simply randomize the insertion order, but we can probably do better with a genetic algorithm. (If you don't know what a GA is, this article is a very approachable read)

Evaluating fitness

In our GA the insertion order and the rotation of the parts form the gene. The fitness function follows these rules:

  1. Minimize the number of unplaceable parts (parts that cannot fit any bin due to its rotation)
  2. Minimize the number of bins used
  3. Minimize the width of all placed parts

The third one is rather arbitrary, as we can also optimize for rectangular bounds or a minimal concave hull. In real-world use the material to be cut tends to be rectangular, and those options tend to result in long slivers of un-used material.

Because small mutations in the gene cause potentially large changes in overall fitness, the individuals of the population can be very similar. By caching NFPs new individuals can be evaluated very quickly.

Performance

SVGnest comparison

Performs similarly to commercial software, after both have run for about 5 minutes.

Configuration parameters

  • Space between parts: Minimum space between parts (eg. for laser kerf, CNC offset etc.)
  • Curve tolerance: The maximum error allowed for linear approximations of Bezier paths and arcs, in SVG units or "pixels". Decrease this value if curved parts appear to slightly overlap.
  • Part rotations: The possible number of rotations to evaluate for each part. eg. 4 for only the cardinal directions. Larger values may improve results, but will be slower to converge.
  • GA population: The population size for the Genetic Algorithm
  • GA mutation rate: The probability of mutation for each gene or part placement. Values from 1-50
  • Part in part: When enabled, places parts in the holes of other parts. This is off by default as it can be resource intensive
  • Explore concave areas: When enabled, solves the concave edge case at a cost of some performance and placement robustness:

Concave flag example

To-do

  • Recursive placement (putting parts in holes of other parts)
  • Customize fitness function (gravity direction, etc)
  • kill worker threads when stop button is clicked
  • fix certain edge cases in NFP generation

svgnest's People

Contributors

bmtm avatar dergenaue avatar dustmason avatar gaetancollaud avatar jack000 avatar johnrees avatar jonnor avatar olilej avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svgnest's Issues

Preserve fill and stroke color

I was trying to use this to process some Ponoko files, which have colored strokes and fills to represent etched/cut line intensities and etched area intensities respectively. Unfortunately, SVGNest removes fill and stroke colors after processing.

I also had some issues with Deepnest.io, but both tools seem great; just not for my use case.

Option to save every nested Sheet as separate SVG file.

Hi, I just discovered this project and I have some feature requests.

Will be practical to be able to save every nested Sheet as a separate SVG file, instead to save only one file containing all the nested sheets like it is right now. I tested the online demo. Maybe an option when are multiple sheets to download all SVG files as a ZIP file will do the trick.

SVG from Kabeja

Hi.

I am trying to load shapes generated with Kabeja. Unfortunately many of them does not appear correct. If I first simplify in Inkscape it works. However simplify is not good, further Id like if it could load for nest directly.

I have attached a few simple examples og SVG. the rendering problem has nothing to do with SVGNEST.

testSVG.zip

Doesn't ever stop

It doesn't seem to ever stop. I've had this running all day! 😄

Every now and then it will reshuffle, but it's always been at 66%. (This is using the demo SVG)

screen shot 2016-01-22 at 17 33 45

i tested your software

it is great, i enjoyed your software. thanks for sharing.i think how we can make better in optimization. any way thanks very much

Nesting takes too long and nothing happens

Hello,

I make drawings on CorelDraw. I save them in SVG, open them in inkscape, ungroup everything, convert to path and save again.

Even so SVGnest does not generate a solution in a reasonable time.

Here the last SVG file I tried: https://goo.gl/86TEHx

In commercial software the nesting was conclued in less than 10 seconds.
Printscreen: https://goo.gl/qxHMMg

SVGnest is running now for more than 20 minutes and nothing happen. Windows Task Manager show me 99% CPU usage.
Printscreen: https://goo.gl/15L1eV

I'm trying with these configurations options: https://goo.gl/UVRZew

Paths Seem to be broken apart.

I'm trying to pack my shapes for a bookshelf using SVGNest. I've been successful using it in the past for other projects. But it seems to think my path is a bunch of smaller paths for the curved shapes.

My workflow was Fusion 360 -> DXF -> Inkscape.

Here is a pastebin with my SVG: Bookmark.svg

Slightly deformed shapes

After nesting few elements (files attached) 2 shapes "S" and "W" letter was slightly different from original.
Why this happened?

The second thing is, space in svgnest configuration was set to 10 (or 12 don't remember) - svg was was set to mm in inkscape. And after nesting the space is around 3,5mm.

clipboard01

zale.zip

lasercut optimisation

Great work !.
I suggest an optimisation which could be added as an option.

If two edges are coincident and one lies enirely within the length of the other, then eliminate the shorter edge.

This will optimise out the duplication of cuts in a way that does not chop an edge into a number of shorter cuts - which frequently generates bumps in a physically cut result. Further types of reuction could be looked at as well but the direction and order of each cut is an ordering problem within the SVG and a tricky one to calculate.

Download SVG Error!

Not able to download the final nested SVG. I tried to download svg after the demo nesting is finished. Got the below error on opening it.

This page contains the following errors:

error on line 1 at column 617: Attribute version redefined
Below is a rendering of the page up to the first error.

Chrome V54

incorrect nesting

Hello,

when nesting attached SVG file with default-settings the"wrong placed " elements will not be recognized.

even increasing space between parts and curve tolerance shows "wrong placed" elements.

any ideas ( is the input svg not correct )?

thx in advance

Francois

nest_test3.svg.zip

svgnesting_fail

quick questions

for nesting for vinyl cutters / printers do options exist to?

nest within grid? where nested svgs are nested only horizontal or vertical?

keep svgs as graphics. assuming we are trying to nest svg of git hub logo, apple logo, ubuntu logo, and facebook logo, and each is made from multiple paths and groups can nesting be set to keep artwork the same ? in other words nest using the outer bbox or size?

thanks!!!

fill holes

Hello,

Is that possible that the code fills the holes, for example an "O" characters inside?
I have some ideas for acceleration if you like it. For example using angles that pre-caclulated on polygons etc, to obtain the likely possible best rotation to fit to the cutout etc.

Can't solve trivial problem.

I'm not sure if I drew it correctly, but this input:

image

Produces this output after 700 iterations:

image

Forcing rotations to 1 thus decreasing incorrect combinations, and you can see there is one triangle left to be solved yet.

I attach here the svg zipped:

test1.svg.zip

Locked pieces

There are times in a job where there are pieces that are required to be in certain locations - due to grain or wood imperfections, etc. After these pieces are located in their manually approved locations it would be desirable to nest remaining pieces. Sometimes it is as simple as the 'primary' pieces are paying for the job and need to be in the best parts of the material, and then the remainder of the material can be used to nest pieces of 'secondary' value.

Note: this is NOT a duplicate request to the 'piece orientation' request - but obviously I'd mark the primary pieces by location and orientation.

Implementation thought: When the initial nesting container is selected and the process triggered, anything already within the container boundaries is treated as locked both positionally and with regard to orientation. Nesting resumes from there?

Error when running demo in Chrome OSX

Clicking on the Demo button using Chrome version 49.0.2623.0 canary (64-bit) under OSX only gives an error in the bottom of the app window.

TypeError: Cannot read property 'length' of undefined

Dxf nesting

Mm i dont know how its hard, but you must add DXF nestin mean cad nestin. This is to be good for me :)

Skip Nesting on Defect Regions

Hi Jack

First of all i thank you for the wonderful project. Being in material optimization job, I know how difficult to work with nesting. This SVGnest does impressive job.

I request you to consider this enhancement in higher priority - "Skipping the nesting in the defect regions of sheet/bin". It is a must need feature when you think about automated nesting coz not every sheet/bin will be a defect free solids.

I guess as discussed in an another thread, "Treat pieces inside the bin as locked" will be a better solution.

Attached samples for your reference
samplenest

Thanks
Nadum Sing

SVG nest and orientations (0 or 180) or (90 or 270)

Example.zip
Interesting stuff this SVGnest.

I also commented in #15. That was about grain-direction in wood. I repeat my comments here:

In metal sheet industry the requirements are somewhat more complex compared to the wood-grain issue.
Metal sheet proces is as follows: Sheet metal is cut (lasered or punched) Next step is bending on a press brake. For the press-brake it is important that the grain (molecular structure) is in the same direction for all identical parts in the nest. That is a prerequisition to have a consistent bend during the complete batch. In most cases it does not matter if grain is along or perpendicular to bending axis.
Now back to nesting and rotations: Commercial nesting software often has an option to select allowable rotations of a part. Most of the time this is set per part to (0 or 180) or to (90 or 270). This will ensure grain directions for identical parts to be the same. However it does not always produce the best material utilisation. It can be the case that a part (which is set to 0 or 180) will fit better if it were set to (90 or 270) and would result in a better utilisation.
I've not seen commercial nesting software that does provide the option per part: try (0 or 180) and try (90 or 270)
Sadly SVGnest does not provide this option either.
However, having taken a look at Jacks code there is a routine that places parts with the help of a genetic algorithm (GA). I think it is in this section that a code change should be made to accomodate the (0 or 180) or (90 or 270) rules. However I'm totally not into GA. Is there anyone who can help to get this done?

I have included a sample. Start with the "example.svg". Set part distance:10 and enable "explore concave areas". This wil result in a nest like "result.svg" (You might wonder where the 3 indentations in the bin are for: These are the reserved areas where the lasercutter places its clamps to move the metal sheet under the laserbeam) The goal would be to let have the same colored parts the same orientation.

Hopefully someone will be helpfull.

Enhancement suggestion: Required orientation

I would like to suggest allowing some parts to be marked to have a 'grain' direction, as well as the stock piece.

This would allow cutting, say plywood, so the grain is going in the 'correct' direction for the part.

This is important in woodworking to keep the grain right, but also in patterned stock of any kind (wall paper for example).

Thank you for the consideration.

Server-side Nesting?

I'd like to take a shot at helping to abstract the algorithmic code from the DOM-dependent code so nests can be ran on a server /lambdas. Would also help keep things modular.

Any guidance would be appreciated, what would be your interest level?

Separate the packing algorithm out from the UI and SVG dependency so it can be used independently

The geometry part of this code depends on plain polygon objects which are lists of {x: y:} points and some worker threads that call the clipper library and a genetic algorithm.

The other half of this code decodes the SVG shapes, organizes them into inner and outer contours, and then linearizes the edges.

It would be very useful if this system were broken into two completely disconnected parts with a simple API between them (that just took the polygons and returned the transformations), because then I could use it for packing other shapes that might be generated within my own javascript UI.

Multi page placement

Hi,
What if I have more pieces that I can fit onto one sheet? Could it place the pieces onto multiple sheets?

Type error

When I uploaded the svg file:

An error is occurred.
TypeError: unable to get property "getAttribute" of undefined or null reference.

Has the file any error?

Will you give any samples in the demo project?

DOMException: Failed to construct 'Worker': Script at '

i can run demo on the web site http://svgnest.com/.
however it reports errors when i download the file and run on the chrome version 60.
the error is shown in follow:
Any guidance would be appreciated. thank you

image

Uncaught DOMException: Failed to construct 'Worker': Script at 'file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/util/eval.js' cannot be accessed from origin 'null'.
at Parallel._spawnWorker (file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/util/parallel.js:167:12)
at Parallel._spawnMapWorker (file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/util/parallel.js:217:24)
at Parallel.p._spawnMapWorker (file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/svgnest.js:351:47)
at file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/util/parallel.js:265:10
at Operation.then (file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/util/parallel.js:51:5)
at Parallel.map (file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/util/parallel.js:263:18)
at SvgNest.launchWorkers (file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/svgnest.js:354:6)
at file:///C:/Users/Administrator/Desktop/fdm%20scheduling/SVGnest-master/SVGnest-master/svgnest.js:235:25
Parallel._spawnWorker @ parallel.js:167
Parallel._spawnMapWorker @ parallel.js:217
p._spawnMapWorker @ svgnest.js:351
(anonymous) @ parallel.js:265
Operation.then @ parallel.js:51
Parallel.map @ parallel.js:263
SvgNest.launchWorkers @ svgnest.js:354
(anonymous) @ svgnest.js:235

Labels for svg 'pieces'

If cutting some items, even if they are drawn out, some might be very similar. It would be nice to attach a label to each piece (possibly a number, with an optional text field - think 20 to 30 characters or so).

This 'legend' could be separate from the parts, but would need to be associated with each part both before and after optimization, so the parts can be identified for use later.

Sorry if this is to wordy. This should be a long term enhancement, not something I expect could be quickly done.

Thanks for your consideration.

Use lowest position of centre of gravity to position the elements

line 185 placementworker.js

            // choose placement that results in the smallest bounding box
    // could use convex hull instead, but it can create oddly shaped nests (triangles or long slivers) which are not optimal for real-world use
    // todo: generalize gravity direction

Consider placing according to the lowest position of centre of gravity, as suggested in this paper:

"Algorithm for 2D irregular-shaped nesting problem based on the NFP algorithm and lowest-gravity-center principle"
LIU Hu-yao, HE Yuan-jun [2005]

If you translated each of your polygons so that its centre of gravity was at the origin (and the rotations were around this point), then this final scan of displaced points (allpoints and shiftvector stuff) would be unnecessary as you would merely need to find the lowest point of finalNfp.

CSV file nesting?

As I see this nesting program is able to nest only imported geometry as SVG shapes.

It will be very useful to be able to nest geometry from CSV files where the elements are defined as dimension values, like in the bellow example:


500, 200, 5
230, 120, 3
120, 320, 2


Where the first column is always the grain orientation, and the last one is the quantity of parts/shape to be nested.

"Performs similarly to commercial software"... which one?

There's probably more than one application that can do packing, which one are you benchmarking against? (because this is a bit like saying "our toothpaste is 30% better than the next leadin brand"... there's no true information there =D)

Using with QtWebKit

Hi, I try to load SVGNest in a QWebView and have an issue :

TypeError: undefined is not an object (evaluating 'element.getAttribute')

Have you any solution ?

Thanks.

Nesting messed up when input with the output of the demo

I've tried to use the output of the demo as an input, because was having difficulty in using my test profiles. I noticed that when demo has been run with 0 spacing, the subsequent nesting performed on it gives some white filled shapes placed outside the bin, and many shapes wildly overlap.
If the demo is run with 0.5 spacing, using this output gives a fine result.
cattura

Troubles about creating hulls

Hello,
I've translated the functions in geometryutil.js into C# for a course project. The problem is that sometimes the polygonHull function may lose several parts in the process. I've been captured that overlapping points with the one on the upper half of A and the other is in the lower cause the problem. Still, there are lots of problems cases I cannot handle.
I wonder if there is another feasible and reliable mean to create the hull of the parts?
thx in advance

Can i add fixed polygons to the base/bin?

Hey,

I've been testing your software and it's amazing. Congratulations!

I have a question regarding de base/bin where the other elements are placed. Is there any way to add a polygon which has to be fixed and not traslated/rotated by the algorithm and will have no pieces inside it placed once the algoritm finishes?

I mean for example a base which is some kind of material which has stains and we dont want to place pieces onto.

I've been thinking if i can nest a svg inside the svg where all the other pieces are and mark it as the bin. Would it be possible?

Thank you very much in advice!

Not allowing parts to be rotated at all

I think this tool would be great for figuring out efficient cutting for fabric as well, however with some fabric it is essential that none of the parts get rotated at all due to the fabric structure.
Setting "Part Rotations" to 0 or 1 still caused the parts to be rotated, I would suggest an option to not rotate any parts.

Can run demo at local?

run_svgnest com

When run demo at svgnest.com, All are fine.

run_local
Download src and extract it into a local directory and run the demo again.
Progress bar is disappeared. nothing was happened.

Plans for command line version or GPU acceleration?

I was wondering if there were any plans for a local command-line base version of this software. As the code seems to involve a lot of floating-point math, I think it would benefit from a CUDA implementation / GPU acceleration, which isn't currently possible with the Javascript base. Obviously, a local version would come before such optimization, but I was wondering if there were any plans to go in this direction.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.