/** * Returns a fixed-size supplier backed by a portion of the given list starting with the first * element and specified by its length {@code numberOfVertices}. * * @param vertices the list by which the supplier will be backed. * @param numberOfVertices the portion's length. * @return the supplier. */ public static Vertex2DSupplier asVertex2DSupplier(List<? extends Point2DReadOnly> vertices, int numberOfVertices) { return asVertex2DSupplier(vertices, 0, numberOfVertices); }
/** * Returns a fixed-size supplier backed by a portion of the given array specified by the first * index {@code startIndex} and the portion length {@code numberOfVertices}. * * @param vertices the array by which the supplier will be backed. * @param startIndex the first vertex index. * @param numberOfVertices the portion's length. * @return the supplier. */ public static Vertex2DSupplier asVertex2DSupplier(Point2DReadOnly[] vertices, int startIndex, int numberOfVertices) { if (numberOfVertices == 0) return emptyVertex2DSupplier(); if (startIndex + numberOfVertices > vertices.length) throw new IllegalArgumentException("The array is too small. Array length = " + vertices.length + ", expected minimum length = " + (startIndex + numberOfVertices)); return asVertex2DSupplier(Arrays.asList(vertices), startIndex, numberOfVertices); }
/** * Tests on a per-vertex basis if this supplier and {@code other} are equal. * * @param other the other supplier to compare against this. * @return {@code true} if the two suppliers are equal. */ default boolean equals(Vertex2DSupplier other) { if (other == null) return false; if (getNumberOfVertices() != other.getNumberOfVertices()) return false; for (int i = 0; i < getNumberOfVertices(); i++) { if (!getVertex(i).equals(other.getVertex(i))) return false; } return true; }
@Test public void testEquals() throws Exception { Random random = new Random(9017); for (int i = 0; i < ITERATIONS; i++) { int sizeA = random.nextInt(100) + 1; List<Point2D> listA = IntStream.range(0, sizeA).mapToObj(v -> EuclidCoreRandomTools.nextPoint2D(random)).collect(Collectors.toList()); List<Point2D> listAPrime = listA.stream().map(Point2D::new).collect(Collectors.toList()); List<Point2D> listSizeA = IntStream.range(0, sizeA).mapToObj(v -> EuclidCoreRandomTools.nextPoint2D(random)).collect(Collectors.toList()); int sizeB = random.nextInt(100) + 1; List<Point2D> listB = IntStream.range(0, sizeB).mapToObj(v -> EuclidCoreRandomTools.nextPoint2D(random)).collect(Collectors.toList()); assertTrue(Vertex2DSupplier.asVertex2DSupplier(listA).equals(Vertex2DSupplier.asVertex2DSupplier(listA))); assertTrue(Vertex2DSupplier.asVertex2DSupplier(listA).equals(Vertex2DSupplier.asVertex2DSupplier(listAPrime))); assertFalse(Vertex2DSupplier.asVertex2DSupplier(listA).equals(Vertex2DSupplier.asVertex2DSupplier(listSizeA))); assertFalse(Vertex2DSupplier.asVertex2DSupplier(listA).equals(Vertex2DSupplier.asVertex2DSupplier(listB))); } }
Vertex2DSupplier supplier = Vertex2DSupplier.asVertex2DSupplier(original); assertEquals(numberOfVertices, supplier.getNumberOfVertices()); for (int j = 0; j < numberOfVertices; j++) assertTrue(original.get(j) == supplier.getVertex(j)); Vertex2DSupplier supplier = Vertex2DSupplier.asVertex2DSupplier(original, numberOfVertices); assertEquals(numberOfVertices, supplier.getNumberOfVertices()); assertTrue(subList.get(j) == supplier.getVertex(j)); Vertex2DSupplier supplier = Vertex2DSupplier.asVertex2DSupplier(original, startIndex, numberOfVertices); assertEquals(numberOfVertices, supplier.getNumberOfVertices()); assertTrue(subList.get(j) == supplier.getVertex(j)); Vertex2DSupplier supplier = Vertex2DSupplier.asVertex2DSupplier(original.toArray(new Point2DReadOnly[0])); assertEquals(numberOfVertices, supplier.getNumberOfVertices()); for (int j = 0; j < numberOfVertices; j++) assertTrue(original.get(j) == supplier.getVertex(j)); Vertex2DSupplier supplier = Vertex2DSupplier.asVertex2DSupplier(original.toArray(new Point2DReadOnly[0]), numberOfVertices); assertEquals(numberOfVertices, supplier.getNumberOfVertices()); assertTrue(subList.get(j) == supplier.getVertex(j));
@Test public void testCreatingEmptySupplier() { Vertex2DSupplier expected = Vertex2DSupplier.emptyVertex2DSupplier(); Vertex2DSupplier actual; actual = Vertex2DSupplier.asVertex2DSupplier(); assertEquals(expected, actual); assertTrue(expected.epsilonEquals(actual, EPSILON)); actual = Vertex2DSupplier.asVertex2DSupplier(new Point2DReadOnly[0], 0); assertEquals(expected, actual); assertTrue(expected.epsilonEquals(actual, EPSILON)); actual = Vertex2DSupplier.asVertex2DSupplier(new Point2DReadOnly[0], 0, 0); assertEquals(expected, actual); assertTrue(expected.epsilonEquals(actual, EPSILON)); actual = Vertex2DSupplier.asVertex2DSupplier(Collections.emptyList()); assertEquals(expected, actual); assertTrue(expected.epsilonEquals(actual, EPSILON)); actual = Vertex2DSupplier.asVertex2DSupplier(Collections.emptyList(), 0); assertEquals(expected, actual); assertTrue(expected.epsilonEquals(actual, EPSILON)); actual = Vertex2DSupplier.asVertex2DSupplier(Collections.emptyList(), 0, 0); assertEquals(expected, actual); assertTrue(expected.epsilonEquals(actual, EPSILON)); }
@Test public void testEpsilonquals() throws Exception { Random random = new Random(9017); for (int i = 0; i < ITERATIONS; i++) { double epsilon = random.nextDouble(); int sizeA = random.nextInt(100) + 1; List<Point2D> listA = IntStream.range(0, sizeA).mapToObj(v -> EuclidCoreRandomTools.nextPoint2D(random)).collect(Collectors.toList()); List<Point2D> listAPrime = listA.stream().map(Point2D::new).collect(Collectors.toList()); listAPrime.forEach(p -> p.add(nextDouble(random, epsilon), nextDouble(random, epsilon))); List<Point2D> listSizeA = listA.stream().map(Point2D::new).collect(Collectors.toList()); listSizeA.forEach(p -> p.add((random.nextBoolean() ? -1.0 : 1.0) * nextDouble(random, epsilon, epsilon + 1.0), (random.nextBoolean() ? -1.0 : 1.0) * nextDouble(random, epsilon, epsilon + 1.0))); int sizeB = random.nextInt(100) + 1; List<Point2D> listB = IntStream.range(0, sizeB).mapToObj(v -> EuclidCoreRandomTools.nextPoint2D(random)).collect(Collectors.toList()); assertTrue("Iteration: " + i, Vertex2DSupplier.asVertex2DSupplier(listA).epsilonEquals(Vertex2DSupplier.asVertex2DSupplier(listA), epsilon)); assertTrue("Iteration: " + i, Vertex2DSupplier.asVertex2DSupplier(listA).epsilonEquals(Vertex2DSupplier.asVertex2DSupplier(listAPrime), epsilon)); assertFalse("Iteration: " + i, Vertex2DSupplier.asVertex2DSupplier(listA).epsilonEquals(Vertex2DSupplier.asVertex2DSupplier(listSizeA), epsilon)); assertFalse("Iteration: " + i, Vertex2DSupplier.asVertex2DSupplier(listA).epsilonEquals(Vertex2DSupplier.asVertex2DSupplier(listB), epsilon)); } } }
return emptyVertex2DSupplier(); if (startIndex + numberOfVertices > vertices.length) throw new IllegalArgumentException("The array is too small. Array length = " + vertices.length + ", expected minimum length = "
@Override public int getNumberOfVertices() { return vertex2dSupplier.getNumberOfVertices(); }
@Override public FramePoint2DReadOnly getVertex(int index) { return new FramePoint2D(referenceFrame, vertex2dSupplier.getVertex(index)); } };
return emptyVertex2DSupplier(); if (startIndex + numberOfVertices > vertices.size()) throw new IllegalArgumentException("The list is too small. List size = " + vertices.size() + ", expected minimum size = "
/** * Tests whether this supplier is empty or not. * * @return {@code true} if this supplier has no vertices, {@code false} if it has at one vertex. */ default boolean isEmpty() { return getNumberOfVertices() == 0; }
/** * Returns a fixed-size supplier backed by a the given array. * * @param vertices the array containing the vertices. Each row contains one point whereas the (at * least) two columns contains in order the coordinates x and y. Not modified. * @return the supplier. */ public static Vertex2DSupplier asVertex2DSupplier(double[][] vertices) { return asVertex2DSupplier(vertices, vertices.length); }
/** * Tests on a per-vertex basis if this supplier and {@code other} are equal to an * {@code epsilon}. * * @param other the other supplier to compare against this. * @param epsilon the tolerance to use. * @return {@code true} if the two suppliers are equal. */ default boolean epsilonEquals(Vertex2DSupplier other, double epsilon) { if (getNumberOfVertices() != other.getNumberOfVertices()) return false; for (int i = 0; i < getNumberOfVertices(); i++) { if (!getVertex(i).epsilonEquals(other.getVertex(i), epsilon)) return false; } return true; }
private YoGraphicPolygon(String name, YoFrameConvexPolygon2D yoFrameConvexPolygon2d, YoFramePoint3D framePoint, YoFrameYawPitchRoll yawPitchRoll, YoFrameQuaternion quaternion, double scale, double height, AppearanceDefinition appearance) { super(name, framePoint, yawPitchRoll, quaternion, scale); if (yoFrameConvexPolygon2d.getNumberOfVertices() <= 0) yoFrameConvexPolygon2d.set(Vertex2DSupplier.emptyVertex2DSupplier()); this.yoFrameConvexPolygon2d = yoFrameConvexPolygon2d; this.appearance = appearance; this.height = height; verticesToDisplay = new ArrayList<>(yoFrameConvexPolygon2d.getMaxNumberOfVertices()); graphics3dObject = new Graphics3DObject(); graphics3dObject.setChangeable(true); MeshDataHolder meshDataHolder = MeshDataGenerator.ExtrudedPolygon(yoFrameConvexPolygon2d, height); instruction = new Graphics3DAddMeshDataInstruction(meshDataHolder, appearance); graphics3dObject.addInstruction(instruction); }
/** * Returns a fixed-size supplier backed by a portion of the given array starting with the first * row and specified by its length {@code numberOfVertices}. * * @param vertices the array containing the vertices. Each row contains one point whereas the (at * least) two columns contains in order the coordinates x and y. Not modified. * @param numberOfVertices the portion's length. * @return the supplier. */ public static Vertex2DSupplier asVertex2DSupplier(double[][] vertices, int numberOfVertices) { return asVertex2DSupplier(vertices, 0, numberOfVertices); }
/** * Updates this bounding box to be the smallest bounding box that includes this and the supplied * points. * * @param vertex2DSupplier the supply of points. */ default void updateToIncludePoints(Vertex2DSupplier vertex2DSupplier) { for (int index = 0; index < vertex2DSupplier.getNumberOfVertices(); index++) updateToIncludePoint(vertex2DSupplier.getVertex(index)); }
/** * Returns a fixed-size supplier backed by the given array. * * @param vertices the array by which the supplier will be backed. * @return the supplier. */ public static Vertex2DSupplier asVertex2DSupplier(Point2DReadOnly... vertices) { return asVertex2DSupplier(Arrays.asList(vertices)); }
/** * Adds the vertices from the given vertex supplier. * <p> * Note that this polygon is marked as being out-of-date. The method {@link #update()} has to be * called afterward before being able to perform operations with this polygon. * </p> * * @param vertex2DSupplier the supplier of vertices. * @see Vertex2DSupplier */ default void addVertices(Vertex2DSupplier vertex2DSupplier) { for (int index = 0; index < vertex2DSupplier.getNumberOfVertices(); index++) { addVertex(vertex2DSupplier.getVertex(index)); } }
/** * Returns a fixed-size supplier backed by a portion of the given array starting with the first * element and specified by its length {@code numberOfVertices}. * * @param vertices the array by which the supplier will be backed. * @param numberOfVertices the portion's length. * @return the supplier. */ public static Vertex2DSupplier asVertex2DSupplier(Point2DReadOnly[] vertices, int numberOfVertices) { return asVertex2DSupplier(Arrays.asList(vertices), numberOfVertices); }