PageRenderTime 55ms CodeModel.GetById 45ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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