use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.
the class AxisAlignedBoundingBox method getOrientationVector.
private double[] getOrientationVector(Iterable<Pnt2d> points) {
double[] centroid = getCentroid(points);
final double xc = centroid[0];
final double yc = centroid[1];
double mu20 = 0;
double mu02 = 0;
double mu11 = 0;
for (Pnt2d p : points) {
double dx = (p.getX() - xc);
double dy = (p.getY() - yc);
mu20 = mu20 + dx * dx;
mu02 = mu02 + dy * dy;
mu11 = mu11 + dx * dy;
}
double A = 2 * mu11;
double B = mu20 - mu02;
double xTheta = B + sqrt(sqr(A) + sqr(B));
double yTheta = A;
double d = radius(xTheta, yTheta);
return (isZero(d)) ? null : new double[] { xTheta / d, yTheta / d };
}
use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.
the class AxisAlignedBoundingBox method getCentroid.
private double[] getCentroid(Iterable<Pnt2d> points) {
int n = 0;
double su = 0;
double sv = 0;
for (Pnt2d p : points) {
su += p.getX();
sv += p.getY();
n++;
}
if (n == 0) {
throw new IllegalArgumentException("empty point sequence!");
}
return new double[] { su / n, sv / n };
}
use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.
the class AxisAlignedBoundingBox method makeBox.
/**
* Calculates the major axis-aligned bounding box of
* the supplied region, as a sequence of four point
* coordinates (p0, p1, p2, p3).
*
* @param points binary region
* @return the region's bounding box as a sequence of 4 coordinates (p0, p1, p2, p3)
*/
private Pnt2d[] makeBox(Iterable<Pnt2d> points) {
// double theta = getOrientationAngle(points);
double[] xy = getOrientationVector(points);
if (xy == null) {
// regin's orientation is undefined
return null;
}
// = Math.cos(theta);
double xa = xy[0];
// = Math.sin(theta);
double ya = xy[1];
double[] ea = { xa, ya };
double[] eb = { ya, -xa };
double amin = Double.POSITIVE_INFINITY;
double amax = Double.NEGATIVE_INFINITY;
double bmin = Double.POSITIVE_INFINITY;
double bmax = Double.NEGATIVE_INFINITY;
for (Pnt2d p : points) {
double u = p.getX();
double v = p.getY();
// project (u,v) on the major axis vector
double a = u * xa + v * ya;
// project (u,v) on perpendicular vector
double b = u * ya - v * xa;
amin = Math.min(a, amin);
amax = Math.max(a, amax);
bmin = Math.min(b, bmin);
bmax = Math.max(b, bmax);
}
Pnt2d[] corners = new Pnt2d[4];
corners[0] = PntDouble.from(add(multiply(amin, ea), multiply(bmin, eb)));
corners[1] = PntDouble.from(add(multiply(amin, ea), multiply(bmax, eb)));
corners[2] = PntDouble.from(add(multiply(amax, ea), multiply(bmax, eb)));
corners[3] = PntDouble.from(add(multiply(amax, ea), multiply(bmin, eb)));
return corners;
}
use of imagingbook.pub.geometry.basic.Pnt2d in project imagingbook-common by imagingbook.
the class Contour method getPolygonPath.
/**
* Get the polygon for this contour (for subsequent drawing).
* An offset can be specified for shifting the contour positions
* at pixel centers (set to 0.5/0.5).
*
* @param xOffset the horizontal offset.
* @param yOffset the vertical offset.
* @return a polygon.
*/
public Path2D getPolygonPath(double xOffset, double yOffset) {
Path2D path = new Path2D.Float();
Pnt2d[] pnts = this.getPointArray();
if (pnts.length > 1) {
path.moveTo(pnts[0].getX() + xOffset, pnts[0].getY() + yOffset);
for (int i = 1; i < pnts.length; i++) {
path.lineTo(pnts[i].getX() + xOffset, pnts[i].getY() + yOffset);
}
path.closePath();
} else {
// mark single pixel region "X"
double x = pnts[0].getX();
double y = pnts[0].getY();
path.moveTo(x + xOffset - 0.5, y + yOffset - 0.5);
path.lineTo(x + xOffset + 0.5, y + yOffset + 0.5);
path.moveTo(x + xOffset - 0.5, y + yOffset + 0.5);
path.lineTo(x + xOffset + 0.5, y + yOffset - 0.5);
}
return path;
}
Aggregations