nglviewer / ngl

WebGL protein viewer
http://nglviewer.org/ngl/
MIT License
669 stars 170 forks source link

Retrieving transformed coordinates of structures shown in ngl viewer #973

Open passionate-code opened 1 year ago

passionate-code commented 1 year ago

I believe there is a similar issue (#794) like mine but in my case the coordinates transformed through .setTransform() method is not parsed by the .PdbWriter().

Here is my sample code:

stage.loadFile("path").then( function( component ){
  var m = new NGL.Matrix4().fromArray([1.0,1.0,1.0,0.5]).transpose();
  var component = o.setTransform(m);
  var pdb_writer = new NGL.PdbWriter(component.structure);
  var data = pdb_writer.getString();
  console.log(data);
}

However the coordinates that I have retrieved are untransformed coordinated as it is similar like coordinates in the loaded file. Any pointer would be appreciated. Thank you.

fredludlow commented 1 year ago

The atom coordinates in the Structure (component.structure) aren't changed - the component matrix is applied later in the pipeline.

The Structure class does have an updatePosition method, so you could apply your transform (you have to calculate the new coordinates yourself) and then save.

Not tested:


/* Transform by 1.0 each in x, y, and z */
const dx = 1.0
const dy = 1.0
const dz = 1.0
const atomCount = component.structure.atomCount
const newCoords = new Float32Array(atomCount * 3)

component.structure.eachAtom(ap => { 
  newCoords[i ++] = ap.x + dx
  newCoords[i ++] = ap.y + dy
  newCoords[i ++] = ap.z + dz
})
component.structure.updatePosition(newCoords)
const data = new NGL.PDbWriter(component.structure).getString()
passionate-code commented 1 year ago

Is there a method to apply the transformation matrix on the individual coordinates? I believe setTransform() can only be applied on component?