PenGestureListener.java

00001 package edu.stanford.hci.r3.pen.gesture;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.IOException;
00005 import java.io.InputStreamReader;
00006 import java.io.Writer;
00007 import java.util.ArrayList;
00008 
00009 import edu.stanford.hci.r3.pen.PenSample;
00010 import edu.stanford.hci.r3.pen.streaming.listeners.PenListener;
00011 
00019 public class PenGestureListener implements PenListener {
00020         
00021         private ArrayList<Gesture> gestures = new ArrayList<Gesture>();
00022 
00023         private ArrayList<ShapeContext> contexts = new ArrayList<ShapeContext>();
00024 
00025         private ArrayList<PenSample> samples = null;
00026 
00027         private int gestureThreshold = 4;
00028 
00029         private int remainingContexts = 0;
00030 
00031         private GestureDatabase database;
00032 
00033         private String author;
00034 
00035         private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
00036 
00037         public void penDown(PenSample sample) {
00038                 samples = new ArrayList<PenSample>();
00039         }
00040 
00041         public void setContexts(ArrayList<ShapeContext> contexts, int remainingContexts) {
00042                 this.contexts = contexts;
00043                 this.remainingContexts = remainingContexts;
00044         }
00045 
00046         public void penUp(PenSample sample) {
00047                 final double categoryThreshold = 20;
00048                 if (samples != null && samples.size() > gestureThreshold && remainingContexts > 0) {
00049                         // normalize samples by first in space, maybe time
00050                         double min_x = Double.MAX_VALUE, min_y = Double.MAX_VALUE;
00051                         long min_t = Long.MAX_VALUE;
00052                         for (PenSample inksample : samples) {
00053                                 min_x = Math.min(min_x, inksample.x);
00054                                 min_y = Math.min(min_y, inksample.y);
00055                                 min_t = Math.min(min_t, inksample.timestamp);
00056                         }
00057                         for (PenSample inksample : samples) {
00058                                 inksample.x -= min_x;
00059                                 inksample.y -= min_y;
00060                                 inksample.timestamp -= min_t;
00061                         }
00062                         System.out.println("Gesture accepted.");
00063                         remainingContexts--;
00064                         int index = -1;
00065                         double distance = Double.MAX_VALUE;
00066                         ShapeContext context = new ShapeContext(samples, author);
00067                         contexts.add(context);
00068                         if (remainingContexts == 0)
00069                                 System.out.println("Done reading gestures.");
00070                 } else if (samples != null && samples.size() > gestureThreshold && database != null) {
00071                         ShapeContext context = new ShapeContext(samples, "");
00072                         database.test(context, true);
00073                 }
00074                 samples = null;
00075         }
00076 
00077         public void sample(PenSample sample) {
00078                 PenSample inkSample = new PenSample(sample.x, sample.y, sample.force, sample.timestamp);
00079                 samples.add(inkSample);
00080         }
00081 
00082         public void quillWrite(Writer writer) throws IOException {
00083                 final String VERSION = "gdt 2.0";
00084 
00085                 writer.write(VERSION + "\n");
00086                 // gesture package
00087                 String name = "bob";
00088                 writer.write("name\t" + name + "\n");
00089                 writer.write("training\n");
00090                 // gesture set
00091                 writer.write("name\t" + name + "\n");
00092                 for (Gesture gesture : gestures) {
00093                         writer.write("category\n");
00094                         gesture.quillWrite(writer);
00095                 }
00096                 // end category
00097                 writer.write("endset\n");
00098                 // end set
00099                 writer.write("test\n");
00100                 writer.write("endpackage\n");
00101                 writer.close();
00102         }
00103 
00104         public void setDatabase(GestureDatabase database) {
00105                 // TODO Auto-generated method stub
00106                 this.database = database;
00107         }
00108 
00109         public void setAuthor(String author) {
00110                 // TODO Auto-generated method stub
00111                 this.author = author;
00112         }
00113 }

Generated on Sat Apr 14 18:21:37 2007 for R3 Paper Toolkit by  doxygen 1.4.7