Loading...
Logo
Processing Forum

Heat map optimisation

in Programming Questions  •  1 year ago  
Hi guys,

I wrote the below heat mapping function. It works reasonably well but sometimes gives me an out of memory error. I wonder is anyone has any suggestions for optimisation. I'm also controlling the fall off of the points a bit brutally with a multiplier (called fallOff). I wonder if this should also be some kind of logarithmic function? Cheers folks, hopefully this thread will expand a bit and we can get good solid simple solution!

cheers
Tom

PImage makeHeatMap(PVector [] dataPoints, float fallOff) {
  PImage timg=createImage(width, height, RGB);
  timg.loadPixels();

  loadPixels();
  dataPoints[0].x=mouseX;
  dataPoints[0].y=mouseY;
  //maxpossible distance between any 2 pixels is the diagonal distance across the screen
  float maxDist = sqrt((width*width)+(height*height));
  float heats[] =new float[pixels.length];
  int x=0;
  int y=0;
  for (int i=0;i<pixels.length;i++) {

    //get average distance to all points
    float averageDistance=0.0f;
    for (int j=0;j< dataPoints.length;j++) {
      float thisDist=dist(x, y, dataPoints[j].x, dataPoints[j].y)  ;
      //multiply the distance by the scaler 
      averageDistance+=thisDist*dataPoints[j].z;
      //float _hue =(1.0f/7.0f)* map(sqrt(thisDist), 0, sqrt(maxDist), 0, 255);
    }
    averageDistance/=dataPoints.length;

    float _hue = map (sqrt(averageDistance), 0, sqrt(maxDist)*fallOff, 0, 255);
    timg.pixels[i]= color(_hue, 255, 255);
    x++;
    if (x>=width) {
      x=0; 
      y++;
    }
  }
  timg.updatePixels();
  return timg;
}

Replies(3)

would be great if you could share the whole code. or at least a running version. otherwise its quite hard to see where the problem is without testing. 
Hi cedrickie,

thanks for your help. Whole sketch below (requires controlP5)

import controlP5.*;

PImage mapImage;
PFont font;
PVector [] _pointers;
float _fallOff = 1.55;
ControlP5 cp5;

void setup() {
  size(800, 600);
  colorMode(HSB);
  font =loadFont("AppleBraille-14.vlw");
  textFont (font,14);
  _pointers=new PVector[7];
  
  for (int i=0;i<_pointers.length;i++) {
    _pointers[i]=new PVector(random(width), random(height), random(10.0));
  }
  //mapImage=makeImage();
  cp5 = new ControlP5(this);
  cp5.addSlider("_fallOff").setPosition(100, 50).setRange(0.0, 2.0);
}

void draw() {

  mapImage=makeHeatMap(_pointers,_fallOff);
  image(mapImage, 0, 0);
  for (int i=0;i<_pointers.length;i++) {
   text(str(_pointers[i].z),_pointers[i].x, _pointers[i].y); 
  }
}

PImage makeHeatMap(PVector [] dataPoints, float fallOff) {
  PImage timg=createImage(width, height, RGB);
  timg.loadPixels();

  loadPixels();
  dataPoints[0].x=mouseX;
  dataPoints[0].y=mouseY;
  //maxpossible distance between any 2 pixels is the diagonal distance across the screen
  float maxDist = sqrt((width*width)+(height*height));
  float heats[] =new float[pixels.length];
  int x=0;
  int y=0;
  for (int i=0;i<pixels.length;i++) {

    //get average distance to all points
    float averageDistance=0.0f;
    for (int j=0;j< dataPoints.length;j++) {
      float thisDist=dist(x, y, dataPoints[j].x, dataPoints[j].y)  ;
     //multiply the distance by the scaler 
      averageDistance+=thisDist*dataPoints[j].z;
      //float _hue =(1.0f/7.0f)* map(sqrt(thisDist), 0, sqrt(maxDist), 0, 255);
    }
    averageDistance/=dataPoints.length;

    float _hue = map (sqrt(averageDistance), 0, sqrt(maxDist)*fallOff, 0, 255);
    timg.pixels[i]= color(_hue, 255, 255);
    x++;
    if (x>=width) {
      x=0; 
      y++;
    }
  }
  timg.updatePixels();
  return timg;
}

void controlEvent(ControlEvent theEvent) {


}

Hi All

I found a good solution (I think to this problem). I posted it here

Tom