2 * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
4 * This file is part of LEA.
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
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
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/>.
25 @METHODDEFAULT("OUT<THIS,THIS<IN,THISLOC=THIS,RETURNLOC=OUT")
26 public class ScanArea {
29 private Point fromPoint;
31 private Point toPoint;
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
43 public ScanArea(Point fromPoint, Point toPoint) {
44 this.fromPoint = fromPoint;
45 this.toPoint = toPoint;
47 this.size = (this.toPoint.x - this.fromPoint.x) * (this.toPoint.y - this.fromPoint.y);
50 public ScanArea(Point fromPoint, Point toPoint, float size) {
51 this.fromPoint = fromPoint;
52 this.toPoint = toPoint;
56 public ScanArea(int fromX, int fromY, int width, int height) {
57 this(new Point(fromX, fromY), new Point(fromX + width, fromY + height));
60 public int getFromX(@LOC("IN") float scaleFactor) {
61 return (int) (this.fromPoint.x * scaleFactor);
64 public int getFromY(@LOC("IN") float scaleFactor) {
65 return (int) (this.fromPoint.y * scaleFactor);
68 public int getToX(@LOC("IN") float scaleFactor) {
69 return (int) (this.toPoint.x * scaleFactor);
72 public int getToY(@LOC("IN") float scaleFactor) {
73 return (int) (this.toPoint.y * scaleFactor);
76 public int getSize(@LOC("IN") float scaleFactor) {
77 return (int) (this.size * Math.pow(scaleFactor, 2));
81 public boolean equals(Object o) {
82 ScanArea other = (ScanArea) o;
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);
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);
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)
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;
112 public String toString() {
113 @LOC("OUT") String str = "";
114 str += "fromPoint=(" + fromPoint.x + "," + fromPoint.y + ")";
115 str += "toPoint=(" + toPoint.x + "," + toPoint.y + ")";
116 str += "size=" + size;