toxiclib voronoi to PDF

edited July 2014 in Library Questions

Trying to export a sketch to PDF, having some issues, not sure why. Any ideas?

It makes the PDF and all, but it seems to be empty.

import toxi.geom.*;
import toxi.geom.mesh2d.*;

import toxi.util.*;
import toxi.util.datatypes.*;

import toxi.processing.*;

import processing.pdf.*;

boolean recording;
PGraphicsPDF pdf;

// radius of the root triangle which encompasses (MUST) all other points
float DSIZE = 10000;

// a Voronoi diagram relies on a Delaunay triangulation behind the scenes
// we simply use this as a front end
Voronoi voronoi;

ToxiclibsSupport gfx;

// render switch
boolean doIgnoreRoot=true;

void setup() {
 pdf = (PGraphicsPDF) createGraphics(width, height, PDF, "pause-resume.pdf");
  size(600, 600);
  smooth();
  voronoi = new Voronoi(DSIZE);
  gfx = new ToxiclibsSupport(this);
}

void draw() {
  background(255);
  stroke(0);
  noFill();
  stroke(0, 0, 255, 50);
  beginShape(TRIANGLES);
  // get the delaunay triangles
  for (Triangle2D t : voronoi.getTriangles()) {
    // ignore any triangles which share a vertex with the initial root triangle
    if (!doIgnoreRoot || (abs(t.a.x)!=DSIZE && abs(t.a.y)!=DSIZE)) {
      gfx.triangle(t, false);
    }
  }
  endShape();
  fill(255, 0, 255);
  noStroke();
  for (Vec2D c : voronoi.getSites()) {
    ellipse(c.x, c.y, 5, 5);
  }
}



void mousePressed() {
  voronoi.addPoint(new Vec2D(mouseX, mouseY));
}

void keyPressed() {

  doIgnoreRoot=!doIgnoreRoot;

  if (key == 'r') {
    if (recording) {
      endRecord();
      println("Recording stopped.");
      recording = false;
    } else {
      beginRecord(pdf);
      println("Recording started.");
      recording = true;
    }
  } else if (key == 'q') {
    if (recording) {
      endRecord();
    }
    exit();
  }  

}

Answers

  • I believe, not entirely positive, that the actually drawing in draw() needs to occur between the beginRecord() and endRecord() calls. You are calling them in keyPressed(). Create a method to execute the beginRecord() and endRecord() and call that method within draw(). Toggle it with a boolean set with keyPressed().

  • Hmm, I tried something similar that started recording on setup, ends on keypress. same problem.

    import toxi.geom.*;
    import toxi.geom.mesh2d.*;
    
    import toxi.util.*;
    import toxi.util.datatypes.*;
    
    import toxi.processing.*;
    
    import processing.pdf.*;
    
    
    // radius of the root triangle which encompasses (MUST) all other points
    float DSIZE = 10000;
    
    // a Voronoi diagram relies on a Delaunay triangulation behind the scenes
    // we simply use this as a front end
    Voronoi voronoi;
    
    ToxiclibsSupport gfx;
    
    // render switch
    boolean doIgnoreRoot=true;
    
    void setup() {
      beginRecord(PDF, "everything.pdf");
      size(600, 600);
      smooth();
      voronoi = new Voronoi(DSIZE);
      gfx = new ToxiclibsSupport(this);
    }
    
    void draw() {
      background(255);
      stroke(0);
      noFill();
      stroke(0, 0, 255, 50);
      beginShape(TRIANGLES);
      // get the delaunay triangles
      for (Triangle2D t : voronoi.getTriangles()) {
        // ignore any triangles which share a vertex with the initial root triangle
        if (!doIgnoreRoot || (abs(t.a.x)!=DSIZE && abs(t.a.y)!=DSIZE)) {
          gfx.triangle(t, false);
        }
      }
      endShape();
      fill(255, 0, 255);
      noStroke();
      for (Vec2D c : voronoi.getSites()) {
        ellipse(c.x, c.y, 5, 5);
      }
    }
    
    
    
    void mousePressed() {
      voronoi.addPoint(new Vec2D(mouseX, mouseY));
    }
    
    void keyPressed() {
    
      doIgnoreRoot=!doIgnoreRoot;
    
      if (key == 'q') {
        endRecord();
        exit();
      }
    
    }
    
  • Move the beginRecord() to the end of setup() and not before the size() call. That works for me, but it doesn't record the triangles, just the ellipses.

  • hmm, yeah. that's closer. no idea why that changed anything, but would be nice to get lines too. rather not have to manually add them.

  • edited July 2014

    I dunno. I tried adding a strokeWeight() call and removing the noStroke(). It made a stroke around the ellipses, but still no lines. Probably has something to do with the library. You are not actually drawing any lines().

  • lukasz, don't forget to choose a category when creating a new topic. Thanks.
    I moved this one for you.

  • oops! forgot you have to do that with new forum. thanks PhiLho.

  • edited July 2014

    I think there are two main problems.

    1. You should start recording at the beginning of draw() and end recording at the end of draw(). Otherwise you will get a massive PDF with layer-upon-layer at 60 fps. You probably just want the visuals of a single screen, much like a save() screenshot.

    2. You're mixing two drawing methods. namely Processing's begin/endShape with Toxiclibs' gfx.triangle(). Choose one of these. If the PDF renderer can handle geometry drawn by the Toxiclibs class remains to be seen. But drawing begin/endShape's will work, but of course then you need actual vertices between those two calls. Now you are not drawing anything used that method.

    Finally, there are two PDF-export voronoi examples in my latest tutorial, which you can check out here. Do note that they use the Mesh instead of the Toxiclibs library to generate the voronoi. But even if you don't want that method/library, you can still see how the PDF export is done.

  • I am basically taking an example straight from creators code. So I am not sure why mixing those two classes would be problem since I don't know inner workings. I will take a look at how you did it though. thanks for link.

    http://forum.processing.org/one/topic/toxiclib-voronoi-example-sketch.html

  • edited July 2014

    See Toxiclibs' drawing class here.

    Apparantly it contains a boolean to turn on/off the begin/endShape calls. So the code example is in fact not doubling up, because it includes the second false parameter. My bad! :-)

Sign In or Register to comment.