UnwrappingLib


A library for the programming environment processing.


With this Library you are able to flatten triangles from tree into two dimensions.
Its usful to unwrapp Lowpolymodels for reconstruction, digital production.



Tell me about problems, bugs, etc..
This was my first library, so i hope you can use it although…

Download unwrappingLib 0003 beta 0.04 beta (Processing 2.0 alpha) in .zip format.

The source code is available on Google Code (0003beta)
Thanks to Karsten Schmidt and Philip Whitfield who gave me some great imputs.

Have a look at the javadoc reference here.


Installation
Download the unwrappingLib and the required toxiclibscore.
Unzip and put the extracted folders into the libraries folder of your processing sketches.


Example1:



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(1024,768, OPENGL);
  pdf = (PGraphicsPDF) createGraphics(width, height, PDF, "unwrapping.pdf"); // initialise PDF
  
  update();
}

void update() {
  u = new Unwrap(this,createFont("Verdana",48)); // construct a new unwrapper

  u.beginStrip(); // begin triangleStrip
  u.addPoint(new Vec3D()); // add new Point to the triangleStrip
  u.addPoint(new Vec3D(0,-200,0));
  u.addPoint(new Vec3D(200,0,0));
  u.addPoint(new Vec3D(200,-300,0));
  u.addPoint(new Vec3D(200,0,-200));
  u.addPoint(new Vec3D(200,-200,-200));
  u.endStrip(CLOSE); // end triangleStrip
}


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(); // draw triangles in 3d
  u.drawNormals(); // draw triangle normals

  //////////////////// 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.draw2d(new Vec2D(width/2, height/2)); // set the 2d triangle and draw it on width/2, height/2
    tri.drawNeighborInfoText(10);  // draw info text for assembling
  }

}

//////////////////// save PDF
void keyReleased(){  

  PGraphicsPDF pd = (PGraphicsPDF) pdf;
  pd.beginDraw();
  pd.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
    pd.noFill();
    tri.draw2d(new Vec2D(width/2, height/2),pd); // set the 2d triangle and draw it on width/2, height/2
    pd.fill(0);
    pd.textMode(SHAPE); // save font with outlines
    tri.drawNeighborInfoText(10,pd); // draw info text for assembling
    pd.nextPage(); // add new Page
    println(idx++);
  }
  pd.dispose();
  pd.endDraw();
  println("pdf_saved");
}


void mouseReleased() {
  update();
}



Example2:




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
    }
  }
}