Search in sources :

Example 71 with IntStream

use of java.util.stream.IntStream in project testing-video by testing-av.

the class Plane method verifyRect.

public void verifyRect(int x, int y, int w, int h, int expected, int deviation, int maxMisses) {
    int x1 = limit(x, width);
    int y1 = limit(y, height);
    int x2 = limit(x + w, width);
    int y2 = limit(y + h, height);
    IntStream range = range(y1, y2);
    if (x2 - x1 > 64) {
        // TODO measure when to switch
        range = range.parallel();
    }
    int count = range.map(iy -> {
        int base = iy * width;
        return verify(base + x1, base + x2, expected, deviation);
    }).sum();
    assertFalse(count + maxMisses < (y2 - y1) * (x2 - x1));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) IntStream.range(java.util.stream.IntStream.range) IntBinaryOperator(java.util.function.IntBinaryOperator) Math.abs(java.lang.Math.abs) IntStream(java.util.stream.IntStream)

Example 72 with IntStream

use of java.util.stream.IntStream in project java-code-show by CodeShowZz.

the class StreamTest method primitiveTypeStreamTest.

@Test
public void primitiveTypeStreamTest() {
    // 创建一个整数流
    IntStream stream = IntStream.of(1, 1, 2, 3, 5);
    int[] arr = { 1, 2, 3, 4, 5 };
    stream = Arrays.stream(arr);
    // 生成0到99
    // Upper bound is excluded
    IntStream zeroToNinetyNine = IntStream.range(0, 100);
    zeroToNinetyNine.forEach((x) -> log.info(x + ""));
    // Upper bound is included
    IntStream zeroToHundred = IntStream.rangeClosed(0, 100);
    // 统计字符串数组中的长度 存储在整数流中
    String[] names = { "Tom", "Jack", "Mary", "Adams", "HuangJunlin" };
    IntStream intStream = Stream.of(names).mapToInt(String::length);
    intStream.forEach(x -> log.info(x + ""));
    // 装箱
    Stream<Integer> integers = IntStream.range(0, 100).boxed();
    integers.forEach(x -> log.info(x + ""));
}
Also used : BigInteger(java.math.BigInteger) IntStream(java.util.stream.IntStream) Test(org.junit.Test)

Example 73 with IntStream

use of java.util.stream.IntStream in project FXyzLib by Birdasaur.

the class TriangulatedMesh method createMesh.

private TriangleMesh createMesh(int level) {
    TriangleMesh m0 = null;
    if (level > 0) {
        m0 = createMesh(level - 1);
    }
    if (level == 0) {
        // check for duplicates or too close
        List<Integer> duplicates = IntStream.range(0, pointsExterior.size()).boxed().filter(i -> pointsExterior.get(i).substract(pointsExterior.get(i == pointsExterior.size() - 1 ? 0 : i + 1)).magnitude() < 100 * EPSILON).map(i -> i).collect(Collectors.toList());
        duplicates.stream().sorted(Collections.reverseOrder()).forEach(i -> pointsExterior.remove(i.intValue()));
        List<PolygonPoint> list = pointsExterior.stream().map(p -> new PolygonPoint(p.x, p.y)).collect(Collectors.toList());
        Polygon poly = new Polygon(list);
        if (bounds.get() != null) {
            maxX = bounds.get().getMaxX();
            minX = bounds.get().getMinX();
            maxY = bounds.get().getMaxY();
            minY = bounds.get().getMinY();
        } else {
            maxX = pointsExterior.stream().mapToDouble(p -> p.x).max().getAsDouble();
            maxY = pointsExterior.stream().mapToDouble(p -> p.y).max().getAsDouble();
            minX = pointsExterior.stream().mapToDouble(p -> p.x).min().getAsDouble();
            minY = pointsExterior.stream().mapToDouble(p -> p.y).min().getAsDouble();
        }
        double rad = getHoleRadius();
        if (pointsHoles != null) {
            steinerPoints = 0;
            numHoles = pointsHoles.size();
            // holes
            pointsHoles.forEach(pHole -> {
                // hole
                List<PolygonPoint> hole = pHole.stream().distinct().map(p -> new PolygonPoint(p.x, p.y)).collect(Collectors.toList());
                holePoints.add(hole.size());
                Polygon polyIn = new Polygon(hole);
                poly.addHole(polyIn);
                holes.add(hole);
            });
        } else if (rad > 0d) {
            steinerPoints = 0;
            numHoles = 1;
            int num = 200;
            holePoints.add(num);
            // circular hole
            List<PolygonPoint> hole = IntStream.range(0, num).mapToObj(i -> new PolygonPoint((maxX + minX) / 2d + rad * Math.cos((num - i) * 2d * Math.PI / num), (maxY + minY) / 2d + rad * Math.sin((num - i) * 2d * Math.PI / num))).collect(Collectors.toList());
            Polygon polyIn = new Polygon(hole);
            poly.addHole(polyIn);
            holes.add(hole);
        } else {
            double radSteiner = Math.sqrt(Math.pow(maxX - minX, 2) + Math.pow(maxY - minY, 2)) / 8d;
            // steiner points
            steiner = IntStream.range(0, steinerPoints).mapToObj(i -> new PolygonPoint((maxX + minX) / 2d + radSteiner * Math.cos(i * 2d * Math.PI / steinerPoints), (maxY + minY) / 2d + radSteiner * Math.sin(i * 2d * Math.PI / steinerPoints))).collect(Collectors.toList());
            poly.addSteinerPoints(steiner);
        }
        PolygonSet ps = new PolygonSet(poly);
        Poly2Tri.triangulate(ps);
        Polygon polRes = ps.getPolygons().get(0);
        List<DelaunayTriangle> tri = polRes.getTriangles();
        points1 = polRes.getPoints();
        extPoints = points1.size();
        if (pointsHoles != null || rad > 0d) {
            holes.forEach(hole -> hole.forEach(points1::add));
        } else {
            steiner.forEach(points1::add);
        }
        int totalHolePoints = holePoints.stream().reduce(0, Integer::sum);
        int numPoints = extPoints + steinerPoints + totalHolePoints;
        FloatCollector pointsBottom = points1.stream().flatMapToDouble(p -> DoubleStream.of(p.getX(), p.getY(), 0d)).collect(() -> new FloatCollector(points1.size() * 3), FloatCollector::add, FloatCollector::join);
        FloatCollector pointsTop = points1.stream().flatMapToDouble(p -> DoubleStream.of(p.getX(), p.getY(), height.get())).collect(() -> new FloatCollector(points1.size() * 3), FloatCollector::add, FloatCollector::join);
        pointsBottom.join(pointsTop);
        points0 = pointsBottom.toArray();
        numVertices = points0.length / 3;
        FloatCollector texBottom = points1.stream().flatMapToDouble(p -> DoubleStream.of((p.getX() - minX) / (maxX - minX), (p.getY() - minY) / (maxY - minY))).collect(() -> new FloatCollector(points1.size() * 2), FloatCollector::add, FloatCollector::join);
        FloatCollector texTop = points1.stream().flatMapToDouble(p -> DoubleStream.of((p.getX() - minX) / (maxX - minX), (p.getY() - minY) / (maxY - minY))).collect(() -> new FloatCollector(points1.size() * 2), FloatCollector::add, FloatCollector::join);
        texBottom.join(texTop);
        texCoord0 = texBottom.toArray();
        numTexCoords = texCoord0.length / 2;
        texCoord1 = IntStream.range(0, numTexCoords).mapToObj(i -> new Point2D(texCoord0[2 * i], texCoord0[2 * i + 1])).collect(Collectors.toList());
        List<int[]> listIndices = tri.stream().map((DelaunayTriangle t) -> {
            int[] pIndex = new int[3];
            for (int j = 0; j < 3; j++) {
                final TriangulationPoint dt = t.points[j];
                int[] toArray = IntStream.range(0, points1.size()).filter(i -> points1.get(i).equals(dt)).toArray();
                if (toArray.length > 0) {
                    pIndex[j] = toArray[0];
                } else {
                    System.out.println("Error " + points1);
                }
            }
            return pIndex;
        }).collect(Collectors.toList());
        // faces
        // base
        IntStream streamBottom = listIndices.stream().map(i -> IntStream.of(i[0], i[0], i[2], i[2], i[1], i[1])).flatMapToInt(i -> i);
        // top
        IntStream streamTop = listIndices.stream().map(i -> IntStream.of(numPoints + i[0], numPoints + i[0], numPoints + i[1], numPoints + i[1], numPoints + i[2], numPoints + i[2])).flatMapToInt(i -> i);
        // vertical, exterior
        IntStream streamExtWalls = IntStream.range(0, extPoints - 1).mapToObj(i -> IntStream.of(i, i, i + 1, i + 1, i + 1 + numPoints, i + 1 + numPoints, i, i, i + 1 + numPoints, i + 1 + numPoints, i + numPoints, i + numPoints)).flatMapToInt(i -> i);
        // vertical, exterior, close polygon
        IntStream streamExtWallsClose = IntStream.of(extPoints - 1, extPoints - 1, 0, 0, 0 + numPoints, 0 + numPoints, extPoints - 1, extPoints - 1, 0 + numPoints, 0 + numPoints, numPoints + extPoints - 1, numPoints + extPoints - 1);
        if (totalHolePoints > 0) {
            // vertical, interior
            // holes
            int acuHolePoints0 = extPoints + steinerPoints, acuHolePoints1;
            IntStream streamIntWalls = IntStream.empty();
            for (List<PolygonPoint> hole : holes) {
                acuHolePoints1 = acuHolePoints0 + hole.size() - 1;
                IntStream streamIntWallsHole = IntStream.range(acuHolePoints0, acuHolePoints1).mapToObj(i -> IntStream.of(i, i, i + 1 + numPoints, i + 1 + numPoints, i + 1, i + 1, i, i, i + numPoints, i + numPoints, i + 1 + numPoints, i + 1 + numPoints)).flatMapToInt(i -> i);
                streamIntWalls = IntStream.concat(streamIntWalls, streamIntWallsHole);
                acuHolePoints0 = acuHolePoints1 + 1;
            }
            // vertical, interior, close holes
            // holes
            acuHolePoints0 = extPoints + steinerPoints;
            IntStream streamIntWallsClose = IntStream.empty();
            for (List<PolygonPoint> hole : holes) {
                acuHolePoints1 = acuHolePoints0 + hole.size() - 1;
                IntStream streamIntWallsCloseHole = IntStream.of(acuHolePoints1, acuHolePoints1, numPoints + acuHolePoints0, numPoints + acuHolePoints0, acuHolePoints0, acuHolePoints0, acuHolePoints1, acuHolePoints1, numPoints + acuHolePoints1, numPoints + acuHolePoints1, numPoints + acuHolePoints0, numPoints + acuHolePoints0);
                streamIntWallsClose = IntStream.concat(streamIntWallsClose, streamIntWallsCloseHole);
                acuHolePoints0 = acuHolePoints1 + 1;
            }
            faces0 = IntStream.concat(streamBottom, IntStream.concat(streamTop, IntStream.concat(streamExtWalls, IntStream.concat(streamExtWallsClose, IntStream.concat(streamIntWalls, streamIntWallsClose))))).toArray();
        } else {
            faces0 = IntStream.concat(streamBottom, IntStream.concat(streamTop, IntStream.concat(streamExtWalls, streamExtWallsClose))).toArray();
        }
        numFaces = faces0.length / 6;
    } else if (m0 != null) {
        points0 = new float[numVertices * m0.getPointElementSize()];
        m0.getPoints().toArray(points0);
        texCoord0 = new float[numTexCoords * m0.getTexCoordElementSize()];
        m0.getTexCoords().toArray(texCoord0);
        faces0 = new int[numFaces * m0.getFaceElementSize()];
        m0.getFaces().toArray(faces0);
    }
    List<Point3D> points1 = IntStream.range(0, numVertices).mapToObj(i -> new Point3D(points0[3 * i], points0[3 * i + 1], points0[3 * i + 2])).collect(Collectors.toList());
    texCoord1 = IntStream.range(0, numTexCoords).mapToObj(i -> new Point2D(texCoord0[2 * i], texCoord0[2 * i + 1])).collect(Collectors.toList());
    List<Face3> faces1 = IntStream.range(0, numFaces).mapToObj(i -> new Face3(faces0[6 * i], faces0[6 * i + 2], faces0[6 * i + 4])).collect(Collectors.toList());
    index.set(points1.size());
    map.clear();
    listFaces.clear();
    listVertices.clear();
    listVertices.addAll(points1);
    faces1.forEach(face -> {
        int v1 = face.p0;
        int v2 = face.p1;
        int v3 = face.p2;
        if (level > 0) {
            int a = getMiddle(v1, points1.get(v1), v2, points1.get(v2));
            int b = getMiddle(v2, points1.get(v2), v3, points1.get(v3));
            int c = getMiddle(v3, points1.get(v3), v1, points1.get(v1));
            listFaces.add(new Face3(v1, a, c));
            listFaces.add(new Face3(v2, b, a));
            listFaces.add(new Face3(v3, c, b));
            listFaces.add(new Face3(a, b, c));
        } else {
            listFaces.add(new Face3(v1, v2, v3));
        }
    });
    map.clear();
    numVertices = listVertices.size();
    numFaces = listFaces.size();
    List<Face3> textures1 = IntStream.range(0, faces0.length / 6).mapToObj(i -> new Face3(faces0[6 * i + 1], faces0[6 * i + 3], faces0[6 * i + 5])).collect(Collectors.toList());
    index.set(texCoord1.size());
    listTextures.clear();
    textures1.forEach(face -> {
        int v1 = face.p0;
        int v2 = face.p1;
        int v3 = face.p2;
        if (level > 0) {
            int a = getMiddle(v1, texCoord1.get(v1), v2, texCoord1.get(v2));
            int b = getMiddle(v2, texCoord1.get(v2), v3, texCoord1.get(v3));
            int c = getMiddle(v3, texCoord1.get(v3), v1, texCoord1.get(v1));
            listTextures.add(new Face3(v1, a, c));
            listTextures.add(new Face3(v2, b, a));
            listTextures.add(new Face3(v3, c, b));
            listTextures.add(new Face3(a, b, c));
        } else {
            listTextures.add(new Face3(v1, v2, v3));
        }
    });
    map.clear();
    texCoord0 = texCoord1.stream().flatMapToDouble(p -> DoubleStream.of(p.getX(), p.getY())).collect(() -> new FloatCollector(texCoord1.size() * 2), FloatCollector::add, FloatCollector::join).toArray();
    numTexCoords = texCoord0.length / 2;
    textureCoords = texCoord0;
    if (level == getLevel()) {
        areaMesh.setWidth(maxX - minX);
        areaMesh.setHeight(maxY - minY);
        rectMesh.setWidth((int) Math.sqrt(texCoord0.length));
        rectMesh.setHeight(texCoord0.length / ((int) Math.sqrt(texCoord0.length)));
        smoothingGroups = getSmoothingGroups(listVertices, listFaces);
    }
    return createMesh();
}
Also used : IntStream(java.util.stream.IntStream) HashMap(java.util.HashMap) DoubleProperty(javafx.beans.property.DoubleProperty) IntegerProperty(javafx.beans.property.IntegerProperty) CullFace(javafx.scene.shape.CullFace) TriangleMesh(javafx.scene.shape.TriangleMesh) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DelaunayTriangle(org.poly2tri.triangulation.delaunay.DelaunayTriangle) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) Polygon(org.poly2tri.polygon.Polygon) Point2D(javafx.geometry.Point2D) ObjectProperty(javafx.beans.property.ObjectProperty) FloatCollector(org.fxyz.utils.FloatCollector) PolygonSet(org.poly2tri.polygon.PolygonSet) DrawMode(javafx.scene.shape.DrawMode) Collectors(java.util.stream.Collectors) DoubleStream(java.util.stream.DoubleStream) Poly2Tri(org.poly2tri.Poly2Tri) List(java.util.List) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) DepthTest(javafx.scene.DepthTest) Face3(org.fxyz.geometry.Face3) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) TriangulationPoint(org.poly2tri.triangulation.TriangulationPoint) Collections(java.util.Collections) PolygonPoint(org.poly2tri.polygon.PolygonPoint) Point3D(org.fxyz.geometry.Point3D) Bounds(javafx.geometry.Bounds) TriangulationPoint(org.poly2tri.triangulation.TriangulationPoint) TriangleMesh(javafx.scene.shape.TriangleMesh) FloatCollector(org.fxyz.utils.FloatCollector) DelaunayTriangle(org.poly2tri.triangulation.delaunay.DelaunayTriangle) PolygonPoint(org.poly2tri.polygon.PolygonPoint) PolygonSet(org.poly2tri.polygon.PolygonSet) TriangulationPoint(org.poly2tri.triangulation.TriangulationPoint) PolygonPoint(org.poly2tri.polygon.PolygonPoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Point2D(javafx.geometry.Point2D) Point3D(org.fxyz.geometry.Point3D) ArrayList(java.util.ArrayList) List(java.util.List) Polygon(org.poly2tri.polygon.Polygon) IntStream(java.util.stream.IntStream) Face3(org.fxyz.geometry.Face3)

Example 74 with IntStream

use of java.util.stream.IntStream in project Gaffer by gchq.

the class LimitedInMemorySortedIterableTest method shouldSortLargeNumberOfItems.

@Test
public void shouldSortLargeNumberOfItems() {
    // Given
    final int streamSize = 1000000;
    final int resultLimit = 10000;
    final IntStream stream = new Random().ints(// generate a few extra in case there are duplicates
    streamSize * 2).distinct().limit(streamSize);
    final LimitedInMemorySortedIterable<Integer> list = new LimitedInMemorySortedIterable<Integer>(Comparator.naturalOrder(), resultLimit, false);
    // When
    stream.forEach(i -> list.add(Math.abs(i)));
    final List<Integer> sortedElements = Lists.newArrayList(list);
    sortedElements.sort(Comparator.naturalOrder());
    // Then
    final List<Integer> expected = Lists.newArrayList(list);
    assertEquals(expected, sortedElements);
}
Also used : Random(java.util.Random) IntStream(java.util.stream.IntStream) Test(org.junit.jupiter.api.Test)

Example 75 with IntStream

use of java.util.stream.IntStream in project Gaffer by gchq.

the class GafferCollectorTest method shouldCollectToLinkedHashSet.

@Test
public void shouldCollectToLinkedHashSet() {
    final IntStream stream = IntStream.range(0, 100);
    final Iterable<Integer> iterable = stream.boxed().collect(toLinkedHashSet());
    assertThat(iterable).isInstanceOf(LinkedHashSet.class).hasSize(100);
}
Also used : GafferCollectors.toLinkedHashSet(uk.gov.gchq.gaffer.commonutil.stream.GafferCollectors.toLinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) IntStream(java.util.stream.IntStream) Test(org.junit.jupiter.api.Test)

Aggregations

IntStream (java.util.stream.IntStream)96 List (java.util.List)19 Test (org.junit.jupiter.api.Test)17 ArrayList (java.util.ArrayList)15 Stream (java.util.stream.Stream)12 Test (org.junit.Test)12 Arrays (java.util.Arrays)11 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)9 Random (java.util.Random)7 DoubleStream (java.util.stream.DoubleStream)6 LongStream (java.util.stream.LongStream)6 Function (java.util.function.Function)5 Pattern (java.util.regex.Pattern)5 DecimalBoxFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.decimalBox.definition.DecimalBoxFieldDefinition)5 MultipleSubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition)5 SubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition)5 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)5 LayoutRow (org.uberfire.ext.layout.editor.api.editor.LayoutRow)5 LayoutTemplate (org.uberfire.ext.layout.editor.api.editor.LayoutTemplate)5