Interaction Design WikiDigital Fabrication

Processing and Laser cutting

This example produces a pseudo 3d form from a depth map image. It produces a pdf file suitable for Laser cutting based on this slicing method.  

import processing.pdf.*;
PGraphicsPDF pdf;

int gridWidth = 100; 
int gridHeight = 100; 
int cutHeight = 50;
float pointDistX = 5; 
float pointDistY = 5; 
ArrayList<Float> imageGreyScaleList = new ArrayList<Float>();
ArrayList<Integer> MaxHeight = new ArrayList<Integer>();
float waveHeight = 20;
PImage baseImage;  


void setup() {

  size(700, 700);
  smooth();
  baseImage = loadImage("data/image.jpg");
  baseImage.loadPixels();
  int loc;
  float combinedHeight = 0;
  for (int i = 0; i < baseImage.width; i++) {
    MaxHeight.add(0);
    for (int j = 0; j < baseImage.height; j++) {  

      loc = i + j*baseImage.width;
      //println(baseImage.pixels[loc]);
      color pixel = baseImage.pixels[loc];
      int greyScale = floor(red(pixel));
      float heightConverted = map(greyScale, 0, 255, 0, -waveHeight);
      imageGreyScaleList.add(heightConverted);
    }
    combinedHeight += waveHeight+cutHeight;
  }
  // create a pdf the lenght of the total graphic
  pdf = (PGraphicsPDF) createGraphics(width, int(combinedHeight), PDF, "export.pdf");
}

void draw() {
  background(255);
  beginRecord(pdf);
  exportSlices();
  endRecord();
  background(255);
  draw2D();
  noLoop();
};

void draw2D() {
  fill(255);
  background(255);
  pushMatrix(); 
  //translate graphic to the center of the screen  
  translate((width-(pointDistX*gridWidth))/2, (height-(pointDistY*gridHeight))/2);
  int loc;
  for (int i = 0; i<gridHeight; i++ ) {
    beginShape();
    for (int j = 0; j<gridWidth; j++) {
      loc = i + j*baseImage.width;
      float posX =  j*pointDistX;
      float posY =  i*pointDistY+imageGreyScaleList.get(loc); // offset y coordinate based on the pixels colour
      curveVertex(posX, posY);
    };

    endShape();
  };
  popMatrix();
};

void exportSlices() {
  noFill();
  background(255);
  pushMatrix(); 
  //translate graphic to the center of the screen  
  //translate((width-(pointDistX*gridWidth))/2, (height-(pointDistY*gridHeight))/2);
  int loc;
  for (int i = 0; i<gridHeight; i++ ) {
    float y =  i*(pointDistY+cutHeight+waveHeight);
    beginShape();
    curveVertex(0, y);
    for (int j = 0; j<gridWidth; j++) {
      loc = i + j*baseImage.width;
      float posX =  j*pointDistX;
      float posY = y+imageGreyScaleList.get(loc); // offset y coordinate based on the pixels colour
      curveVertex(posX, posY);
   
    };
    vertex(gridWidth*pointDistX, y);
    vertex(gridWidth*pointDistX, y+cutHeight);
    vertex(0, y+cutHeight);
    vertex(0, y);
    pushStyle();
    fill(0);
    text("no_ "+i, 5, y+cutHeight-5);
    popStyle();
    endShape();
  };
  popMatrix();
};