use of net.imglib2.RealPoint in project imagej-ops by imagej.
the class DefaultMinimumFeret method calculate.
@Override
public Pair<RealLocalizable, RealLocalizable> calculate(Polygon2D input) {
final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
double distance = Double.POSITIVE_INFINITY;
RealLocalizable p0 = points.get(0);
RealLocalizable p1 = points.get(0);
double tmpDist = 0;
RealLocalizable tmpP0 = p0;
RealLocalizable tmpP1 = p1;
for (int i = 0; i < points.size() - 2; i++) {
final RealLocalizable lineStart = points.get(i);
final RealLocalizable lineEnd = points.get(i + 1);
tmpDist = 0;
final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
for (int j = 0; j < points.size(); j++) {
if (j != i && j != i + 1) {
final RealLocalizable ttmpP0 = points.get(j);
final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
if (tmp > tmpDist) {
tmpDist = tmp;
final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
tmpP0 = new RealPoint(vp.getX(), vp.getY());
tmpP1 = ttmpP0;
}
}
}
if (tmpDist < distance) {
distance = tmpDist;
p0 = tmpP0;
p1 = tmpP1;
}
}
final RealLocalizable lineStart = points.get(points.size() - 1);
final RealLocalizable lineEnd = points.get(0);
final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
tmpDist = 0;
for (int j = 0; j < points.size(); j++) {
if (j != points.size() - 1 && j != 0 + 1) {
final RealLocalizable ttmpP0 = points.get(j);
final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
if (tmp > tmpDist) {
tmpDist = tmp;
final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
tmpP0 = new RealPoint(vp.getX(), vp.getY());
tmpP1 = ttmpP0;
}
}
}
if (tmpDist < distance) {
distance = tmpDist;
p0 = tmpP0;
p1 = tmpP1;
}
return new ValuePair<>(p0, p1);
}
use of net.imglib2.RealPoint in project imagej-ops by imagej.
the class CentroidII method calculate.
@Override
public RealLocalizable calculate(final IterableInterval<?> input) {
int numDimensions = input.numDimensions();
double[] output = new double[numDimensions];
Cursor<?> c = input.localizingCursor();
double[] pos = new double[numDimensions];
while (c.hasNext()) {
c.fwd();
c.localize(pos);
for (int i = 0; i < output.length; i++) {
output[i] += pos[i];
}
}
for (int i = 0; i < output.length; i++) {
output[i] = output[i] / input.size();
}
return new RealPoint(output);
}
use of net.imglib2.RealPoint in project imagej-ops by imagej.
the class CentroidMesh method calculate.
@Override
public RealLocalizable calculate(final Mesh input) {
double c_x = 0;
double c_y = 0;
double c_z = 0;
for (int i = 0; i < input.getFacets().size(); i++) {
TriangularFacet f = (TriangularFacet) input.getFacets().get(i);
Vector3D normal = f.getNormal();
Vector3D a = f.getP0();
Vector3D b = f.getP1();
Vector3D c = f.getP2();
c_x += (1 / 24d) * normal.getX() * (Math.pow((a.getX() + b.getX()), 2) + Math.pow((b.getX() + c.getX()), 2) + Math.pow((c.getX() + a.getX()), 2));
c_y += (1 / 24d) * normal.getY() * (Math.pow((a.getY() + b.getY()), 2) + Math.pow((b.getY() + c.getY()), 2) + Math.pow((c.getY() + a.getY()), 2));
c_z += (1 / 24d) * normal.getZ() * (Math.pow((a.getZ() + b.getZ()), 2) + Math.pow((b.getZ() + c.getZ()), 2) + Math.pow((c.getZ() + a.getZ()), 2));
}
double d = 1 / (2 * sizeFunc.calculate(input).get());
c_x *= d;
c_y *= d;
c_z *= d;
return new RealPoint(-c_x, -c_y, -c_z);
}
use of net.imglib2.RealPoint in project imagej-ops by imagej.
the class CentroidPolygon method calculate.
@Override
public RealLocalizable calculate(final Polygon2D input) {
double area = sizeFunc.calculate(input).get();
double cx = 0;
double cy = 0;
for (int i = 0; i < input.numVertices(); i++) {
RealLocalizable p0 = input.vertex(i);
RealLocalizable p1 = input.vertex((i + 1) % input.numVertices());
double p0_x = p0.getDoublePosition(0);
double p0_y = p0.getDoublePosition(1);
double p1_x = p1.getDoublePosition(0);
double p1_y = p1.getDoublePosition(1);
cx += (p0_x + p1_x) * (p0_x * p1_y - p1_x * p0_y);
cy += (p0_y + p1_y) * (p0_x * p1_y - p1_x * p0_y);
}
return new RealPoint(cx / (area * 6), cy / (area * 6));
}
use of net.imglib2.RealPoint in project imagej-ops by imagej.
the class PolygonFeatureTests method boundingBox.
@Test
public void boundingBox() {
// ground truth verified with matlab
final List<? extends RealLocalizable> received = GeomUtils.vertices(((Polygon2D) ops.run(DefaultBoundingBox.class, contour)));
final RealPoint[] expected = new RealPoint[] { new RealPoint(1, 6), new RealPoint(1, 109), new RealPoint(78, 109), new RealPoint(78, 6) };
assertEquals("Number of polygon points differs.", expected.length, received.size());
for (int i = 0; i < expected.length; i++) {
assertEquals("Polygon point " + i + " differs in x-coordinate.", expected[i].getDoublePosition(0), received.get(i).getDoublePosition(0), EPSILON);
assertEquals("Polygon point " + i + " differs in y-coordinate.", expected[i].getDoublePosition(1), received.get(i).getDoublePosition(1), EPSILON);
}
}
Aggregations