Skip to content

Instantly share code, notes, and snippets.

@axiomsofchoice
Created January 17, 2011 00:41
Show Gist options
  • Save axiomsofchoice/782314 to your computer and use it in GitHub Desktop.
Save axiomsofchoice/782314 to your computer and use it in GitHub Desktop.
Hacked Kinect Version of Jmol
package org.openscience.jmol.app;
import java.awt.Point;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JFrame;
import javax.vecmath.Vector3f;
import org.openscience.jmol.app.jmolpanel.*;
import com.illposed.osc.OSCListener;
import com.illposed.osc.OSCMessage;
import com.illposed.osc.OSCPort;
import com.illposed.osc.OSCPortIn;
public class Jmol extends JmolPanel {
public Jmol(JmolApp jmolApp, Splash splash, JFrame frame, Jmol parent, int startupWidth,
int startupHeight, String commandOptions, Point loc) {
super(jmolApp, splash, frame, parent, startupWidth, startupHeight, commandOptions, loc);
}
public static void main(final String[] args) throws Exception {
final JmolApp jmolApp = new JmolApp(args);
startJmol(jmolApp);
jmolApp.viewer.evalString("load http://scripts.iucr.org/cgi-bin/sendcif?hb5777sup1");
// final TransformManager transformManager = ((Viewer)jmolApp.viewer).transformManager;
//
//
OSCPortIn receiver = new OSCPortIn(OSCPort.defaultSCOSCPort());
OSCListener listener = new OSCListener() {
final Lock lock = new ReentrantLock();
// Starting values for user 1
float lx, ly, lz;
// Assume no initial rotation from user 2
// The angle is in radians
float l_omega = 0.0f ;
public void acceptMessage(java.util.Date time, OSCMessage message) {
if (lock.tryLock()) {
try {
Object[] args = message.getArguments();
// int user1 = (Integer)args[0];
// float user1_x0 = (Float)args[1];
// float user1_y0 = (Float)args[2];
// float user1_z0 = (Float)args[3];
// float user1_x1 = (Float)args[4];
// float user1_y1 = (Float)args[5];
// float user1_z1 = (Float)args[6];
int user2 = (Integer)args[7];
float user2_x0 = (Float)args[8];
float user2_y0 = (Float)args[9];
float user2_z0 = (Float)args[10];
float user2_x1 = (Float)args[11];
float user2_y1 = (Float)args[12];
float user2_z1 = (Float)args[13];
// Compute a pure scalar rotation (for the bond angle) relative to
// a unit vector in the x-axis
Vector3f v = new Vector3f(user2_x1-user2_x0, user2_y1-user2_y0, user2_z1-user2_z0);
// Get angle in radians
float ax = v.angle(new Vector3f(1f, 0f, 0f));
// Ensure positive angles only
if (v.y < 0) {
ax = -ax;
}
float d_omega = l_omega - ax ;
int inc_amount = (int)((360.0f * d_omega)/(2.0 * Math.PI)) ;
// Convert angle to degrees
jmolApp.viewer.evalString("rotate branch (C6) (C7) " + inc_amount );
l_omega += d_omega ;
// Vector3f v = new Vector3f(x1-x0, y1-y0, z1-z0);
// float l = Math.abs(v.length());
// if (l < 0.12) {
// transformManager.zoomToPercent(0.95f*transformManager.getZoomPercentFloat());
// } else if (l > 0.4) {
// transformManager.zoomToPercent(1.05f*transformManager.getZoomPercentFloat());
// }
//
// if (l > 0.05) {
//
// float ax = v.angle(new Vector3f(1f, 0f, 0f));
// if (v.y < 0) {
// ax = -ax;
// }
//
// float ay = v.angle(new Vector3f(0f, 1f, 0f));
// float az = v.angle(new Vector3f(0f, 0f, 1f));
//
// float dx = lx-ax;
// float dy = ly-ay;
// float dz = lz-az;
//
// lx = ax;
// ly = ay;
// lz = az;
//
// Matrix3f mx = new Matrix3f();
// mx.setIdentity();
//// mx.rotX(dx);
//
// Matrix3f my = new Matrix3f();
// my.setIdentity();
//// my.rotY(dy);
//
// Matrix3f mz = new Matrix3f();
// mz.setIdentity();
//// mz.rotY(dz);
//
// Matrix3f mat = new Matrix3f();
// mat.setIdentity();
// mat.mul(mx);
// mat.mul(my);
// mat.mul(mz);
//
// transformManager.applyRotation(mat, false, null);
//
// jmolApp.viewer.refresh(1, "");
} finally {
lock.unlock();
}
}
}
};
receiver.addListener("/pmrhack", listener);
receiver.startListening();
}
public static Jmol getJmol(JFrame baseframe,
int width, int height, String commandOptions) {
JmolApp jmolApp = new JmolApp(new String[] {});
jmolApp.startupHeight = height;
jmolApp.startupWidth = width;
jmolApp.commandOptions = commandOptions;
return getJmol(jmolApp, baseframe);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment