org.apache.commons.math3.geometry.euclidean.twod.Vector2D Java Examples

The following examples show how to use org.apache.commons.math3.geometry.euclidean.twod.Vector2D. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar circle = new CircleScalar();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);
    NonLinearConjugateGradientOptimizer optimizer =
        new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE,
                                                new SimpleValueChecker(1e-30, 1e-30),
                                                new BrentSolver(1e-15, 1e-13));
    PointValuePair optimum =
        optimizer.optimize(100, circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 });
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.960161753, circle.getRadius(center), 1.0e-8);
    Assert.assertEquals(96.075902096, center.getX(), 1.0e-8);
    Assert.assertEquals(48.135167894, center.getY(), 1.0e-8);
}
 
Example #2
Source File: LineTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAbscissa() {
    Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2));
    Assert.assertEquals(0.0,
                        (l.toSubSpace(new Vector2D(-3,  4))).getX(),
                        1.0e-10);
    Assert.assertEquals(0.0,
                        (l.toSubSpace(new Vector2D( 3, -4))).getX(),
                        1.0e-10);
    Assert.assertEquals(-5.0,
                        (l.toSubSpace(new Vector2D( 7, -1))).getX(),
                        1.0e-10);
    Assert.assertEquals( 5.0,
                         (l.toSubSpace(new Vector2D(-1, -7))).getX(),
                         1.0e-10);
}
 
Example #3
Source File: CircleProblem.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public DerivativeStructure[] value(DerivativeStructure[] params) {
    final DerivativeStructure cx = params[0];
    final DerivativeStructure cy = params[1];
    final DerivativeStructure r = params[2];

    final DerivativeStructure[] model = new DerivativeStructure[points.size() * 2];

    for (int i = 0; i < points.size(); i++) {
        final Vector2D p = points.get(i);

        // Find the circle point closest to the observed point
        // (observed points are points add through the addPoint method above)
        final DerivativeStructure dX = cx.subtract(p.getX());
        final DerivativeStructure dY = cy.subtract(p.getY());
        final DerivativeStructure scaling = r.divide(dX.multiply(dX).add(dY.multiply(dY)).sqrt());
        final int index  = i * 2;
        model[index]     = cx.subtract(scaling.multiply(dX));
        model[index + 1] = cy.subtract(scaling.multiply(dY));

    }

    return model;

}
 
Example #4
Source File: PolygonsSetTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSingularPoint() {
    Vector2D[][] vertices = new Vector2D[][] {
        new Vector2D[] {
            new Vector2D( 0.0,  0.0),
            new Vector2D( 1.0,  0.0),
            new Vector2D( 1.0,  1.0),
            new Vector2D( 0.0,  1.0),
            new Vector2D( 0.0,  0.0),
            new Vector2D(-1.0,  0.0),
            new Vector2D(-1.0, -1.0),
            new Vector2D( 0.0, -1.0)
        }
    };
    PolygonsSet set = buildSet(vertices);
    checkVertices(set.getVertices(), vertices);
}
 
Example #5
Source File: GeometryDataSetGenerator.java    From geowave with Apache License 2.0 6 votes vote down vote up
private static final List<Point> generatePoints(
    final GeometryFactory factory,
    final Vector2D coordinateOne,
    final Vector2D coordinateTwo,
    final double distanceFactor,
    final int points) {
  final List<Point> results = new ArrayList<>();
  final Random rand = new Random();
  final Vector2D originVec = coordinateTwo.subtract(coordinateOne);
  for (int i = 0; i < points; i++) {
    // HP Fortify "Insecure Randomness" false positive
    // This random number is not used for any purpose
    // related to security or cryptography
    final double factor = rand.nextDouble();
    final Vector2D projectionPoint = originVec.scalarMultiply(factor);
    final double direction = rand.nextGaussian() * distanceFactor;
    final Vector2D orthogonal = new Vector2D(originVec.getY(), -originVec.getX());

    results.add(
        factory.createPoint(
            toCoordinate(
                orthogonal.scalarMultiply(direction).add(projectionPoint).add(coordinateOne))));
  }
  return results;
}
 
Example #6
Source File: PolygonsSetTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testEmptyDifference() {
    Vector2D[][] vertices1 = new Vector2D[][] {
        new Vector2D[] {
            new Vector2D( 0.5, 3.5),
            new Vector2D( 0.5, 4.5),
            new Vector2D(-0.5, 4.5),
            new Vector2D(-0.5, 3.5)
        }
    };
    PolygonsSet set1 = buildSet(vertices1);
    Vector2D[][] vertices2 = new Vector2D[][] {
        new Vector2D[] {
            new Vector2D( 1.0, 2.0),
            new Vector2D( 1.0, 8.0),
            new Vector2D(-1.0, 8.0),
            new Vector2D(-1.0, 2.0)
        }
    };
    PolygonsSet set2 = buildSet(vertices2);
    Assert.assertTrue(new RegionFactory<Euclidean2D>().difference(set1.copySelf(), set2.copySelf()).isEmpty());
}
 
Example #7
Source File: LineTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAbscissa() {
    Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2));
    Assert.assertEquals(0.0,
                        (l.toSubSpace(new Vector2D(-3,  4))).getX(),
                        1.0e-10);
    Assert.assertEquals(0.0,
                        (l.toSubSpace(new Vector2D( 3, -4))).getX(),
                        1.0e-10);
    Assert.assertEquals(-5.0,
                        (l.toSubSpace(new Vector2D( 7, -1))).getX(),
                        1.0e-10);
    Assert.assertEquals( 5.0,
                         (l.toSubSpace(new Vector2D(-1, -7))).getX(),
                         1.0e-10);
}
 
Example #8
Source File: LineTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAbscissa() {
    Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2));
    Assert.assertEquals(0.0,
                        (l.toSubSpace(new Vector2D(-3,  4))).getX(),
                        1.0e-10);
    Assert.assertEquals(0.0,
                        (l.toSubSpace(new Vector2D( 3, -4))).getX(),
                        1.0e-10);
    Assert.assertEquals(-5.0,
                        (l.toSubSpace(new Vector2D( 7, -1))).getX(),
                        1.0e-10);
    Assert.assertEquals( 5.0,
                         (l.toSubSpace(new Vector2D(-1, -7))).getX(),
                         1.0e-10);
}
 
Example #9
Source File: PolyhedronsSet.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public SubHyperplane<Euclidean2D> apply(final SubHyperplane<Euclidean2D> sub,
                                        final Hyperplane<Euclidean3D> original,
                                        final Hyperplane<Euclidean3D> transformed) {
    if (original != cachedOriginal) {
        // we have changed hyperplane, reset the in-hyperplane transform

        final Plane   oPlane = (Plane) original;
        final Plane   tPlane = (Plane) transformed;
        final Vector2D shift  = tPlane.toSubSpace(apply(oPlane.getOrigin()));
        final AffineTransform at =
            AffineTransform.getTranslateInstance(shift.getX(), shift.getY());

        cachedOriginal  = (Plane) original;
        cachedTransform =
            org.apache.commons.math3.geometry.euclidean.twod.Line.getTransform(at);

    }

    return ((SubLine) sub).applyTransform(cachedTransform);

}
 
Example #10
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static List<Vector2D> makeMoons(int samples, boolean shuffle, double noise, RandomGenerator random) {
    NormalDistribution dist = new NormalDistribution(random, 0.0, noise, 1e-9);

    int nSamplesOut = samples / 2;
    int nSamplesIn = samples - nSamplesOut;
    
    List<Vector2D> points = new ArrayList<Vector2D>();
    double range = FastMath.PI;
    double step = range / (nSamplesOut / 2.0);
    for (double angle = 0; angle < range; angle += step) {
        Vector2D outerCircle = new Vector2D(FastMath.cos(angle), FastMath.sin(angle));
        points.add(outerCircle.add(generateNoiseVector(dist)));
    }

    step = range / (nSamplesIn / 2.0);
    for (double angle = 0; angle < range; angle += step) {
        Vector2D innerCircle = new Vector2D(1 - FastMath.cos(angle), 1 - FastMath.sin(angle) - 0.5);
        points.add(innerCircle.add(generateNoiseVector(dist)));
    }
    
    if (shuffle) {
        Collections.shuffle(points, new RandomAdaptor(random));
    }

    return points;
}
 
Example #11
Source File: PolyhedronsSet.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Check if a point belongs to the boundary part of a node.
 * @param point point to check
 * @param node node containing the boundary facet to check
 * @return the boundary facet this points belongs to (or null if it
 * does not belong to any boundary facet)
 */
private SubHyperplane<Euclidean3D> boundaryFacet(final Vector3D point,
                                                 final BSPTree<Euclidean3D> node) {
    final Vector2D point2D = ((Plane) node.getCut().getHyperplane()).toSubSpace(point);
    @SuppressWarnings("unchecked")
    final BoundaryAttribute<Euclidean3D> attribute =
        (BoundaryAttribute<Euclidean3D>) node.getAttribute();
    if ((attribute.getPlusOutside() != null) &&
        (((SubPlane) attribute.getPlusOutside()).getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) {
        return attribute.getPlusOutside();
    }
    if ((attribute.getPlusInside() != null) &&
        (((SubPlane) attribute.getPlusInside()).getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) {
        return attribute.getPlusInside();
    }
    return null;
}
 
Example #12
Source File: GetVerticesEvaluator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Object doWork(Object value) throws IOException {
  if(!(value instanceof ConvexHull2D)){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a ConvexHull2D",toExpression(constructingFactory), value.getClass().getSimpleName()));
  } else {
    ConvexHull2D convexHull2D = (ConvexHull2D)value;
    Vector2D[] vectors = convexHull2D.getVertices();
    double[][] data = new double[vectors.length][2];
    for(int i=0; i<vectors.length; i++) {
      data[i][0] = vectors[i].getX();
      data[i][1] = vectors[i].getY();
    }

    return new Matrix(data);
  }
}
 
Example #13
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    final double[] start = {-12, -12};
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }

    Optimum optimum = optimizer.optimize(builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
    Assert.assertTrue(optimum.getEvaluations() < 25);
    Assert.assertEquals(0.043, optimum.getRMS(), 1e-3);
    Assert.assertEquals(0.292235, circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738, center.getX(), 1e-6);
    Assert.assertEquals(0.2075001, center.getY(), 1e-6);
}
 
Example #14
Source File: PolyhedronsSet.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Check if a point belongs to the boundary part of a node.
 * @param point point to check
 * @param node node containing the boundary facet to check
 * @return the boundary facet this points belongs to (or null if it
 * does not belong to any boundary facet)
 */
private SubHyperplane<Euclidean3D> boundaryFacet(final Vector3D point,
                                                 final BSPTree<Euclidean3D> node) {
    final Vector2D point2D = ((Plane) node.getCut().getHyperplane()).toSubSpace(point);
    @SuppressWarnings("unchecked")
    final BoundaryAttribute<Euclidean3D> attribute =
        (BoundaryAttribute<Euclidean3D>) node.getAttribute();
    if ((attribute.getPlusOutside() != null) &&
        (((SubPlane) attribute.getPlusOutside()).getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) {
        return attribute.getPlusOutside();
    }
    if ((attribute.getPlusInside() != null) &&
        (((SubPlane) attribute.getPlusInside()).getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) {
        return attribute.getPlusInside();
    }
    return null;
}
 
Example #15
Source File: PolyhedronsSet.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public SubHyperplane<Euclidean2D> apply(final SubHyperplane<Euclidean2D> sub,
                                        final Hyperplane<Euclidean3D> original,
                                        final Hyperplane<Euclidean3D> transformed) {
    if (original != cachedOriginal) {
        // we have changed hyperplane, reset the in-hyperplane transform

        final Plane    oPlane = (Plane) original;
        final Plane    tPlane = (Plane) transformed;
        final Vector3D p00    = oPlane.getOrigin();
        final Vector3D p10    = oPlane.toSpace(new Vector2D(1.0, 0.0));
        final Vector3D p01    = oPlane.toSpace(new Vector2D(0.0, 1.0));
        final Vector2D  tP00   = tPlane.toSubSpace(apply(p00));
        final Vector2D  tP10   = tPlane.toSubSpace(apply(p10));
        final Vector2D  tP01   = tPlane.toSubSpace(apply(p01));
        final AffineTransform at =
            new AffineTransform(tP10.getX() - tP00.getX(), tP10.getY() - tP00.getY(),
                                tP01.getX() - tP00.getX(), tP01.getY() - tP00.getY(),
                                tP00.getX(), tP00.getY());

        cachedOriginal  = (Plane) original;
        cachedTransform = org.apache.commons.math3.geometry.euclidean.twod.Line.getTransform(at);

    }
    return ((SubLine) sub).applyTransform(cachedTransform);
}
 
Example #16
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public DerivativeStructure getRadius(DerivativeStructure cx, DerivativeStructure cy) {
    DerivativeStructure r = cx.getField().getZero();
    for (Vector2D point : points) {
        r = r.add(distance(point, cx, cy));
    }
    return r.divide(points.size());
}
 
Example #17
Source File: PolygonsSetTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testBug20041003() {

    Line[] l = {
        new Line(new Vector2D(0.0, 0.625000007541172),
                 new Vector2D(1.0, 0.625000007541172)),
                 new Line(new Vector2D(-0.19204433621902645, 0.0),
                          new Vector2D(-0.19204433621902645, 1.0)),
                          new Line(new Vector2D(-0.40303524786887,  0.4248364535319128),
                                   new Vector2D(-1.12851149797877, -0.2634107480798909)),
                                   new Line(new Vector2D(0.0, 2.0),
                                            new Vector2D(1.0, 2.0))
    };

    BSPTree<Euclidean2D> node1 =
        new BSPTree<Euclidean2D>(new SubLine(l[0],
                                      new IntervalsSet(intersectionAbscissa(l[0], l[1]),
                                                       intersectionAbscissa(l[0], l[2]))),
                                                       new BSPTree<Euclidean2D>(Boolean.TRUE), new BSPTree<Euclidean2D>(Boolean.FALSE),
                                                       null);
    BSPTree<Euclidean2D> node2 =
        new BSPTree<Euclidean2D>(new SubLine(l[1],
                                      new IntervalsSet(intersectionAbscissa(l[1], l[2]),
                                                       intersectionAbscissa(l[1], l[3]))),
                                                       node1, new BSPTree<Euclidean2D>(Boolean.FALSE), null);
    BSPTree<Euclidean2D> node3 =
        new BSPTree<Euclidean2D>(new SubLine(l[2],
                                      new IntervalsSet(intersectionAbscissa(l[2], l[3]),
                                                       Double.POSITIVE_INFINITY)),
                                                       node2, new BSPTree<Euclidean2D>(Boolean.FALSE), null);
    BSPTree<Euclidean2D> node4 =
        new BSPTree<Euclidean2D>(l[3].wholeHyperplane(), node3, new BSPTree<Euclidean2D>(Boolean.FALSE), null);

    PolygonsSet set = new PolygonsSet(node4);
    Assert.assertEquals(0, set.getVertices().length);

}
 
Example #18
Source File: LineTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testOriginOffset() {
    Line l1 = new Line(new Vector2D(0, 1), new Vector2D(1, 2));
    Assert.assertEquals(FastMath.sqrt(0.5), l1.getOriginOffset(), 1.0e-10);
    Line l2 = new Line(new Vector2D(1, 2), new Vector2D(0, 1));
    Assert.assertEquals(-FastMath.sqrt(0.5), l2.getOriginOffset(), 1.0e-10);
}
 
Example #19
Source File: Texture.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public Texture(String domain, String name) {
	super(domain, name);
	this.dimension = Vector2DUtil.ONE;
	this.minUV = Vector2DUtil.ONE;
	this.maxUV = Vector2D.ZERO;
}
 
Example #20
Source File: AklToussaintHeuristic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if the given point is located within the convex quadrilateral.
 * @param point the point to check
 * @param quadrilateralPoints the convex quadrilateral, represented by 4 points
 * @return {@code true} if the point is inside the quadrilateral, {@code false} otherwise
 */
private static boolean insideQuadrilateral(final Vector2D point,
                                           final List<Vector2D> quadrilateralPoints) {

    Vector2D p1 = quadrilateralPoints.get(0);
    Vector2D p2 = quadrilateralPoints.get(1);

    if (point.equals(p1) || point.equals(p2)) {
        return true;
    }

    // get the location of the point relative to the first two vertices
    final double last = point.crossProduct(p1, p2);
    final int size = quadrilateralPoints.size();
    // loop through the rest of the vertices
    for (int i = 1; i < size; i++) {
        p1 = p2;
        p2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);

        if (point.equals(p1) || point.equals(p2)) {
            return true;
        }

        // do side of line test: multiply the last location with this location
        // if they are the same sign then the operation will yield a positive result
        // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
        if (last * point.crossProduct(p1, p2) < 0) {
            return false;
        }
    }
    return true;
}
 
Example #21
Source File: PolygonsSetTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testConcentric() {
    double h = FastMath.sqrt(3.0) / 2.0;
    Vector2D[][] vertices1 = new Vector2D[][] {
        new Vector2D[] {
            new Vector2D( 0.00, 0.1 * h),
            new Vector2D( 0.05, 0.1 * h),
            new Vector2D( 0.10, 0.2 * h),
            new Vector2D( 0.05, 0.3 * h),
            new Vector2D(-0.05, 0.3 * h),
            new Vector2D(-0.10, 0.2 * h),
            new Vector2D(-0.05, 0.1 * h)
        }
    };
    PolygonsSet set1 = buildSet(vertices1);
    Vector2D[][] vertices2 = new Vector2D[][] {
        new Vector2D[] {
            new Vector2D( 0.00, 0.0 * h),
            new Vector2D( 0.10, 0.0 * h),
            new Vector2D( 0.20, 0.2 * h),
            new Vector2D( 0.10, 0.4 * h),
            new Vector2D(-0.10, 0.4 * h),
            new Vector2D(-0.20, 0.2 * h),
            new Vector2D(-0.10, 0.0 * h)
        }
    };
    PolygonsSet set2 = buildSet(vertices2);
    Assert.assertTrue(set2.contains(set1));
}
 
Example #22
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double value(double[] variables)  {
    Vector2D center = new Vector2D(variables[0], variables[1]);
    double radius = getRadius(center);

    double sum = 0;
    for (Vector2D point : points) {
        double di = point.distance(center) - radius;
        sum += di * di;
    }

    return sum;
}
 
Example #23
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public DerivativeStructure getRadius(DerivativeStructure cx, DerivativeStructure cy) {
    DerivativeStructure r = cx.getField().getZero();
    for (Vector2D point : points) {
        r = r.add(distance(point, cx, cy));
    }
    return r.divide(points.size());
}
 
Example #24
Source File: PolygonsSetTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testBug20041003() {

    Line[] l = {
        new Line(new Vector2D(0.0, 0.625000007541172),
                 new Vector2D(1.0, 0.625000007541172)),
                 new Line(new Vector2D(-0.19204433621902645, 0.0),
                          new Vector2D(-0.19204433621902645, 1.0)),
                          new Line(new Vector2D(-0.40303524786887,  0.4248364535319128),
                                   new Vector2D(-1.12851149797877, -0.2634107480798909)),
                                   new Line(new Vector2D(0.0, 2.0),
                                            new Vector2D(1.0, 2.0))
    };

    BSPTree<Euclidean2D> node1 =
        new BSPTree<Euclidean2D>(new SubLine(l[0],
                                      new IntervalsSet(intersectionAbscissa(l[0], l[1]),
                                                       intersectionAbscissa(l[0], l[2]))),
                                                       new BSPTree<Euclidean2D>(Boolean.TRUE), new BSPTree<Euclidean2D>(Boolean.FALSE),
                                                       null);
    BSPTree<Euclidean2D> node2 =
        new BSPTree<Euclidean2D>(new SubLine(l[1],
                                      new IntervalsSet(intersectionAbscissa(l[1], l[2]),
                                                       intersectionAbscissa(l[1], l[3]))),
                                                       node1, new BSPTree<Euclidean2D>(Boolean.FALSE), null);
    BSPTree<Euclidean2D> node3 =
        new BSPTree<Euclidean2D>(new SubLine(l[2],
                                      new IntervalsSet(intersectionAbscissa(l[2], l[3]),
                                                       Double.POSITIVE_INFINITY)),
                                                       node2, new BSPTree<Euclidean2D>(Boolean.FALSE), null);
    BSPTree<Euclidean2D> node4 =
        new BSPTree<Euclidean2D>(l[3].wholeHyperplane(), node3, new BSPTree<Euclidean2D>(Boolean.FALSE), null);

    PolygonsSet set = new PolygonsSet(node4);
    Assert.assertEquals(0, set.getVertices().length);

}
 
Example #25
Source File: CircleVectorial.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double getRadius(Vector2D center) {
    double r = 0;
    for (Vector2D point : points) {
        r += point.distance(center);
    }
    return r / points.size();
}
 
Example #26
Source File: CircleVectorial.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double[] value(double[] variables) {
    Vector2D center = new Vector2D(variables[0], variables[1]);
    double radius = getRadius(center);

    double[] residuals = new double[points.size()];
    for (int i = 0; i < residuals.length; ++i) {
        residuals[i] = points.get(i).distance(center) - radius;
    }

    return residuals;
}
 
Example #27
Source File: CircleVectorial.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ModelFunctionJacobian getModelFunctionJacobian() {
    return new ModelFunctionJacobian(new MultivariateMatrixFunction() {
            public double[][] value(double[] params) {
                final int n = points.size();
                final Vector2D center = new Vector2D(params[0], params[1]);

                double dRdX = 0;
                double dRdY = 0;
                for (Vector2D pk : points) {
                    double dk = pk.distance(center);
                    dRdX += (center.getX() - pk.getX()) / dk;
                    dRdY += (center.getY() - pk.getY()) / dk;
                }
                dRdX /= n;
                dRdY /= n;

                // Jacobian of the radius residuals.
                double[][] jacobian = new double[n][2];
                for (int i = 0; i < n; i++) {
                    final Vector2D pi = points.get(i);
                    final double di = pi.distance(center);
                    jacobian[i][0] = (center.getX() - pi.getX()) / di - dRdX;
                    jacobian[i][1] = (center.getY() - pi.getY()) / di - dRdY;
                }

                return jacobian;
            }
    });
}
 
Example #28
Source File: LineTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testContains() {
    Line l = new Line(new Vector2D(0, 1), new Vector2D(1, 2));
    Assert.assertTrue(l.contains(new Vector2D(0, 1)));
    Assert.assertTrue(l.contains(new Vector2D(1, 2)));
    Assert.assertTrue(l.contains(new Vector2D(7, 8)));
    Assert.assertTrue(! l.contains(new Vector2D(8, 7)));
}
 
Example #29
Source File: CircleVectorial.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public double getRadius(Vector2D center) {
    double r = 0;
    for (Vector2D point : points) {
        r += point.distance(center);
    }
    return r / points.size();
}
 
Example #30
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<Vector2D> makeBlobs(int samples, int centers, double clusterStd,
                                       double min, double max, boolean shuffle, RandomGenerator random) {

    NormalDistribution dist = new NormalDistribution(random, 0.0, clusterStd, 1e-9);

    double range = max - min;
    Vector2D[] centerPoints = new Vector2D[centers];
    for (int i = 0; i < centers; i++) {
        double x = random.nextDouble() * range + min;
        double y = random.nextDouble() * range + min;
        centerPoints[i] = new Vector2D(x, y);
    }
    
    int[] nSamplesPerCenter = new int[centers];
    int count = samples / centers;
    Arrays.fill(nSamplesPerCenter, count);
    
    for (int i = 0; i < samples % centers; i++) {
        nSamplesPerCenter[i]++;
    }
    
    List<Vector2D> points = new ArrayList<Vector2D>();
    for (int i = 0; i < centers; i++) {
        for (int j = 0; j < nSamplesPerCenter[i]; j++) {
            Vector2D point = new Vector2D(dist.sample(), dist.sample());
            points.add(point.add(centerPoints[i]));
        }
    }
    
    if (shuffle) {
        Collections.shuffle(points, new RandomAdaptor(random));
    }

    return points;
}