|
import processing.opengl.*;
import processing.pdf.*;
import toxi.geom.*;
import unwrap.*;
Unwrap u;
ArrayList t;
Vec3D cam = new Vec3D(0,0,0);
PGraphicsPDF pdf;
void setup() {
size(screen.width,screen.height, OPENGL);
pdf = (PGraphicsPDF) createGraphics(width, height, PDF, "unwrapping.pdf"); // initialise PDF
update();
}
void update() {
// initialise Unwrap
u = new Unwrap(this,createFont("Verdana",48)); // construct a new unwrapper
//////////////////// calculate Cylinder
float degree, num, cosval, sinval, xr, yr;
xr = 200;
yr = 200;
num = random(4,15);
degree = radians(360 / num);
u.beginStrip(); // begin triangleStrip
for (int i = 0; i < num ; i++) {
cosval = cos(degree * i);
sinval = sin(degree * i);
u.addPoint(cosval * xr*random(2), -yr, sinval * xr*random(2)); // add points to Strip
u.addPoint(cosval * xr*random(2), yr, sinval * xr*random(2)); // add points to Strip
}
u.endStrip(CLOSE); // end and close Strip
u.clip = new CustomUClip(); // set custom clip
}
void draw() {
background(200);
stroke(255);
fill(200);
lights();
// camera
float d=frameCount;
cam.x=sin(radians(d))*mouseX*4;
cam.y=mouseY*4-height;
cam.z=cos(radians(d))*mouseX*4;
camera(cam.x,cam.y,cam.z,0,0,0,0,1,0);
//////////////////// draw 3d
// u.draw3d(); // display triangles
u.drawNormals(); // draw triangle normals
u.display3dIntersection(); // display triangles and triangle-triangle intersections
//////////////////// draw 2d
camera();
noFill();
t = u.triangles; // get the 2d triangles
for(Iterator i=t.iterator(); i.hasNext();) { // loop the triangle arraylist
UTriangle tri=(UTriangle)i.next(); // get next triangle
tri.set2d(new Vec2D(width/2, height/2)); // set the 2d triangle with given coordinates
tri.drawNeighborInfoText(10); // draw info text for assembling
tri.drawClips(); // draw clips
}
}
//////////////////// save PDF
void keyReleased(){
PGraphicsPDF pd = (PGraphicsPDF) pdf;
pdf.beginDraw();
pdf.stroke(0);
int idx=0;
t = u.triangles; // get 2d triangles
for(Iterator i=t.iterator(); i.hasNext();) { // loop the triangle arraylist
UTriangle tri=(UTriangle)i.next(); // get next triangle
pdf.noFill();
tri.set2d(new Vec2D(width/2, height/2)); // set the 2d triangle with given coordinates
tri.drawClips(pdf); // draw Clips
pdf.fill(0);
pdf.textMode(SHAPE); // save font with outlines
tri.drawNeighborInfoText(10,pdf); // draw info text for assembling
pd.nextPage(); // add new Page
println(idx++);
}
pdf.dispose();
pdf.endDraw();
println("pdf_saved");
}
void mouseReleased() {
update();
}
class CustomUClip extends UClip{
void drawClip(USide s, Vec2D a, Vec2D b, PGraphics pg){ // override this function to create custom clips:
if(s.neighbor != null && s.sideState == true){ //if triangle neighbor exist and neighbor side has no clip
pg.beginShape(); // begin PGraphics shape
pg.vertex(a.x, a.y); // add PGraphics vertex
// get the direction from two points and rotate around the first one with the given angle and distance
Vec2D a2 = getRotatedPoint(a, b, -radians(30), 20);
pg.vertex(a2.x, a2.y); // add PGraphics vertex
// get the direction from two points and rotate around the first one with the given angle and distance
Vec2D b2 = getRotatedPoint(b, a, radians(30), 20);
pg.vertex(b2.x, b2.y); // add PGraphics vertex
pg.vertex(b.x, b.y); // add PGraphics vertex
pg.endShape();// end PGraphics shape
drawDashedLine(a,b,200,pg); // draw a dashed line from a to b
}
else{
pg.line(a.x,a.y,b.x,b.y); // draw single line
}
}
} |