Tag:

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 »

Sample code to use mouse wheel events in Processing.

Read the rest of this entry »

1 Comment »

Here is a simple example showing how to use unlekkerLib to output and input 3D geometry in STL format.

// STLBoxes.pde - demonstrates how to use unlekkerLib to
// import / export STL geometry data.
//
// Marius Watz - http://workshop.evolutionzone.com/

import unlekker.data.*;

STL stl;

public void setup() {
  size(400,400, P3D);
  frameRate(25);
  sphereDetail(12);
}

public void draw() {
  translate(width/2,height/2);

  if(frameCount==10) outputSTL();
  else if(frameCount==11) readSTL();
  if(frameCount<12) return;

  background(0);
  noStroke();
  lights();
  rotateY(radians(frameCount));
  rotateX(radians(frameCount*0.25f));
  fill(0,200,255, 128);
  stl.draw();

}

public void readSTL() {
  stl=new STL(this,"Boxes.stl");
  stl.normalize(400); // scale object
  stl.center(); // center it around world origin
}

public void outputSTL() {
  float rad;

  stl=(STL)beginRaw("unlekker.data.STL","Boxes.stl");
  for(int i=0; i<200; i++) {
    pushMatrix();
    translate(random(-200,200),0,-random(400));
    rotateX(((float)(int)random(6))*radians(30));
    rotateY(((float)(int)random(6))*radians(30));

    rad=random(5,25);
    if(random(100)>5) box(rad,random(50,200),rad);
    else sphere(rad);
    popMatrix();
  }
  endRaw();
}

8 Comments »

After suggesting to my AHO class that they consider using RSS feeds in Processing, I realized that there is actually no built-in functionality to do so. The XML class built into Processing is too basic to handle feeds, it seems. To remedy the situation I’ve cooked up a quick hack using the ROME library for RSS / Atom syndication.

To run the code below you’ll need to download ROME and JDOM. Make a “code” subfolder in your sketch and paste “jdom.jar” and “rome-*.jar” into it, then run the code as given. The FeedReader and FeedEntry convenience classes take care of parsing the feed and returning the entries with the most common fields included. Error checking is rudimentary, however.

Code - feedParser.pde

Read the rest of this entry »

7 Comments »

2D line intersection

Code for 2D line intersection

I’ve been struggling with finding the intersection between two lines so that the resulting point actually lies on the specified line segments. Most line intersection formulas use an infinite line model, so the point will tend to lie on an infinite extension of the segments. Which is not what I wanted at all.

First I implemented Paul Bourke’s excellent breakdown of the math. Then after much experimentation I realized I could calculate the distances from the intersection point to the end points of the two lines, and then check their combined length against the length of the lines. If the lenghts are the same then the intersection point is actually on the line segment. Quod erat demonstrandum.

The code also checks to see if the two lines are parallel using the dot product of the two vectors. Considering that I’m actually fairly inept at math I feel like I’ve scored a minor victory.

Code - lineIntersect.pde

Read the rest of this entry »

7 Comments »

JDIC embedded browser engine

Screenshot of IE running integrated with Processing and other AWT components.

For a while I’ve been wondering if it couldn’t be useful to be able to render proper HTML from inside Processing. While Processing is excellent for realtime graphics, the typographic support is a little basic when faced with the task of designing a more complex layout. Rather than write a new library, why not just use a fast HTML engine?

While there are quite a few HTML rendering engines out there, not all are very complete or indeed very fast. This might be one of the cases where using native code makes sense. Both Internet Explorer and Mozilla offer ways of embedding their rendering engines through native bindings, and after a little googling I was able to find JDIC - the JDesktop Integration Components project.

JDIC aims to bring Java applications closer to feeling like “real” desktop apps. I’m not sure if that’s a battle I would have taken on myself, but in any case they have exactly what I needed: A simplified web browser ready for embedding, able to use either IE or Mozilla as engines. A little coding later and I had a hybrid Processing / AWT application running a web browser. It will even support Flash and Java content, provided that you’ve installed the proper plugins.

The WebBrowser.setContent() function is perfect for loading your own machine-generated content, and events can be captured and processed appropriately. It would even be possible to have a hybrid application, with part of the interface being straight Processing and the rest AWT. I’m keen to try using this to create more complex on-screen layouts. HTML and CSS will always look much better than anything one could create using Swing.

See Flickr for some screenshots.

Code - JDICsample.pde

Read the rest of this entry »

2 Comments »

For two joined bezier curves to be continuous (i.e. no corners or abrupt slope changes), the control points on each side of the joining point must be colinear. This behavior is default in vector editing programs like Adobe Illustrator.

An example in Processing:

// BezierJoins.pde
// Marius Watz - http://workshop.evolutionzone.com

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

void draw() {
  background(200);
  noFill();

  float xD=width/2-mouseX;
  float yD=height/2-mouseY;  

  bezier(0,0,
    300,100,
    width/2-xD,height/2-yD,
    width/2,height/2);

  bezier(width/2,height/2,
    width/2+xD,height/2+yD,
    400,300,
    400,400);
}

1 Comment »

[Merz Akademie workshop] Here is the code I wrote to create a 3D mesh by rotating a set of 2D points:

// 3dmesh.pde
// Marius Watz - http://workshop.evolutionzone.com

import processing.opengl.*;

int numrot;
float px[],py[],mesh[][][];

void setup() {
  size(700,700, OPENGL);

  // To create a mesh, first create two arrays to hold the
  // X and Y coordinates of the path you will use to create it
  px=new float[20];
  py=new float[20];
  float t=0;
  for(int i=0; i< px.length; i++) {
    px[i]=bezierPoint(0,130, 130, 0, t);
    py[i]=bezierPoint(450,350,150,50, t);
    t+=(1.0/(float)(px.length-1));
  }

  // use createMesh to create the mesh. createMesh takes three parameters:
  // num == number of rotations
  // startDeg == start degree of rotation
  // endDeg == end degree of rotation

  mesh=createMesh(px,py,20, -60,60);
}

void draw() {

translate(width/2,height/2);
  pushMatrix();

  background(0);
  lights();

  rotateY(radians(frameCount));
  rotateX(radians(frameCount)/2);

  stroke(255);
  noStroke();
  fill(255,120,0);

  translate(0,0);
  for(int i=0; i< 6; i++) {
    rotateZ(radians(60));
    pushMatrix();
    rotateX(radians(-60));

    // to draw the mesh, use drawMesh and pass your mesh variable to it
    drawMesh(mesh);
    popMatrix();
  }

  popMatrix();
}

float [][][] createMesh(float px[],float py[],int numrot,
  float startDeg,float endDeg) {

  float deg,x,z;
  double cosval,sinval,tmp1,tmp2;

  float [][][] mesh=new float[numrot][px.length][3];
  endDeg-=startDeg; 

  int meshindex=0;
  for(int i=0; i< numrot; i++) {
    deg=radians(startDeg+(endDeg/(float)(numrot-1))*(float)i);
    for(int j=0; j< px.length; j++) {
      x=px[j];
      z=0;
      cosval=Math.cos(deg);
      sinval=Math.sin(deg);
      tmp1=x*cosval - z*sinval;
      tmp2=x*sinval + z*cosval;
      mesh[i][j][0]=(float)tmp1;
      mesh[i][j][1]=py[j];
      mesh[i][j][2]=(float)tmp2;
    }
  }

  return mesh;
}

void drawMesh(float mesh[][][]) {
  println(mesh.length+" "+mesh[0].length+" "+mesh[0][0].length);
  for(int i=0; i< mesh.length-1; i++) {
    beginShape(QUAD_STRIP);
    for(int j=0; j< mesh[0].length; j++) {
      vertex(mesh[i][j][0],mesh[i][j][1],mesh[i][j][2]);
      vertex(mesh[i+1][j][0],mesh[i+1][j][1],mesh[i+1][j][2]);
    }
    endShape();
  }
}

1 Comment »

I’ve uploaded some demo images generated with the aTileSaver class to my Flickr account. They show images at reduced size and at 100%. Make sure to look at the original sizes for the full effect.

As should be expected, OpenGL doesn’t give quite the crispness that a raytracer or similar production tool might yield, but the sheer resolution should be enough to compensate. I have uploaded a 6400 x 6400 pixel JPG (RandBox 0023) as an example, but annoyingly Flickr isn’t able to create a thumbnail for it because of the large size. Go directly to the original size for a closeup view.

Read the rest of this entry »

No Comments »

070324_tilesaver.gif

High-resolution images from Processing using tiling (1.6% / 20%)

A while back I posted about rendering very high-resolution images from Processing using a tiling technique. I had implemented a working version of a solution first described by "surelyyoujest", but didn’t have time to post a clean version of the code. (I did however post ImageStitcher.pde, which is code for stitching these tiled images back together.) But after a long delay, here is finally the more useful bit of code.

The code works by panning the camera over the original viewport area, subdividing the image into tiles. This way, OpenGL’s limitations on maximum resolution can be circumvented. As long as enough memory is allocated, the images created can be very big indeed. The images shown above are from a 15360 x 15360 pixel image, shown at 1.6% and 20% respectively. With 1.5 GB assigned to Java I have so far successfully saved 20k x 20k images. That’s large enough to print 2×2 meter prints at 260 DPI.

What follows is the aTileSaver class and a simple demo application. I will post a more complex example soon.

Update: I’ve changed a few minor details in the code. Make note of the version number if you have download it already. This code is likely to change..

Source code - aTileSaver.pde

Read the rest of this entry »

26 Comments »