Search in sources :

Example 1 with LineSegment2D_F64

use of georegression.struct.line.LineSegment2D_F64 in project BoofCV by lessthanoptimal.

the class AdjustPolygonForThresholdBias method process.

/**
 * Processes and adjusts the polygon
 *
 * @param polygon The polygon that is to be adjusted. Modified.
 * @param clockwise Is the polygon in a lockwise orientation?
 */
public void process(Polygon2D_F64 polygon, boolean clockwise) {
    int N = polygon.size();
    segments.resize(N);
    // Apply the adjustment independently to each side
    for (int i = N - 1, j = 0; j < N; i = j, j++) {
        int ii, jj;
        if (clockwise) {
            ii = i;
            jj = j;
        } else {
            ii = j;
            jj = i;
        }
        Point2D_F64 a = polygon.get(ii), b = polygon.get(jj);
        double dx = b.x - a.x;
        double dy = b.y - a.y;
        double l = Math.sqrt(dx * dx + dy * dy);
        // only needs to be shifted in two directions
        if (dx < 0)
            dx = 0;
        if (dy > 0)
            dy = 0;
        LineSegment2D_F64 s = segments.get(ii);
        s.a.x = a.x - dy / l;
        s.a.y = a.y + dx / l;
        s.b.x = b.x - dy / l;
        s.b.y = b.y + dx / l;
    }
    // Find the intersection between the adjusted lines to convert it back into polygon format
    for (int i = N - 1, j = 0; j < N; i = j, j++) {
        int ii, jj;
        if (clockwise) {
            ii = i;
            jj = j;
        } else {
            ii = j;
            jj = i;
        }
        UtilLine2D_F64.convert(segments.get(ii), ga);
        UtilLine2D_F64.convert(segments.get(jj), gb);
        if (null != Intersection2D_F64.intersection(ga, gb, intersection)) {
            // very acute angles can cause a large delta. This is conservative and prevents that
            if (intersection.distance2(polygon.get(jj)) < 20) {
                polygon.get(jj).set(intersection);
            }
        }
    }
}
Also used : LineSegment2D_F64(georegression.struct.line.LineSegment2D_F64) Point2D_F64(georegression.struct.point.Point2D_F64)

Example 2 with LineSegment2D_F64

use of georegression.struct.line.LineSegment2D_F64 in project BoofCV by lessthanoptimal.

the class TestPolylineSplitMerge method assignLine_segment.

@Test
public void assignLine_segment() {
    List<Point2D_I32> contour = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        contour.add(new Point2D_I32(i, 2));
    }
    // make these points offset from all the others. That way if it grabs the wrong points the line will be wrong
    contour.get(1).set(1, 5);
    contour.get(9).set(9, 5);
    LineSegment2D_F64 line = new LineSegment2D_F64();
    PolylineSplitMerge.assignLine(contour, 1, 9, line);
    assertEquals(1, Distance2D_F64.distanceSq(line, 0, 5), GrlConstants.TEST_F64);
    assertEquals(0, Distance2D_F64.distanceSq(line, 2, 5), GrlConstants.TEST_F64);
    assertEquals(0, Distance2D_F64.distanceSq(line, 8, 5), GrlConstants.TEST_F64);
}
Also used : LineSegment2D_F64(georegression.struct.line.LineSegment2D_F64) ArrayList(java.util.ArrayList) Point2D_I32(georegression.struct.point.Point2D_I32) Test(org.junit.Test)

Example 3 with LineSegment2D_F64

use of georegression.struct.line.LineSegment2D_F64 in project BoofCV by lessthanoptimal.

the class TestSnapToLineEdge method easy_aligned.

public void easy_aligned(Class imageType) {
    setup(null, imageType);
    SnapToLineEdge alg = new SnapToLineEdge(10, 2, imageType);
    alg.setImage(image);
    int r = 2;
    LineSegment2D_F64 bottom = new LineSegment2D_F64(x1 - r, y0, x0 + r, y0);
    LineSegment2D_F64 left = new LineSegment2D_F64(x0, y0 + r, x0, y1 - r);
    LineSegment2D_F64 top = new LineSegment2D_F64(x0 + r, y1, x1 - r, y1);
    LineSegment2D_F64 right = new LineSegment2D_F64(x1, y1 - r, x1, y0 + r);
    differentInitial(alg, bottom);
    differentInitial(alg, left);
    differentInitial(alg, top);
    differentInitial(alg, right);
}
Also used : LineSegment2D_F64(georegression.struct.line.LineSegment2D_F64)

Example 4 with LineSegment2D_F64

use of georegression.struct.line.LineSegment2D_F64 in project BoofCV by lessthanoptimal.

the class TestSnapToLineEdge method localToGlobal.

@Test
public void localToGlobal() {
    LineSegment2D_F64 segment = new LineSegment2D_F64(10, 20, 50, -10);
    SnapToLineEdge alg = new SnapToLineEdge(10, 2, GrayU8.class);
    alg.center.set(20, 23);
    alg.localScale = 10;
    LineSegment2D_F64 local = segment.copy();
    toLocal(local.a, alg);
    toLocal(local.b, alg);
    LineGeneral2D_F64 expected = new LineGeneral2D_F64();
    LineGeneral2D_F64 found = new LineGeneral2D_F64();
    UtilLine2D_F64.convert(segment, expected);
    UtilLine2D_F64.convert(local, found);
    alg.localToGlobal(found);
    expected.normalize();
    found.normalize();
    assertEquals(expected.A, found.A, 1e-8);
    assertEquals(expected.B, found.B, 1e-8);
    assertEquals(expected.C, found.C, 1e-8);
}
Also used : LineSegment2D_F64(georegression.struct.line.LineSegment2D_F64) LineGeneral2D_F64(georegression.struct.line.LineGeneral2D_F64) Test(org.junit.Test)

Example 5 with LineSegment2D_F64

use of georegression.struct.line.LineSegment2D_F64 in project BoofCV by lessthanoptimal.

the class TestSnapToLineEdge method fit_noisy_affine.

public void fit_noisy_affine(Affine2D_F64 affine, Class imageType) {
    setup(affine, imageType);
    double tol = 0.5;
    SnapToLineEdge alg = new SnapToLineEdge(10, 2, imageType);
    Polygon2D_F64 input = new Polygon2D_F64(4);
    AffinePointOps_F64.transform(affine, new Point2D_F64(x0, y0), input.get(0));
    AffinePointOps_F64.transform(affine, new Point2D_F64(x0, y1), input.get(1));
    AffinePointOps_F64.transform(affine, new Point2D_F64(x1, y1), input.get(2));
    AffinePointOps_F64.transform(affine, new Point2D_F64(x1, y0), input.get(3));
    LineGeneral2D_F64[] found = new LineGeneral2D_F64[input.size()];
    for (int i = 0; i < found.length; i++) {
        found[i] = new LineGeneral2D_F64();
    }
    for (int i = 0; i < 10; i++) {
        // add some noise
        Polygon2D_F64 noisy = input.copy();
        addNoise(noisy, 2);
        alg.setImage(image);
        for (int j = 0; j < noisy.size(); j++) {
            LineSegment2D_F64 edge = noisy.getLine(j, null);
            double slopeX = edge.slopeX();
            double slopeY = edge.slopeY();
            double r = Math.sqrt(slopeX * slopeX + slopeY * slopeY);
            slopeX /= r;
            slopeY /= r;
            // shrink it slightly to avoid weirdness along the corner
            edge.a.x += 2 * slopeX;
            edge.a.y += 2 * slopeY;
            edge.b.x -= 2 * slopeX;
            edge.b.y -= 2 * slopeY;
            // optimize it a few times to get a good solution
            for (int k = 0; k < 5; k++) {
                assertTrue(alg.refine(edge.a, edge.b, found[j]));
                edge.a.set(ClosestPoint2D_F64.closestPoint(found[j], edge.a, null));
                edge.b.set(ClosestPoint2D_F64.closestPoint(found[j], edge.b, null));
            }
        }
        // compute the corners and see how it did
        for (int j = 0; j < input.size(); j++) {
            int k = (j + 1) % input.size();
            Point2D_F64 c = new Point2D_F64();
            Intersection2D_F64.intersection(found[j], found[k], c);
            double d = c.distance(input.get(k));
            assertTrue(d <= tol);
        }
    }
}
Also used : LineSegment2D_F64(georegression.struct.line.LineSegment2D_F64) Point2D_F64(georegression.struct.point.Point2D_F64) ClosestPoint2D_F64(georegression.metric.ClosestPoint2D_F64) LineGeneral2D_F64(georegression.struct.line.LineGeneral2D_F64) Polygon2D_F64(georegression.struct.shapes.Polygon2D_F64)

Aggregations

LineSegment2D_F64 (georegression.struct.line.LineSegment2D_F64)8 LineGeneral2D_F64 (georegression.struct.line.LineGeneral2D_F64)3 Point2D_F64 (georegression.struct.point.Point2D_F64)3 Test (org.junit.Test)3 Polygon2D_F64 (georegression.struct.shapes.Polygon2D_F64)2 ClosestPoint2D_F64 (georegression.metric.ClosestPoint2D_F64)1 Point2D_I32 (georegression.struct.point.Point2D_I32)1 Vector2D_F64 (georegression.struct.point.Vector2D_F64)1 ArrayList (java.util.ArrayList)1