View Javadoc

1   /*
2    * @(#)$Id: RecordedGesture3D.java
3    *
4    * Author       :   Arthur Vogels, arthur.vogels@gmail.com
5    *
6    * Purpose      :	Temporarily stores a gesture that has been recorded,
7    * 					serving as input for the recognizer.
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date             Who         	Reason
14   *
15   * Dec 02, 2008     arthurvogels    Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  package org.ximtec.igesture.util.additions3d;
27  
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Vector;
31  import java.util.logging.Level;
32  import java.util.logging.Logger;
33  
34  import org.sigtec.ink.input.TimestampedInputEvent;
35  import org.sigtec.input.InputHandler;
36  
37  public class RecordedGesture3D implements Cloneable, InputHandler {
38  	
39  	private static final Logger LOGGER = Logger.getLogger(RecordedGesture3D.class.getName());
40  	
41  	private List<Point3D> points; // List of position data
42  	private Accelerations accelerations; // Accelerations list that comes
43  
44  	// from the input device
45  
46  	/**
47  	 * Constructor
48  	 */
49  	public RecordedGesture3D() {
50  		points = new Vector<Point3D>();
51  	}
52  
53  	/**
54  	 * Constructor
55  	 * 
56  	 * @param points
57  	 *            A list of Point3D to be set as the position data of this
58  	 *            RecordedGesture3D
59  	 */
60  	public RecordedGesture3D(List<Point3D> points) {
61  		this();
62  		if (points != null) {
63  			this.points = points;
64  		}
65  	}
66  
67  	/**
68  	 * Adds a point to the gesture
69  	 * 
70  	 * @param point
71  	 *            The Point3D to be added to the gesture
72  	 */
73  	public void add(Point3D point) {
74  		points.add(point);
75  	}
76  
77  	/**
78  	 * Adds a list of points to the gesture
79  	 * 
80  	 * @param points
81  	 *            The list of points to be added to the gesture
82  	 */
83  	public void addAll(List<Point3D> points) {
84  		this.points.addAll(points);
85  	}
86  
87  	/**
88  	 * Removes a point from the gesture
89  	 * 
90  	 * @param point
91  	 *            The point to be removed from the gesture
92  	 */
93  	public void remove(Point3D point) {
94  		points.remove(point);
95  	}
96  
97  	/**
98  	 * Returns an Iterator on points
99  	 * 
100 	 * @return The Iterator on the points list
101 	 */
102 	public Iterator<Point3D> iterator() {
103 		return points.iterator();
104 	}
105 
106 	/**
107 	 * Returns point by index
108 	 * 
109 	 * @param index
110 	 *            The index of the point to be returned
111 	 * @return The Point3D with the given index
112 	 */
113 	public Point3D get(int index) {
114 		return points.get(index);
115 	}
116 
117 	/**
118 	 * Returns the list of points
119 	 * 
120 	 * @return The list of Point3D from this gesture
121 	 */
122 	public List<Point3D> getPoints() {
123 		return points;
124 	}
125 
126 	/**
127 	 * Returns the size of the points list
128 	 * 
129 	 * @return The size of the points list
130 	 */
131 	public int size() {
132 		return points.size();
133 	}
134 
135 	/**
136 	 * Returns the start point of the gesture
137 	 * 
138 	 * @return The start Point3D of the gesture
139 	 */
140 	public Point3D getStartPoint() {
141 		return (!points.isEmpty()) ? (Point3D) points.get(0) : null;
142 	}
143 
144 	/**
145 	 * Returns the end point of the gesture
146 	 * 
147 	 * @return The end Point3D of the gesture
148 	 */
149 	public Point3D getEndPoint() {
150 		return (!points.isEmpty()) ? (Point3D) points.get(points.size() - 1)
151 				: null;
152 	}
153 
154 	/**
155 	 * Returns the duration of the gesture
156 	 * 
157 	 * @return The duration of the gesture in milliseconds
158 	 */
159 	public long getDuration() {
160 		Point3D startPoint = getStartPoint();
161 		Point3D endPoint = getEndPoint();
162 
163 		if ((startPoint != null) && (endPoint != null)) {
164 			return endPoint.getTimeStamp() - startPoint.getTimeStamp();
165 		} else {
166 			return 0;
167 		}
168 	}
169 
170 	/**
171 	 * Returns true if the optional timestamp is available for all the points
172 	 * the trace contains. It is assumed that either all or none of the points
173 	 * have a timestamp.
174 	 * 
175 	 * @return true if a timestamp is available.
176 	 */
177 	public boolean hasTimestamp() {
178 		return getStartPoint().hasTimeStamp();
179 	}
180 
181 	@Override
182 	public synchronized void handle(Object invoker,
183 			TimestampedInputEvent timestampedEvent) {
184 		// TODO Auto-generated method stub
185 
186 	}
187 
188 	/**
189 	 * Returns the list of acceleration data from this gesture
190 	 * 
191 	 * @return The WiiAccelerations from this gesture
192 	 */
193 	public Accelerations getAccelerations() {
194 		return accelerations;
195 	}
196 
197 	/**
198 	 * Sets an accelerations list for this gesture
199 	 * 
200 	 * @param accelerations
201 	 *            The accelerations list WiiAccelerations object
202 	 */
203 	public void setAccelerations(Accelerations accelerations) {
204 		this.accelerations = accelerations;
205 	}
206 
207 	/**
208 	 * Sets a points list for this gesture
209 	 * 
210 	 * @param pointsList
211 	 *            The points list
212 	 */
213 	public void setPoints(List<Point3D> pointsList) {
214 		this.points = pointsList;		
215 	}
216 	
217    /**
218     * Clones a recordedgesture3D.
219     * @return the cloned recordedgesture3D.
220     */
221    @Override
222    @SuppressWarnings("unchecked")
223    public Object clone() {
224       RecordedGesture3D clone = null;
225 
226       try {
227          clone = (RecordedGesture3D)super.clone();
228          List<Point3D> clonedPoints = new Vector<Point3D>();
229 
230          for (Point3D point : points) {
231         	 clonedPoints.add((Point3D)point.clone());
232          }
233 
234          clone.points = clonedPoints;
235          clone.accelerations = (Accelerations) accelerations.clone();
236       }
237       catch (CloneNotSupportedException e) {
238          LOGGER.log(Level.SEVERE, e.toString());
239       }
240 
241       return clone;
242    } // clone
243    
244 }