Search in sources :

Example 1 with Material

use of org.twak.utils.geom.ObjDump.Material 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");
}
Also used : XStream(com.thoughtworks.xstream.XStream) InaxPoint3dCollector(org.twak.utils.streams.InaxPoint3dCollector) FileWriter(java.io.FileWriter) Matrix4d(javax.vecmath.Matrix4d) Face(org.twak.utils.geom.ObjDump.Face) Auto(org.twak.utils.ui.auto.Auto) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Vector3d(javax.vecmath.Vector3d) Matrix4f(com.jme3.math.Matrix4f) File(java.io.File) Tuple3d(javax.vecmath.Tuple3d) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) Entry(java.util.Map.Entry) ObjDump(org.twak.utils.geom.ObjDump) Material(org.twak.utils.geom.ObjDump.Material) Set(java.util.Set) HashSet(java.util.HashSet) XStream(com.thoughtworks.xstream.XStream) FileWriter(java.io.FileWriter) Material(org.twak.utils.geom.ObjDump.Material) IOException(java.io.IOException) Matrix4d(javax.vecmath.Matrix4d) Vector3d(javax.vecmath.Vector3d) ObjDump(org.twak.utils.geom.ObjDump) Tuple3d(javax.vecmath.Tuple3d) ArrayList(java.util.ArrayList) List(java.util.List) InaxPoint3dCollector(org.twak.utils.streams.InaxPoint3dCollector) Face(org.twak.utils.geom.ObjDump.Face) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

Matrix4f (com.jme3.math.Matrix4f)1 XStream (com.thoughtworks.xstream.XStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Matrix4d (javax.vecmath.Matrix4d)1 Tuple3d (javax.vecmath.Tuple3d)1 Vector3d (javax.vecmath.Vector3d)1 ObjDump (org.twak.utils.geom.ObjDump)1 Face (org.twak.utils.geom.ObjDump.Face)1 Material (org.twak.utils.geom.ObjDump.Material)1 InaxPoint3dCollector (org.twak.utils.streams.InaxPoint3dCollector)1 Auto (org.twak.utils.ui.auto.Auto)1