Added the class RotatedRect to Java API.

Added tests for the class.
This commit is contained in:
Leonid Beynenson
2011-07-12 15:39:38 +00:00
parent b58dc21074
commit e59f530338
6 changed files with 271 additions and 9 deletions

View File

@@ -45,4 +45,11 @@ public class Point {
public boolean inside(Rect r) {
return r.contains(this);
}
@Override
public String toString() {
if (this == null) return "null";
return "{" + x + ", " + y + "}";
}
}

View File

@@ -3,7 +3,7 @@ package org.opencv;
//javadoc:Rect_
public class Rect {
int x, y, width, height;
public int x, y, width, height;
public Rect(int x, int y, int width, int height) {
this.x = x;
@@ -24,7 +24,7 @@ public class Rect {
}
public Rect(Point p, Size s) {
this((int)p.x, (int)p.y, s.width, s.height);
this((int)p.x, (int)p.y, (int)s.width, (int)s.height);
}
public Rect clone() {
@@ -74,4 +74,10 @@ public class Rect {
Rect it = (Rect) obj;
return x == it.x && y == it.y && width == it.width && height == it.height;
}
@Override
public String toString() {
if (this == null) return "null";
return "{" + x + ", " + y + ", " + width + "x" + height+"}";
}
}

View File

@@ -0,0 +1,87 @@
package org.opencv;
//javadoc:RotatedRect_
public class RotatedRect {
public Point center;
public Size size;
public double angle;
public RotatedRect() {
this.angle=0;
}
public RotatedRect(Point c, Size s, double a) {
this.center = c.clone();
this.size = s.clone();
this.angle = a;
}
public void points(Point pt[])
{
double _angle = angle*Math.PI/180.0;
double b = (double)Math.cos(_angle)*0.5f;
double a = (double)Math.sin(_angle)*0.5f;
pt[0] = new Point(
center.x - a*size.height - b*size.width,
center.y + b*size.height - a*size.width);
pt[1] = new Point(
center.x + a*size.height - b*size.width,
center.y - b*size.height - a*size.width);
pt[2] = new Point(
2*center.x - pt[0].x,
2*center.y - pt[0].y);
pt[3] = new Point(
2*center.x - pt[1].x,
2*center.y - pt[1].y);
}
public Rect boundingRect()
{
Point pt[] = new Point[4];
points(pt);
Rect r=new Rect((int)Math.floor(Math.min(Math.min(Math.min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
(int)Math.floor(Math.min(Math.min(Math.min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
(int)Math.ceil(Math.max(Math.max(Math.max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
(int)Math.ceil(Math.max(Math.max(Math.max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
r.width -= r.x - 1;
r.height -= r.y - 1;
return r;
}
public RotatedRect clone() {
return new RotatedRect(center, size, angle);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(center.x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(center.y);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(size.width);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(size.height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(angle);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof RotatedRect)) return false;
RotatedRect it = (RotatedRect) obj;
return center.equals(it.center) && size.equals(it.size) && angle == it.angle;
}
}

View File

@@ -3,9 +3,9 @@ package org.opencv;
//javadoc:Size_
public class Size {
public int width, height;
public double width, height;
public Size(int width, int height) {
public Size(double width, double height) {
this.width = width;
this.height = height;
}
@@ -15,8 +15,8 @@ public class Size {
}
public Size(Point p) {
width = (int) p.x;
height = (int) p.y;
width = (double) p.x;
height = (double) p.y;
}
public double area() {