Sound 6: Rhino Data to Processing

The above grasshopper definition exports three dimensional coordinates for geometry to be imported into processing. You can download the definition here: cell_export.gh

The processing sketch below imports the three dimensional coordinates from the definition above and redraws them in P3D.

ArrayList <PVector> pStart = new ArrayList<PVector>();
ArrayList <PVector> pEnd = new ArrayList<PVector>();


void setup() {
  size(1400, 1000,P3D);

  frameRate(30);

  // Load text file as a string
  String[] spts = loadStrings("startpoints.txt");
  // Convert string into an array of integers using ',' as a delimiter
 
  for(int i = 0; i < (spts.length/3); ++i){
    float xx = float(spts[i*3])*40;
    float yy = float(spts[i*3+1])*40;
    float zz = float(spts[i*3+2])*40;
    pStart.add(new PVector(xx,yy,zz));
  }
  

  String[] spts2 = loadStrings("endpoints.txt");
  for(int i = 0; i < (spts2.length/3); ++i){
    float xx = float(spts2[i*3])*40;
    float yy = float(spts2[i*3+1])*40;
    float zz = float(spts2[i*3+2])*40;
    pEnd.add(new PVector(xx,yy,zz));
  }
}

void draw() {
  background(0);
  translate(width/2,height/2);
  for(int i = 0; i < (pStart.size()); ++i){
    PVector vs =  pStart.get(i);
    PVector ve =  pEnd.get(i);
    stroke(255);
    line(vs.x, vs.y, vs.z, ve.x, ve.y, ve.z);
  }
}

The sketch below adds camera functionality.

ArrayList <PVector> pStart = new ArrayList<PVector>();
ArrayList <PVector> pEnd = new ArrayList<PVector>();

int oldx = mouseX;
int oldy = mouseY;
float rotx = PI;
float roty = 0;
float zcam = 0;
int initialize = 0;


void setup() {
  size(1400, 1000,P3D);

  frameRate(30);

  // Load text file as a string
  String[] spts = loadStrings("startpoints.txt");
  // Convert string into an array of integers using ',' as a delimiter
 
  for(int i = 0; i < (spts.length/3); ++i){
    float xx = float(spts[i*3])*40;
    float yy = float(spts[i*3+1])*40;
    float zz = float(spts[i*3+2])*40;
    pStart.add(new PVector(xx,yy,zz));
  }
  

  String[] spts2 = loadStrings("endpoints.txt");
  for(int i = 0; i < (spts2.length/3); ++i){
    float xx = float(spts2[i*3])*40;
    float yy = float(spts2[i*3+1])*40;
    float zz = float(spts2[i*3+2])*40;
    pEnd.add(new PVector(xx,yy,zz));
  }
}

void draw() {
  background(0);

  ///CAMERA//////////////////
  pushMatrix();
 
  cam();
  ///END CAMERA//////////////////
 
  
  for(int i = 0; i < (pStart.size()); ++i){ 
    PVector vs = pStart.get(i); 
    PVector ve = pEnd.get(i); 
    stroke(255); 
    line(vs.x, vs.y, vs.z, ve.x, ve.y, ve.z); 
  } 
  popMatrix(); 
} 

void cam() { 
  int newx = mouseX; 
  int newy = mouseY; 
  translate(width/2, height/2,zcam); 
  rotateY(rotx); 
  rotateX(roty); 
  translate(0,0,0); 
  //rotateZ(PI); 
if ((mousePressed == true) && (mouseY > 200) ) {
    rotx = rotx + (oldx-newx)/50.0;
    roty = roty + (oldy-newy)/50.0;
  }
  
  if(initialize == 0){
     rotx = rotx + (oldx-newx)/50.0;
    roty = roty + (oldy-newy)/50.0;
    initialize = 1;
  }
  oldx = newx;
  oldy = newy;
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  zcam = zcam - e*5;
}

import ddf.minim.*;
import ddf.minim.analysis.*;
import controlP5.*;
ControlP5 cp5;
Minim minim;
AudioInput in;

int lifespan = 25;
int rannum = 20;
float rate = 1;
float threshold = 0.5;
float time = 100;
float amplitude = 100;

boolean Random = false;
boolean trail = true;
boolean Pulse = false;
boolean multiple = false;

float trailcount = 20000;

ArrayList <PVector> pStart = new ArrayList<PVector>();
ArrayList <PVector> pEnd = new ArrayList<PVector>();
ArrayList <PVector> light = new ArrayList<PVector>();

int[] trailseed = new int[20];
float[] trailtime = new float[20];

int tcount = 0;

FloatList chpts;

FloatList on;

float x,y,z;

float scroll;

float radius;

int spannum;

float rh = 0;
int lastmy;
float pdist = 1000;

int pcenter;

int oldx = mouseX;
int oldy = mouseY;
float rotx = PI;
float roty = 0;
float zcam = 0;
int initialize = 0;

void setup() {
size(1400, 1000,P3D);

frameRate(30);

minim = new Minim(this);

// use the getLineIn method of the Minim object to get an AudioInput
in = minim.getLineIn();

radius = 0;

x = width/2;
y = height/2;
z = 0;

on = new FloatList();

// Load text file as a string
String[] spts = loadStrings("startpoints.txt");
// Convert string into an array of integers using ',' as a delimiter

for(int i = 0; i < (spts.length/3); ++i){
float xx = float(spts[i*3])*40;
float yy = float(spts[i*3+1])*40;
float zz = float(spts[i*3+2])*40;
pStart.add(new PVector(xx,yy,zz));
on.append(0.0);
}

String[] spts2 = loadStrings("endpoints.txt");
for(int i = 0; i < (spts2.length/3); ++i){
float xx = float(spts2[i*3])*40;
float yy = float(spts2[i*3+1])*40;
float zz = float(spts2[i*3+2])*40;
pEnd.add(new PVector(xx,yy,zz));
}

for(int j = 0; j < (trailseed.length); ++j){
trailseed[j] = 0;
trailtime[j] = 0;
light.add(pEnd.get(j));
}

trailtime[0] = 2000;

cp5 = new ControlP5(this);

cp5.addSlider("lifespan")
.setPosition(300,40)
.setRange(0,100)
.setSize(240,20)
.setColorForeground(color(20,200,200))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(color(0,0,0))
.setColorActive(color(0,255,255))
;

cp5.addSlider("rate")
.setPosition(300,80)
.setRange(0.0,10.0)
.setSize(240,20)
.setColorForeground(color(20,200,200))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(color(0,0,0))
.setColorActive(color(0,255,255))
;

cp5.addSlider("time")
.setPosition(680,40)
.setRange(0.0,100.0)
.setSize(160,20)
.setColorForeground(color(20,200,200))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(color(0,0,0))
.setColorActive(color(0,255,255))
;

cp5.addSlider("rannum")
.setPosition(640,80)
.setRange(0.0,100.0)
.setSize(200,20)
.setColorForeground(color(20,200,200))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(color(0,0,0))
.setColorActive(color(0,255,255))
;

cp5.addSlider("amplitude")
.setPosition(40,40)
.setRange(0,200)
.setSize(200,20)
.setColorForeground(color(20,200,200))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(color(0,0,0))
.setColorActive(color(0,255,255))
;

cp5.addSlider("threshold")
.setPosition(40,80)
.setRange(0.0,1.0)
.setSize(200,20)
.setColorForeground(color(20,200,200))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(color(0,0,0))
.setColorActive(color(0,255,255))
;

cp5.addToggle("trail")
.setPosition(600,40)
.setSize(20,20)
.setColorForeground(color(20,20,20))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(0xffff88ff)
.setColorActive(color(0,200,200))
;

cp5.addToggle("multiple")
.setPosition(640,40)
.setSize(20,20)
.setColorForeground(color(20,20,20))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(0xffff88ff)
.setColorActive(color(0,200,200))
;

cp5.addToggle("Random")
.setPosition(600,80)
.setSize(20,20)
.setColorForeground(color(20,20,20))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(0xffff88ff)
.setColorActive(color(0,200,200))
;

cp5.addToggle("Pulse")
.setPosition(600,120)
.setSize(20,20)
.setColorForeground(color(20,20,20))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(0xffff88ff)
.setColorActive(color(0,200,200))
;

}

void draw() {
background(0);

///GET SOUND/////////////

float sd = 0;
float tnum = threshold * 200;
for(int i = 0; i < in.bufferSize(); i++){ if(abs(in.mix.get(i)) > sd){
sd = abs(in.mix.get(i));
}
}
sd = sd*2*amplitude;

///////////////////////////

if((sd > tnum)){
if(tcount == 19){
tcount = 0;
}else{
tcount++;
}
trailtime[tcount] = 2000;
trailseed[tcount] = int(random(pStart.size()));
}
if(multiple == false){
tcount = 0;
}

if(Pulse == true){
if((sd > tnum)){
pcenter = int(random(pStart.size()));
pdist = 0;
}
}

pdist = pdist + 10;
///SOUND BAR VISUALISER////

noStroke();
fill(color(70,70,70));
rect(40, 120, 60, 200);
fill(color(20,200,200));
rect(40, 320, 60, -sd);
stroke(255);
strokeWeight(1);
line(110,320-tnum,120,320-tnum);
////////////////////////////

///CAMERA//////////////////
pushMatrix();

cam();
///END CAMERA//////////////////

for(int i = 0; i < (pStart.size()); ++i){ 
  float r = random(50); 
  PVector vs = pStart.get(i); 
  PVector ve = pEnd.get(i); 

  ///02. RANDOM////////////// 
  if(Random == true){ if(sd > tnum){
    float ro = random(100);
    if(ro < rannum){
      on.set(i,lifespan);
    }
  }
}

///END RANDOM//////////////

/////03. PULSE//////////////////

if((Pulse == true)){
  PVector vc = pStart.get(pcenter);
  float vdist = dist(vs.x,vs.y,vs.z,vc.x,vc.y,vc.z);
  float dif = vdist - pdist;
  if((dif < 10) && (dif > -10)){
    on.set(i,lifespan);
  }
}

/////PULSE END//////////////

for(int c = 0; c < (trailseed.length); ++c){ 
  if((trail == true) && (trailtime[c]> 0)){
    if( i == trailseed[c]){
      on.set(i,lifespan);
      float stn = random(50);
      if(stn > 25){
        light.set(c,vs);
      }else{
        light.set(c,ve);
      }
    }
   }
}

float onoff = on.get(i);

if( onoff > 0){
  stroke(0,255,255);
  if((onoff/6 < 1) && (onoff/6 > 0)){
    strokeWeight(1);
   }else{
     strokeWeight(int(onoff/6));
   }
 }else{
   stroke(100);
   strokeWeight(1);
}

onoff = on.get(i);
on.set(i,(onoff-rate));

noFill();

line(vs.x, vs.y, vs.z, ve.x, ve.y, ve.z);

}

for(int n = 0; n < (trailseed.length); ++n){

if((time == 100) && (n == 0)){
  trailtime[n] = 2000;
}else{
  if((trailtime[n]-(100-time)) < 0){
    trailtime[n] = 0;
  }else{
    trailtime[n] = trailtime[n] - (100-time);
  }
}

chpts = new FloatList();
int pcount = 0;
for(int i = 0; i < (pStart.size()); ++i){ 
  PVector vs = pStart.get(i); 
  PVector ve = pEnd.get(i); 
  PVector lc = light.get(n); 
  PVector ivs = new PVector(int(vs.x), int(vs.y), int(vs.z)); 
  PVector ive = new PVector(int(ve.x), int(ve.y), int(ve.z)); 
  PVector itest = new PVector(int(lc.x), int(lc.y), int(lc.z)); int ch = 0; 
  if( (ivs.x == itest.x) || (ive.x == itest.x) ){ ch++; } 
    if( (ivs.y == itest.y) || (ive.y == itest.y) ){ 
      ch++; 
    } if( (ivs.z == itest.z) || (ive.z == itest.z) ){ 
      ch++; 
    } float onoff = on.get(i); 
    if((ch == 3) && (i != spannum)){ 
      chpts.append(i); pcount++; 
    } 
   } 
int ci = int(random(chpts.size())); 
trailseed[n] = int(chpts.get(ci)); 
} 
popMatrix();
} 

void cam() { 
  int newx = mouseX; int newy = mouseY; 
  translate(width/2, height/2,zcam); 
  rotateY(rotx); rotateX(roty); 
  translate(0,0,0); //rotateZ(PI); 
  if ((mousePressed == true) && (mouseY > 200) ) {
  rotx = rotx + (oldx-newx)/50.0;
  roty = roty + (oldy-newy)/50.0;
}

if(initialize == 0){
rotx = rotx + (oldx-newx)/50.0;
roty = roty + (oldy-newy)/50.0;
initialize = 1;
}
oldx = newx;
oldy = newy;
}

void mouseWheel(MouseEvent event) {
float e = event.getCount();
zcam = zcam - e*5;
}