diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistance.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistance.java index f4dc9414fd2f61a96d02faad8e65d465650a2fc6..6d2664044e189b7ea649eaa8de5ac7bf512d1640 100644 --- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistance.java +++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/HausdorffDistance.java @@ -115,8 +115,6 @@ public class HausdorffDistance extends MeshVisitor { private final boolean parallel; - protected static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - /** * The source triangular mesh (set of mesh facets) stred in k-d tree. */ @@ -270,34 +268,23 @@ public class HausdorffDistance extends MeshVisitor { final List<MeshPoint> vertices = comparedFacet.getVertices(); final List<Future<KdTreeVisitor>> results = new ArrayList<>(vertices.size()); + ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + for (int i = 0; i < vertices.size(); i++) { // for each vertex of comparedFacet - MeshPoint inspectedPoint = vertices.get(i); - - KdTreeVisitor visitor = null; - switch (getStrategy()) { - case POINT_TO_POINT_ABSOLUTE: - visitor = new KdTreeDistance(inspectedPoint.getPosition(), inParallel()); - break; - case POINT_TO_POINT: - visitor = new KdTreeDistanceToVertices(inspectedPoint.getPosition(), inParallel()); - break; - default: - case POINT_TO_TRIANGLE_APPROXIMATE: - visitor = new KdTreeApproxDistanceToTriangles(inspectedPoint.getPosition(), inParallel()); - break; - } - - kdTree.accept(visitor); // compute the distance for i-th vertex + KdTreeVisitor visitor = instantiateVisitor(vertices.get(i).getPosition()); + kdTree.accept(visitor); // compute the distance for i-th vertex or prepare the visitor for computation if (inParallel()) { // fork and continue - final Future<KdTreeVisitor> result = EXECUTOR.submit(visitor); - results.add(result); - } else { // get and update distance immediately - addDistanceToPoint(visitor, inspectedPoint, distList); + results.add(executor.submit(visitor)); + } else { // process distance results immediately + addDistanceToPoint(visitor, vertices.get(i), distList); } } - if (inParallel()) { // process asynchronous calls + if (inParallel()) { // process asynchronous computation of distance + executor.shutdown(); + while (!executor.isTerminated()){ + } try { int i = 0; for (Future<KdTreeVisitor> res: results) { @@ -311,6 +298,18 @@ public class HausdorffDistance extends MeshVisitor { } } + protected KdTreeVisitor instantiateVisitor(Vector3d inspectedPoint) { + switch (getStrategy()) { + case POINT_TO_POINT_ABSOLUTE: + return new KdTreeDistance(inspectedPoint, inParallel()); + case POINT_TO_POINT: + return new KdTreeDistanceToVertices(inspectedPoint, inParallel()); + default: + case POINT_TO_TRIANGLE_APPROXIMATE: + return new KdTreeApproxDistanceToTriangles(inspectedPoint, inParallel()); + } + } + protected void addDistanceToPoint(KdTreeVisitor vis, MeshPoint point, List<Double> pointDistances) { if (relativeDist && vis instanceof DistanceWithNearestPoints) { double relDist = checkDist((DistanceWithNearestPoints)vis, point); 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 6f2958b6ef0e0ce0e83bd9b6fa09c24a80ea0c20..079fe9b836da229b82662173277a0798c5b6e47d 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 @@ -89,17 +89,11 @@ public class MeshModel { * Otherwise, the visitor is applied sequentionally (and synchronously). * * @param visitor Visitor to be apllied for the computation - * @param exec Exisitng executor. If {@code null}, then a new private executor is used. * @throws {@code NullPointerException} if the visitor is {@code null} */ - public void compute(MeshVisitor visitor, ExecutorService exec) { + public void compute(MeshVisitor visitor) { if (visitor.isAsynchronous() && visitor.isThreadSafe()) { - ExecutorService executor = exec; - if (executor == null) { - int threads = Runtime.getRuntime().availableProcessors(); - executor = Executors.newFixedThreadPool(threads); - } - + ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); List<Future<MeshVisitor>> results = new LinkedList<>(); for (MeshFacet f: this.facets) { @@ -108,7 +102,9 @@ public class MeshModel { results.add(result); } - //executor.shutdown(); + executor.shutdown(); + while (!executor.isTerminated()){ + } try { for (Future<MeshVisitor> f : results) { @@ -124,15 +120,6 @@ public class MeshModel { } } - /** - * Applies the visitor to all mesh facets, either sequentially or in parallel. - * - * @param visitor Visitor to be apllied - */ - public void compute(MeshVisitor visitor) { - compute(visitor, null); - } - /** * Registers listeners (objects concerned in the mesh model changes) to receive events. * If listener is {@code null}, no exception is thrown and no action is taken.