abb581b04e81c6d9f969bcfff7edf1c3967b54bd
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTrackingInfer / 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
25
26 public class ScanArea {
27
28   
29   private Point fromPoint;
30   
31   private Point toPoint;
32   
33   private float size;
34
35   /**
36    * Imagine you want to classify an image with 100px x 100px what would be the
37    * scanarea in this kind of image. That size gets automatically scalled to fit
38    * bigger images
39    * 
40    * @param fromPoint
41    * @param toPoint
42    */
43   public ScanArea(Point fromPoint, Point toPoint) {
44     this.fromPoint = fromPoint;
45     this.toPoint = toPoint;
46
47     this.size = (this.toPoint.x - this.fromPoint.x) * (this.toPoint.y - this.fromPoint.y);
48   }
49
50   public ScanArea(Point fromPoint, Point toPoint, float size) {
51     this.fromPoint = fromPoint;
52     this.toPoint = toPoint;
53     this.size = size;
54   }
55
56   public ScanArea(int fromX, int fromY, int width, int height) {
57     this(new Point(fromX, fromY), new Point(fromX + width, fromY + height));
58   }
59
60   public int getFromX( float scaleFactor) {
61     return (int) (this.fromPoint.x * scaleFactor);
62   }
63
64   public int getFromY( float scaleFactor) {
65     return (int) (this.fromPoint.y * scaleFactor);
66   }
67
68   public int getToX( float scaleFactor) {
69     return (int) (this.toPoint.x * scaleFactor);
70   }
71
72   public int getToY( float scaleFactor) {
73     return (int) (this.toPoint.y * scaleFactor);
74   }
75
76   public int getSize( float scaleFactor) {
77     return (int) (this.size * Math.pow(scaleFactor, 2));
78   }
79
80   @Override
81   public boolean equals(Object o) {
82     ScanArea other = (ScanArea) o;
83
84     return pointsWithin(other.fromPoint.x, other.toPoint.x, this.fromPoint.x, this.toPoint.x)
85         && pointsWithin(other.fromPoint.y, other.toPoint.y, this.fromPoint.y, this.toPoint.y);
86   }
87
88   private static boolean pointsWithin(int pointA1, int pointA2, int pointB1, int pointB2) {
89     boolean within = false;
90     within = within || (pointB1 >= pointA1 && pointB1 <= pointA2);
91     within = within || (pointB2 >= pointA1 && pointB2 <= pointA2);
92     within = within || (pointA1 >= pointB1 && pointA1 <= pointB2);
93     within = within || (pointA2 >= pointB1 && pointA2 <= pointB2);
94
95     return within;
96   }
97
98   // private boolean checkPoints(ScanArea a, ScanArea b) {
99   // Point[] pointsToCheck = new Point[] {
100   // a.fromPoint, a.toPoint,
101   // new Point (a.fromPoint.x, a.toPoint.y),
102   // new Point (a.toPoint.x, a.fromPoint.y)
103   // };
104   // for (Point point: pointsToCheck) {
105   // if (point.x >= b.fromPoint.x && point.x <= b.toPoint.x &&
106   // point.y >= b.fromPoint.y && point.y <= b.toPoint.y) return true;
107   // }
108   //
109   // return false;
110   // }
111
112   public String toString() {
113      String str = "";
114     str += "fromPoint=(" + fromPoint.x + "," + fromPoint.y + ")";
115     str += "toPoint=(" + toPoint.x + "," + toPoint.y + ")";
116     str += "size=" + size;
117     return str;
118   }
119 }