Problem with getZ() with the official LeapMotion Library

edited October 2014 in Library Questions

Hello, I make a little code to show the fingers with the LeapMotion. I think my code is right, but there's reactivity problem with the depth (>getZ()) when I make straight move on the Z axis the refresh is very very slow. How to improve the Z axis reactivity ?

FingerLeap f ;

void setup() {
  size(800,600, P3D) ;
  f = new FingerLeap() ;
  colorSetup() ;
}

void draw() {
  background(0) ;
  f.updateLeap() ;
  leapFingerSingleInfo() ;
}

//COLOR
color rouge, noir, blanc, gris ;

void colorSetup() {
  colorMode (HSB, 360,100,100) ;
  blanc = color(0,0,100);
  rouge = color(10,100,100);
  noir = color(10,100,0);
  gris = color(10,50,50);
}


// Function
void leapFingerSingleInfo() {
  if(f.fingerCheck) {
    for(int i = 0; i < f.num ; i++) {
      if(f.fingerVisible[i]) {
        //display info
        pushMatrix() ;
        PVector pos = new PVector(f.normPos[i].x*width, height -f.normPos[i].y*height, f.normPos[i].z *(width+height) -((width+height)/2)) ;
        translate(pos.x, pos.y, pos.z) ;
        rotateX(f.pitch[i] +0.7) ;
        rotateY(f.roll[i] +0.9) ;
        rotateZ(f.yaw[i] +0.9) ;

        fill(blanc) ;
        text("My name is " + f.ID[i], 0, 0) ;
        fill(rouge) ;
        text("my position is " + (int)pos.x + " / " + (int)pos.y + " / " + (int)pos.z, 0,12) ;
        text("my XXX oriantation " + f.pitch[i]+"°", 0,24) ;
        text("my YYY oriantation " + f.roll[i]+"°", 0,36) ;
        text("my ZZZ oriantation " + f.yaw[i]+"°", 0,48) ;
        String mag = String.format("%.5f", f.magnitude[i]) ;
        text("my magnitude " + mag, 0,60) ;
        popMatrix() ;
      }
    }
  }
}


// CLASS
import com.leapmotion.leap.*;
com.leapmotion.leap.Controller leap;


class FingerLeap {
  //global
  // Minimum value to accept finger in the count. The value must be between 0 to 1 ;
  float minValueToCountFinger = .9 ; 
  // range value around the first finger to accepted in the finger count 
  float rangeAroundTheFirstFinger = .2 ;
  //check if the fingers is present or no
  boolean fingerCheck ;
  boolean [] fingerVisible ;
  //for each finger
  int num ;
  int activefingers ;
  int [] ID ;
  int [] IDleap ;
  PVector [] normPos ;
  float [] magnitude, roll, pitch, yaw ;

  // average data
  PVector averageNormPos, averagePos ;

  FingerLeap() {
    leap = new Controller();
  }


  void updateLeap() {
    InteractionBox iBox = leap.frame().interactionBox();
    PointableList objectNum = leap.frame().pointables();
    //check finger
    fingerCheck = !objectNum.isEmpty() ;
    // create and init var ;
    num = objectNum.count() ;
    IDleap = new int[num] ;
    ID = new int[num] ;
    activefingers = 0 ;
    fingerVisible = new boolean [num] ;
    normPos = new PVector[num] ;
    magnitude = new float [num] ; 
    roll= new float [num] ; 
    pitch= new float [num] ; 
    yaw = new float [num] ;

    //calcul of the average pos
    PVector addPos = new PVector() ;


    // give info to variables for each finger display or not
    for(int i = 0; i < objectNum.count(); i++) {
      //initialization
      Pointable object = objectNum.get(i);
      Vector normalPos = iBox.normalizePoint(object.stabilizedTipPosition());

      // catch info
      IDleap[i] = object.id() ;
      ID[i] = i  ;
      // retunr value between 0 to 1 
      normPos[i] = new PVector(normalPos.getX(),normalPos.getY(),normalPos.getZ()) ;

      // misc value
      magnitude[i] = normalPos.magnitude() ; 
      roll[i] = normalPos.roll() ; 
      pitch[i] = normalPos.pitch() ; 
      yaw[i] = normalPos.yaw() ;


      // Check if the finger can be count or not
      // return the num of fingers active in the windows not the objectNum count who work with 5 base
      // first check to accept the first finger
      if(activefingers < 1) {
        if(normalPos.getZ() < minValueToCountFinger) {
          fingerVisible[i] = true ;
          // init the value and the finger num visible when we detected the first finger in the detection field of the leap motion.
          addPos = new PVector(normalPos.getX(),normalPos.getY(),normalPos.getZ()) ;
          activefingers += 1 ;
        }
      // if there is one finger, we compare if the others are close of the firsts fingers  
      } else {
        if(normalPos.getZ() < minValueToCountFinger) {
          // check the num of fingers, because if there is only finger we dion't need to divide the value by 2
          // after we divide by two because we've added a value at the end of this checking.
          if(activefingers > 1) addPos.div(2) ;
          // check if the finger detected is in the range or not, we must do that because the detection of the finger is not perfect and add finger when the hand is deepness in detector
          if (normalPos.getZ() > addPos.z -rangeAroundTheFirstFinger && normalPos.getZ() < addPos.z +rangeAroundTheFirstFinger) {
            fingerVisible[i] = true ;
            // if it's ok we add value
            addPos.add(normalPos.getX(),normalPos.getY(),normalPos.getZ()) ;
            // if it's ok we add a visible finger in the count
            activefingers += 1 ;
          }
        }
      }
    }

    /*
    Average position of all visible fingers
    Finalize the calcule, and dodge the bad value
    */
    if(addPos.x >= 1.0) addPos.x /= 2.0 ;
    if(addPos.y >= 1.0) addPos.y /= 2.0 ;
    if(addPos.z >= 1.0) addPos.z /= 2.0 ;
    averageNormPos = addPos.copy() ;
  }
}

```

Tagged:
Sign In or Register to comment.