Search in sources :

Example 1 with Tuple3d

use of javax.vecmath.Tuple3d in project chordatlas by twak.

the class MiniGen method clip.

public void clip(Loop<Point3d> in, File objLocation) {
    ObjDump obj = new ObjDump();
    double[] bounds = Loopz.minMaxXZ(in);
    List<LinearForm3D> halfPlanes = new ArrayList();
    File writeFolder = objLocation.getParentFile();
    for (Pair<Point3d, Point3d> p : in.pairs()) {
        Vector3d norm = new Vector3d(p.second());
        norm.sub(p.first());
        norm = new Vector3d(-norm.z, 0, norm.x);
        norm.normalize();
        halfPlanes.add(new LinearForm3D(norm, p.first()));
    }
    Map<File, File> copied = new HashMap<>();
    int nameCount = 0;
    double minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
    for (Map.Entry<Integer, Matrix4d> e : trans.index.entrySet()) {
        if (!inBounds(e.getValue(), Collections.singletonList(bounds)))
            continue;
        else {
            Matrix4d m = new Matrix4d();
            m.mul(Jme3z.fromMatrix(trans.offset), e.getValue());
            File readFolder = new File(Tweed.toWorkspace(root), e.getKey() + "");
            ObjDump or = new ObjDump(new File(readFolder, "model.obj"));
            or.computeMissingNormals();
            for (ObjDump.Material mat : or.material2Face.keySet()) {
                f: for (Face f : or.material2Face.get(mat)) {
                    for (int j = 0; j < f.vtIndexes.size(); j++) {
                        Point3d pt = new Point3d(or.orderVert.get(f.vtIndexes.get(j)));
                        m.transform(pt);
                        if (pt.x > bounds[0] && pt.x < bounds[1] && pt.z > bounds[2] && pt.z < bounds[3])
                            if (inside(pt, halfPlanes)) {
                                if (IMPORT_TEXTURES && !((obj.currentMaterial != null && obj.currentMaterial.equals(mat)) || (obj.currentMaterial == null && mat == null))) {
                                    File source = new File(readFolder, mat.filename);
                                    ObjDump.Material newMat;
                                    if (copied.containsKey(source)) {
                                        newMat = new ObjDump.Material(mat);
                                        newMat.filename = copied.get(source).getName();
                                    } else {
                                        newMat = makeUnique(mat, writeFolder);
                                        File destFile = new File(writeFolder, newMat.filename);
                                        copied.put(source, destFile);
                                        try {
                                            Files.copy(source.toPath(), new FileOutputStream(destFile));
                                        } catch (IOException e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                    newMat.diffuse = new double[] { 0, 0, 0 };
                                    newMat.ambient = new double[] { 1, 1, 1 };
                                    newMat.specular = new double[] { 0, 0, 0 };
                                    newMat.name = "mat_" + (nameCount++);
                                    obj.setCurrentMaterial(newMat);
                                }
                                List<Point3d> fVerts = new ArrayList<>(3), fNorms = new ArrayList<>(3);
                                List<Point2d> fUVs = new ArrayList<>(2);
                                for (int i = 0; i < f.vtIndexes.size(); i++) {
                                    Point3d vts = new Point3d(or.orderVert.get(f.vtIndexes.get(i)));
                                    Point3d ns = new Point3d(or.orderNorm.get(f.normIndexes.get(i)));
                                    ns.add(vts);
                                    m.transform(vts);
                                    m.transform(ns);
                                    ns.sub(vts);
                                    minY = Math.min(vts.y, minY);
                                    maxY = Math.max(vts.y, maxY);
                                    fVerts.add(vts);
                                    fNorms.add(ns);
                                    fUVs.add(new Point2d(or.orderUV.get(f.uvIndexes.get(i))));
                                }
                                obj.addFace(fVerts, fNorms, fUVs);
                                continue f;
                            }
                    }
                }
            }
        }
    }
    for (Tuple3d t : obj.orderVert) t.y -= (maxY - minY) * 0.03 + minY;
    obj.dump(objLocation);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ObjDump(org.twak.utils.geom.ObjDump) Point3d(javax.vecmath.Point3d) Face(org.twak.utils.geom.ObjDump.Face) IOException(java.io.IOException) LinearForm3D(org.twak.utils.geom.LinearForm3D) Matrix4d(javax.vecmath.Matrix4d) Vector3d(javax.vecmath.Vector3d) Point2d(javax.vecmath.Point2d) Tuple3d(javax.vecmath.Tuple3d) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Tuple3d

use of javax.vecmath.Tuple3d 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

File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Matrix4d (javax.vecmath.Matrix4d)2 Tuple3d (javax.vecmath.Tuple3d)2 Vector3d (javax.vecmath.Vector3d)2 ObjDump (org.twak.utils.geom.ObjDump)2 Face (org.twak.utils.geom.ObjDump.Face)2 Matrix4f (com.jme3.math.Matrix4f)1 XStream (com.thoughtworks.xstream.XStream)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Point2d (javax.vecmath.Point2d)1 Point3d (javax.vecmath.Point3d)1