}
int zmin = -1;
int zPerThread = zsize / threadCount;
for (int i = 0; i < threadCount; i++) {
GenerateFacesThread thread = new GenerateFacesThread(pSampler, pSeekValue);
threads.add(thread);
thread.setZmin(zmin);
thread.setWithNormals(withNormals);
thread.setProgressUpdater(progressUpdater, totalProgress);
int zmax = zmin + zPerThread - 1;
if (zmax >= pSampler.getStackZSize()) {
zmax = pSampler.getStackZSize() - 1;
}
thread.setZmin(zmin);
thread.setZmax(zmax);
zmin += zPerThread;
new Thread(thread).start();
}
while (true) {
boolean finished = true;
for (int i = threads.size() - 1; i >= 0; i--) {
GenerateFacesThread thread = threads.get(i);
if (!thread.isDone()) {
finished = false;
break;
}
}
try {
Thread.sleep(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if (finished) {
break;
}
}
// Merge result
List<Point> faces = new ArrayList<Point>();
List<Point> normals;
if (withNormals) {
normals = new ArrayList<Point>();
for (GenerateFacesThread thread : threads) {
faces.addAll(thread.getFaces());
normals.addAll(thread.getNormals());
}
}
else {
normals = null;
for (GenerateFacesThread thread : threads) {
faces.addAll(thread.getFaces());
}
}
return new RawFaces(faces, normals);
}