PageRenderTime 14ms CodeModel.GetById 4ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/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 ***********************************************************************/
 18package org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor;
 19
 20import java.util.ArrayList;
 21import java.util.List;
 22
 23
 24import org.mt4j.components.interfaces.IMTComponent3D;
 25import org.mt4j.components.visibleComponents.shapes.MTPolygon;
 26import org.mt4j.input.inputData.InputCursor;
 27import org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor.UnistrokeUtils.Direction;
 28import org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor.UnistrokeUtils.UnistrokeGesture;
 29import org.mt4j.input.inputProcessors.componentProcessors.unistrokeProcessor.UnistrokeUtils.Recognizer;
 30import org.mt4j.util.MTColor;
 31import org.mt4j.util.math.Tools3D;
 32import org.mt4j.util.math.ToolsGeometry;
 33import org.mt4j.util.math.Vector3D;
 34import org.mt4j.util.math.Vertex;
 35
 36import processing.core.PApplet;
 37
 38/**
 39 * The Class DollarGestureContext.
 40 */
 41public class UnistrokeContext {
 42	
 43	/** The gesture aborted. */
 44	protected boolean gestureAborted;
 45	
 46	/** The cursor. */
 47	private InputCursor cursor;
 48	
 49	/** The new position. */
 50	private Vector3D newPosition;
 51
 52	/** The visualizer. */
 53	private MTPolygon visualizer;
 54	
 55	/** The vertex points. */
 56	private Vertex[] vertexPoints;
 57	
 58	/** The points. */
 59	private List<Vector3D> points;
 60	
 61	/** The points_resampled. */
 62	private List<Vector3D> points_resampled;
 63	
 64	/** The plane normal. */
 65	private Vector3D planeNormal;
 66	
 67	/** The point in plane. */
 68	private Vector3D pointInPlane;
 69	
 70	/** The pa. */
 71	private PApplet pa;
 72	
 73	/** The recognizer. */
 74	private Recognizer recognizer;
 75	
 76	/** The dollar utils. */
 77	private UnistrokeUtils dollarUtils;
 78	
 79	/** The target. */
 80	private IMTComponent3D target;
 81	
 82	/**
 83	 * Instantiates a new dollar gesture context.
 84	 *
 85	 * @param pa the pa
 86	 * @param planeNormal the plane normal
 87	 * @param pointInPlane the point in plane
 88	 * @param cursor the cursor
 89	 * @param recognizer the recognizer
 90	 * @param dollarUtils the dollar utils
 91	 * @param target the target
 92	 */
 93	public UnistrokeContext(PApplet pa, Vector3D planeNormal, Vector3D pointInPlane, InputCursor cursor, Recognizer recognizer, UnistrokeUtils dollarUtils, IMTComponent3D target) {
 94		gestureAborted = false;
 95		this.cursor = cursor;
 96		this.planeNormal = planeNormal;
 97		this.pointInPlane = pointInPlane;
 98		this.pa = pa;
 99		this.recognizer = recognizer;
100		this.dollarUtils = dollarUtils;
101		this.target = target;
102		
103		//		Vector3D newPos = ToolsGeometry.getRayPlaneIntersection(Tools3D	.getCameraPickRay(pa, camera, cursor.getCurrentEvent().getPosX(), cursor.getCurrentEvent().getPosY()),
104		//				planeNormal, pointInPlane);
105		Vector3D newPos = ToolsGeometry.getRayPlaneIntersection(
106				Tools3D.getCameraPickRay(pa, target, cursor.getCurrentEvtPosX(), cursor.getCurrentEvtPosY()), 
107				planeNormal, 
108				pointInPlane);
109
110		if (newPos == null) {
111			System.out.println("DollarGestureContext"
112					+ " intersection with plane was null in class: "
113					+ this.getClass().getName());
114			gestureAborted = true;
115
116			return;
117		}
118		
119		points = new ArrayList<Vector3D>();
120		vertexPoints = new Vertex[] {};
121		
122		this.newPosition = newPos;
123		points.add(newPos);
124
125		visualizer =  new MTPolygon(pa, new Vertex[] {new Vertex(0,0), new Vertex(1,1), new Vertex(2,2)});
126		visualizer.setPickable(false);
127		visualizer.setDepthBufferDisabled(false);
128//		visualizer.attachCamera(new MTCamera(pa));
129	}
130
131
132	/**
133	 * Gets the visualizer.
134	 *
135	 * @return the visualizer
136	 */
137	public MTPolygon getVisualizer(){
138		return this.visualizer;
139	}
140
141
142	/**
143	 * Update.
144	 *
145	 * @param m the m
146	 */
147	public void update(InputCursor m) {
148		if (!gestureAborted) {
149			Vector3D newPos = ToolsGeometry.getRayPlaneIntersection(
150					Tools3D.getCameraPickRay(pa, target, cursor.getCurrentEvtPosX(), cursor.getCurrentEvtPosY()), 
151					planeNormal, 
152					pointInPlane);
153			
154			this.newPosition = newPos;
155
156			points.add(this.newPosition);
157			int numPoints = points.size();
158
159			List<Vertex> tempList = new ArrayList<Vertex>();
160
161			if (points.size() > 64) {
162				numPoints = 64 + (int)Math.log(points.size() - 64);
163			}
164
165			points_resampled = new ArrayList<Vector3D>();
166			for (Vector3D point: points) {
167				points_resampled.add(new Vector3D(point.getX(), point.getY()));
168			}
169
170			points_resampled = dollarUtils.Resample(points_resampled, numPoints, Direction.CLOCKWISE);
171
172			tempList = new ArrayList<Vertex>();
173			for (Vector3D point: points_resampled) { //TODO avoid loop copy?
174				tempList.add(new Vertex(point.getX(), point.getY()));
175			}
176
177			vertexPoints = (Vertex[]) tempList.toArray(new Vertex[0]);
178
179			if (vertexPoints != null) 
180				visualizer.setVertices(vertexPoints);
181			visualizer.setNoFill(true);
182			visualizer.setStrokeWeight(5);
183			visualizer.setStrokeColor(new MTColor(255,255, 0, 192));
184		}
185	}
186
187	/**
188	 * Recognize gesture.
189	 *
190	 * @return the dollar gesture
191	 */
192	public UnistrokeGesture recognizeGesture(){
193		if (points_resampled != null){
194			return recognizer.Recognize(points_resampled);
195		}else{
196			return UnistrokeGesture.NOGESTURE;
197		}
198	}
199
200
201	/**
202	 * Gets the points.
203	 *
204	 * @return the points
205	 */
206	protected List<Vector3D> getPoints() {
207		return points;
208	}
209
210	/**
211	 * Sets the points.
212	 *
213	 * @param points the new points
214	 */
215	protected void setPoints(List<Vector3D> points) {
216		this.points = points;
217	}
218
219
220	public boolean isGestureAborted() {
221		return this.gestureAborted;
222	}
223}