Category: Code
Demo of Lee Byron\'s Mesh library

MeshLibDemo.pde - Demo of Lee Byron’s Mesh library

Lee Byron has written a neat little Processing library called Mesh which allows for easy calculation and display of Voronoi, Delaunay and Convex Hull diagrams.

Given a set of points, these diagrams calculate the minimal regions around the points (Voronoi), an optimal triangulation of the points (Delaunay) or the polygon shape that contains all the points (Convex Hull). So far the library only supports the 2D versions of the diagrams, but it is in part based on the QuickHull3D java library which also handles 3D hulls.

Byron didn’t include any code examples in the current release, so I hacked up a quick demo.

Code: MeshLibDemo.pde

To run this example, download MeshLibDemo.zip and unzip it inside your Processing sketches folder. The Mesh library is included in a “libraries” subfolder, but you’ll have to restart Processing for the library to be recognized.

I’m posting the full code below for easy reference.

Read the rest of this entry »

1 Comment »

I’ve decided to put my Processing hacks from the Code & hacks page and consolidate them into a downloadable archive. Thus the Code & Form code library is born. It will contain various demos and hacks, as well as example code for the unlekkerLib library. It should be much easier to publish code this way, since students etc. can simply download the archive and dump the contents in their sketchbook folder for quick access.

I’ve started a Google Code repository for this purpose, which seems a good way to go. I’m still figuring out SVN etc, hopefully I can switch to publishing the unlekkerLib source “live” through SVN once I figure it out. I’ll also publish archives of code written during past workshops to the repository.

There seems to be a growing list of Processing-related Google Code repositories out there, examples include Toxi’s Toxiclibs, interfascia and jddaniels. Do a search for processing.org to find more.

2 Comments »

The following Processing example shows how to set up a separate thread for loading images into a sketch. I wrote it up in response to this post on the Processing forums, figuring it will be useful to some of my students too.

Code: ThreadImageLoader.pde

Read the rest of this entry »

7 Comments »

Turns out Java has no mechanism for discovering the names of disk volumes. You can use java.io.File.listRoots() to get the root paths for multiple disks, but you can’t auto-discover their names.

Here’s a quick hack that will work on Windows. It uses Runtime.getRuntime().exec() to run “cmd /c dir “, the output of which is parsed to get the volume name.

import java.io.*; 

void setup() {
  println("Testing getVolName");

  String path="D:";
  println("Volume name for "+path+" is: "+
    "'"+getDiskVolumeName(path)+"'");
}

public static String getDiskVolumeName(String path) {
  String volname="Unknown";
  String check="Volume in drive "+path.charAt(0)+" is ";
  try  {
    Process p=Runtime.getRuntime().exec("cmd /c dir "+path);
    //p.waitFor(); 

    BufferedReader reader=new BufferedReader(
      new InputStreamReader(p.getInputStream()));
    String line=reader.readLine();
    while(line!=null) {
      if(line.indexOf(check)!=-1)
        volname=line.substring(line.indexOf(check)+check.length());

//      System.out.println(line);
      line=reader.readLine();
    }
  }
  catch(Exception e1) {
    println("Failure: "+e1.toString());
  }
  return volname;
}

No Comments »

080205_rhinoscript.jpg

Rhinoscript sketch, extruding a revolution surface along random curves. Good cheesy fun.

I had a chance to see a bit more of the impressive tool Rhino 4 during the Generator.x 2.0 workshop, and so I thought I’d have a go at making a simple sketch in Rhinoscript. As it turns out, the fact that Rhinoscript is based on VBScript makes coding feel horrible at first. Seriously, who would want to use syntax like that? It might be easy for beginners to pick up, but it quickly gets painful once you’re dealing with complex API calls and 100+ lines of code.

Nevertheless, frustration soon gives way to amazement at the built-in Rhino library and its vast array of heavy-duty functions for creating and manipulating curves, meshes and NURBS surfaces. In comparison, mesh generation in Processing is enough to give anyone a headache, and I seriously doubt anyone would even attempt to implement NURBS. Even Boolean mesh operations is a staggering task, with no good Java libraries readily available.

While Rhinoscript is firmly a non-realtime tool, its power for pure geometry is amazing. I would definitely use Rhino as a creative tool for digital fabrication projects, where animation is not the goal. There are some excellent RhinoScript resources online, for starters look at RhinoScript 101 and David Rutten’s tutorial. I would also definitely recommend using the Monkey Script editor instead of the built-in editor, it’s more powerful and has a very useful documentation feature.

The script below gives a basic idea of the Rhino syntax, and while it is a basic sketch suffering from 3D clichées, it shows the power and versatility of Rhinoscript. I just wish it wasn’t Visual Basic.

Code: RandRail.rvb

Read the rest of this entry »

6 Comments »

Simple example of how to generate meshes with Processing and then output them to STL using unlekkerLib.

import unlekker.util.*;
import unlekker.geom.*;
import unlekker.data.*;

import processing.opengl.*;

boolean doSTL=false;

void setup() {
  size(400,400, OPENGL);
}

void draw() {
  background(100);

  if(doSTL) {
    beginRaw("unlekker.data.STL","cyl.stl");
  }

  translate(width/2, height/2, 0);
  rotateY(radians(frameCount));
  rotateX(radians(frameCount));

  fill(255,255,255);

  for(int i=0; i<100; i++) {
    pushMatrix();
    translate(random(-300,300),random(-300,300),random(-300,300));
    rotateY(random(PI*2));
    rotateX(random(PI*2));
    cylinder(50,random(50,200));
    popMatrix();
  }

  if(doSTL) {
    endRaw();
    doSTL=false;
  }

}

void keyPressed() {
  if(key=='s') doSTL=true;
}

void cylinder(float w,float h) {
  float px,pz;

  beginShape(QUAD_STRIP);
  for(float i=0; i<13; i++) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,-h,pz);
    vertex(px,h,pz);
  }
  endShape();

  beginShape(TRIANGLE_FAN);
  vertex(0,-h,0);
  for(float i=12; i>-1; i–) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,-h,pz);
  }
  endShape();

  beginShape(TRIANGLE_FAN);
  vertex(0,h,0);
  for(float i=0; i<13; i++) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,h,pz);
  }
  endShape();
}

7 Comments »

The AHO students needed a simple file uploader that would automatically transfer files from a local folder to a web server. The following application will simply watch a given folder and upload any files it contains to the FTP server. Note that it will delete the local copy upon successful upload, so be careful how you use it.

Code: FTPUploader.pde

The code for the application is given below, but downloading the following ZIP will give you the required edtFTPj library files as well as a sample config file:

Read the rest of this entry »

1 Comment »

Ira Greenberg’s "Processing: Creative Coding and Computational Art" (published by Friends of Ed) was the first Processing book to hit the shelves this fall. I haven’t had a chance to look at it in Person, but from the sample chapters provided it looks very useful.

“Appendix C: Integrating Processing within Java” should be of interest to anyone looking to better understand how Processing and Java work together. It breaks down the basics of how pure Java syntax differs from Processing, and shows how you can make the switch quite easily. It wraps up with a useful example of how to write a Swing GUI application with a Processing sketch as a GUI Component.

The two other sample chapters deal respectively with 3D rendering (including a quick introduction to vector math) and drawing more complex shapes. You can download all of them from the Friends of Ed site, where you can also buy the book in hardcopy or ebook form.

[via Processing forums: Two windows in pure java?]

1 Comment »

I got an email from two Caseys last night (i.e. [Casey Alt-http://caseyalt.com/] and Casey Reas), announcing the relaunch of the artsoftware.org Wiki. The intention of the site is to be a gathering point for information about free and Open Source software created by and for artists.

Take a look at the list of existing pages and see if you can’t contribute something…

No Comments »

I just posted a quick update to unlekkerLib. It seems the first version I posted used Java 5 because I hadn’t configured Eclipse correctly. This version is compiled for 1.4. I have tested it with Processing 0125, but please give me feedback if it gives you trouble.

I added two new functions to the library, nothing big but could be useful for some people:

[071028] unlekkerLib-0002 features:
  • unlekker.data.POVRay: Primitive POV-Ray triangle geometry export.
  • unlekker.data.FeedReader: Feed reading utility class (requires installing extra JARs)

To use the feed classes simply use the code from this previous post, minus the class definitions.

1 Comment »