Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve topology while rounding area vertex coordinates before MC #36

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions src/main/java/ini/trakem2/utils/M.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -355,33 +356,6 @@ static public final Collection<Polygon> getPolygons(final Area area) {

return pols;
}
static public final Collection<Polygon> getPolygonsByRounding(final Area area) {
final ArrayList<Polygon> pols = new ArrayList<Polygon>();
Polygon pol = new Polygon();

final float[] coords = new float[6];
for (final PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) {
final int seg_type = pit.currentSegment(coords);
switch (seg_type) {
case PathIterator.SEG_MOVETO:
case PathIterator.SEG_LINETO:
pol.addPoint(Math.round(coords[0]), Math.round(coords[1]));
break;
case PathIterator.SEG_CLOSE:
pols.add(pol);
pol = new Polygon();
break;
default:
Utils.log2("WARNING: unhandled seg type.");
break;
}
pit.next();
if (pit.isDone()) {
break;
}
}
return pols;
}

/** Return a new Area resulting from applying @param ict to @param a;
* assumes areas consists of paths with moveTo, lineTo and close operations. */
Expand Down Expand Up @@ -490,11 +464,31 @@ static public final Area areaInInts(final Area area) {

/** Converts all points in @param area to ints by rounding. */
static public final Area areaInIntsByRounding(final Area area) {
final Area a = new Area();
for (final Polygon pol : M.getPolygonsByRounding(area)) {
a.add(new Area(pol));
final Path2D.Float pol = new Path2D.Float();
final float[] coords = new float[6];
for (final PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) {
final int seg_type = pit.currentSegment(coords);
switch (seg_type) {
case PathIterator.SEG_MOVETO:
pol.moveTo(Math.round(coords[0]), Math.round(coords[1]));
break;
case PathIterator.SEG_LINETO:
pol.lineTo(Math.round(coords[0]), Math.round(coords[1]));
break;
case PathIterator.SEG_CLOSE:
pol.closePath();
break;
default:
Utils.log2("WARNING: unhandled seg type.");
break;
}
pit.next();
if (pit.isDone()) {
break;
}
}
return a;

return new Area(pol);
}

/* ================================================= */
Expand Down
Loading