use of net.imglib2.roi.geom.real.DefaultWritablePolygon2D 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.DefaultWritablePolygon2D 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.DefaultWritablePolygon2D in project imagej-ops by imagej.
the class DefaultContour method calculate.
@Override
public Polygon2D calculate(final RandomAccessibleInterval<B> input) {
List<RealPoint> p = new ArrayList<>();
final B var = Util.getTypeFromInterval(input).createVariable();
final RandomAccess<B> raInput = Views.extendValue(input, var).randomAccess();
final Cursor<B> cInput = Views.flatIterable(input).cursor();
final ClockwiseMooreNeighborhoodIterator<B> cNeigh = new ClockwiseMooreNeighborhoodIterator<>(raInput);
double[] position = new double[2];
double[] startPos = new double[2];
// find first true pixel
while (cInput.hasNext()) {
// we are looking for a true pixel
if (cInput.next().get()) {
raInput.setPosition(cInput);
raInput.localize(startPos);
// add to polygon
p.add(new RealPoint(startPos[0], startPos[1]));
// backtrack:
raInput.move(-1, 0);
cNeigh.reset();
while (cNeigh.hasNext()) {
if (cNeigh.next().get()) {
boolean specialBacktrack = false;
raInput.localize(position);
if (startPos[0] == position[0] && startPos[1] == position[1]) {
// startPoint was found.
if (useJacobs) {
// Jacobs stopping criteria
final int index = cNeigh.getIndex();
if (index == 1 || index == 0) {
// Jonathans refinement to
// non-terminating jacobs criteria
specialBacktrack = true;
} else if (index == 2 || index == 3) {
// way.
break;
}
// else criteria not fulfilled, continue.
} else {
break;
}
}
// add found point to polygon
p.add(new RealPoint(position[0], position[1]));
if (specialBacktrack) {
cNeigh.backtrackSpecial();
} else {
cNeigh.backtrack();
}
}
}
// we only need to extract one contour.
break;
}
}
return new DefaultWritablePolygon2D(p);
}
use of net.imglib2.roi.geom.real.DefaultWritablePolygon2D in project imagej-ops by imagej.
the class DefaultConvexHull2D method calculate.
@Override
public Polygon2D calculate(final Polygon2D input) {
// create a copy of points because se will get resorted, etc.
List<? extends RealLocalizable> RealPoints = new ArrayList<>(GeomUtils.vertices(input));
// Sort RealPoints of P by x-coordinate (in case of a tie, sort by
// y-coordinate).
Collections.sort(RealPoints, 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 = o1x.compareTo(o2x);
if (result == 0) {
return new Double(o1.getDoublePosition(1)).compareTo(new Double(o2.getDoublePosition(1)));
}
return result;
}
});
// Initialize U and L as empty lists.
// lists will hold vertices of upper and lower hulls
// respectively.
final List<RealLocalizable> U = new ArrayList<>();
final List<RealLocalizable> L = new ArrayList<>();
// build lower hull
for (final RealLocalizable p : RealPoints) {
// turn: remove last RealPoint from L
while (L.size() >= 2 && ccw(L.get(L.size() - 2), L.get(L.size() - 1), p) <= 0) {
L.remove(L.size() - 1);
}
L.add(p);
}
// build upper hull
Collections.reverse(RealPoints);
for (final RealLocalizable p : RealPoints) {
// turn: remove last RealPoint from U
while (U.size() >= 2 && ccw(U.get(U.size() - 2), U.get(U.size() - 1), p) <= 0) {
U.remove(U.size() - 1);
}
U.add(p);
}
// Remove last RealPoint of each list (it's same as first
// RealPoint
// of or list).
L.remove(L.size() - 1);
U.remove(U.size() - 1);
// concatenate L and U
L.addAll(U);
return new DefaultWritablePolygon2D(L);
}
use of net.imglib2.roi.geom.real.DefaultWritablePolygon2D in project imagej-ops by imagej.
the class AbstractFeatureTest method getPolygon.
protected static Polygon2D getPolygon() {
final List<RealPoint> vertices = new ArrayList<>();
try {
Files.lines(Paths.get(AbstractFeatureTest.class.getResource("2d_geometric_features_polygon.txt").toURI())).forEach(l -> {
String[] coord = l.split(" ");
RealPoint v = new RealPoint(new double[] { Double.parseDouble(coord[0]), Double.parseDouble(coord[1]) });
vertices.add(v);
});
} catch (IOException | URISyntaxException exc) {
exc.printStackTrace();
}
return new DefaultWritablePolygon2D(vertices);
}
Aggregations