From ea7b6c42adcc928f8024b1c3747b5a09c081b324 Mon Sep 17 00:00:00 2001 From: Radek Oslejsek <oslejsek@fi.muni.cz> Date: Wed, 17 Mar 2021 16:24:18 +0100 Subject: [PATCH] Refactoring --- .../cz/fidentis/analyst/EfficiencyTests.java | 44 +++++++++++++++++- .../kdtree/ClosestNodeMultipleVisitor.java | 7 ++- .../kdtree/DistanceKdTreeVisitor.java | 22 ++++----- .../kdtree/DistanceToKdTrianglesApprox.java | 17 +++---- .../visitors/kdtree/DistanceToKdVertices.java | 17 +++---- .../visitors/mesh/BoundingBoxVisitor.java | 10 ++--- .../visitors/mesh/DistanceMeshVisitor.java | 35 ++++----------- .../visitors/mesh/DistanceToTriangles.java | 14 +++--- .../mesh/DistanceToTrianglesApprox.java | 17 +++---- .../visitors/mesh/DistanceToVertices.java | 17 +++---- .../mesh/HausdorffDistanceBruteForce.java | 30 ++++++------- .../mesh/HausdorffDistanceKdTree.java | 34 +++++++------- .../mesh/HausdorffDistanceVisitor.java | 7 ++- .../visitors/mesh/TriangleListVisitor.java | 14 +++--- .../analyst/kdtree/KdTreeVisitor.java | 41 +++++++++-------- .../cz/fidentis/analyst/mesh/MeshVisitor.java | 45 ++++++++++--------- .../fidentis/analyst/mesh/core/MeshModel.java | 4 +- 17 files changed, 185 insertions(+), 190 deletions(-) diff --git a/Comparison/src/main/java/cz/fidentis/analyst/EfficiencyTests.java b/Comparison/src/main/java/cz/fidentis/analyst/EfficiencyTests.java index 359b3c31..3beaf276 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/EfficiencyTests.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/EfficiencyTests.java @@ -6,6 +6,8 @@ import cz.fidentis.analyst.mesh.core.MeshFacet; import cz.fidentis.analyst.visitors.mesh.HausdorffDistanceBruteForce; import cz.fidentis.analyst.visitors.mesh.DistanceToTriangles; import cz.fidentis.analyst.visitors.mesh.DistanceToVertices; +import cz.fidentis.analyst.visitors.mesh.HausdorffDistanceKdTree; +import cz.fidentis.analyst.visitors.mesh.HausdorffDistanceVisitor; import cz.fidentis.analyst.visitors.mesh.HausdorffDistanceVisitor.Strategy; import java.io.File; import java.io.IOException; @@ -35,9 +37,27 @@ public class EfficiencyTests { boy = new HumanFace(boyFile); girl = new HumanFace(girlFile); - System.out.println(measureKdTreeCreation(boy) + " msec: Kd-tree creation of the boy"); - System.out.println(measureKdTreeCreation(boy) + " msec: Kd-tree creation of the girl"); + System.out.println(measureKdTreeCreation(boy) + "\tmsec:\tKd-tree creation of the boy"); + System.out.println(measureKdTreeCreation(boy) + "\tmsec:\tKd-tree creation of the girl"); + boolean relativeDist = false; + boolean printDetails = false; + + System.out.println(); + testAndPrint(girl, new HausdorffDistanceKdTree(boy.getMeshModel(), Strategy.POINT_TO_POINT, relativeDist, true), printDetails); + testAndPrint(girl, new HausdorffDistanceKdTree(boy.getMeshModel(), Strategy.POINT_TO_POINT, relativeDist, false), printDetails); + testAndPrint(girl, new HausdorffDistanceBruteForce(boy.getMeshModel(), Strategy.POINT_TO_POINT, relativeDist, true), printDetails); + testAndPrint(girl, new HausdorffDistanceBruteForce(boy.getMeshModel(), Strategy.POINT_TO_POINT, relativeDist, false), printDetails); + System.out.println(); + testAndPrint(girl, new HausdorffDistanceKdTree(boy.getMeshModel(), Strategy.POINT_TO_TRIANGLE_APPROXIMATE, relativeDist, true), printDetails); + testAndPrint(girl, new HausdorffDistanceKdTree(boy.getMeshModel(), Strategy.POINT_TO_TRIANGLE_APPROXIMATE, relativeDist, false), printDetails); + testAndPrint(girl, new HausdorffDistanceBruteForce(boy.getMeshModel(), Strategy.POINT_TO_TRIANGLE_APPROXIMATE, relativeDist, true), printDetails); + testAndPrint(girl, new HausdorffDistanceBruteForce(boy.getMeshModel(), Strategy.POINT_TO_TRIANGLE_APPROXIMATE, relativeDist, false), printDetails); + System.out.println(); + testAndPrint(girl, new HausdorffDistanceBruteForce(boy.getMeshModel(), Strategy.POINT_TO_TRIANGLE, relativeDist, true), printDetails); + testAndPrint(girl, new HausdorffDistanceBruteForce(boy.getMeshModel(), Strategy.POINT_TO_TRIANGLE, relativeDist, false), printDetails); + + /* boolean print = false; System.out.println(measureDist(new Vector3d(), boy, false, print, true) + " msec: Distance (sequential, point-to-point)"); @@ -50,6 +70,26 @@ public class EfficiencyTests { System.out.println(measureHD(boy, girl, false, true, print, true) + " msec: Hausdorff distance (relative, concurrent, point-to-point)"); System.out.println(measureHD(boy, girl, false, false, print, false) + " msec: Hausdorff distance (relative, sequential, point-to-tri)"); System.out.println(measureHD(boy, girl, false, true, print, false) + " msec: Hausdorff distance (relative, concurrent, point-to-tri)"); +*/ + } + + protected static void testAndPrint(HumanFace face, HausdorffDistanceVisitor vis, boolean printDetails) { + long startTime = System.currentTimeMillis(); + face.getMeshModel().compute(vis); + long endTime = System.currentTimeMillis(); + + System.out.println((endTime-startTime) + + "\tmsec: " + + vis.getStrategy() + + "\t" + vis.getClass().getSimpleName() + + "\t" + (vis.isAsynchronous() ? "concurrently" : "sequentially")); + + if (printDetails) { + Map<MeshFacet, List<Double>> results = vis.getDistances(); + for (MeshFacet facet: results.keySet()) { + System.out.println(results.get(facet)); + } + } } private static long measureKdTreeCreation(HumanFace face) { diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/ClosestNodeMultipleVisitor.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/ClosestNodeMultipleVisitor.java index e8218d0f..c528e61f 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/ClosestNodeMultipleVisitor.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/ClosestNodeMultipleVisitor.java @@ -41,12 +41,11 @@ public class ClosestNodeMultipleVisitor extends KdTreeVisitor { /** * @param point The 3D location for which the closest points are to be computed. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public ClosestNodeMultipleVisitor(Vector3d point, boolean concurrently) { - super(concurrently); + public ClosestNodeMultipleVisitor(Vector3d point, boolean asynchronous) { + super(asynchronous); if (point == null) { throw new IllegalArgumentException("point"); } diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceKdTreeVisitor.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceKdTreeVisitor.java index 78199c63..45d8439d 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceKdTreeVisitor.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceKdTreeVisitor.java @@ -17,10 +17,6 @@ import cz.fidentis.analyst.visitors.NearestMeshFacets; * <p> * The visitor returns the minimal distance and corresponding mesh facets. * </p> - * <p> - * This abstract visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple k-d trees simultaneously. - * </p> * * @author Radek Oslejsek */ @@ -52,12 +48,11 @@ public abstract class DistanceKdTreeVisitor extends KdTreeVisitor implements Nea * @param relativeDistance If {@true}, the visitor calculates the distances with respect * to the normals of the 3D point (the normal has to be present), * i.e., we can get negative distance. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceKdTreeVisitor(MeshPoint point, boolean relativeDistance, boolean concurrently) { - super(concurrently); + public DistanceKdTreeVisitor(MeshPoint point, boolean relativeDistance, boolean asynchronous) { + super(asynchronous); if (point == null) { throw new IllegalArgumentException("point"); } @@ -72,12 +67,11 @@ public abstract class DistanceKdTreeVisitor extends KdTreeVisitor implements Nea * Constructor. * * @param point A 3D point from which distance is computed. Must not be {@code null} - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceKdTreeVisitor(Vector3d point, boolean concurrently) { - this (new MeshPointImpl(point, null, null), false, concurrently); + public DistanceKdTreeVisitor(Vector3d point, boolean asynchronous) { + this (new MeshPointImpl(point, null, null), false, asynchronous); } @Override @@ -105,7 +99,7 @@ public abstract class DistanceKdTreeVisitor extends KdTreeVisitor implements Nea /** * @return -1 if the distance is migger (no update), 0 the same , 1 smaller */ - protected synchronized int checkAndUpdateFacets(double dist, int sign, Collection<MeshFacet> facets) { + protected int checkAndUpdateFacets(double dist, int sign, Collection<MeshFacet> facets) { if (dist > distance) { return -1; } @@ -125,7 +119,7 @@ public abstract class DistanceKdTreeVisitor extends KdTreeVisitor implements Nea /** * @return -1 if the distance is migger (no update), 0 the same , 1 smaller */ - protected synchronized int checkAndUpdateFacet(double dist, int sign, MeshFacet facet) { + protected int checkAndUpdateFacet(double dist, int sign, MeshFacet facet) { if (dist > distance) { return -1; } diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdTrianglesApprox.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdTrianglesApprox.java index 1ab652ba..2e73e7b9 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdTrianglesApprox.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdTrianglesApprox.java @@ -35,8 +35,7 @@ import cz.fidentis.analyst.visitors.NearestMeshTriangles; * points (see {@link NearestMeshTriangles} for more details). * </p> * <p> - * This visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple KD trees simultaneously. + * This visitor is thread-safe. * </p> * * @author Daniel Schramm @@ -51,22 +50,20 @@ public class DistanceToKdTrianglesApprox extends DistanceKdTreeVisitor implement * @param relativeDistance If {@true}, the visitor calculates the distances with respect * to the normals of the 3D point (the normal has to be present), * i.e., we can get negative distance. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToKdTrianglesApprox(MeshPoint point, boolean relativeDistance, boolean concurrently) { - super(point, relativeDistance, concurrently); + public DistanceToKdTrianglesApprox(MeshPoint point, boolean relativeDistance, boolean asynchronous) { + super(point, relativeDistance, asynchronous); } /** * @param point A 3D point from which distance is computed. Must not be {@code null} - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToKdTrianglesApprox(Vector3d point, boolean concurrently) { - this (new MeshPointImpl(point, null, null), false, concurrently); + public DistanceToKdTrianglesApprox(Vector3d point, boolean asynchronous) { + this (new MeshPointImpl(point, null, null), false, asynchronous); } @Override diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdVertices.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdVertices.java index e0b38662..82683042 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdVertices.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/kdtree/DistanceToKdVertices.java @@ -22,8 +22,7 @@ import cz.fidentis.analyst.visitors.NearestMeshPoints; * (see {@link NearestMeshPoints} for more details). * </p> * <p> - * This visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple k-d trees simultaneously. + * This visitor is thread-safe. * </p> * * @author Daniel Schramm @@ -37,22 +36,20 @@ public class DistanceToKdVertices extends DistanceKdTreeVisitor implements Neare * @param relativeDistance If {@true}, the visitor calculates the distances with respect * to the normals of the 3D point (the normal has to be present), * i.e., we can get negative distance. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToKdVertices(MeshPoint point, boolean relativeDistance, boolean concurrently) { - super(point, relativeDistance, concurrently); + public DistanceToKdVertices(MeshPoint point, boolean relativeDistance, boolean asynchronous) { + super(point, relativeDistance, asynchronous); } /** * @param point A 3D point from which distance is computed. Must not be {@code null} - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToKdVertices(Vector3d point, boolean concurrently) { - this (new MeshPointImpl(point, null, null), false, concurrently); + public DistanceToKdVertices(Vector3d point, boolean asynchronous) { + this (new MeshPointImpl(point, null, null), false, asynchronous); } @Override diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/BoundingBoxVisitor.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/BoundingBoxVisitor.java index 6a121d1f..f8011106 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/BoundingBoxVisitor.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/BoundingBoxVisitor.java @@ -6,8 +6,7 @@ import cz.fidentis.analyst.mesh.core.MeshFacet; /** * Visitor that computes a 3D bounding box (cube). * <p> - * This visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple mesh models or facets simultaneously. + * This visitor is thread-safe. * </p> * * @author Radek Oslejsek @@ -18,11 +17,10 @@ public class BoundingBoxVisitor extends MeshVisitor { /** * - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple mesf facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. */ - public BoundingBoxVisitor(boolean concurrently) { - super(concurrently); + public BoundingBoxVisitor(boolean asynchronous) { + super(asynchronous); } @Override diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceMeshVisitor.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceMeshVisitor.java index ec1d3d80..a29837da 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceMeshVisitor.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceMeshVisitor.java @@ -12,25 +12,10 @@ import java.util.List; import javax.vecmath.Vector3d; /** - * A functor. When instantiated, it can be gradually applied to multiple mesh facets. - * It inspects the state of triangular meshes one by one, and (cumulatevely) computes results. + * The common super-class for visitors computing minimal distance of the given 3D point + * to triangular meshes (mesh facets) * <p> - * Implement this interface whenever you want to define new algorithm over a {@link MeshFacet}. - * </p> - * <p> - * The visitor can be instatiated either as sequential and concurrent. - * The {@link cz.fidentis.analyst.mesh.MeshVisitor#visit} inspection method - * of a sequential visitor is applied immediately. On the contrary, if the - * visitor is concurrent and thread-safe, then the method only stores the inpected - * mesh for later concurrent inspection. To trigger the mesh inspection, - * a {@link java.util.concurrent.ExecutorService} has to be used calling - * the {@link cz.fidentis.analyst.mesh.MeshVisitor#call} method asynchronously - * and then waiting for the results of all triggered visitors. - * </p> - * <p> - * All visitors have to be cloneable. A returned clone represents a visitor in - * the initial state, i.e., only the configuration options provided to the constructor - * are cloned while the computation state is set to initial values. + * The visitor returns the minimal distance and corresponding mesh facets. * </p> * * @author Radek Oslejsek @@ -63,12 +48,11 @@ public abstract class DistanceMeshVisitor extends MeshVisitor implements Nearest * @param relativeDistance If {@true}, the visitor calculates the distances with respect * to the normals of the 3D point (the normal has to be present), * i.e., we can get negative distance. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceMeshVisitor(MeshPoint point, boolean relativeDistance, boolean concurrently) { - super(concurrently); + public DistanceMeshVisitor(MeshPoint point, boolean relativeDistance, boolean asynchronous) { + super(asynchronous); if (point == null) { throw new IllegalArgumentException("point"); } @@ -83,12 +67,11 @@ public abstract class DistanceMeshVisitor extends MeshVisitor implements Nearest * Constructor. * * @param point A 3D point from which distance is computed. Must not be {@code null} - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceMeshVisitor(Vector3d point, boolean concurrently) { - this (new MeshPointImpl(point, null, null), false, concurrently); + public DistanceMeshVisitor(Vector3d point, boolean asynchronous) { + this (new MeshPointImpl(point, null, null), false, asynchronous); } @Override diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTriangles.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTriangles.java index 77d10ced..b7a45498 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTriangles.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTriangles.java @@ -37,23 +37,21 @@ public class DistanceToTriangles extends DistanceMeshVisitor implements NearestM * @param relativeDistance If {@true}, the visitor calculates the distances with respect * to the normals of the 3D point (the normal has to be present), * i.e., we can get negative distance. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToTriangles(MeshPoint point, boolean relativeDistance, boolean concurrently) { - super(point, relativeDistance, concurrently); + public DistanceToTriangles(MeshPoint point, boolean relativeDistance, boolean asynchronous) { + super(point, relativeDistance, asynchronous); } /** * @param point A 3D point from which distance is computed. Must not be {@code null} * @param strategy Strategy got the computation of distance - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToTriangles(Vector3d point, boolean concurrently) { - this (new MeshPointImpl(point, null, null), false, concurrently); + public DistanceToTriangles(Vector3d point, boolean asynchronous) { + this (new MeshPointImpl(point, null, null), false, asynchronous); } @Override diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTrianglesApprox.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTrianglesApprox.java index 7364af2f..595694d3 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTrianglesApprox.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToTrianglesApprox.java @@ -29,8 +29,7 @@ import javax.vecmath.Vector3d; * points (see {@link NearestMeshTriangles} for more details). * </p> * <p> - * This visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple mesh models or facets simultaneously. + * This visitor is thread-safe. * </p> * * @author Radek Oslejsek @@ -45,22 +44,20 @@ public class DistanceToTrianglesApprox extends DistanceMeshVisitor implements Ne * @param relativeDistance If {@true}, the visitor calculates the distances with respect * to the normals of the 3D point (the normal has to be present), * i.e., we can get negative distance. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToTrianglesApprox(MeshPoint point, boolean relativeDistance, boolean concurrently) { - super(point, relativeDistance, concurrently); + public DistanceToTrianglesApprox(MeshPoint point, boolean relativeDistance, boolean asynchronous) { + super(point, relativeDistance, asynchronous); } /** * @param point A 3D point from which distance is computed. Must not be {@code null} - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple KD trees. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToTrianglesApprox(Vector3d point, boolean concurrently) { - this (new MeshPointImpl(point, null, null), false, concurrently); + public DistanceToTrianglesApprox(Vector3d point, boolean asynchornous) { + this (new MeshPointImpl(point, null, null), false, asynchornous); } @Override diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToVertices.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToVertices.java index 99f29044..45324d09 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToVertices.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/DistanceToVertices.java @@ -17,8 +17,7 @@ import javax.vecmath.Vector3d; * (see {@link NearestMeshPoints} for more details). * </p> * <p> - * This visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple mesh models or facets simultaneously. + * This visitor is thread-safe. * </p> * * @author Matej Lukes @@ -33,23 +32,21 @@ public class DistanceToVertices extends DistanceMeshVisitor implements NearestMe * @param relativeDistance If {@true}, the visitor calculates the distances with respect * to the normals of the 3D point (the normal has to be present), * i.e., we can get negative distance. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToVertices(MeshPoint point, boolean relativeDistance, boolean concurrently) { - super(point, relativeDistance, concurrently); + public DistanceToVertices(MeshPoint point, boolean relativeDistance, boolean asynchronous) { + super(point, relativeDistance, asynchronous); } /** * @param point A 3D point from which distance is computed. Must not be {@code null} * @param strategy Strategy got the computation of distance - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public DistanceToVertices(Vector3d point, boolean concurrently) { - this (new MeshPointImpl(point, null, null), false, concurrently); + public DistanceToVertices(Vector3d point, boolean asynchronous) { + this (new MeshPointImpl(point, null, null), false, asynchronous); } @Override diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceBruteForce.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceBruteForce.java index c3d01710..7acd4077 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceBruteForce.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceBruteForce.java @@ -23,8 +23,7 @@ import java.util.logging.Logger; * When applied to other facets, it computes Huasdorff distance from them to * the source mesh. * <p> - * This visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple mesh models or facets simultaneously. + * This visitor is thread-safe. * </p> * * @author Matej Lukes @@ -47,12 +46,11 @@ public class HausdorffDistanceBruteForce extends HausdorffDistanceVisitor { * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws {@code IllegalArgumentException} if some parametr is wrong */ - public HausdorffDistanceBruteForce(Set<MeshFacet> mainFacets, Strategy strategy, boolean relativeDistance, boolean concurrently) { - super(strategy, relativeDistance, concurrently); + public HausdorffDistanceBruteForce(Set<MeshFacet> mainFacets, Strategy strategy, boolean relativeDistance, boolean asynchronous) { + super(strategy, relativeDistance, asynchronous); if (mainFacets == null) { throw new IllegalArgumentException("mainFacets"); } @@ -67,12 +65,11 @@ public class HausdorffDistanceBruteForce extends HausdorffDistanceVisitor { * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public HausdorffDistanceBruteForce(MeshFacet mainFacet, Strategy strategy, boolean relativeDistance, boolean concurrently) { - this(new HashSet<>(Collections.singleton(mainFacet)), strategy, relativeDistance, concurrently); + public HausdorffDistanceBruteForce(MeshFacet mainFacet, Strategy strategy, boolean relativeDistance, boolean asynchronous) { + this(new HashSet<>(Collections.singleton(mainFacet)), strategy, relativeDistance, asynchronous); if (mainFacet == null) { throw new IllegalArgumentException("mainFacet"); } @@ -87,12 +84,11 @@ public class HausdorffDistanceBruteForce extends HausdorffDistanceVisitor { * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public HausdorffDistanceBruteForce(MeshModel mainModel, Strategy strategy, boolean relativeDistance, boolean concurrently) { - this(new HashSet<>(mainModel.getFacets()), strategy, relativeDistance, concurrently); + public HausdorffDistanceBruteForce(MeshModel mainModel, Strategy strategy, boolean relativeDistance, boolean asynchronous) { + this(new HashSet<>(mainModel.getFacets()), strategy, relativeDistance, asynchronous); if (mainModel.getFacets().isEmpty()) { throw new IllegalArgumentException("mainModel"); } @@ -110,9 +106,9 @@ public class HausdorffDistanceBruteForce extends HausdorffDistanceVisitor { boolean firstApplication = true; for (final MeshFacet mainFacet: myFacets) { for (int i = 0; i < vertices.size(); i++) { // for each vertex of comparedFacet - final DistanceMeshVisitor visitor = instantiateVisitor(vertices.get(i), concurrently()); + final DistanceMeshVisitor visitor = instantiateVisitor(vertices.get(i), isAsynchronous()); mainFacet.accept(visitor); - if (concurrently()) { // fork and continue + if (isAsynchronous()) { // fork and continue final Future<MeshVisitor> result = executor.submit(visitor); results.add(result); } else { // get and update distance @@ -120,7 +116,7 @@ public class HausdorffDistanceBruteForce extends HausdorffDistanceVisitor { } } - if (concurrently()) { // process asynchronous calls + if (isAsynchronous()) { // process asynchronous calls try { for (int i = 0; i < results.size(); i++) { final DistanceMeshVisitor visitor = (DistanceMeshVisitor) results.get(i).get(); // waits until all computations are finished diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceKdTree.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceKdTree.java index ea9f63e7..9988f589 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceKdTree.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceKdTree.java @@ -48,12 +48,11 @@ public class HausdorffDistanceKdTree extends HausdorffDistanceVisitor { * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public HausdorffDistanceKdTree(Set<MeshFacet> mainFacets, Strategy strategy, boolean relativeDistance, boolean concurrently) { - super(strategy, relativeDistance, concurrently); + public HausdorffDistanceKdTree(Set<MeshFacet> mainFacets, Strategy strategy, boolean relativeDistance, boolean asynchronous) { + super(strategy, relativeDistance, asynchronous); if (mainFacets == null) { throw new IllegalArgumentException("mainFacets"); } @@ -68,12 +67,11 @@ public class HausdorffDistanceKdTree extends HausdorffDistanceVisitor { * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public HausdorffDistanceKdTree(MeshFacet mainFacet, Strategy strategy, boolean relativeDistance, boolean concurrently) { - this(new HashSet<>(Collections.singleton(mainFacet)), strategy, relativeDistance, concurrently); + public HausdorffDistanceKdTree(MeshFacet mainFacet, Strategy strategy, boolean relativeDistance, boolean asynchronous) { + this(new HashSet<>(Collections.singleton(mainFacet)), strategy, relativeDistance, asynchronous); if (mainFacet == null) { throw new IllegalArgumentException("mainFacet"); } @@ -88,12 +86,11 @@ public class HausdorffDistanceKdTree extends HausdorffDistanceVisitor { * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public HausdorffDistanceKdTree(MeshModel mainModel, Strategy strategy, boolean relativeDistance, boolean concurrently) { - this(new HashSet<>(mainModel.getFacets()), strategy, relativeDistance, concurrently); + public HausdorffDistanceKdTree(MeshModel mainModel, Strategy strategy, boolean relativeDistance, boolean asynchronous) { + this(new HashSet<>(mainModel.getFacets()), strategy, relativeDistance, asynchronous); if (mainModel.getFacets().isEmpty()) { throw new IllegalArgumentException("mainModel"); } @@ -107,12 +104,11 @@ public class HausdorffDistanceKdTree extends HausdorffDistanceVisitor { * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws IllegalArgumentException if some parametr is wrong */ - public HausdorffDistanceKdTree(KdTree mainKdTree, Strategy strategy, boolean relativeDistance, boolean concurrently) { - super(strategy, relativeDistance, concurrently); + public HausdorffDistanceKdTree(KdTree mainKdTree, Strategy strategy, boolean relativeDistance, boolean asynchronous) { + super(strategy, relativeDistance, asynchronous); if (mainKdTree == null) { throw new IllegalArgumentException("mainKdTree"); } @@ -138,9 +134,9 @@ public class HausdorffDistanceKdTree extends HausdorffDistanceVisitor { final List<MeshPoint> vertices = comparedFacet.getVertices(); for (int i = 0; i < vertices.size(); i++) { // for each vertex of comparedFacet - final DistanceKdTreeVisitor visitor = instantiateVisitor(vertices.get(i), concurrently()); + final DistanceKdTreeVisitor visitor = instantiateVisitor(vertices.get(i), isAsynchronous()); kdTree.accept(visitor); - if (concurrently()) { // fork and continue + if (isAsynchronous()) { // fork and continue final Future<KdTreeVisitor> result = executor.submit(visitor); results.add(result); } else { // get and update distance @@ -148,7 +144,7 @@ public class HausdorffDistanceKdTree extends HausdorffDistanceVisitor { } } - if (concurrently()) { // process asynchronous calls + if (isAsynchronous()) { // process asynchronous calls try { for (int i = 0; i < results.size(); i++) { final DistanceKdTreeVisitor visitor = (DistanceKdTreeVisitor) results.get(i).get(); // waits until all computations are finished diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceVisitor.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceVisitor.java index 375bd1ed..5d2037ec 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceVisitor.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistanceVisitor.java @@ -62,12 +62,11 @@ public abstract class HausdorffDistanceVisitor extends MeshVisitor implements Ha * @param relativeDistance If true, then the visitor calculates the relative distances with respect * to the normal vectors of source facets (normal vectors have to present), * i.e., we can get negative distances. - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently when inspecting multiple mesh facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. * @throws {@code IllegalArgumentException} if some parametr is wrong */ - public HausdorffDistanceVisitor(Strategy strategy, boolean relativeDistance, boolean concurrently) { - super(concurrently); + public HausdorffDistanceVisitor(Strategy strategy, boolean relativeDistance, boolean asynchronous) { + super(asynchronous); this.strategy = strategy; this.relativeDist = relativeDistance; } diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/TriangleListVisitor.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/TriangleListVisitor.java index d68315b1..bd136b76 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/TriangleListVisitor.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/TriangleListVisitor.java @@ -11,11 +11,10 @@ import java.util.List; * It is usefull for collecting triangles of multiple mesh facets. * For iterating through triangles of a single facet, use the iterator instead. * Or iterate through the facet in a for-each cycle. -* <p> - * This visitor is thread-safe. A single instance of the visitor can be used - * to inspect multiple mesh models or facets simultaneously. + * <p> + * This visitor is thread-safe. * </p> - * + * * @author Radek Oslejsek */ public class TriangleListVisitor extends MeshVisitor { @@ -24,11 +23,10 @@ public class TriangleListVisitor extends MeshVisitor { /** * - * @param concurrently If {@code true} and this visitor is thread-safe, then - * the visitor is applied concurrently on multiple mesf facets. + * @param asynchronous If {@code true}, then asynchronous visitor is created. */ - public TriangleListVisitor(boolean concurrently) { - super(concurrently); + public TriangleListVisitor(boolean asynchronous) { + super(asynchronous); } @Override diff --git a/MeshModel/src/main/java/cz/fidentis/analyst/kdtree/KdTreeVisitor.java b/MeshModel/src/main/java/cz/fidentis/analyst/kdtree/KdTreeVisitor.java index df122b9e..091e696a 100644 --- a/MeshModel/src/main/java/cz/fidentis/analyst/kdtree/KdTreeVisitor.java +++ b/MeshModel/src/main/java/cz/fidentis/analyst/kdtree/KdTreeVisitor.java @@ -9,24 +9,30 @@ import java.util.concurrent.Callable; * Implement this interface whenever you want to define new algorithm over a k-d tree. * </p> * <p> - * The visitor can be instatiated either as sequential and concurrent. + * The visitor can be instatiated either as synchronous or asynchronous. * The {@link cz.fidentis.analyst.kdtree.KdTreeVisitor#visit} inspection method - * of a sequential visitor is applied immediately. On the contrary, if the - * visitor is concurrent and thread-safe, then the method only stores the inpected - * k-d tree for later concurrent inspection. To trigger the tree inspection, + * of a synchronous visitor is applied immediately. On the contrary, if the + * visitor is asynchronous, then the method only stores the inpected + * k-d tree for later concurrent inspection. To trigger the inspection, * a {@link java.util.concurrent.ExecutorService} has to be used calling * the {@link cz.fidentis.analyst.kdtree.KdTreeVisitor#call} method asynchronously * and then waiting for the results of all triggered visitors. * </p> + * <p> + * If the visitor is thread-safe, then a single instance of the visitor + * can visit concurrenly (and assynchronously) multiple k-d trees. Otherwise, + * the parallel inspection is still possible, but a new instance of the visitor + * has to be used for each k-d tree. + * </p> * * @author Daniel Schramm */ public abstract class KdTreeVisitor implements Callable<KdTreeVisitor> { - private boolean concurrently; + private boolean asynchronous; /** - * KD tree stored temporarily for later cuncurrent inspection via + * K-d tree stored temporarily for later cuncurrent inspection via * the {@link cz.fidentis.analyst.kdtree.KdTreeVisitor#visit} method. */ private KdTree kdTree; @@ -34,15 +40,14 @@ public abstract class KdTreeVisitor implements Callable<KdTreeVisitor> { /** * Constructor. * - * @param concurrently If {@code true} and the visitor is thread-safe, then - * the visitor is created as concurent + * @param asynchronous If {@code true}, then asynchronous visitor is created. * (the {@link cz.fidentis.analyst.kdtree.KdTreeVisitor#visit} is not executed * immediately but postponed for concurrent asynchronous execution). * Otherwise, the visitor is created as sequential * (the {@link cz.fidentis.analyst.kdtree.KdTreeVisitor#visit} is executed immediately.) */ - public KdTreeVisitor(boolean concurrently) { - this.concurrently = concurrently; + public KdTreeVisitor(boolean asynchronous) { + this.asynchronous = asynchronous; } /** @@ -63,14 +68,12 @@ public abstract class KdTreeVisitor implements Callable<KdTreeVisitor> { } /** - * Returns {@code true} if this visitor is to be applied concurrently, i.e., - * if the concurrency parameter was set and the visitor is thread-safe. + * Returns {@code true} if this visitor was created as asynchronous. * - * @return {@code true} if this visitor is to be applied concurrently, i.e., - * if the concurrency parameter was set and the visitor is thread-safe. + * @return {@code true} if this visitor was created as asynchronous. */ - public boolean concurrently() { - return this.concurrently && isThreadSafe(); + public boolean isAsynchronous() { + return this.asynchronous; } /** @@ -82,13 +85,13 @@ public abstract class KdTreeVisitor implements Callable<KdTreeVisitor> { /** * The main visitation method, either invoked immediately (if the visitor - * was created a sequentional) or postponed to later parallel execution - * (if the visitor is thread-safe and has been created a concurrent). + * was created a synchronous) or postponed to later parallel execution + * (if the visitor was created a asynchronous). * * @param kdTree KD tree to be visited */ public void visit(KdTree kdTree) { - if (concurrently && isThreadSafe()) { + if (asynchronous) { this.kdTree = kdTree; } else { visitKdTree(kdTree); diff --git a/MeshModel/src/main/java/cz/fidentis/analyst/mesh/MeshVisitor.java b/MeshModel/src/main/java/cz/fidentis/analyst/mesh/MeshVisitor.java index d7f74094..d58a681e 100644 --- a/MeshModel/src/main/java/cz/fidentis/analyst/mesh/MeshVisitor.java +++ b/MeshModel/src/main/java/cz/fidentis/analyst/mesh/MeshVisitor.java @@ -10,21 +10,27 @@ import java.util.concurrent.Callable; * Implement this interface whenever you want to define new algorithm over a mesh. * </p> * <p> - * The visitor can be instatiated either as sequential or concurrent. + * The visitor can be instatiated either as synchronous or asynchronous. * The {@link cz.fidentis.analyst.mesh.MeshVisitor#visit} inspection method - * of a sequential visitor is applied immediately. On the contrary, if the - * visitor is concurrent and thread-safe, then the methed only stores the inpected - * mesh facet for later concurrent inspection. To trigger the facet inspection, - * a {@link java.util.concurrent.ExecutorService} has to be used to call - * the {@link cz.fidentis.analyst.mesh.mesh.MeshVisitor#call} method asynchronously + * of a synchronous visitor is applied immediately. On the contrary, if the + * visitor is asynchronous, then the method only stores the inpected + * mesh for later concurrent inspection. To trigger the inspection, + * a {@link java.util.concurrent.ExecutorService} has to be used calling + * the {@link cz.fidentis.analyst.mesh.MeshVisitor#call} method asynchronously * and then waiting for the results of all triggered visitors. * </p> - * + * <p> + * If the visitor is thread-safe, then a single instance of the visitor + * can visit concurrenly (and assynchronously) multiple kd-trees. Otherwise, + * the parallel inspection is still possible, but a new instance of the visitor + * has to be used for each k-d tree. + * </p> + * * @author Radek Oslejsek */ public abstract class MeshVisitor implements Callable<MeshVisitor> { - private boolean concurrently; + private boolean asynchronous; /** * Facet stored temporarily for later cuncurrent inspection via @@ -36,15 +42,14 @@ public abstract class MeshVisitor implements Callable<MeshVisitor> { /** * Constructor. * - * @param concurrently If {@code true} and the visitor is thread-safe, then - * the visitor is created as concurent + * @param asynchronous If {@code true}, then asynchronous visitor is created. * (the {@link cz.fidentis.analyst.mesh.MeshVisitor#visit} is not executed * immediately but postponed for concurrent asynchronous execution). * Otherwise, the visitor is created as sequential * (the {@link cz.fidentis.analyst.mesh.MeshVisitor#visit} is executed immediately.) */ - public MeshVisitor(boolean concurrently) { - this.concurrently = concurrently; + public MeshVisitor(boolean asynchronous) { + this.asynchronous = asynchronous; } /** @@ -65,14 +70,12 @@ public abstract class MeshVisitor implements Callable<MeshVisitor> { } /** - * Returns {@code true} if this visitor is to be applied concurrently, i.e., - * if the concurrency parameter was set and the visitor is thread-safe. + * Returns {@code true} if this visitor was created as asynchronous. * - * @return {@code true} if this visitor is to be applied concurrently, i.e., - * if the concurrency parameter was set and the visitor is thread-safe. + * @return {@code true} if this visitor was created as asynchronous. */ - public boolean concurrently() { - return this.concurrently && isThreadSafe(); + public boolean isAsynchronous() { + return this.asynchronous; } /** @@ -84,13 +87,13 @@ public abstract class MeshVisitor implements Callable<MeshVisitor> { /** * The main visitation method, either invoked immediately (if the visitor - * was created a sequentional) or postponed to later parallel execution - * (if the visitor is thread-safe and has been created a concurrent). + * was created a synchronous) or postponed to later parallel execution + * (if the visitor was created a asynchronous). * * @param facet Mesh facet to be visited. */ public void visit(MeshFacet facet) { - if (concurrently && isThreadSafe()) { + if (asynchronous) { this.facet = facet; } else { visitMeshFacet(facet); diff --git a/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshModel.java b/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshModel.java index 6d3711eb..0c108b52 100644 --- a/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshModel.java +++ b/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshModel.java @@ -91,7 +91,7 @@ public class MeshModel { * @throws {@code NullPointerException} if the visitor is {@code null} */ public void compute(MeshVisitor visitor, ExecutorService exec) { - if (visitor.concurrently()) { + if (visitor.isAsynchronous()) { ExecutorService executor = exec; if (executor == null) { int threads = Runtime.getRuntime().availableProcessors(); @@ -110,7 +110,7 @@ public class MeshModel { try { for (Future<MeshVisitor> f : results) { - f.get(); // wait ntil all computations are finished + f.get(); // wait until all computations are finished } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MeshModel.class.getName()).log(Level.SEVERE, null, ex); -- GitLab