From 6402a9ea76b62d43b9b9a32826d14c797d860efb Mon Sep 17 00:00:00 2001
From: Radek Oslejsek <oslejsek@fi.muni.cz>
Date: Wed, 5 May 2021 18:22:07 +0200
Subject: [PATCH] Removed parallel computation of avg template face

---
 .../analyst/face/AvgFaceConstructor.java      | 88 +++----------------
 .../fidentis/analyst/face/BatchProcessor.java |  4 +-
 2 files changed, 14 insertions(+), 78 deletions(-)

diff --git a/Comparison/src/main/java/cz/fidentis/analyst/face/AvgFaceConstructor.java b/Comparison/src/main/java/cz/fidentis/analyst/face/AvgFaceConstructor.java
index 3611950b..e7fe7d0a 100644
--- a/Comparison/src/main/java/cz/fidentis/analyst/face/AvgFaceConstructor.java
+++ b/Comparison/src/main/java/cz/fidentis/analyst/face/AvgFaceConstructor.java
@@ -13,13 +13,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import javax.vecmath.Point3d;
 import javax.vecmath.Vector3d;
 
@@ -37,8 +30,6 @@ public class AvgFaceConstructor extends KdTreeVisitor {
     
     private MeshModel avgMeshModel = null;
     
-    private final boolean parallel;
-    
     private int numInspectedFacets = 0;
     
     private final Map<MeshFacet, List<Vector3d>> transformations = new HashMap<>();
@@ -47,11 +38,10 @@ public class AvgFaceConstructor extends KdTreeVisitor {
      * Constructor.
      * 
      * @param templateFacet Mesh facet which is transformed to the averaged mesh
-     * @param parallel If {@code true}, then the algorithm runs concurrently utilizing all CPU cores
      * @throws IllegalArgumentException if some parameter is wrong
      */
-    public AvgFaceConstructor(MeshFacet templateFacet, boolean parallel) {
-        this(new HashSet<>(Collections.singleton(templateFacet)), parallel);
+    public AvgFaceConstructor(MeshFacet templateFacet) {
+        this(new HashSet<>(Collections.singleton(templateFacet)));
         if (templateFacet == null) {
             throw new IllegalArgumentException("templateFacet");
         }
@@ -61,10 +51,9 @@ public class AvgFaceConstructor extends KdTreeVisitor {
      * Constructor.
      * 
      * @param templateFacets Mesh facets that are transformed to the averaged mesh
-     * @param parallel If {@code true}, then the algorithm runs concurrently utilizing all CPU cores
      * @throws IllegalArgumentException if some parameter is wrong
      */
-    public AvgFaceConstructor(Set<MeshFacet> templateFacets, boolean parallel) {
+    public AvgFaceConstructor(Set<MeshFacet> templateFacets) {
         if (templateFacets == null || templateFacets.isEmpty()) {
             throw new IllegalArgumentException("templateFacets");
         }
@@ -73,19 +62,16 @@ public class AvgFaceConstructor extends KdTreeVisitor {
         templateFacets.parallelStream().forEach(
                 f -> this.transformations.put(f, new ArrayList<>(f.getVertices().size()))
         );
-        
-        this.parallel = parallel;
     }
     
     /**
      * Constructor.
      * 
      * @param templateModel Mesh model which is transformed to the averaged mesh
-     * @param parallel If {@code true}, then the algorithm runs concurrently utilizing all CPU cores
      * @throws IllegalArgumentException if some parameter is wrong
      */
-    public AvgFaceConstructor(MeshModel templateModel, boolean parallel) {
-        this(new HashSet<>(templateModel.getFacets()), parallel);
+    public AvgFaceConstructor(MeshModel templateModel) {
+        this(new HashSet<>(templateModel.getFacets()));
     }
     
     @Override
@@ -93,45 +79,20 @@ public class AvgFaceConstructor extends KdTreeVisitor {
         avgMeshModel = null;
         numInspectedFacets++;
 
-        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
-        final Map<MeshFacet, List<Future<KdTreeClosestNode>>> results = new HashMap<>();
-        
         for (MeshFacet myFacet: transformations.keySet()) {
             for (int i = 0; i < myFacet.getNumberOfVertices(); i++) {
                 Point3d myPoint = myFacet.getVertex(i).getPosition();
                 KdTreeClosestNode visitor = new KdTreeClosestNode(myPoint);
+                kdTree.accept(visitor);
                 
-                if (inParallel()) {
-                    if (!results.containsKey(myFacet)) {
-                        results.put(myFacet, new ArrayList<>(myFacet.getNumberOfVertices()));
-                    }
-                    results.get(myFacet).add(executor.submit(new Callable(){
-                        @Override
-                        public Object call() throws Exception {
-                            kdTree.accept(visitor);
-                            return visitor;
-                        }
-                    }));
+                // update result:
+                Vector3d moveDir = new Vector3d(visitor.getAnyClosestNode().getLocation());
+                moveDir.sub(visitor.getReferencePoint());
+                if (transformations.get(myFacet).size() < myFacet.getNumTriangles()) { // First inspected facet
+                    transformations.get(myFacet).add(moveDir);
                 } else {
-                    kdTree.accept(visitor);
-                    updateResults(visitor, myFacet, i);
-                }
-            }
-        }
-            
-        if (inParallel()) {
-            executor.shutdown();
-            while (!executor.isTerminated()){}
-            try {
-                for (MeshFacet facet: results.keySet()) {
-                    int i = 0;
-                    for (Future<KdTreeClosestNode> res: results.get(facet)) {
-                        final KdTreeClosestNode visitor = res.get();
-                        updateResults(visitor, facet, i++);
-                    }
+                    transformations.get(myFacet).get(i).add(moveDir);
                 }
-            } catch (final InterruptedException | ExecutionException ex) {
-                Logger.getLogger(AvgFaceConstructor.class.getName()).log(Level.SEVERE, null, ex);
             }
         }
     }
@@ -156,29 +117,4 @@ public class AvgFaceConstructor extends KdTreeVisitor {
         return avgMeshModel;
     }
     
-    /**
-     * Returns {@code true} if the distance computation is parallel.
-     * @return {@code true} if the distance computation is parallel.
-     */
-    public boolean inParallel() {
-        return parallel;
-    }
-    
-    /**
-     * Updates transformation.
-     * 
-     * @param visitor Visitor
-     * @param inspectedFacet Mesh facet that has been inspected
-     * @param vertIndex Vertex index of the inspected facet to be updated
-     */
-    protected void updateResults(KdTreeClosestNode visitor, MeshFacet inspectedFacet, int vertIndex) {
-        Vector3d moveDir = new Vector3d(visitor.getAnyClosestNode().getLocation());
-        moveDir.sub(visitor.getReferencePoint());
-        if (transformations.get(inspectedFacet).size() < inspectedFacet.getNumTriangles()) {
-            // First inspected facet
-            transformations.get(inspectedFacet).add(moveDir);
-        } else {
-            transformations.get(inspectedFacet).get(vertIndex).add(moveDir);
-        }
-    }
 }
diff --git a/Comparison/src/main/java/cz/fidentis/analyst/face/BatchProcessor.java b/Comparison/src/main/java/cz/fidentis/analyst/face/BatchProcessor.java
index 78c42889..35d00d95 100644
--- a/Comparison/src/main/java/cz/fidentis/analyst/face/BatchProcessor.java
+++ b/Comparison/src/main/java/cz/fidentis/analyst/face/BatchProcessor.java
@@ -10,7 +10,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 /**
- *
+ * 
  * @author Radek Oslejsek
  */
 public class BatchProcessor {
@@ -78,7 +78,7 @@ public class BatchProcessor {
         this.templateFace = pathToFace(initTempPath);
         
         HumanFaceFactory.instance().setStrategy(HumanFaceFactory.Strategy.MRU);
-        AvgFaceConstructor constructor = new AvgFaceConstructor(this.templateFace.getMeshModel(), true);
+        AvgFaceConstructor constructor = new AvgFaceConstructor(this.templateFace.getMeshModel());
         for (String facePath: faces) {
             String faceId = HumanFaceFactory.instance().loadFace(new File(facePath));
             if (this.templateFace.getId().equals(faceId)) { // skip the same face
-- 
GitLab