Yisaer / Nest4J

an open source nest algorithm by java based on SVGNest
MIT License
75 stars 35 forks source link

Final transformation results #10

Open Arun-A-P opened 4 years ago

Arun-A-P commented 4 years ago

First off, I wanted to say this is a brilliant tool you've written here. It's works amazingly well, easy to use and understand.

I just wanted some help understanding the final transformations. I'm using Nest4J as a command line jar and I'm passing it the list of polygon vertices in a file. To make sure the polygons do not overlap I just add 1000 to the x-coordinate of the previous model.

void readFile(String filePath) throws IOException{
    File file = new File(filePath);
    BufferedReader br = new BufferedReader(new FileReader(file));

    binWidth = Double.parseDouble(br.readLine().trim());
    binHeight = Double.parseDouble(br.readLine().trim());
    spacingBetweenModels = Double.parseDouble(br.readLine().trim());

    bin = new NestPath();
    bin.add(0, 0);
    bin.add(binWidth, 0);
    bin.add(binWidth, binHeight);
    bin.add(0, binHeight);
    bin.bid = 0;

    String line;
    int count = 1;
    while ((line = br.readLine()) != null) {
        NestPath model = new NestPath();
        String[] input = line.split(",");
        for(int i = 0; i < input.length; i = i + 2){
            model.add(Double.parseDouble(input[i]) + count * 1000, Double.parseDouble(input[i+1]));
        }
        model.bid = count++;
        model.setRotation(360);
        listOfModels.add(model);
      } 

    br.close();
}

So, it keeps adding 1000 to the x-coordinate off the previous model, so nothing ends up overlapping. I tested it out by writing an SVG file and it works perfectly!

My question is about the final Placement output. What does translate refer to with respect to the original point? Also, is rotation after translation and is it with the original point as the center of rotation? I wanted to reproduce the output in my application so I wanted a way to use the placement results in my application after removing the +1000 x coordinate addition I made to not make them overlap.

Yisaer commented 4 years ago

Hi @Arun-A-P , here is the code how rotate works. In Nest4J, the ploygon rotate arround its center so it's ok to transfer the each polygon x coordinate. https://github.com/Yisaer/Nest4J/blob/80a5fa910a8145ddf1f9af659294007b27864220/src/main/java/com/qunhe/util/nest/util/GeometryUtil.java#L197-L212

Arun-A-P commented 4 years ago

Hey @Yisaer that's great! I understand how rotation works now. I had a question about the final translation though. I'm only transferring the x coordinate as you can see but the final translation has the y coordinate translation very high as well. How do I interpret the translations?

Arun-A-P commented 4 years ago

Here, is an example for the output I'm trying to understand:

Input

Bin Coordinates - (0, 0) (180, 0) (180, 100) (0, 100)

Model 1 Coordinates - (1000.0, 0.0) (1000.0, 20.0) (1020.0, 20.0) (1020.0, 0.0)

Model 2 Coordinates - (2000.0, 0.0) (2000.0, 40.0) (2040.0, 40.0) (2040.0, 0.0)

Output Placements -

Model 1 - Translation X - -412.6182 Translation Y - 924.4339 Rotation - 295.0

Model 2 - Translation X - 1843.5397 Translation Y - 956.5316 Rotation - 206.0

Final Placement Screenshot -

Screen Shot 2020-01-20 at 3 27 43 PM

As you can see from the screenshot, it looks like the first object is closer to the top left of the bin. So, it's coordinates should be closer to (0, 0) but from the applied translations you would get the X and Y coordinate of Model 1 and Model 2 to be well out of the bin. So, I wanted to know if I'm reading the final results wrong or if there is any problem with how I'm getting the results. From the screenshots it looks like the first model has not been rotated but from the rotation output it looks like it has been rotated at an angle.

Here's the code from which I try to get the final placements -

List<List<Placement>> appliedPlacement = nest.startNest();

  for (Placement placement : appliedPlacement.get(0)) 
  {
    String bid = Integer.toString(placement.bid);
    String translationX = Double.toString(placement.translate.x + 10);
    String translationY = Double.toString(placement.translate.y);
    String rotation = Double.toString(placement.rotate);
    writer.println(bid + "," + translationX + "," + translationY + "," + rotation);
 }