Writing data from .tsv to buffer

edited October 2013 in How To...

Hi

I'm new to processing and programming overall but i'm gradually learning it. I have a project where I have a tsv file with around 122k rows of data. X and Y coordinates, dates and one other column to mark the color - table has been ordered by date. I also have a slider imported from ControlP5 library to control the number of rows to draw on screen. I have managed to find a workaround so I wouldn't need to draw ALL the ellipses every time I move the slider (except moving backwards with slider). Now what if would want to use scale() function to make it available for user to zoom in? I couldn't get it working so I thought if I would write the code so It would loop endlessly and always draw the rows from scratch, then the zoom would work but then again the whole sctech gets really slow.

Would it be better to write these coordinates into buffer somehow?

Really hard to explain but here'smy code, maybe it'll help a bit.

SORRY but I couldn't find a proper way to paste the code here...

import controlP5.*;


ControlP5 cp5;
Textlabel textlabel;

Table mpInfo;
int rowCount;
int currentRow = 0; // current value of slider
PFont f;
String date;
color fillColor;

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

  background(0);
  mpInfo = new Table("map.tsv");
  rowCount = mpInfo.getRowCount();

  //creating slider
  cp5 = new ControlP5(this);
  cp5.addSlider("timeline")
    .setPosition(20,20)
    .setSize(600,20)
    .setRange(0,rowCount)
    .setValue(currentRow);

  f = createFont("Verdana",24,true);

  for (int row = 0; row < rowCount; row++) {
    //Lat is Y and Long is X, so Y is actually 1st column in the file and X is the 2nd
    float x_koord = mpInfo.getFloat(row, 1);
    float y_koord = mpInfo.getFloat(row, 0);
    mpInfo.setFloat(row, 0, map(x_koord, 21.870, 27.820, 50, 850));
    mpInfo.setFloat(row, 1, map(y_koord, 57.551, 59.658, 540, 100)); //Note the last 2 map params, if these are in other order, map gets inverted
  }   

  smooth();
  noStroke(); 
}

void draw() {
    if (currentRow > 0) {
      fill(0); // Draw background box beneath the text
      rect(30,50, 320, 60);
      textFont(f,24);
      fill(255);
      //println(date);
      text(date, 30, 80);
      text("Installed meters: "+currentRow, 30, 110);
  } else {
      fill(0);
      rect(30,50, 250, 60);
      textFont(f,24);
      fill(255);
      //println(date);
      text("Move timeline slider", 30, 80);
  }
  fill(#1c3858); // box fill for legend background
  rect(30,550, 250, 105, 10); // box for legend
  fill(#FFCC00); // box fill for PLC marking
  rect(40, 560, 20,20); // PLC marking box
  fill(255); // fill color for descriptive text
  textFont(f,18);
  text("PLC", 70, 575); 
  fill(#009933); // box fill for P2P marking
  rect(40, 600, 20,20); // P2P marking box
  fill(255); // fill color for descriptive text
  textFont(f,18);
  text("P2P", 70, 615);
  text("Press \"Spacebar\" to reset", 40, 645);

}

void doDraw(int from, int to) {
  for (int row = from; row < to; row++) {
    int plc_vs_p2p = mpInfo.getInt(row, 3);
    if (plc_vs_p2p == 1) { //1 = PLC Yellow
       fillColor = #FFCC00;
    } else { // 2 = P2P Green
       fillColor = #009933;
    }
    fill(fillColor);
    ellipse(mpInfo.getFloat(row,0), mpInfo.getFloat(row,1), 1, 1);
    date = mpInfo.getString(row,2);
  }
}

void timeline(int rowNumber) {
  if(currentRow != rowNumber){
     //Optimization: Just draw the dots that need to be added, not from zero
     if(rowNumber > currentRow) {
       doDraw(currentRow, rowNumber); 
       currentRow=rowNumber;
     } else {
       //Clear and redraw. Not too effective, but no better ideas.
       background(0);
       doDraw(0, rowNumber); 
       currentRow=rowNumber;  
     }
  }
}

void keyPressed() {
    if (key == ' ') {
      setup();
    }

}

Answers

  • edited October 2013

    To format a pasted code, merely highlight it and then press CTRL+K. That'll do the 4 needed space indentation for you! %%-

  • thanks. the problem is still there. Is it even possible to pan/zoom without drawing everything from the scratch every time i move the canvas?

  • The base behavior of Processing is to redraw everything on each frame...

Sign In or Register to comment.