View Javadoc

1   /*
2    * @(#)$Id: Gesture3DTool.java
3    *
4    * Author		:	Arthur Vogels, arthur.vogels@gmail.com
5    *                  
6    *
7    * Purpose		:   Tool for creating and manipulating 3D Gesture 
8    * 					sets with the WiiMote.
9    *
10   * -----------------------------------------------------------------------
11   *
12   * Revision Information:
13   *
14   * Date				Who			Reason
15   *
16   * 15.01.2009		vogelsar	Initial Release
17   *
18   * -----------------------------------------------------------------------
19   *
20   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
21   *
22   * This software is the proprietary information of ETH Zurich.
23   * Use is subject to license terms.
24   * 
25   */
26  
27  package org.ximtec.igesture.algorithm.rubine3d.tools;
28  
29  import java.io.File;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Vector;
33  
34  import org.ximtec.igesture.Recogniser;
35  import org.ximtec.igesture.algorithm.AlgorithmException;
36  import org.ximtec.igesture.algorithm.rubine3d.Rubine3DAlgorithm;
37  import org.ximtec.igesture.configuration.Configuration;
38  import org.ximtec.igesture.core.Gesture;
39  import org.ximtec.igesture.core.GestureClass;
40  import org.ximtec.igesture.core.GestureSample3D;
41  import org.ximtec.igesture.core.GestureSet;
42  import org.ximtec.igesture.core.ResultSet;
43  import org.ximtec.igesture.core.SampleDescriptor3D;
44  import org.ximtec.igesture.storage.StorageManager;
45  import org.ximtec.igesture.util.additions3d.RecordedGesture3D;
46  
47  public class Gesture3DTool {
48  
49  	private StorageManager storage; // The storage manager for database or xml
50  	private WiiReader reader; // The WiiReader to read from the WiiMote
51  	private Recogniser recogniser; // The recogniser
52  	private Gesture3DToolUI ui;
53  
54  	public Gesture3DTool(String gestureSetName) {
55  		storage = new StorageManager(StorageManager
56  				.createStorageEngine(new File(gestureSetName)));
57  		reader = new WiiReader();
58  	}
59  
60  	/**
61  	 * Initialises the WiiReader
62  	 * 
63  	 */
64  	public void initWiiMote() {
65  		reader.init();
66  	}
67  
68  	/**
69  	 * Disconnects the wiimote
70  	 */
71  	public void disconnectWiiMote() {
72  		reader.disconnect();
73  	}
74  
75  	public void setUI(Gesture3DToolUI ui) {
76  		this.ui = ui;
77  	}
78  
79  	/**
80  	 * Returns the panel of the WiiReader in this controller
81  	 * 
82  	 * @return The panel of the WiiReader in this controller
83  	 */
84  	public WiiReaderPanel getWiiReaderPanel() {
85  		return reader.getPanel();
86  	}
87  
88  	/**
89  	 * Adds a new GestureSet with name setName to the database
90  	 * 
91  	 * @param setName
92  	 *            The name of the new GestureSet
93  	 */
94  	public void addGestureSet(String setName) {
95  		System.err.println("Adding new gesture set with name \"" + setName
96  				+ "\"");
97  		GestureSet set = new GestureSet(setName);
98  		storage.store(set);
99  		storage.commit();
100 	}
101 
102 	/**
103 	 * Returns a list of all the GestureSets in the database
104 	 * 
105 	 * @return
106 	 */
107 	public List<GestureSet> getGestureSets() {
108 		return storage.load(GestureSet.class);
109 	}
110 
111 	/**
112 	 * Returns GestureSet with name setName from the database
113 	 * 
114 	 * @param setName
115 	 *            The name of the GestureSet that is to be retrieved
116 	 * @return The retrieved GestureSet
117 	 */
118 	public GestureSet getGestureSet(String setName) {
119 		if (storage.load(GestureSet.class, "name", setName).size() > 0)
120 			return storage.load(GestureSet.class, "name", setName).get(0);
121 		else
122 			return null;
123 	}
124 
125 	/**
126 	 * Adds a new gesture class with name className to a gesture set with name
127 	 * setName
128 	 * 
129 	 * @param setName
130 	 * @param className
131 	 */
132 	public void addGestureClass(String setName, String className) {
133 		System.err.print("addGestureClass(): ");
134 		GestureClass newClass = new GestureClass(className);
135 		if (storage.load(GestureSet.class, "name", setName).size() > 0) {
136 			GestureSet tempSet = storage
137 					.load(GestureSet.class, "name", setName).get(0);
138 			tempSet.addGestureClass(newClass);
139 			storage.update(tempSet);
140 			storage.commit();
141 			System.err.println("Added new gesture class with name \""
142 					+ className + "\" to gesture set \"" + setName + "\"");
143 		} else
144 			System.err.println("Nothing added");
145 	}
146 
147 	/**
148 	 * Adds the gesture sample that is read by the WiiReader reader to the
149 	 * Sample3DDescriptor in gesture class with name className in gesture set
150 	 * with name setName. If no Sample3DDescriptor is present in the gesture
151 	 * class a new one is created
152 	 * 
153 	 * @param setName
154 	 *            The name of the gesture set that contains the gesture class
155 	 * @param className
156 	 *            The name of the gesture class the sample should be added to
157 	 */
158 	public void addCurrentGestureSampleToGestureClass(String setName,
159 			String className) {
160 		System.err.print("addCurrentGestureSampleToGestureClass(): ");
161 		// If the gesture set with given name exists
162 		if (storage.load(GestureSet.class, "name", setName).size() > 0) {
163 			GestureSet tempSet = storage
164 					.load(GestureSet.class, "name", setName).get(0);
165 			// If the gesture set contains a gesture class with name className
166 			if (tempSet.getGestureClass(className) != null) {
167 				// If the gesture class does not contain a Sample3DDescriptor
168 				// yet
169 				if (tempSet.getGestureClass(className).getDescriptor(
170 						SampleDescriptor3D.class) == null) {
171 					SampleDescriptor3D desc = new SampleDescriptor3D();
172 					tempSet.getGestureClass(className).addDescriptor(desc);
173 				}
174 				GestureSample3D gesture = (GestureSample3D) reader.getGesture();
175 				// If the reader has a valid gesture sample available
176 				if (gesture.getGesture().getAccelerations() != null) {
177 					// Add the sample from Wiireader to the Sample3DDescriptor
178 					tempSet.getGestureClass(className).getDescriptor(
179 							SampleDescriptor3D.class).addSample(gesture);
180 					// Update the set in the database and commit changes
181 					storage.update(tempSet);
182 					storage.commit();
183 					System.err.println("Added GestureSample3D with name \""
184 							+ gesture.getName()
185 							+ "\" to gesture class with name \"" + className
186 							+ "\" in gesture set \"" + setName + "\"");
187 					// Job's done
188 
189 					// DEBUG
190 
191 //					Iterator i = tempSet.getGestureClass(className)
192 //							.getDescriptor(SampleDescriptor3D.class).getSamples()
193 //							.iterator();
194 //					while (i.hasNext()) {
195 //						GestureSample3D tempSample = (GestureSample3D) i.next();
196 //						System.out.println("Name: \"" + tempSample.getName()
197 //								+ "\"");
198 //					}
199 
200 					return;
201 				}
202 			}
203 		}
204 		System.err.println("Nothing added");
205 	}
206 
207 	/**
208 	 * Returns a list of gesture samples from gesture class with name className
209 	 * from gesture set with name setName
210 	 * 
211 	 * @param setName
212 	 *            The name of the gesture set to look in
213 	 * @param className
214 	 *            The name of the gesture class to look in
215 	 * @return The list of found gesture samples
216 	 */
217 	public List<Gesture<RecordedGesture3D>> getGestureSamples(String setName, String className) {
218 		System.err.println("getGestureSamples() for GestureSet " + setName
219 				+ " and GestureClass " + className + ".");
220 		// Create return variable
221 		List<Gesture<RecordedGesture3D>> returnList = new Vector<Gesture<RecordedGesture3D>>();
222 		// If there is a gesture set with name setName
223 		if (storage.load(GestureSet.class, "name", setName).size() > 0) {
224 			GestureSet tempSet = storage
225 					.load(GestureSet.class, "name", setName).get(0);
226 			// If there is a gesture class with name className in tempSet
227 			if (tempSet.getGestureClass(className) != null) {
228 				GestureClass tempClass = tempSet.getGestureClass(className);
229 				// Get descriptor from gesture class if it contains a
230 				// SampleDescriptor
231 				SampleDescriptor3D descriptor = tempClass
232 						.getDescriptor(SampleDescriptor3D.class);
233 				// If there is a descriptor, take the list of samples from the
234 				// descriptor
235 				if (descriptor != null)
236 					returnList = descriptor.getSamples();
237 				else
238 					System.err
239 							.println("There is no SampleDescriptor in gesture class with name \""
240 									+ className
241 									+ "\" in gesture set \""
242 									+ setName + "\".");
243 			} else
244 				System.err.println("There is no gesture class with name \""
245 						+ className + "\" in gesture set \"" + setName + "\".");
246 		} else
247 			System.err.println("There is no gesture set with name \"" + setName
248 					+ "\".");
249 		// Return list
250 		return returnList;
251 	}
252 
253 	/**
254 	 * Recognises the current gesture from the WiiReader against the gesture set
255 	 * with name setName
256 	 * 
257 	 * @param setName
258 	 *            the set to be recognised against
259 	 */
260 	public void recognise(String setName) {
261 		// Configure recogniser
262 		//new Rubine3DAlgorithm();
263 		Configuration config = new Configuration();
264 		config.addAlgorithm(Rubine3DAlgorithm.class.getName());
265 		config.addGestureSet(getGestureSet(setName));
266 		try {
267 			recogniser = new Recogniser(config);
268 		} catch (AlgorithmException e) {
269 			e.printStackTrace();
270 		}
271 		// Recognise
272 		ResultSet resultSet = recogniser.recognise(reader.getGesture());
273 		ui.setResultField(resultSet);
274 		//
275 		System.err.println("Number of Results in ResultSet: "
276 				+ resultSet.getResults().size());
277 	}
278 
279 	/**
280 	 * Removes Sample with the number sampleNumber from the gesture class with
281 	 * name className from gesture set with name setName
282 	 * 
283 	 * @param setName
284 	 * @param className
285 	 * @param sampleNumber
286 	 */
287 	public void removeSample(String setName, String className, int sampleNumber) {
288 		GestureSet tempSet = storage.load(GestureSet.class, "name", setName)
289 				.get(0);
290 		if (tempSet.getGestureClass(className).getDescriptor(
291 				SampleDescriptor3D.class).getSamples().size() > 0) {
292 			tempSet.getGestureClass(className).getDescriptor(
293 					SampleDescriptor3D.class).getSamples().remove(sampleNumber);
294 			storage.update(tempSet);
295 		} else
296 			System.err
297 					.println("Gesture3DTool: No samples can be removed from Gesture class "
298 							+ className
299 							+ " in gesture set "
300 							+ setName
301 							+ " because there are no samples present.");
302 
303 	}
304 
305 	/**
306 	 * Removes the gesture set with name setName from storage
307 	 * 
308 	 * @param setName
309 	 */
310 	public void removeSet(String setName) {
311 		if (storage.load(GestureSet.class, "name", setName).get(0) != null) {
312 			storage.remove(storage.load(GestureSet.class, "name", setName).get(
313 					0));
314 		} else
315 			System.err.println("Gesture3DTool: Gesture set " + setName
316 					+ " can not be removed because it does not exist");
317 	}
318 
319 	/**
320 	 * Removes the gesture class with name className from gesture set with name
321 	 * setName in storage
322 	 * 
323 	 * @param setName
324 	 * @param className
325 	 */
326 	public void removeClass(String setName, String className) {
327 		GestureSet tempSet = storage.load(GestureSet.class, "name", setName)
328 				.get(0);
329 		if (tempSet.getGestureClass(className) != null) {
330 			tempSet.removeGestureClass(tempSet.getGestureClass(className));
331 			storage.update(tempSet);
332 		} else
333 			System.err.println("Gesture3DTool: Gesture class " + className
334 					+ " in gesture set " + setName
335 					+ " can not be removed because it does not exist");
336 
337 	}
338 
339 }