use of org.twak.utils.streams.InaxPoint3dCollector in project chordatlas by twak.
the class MiniTransform method convertToMini.
public static void convertToMini(Iterable<File> bigObj, File outfile, Matrix4d transform) {
outfile.mkdirs();
// .iterator().next() );
ObjDump src = new ObjDump(bigObj);
src.centerVerts();
src.transform(transform);
long count = src.material2Face.entrySet().stream().mapToInt(x -> x.getValue().size()).sum();
double[] bounds = src.orderVert.stream().collect(new InaxPoint3dCollector());
long targetCount = 5000;
double volume = (bounds[1] - bounds[0]) * (bounds[3] - bounds[2]) * (bounds[5] - bounds[4]);
double edgeLength = Math.pow(volume / (count / targetCount), 0.3333);
int xc = (int) Math.ceil((bounds[1] - bounds[0]) / edgeLength), yc = (int) Math.ceil((bounds[3] - bounds[2]) / edgeLength), zc = (int) Math.ceil((bounds[5] - bounds[4]) / edgeLength);
Set<Face>[][][] faces = new Set[xc][yc][zc];
for (Entry<Material, List<Face>> e : src.material2Face.entrySet()) for (Face f : e.getValue()) {
Tuple3d vt = src.orderVert.get(f.vtIndexes.get(0));
int ix = (int) ((vt.x - bounds[0]) / edgeLength);
int iy = (int) ((vt.y - bounds[2]) / edgeLength);
int iz = (int) ((vt.z - bounds[4]) / edgeLength);
if (faces[ix][iy][iz] == null)
faces[ix][iy][iz] = new HashSet();
faces[ix][iy][iz].add(f);
}
int dir = 0;
MiniTransform mt = new MiniTransform();
for (int x = 0; x < xc; x++) for (int y = 0; y < yc; y++) for (int z = 0; z < zc; z++) {
Set<Face> miniF = faces[x][y][z];
if (miniF == null)
continue;
Matrix4d trans = new Matrix4d();
trans.setIdentity();
trans.setScale(edgeLength / 255);
trans.setTranslation(new Vector3d(x * edgeLength + bounds[0], y * edgeLength + bounds[2], z * edgeLength + bounds[4]));
Matrix4d pack = new Matrix4d(trans);
pack.invert();
ObjDump mini = new ObjDump();
miniF.stream().forEach(f -> mini.addFaceFrom(f, src));
mini.transform(pack);
mini.dump(new File(new File(outfile, "" + dir), OBJ));
mt.index.put(dir, trans);
dir++;
}
try {
new XStream().toXML(mt, new FileWriter(new File(outfile, INDEX)));
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("wrote " + count + " faces to " + dir + " meshes");
}
Aggregations