How to extract values from a PVector?

please format code with </> button * homework policy * asking questions

I am using the leapmotion library and some values are coming in a PVector.
They print out fine, but when I put them in a string to be sent to Arduino I do get brackes and commas…

String HandOut = (“H”+ handPosition + " " + handRoll+" “+handPitch+” “+handPitch +” "+handPinch)
puts out
H[ 539.59766, 413.71576, 23.220325 ] -13.140116 49.55902 49.55902 0.76153696

This is very hard to parse in values on the Arduino side.
The "handPostion is defined as a Pvector and the rest are just floats.
PVector handStabilized = hand.getStabilizedPosition();

What to do?

Thanks a lot,
Mitch

You can create some function which replace() the undesirable characters w/ a common delimiter 1. :bulb:

For example the util function toTsvPVector() below go w/ dilimeter character '\t' :upside_down_face::

void setup() {
  final PVector vec = PVector.random3D(this).mult(100);
  println(vec);
  println(toTsvPVector(vec));
  exit();
}

static final String toTsvPVector(final PVector v) {
  return v.toString().replace("[ ", "").replace(" ]", "").replace(", ", "\t");
}

You can simply pick character TAB as the delimiter for all those 5 sent variables: :grinning:
final String tsv = toTsvPVector(hand.getStabilizedPosition()) + TAB + handRoll + TAB + handPitch + TAB + handPitch + TAB + handPitch + ENTER;

1 Like

yey, that worked.
Thanks a lot. Now I am faced with too many decimals. I could round the lone floats, but what to do about the vals inside the PVector?

the final String tsv = toTsvPVector(hand.getPosition()) + " "
+ round(handRoll) +" " + round(handPitch) + " " + round (handYaw) + " " + round (handPinch*100) + ‘\n’;
gives out:

985.884 371.61603 54.546585 2 41 -10 86

PS. Thanks for the TAB but I am using spacebar to delimitate.

You can choose whatever character as the separator delimiter as long as you stick to it:

void setup() {
  final PVector vec = PVector.random3D(this).mult(100);
  println(vec);
  println(toDsvPVector(vec));
  println(toDsvPVector(vec, " "));
  println(toDsvPVector(vec, ","));
  exit();
}

static final String toDsvPVector(final PVector v) {
  return toDsvPVector(v, "\t");
}

static final String toDsvPVector(final PVector v, final String delim) {
  return v.toString().replace("[ ", "").replace(" ]", "").replace(", ", delim);
}

You can’t change its 3 float primitive datatype values!
You’re gonna need another container for it; for example an int[] array.
BtW you can grab a float[] outta a PVector via its array() method:

After that you just need another util function to convert that float[] to a rounded int[].
Then use Arrays.toString() to convert the whole array to a String and use some other String methods like substring() & replace():
Arrays (Java SE 11 & JDK 11 )
String (Java SE 11 & JDK 11 )

// Discourse.Processing.org/t/how-to-extract-values-from-a-pvector/25778/4
// GoToLoop (2020/Nov/28)

import java.util.Arrays;

void setup() {
  final PVector vec = PVector.random3D(this).mult(100);
  final int[] roundedVec = roundedArray(vec.array());
  println(vec);

  final String tsv = toDsvArr(roundedVec);
  println(tsv);

  final String dsv = toDsvArr(" ", roundedVec);
  println(dsv);

  final String csv = toDsvArr(",", roundedVec);
  println(csv);

  exit();
}

@SafeVarargs static final int[] roundedArray(final float... arr) {
  final int[] rounded = new int[arr.length];
  for (int i = 0; i < arr.length; rounded[i] = round(arr[i++]));
  return rounded;
}

@SafeVarargs static final String toDsvArr(final int... arr) {
  return toDsvArr("\t", arr);
}

@SafeVarargs static final String toDsvArr(final String sep, final int... arr) {
  final String arrStr = Arrays.toString(arr);
  return arrStr.substring(1, arrStr.length() - 1).replace(", ", sep);
}

You can use join() in place of Arrays.toString(). That’d be easier now that I’ve checked that out: :wink:

@SafeVarargs static final String toDsvArr(final String sep, final int... arr) {
  return join(str(arr), sep);
}

Thanks a lot,
I ended up with

PVector v = hand.getPosition();
float[] f = v.array();
int X= round(f[0]); 
int Y= round(f[1]); 
int Z= round(f[2]); 
  
     final String tsv = "H"+X + " "+Y + " "+Z + " "
+ round(handRoll) +" " + round(handPitch) + " " + round (handYaw) + " " + round (handPinch*100) + '\n';
   

 println (tsv);

and I am very happy.