View Javadoc

1   /*
2    * @(#)$Id: GestureSignature.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose      : 	Represents the signature of a gestrue.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Dec 26, 2006     ukurmann    Initial Release
15   * Mar 17, 2007     bsigner     Cleanup
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  
27  package org.ximtec.igesture.algorithm.signature;
28  
29  import java.util.ArrayList;
30  import java.util.BitSet;
31  import java.util.List;
32  
33  import org.sigtec.ink.Note;
34  import org.sigtec.ink.Point;
35  import org.sigtec.ink.Trace;
36  import org.sigtec.ink.input.Location;
37  import org.sigtec.util.Constant;
38  import org.ximtec.igesture.algorithm.feature.FeatureTool;
39  import org.ximtec.igesture.core.GestureClass;
40  import org.ximtec.igesture.util.GestureTool;
41  
42  
43  /**
44   * Interface for distance functions.
45   * 
46   * @version 1.0 Dec 2006
47   * @author Ueli Kurmann, igesture@uelikurmann.ch
48   * @author Beat Signer, signer@inf.ethz.ch
49   */
50  public class GestureSignature {
51  
52     private BitSet signatures;
53  
54     private Note note;
55  
56     private int numberOfPoints;
57  
58     private int rasterSize;
59  
60     private int gridSize;
61  
62     private GestureClass gestureClass;
63  
64     private Position lastPosition;
65  
66     private Grid grid;
67  
68     public class Position {
69  
70        public int x;
71        public int y;
72        Location location;
73  
74  
75        public Position(int x, int y) {
76           this.x = x;
77           this.y = y;
78        }
79  
80  
81        public boolean equals(Position p) {
82           return (p.x == this.x && p.y == this.y);
83        } // equals
84  
85  
86        @Override
87        public String toString() {
88           return "X=" + x + " Y=" + y;
89        } // toString
90  
91     }
92  
93  
94     /**
95      * Constructs a new gesture signature.
96      * 
97      * @param note the note.
98      * @param gestureClass the gesture class.
99      * @param rasterSize the raster's size.
100     * @param gridSize the number of cells the grid have in a row.
101     */
102    public GestureSignature(Note note, GestureClass gestureClass, int rasterSize,
103          int gridSize) {
104       this.signatures = new BitSet();
105       this.numberOfPoints = 0;
106       this.rasterSize = rasterSize;
107       this.gridSize = gridSize;
108       this.grid = Grid.createInstance(gridSize);
109       this.gestureClass = gestureClass;
110       this.note = note;
111       init();
112    }
113 
114 
115    private void init() {
116       List<GestureSignature.Position> points = new ArrayList<GestureSignature.Position>();
117       note.moveTo(0, 0);
118       Trace trace = FeatureTool.createTrace((Note)note.clone());
119       
120       double scale = GestureTool.scaleTraceTo(trace, rasterSize, rasterSize);
121       trace.scale(scale, scale);
122 
123       for (Point point : trace.getPoints()) {
124          Position p = getPosition(point);
125 
126          if (lastPosition == null || !p.equals(lastPosition)) {
127             addSignature(grid.getSignature(p.x, p.y));
128             lastPosition = p;
129          }
130 
131          points.add(lastPosition);
132       }
133 
134    } // init
135 
136 
137    /**
138     * Computes the position in the grid.
139     * 
140     * @param point the point whose position has to be computed.
141     * @return the position of the point within the grid.
142     */
143    private Position getPosition(Point point) {
144       final Position result = new Position(0, 0);
145       result.x = (int)(point.getX() / ((rasterSize / gridSize) + 1));
146       result.y = (int)(point.getY() / ((rasterSize / gridSize) + 1));
147       return result;
148    } // getPosition
149 
150 
151    /**
152     * Appends a point (signature point) to the gesture signature.
153     * 
154     * @param bit the signature point to be added.
155     */
156    public void addSignature(BitSet bit) {
157       for (int i = 0; i < grid.getBitStringLength(); i++) {
158          this.signatures.set(grid.getBitStringLength() * numberOfPoints + i, bit
159                .get(i));
160       }
161 
162       numberOfPoints++;
163    } // addSignature
164 
165 
166    /**
167     * Returns the signature for the point at position index.
168     * 
169     * @param index the position of the signature to be returned.
170     * @return the signature for the point at position index.
171     */
172    public BitSet getPointSignature(int index) {
173       signatures.length();
174       return signatures.get(index * grid.getBitStringLength(), (index + 1)
175             * grid.getBitStringLength());
176    } // getPointSignature
177 
178 
179    /**
180     * Returns the number of points in the signature.
181     * 
182     * @return the number of points in the signature.
183     */
184    public int getNumberOfPoints() {
185       return this.numberOfPoints;
186    } // getNumberOfPoints
187 
188 
189    /**
190     * Returns the lenght of the bit string for a single point.
191     * 
192     * @return the lenght of the bit string for a single point.
193     */
194    public int getBitStringLength() {
195       return grid.getBitStringLength();
196    } // getBitStringLength
197 
198 
199    /**
200     * Returns the gesture class this signature belongs to.
201     * 
202     * @return the gesture class this signature belongs to.
203     */
204    public GestureClass getGestureClass() {
205       return gestureClass;
206    } // getGestureClass
207 
208 
209    @Override
210    public String toString() {
211       final StringBuilder sb = new StringBuilder();
212 
213       for (int i = 0; i < getNumberOfPoints(); i++) {
214          final BitSet bitSet = getPointSignature(i);
215 
216          for (int j = 0; j < grid.getBitStringLength(); j++) {
217             sb.append(bitSet.get(j) ? Constant.ONE : Constant.ZERO);
218          }
219 
220       }
221 
222       return sb.toString();
223    } // toString
224 
225 }