Skip to content
Snippets Groups Projects
Commit 6402a9ea authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

Removed parallel computation of avg template face

parent e359b9ed
No related branches found
No related tags found
No related merge requests found
...@@ -13,13 +13,6 @@ import java.util.HashSet; ...@@ -13,13 +13,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; 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.Point3d;
import javax.vecmath.Vector3d; import javax.vecmath.Vector3d;
...@@ -37,8 +30,6 @@ public class AvgFaceConstructor extends KdTreeVisitor { ...@@ -37,8 +30,6 @@ public class AvgFaceConstructor extends KdTreeVisitor {
private MeshModel avgMeshModel = null; private MeshModel avgMeshModel = null;
private final boolean parallel;
private int numInspectedFacets = 0; private int numInspectedFacets = 0;
private final Map<MeshFacet, List<Vector3d>> transformations = new HashMap<>(); private final Map<MeshFacet, List<Vector3d>> transformations = new HashMap<>();
...@@ -47,11 +38,10 @@ public class AvgFaceConstructor extends KdTreeVisitor { ...@@ -47,11 +38,10 @@ public class AvgFaceConstructor extends KdTreeVisitor {
* Constructor. * Constructor.
* *
* @param templateFacet Mesh facet which is transformed to the averaged mesh * @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 * @throws IllegalArgumentException if some parameter is wrong
*/ */
public AvgFaceConstructor(MeshFacet templateFacet, boolean parallel) { public AvgFaceConstructor(MeshFacet templateFacet) {
this(new HashSet<>(Collections.singleton(templateFacet)), parallel); this(new HashSet<>(Collections.singleton(templateFacet)));
if (templateFacet == null) { if (templateFacet == null) {
throw new IllegalArgumentException("templateFacet"); throw new IllegalArgumentException("templateFacet");
} }
...@@ -61,10 +51,9 @@ public class AvgFaceConstructor extends KdTreeVisitor { ...@@ -61,10 +51,9 @@ public class AvgFaceConstructor extends KdTreeVisitor {
* Constructor. * Constructor.
* *
* @param templateFacets Mesh facets that are transformed to the averaged mesh * @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 * @throws IllegalArgumentException if some parameter is wrong
*/ */
public AvgFaceConstructor(Set<MeshFacet> templateFacets, boolean parallel) { public AvgFaceConstructor(Set<MeshFacet> templateFacets) {
if (templateFacets == null || templateFacets.isEmpty()) { if (templateFacets == null || templateFacets.isEmpty()) {
throw new IllegalArgumentException("templateFacets"); throw new IllegalArgumentException("templateFacets");
} }
...@@ -73,19 +62,16 @@ public class AvgFaceConstructor extends KdTreeVisitor { ...@@ -73,19 +62,16 @@ public class AvgFaceConstructor extends KdTreeVisitor {
templateFacets.parallelStream().forEach( templateFacets.parallelStream().forEach(
f -> this.transformations.put(f, new ArrayList<>(f.getVertices().size())) f -> this.transformations.put(f, new ArrayList<>(f.getVertices().size()))
); );
this.parallel = parallel;
} }
/** /**
* Constructor. * Constructor.
* *
* @param templateModel Mesh model which is transformed to the averaged mesh * @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 * @throws IllegalArgumentException if some parameter is wrong
*/ */
public AvgFaceConstructor(MeshModel templateModel, boolean parallel) { public AvgFaceConstructor(MeshModel templateModel) {
this(new HashSet<>(templateModel.getFacets()), parallel); this(new HashSet<>(templateModel.getFacets()));
} }
@Override @Override
...@@ -93,45 +79,20 @@ public class AvgFaceConstructor extends KdTreeVisitor { ...@@ -93,45 +79,20 @@ public class AvgFaceConstructor extends KdTreeVisitor {
avgMeshModel = null; avgMeshModel = null;
numInspectedFacets++; numInspectedFacets++;
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
final Map<MeshFacet, List<Future<KdTreeClosestNode>>> results = new HashMap<>();
for (MeshFacet myFacet: transformations.keySet()) { for (MeshFacet myFacet: transformations.keySet()) {
for (int i = 0; i < myFacet.getNumberOfVertices(); i++) { for (int i = 0; i < myFacet.getNumberOfVertices(); i++) {
Point3d myPoint = myFacet.getVertex(i).getPosition(); Point3d myPoint = myFacet.getVertex(i).getPosition();
KdTreeClosestNode visitor = new KdTreeClosestNode(myPoint); KdTreeClosestNode visitor = new KdTreeClosestNode(myPoint);
kdTree.accept(visitor);
if (inParallel()) { // update result:
if (!results.containsKey(myFacet)) { Vector3d moveDir = new Vector3d(visitor.getAnyClosestNode().getLocation());
results.put(myFacet, new ArrayList<>(myFacet.getNumberOfVertices())); moveDir.sub(visitor.getReferencePoint());
} if (transformations.get(myFacet).size() < myFacet.getNumTriangles()) { // First inspected facet
results.get(myFacet).add(executor.submit(new Callable(){ transformations.get(myFacet).add(moveDir);
@Override
public Object call() throws Exception {
kdTree.accept(visitor);
return visitor;
}
}));
} else { } else {
kdTree.accept(visitor); transformations.get(myFacet).get(i).add(moveDir);
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++);
}
} }
} catch (final InterruptedException | ExecutionException ex) {
Logger.getLogger(AvgFaceConstructor.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
} }
...@@ -156,29 +117,4 @@ public class AvgFaceConstructor extends KdTreeVisitor { ...@@ -156,29 +117,4 @@ public class AvgFaceConstructor extends KdTreeVisitor {
return avgMeshModel; 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);
}
}
} }
...@@ -10,7 +10,7 @@ import java.util.ArrayList; ...@@ -10,7 +10,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* *
* @author Radek Oslejsek * @author Radek Oslejsek
*/ */
public class BatchProcessor { public class BatchProcessor {
...@@ -78,7 +78,7 @@ public class BatchProcessor { ...@@ -78,7 +78,7 @@ public class BatchProcessor {
this.templateFace = pathToFace(initTempPath); this.templateFace = pathToFace(initTempPath);
HumanFaceFactory.instance().setStrategy(HumanFaceFactory.Strategy.MRU); 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) { for (String facePath: faces) {
String faceId = HumanFaceFactory.instance().loadFace(new File(facePath)); String faceId = HumanFaceFactory.instance().loadFace(new File(facePath));
if (this.templateFace.getId().equals(faceId)) { // skip the same face if (this.templateFace.getId().equals(faceId)) { // skip the same face
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment