We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › noob seek. optimization help for 3d dataviz graph
Page Index Toggle Pages: 1
noob seek. optimization help for 3d dataviz graph (Read 594 times)
noob seek. optimization help for 3d dataviz graph
Oct 26th, 2008, 1:14am
 
Hi

I have been working on this 3d graph on and off over the last little while hacking and tacking bits and pieces together from different places and I need some help structuring the procedural flows...I'm not a computer scientist so please bare with me.

I have a table of data that is being parsed into a series of ellipses representing lines of data - each ellipse is being placed somewhere in 3d space based on values read from the table. I finally have it working the way I want but the performance is so slow that it is practically useless at this point.

I am pretty sure it could be cleaned up but I wouldnt know where to begin. That's where I suppose you come in...I find that  things break down around the reading and parsing of the data - before that everything is real smooth. Hit 'x' 'y' or 'z' to see table indices, left mouse button for camera tumble and right for zoom. there are sliders to control the overall dimensions of the graph, which in turn changes the incremental value between indices.

if you comment out the call to the drawAnimals(); function within draw you will see the vast improvement in performance....

can't export for some reason, here's a zip w/the necessary files:
www.upsdn.ca/processing/VD_ch04_animals02.zip

thanks so much, this is an immense help.
g
Re: noob seek. optimization help for 3d dataviz gr
Reply #1 - Oct 26th, 2008, 1:16am
 
main sketch pt1:

import processing.opengl.*;
import damkjer.ocd.*;

import controlP5.*;

ControlP5 controlP5;
ControlWindow controlWindow;

Camera camera1;

Table animalValuesTable;
int rowCount;
int columnCount;

PFont plotFont;
int minX, minY, minZ;

public int maxX = 1000;
public int maxY = 1000;
public int maxZ = 1000;

float labelX, labelY;

String diet;
String climate;
String special;

int dietIndex;
int climateIndex;
int specialIndex;

HashMap dietIndices;
HashMap climateIndices;
HashMap specialIndices;

String[] dietTypes= { "carnivore", "piscivores" , "omnivore" , "sanguinivores" , "saprovore" , "insectivore",  "nectarivore", "herbivore"};
String[] climateTypes= {  "Desert", "Tropical Rainforest", "Savanna", "Wetlands", "Grassland", "Temperate Forest","Tundra" };
String[] specialTypes = { "air", "trees", "ground", "underground", "water"};

int w = 1200;
int h = 800;

// visibility of line
boolean xvisible = false;
boolean yvisible = false;
boolean zvisible = false;

// prevent uncontrollable toggle
boolean keyreset = true;

void setup(){
   size(w, h, OPENGL);
   
   controlP5 = new ControlP5(this);
   controlP5.setAutoDraw(false);
 
   controlWindow = controlP5.addControlWindow("controlP5window",100,100,400,200);
   controlWindow.setBackground(color(40));
 
   hint(ENABLE_OPENGL_4X_SMOOTH);
   hint(ENABLE_NATIVE_FONTS);
   
   //camera1 = new Camera(this);
   camera1 = new Camera(this, 1800, 1800, 1800, maxX, maxY, maxZ);
     
   plotFont = createFont("Swiss721BT-Light-12",12);
   textFont(plotFont);
   
   Controller xSlider = controlP5.addSlider("maxX",0,1000,180,40,100,10); //(name, start_value, end_value, xpos, ypos, slider_width, slider_height)
   xSlider.setWindow(controlWindow);
   Controller ySlider = controlP5.addSlider("maxY",0,1000,180,60,100,10);
   ySlider.setWindow(controlWindow);
   Controller zSlider = controlP5.addSlider("maxZ",0,1000,180,80,100,10);
   zSlider.setWindow(controlWindow);
 
}
   
void draw(){
 background(224);
 camera1.feed();
 
 animalValuesTable = new Table("animalValues-06.txt");
 rowCount = animalValuesTable.getRowCount();
 
 textFont(plotFont);
 fill(255);
 rectMode(CORNERS);
 noStroke();
 rect(minX,minY,maxX,maxY);
 
 setupDiets();
 setupClimates();
 setupSpecials();
 setupGrid();
 drawAnimals();
 
 smooth();
}
Re: noob seek. optimization help for 3d dataviz gr
Reply #2 - Oct 26th, 2008, 1:17am
 
main sketch pt 2:
void setupGrid(){
 
 if (keyPressed && key == 'x' && keyreset == true) {
   // got first signal
   keyreset = false;
   if(xvisible == false) {
     xvisible = true;  
   }  
   else {
    xvisible = false;  
   }
 }
 
 if (keyPressed && key == 'y' && keyreset == true) {
   // got first signal
   keyreset = false;
   if(yvisible == false) {
     yvisible = true;  
   }  
   else {
    yvisible = false;  
   }
 }
  if (keyPressed && key == 'z' && keyreset == true) {
   // got first signal
   keyreset = false;
   if(zvisible == false) {
     zvisible = true;  
   }  
   else {
    zvisible = false;  
   }
 }
 int numX = dietTypes.length+1;
 int numY = climateTypes.length+1;
 int numZ = specialTypes.length+1;
 
 int incX = (maxX-minX)/numX;
 int incY = (maxY-minY)/numY;
 int incZ = (maxZ-minZ)/numZ;
 
 for (int k =0; k < numZ+1; k++) { //for every plane along the Z axis
 // draw the line only if set to "visible"
 if(xvisible == true) {
   stroke(0,255,0);
  //draw a a series of lines along the X axis
   for (int i = 0; i < numX+1; i++) { //hor. direction
     line(minX + i * incX,   minY,   minZ + k * incZ,     minX + i * incX,   maxY,   minZ + k * incZ);
   }
 }
 if(yvisible == true) {
   stroke(0,0,255);
   //draw a a series of lines along the Y axis
   for (int j = 0; j < numY+1; j++) { //=vert.direction
     line(minX,   minY + j * incY,   minZ + k * incZ, maxX,   minY + j * incY,   minZ + k * incZ);
   }
 }
 }
 if(zvisible == true) {
 stroke(255,0,0);
 for (int i = 0; i < numX+1; i++) {
   for (int j= 0; j < numY+1; j++) {
     line(minX + i * incX,   minY + j * incY,   minZ,  minX + i * incX,   minY + j * incY,  maxZ);
   }
 }
 }
 
}

void drawAnimals (){
 for (int row=1; row<rowCount; row++) {
     float weight = animalValuesTable.getFloat(row,1);
     String diet = animalValuesTable.getString(row,15);
     String climate = animalValuesTable.getString(row,4);
     String common = animalValuesTable.getString(row,0);
     String special = animalValuesTable.getString(row,10);
     
     int y = dietIndex(diet);
     float yfloat = y;
     yfloat = map(y, 0, dietTypes.length-1, minY, maxY);
     
     int x = climateIndex(climate);
     float xfloat = x;
     xfloat = map(x, 0, climateTypes.length-1, minX, maxZ);
     
     int z = specialIndex(special);
     float zfloat = z;
     zfloat = map(z, 0 , specialTypes.length-1, minZ, maxZ);
     
   
     drawData(xfloat, yfloat, zfloat, common, diet, weight);
     //drawCylinder(x, y, random(0, 100) , weight, weight, 50, 12);
   }
}
 

void drawData (float x, float y, float z, String common, String diet, float weight) {
 pushMatrix();
 translate(0,0,z);
 if (diet.equals("carnivore")){
     fill(#FF5166);
     }
 else if (diet.equals("herbivore")) {
     fill(#EC5166);
   } else {
     fill(#CC5166);
   }
   
    ellipse(x,y, weight/8,weight/8);
   
   //println(diet);
   fill(0);
   PFont font = loadFont("Swiss721BT-Light-12.vlw");
   
   textFont(font);
   //textSize(24);
   text(common, x, y-weight/16);
   popMatrix();
}
Re: noob seek. optimization help for 3d dataviz gr
Reply #3 - Oct 26th, 2008, 1:18am
 
main sketch pt3:

//////////////////////////
// MOUSE and KEY EVENTS //
//////////////////////////

void mouseDragged()
{
 if ( mouseButton == LEFT )
 {
   camera1.tumble(radians(mouseX - pmouseX), radians(mouseY - pmouseY));
 }
 if (/* keyPressed && key == 'z' &&*/ mouseButton == RIGHT )
 {
   camera1.zoom(radians(mouseY - pmouseY)/2);
 }
 if ( keyPressed && key == 'd' && mouseButton == RIGHT )
 {
   camera1.dolly(radians(mouseY - pmouseY)/2);
 }
}
// check if the button has been released
void keyReleased() {
 keyreset = true;  
}


////////////////////////////
// SETUP MAPS and INDICES //
////////////////////////////

void setupDiets() {
 dietIndices = new HashMap();
 for (int i = 0; i <  dietTypes.length; i++) {
   dietIndices.put(dietTypes[i], new Integer(i));    
 }
}

int dietIndex(String diet) {
 Integer index = (Integer) dietIndices.get(diet);
 return index.intValue();
}

void setupClimates() {
 climateIndices = new HashMap();
 
 for (int i = 0; i <  climateTypes.length; i++) {
   climateIndices.put(climateTypes[i], new Integer(i));    
 }
}

int climateIndex(String climate) {
 Integer index = (Integer) climateIndices.get(climate);
 return index.intValue();
}

void setupSpecials() {
 specialIndices = new HashMap();
 
 for (int i = 0; i <  specialTypes.length; i++) {
   specialIndices.put(specialTypes[i], new Integer(i));    
 }
}

int specialIndex(String special) {
 Integer index = (Integer) specialIndices.get(special);
 return index.intValue();
}
Re: noob seek. optimization help for 3d dataviz gr
Reply #4 - Oct 26th, 2008, 1:38am
 
getting rid of these lines within the function seemd to help...

PFont font = loadFont("Swiss721BT-Light-12.vlw");
textFont(font);
Page Index Toggle Pages: 1