TeZ LIFE/FORMS workshop info + code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.1 KiB

3 years ago
  1. /**
  2. * Blob Class
  3. *
  4. * Based on this example by Daniel Shiffman:
  5. * http://shiffman.net/2011/04/26/opencv-matching-faces-over-time/
  6. *
  7. * @author: Jordi Tost (@jorditost)
  8. *
  9. * University of Applied Sciences Potsdam, 2014
  10. */
  11. import oscP5.*;
  12. import netP5.*;
  13. private static NetAddress oscOutAddress;
  14. private static OscMessage mMessage;
  15. class Blob {
  16. private final static int OSC_OUT_PERIOD = 100;
  17. private final static int OSC_OUT_PORT = 8666;
  18. private final static String OSC_OUT_HOST = "localhost";
  19. private final static String OSC_OUT_PATTERN = "/blobOSC/";
  20. private PApplet parent;
  21. // Contour
  22. public Contour contour;
  23. // Am I available to be matched?
  24. public boolean available;
  25. // Should I be deleted?
  26. public boolean delete;
  27. // How long should I live if I have disappeared?
  28. private int initTimer = 5; //127;
  29. public int timer;
  30. //public int xID;
  31. // Unique ID for each blob
  32. int id;
  33. ///////////////////////////////
  34. // Make me
  35. Blob(PApplet parent, int id, Contour c) {
  36. this.parent = parent;
  37. this.id = id;
  38. this.contour = new Contour(parent, c.pointMat);
  39. available = true;
  40. delete = false;
  41. timer = initTimer;
  42. oscOutAddress = new NetAddress(OSC_OUT_HOST, OSC_OUT_PORT);
  43. mMessage = new OscMessage(OSC_OUT_PATTERN);
  44. }
  45. ///////////////////////////////
  46. // Show me
  47. void display() {
  48. Rectangle r = contour.getBoundingBox();
  49. float opacity = map(timer, 0, initTimer, 0, 127);
  50. fill(0, 0, 255, opacity);
  51. stroke(0, 0, 255);
  52. rect(r.x, r.y, r.width, r.height);
  53. fill(255, 2*opacity);
  54. textSize(26);
  55. text(""+id, r.x+10, r.y+30);
  56. }
  57. ///////////////////////////////
  58. // Give me a new contour for this blob (shape, points, location, size)
  59. // Oooh, it would be nice to lerp here!
  60. void update(Contour newC) {
  61. contour = new Contour(parent, newC.pointMat);
  62. // Is there a way to update the contour's points without creating a new one?
  63. /*ArrayList<PVector> newPoints = newC.getPoints();
  64. Point[] inputPoints = new Point[newPoints.size()];
  65. for(int i = 0; i < newPoints.size(); i++){
  66. inputPoints[i] = new Point(newPoints.get(i).x, newPoints.get(i).y);
  67. }
  68. contour.loadPoints(inputPoints);*/
  69. timer = initTimer;
  70. }
  71. ///////////////////////////////
  72. // Count me down, I am gone
  73. void countDown() {
  74. timer--;
  75. }
  76. ///////////////////////////////
  77. // I am deed, delete me
  78. boolean dead() {
  79. if (timer < 0) return true;
  80. return false;
  81. }
  82. ///////////////////////////////
  83. public Rectangle getBoundingBox() {
  84. return contour.getBoundingBox();
  85. }
  86. ///////////////////////////////
  87. //send OSC Data
  88. public void sendOsc() {
  89. Rectangle r = contour.getBoundingBox();
  90. double mx = r.getCenterX();
  91. float x = (float) mx;
  92. double my = r.getCenterY();
  93. float y = (float) my;
  94. String mAddrPatt = OSC_OUT_PATTERN;
  95. mMessage.clear();
  96. mMessage.setAddrPattern(mAddrPatt+"id"+"x"+"y"+"w"+"h");
  97. mMessage.add(id);
  98. mMessage.add(x);
  99. mMessage.add(y);
  100. mMessage.add(r.width);
  101. mMessage.add(r.height);
  102. OscP5.flush(mMessage, oscOutAddress);
  103. }
  104. }