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();
}
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()));
}
}
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;
}
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);
}
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();
}
Aggregations