View Javadoc

1   /*
2    * @(#)$Id: StrokeInfo.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose		: 	Analyses an input gesture and creates a string 
7    * 					describing it.
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date				Who			Reason
14   *
15   * Dec 6, 2006		ukurmann	Initial Release
16   * Mar 16, 2007     bsigner     Cleanup
17   * Mar 19, 2007		ukurmann	Add Constants
18   *
19   * -----------------------------------------------------------------------
20   *
21   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
22   *
23   * This software is the proprietary information of ETH Zurich.
24   * Use is subject to license terms.
25   * 
26   */
27  
28  
29  package org.ximtec.igesture.algorithm.siger;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import org.sigtec.ink.Note;
35  import org.sigtec.ink.Point;
36  import org.sigtec.util.Constant;
37  import org.ximtec.igesture.util.GestureTool;
38  
39  
40  /**
41   * Analyses an input gesture and creates a string describing it.
42   * 
43   * @version 1.0 Dec 2006
44   * @author Ueli Kurmann, igesture@uelikurmann.ch
45   * @author Beat Signer, signer@inf.ethz.ch
46   */
47  public class StrokeInfo {
48  
49     private static final int ANGLE_E0 = 0;
50  
51  private static final double ANGLE_N_NE = 292.5;
52  
53  private static final double ANGLE_NW_N = 247.5;
54  
55  private static final double ANGLE_W_NW = 202.5;
56  
57  private static final double ANGLE_SW_W = 157.5;
58  
59  private static final double ANGLE_S_SW = 112.5;
60  
61  private static final double ANGLE_SE_S = 67.5;
62  
63  private static final double ANGLE_E_SE = 22.5;
64  
65  private static final int ANGLE_E = 360;
66  
67  private static final double ANGLE_NE_E = 337.5;
68  
69  private List<Direction> directions;
70  
71     private Note note;
72  
73     private Statistics statistics;
74  
75  
76     public StrokeInfo(Note note) {
77        this.note = GestureTool.getCharacteristicNote((Note)note.clone(), 3, 10);
78        directions = new ArrayList<Direction>();
79        final List<Point> points = this.note.getPoints();
80  
81        for (int i = 1; i < points.size(); i++) {
82           final double angle = GestureTool.getAngle(points.get(i - 1), points
83                 .get(i));
84  
85           if (!Double.isNaN(angle)) {
86              directions.add(getDirection(angle));
87           }
88  
89        }
90  
91        this.statistics = new Statistics(directions, this.note);
92     } // StrokeInfo
93  
94  
95     /**
96      * Returns a string representation of the stroke info.
97      * 
98      * @return string representation of the stroke info.
99      */
100    public String getString() {
101       final StringBuilder sb = new StringBuilder();
102 
103       for (final Direction d : directions) {
104          sb.append(d.name() + Constant.COMMA);
105       }
106 
107       return sb.toString();
108    } // getString
109 
110 
111    /**
112     * Returns the direction based on the gradient's angle.
113     * 
114     * @param angle the angle of the gradient.
115     * @return the direction (8 cardial points).
116     */
117    private Direction getDirection(double angle) {
118       if ((angle >= ANGLE_NE_E && angle < ANGLE_E)
119 				|| (angle >= ANGLE_E0 && angle < ANGLE_E_SE)) {
120          return Direction.E;
121       }
122       else if (angle >= ANGLE_E_SE && angle < ANGLE_SE_S) {
123          return Direction.SE;
124       }
125       else if (angle >= ANGLE_SE_S && angle < ANGLE_S_SW) {
126          return Direction.S;
127       }
128       else if (angle >= ANGLE_S_SW && angle < ANGLE_SW_W) {
129          return Direction.SW;
130       }
131       else if (angle >= ANGLE_SW_W && angle < ANGLE_W_NW) {
132          return Direction.W;
133       }
134       else if (angle >= ANGLE_W_NW && angle < ANGLE_NW_N) {
135          return Direction.NW;
136       }
137       else if (angle >= ANGLE_NW_N && angle < ANGLE_N_NE) {
138          return Direction.N;
139       }
140       else if (angle >= ANGLE_N_NE && angle < ANGLE_NE_E) {
141          return Direction.NE;
142       }
143       throw new IllegalStateException();
144    } // getDirection
145 
146 
147    /**
148     * Returns the statistics object.
149     * 
150     * @return the statistic object.
151     */
152    public Statistics getStatistics() {
153       return statistics;
154    } // getStatistics
155 
156 
157    @Override
158    public String toString() {
159       final StringBuilder sb = new StringBuilder();
160       sb.append(Constant.OPEN_ANGULAR_BRACKET);
161       
162       for (final Direction d : directions) {
163          sb.append(d.name() + Constant.COMMA);
164       }
165       
166       sb.append(Constant.CLOSE_ANGULAR_BRACKET);
167       return sb.toString();
168    } // toString
169 
170 }