use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class DefaultMinorMajorAxis method calculate.
@Override
public Pair<DoubleType, DoubleType> calculate(final Polygon2D input) {
List<RealLocalizable> points = new ArrayList<>(GeomUtils.vertices(input));
// Sort RealLocalizables of P by x-coordinate (in case of a tie,
// sort by
// y-coordinate). Sorting is counter clockwise.
Collections.sort(points, new Comparator<RealLocalizable>() {
@Override
public int compare(final RealLocalizable o1, final RealLocalizable o2) {
final Double o1x = new Double(o1.getDoublePosition(0));
final Double o2x = new Double(o2.getDoublePosition(0));
final int result = o2x.compareTo(o1x);
if (result == 0) {
return new Double(o2.getDoublePosition(1)).compareTo(new Double(o1.getDoublePosition(1)));
}
return result;
}
});
points.add(points.get(0));
// calculate minor and major axis
double[] minorMajorAxis = getMinorMajorAxis(input, points);
return new ValuePair<>(new DoubleType(minorMajorAxis[0]), new DoubleType(minorMajorAxis[1]));
}
use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class DefaultSmallestEnclosingRectangle method rotate.
/**
* Rotates the given Polygon2D consisting of a list of RealPoints by the
* given angle about the given center.
*
* @param inPoly A Polygon2D consisting of a list of RealPoint RealPoints
* @param angle the rotation angle
* @param center the rotation center
* @return a rotated polygon
*/
private Polygon2D rotate(final Polygon2D inPoly, final double angle, final RealLocalizable center) {
List<RealLocalizable> out = new ArrayList<>();
for (RealLocalizable RealPoint : GeomUtils.vertices(inPoly)) {
// double angleInRadians = Math.toRadians(angleInDegrees);
double cosTheta = Math.cos(angle);
double sinTheta = Math.sin(angle);
double x = cosTheta * (RealPoint.getDoublePosition(0) - center.getDoublePosition(0)) - sinTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(0);
double y = sinTheta * (RealPoint.getDoublePosition(0) - center.getDoublePosition(0)) + cosTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(1);
out.add(new RealPoint(x, y));
}
return new DefaultWritablePolygon2D(out);
}
use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class DefaultBoundingBox method calculate.
@Override
public Polygon2D calculate(final Polygon2D input) {
double min_x = Double.POSITIVE_INFINITY;
double max_x = Double.NEGATIVE_INFINITY;
double min_y = Double.POSITIVE_INFINITY;
double max_y = Double.NEGATIVE_INFINITY;
for (final RealLocalizable rl : GeomUtils.vertices(input)) {
if (rl.getDoublePosition(0) < min_x) {
min_x = rl.getDoublePosition(0);
}
if (rl.getDoublePosition(0) > max_x) {
max_x = rl.getDoublePosition(0);
}
if (rl.getDoublePosition(1) < min_y) {
min_y = rl.getDoublePosition(1);
}
if (rl.getDoublePosition(1) > max_y) {
max_y = rl.getDoublePosition(1);
}
}
final List<RealLocalizable> bounds = new ArrayList<>();
bounds.add(new RealPoint(min_x, min_y));
bounds.add(new RealPoint(min_x, max_y));
bounds.add(new RealPoint(max_x, max_y));
bounds.add(new RealPoint(max_x, min_y));
return new DefaultWritablePolygon2D(bounds);
}
use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class DefaultFeretsDiameterForAngle method compute.
@Override
public void compute(Polygon2D input, DoubleType output) {
final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
final double angleRad = -angle * Math.PI / 180.0;
double minX = Double.POSITIVE_INFINITY;
double maxX = Double.NEGATIVE_INFINITY;
for (RealLocalizable p : points) {
final double tmpX = p.getDoublePosition(0) * Math.cos(angleRad) - p.getDoublePosition(1) * Math.sin(angleRad);
minX = tmpX < minX ? tmpX : minX;
maxX = tmpX > maxX ? tmpX : maxX;
}
output.set(Math.abs(maxX - minX));
}
use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class DefaultMaximumFeret method calculate.
@Override
public Pair<RealLocalizable, RealLocalizable> calculate(Polygon2D input) {
final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
double distance = Double.NEGATIVE_INFINITY;
RealLocalizable p0 = points.get(0);
RealLocalizable p1 = points.get(0);
for (int i = 0; i < points.size(); i++) {
for (int j = i + 2; j < points.size(); j++) {
final RealLocalizable tmpP0 = points.get(i);
final RealLocalizable tmpP1 = points.get(j);
final double tmp = Math.sqrt(Math.pow(tmpP0.getDoublePosition(0) - tmpP1.getDoublePosition(0), 2) + Math.pow(tmpP0.getDoublePosition(1) - tmpP1.getDoublePosition(1), 2));
if (tmp > distance) {
distance = tmp;
p0 = tmpP0;
p1 = tmpP1;
}
}
}
return new ValuePair<>(p0, p1);
}
Aggregations