Search in sources :

Example 1 with Matrix4d

use of javax.vecmath.Matrix4d in project mmtf-spark by sbl-sdsc.

the class StructureToBioassembly2 method call.

@Override
public Iterator<Tuple2<String, StructureDataInterface>> call(Tuple2<String, StructureDataInterface> t) throws Exception {
    StructureDataInterface structure = t._2;
    // Map<Integer, Integer> atomMap = new HashMap<>();
    List<Tuple2<String, StructureDataInterface>> resList = new ArrayList<>();
    // for each of them, create one structure.
    for (int i = 0; i < structure.getNumBioassemblies(); i++) {
        int[] chainsPerModel = new int[structure.getNumModels()];
        // initiate the bioassembly structure.
        AdapterToStructureData bioAssembly = new AdapterToStructureData();
        // set the structureID.
        String structureId = structure.getStructureId() + "-BioAssembly" + structure.getBioassemblyName(i);
        int totAtoms = 0, totBonds = 0, totGroups = 0, totChains = 0, totModels = 0;
        int numTrans = structure.getNumTransInBioassembly(i);
        totModels = structure.getNumModels();
        int[][] bioChainList = new int[numTrans][];
        double[][] transMatrix = new double[numTrans][];
        // calculate the total data we will use to initialize the structure.
        for (int ii = 0; ii < numTrans; ii++) {
            bioChainList[ii] = structure.getChainIndexListForTransform(i, ii);
            transMatrix[ii] = structure.getMatrixForTransform(i, ii);
            for (int j = 0; j < totModels; j++) {
                totChains += bioChainList[ii].length;
                chainsPerModel[j] += bioChainList[ii].length;
                // System.out.println(bioChainList[ii].length + " "  + Arrays.toString(bioChainList[ii]));
                for (int k = 0, groupCounter = 0; k < structure.getChainsPerModel()[j]; k++) {
                    boolean adding = false;
                    for (int currChain : bioChainList[ii]) {
                        if (currChain == k)
                            adding = true;
                    }
                    if (adding) {
                        // System.out.println("adding groups");
                        totGroups += structure.getGroupsPerChain()[k];
                    }
                    for (int h = 0; h < structure.getGroupsPerChain()[k]; h++, groupCounter++) {
                        if (adding) {
                            int groupIndex = structure.getGroupTypeIndices()[groupCounter];
                            totAtoms += structure.getNumAtomsInGroup(groupIndex);
                            totBonds += structure.getGroupBondOrders(groupIndex).length;
                        }
                    }
                }
            }
        }
        // init
        // System.out.println("Initializing the structure with\n"
        // + " totModel = " + totModels + ", totChains = " + totChains + ", totGroups = " + totGroups + ", totAtoms = "
        // + totAtoms + ", totBonds = " + totBonds + ", name : " + structureId);
        bioAssembly.initStructure(totBonds, totAtoms, totGroups, totChains, totModels, structureId);
        DecoderUtils.addXtalographicInfo(structure, bioAssembly);
        DecoderUtils.addHeaderInfo(structure, bioAssembly);
        /*
			 * Now we have bioChainList and transMatrix.
			 * bioChainList[i] is the ith trans' list of chains it has.  
			 * transMatrix[i] is the matrix that is going to be applied on those chains.
			 */
        // initialize the indices.
        int modelIndex = 0;
        int chainIndex = 0;
        int groupIndex = 0;
        int atomIndex = 0;
        int chainCounter = 0;
        // loop through models
        for (int ii = 0; ii < structure.getNumModels(); ii++) {
            // precalculate indices
            // this number is not correct if BA has fewer chains than the AU
            // int numChainsPerModel = structure.getChainsPerModel()[modelIndex] * numTrans;
            int numChainsPerModel = chainsPerModel[ii];
            System.out.println("numChainsPerModel: " + numChainsPerModel);
            bioAssembly.setModelInfo(modelIndex, numChainsPerModel);
            int[] chainToEntityIndex = getChainToEntityIndex(structure);
            // loop through chains
            for (int j = 0; j < structure.getChainsPerModel()[modelIndex]; j++) {
                // loop through each trans
                int currGroupIndex = groupIndex;
                int currAtomIndex = atomIndex;
                for (int k = 0; k < numTrans; k++) {
                    // get the currChainList that needs to be added
                    int[] currChainList = bioChainList[k];
                    double[] currMatrix = transMatrix[k];
                    boolean addThisChain = false;
                    for (int currChain : currChainList) {
                        if (currChain == j)
                            addThisChain = true;
                    }
                    groupIndex = currGroupIndex;
                    atomIndex = currAtomIndex;
                    float[] xCoords = structure.getxCoords();
                    float[] yCoords = structure.getyCoords();
                    float[] zCoords = structure.getzCoords();
                    // float[] floatMatrix = Floats.toArray(Doubles.asList(currMatrix));
                    // Matrix4f m = new Matrix4f(floatMatrix);
                    Matrix4d md = new Matrix4d(currMatrix);
                    if (addThisChain) {
                        int entityToChainIndex = chainToEntityIndex[chainIndex];
                        // System.out.println("adding chain : " + chainIndex);
                        // TODO
                        // not sure
                        bioAssembly.setEntityInfo(new int[] { chainCounter }, structure.getEntitySequence(entityToChainIndex), structure.getEntityDescription(entityToChainIndex), structure.getEntityType(entityToChainIndex));
                        // TODO create unique chain ids
                        bioAssembly.setChainInfo(structure.getChainIds()[chainIndex] + (k + 1), structure.getChainNames()[chainIndex] + (k + 1), // bioAssembly.setChainInfo(structure.getChainIds()[chainIndex], structure.getChainNames()[chainIndex],
                        structure.getGroupsPerChain()[chainIndex]);
                        chainCounter++;
                    }
                    // loop through the groups in the chain
                    for (int jj = 0; jj < structure.getGroupsPerChain()[chainIndex]; jj++) {
                        int currgroup = structure.getGroupTypeIndices()[groupIndex];
                        if (addThisChain) {
                            bioAssembly.setGroupInfo(structure.getGroupName(currgroup), structure.getGroupIds()[groupIndex], structure.getInsCodes()[groupIndex], structure.getGroupChemCompType(currgroup), structure.getNumAtomsInGroup(currgroup), structure.getGroupBondOrders(currgroup).length, structure.getGroupSingleLetterCode(currgroup), structure.getGroupSequenceIndices()[groupIndex], structure.getSecStructList()[groupIndex]);
                        }
                        for (int kk = 0; kk < structure.getNumAtomsInGroup(currgroup); kk++) {
                            // System.out.println("currgroup : " + currgroup + " curratom : " + kk);
                            if (addThisChain) {
                                Point3f p1 = new Point3f(xCoords[atomIndex], yCoords[atomIndex], zCoords[atomIndex]);
                                // m.transform(p1);
                                md.transform(p1);
                                // System.out.println(kk + " " + currgroup);
                                bioAssembly.setAtomInfo(structure.getGroupAtomNames(currgroup)[kk], structure.getAtomIds()[atomIndex], structure.getAltLocIds()[atomIndex], p1.x, p1.y, p1.z, structure.getOccupancies()[atomIndex], structure.getbFactors()[atomIndex], structure.getGroupElementNames(currgroup)[kk], structure.getGroupAtomCharges(currgroup)[kk]);
                            }
                            // inc the atomIndex
                            atomIndex++;
                        }
                        if (addThisChain) {
                            for (int l = 0; l < structure.getGroupBondOrders(currgroup).length; l++) {
                                // System.out.println(structure.getGroupBondOrders(currgroup).length + " " + l);
                                int bondIndOne = structure.getGroupBondIndices(currgroup)[l * 2];
                                int bondIndTwo = structure.getGroupBondIndices(currgroup)[l * 2 + 1];
                                int bondOrder = structure.getGroupBondOrders(currgroup)[l];
                                bioAssembly.setGroupBond(bondIndOne, bondIndTwo, bondOrder);
                            }
                        }
                        // inc the groupIndex
                        groupIndex++;
                    }
                    if (addThisChain) {
                    // Add inter-group bond info
                    // for(int l = 0;  l < structure.getInterGroupBondOrders().length; l++){
                    // int bondIndOne = structure.getInterGroupBondIndices()[l*2];
                    // int bondIndTwo = structure.getInterGroupBondIndices()[l*2+1];
                    // int bondOrder = structure.getInterGroupBondOrders()[l];
                    // Integer indexOne = atomMap.get(bondIndOne);
                    // if (indexOne != null) {
                    // Integer indexTwo = atomMap.get(bondIndTwo);
                    // if (indexTwo != null) {
                    // bioAssembly.setInterGroupBond(indexOne, indexTwo, bondOrder);
                    // }
                    // }
                    }
                }
                // inc the chainIndex
                chainIndex++;
            }
            // inc the modelIndex
            modelIndex++;
        }
        bioAssembly.finalizeStructure();
        resList.add(new Tuple2<String, StructureDataInterface>(structureId, bioAssembly));
    }
    return resList.iterator();
}
Also used : ArrayList(java.util.ArrayList) StructureDataInterface(org.rcsb.mmtf.api.StructureDataInterface) Matrix4d(javax.vecmath.Matrix4d) AdapterToStructureData(org.rcsb.mmtf.encoder.AdapterToStructureData) Point3f(javax.vecmath.Point3f) Tuple2(scala.Tuple2)

Example 2 with Matrix4d

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

the class MiniGen method kill.

@Override
public void kill() {
    for (Map.Entry<Integer, Matrix4d> e : trans.index.entrySet()) {
        if (!inBounds(e.getValue(), bounds))
            continue;
        File absRoot = Tweed.toWorkspace(MiniGen.this.root);
        System.out.println("loading mesh " + e.getKey() + " from " + absRoot);
        File f = new File(absRoot, e.getKey() + "/model.obj");
        tweed.getAssetManager().deleteFromCache(new ModelKey(tweed.makeWorkspaceRelative(f).toString()));
    }
}
Also used : Matrix4d(javax.vecmath.Matrix4d) ModelKey(com.jme3.asset.ModelKey) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File)

Example 3 with Matrix4d

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

the class MiniGen method inBounds.

private boolean inBounds(Matrix4d mini, List<double[]> bounds) {
    // mini matrix is in mini-mesh format: a translation from a 255^3 cube in the first quadrant
    // trans.offset is a transform from that space, into jme rendered space (cartesian in meters, around the origin)
    Matrix4d m = new Matrix4d();
    m.mul(Jme3z.fromMatrix(trans.offset), mini);
    for (Point2d p : Arrays.stream(cubeCorners).map(c -> {
        Point3d tmp = new Point3d();
        m.transform(c, tmp);
        return new Point2d(tmp.x, tmp.z);
    }).collect(Collectors.toList())) {
        for (double[] bound : bounds) {
            if (bound[0] < p.x && bound[1] > p.x && bound[2] < p.y && bound[3] > p.y)
                return true;
        }
    }
    return false;
}
Also used : Matrix4d(javax.vecmath.Matrix4d) XStream(com.thoughtworks.xstream.XStream) Arrays(java.util.Arrays) Matrix4d(javax.vecmath.Matrix4d) Face(org.twak.utils.geom.ObjDump.Face) Mode(com.jme3.scene.Mesh.Mode) Tuple3d(javax.vecmath.Tuple3d) MiniTransform(org.twak.readTrace.MiniTransform) Loop(org.twak.utils.collections.Loop) Node(com.jme3.scene.Node) AlignTool(org.twak.tweed.tools.AlignTool) Map(java.util.Map) Material(com.jme3.material.Material) ChangeListener(javax.swing.event.ChangeListener) Point3d(javax.vecmath.Point3d) ChangeEvent(javax.swing.event.ChangeEvent) ListDownLayout(org.twak.utils.ui.ListDownLayout) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) JSlider(javax.swing.JSlider) JCheckBox(javax.swing.JCheckBox) Mesh(com.jme3.scene.Mesh) JPanel(javax.swing.JPanel) Geometry(com.jme3.scene.Geometry) ActionListener(java.awt.event.ActionListener) LinearForm3D(org.twak.utils.geom.LinearForm3D) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Pair(org.twak.utils.Pair) Vector3d(javax.vecmath.Vector3d) Filez(org.twak.utils.Filez) Tweed(org.twak.tweed.Tweed) ArrayList(java.util.ArrayList) Spatial(com.jme3.scene.Spatial) JComponent(javax.swing.JComponent) JButton(javax.swing.JButton) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BlendMode(com.jme3.material.RenderState.BlendMode) ActionEvent(java.awt.event.ActionEvent) File(java.io.File) Loopz(org.twak.utils.collections.Loopz) Point2d(javax.vecmath.Point2d) Jme3z(org.twak.siteplan.jme.Jme3z) EventMoveHandle(org.twak.tweed.EventMoveHandle) HandleMe(org.twak.tweed.handles.HandleMe) ObjDump(org.twak.utils.geom.ObjDump) ModelKey(com.jme3.asset.ModelKey) ColorRGBA(com.jme3.math.ColorRGBA) Collections(java.util.Collections) Transform(com.jme3.math.Transform) Point2d(javax.vecmath.Point2d) Point3d(javax.vecmath.Point3d)

Example 4 with Matrix4d

use of javax.vecmath.Matrix4d 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 5 with Matrix4d

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

the class MiniGen method calculate.

@Override
public void calculate() {
    for (Spatial s : gNode.getChildren()) s.removeFromParent();
    Material mat;
    if (transparency == 1)
        mat = new Material(tweed.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
    else {
        mat = new Material(tweed.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        mat.setColor("Color", new ColorRGBA(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, transparency));
    }
    for (Map.Entry<Integer, Matrix4d> e : trans.index.entrySet()) {
        if (!inBounds(e.getValue(), bounds))
            continue;
        File absRoot = Tweed.toWorkspace(MiniGen.this.root);
        System.out.println("loading mesh " + e.getKey() + " from " + absRoot);
        File f = new File(absRoot, e.getKey() + "/model.obj");
        Spatial mesh = tweed.getAssetManager().loadModel(tweed.makeWorkspaceRelative(f).toString());
        mesh.setLocalTransform(Jme3z.toJmeTransform(e.getValue()));
        Mode mode = renderLines ? Mesh.Mode.Lines : Mesh.Mode.Triangles;
        List<Spatial> ls;
        if (mesh instanceof Node)
            ls = ((Node) mesh).getChildren();
        else
            ls = Collections.singletonList(mesh);
        for (Spatial g : ls) {
            Geometry geometry = (Geometry) g;
            Mesh m = geometry.getMesh();
            m.setMode(mode);
            if (geometry.getMaterial().getName() == null)
                mesh.setMaterial(mat);
            gNode.attachChild(geometry);
            mesh.setUserData(Gen.class.getSimpleName(), new Object[] { this });
        }
    }
    Transform t = new Transform();
    t.fromTransformMatrix(trans.offset);
    gNode.setLocalTransform(t);
    gNode.setUserData(Gen.class.getSimpleName(), new Object[] { this });
    gNode.setUserData(HandleMe.class.getSimpleName(), true);
    gNode.setUserData(EventMoveHandle.class.getSimpleName(), new Object[] { new EventMoveHandle() {

        @Override
        public void posChanged() {
            MiniGen.this.save();
        }
    } });
    gNode.updateGeometricState();
    super.calculate();
}
Also used : EventMoveHandle(org.twak.tweed.EventMoveHandle) Mode(com.jme3.scene.Mesh.Mode) BlendMode(com.jme3.material.RenderState.BlendMode) Node(com.jme3.scene.Node) Mesh(com.jme3.scene.Mesh) Material(com.jme3.material.Material) Matrix4d(javax.vecmath.Matrix4d) Geometry(com.jme3.scene.Geometry) ColorRGBA(com.jme3.math.ColorRGBA) Spatial(com.jme3.scene.Spatial) HandleMe(org.twak.tweed.handles.HandleMe) MiniTransform(org.twak.readTrace.MiniTransform) Transform(com.jme3.math.Transform) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File)

Aggregations

Matrix4d (javax.vecmath.Matrix4d)18 Vector3d (javax.vecmath.Vector3d)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Point3d (javax.vecmath.Point3d)6 Map (java.util.Map)5 Point2d (javax.vecmath.Point2d)5 IOException (java.io.IOException)4 ObjDump (org.twak.utils.geom.ObjDump)4 Transform (com.jme3.math.Transform)3 XStream (com.thoughtworks.xstream.XStream)3 FileOutputStream (java.io.FileOutputStream)3 List (java.util.List)3 Tuple3d (javax.vecmath.Tuple3d)3 Loop (org.twak.utils.collections.Loop)3 LinearForm3D (org.twak.utils.geom.LinearForm3D)3 Face (org.twak.utils.geom.ObjDump.Face)3 ModelKey (com.jme3.asset.ModelKey)2 Material (com.jme3.material.Material)2