Commits

Karsten Schmidt  committed 8eb238b

adding PackedLayout algorithm and minor updates to collision detection, added setLayout() and new constructor to Unwrapper

  • Participants
  • Parent commits 3dbfdb5

Comments (0)

Files changed (5)

File src.test/flatworld/test/UnwrapTest.java

 
 import processing.core.PApplet;
 import processing.pdf.PGraphicsPDF;
-import toxi.geom.AABB;
-import toxi.geom.Plane;
+import toxi.geom.Sphere;
 import toxi.geom.mesh.WETriangleMesh;
 import toxi.math.conversion.UnitTranslator;
 import toxi.util.DateUtils;
 import flatworld.EdgeRenderStrategy;
 import flatworld.GlueTabEdgeStrategy;
+import flatworld.HoleMountStrategy;
+import flatworld.PackedLayout;
 import flatworld.UnwrapSheet;
 import flatworld.Unwrapper;
 
         if (pdfPath != null) {
             for (Iterator<UnwrapSheet> i = unwrap.getSheets().iterator(); i
                     .hasNext();) {
-                i.next().draw(pdf, useLabels);
+                i.next().draw(pdf, true, useLabels);
                 if (i.hasNext()) {
                     pdf.nextPage();
                 }
             pdfPath = null;
             println("done...");
         } else {
-            unwrap.getSheets().get(currSheetID).draw(g, useLabels);
+            unwrap.getSheets().get(currSheetID).draw(g, true, useLabels);
         }
     }
 
 
     private void reset() {
         WETriangleMesh mesh;
-        mesh = new WETriangleMesh().addMesh(new Plane().toMesh(200));
-        // mesh = new WETriangleMesh().addMesh(new Sphere(300).toMesh(5));
-        mesh = new WETriangleMesh().addMesh(new AABB(100).toMesh());
+        // mesh = new WETriangleMesh().addMesh(new Plane().toMesh(200));
+        mesh = new WETriangleMesh().addMesh(new Sphere(200).toMesh(5));
+        // mesh = new WETriangleMesh().addMesh(new AABB(100).toMesh());
         println(mesh);
         unwrap = new Unwrapper(width, height);
-        unwrap.unwrapMesh(mesh, 1, edgeStrategy);
+        unwrap.setLayout(new PackedLayout());
+        unwrap.setEdgeStrategy(edgeStrategy);
+        unwrap.unwrapMesh(mesh, 1);
         currSheetID = 0;
     }
 
         textFont(createFont("SansSerif", 9));
         textAlign(CENTER);
         edgeStrategy = new GlueTabEdgeStrategy(TAB_WIDTH, 0.1f);
+        edgeStrategy = new HoleMountStrategy(2, 8, 40);
+        edgeStrategy.setLabelInset(20);
         reset();
     }
 }

File src/flatworld/PackedLayout.java

+package flatworld;
+
+import java.util.List;
+
+import toxi.geom.Vec2D;
+import toxi.math.MathUtils;
+
+public class PackedLayout implements UnwrapLayoutStrategy {
+
+    protected float scanDelta;
+    protected float scanBleed;
+    protected float scanTheta;
+
+    public PackedLayout() {
+        this(20, 20, MathUtils.TWO_PI / 36);
+    }
+
+    public PackedLayout(float scanBleed, float scanDelta, float scanTheta) {
+        this.scanBleed = scanBleed;
+        this.scanDelta = scanDelta;
+        this.scanTheta = scanTheta;
+    }
+
+    protected float getMinDistanceOnSheet(UnwrappedFace face, UnwrapSheet sheet) {
+        float minDist = 0;
+        for (UnwrappedFace f : sheet.faces) {
+            minDist +=
+                    f.collTri.getClosestPointTo(face.collTri.a)
+                            .distanceToSquared(face.collTri.a);
+            minDist +=
+                    f.collTri.getClosestPointTo(face.collTri.b)
+                            .distanceToSquared(face.collTri.b);
+            minDist +=
+                    f.collTri.getClosestPointTo(face.collTri.b)
+                            .distanceToSquared(face.collTri.b);
+        }
+        return minDist;
+    }
+
+    public boolean placeFace(UnwrappedFace face, List<UnwrapSheet> sheets) {
+        UnwrapSheet bestSheet = null;
+        Vec2D bestPos = null;
+        float bestTheta = 0;
+        float minDist = Float.MAX_VALUE;
+        face.setPosition(new Vec2D(), 0);
+        float faceArea = face.getArea();
+        boolean isPlaced = false;
+        for (UnwrapSheet sheet : sheets) {
+            float width = sheet.getBounds().width;
+            float height = sheet.getBounds().height;
+            if (sheet.getFreeArea() >= faceArea) {
+                for (float x = scanBleed, y = scanBleed; y <= height
+                        - scanBleed;) {
+                    Vec2D pos = new Vec2D(x, y);
+                    for (float theta = 0; theta < MathUtils.TWO_PI; theta +=
+                            scanTheta) {
+                        face.setPosition(pos, theta);
+                        if (sheet.isFacePlacable(face)) {
+                            float dist = getMinDistanceOnSheet(face, sheet);
+                            if (dist <= minDist) {
+                                bestSheet = sheet;
+                                bestPos = pos;
+                                bestTheta = theta;
+                                minDist = dist;
+                            }
+                        }
+                    }
+                    x += scanDelta;
+                    if (x > width - scanBleed) {
+                        x = scanBleed;
+                        y += scanDelta;
+                    }
+                }
+            }
+        }
+        if (bestSheet != null) {
+            face.setPosition(bestPos, bestTheta);
+            bestSheet.add(face);
+            isPlaced = true;
+        }
+        return isPlaced;
+    }
+}

File src/flatworld/UnwrapSheet.java

         }
     }
 
+    public Rect getBounds() {
+        return bounds;
+    }
+
     public float getFillRatio() {
         return usedArea / totalArea;
     }

File src/flatworld/UnwrappedFace.java

 
     private float area;
 
+    private float minOffset = 2;
+
     public UnwrappedFace(Triangle2D t, int id, float scale, UnwrapEdge eab,
             UnwrapEdge ebc, UnwrapEdge eca) {
         this.origTri = t;
     }
 
     public Rect getBounds() {
-        return tri.getBounds();
+        return collTri.getBounds();
     }
 
     public boolean intersectsFace(UnwrappedFace f) {
         // if (collTri.isClockwise()) {
         // collTri.flipVertexOrder();
         // }
-        collTri.adjustTriangleSizeBy(edgeAB.getOffset(), edgeBC.getOffset(),
-                edgeCA.getOffset());
+        collTri.adjustTriangleSizeBy(edgeAB.getOffset() + minOffset,
+                edgeBC.getOffset() + minOffset, edgeCA.getOffset() + minOffset);
     }
 }

File src/flatworld/Unwrapper.java

         this.edgeStrategy = edgeStrategy;
     }
 
+    public void setLayout(UnwrapLayoutStrategy layout) {
+        this.layout = layout;
+    }
+
     public void unwrapMesh(WETriangleMesh mesh, float scale) {
         sheets.clear();
         int id = 1;