/src/org/mt4j/input/inputProcessors/componentProcessors/unistrokeProcessor/UnistrokeContext.java

http://mt4j.googlecode.com/ · Java · 223 lines · 107 code · 44 blank · 72 comment · 10 complexity · b3f29b1ba30bebe318be04d50c233f38 MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2010 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import org.mt4j.components.interfaces.IMTComponent3D;
  22. import org.mt4j.components.visibleComponents.shapes.MTPolygon;
  23. import org.mt4j.input.inputData.InputCursor;
  24. import org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor.UnistrokeUtils.Direction;
  25. import org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor.UnistrokeUtils.UnistrokeGesture;
  26. import org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor.UnistrokeUtils.Recognizer;
  27. import org.mt4j.util.MTColor;
  28. import org.mt4j.util.math.Tools3D;
  29. import org.mt4j.util.math.ToolsGeometry;
  30. import org.mt4j.util.math.Vector3D;
  31. import org.mt4j.util.math.Vertex;
  32. import processing.core.PApplet;
  33. /**
  34. * The Class DollarGestureContext.
  35. */
  36. public class UnistrokeContext {
  37. /** The gesture aborted. */
  38. protected boolean gestureAborted;
  39. /** The cursor. */
  40. private InputCursor cursor;
  41. /** The new position. */
  42. private Vector3D newPosition;
  43. /** The visualizer. */
  44. private MTPolygon visualizer;
  45. /** The vertex points. */
  46. private Vertex[] vertexPoints;
  47. /** The points. */
  48. private List<Vector3D> points;
  49. /** The points_resampled. */
  50. private List<Vector3D> points_resampled;
  51. /** The plane normal. */
  52. private Vector3D planeNormal;
  53. /** The point in plane. */
  54. private Vector3D pointInPlane;
  55. /** The pa. */
  56. private PApplet pa;
  57. /** The recognizer. */
  58. private Recognizer recognizer;
  59. /** The dollar utils. */
  60. private UnistrokeUtils dollarUtils;
  61. /** The target. */
  62. private IMTComponent3D target;
  63. /**
  64. * Instantiates a new dollar gesture context.
  65. *
  66. * @param pa the pa
  67. * @param planeNormal the plane normal
  68. * @param pointInPlane the point in plane
  69. * @param cursor the cursor
  70. * @param recognizer the recognizer
  71. * @param dollarUtils the dollar utils
  72. * @param target the target
  73. */
  74. public UnistrokeContext(PApplet pa, Vector3D planeNormal, Vector3D pointInPlane, InputCursor cursor, Recognizer recognizer, UnistrokeUtils dollarUtils, IMTComponent3D target) {
  75. gestureAborted = false;
  76. this.cursor = cursor;
  77. this.planeNormal = planeNormal;
  78. this.pointInPlane = pointInPlane;
  79. this.pa = pa;
  80. this.recognizer = recognizer;
  81. this.dollarUtils = dollarUtils;
  82. this.target = target;
  83. // Vector3D newPos = ToolsGeometry.getRayPlaneIntersection(Tools3D .getCameraPickRay(pa, camera, cursor.getCurrentEvent().getPosX(), cursor.getCurrentEvent().getPosY()),
  84. // planeNormal, pointInPlane);
  85. Vector3D newPos = ToolsGeometry.getRayPlaneIntersection(
  86. Tools3D.getCameraPickRay(pa, target, cursor.getCurrentEvtPosX(), cursor.getCurrentEvtPosY()),
  87. planeNormal,
  88. pointInPlane);
  89. if (newPos == null) {
  90. System.out.println("DollarGestureContext"
  91. + " intersection with plane was null in class: "
  92. + this.getClass().getName());
  93. gestureAborted = true;
  94. return;
  95. }
  96. points = new ArrayList<Vector3D>();
  97. vertexPoints = new Vertex[] {};
  98. this.newPosition = newPos;
  99. points.add(newPos);
  100. visualizer = new MTPolygon(pa, new Vertex[] {new Vertex(0,0), new Vertex(1,1), new Vertex(2,2)});
  101. visualizer.setPickable(false);
  102. visualizer.setDepthBufferDisabled(false);
  103. // visualizer.attachCamera(new MTCamera(pa));
  104. }
  105. /**
  106. * Gets the visualizer.
  107. *
  108. * @return the visualizer
  109. */
  110. public MTPolygon getVisualizer(){
  111. return this.visualizer;
  112. }
  113. /**
  114. * Update.
  115. *
  116. * @param m the m
  117. */
  118. public void update(InputCursor m) {
  119. if (!gestureAborted) {
  120. Vector3D newPos = ToolsGeometry.getRayPlaneIntersection(
  121. Tools3D.getCameraPickRay(pa, target, cursor.getCurrentEvtPosX(), cursor.getCurrentEvtPosY()),
  122. planeNormal,
  123. pointInPlane);
  124. this.newPosition = newPos;
  125. points.add(this.newPosition);
  126. int numPoints = points.size();
  127. List<Vertex> tempList = new ArrayList<Vertex>();
  128. if (points.size() > 64) {
  129. numPoints = 64 + (int)Math.log(points.size() - 64);
  130. }
  131. points_resampled = new ArrayList<Vector3D>();
  132. for (Vector3D point: points) {
  133. points_resampled.add(new Vector3D(point.getX(), point.getY()));
  134. }
  135. points_resampled = dollarUtils.Resample(points_resampled, numPoints, Direction.CLOCKWISE);
  136. tempList = new ArrayList<Vertex>();
  137. for (Vector3D point: points_resampled) { //TODO avoid loop copy?
  138. tempList.add(new Vertex(point.getX(), point.getY()));
  139. }
  140. vertexPoints = (Vertex[]) tempList.toArray(new Vertex[0]);
  141. if (vertexPoints != null)
  142. visualizer.setVertices(vertexPoints);
  143. visualizer.setNoFill(true);
  144. visualizer.setStrokeWeight(5);
  145. visualizer.setStrokeColor(new MTColor(255,255, 0, 192));
  146. }
  147. }
  148. /**
  149. * Recognize gesture.
  150. *
  151. * @return the dollar gesture
  152. */
  153. public UnistrokeGesture recognizeGesture(){
  154. if (points_resampled != null){
  155. return recognizer.Recognize(points_resampled);
  156. }else{
  157. return UnistrokeGesture.NOGESTURE;
  158. }
  159. }
  160. /**
  161. * Gets the points.
  162. *
  163. * @return the points
  164. */
  165. protected List<Vector3D> getPoints() {
  166. return points;
  167. }
  168. /**
  169. * Sets the points.
  170. *
  171. * @param points the new points
  172. */
  173. protected void setPoints(List<Vector3D> points) {
  174. this.points = points;
  175. }
  176. public boolean isGestureAborted() {
  177. return this.gestureAborted;
  178. }
  179. }