diff --git a/Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/PrioritySphereTest.java b/Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/PrioritySphereTest.java index 4ecf0d7f24a10d35ef8797e2720996fa00f0973e..5e439562d65b9699bde35ff69d09e7a2c9ae08e7 100644 --- a/Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/PrioritySphereTest.java +++ b/Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/PrioritySphereTest.java @@ -5,10 +5,9 @@ import cz.fidentis.analyst.mesh.core.MeshFacet; import cz.fidentis.analyst.mesh.core.MeshFacetImpl; import cz.fidentis.analyst.mesh.core.MeshModel; import cz.fidentis.analyst.mesh.core.MeshPointImpl; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; import java.util.stream.DoubleStream; import javax.vecmath.Point3d; @@ -19,7 +18,7 @@ import org.junit.jupiter.api.Test; public class PrioritySphereTest { - private static final double DELTA = 1e-15; + private static final double DELTA = 1e-11; protected MeshFacet getTrivialFacet(int count, double xSpace, double ySpace, double zSpace, int offset) { final MeshFacet facet = new MeshFacetImpl(); @@ -33,12 +32,15 @@ public class PrioritySphereTest { return facet; } - protected void performTest(Map<MeshFacet, List<Double>> facetsWithExpectedPriorities, + protected void performTest(List<MeshFacet> facets, + List<List<Double>> expectedPriorities, + List<Double> expectedWeightedDistances, PrioritySphere visitor, boolean concurrently) { - - final Set<MeshFacet> facets = facetsWithExpectedPriorities.keySet(); + assertEquals(facets.size(), expectedPriorities.size()); + assertEquals(facets.size(), expectedWeightedDistances.size()); + MeshModel model = new MeshModel(); // model.addFacets(new HashSet<>(facets)); for (final MeshFacet facet: facets) { @@ -48,76 +50,144 @@ public class PrioritySphereTest { model.compute(visitor, concurrently); final Map<MeshFacet, List<Double>> priorities = visitor.getPriorities(); - assertEquals(facetsWithExpectedPriorities.size(), priorities.size()); + final Map<MeshFacet, Double> weightedDistances = visitor.getWeightedDistances(); + assertEquals(facets.size(), priorities.size()); + assertEquals(facets.size(), weightedDistances.size()); - for (final MeshFacet facet: facets) { + for (int i = 0; i < facets.size(); i++) { + final MeshFacet facet = facets.get(i); + assertTrue(priorities.containsKey(facet)); - final List<Double> expectedPriorities = facetsWithExpectedPriorities.get(facet); - final List<Double> actualPriorities = priorities.get(facet); - assertEquals(expectedPriorities.size(), actualPriorities.size()); + final List<Double> expectedPrioritiesList = expectedPriorities.get(i); + final List<Double> actualPrioritiesList = priorities.get(facet); + assertEquals(expectedPrioritiesList.size(), actualPrioritiesList.size()); - for (int i = 0; i < expectedPriorities.size(); i++) { - assertEquals(expectedPriorities.get(i), actualPriorities.get(i), DELTA); + for (int j = 0; j < expectedPrioritiesList.size(); j++) { + assertEquals(expectedPrioritiesList.get(j), actualPrioritiesList.get(j), DELTA); } + + assertTrue(weightedDistances.containsKey(facet)); + + final double expectedWeightedDistance = expectedWeightedDistances.get(i); + final double actualWeightedDistance = weightedDistances.get(facet); + assertEquals(expectedWeightedDistance, actualWeightedDistance, DELTA); } } @Test public void variousPrioritiesTest() { - final List<Double> priorities = List.of(1d, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0d, 0d); - final Map<MeshFacet, List<Double>> expected = new HashMap<>(); - expected.put(getTrivialFacet(12, 1, 0, 0, 0), priorities); - expected.put(getTrivialFacet(12, -1, 0, 0, 0), priorities); - expected.put(getTrivialFacet(12, 0, 1, 0, 0), priorities); - expected.put(getTrivialFacet(12, 0, -1, 0, 0), priorities); - expected.put(getTrivialFacet(12, 0, 0, 1, 0), priorities); - expected.put(getTrivialFacet(12, 0, 0, -1, 0), priorities); + final List<Double> prioritiesList = List.of(1d, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0d, 0d); + final double weightedDistance = 0 + 0.9 + 1.6 + 2.1 + 2.4 + 2.5 + 2.4 + 2.1 + 1.6 + 0.9 + 0 + 0; + + final List<MeshFacet> facets = new ArrayList<>(); + final List<List<Double>> expectedPriorities = new ArrayList<>(); + final List<Double> expectedWeightedDistances = new ArrayList<>(); + + facets.add(getTrivialFacet(12, 1, 0, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, -1, 0, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, 1, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, -1, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, 0, 1, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, 0, -1, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); final PrioritySphere visitor = new PrioritySphere(new Point3d(0, 0, 0), 10); - performTest(expected, visitor, false); + performTest(facets, expectedPriorities, expectedWeightedDistances, visitor, false); } @Test public void allZeroPrioritiesTest() { - final Map<MeshFacet, List<Double>> expected = new HashMap<>(); - expected.put(getTrivialFacet(1, 3, 0, 0, 1), List.of(0d)); - expected.put(getTrivialFacet(5, 0, 3, 0, 1), List.of(0d, 0d, 0d, 0d, 0d)); - expected.put(getTrivialFacet(3, -3, -3, -3, 1), List.of(0d, 0d, 0d)); + final List<MeshFacet> facets = new ArrayList<>(); + final List<List<Double>> expectedPriorities = new ArrayList<>(); + final List<Double> expectedWeightedDistances = new ArrayList<>(); + + facets.add(getTrivialFacet(1, 3, 0, 0, 1)); + expectedPriorities.add(List.of(0d)); + expectedWeightedDistances.add(0d); + facets.add(getTrivialFacet(5, 0, 3, 0, 1)); + expectedPriorities.add(List.of(0d, 0d, 0d, 0d, 0d)); + expectedWeightedDistances.add(0d); + facets.add(getTrivialFacet(3, -3, -3, -3, 1)); + expectedPriorities.add(List.of(0d, 0d, 0d)); + expectedWeightedDistances.add(0d); final PrioritySphere visitor = new PrioritySphere(new Point3d(0, 0, 0), 3); - performTest(expected, visitor, false); + performTest(facets, expectedPriorities, expectedWeightedDistances, visitor, false); } @Test public void noZeroPriorityTest() { - final Map<MeshFacet, List<Double>> expected = new HashMap<>(); - expected.put(getTrivialFacet(1, 10, 0, 0, 1), List.of(0.9)); - expected.put(getTrivialFacet(5, 0, -20, 0, 0), List.of(1d, 0.8, 0.6, 0.4, 0.2)); - expected.put(getTrivialFacet(100, 0, 0, -1, 0), DoubleStream.iterate(1, d -> d > 0d, d -> d - 0.01) + final List<MeshFacet> facets = new ArrayList<>(); + final List<List<Double>> expectedPriorities = new ArrayList<>(); + final List<Double> expectedWeightedDistances = new ArrayList<>(); + + facets.add(getTrivialFacet(1, 10, 0, 0, 1)); + expectedPriorities.add(List.of(0.9)); + expectedWeightedDistances.add(9d); + facets.add(getTrivialFacet(5, 0, -20, 0, 0)); + expectedPriorities.add(List.of(1d, 0.8, 0.6, 0.4, 0.2)); + expectedWeightedDistances.add(0d + 16d + 24d + 24d + 16d); + + facets.add(getTrivialFacet(100, 0, 0, -1, 0)); + final List<Double> priorities = DoubleStream.iterate(1, d -> d > 0d, d -> d - 0.01) .boxed() - .collect(Collectors.toList())); + .collect(Collectors.toList()); + double weightedDistance = 0; + for (int i = 0; i < 100; i++) { + weightedDistance += i * priorities.get(i); + } + expectedPriorities.add(priorities); + expectedWeightedDistances.add(weightedDistance); final PrioritySphere visitor = new PrioritySphere(new Point3d(0, 0, 0), 100); - performTest(expected, visitor, false); + performTest(facets, expectedPriorities, expectedWeightedDistances, visitor, false); } @Test public void concurrencyTest() { - final List<Double> priorities = List.of(1d, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0d, 0d); - final Map<MeshFacet, List<Double>> expected = new HashMap<>(); - expected.put(getTrivialFacet(12, 1, 0, 0, 0), priorities); - expected.put(getTrivialFacet(12, -1, 0, 0, 0), priorities); - expected.put(getTrivialFacet(12, 0, 1, 0, 0), priorities); - expected.put(getTrivialFacet(12, 0, -1, 0, 0), priorities); - expected.put(getTrivialFacet(12, 0, 0, 1, 0), priorities); - expected.put(getTrivialFacet(12, 0, 0, -1, 0), priorities); + final List<Double> prioritiesList = List.of(1d, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0d, 0d); + final double weightedDistance = 0 + 0.9 + 1.6 + 2.1 + 2.4 + 2.5 + 2.4 + 2.1 + 1.6 + 0.9 + 0 + 0; + + final List<MeshFacet> facets = new ArrayList<>(); + final List<List<Double>> expectedPriorities = new ArrayList<>(); + final List<Double> expectedWeightedDistances = new ArrayList<>(); + + facets.add(getTrivialFacet(12, 1, 0, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, -1, 0, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, 1, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, -1, 0, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, 0, 1, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); + facets.add(getTrivialFacet(12, 0, 0, -1, 0)); + expectedPriorities.add(prioritiesList); + expectedWeightedDistances.add(weightedDistance); final PrioritySphere visitor = new PrioritySphere(new Point3d(0, 0, 0), 10); - performTest(expected, visitor, true); + performTest(facets, expectedPriorities, expectedWeightedDistances, visitor, true); } }