a7955960cb1e4fa7a2dc33a3aa4942ed5a416a4c
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / ScanArea.java
1 /*
2  * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
3  * 
4  * This file is part of LEA.
5  * 
6  * LEA is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option) any
9  * later version.
10  * 
11  * LEA is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  * 
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with LEA. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * 
22  * @author Florian
23  */
24 public class ScanArea {
25
26   private Point fromPoint;
27   private Point toPoint;
28   private float size;
29
30   /**
31    * Imagine you want to classify an image with 100px x 100px what would be the
32    * scanarea in this kind of image. That size gets automatically scalled to fit
33    * bigger images
34    * 
35    * @param fromPoint
36    * @param toPoint
37    */
38   public ScanArea(Point fromPoint, Point toPoint) {
39     this.fromPoint = fromPoint;
40     this.toPoint = toPoint;
41
42     this.size = (this.toPoint.x - this.fromPoint.x) * (this.toPoint.y - this.fromPoint.y);
43   }
44
45   public ScanArea(Point fromPoint, Point toPoint, float size) {
46     this.fromPoint = fromPoint;
47     this.toPoint = toPoint;
48     this.size = size;
49   }
50
51   public ScanArea(int fromX, int fromY, int width, int height) {
52     this(new Point(fromX, fromY), new Point(fromX + width, fromY + height));
53   }
54
55   public int getFromX(float scaleFactor) {
56     return (int) (this.fromPoint.x * scaleFactor);
57   }
58
59   public int getFromY(float scaleFactor) {
60     return (int) (this.fromPoint.y * scaleFactor);
61   }
62
63   public int getToX(float scaleFactor) {
64     return (int) (this.toPoint.x * scaleFactor);
65   }
66
67   public int getToY(float scaleFactor) {
68     return (int) (this.toPoint.y * scaleFactor);
69   }
70
71   public int getSize(float scaleFactor) {
72     return (int) (this.size * Math.pow(scaleFactor, 2));
73   }
74
75   @Override
76   public boolean equals(Object o) {
77     ScanArea other = (ScanArea) o;
78
79     return pointsWithin(other.fromPoint.x, other.toPoint.x, this.fromPoint.x, this.toPoint.x)
80         && pointsWithin(other.fromPoint.y, other.toPoint.y, this.fromPoint.y, this.toPoint.y);
81   }
82
83   private static boolean pointsWithin(int pointA1, int pointA2, int pointB1, int pointB2) {
84     boolean within = false;
85     within = within || (pointB1 >= pointA1 && pointB1 <= pointA2);
86     within = within || (pointB2 >= pointA1 && pointB2 <= pointA2);
87     within = within || (pointA1 >= pointB1 && pointA1 <= pointB2);
88     within = within || (pointA2 >= pointB1 && pointA2 <= pointB2);
89
90     return within;
91   }
92
93   // private boolean checkPoints(ScanArea a, ScanArea b) {
94   // Point[] pointsToCheck = new Point[] {
95   // a.fromPoint, a.toPoint,
96   // new Point (a.fromPoint.x, a.toPoint.y),
97   // new Point (a.toPoint.x, a.fromPoint.y)
98   // };
99   // for (Point point: pointsToCheck) {
100   // if (point.x >= b.fromPoint.x && point.x <= b.toPoint.x &&
101   // point.y >= b.fromPoint.y && point.y <= b.toPoint.y) return true;
102   // }
103   //
104   // return false;
105   // }
106
107   public String toString() {
108     String str = "";
109     str += "fromPoint=(" + fromPoint.x + "," + fromPoint.y + ")";
110     str += "toPoint=(" + toPoint.x + "," + toPoint.y + ")";
111     str += "size=" + size;
112     return str;
113   }
114 }