c635d89a9508558c793ec5fd7301cf5a0379046b
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / DeviationScanner.java
1 /*\r
2  * Copyright 2009 (c) Florian Frankenberger (darkblue.de)\r
3  * \r
4  * This file is part of LEA.\r
5  * \r
6  * LEA is free software: you can redistribute it and/or modify it under the\r
7  * terms of the GNU Lesser General Public License as published by the Free\r
8  * Software Foundation, either version 3 of the License, or (at your option) any\r
9  * later version.\r
10  * \r
11  * LEA is distributed in the hope that it will be useful, but WITHOUT ANY\r
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR\r
13  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more\r
14  * details.\r
15  * \r
16  * You should have received a copy of the GNU Lesser General Public License\r
17  * along with LEA. If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 \r
20 /**\r
21  * No description given.\r
22  * \r
23  * @author Florian Frankenberger\r
24  */\r
25 @LATTICE("DEV<C,C<SIZE,SIZE*,C*,DEV*")\r
26 @METHODDEFAULT("OUT<THIS,THIS<IN,THISLOC=THIS,RETURNLOC=OUT")\r
27 public class DeviationScanner {\r
28 \r
29   @LOC("DEV")\r
30   private int x[];\r
31   @LOC("DEV")\r
32   private int y[];\r
33 \r
34   public static final int LEFT_UP = 0;\r
35   public static final int UP = 1;\r
36   public static final int RIGHT_UP = 2;\r
37   public static final int LEFT = 3;\r
38   public static final int NONE = 4;\r
39   public static final int RIGHT = 5;\r
40   public static final int LEFT_DOWN = 6;\r
41   public static final int DOWN = 7;\r
42   public static final int RIGHT_DOWN = 8;\r
43 \r
44   public DeviationScanner() {\r
45     x = new int[3];\r
46     y = new int[3];\r
47     SSJAVA.arrayinit(x, -1);\r
48     SSJAVA.arrayinit(y, -1);\r
49   }\r
50 \r
51   @LATTICE("THIS<IN,THISLOC=THIS")\r
52   public void addEyePosition(@LOC("IN") int inx, @LOC("IN") int iny) {\r
53     SSJAVA.append(x, inx);\r
54     SSJAVA.append(y, iny);\r
55   }\r
56 \r
57   @LATTICE("THIS<C,THIS<IN,THISLOC=THIS,C*")\r
58   @RETURNLOC("THIS,DeviationScanner.DEV")\r
59   public int scanForDeviation(@LOC("IN") Rectangle2D faceRect) {\r
60 \r
61     @LOC("THIS,DeviationScanner.DEV") int deviation = NONE;\r
62 \r
63     for (@LOC("C") int i = 0; i < 3; i++) {\r
64       if (x[i] == -1) {\r
65         return deviation;\r
66       }\r
67     }\r
68 \r
69     @LOC("THIS,DeviationScanner.DEV") double deviationX = 0;\r
70     @LOC("THIS,DeviationScanner.DEV") double deviationY = 0;\r
71 \r
72     @LOC("THIS,DeviationScanner.DEV") int lastIdx = -1;\r
73     for (@LOC("THIS,DeviationScanner.DEV") int i = 0; i < 3; ++i) {\r
74       if (lastIdx != -1) {\r
75         deviationX += (x[i] - x[lastIdx]);\r
76         deviationY += (y[i] - y[lastIdx]);\r
77       }\r
78       lastIdx = i;\r
79     }\r
80 \r
81     @LOC("THIS,DeviationScanner.DEV") final double deviationPercentX = 0.04;\r
82     @LOC("THIS,DeviationScanner.DEV") final double deviationPercentY = 0.04;\r
83 \r
84     deviationX /= faceRect.getWidth();\r
85     deviationY /= faceRect.getWidth();\r
86 \r
87     @LOC("THIS,DeviationScanner.DEV") int deviationAbsoluteX = 0;\r
88     @LOC("THIS,DeviationScanner.DEV") int deviationAbsoluteY = 0;\r
89     if (deviationX > deviationPercentX)\r
90       deviationAbsoluteX = 1;\r
91     if (deviationX < -deviationPercentX)\r
92       deviationAbsoluteX = -1;\r
93     if (deviationY > deviationPercentY)\r
94       deviationAbsoluteY = 1;\r
95     if (deviationY < -deviationPercentY)\r
96       deviationAbsoluteY = -1;\r
97 \r
98     deviation = getDirectionFor(deviationAbsoluteX, deviationAbsoluteY);\r
99 \r
100     if (deviation != NONE) {\r
101       SSJAVA.arrayinit(x, -1);\r
102       SSJAVA.arrayinit(y, -1);\r
103     }\r
104 \r
105     return deviation;\r
106   }\r
107 \r
108   @LATTICE("OUT<IN,OUT<THIS,THISLOC=THIS,RETURNLOC=OUT")\r
109   public int getDirectionFor(@LOC("IN") int directionX, @LOC("IN") int directionY) {\r
110 \r
111     if (directionX == +1 && directionY == -1) {\r
112       return LEFT_UP;\r
113     } else if (directionX == 0 && directionY == -1) {\r
114       return UP;\r
115     } else if (directionX == -1 && directionY == -1) {\r
116       return RIGHT_UP;\r
117     } else if (directionX == +1 && directionY == 0) {\r
118       return LEFT;\r
119     } else if (directionX == 0 && directionY == 0) {\r
120       return NONE;\r
121     } else if (directionX == -1 && directionY == 0) {\r
122       return RIGHT;\r
123     } else if (directionX == +1 && directionY == +1) {\r
124       return LEFT_DOWN;\r
125     } else if (directionX == 0 && directionY == +1) {\r
126       return DOWN;\r
127     } else if (directionX == -1 && directionY == +1) {\r
128       return RIGHT_DOWN;\r
129     }\r
130 \r
131     return -1;\r
132   }\r
133 \r
134   public String toStringDeviation(@LOC("IN") int dev) {\r
135     if (dev == LEFT_UP) {\r
136       return "LEFT_UP";\r
137     } else if (dev == UP) {\r
138       return "UP";\r
139     } else if (dev == RIGHT_UP) {\r
140       return "RIGHT_UP";\r
141     } else if (dev == LEFT) {\r
142       return "LEFT";\r
143     } else if (dev == NONE) {\r
144       return "NONE";\r
145     } else if (dev == RIGHT) {\r
146       return "RIGHT";\r
147     } else if (dev == LEFT_DOWN) {\r
148       return "LEFT_DOWN";\r
149     } else if (dev == DOWN) {\r
150       return "DOWN";\r
151     } else if (dev == RIGHT_DOWN) {\r
152       return "RIGHT_DOWN";\r
153     }\r
154     return "ERROR";\r
155   }\r
156 \r
157 }\r