Guest User

Untitled

a guest
Jul 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import oscP5.*;
  2. import netP5.*;
  3. import toxi.geom.Quaternion;
  4. import java.util.Map;
  5. import java.util.HashMap;
  6. import processing.core.*;
  7. import java.lang.reflect.*;
  8.  
  9.  
  10.  
  11. class Chordata {
  12.  
  13. OscP5 oscP5;
  14.  
  15. Method displayArm;
  16. PApplet parent;
  17.  
  18. HashMap bones = new HashMap<String, Quaternion>();
  19. Chordata(PApplet parent_, int port) {
  20. parent = parent_;
  21. /* start oscP5, listening for incoming messages at port 12000 */
  22. oscP5 = new OscP5(this, port);
  23.  
  24. /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
  25. * an ip address and a port number. myRemoteLocation is used as parameter in
  26. * oscP5.send() when sending osc packets to another computer, device,
  27. * application. usage see below. for testing purposes the listening port
  28. * and the port of the remote location address are the same, hence you will
  29. * send messages back to this sketch.
  30. */
  31.  
  32.  
  33. try {
  34. displayArm = parent.getClass().getMethod("displayBone",
  35. new Class[] {
  36. String.class
  37. }
  38. );
  39. }
  40. catch (Exception e) {
  41. // no such method, or an error.. which is fine, just ignore
  42.  
  43. PApplet.println("Missing... " + e.getMessage());
  44. }
  45. }
  46.  
  47. public synchronized void display() {
  48. parent.pushMatrix();
  49. for (Object me: bones.entrySet()) {
  50. String boneName = (String) ((Map.Entry) me).getKey();
  51. Quaternion boneOrientation = (Quaternion) ((Map.Entry) me).getValue();
  52. //println(boneName + "," + boneOrientation);
  53. displayEvent(boneName, boneOrientation);
  54. }
  55. parent.popMatrix();
  56.  
  57. }
  58.  
  59. private void displayEvent(String boneName, Quaternion quat) {
  60. if (displayArm != null) {
  61. try {
  62. //displayMethod.invoke(parent, i);
  63. parent.pushMatrix();
  64. if (quat != null) {
  65. parent.applyMatrix(getMatrix(quat));
  66.  
  67. displayArm.invoke(parent, boneName);
  68. parent.popMatrix();
  69. }
  70. }
  71. catch (Exception e) {
  72. PApplet.println("Disabling displayMethod() because of an error.");
  73. // e.printStackTrace();
  74. displayArm = null;
  75. }
  76. }
  77. }
  78.  
  79.  
  80. PMatrix getMatrix(Quaternion q) {
  81. float[] m = new float[16];
  82. q.toMatrix4x4().toFloatArray(m);
  83. PMatrix result = new PMatrix3D(
  84. m[0], m[1], m[2], m[3],
  85. m[4], m[5], m[6], m[7],
  86. m[8], m[9], m[10], m[11],
  87. m[12], m[13], m[14], m[15]);
  88. return result;
  89. }
  90.  
  91.  
  92. /* incoming osc message are forwarded to the oscEvent method. */
  93. synchronized void oscEvent(OscMessage theOscMessage) {
  94. /* print the address pattern and the typetag of the received OscMessage */
  95. //print("### received an osc message.");
  96. bones.put(theOscMessage.addrPattern(), new Quaternion(theOscMessage.get(1).floatValue(), theOscMessage.get(2).floatValue(), theOscMessage.get(3).floatValue(), theOscMessage.get(0).floatValue()));
  97. //PApplet.print(" addrpattern: "+theOscMessage.addrPattern() + "\n");
  98. //for (int i = 0; i < 4; i++) {
  99. // println(" typetag: "+theOscMessage.get(i).floatValue());
  100. //}
  101. }
  102. }
Add Comment
Please, Sign In to add comment