Best way to return multiple column array

edited October 2016 in Programming Questions

Hi

What is the best way to return <Int, Float, Float> values from a method using an array? I am trying to make a fiducial class which detects the x and y location of the fiducials identified using an ID.

I am new to processing and trying to get a hang of it. Thanks in advance.

Regards

S

Answers

  • Don't use an array. Create a class that contains these values and return an instance of that instead.

    You could also use an array of Numbers.

  • edited October 2016

    Java got 8 primitive types. And unfortunately, if we need to return all of those primitive types, we have to create a function for each of them: #:-S

    http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    void setup() {
      char[] ch = createCharArr(5);
      long[] lg = createLongArr(7);
      exit();
    }
    
    static final boolean[] createBoolArr(int len) {
      return new boolean[len];
    }
    
    static final byte[] createByteArr(int len) {
      return new byte[len];
    }
    
    static final short[] createShortArr(int len) {
      return new short[len];
    }
    
    static final char[] createCharArr(int len) {
      return new char[len];
    }
    
    static final int[] createIntArr(int len) {
      return new int[len];
    }
    
    static final long[] createLongArr(int len) {
      return new long[len];
    }
    
    static final float[] createFloatArr(int len) {
      return new float[len];
    }
    
    static final double[] createDoubleArr(int len) {
      return new double[len];
    }
    
  • edited October 2016 Answer ✓

    As mentioned, create a class to encapsulate those 3 values w/ diff. datatypes: *-:)

    // forum.Processing.org/two/discussion/18704/
    // best-way-to-return-multiple-column-array#Item_3
    
    // GoToLoop (2016-Oct-25)
    
    void setup() {
      final Trio t = new Trio();
      println(t);
    
      t.set(MAX_INT, THIRD_PI, 1e3);
      println(t);
    
      t.f2 *= 1e-2;
      println(t.f2);
    
      t.i /= t.f1;
      println(t.i);
    
      println(t);
      exit();
    }
    
    static class Trio {
      int i;
      float f1, f2;
    
      Trio() {
      }
    
      Trio(int i, float f1, float f2) {
        set(i, f1, f2);
      }
    
      Trio set(int _i, float _f1, float _f2) {
        i  = _i;
        f1 = _f1;
        f2 = _f2;
        return this;
      }
    
      @ Override String toString() {
        return "[ " + i + ", " + f1 + ", " + f2 + " ]";
      }
    }
    
  • Trio myFunction() {
    
    }
    
  • Hi

    Thanks guys. For now i am taking a simpler approach. Here is the code.

    class Fiducial {
    
      //Change the Fiducial Marker numbers to the ones which needs to be detected. 
      //BoofCV returns false positives sometimes. 
    
      int[] fiducialsToDetect = {284, 643};
    
      Fiducial() {
      }
    
      float[] trackFiducialLocation(int id) {
    
        float[] xy = new float[2];
    
        List<FiducialFound> found = detector.detect(cam);
    
        for ( FiducialFound f : found ) {
          if (f.getId() == id) {
            xy[0] = (float) f.getImageLocation().getX();
            xy[1] = (float) f.getImageLocation().getY();
          }
          break;
        }
        return xy;
      }
    
      IntList detectAvailableFiducials() {
    
        // Detects available fiducials on the camera. 
        // Note that calling this method might return zero values because the fiducial might not be in the line of vision.
        // The return values must be checked and appended.
        // Edit int[] fiducialsToDetect variable to edit list of expected fiducial IDs.
    
        IntList d = new IntList();
        List<FiducialFound> found = detector.detect(cam);
        for ( FiducialFound f : found ) {
          int id = (int) f.getId();
          if (!d.hasValue(id)) {
            for (int i = 0; i<fiducialsToDetect.length; i++) {
              if (id==fiducialsToDetect[i]) {
                d.append(id);
              }
            }
          }
        }
        d.sort();
        if (debug) println(d);
        return d;
      }
    }
    

    Thanks

    S

  • Glad you found a solution that works for you.

    If you are trying to return 2 or 3 float values that relate to xyz coordinates or offsets (e.g. your float[] xy), an easy way is creating and returning a PVector object, then accessing the x y z properties. PVector is idiomatic to Processing and the class comes with a number of convenience methods.

    void draw () {
      PVector fid = trackFiducialLocation();
      println(fid.x, fid.y);
    }
    
    PVector trackFiducialLocation () {
      return new PVector(10,20);
    }
    
Sign In or Register to comment.