412d9e23a4e67ff61ff5b9b77028441ca9d9f79a
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTrackingInfer / 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 \r
26 public class DeviationScanner {\r
27 \r
28   private int x[];\r
29   private int y[];\r
30 \r
31   // LEFT_UP(+1, -1), UP(0, -1), RIGHT_UP(-1, -1), LEFT(+1, 0), NONE(0, 0),\r
32   // RIGHT(-1, 0), LEFT_DOWN(\r
33   // +1, +1), DOWN(0, +1), RIGHT_DOWN(-1, +1);\r
34 \r
35   public static final int LEFT_UP = 0;\r
36   public static final int UP = 1;\r
37   public static final int RIGHT_UP = 2;\r
38   public static final int LEFT = 3;\r
39   public static final int NONE = 4;\r
40   public static final int RIGHT = 5;\r
41   public static final int LEFT_DOWN = 6;\r
42   public static final int DOWN = 7;\r
43   public static final int RIGHT_DOWN = 8;\r
44 \r
45   public DeviationScanner() {\r
46     x = new int[3];\r
47     y = new int[3];\r
48     SSJAVA.arrayinit(x, -1);\r
49     SSJAVA.arrayinit(y, -1);\r
50   }\r
51 \r
52   public void addEyePosition(int inx, int iny) {\r
53     SSJAVA.append(x, inx);\r
54     SSJAVA.append(y, iny);\r
55   }\r
56 \r
57   public int scanForDeviation(Rectangle2D faceRect) {\r
58 \r
59     int deviation = NONE;\r
60 \r
61     for (int i = 0; i < 3; i++) {\r
62       if (x[i] == -1) {\r
63         return deviation;\r
64       }\r
65     }\r
66 \r
67     double deviationX = 0;\r
68     double deviationY = 0;\r
69 \r
70     int lastIdx = -1;\r
71     for (int i = 0; i < 3; ++i) {\r
72       if (lastIdx != -1) {\r
73         deviationX += (x[i] - x[lastIdx]);\r
74         deviationY += (y[i] - y[lastIdx]);\r
75       }\r
76       lastIdx = i;\r
77     }\r
78 \r
79     final double deviationPercentX = 0.04;\r
80     final double deviationPercentY = 0.04;\r
81 \r
82     deviationX /= faceRect.getWidth();\r
83     deviationY /= faceRect.getWidth();\r
84 \r
85     int deviationAbsoluteX = 0;\r
86     int deviationAbsoluteY = 0;\r
87     if (deviationX > deviationPercentX)\r
88       deviationAbsoluteX = 1;\r
89     if (deviationX < -deviationPercentX)\r
90       deviationAbsoluteX = -1;\r
91     if (deviationY > deviationPercentY)\r
92       deviationAbsoluteY = 1;\r
93     if (deviationY < -deviationPercentY)\r
94       deviationAbsoluteY = -1;\r
95 \r
96     deviation = getDirectionFor(deviationAbsoluteX, deviationAbsoluteY);\r
97 \r
98     if (deviation != NONE) {\r
99       SSJAVA.arrayinit(x, -1);\r
100       SSJAVA.arrayinit(y, -1);\r
101     }\r
102 \r
103     return deviation;\r
104   }\r
105 \r
106   public int getDirectionFor(int directionX, int directionY) {\r
107 \r
108     if (directionX == +1 && directionY == -1) {\r
109       return LEFT_UP;\r
110     } else if (directionX == 0 && directionY == -1) {\r
111       return UP;\r
112     } else if (directionX == -1 && directionY == -1) {\r
113       return RIGHT_UP;\r
114     } else if (directionX == +1 && directionY == 0) {\r
115       return LEFT;\r
116     } else if (directionX == 0 && directionY == 0) {\r
117       return NONE;\r
118     } else if (directionX == -1 && directionY == 0) {\r
119       return RIGHT;\r
120     } else if (directionX == +1 && directionY == +1) {\r
121       return LEFT_DOWN;\r
122     } else if (directionX == 0 && directionY == +1) {\r
123       return DOWN;\r
124     } else if (directionX == -1 && directionY == +1) {\r
125       return RIGHT_DOWN;\r
126     }\r
127 \r
128     return -1;\r
129   }\r
130 \r
131   public String toStringDeviation(int dev) {\r
132     if (dev == LEFT_UP) {\r
133       return "LEFT_UP";\r
134     } else if (dev == UP) {\r
135       return "UP";\r
136     } else if (dev == RIGHT_UP) {\r
137       return "RIGHT_UP";\r
138     } else if (dev == LEFT) {\r
139       return "LEFT";\r
140     } else if (dev == NONE) {\r
141       return "NONE";\r
142     } else if (dev == RIGHT) {\r
143       return "RIGHT";\r
144     } else if (dev == LEFT_DOWN) {\r
145       return "LEFT_DOWN";\r
146     } else if (dev == DOWN) {\r
147       return "DOWN";\r
148     } else if (dev == RIGHT_DOWN) {\r
149       return "RIGHT_DOWN";\r
150     }\r
151     return "ERROR";\r
152   }\r
153 \r
154 }\r