Permalink
Browse files

Initial attempt at incorporating Java Area and Polygon classes. Limit…

…ing potential breakage to DXF writer code.
  • Loading branch information...
clothbot committed Aug 27, 2010
1 parent c801704 commit 8680dbdf8298d4d8873a2999507674502135dcc9
Showing with 169 additions and 3 deletions.
  1. +43 −0 Area2D.pde
  2. +120 −0 Poly2D.pde
  3. +3 −0 SuperSkein.pde
  4. +3 −3 config.txt
View
@@ -0,0 +1,43 @@
+// Testing 2D boolean operation using Java built-in Area construct.
+// - extending example from http://wiki.processing.org/w/Using_AWT's_Polygon_class
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Area;
+import java.awt.geom.PathIterator;
+import java.awt.Polygon;
+
+class Area2D extends Area{
+ float gridScale;
+ public Area2D(Poly2D p) {
+ super( (Polygon) p );
+ gridScale=p.gridScale;
+ }
+ public Area2D(float gScale) {
+ super();
+ gridScale=gScale;
+ }
+
+ boolean contains(int x, int y) {
+ double dx=x/gridScale;
+ double dy=y/gridScale;
+ return super.contains(dx,dy);
+ }
+
+ boolean contains(double x, double y) {
+ double dx=x/gridScale;
+ double dy=y/gridScale;
+ return super.contains(dx,dy);
+ }
+
+ void drawMe(){
+ PathIterator pathIter=getPathIterator(new AffineTransform());
+ beginShape();
+ float[] newCoords={0,0};
+ while(!pathIter.isDone()) {
+ pathIter.currentSegment(newCoords);
+ vertex(gridScale*newCoords[0],gridScale*newCoords[1]);
+ pathIter.next();
+ }
+ endShape();
+ }
+}
+
View
@@ -0,0 +1,120 @@
+// Testing 2D boolean operation using Java built-in Area construct.
+// - extending example from http://wiki.processing.org/w/Using_AWT's_Polygon_class
+
+import java.awt.Polygon;
+
+/*
+ The class inherit all the fields, constructors and functions
+ of the java.awt.Polygon class, including contains(), xpoint,ypoint,npoint
+*/
+
+class Poly2D extends Polygon{
+ float gridScale;
+ float epsilon;
+
+ public Poly2D(float gScale) {
+ super();
+ gridScale=gScale;
+ epsilon=gridScale/2;
+ }
+
+ public Poly2D(float gScale, int[] x,int[] y, int n) {
+ super();
+ gridScale=gScale;
+ epsilon=gridScale/2;
+ //call the java.awt.Polygon addPoint function
+ for(int i=0;i<n;i++) {
+ super.addPoint((int) round(x[i]/gridScale),(int) round(y[i]/gridScale));
+ }
+ }
+
+ public Poly2D(float gScale, double[] x,double[] y, int n) {
+ super();
+ gridScale=gScale;
+ epsilon=gridScale/2;
+ println("Area2D gridScale="+gridScale);
+ //call the java.awt.Polygon addPoint function
+ for(int i=0;i<n;i++) {
+ super.addPoint((int) (x[i]/gridScale),(int) (y[i]/gridScale));
+ }
+ }
+
+ void addPoint(float xf, float yf) {
+ super.addPoint((int) (xf/gridScale), (int) (yf/gridScale));
+ }
+
+ ArrayList Slice2Poly2DList(Slice thisSlice) {
+ ArrayList nuAL=new ArrayList();
+ Poly2D nuP2D=new Poly2D(gridScale);
+ Line2D prevLine=(Line2D) thisSlice.Lines.get(0);
+ PVector startPt = prevLine.getPoint1();
+ nuP2D.addPoint(startPt.x,startPt.y);
+ for(int i = 1; i<thisSlice.Lines.size(); i++ ) {
+ Line2D thisLine=(Line2D) thisSlice.Lines.get(i);
+ PVector prevPt = prevLine.getPoint2();
+ PVector currPt = thisLine.getPoint1();
+ if(abs(prevPt.x-currPt.x)<epsilon && abs(prevPt.y-currPt.y)<epsilon) {
+ nuP2D.addPoint(currPt.x,currPt.y);
+ } else {
+ nuP2D.addPoint(prevPt.x,prevPt.y);
+ nuAL.add(nuP2D);
+ nuP2D=new Poly2D(gridScale);
+ nuP2D.addPoint(currPt.x,currPt.y);
+ startPt=currPt;
+ }
+ if(abs(startPt.x-currPt.x)<epsilon && abs(startPt.y-currPt.y)<epsilon) {
+ nuP2D.addPoint(currPt.x,currPt.y);
+ i++;
+ if(i<thisSlice.Lines.size()) {
+ nuAL.add(nuP2D);
+ nuP2D=new Poly2D(gridScale);
+ prevLine=(Line2D) thisSlice.Lines.get(i);
+ startPt=prevLine.getPoint1();
+ nuP2D.addPoint(startPt.x,startPt.y);
+ } else {
+ PVector lastPt=thisLine.getPoint2();
+ nuP2D.addPoint(lastPt.x,lastPt.y);
+ nuAL.add(nuP2D);
+ }
+ } else {
+ prevLine=thisLine;
+ }
+ }
+ return nuAL;
+ }
+
+ void translate(int x, int y) {
+ super.translate((int) round(x/gridScale),(int) round(y/gridScale));
+ }
+
+ void translate(double x, double y) {
+ super.translate((int) (x/gridScale),(int) (y/gridScale));
+ }
+
+
+ boolean contains(int x, int y) {
+ double dx=x/gridScale;
+ double dy=y/gridScale;
+ return super.contains(dx,dy);
+ }
+
+ boolean contains(double x, double y) {
+ double dx=x/gridScale;
+ double dy=y/gridScale;
+ return super.contains(dx,dy);
+ }
+
+
+ float getGrid() {
+ return gridScale;
+ }
+
+ void drawMe(){
+ beginShape();
+ for(int i=0;i<npoints;i++){
+ vertex(round(gridScale*xpoints[i]),round(gridScale*ypoints[i]));
+ }
+ endShape();
+ }
+}
+
View
@@ -405,6 +405,7 @@ class DXFWriteProc implements Runnable{
output.println("\nmodule dxf_slice(index=0) {");
Slice ThisSlice;
+ ArrayList PolyArray;
float Layers = STLFile.bz2/LayerThickness;
int renderWidth=width, renderHeight=height;
int sliceCount=0;
@@ -418,6 +419,8 @@ class DXFWriteProc implements Runnable{
pgDxf.setLayer(DXFSliceNum);
DXFWriteFraction = (ZLevel/(STLFile.bz2-LayerThickness));
ThisSlice = new Slice(STLFile,ZLevel);
+ Poly2D ThisPoly2D = new Poly2D(0.01);
+ PolyArray = ThisPoly2D.Slice2Poly2DList(ThisSlice);
lin = (Line2D) ThisSlice.Lines.get(0);
for(int j = 0;j<ThisSlice.Lines.size();j++)
{
View
@@ -1,6 +1,6 @@
-CONFIG_SCALE 1.389099
-CONFIG_XROTATE 0.34234
-CONFIG_STLFILE C:\git\SuperSkein\triess_printable03.stl
+CONFIG_SCALE 1.39
+CONFIG_XROTATE 0.34
+CONFIG_STLFILE sculpt_dragon.stl
MACHINE_OPTEMP 220
MACHINE_FLOWRATE 190
CONFIG_SINK 2.0

0 comments on commit 8680dbd

Please sign in to comment.