/src/org/mt4j/input/inputProcessors/GestureUtils.java

http://mt4j.googlecode.com/ · Java · 93 lines · 40 code · 15 blank · 38 comment · 12 complexity · 19fa718478963db3dfbbeff4960f81a4 MD5 · raw file

  1. package org.mt4j.input.inputProcessors;
  2. import org.mt4j.components.interfaces.IMTComponent3D;
  3. import org.mt4j.input.inputData.InputCursor;
  4. import org.mt4j.util.math.Tools3D;
  5. import org.mt4j.util.math.ToolsGeometry;
  6. import org.mt4j.util.math.Vector3D;
  7. import processing.core.PApplet;
  8. public class GestureUtils {
  9. ////
  10. /**
  11. * Gets the intersection point of a cursor and a specified component.
  12. * Can return null if the cursor doesent intersect the component.
  13. *
  14. * @param app the app
  15. * @param c the c
  16. * @return the intersection
  17. */
  18. public static Vector3D getIntersection(PApplet app, InputCursor c){
  19. return getIntersection(app, c.getTarget(), c);
  20. }
  21. /**
  22. * Gets the intersection point of a cursor and a specified component.
  23. * Can return null if the cursor doesent intersect the component.
  24. *
  25. * @param app the app
  26. * @param component the component
  27. * @param c the c
  28. * @return the intersection
  29. */
  30. public static Vector3D getIntersection(PApplet app, IMTComponent3D component, InputCursor c){
  31. //First check intersection with the specified component
  32. Vector3D ret = component.getIntersectionGlobal(Tools3D.getCameraPickRay(app, component, c));
  33. //Then if no intersection -> check with the current target of the cursor
  34. IMTComponent3D currentTarget = c.getCurrentEvent().getCurrentTarget();
  35. if (ret == null && currentTarget != component && currentTarget != null){
  36. ret = c.getCurrentEvent().getCurrentTarget().getIntersectionGlobal(Tools3D.getCameraPickRay(app, currentTarget, c));
  37. }
  38. return ret;
  39. }
  40. public static Vector3D getPlaneIntersection(PApplet app, Vector3D planeNormal, Vector3D pointInPlane, InputCursor c){
  41. Vector3D intersection = ToolsGeometry.getRayPlaneIntersection(
  42. Tools3D.getCameraPickRay(app, c.getTarget(), c.getCurrentEvtPosX(), c.getCurrentEvtPosY()),
  43. planeNormal,
  44. pointInPlane);
  45. IMTComponent3D currentTarget = c.getCurrentEvent().getCurrentTarget();
  46. if (intersection == null && currentTarget != c.getTarget() && currentTarget != null){
  47. intersection = ToolsGeometry.getRayPlaneIntersection(
  48. Tools3D.getCameraPickRay(app, currentTarget, c.getCurrentEvtPosX(), c.getCurrentEvtPosY()),
  49. planeNormal,
  50. pointInPlane);
  51. }
  52. return intersection;
  53. }
  54. /**
  55. * Checks if the distance between a reference cursor and a cursor is greater than the distance to another cursor.
  56. *
  57. * @param reference the reference
  58. * @param oldCursor the old cursor
  59. * @param newCursor the new cursor
  60. * @return true, if is cursor distance greater
  61. */
  62. public static boolean isCursorDistanceGreater(InputCursor reference, InputCursor oldCursor, InputCursor newCursor){
  63. // float distanceToOldCursor = reference.getPosition().distance2D(oldCursor.getPosition());
  64. // float distanceToNewCursor = reference.getPosition().distance2D(newCursor.getPosition());
  65. // return distanceToNewCursor > distanceToOldCursor;
  66. return getDistance(reference, newCursor) > getDistance(reference, oldCursor);
  67. }
  68. /**
  69. * Gets the distance between two cursors.
  70. *
  71. * @param a the a
  72. * @param b the b
  73. * @return the distance
  74. */
  75. public static float getDistance(InputCursor a, InputCursor b){
  76. return a.getPosition().distance2D(b.getPosition());
  77. }
  78. }