1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
import java.awt.*;
import java.awt.event.*;
public class trackball extends MouseAdapter implements MouseMotionListener{
private final float trackballSize;
private int prevX = 0;
private int prevY = 0;
private int startX = 0;
private int startY = 0;
private float[] curQuat = buildQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
private float[] lastQuat = curQuat;
private boolean spin = false;
public trackball(){
trackballSize = 0.8f;
}
/** specify component virtual trackball is in */
public void listen(Component component){
component.addMouseListener(this);
component.addMouseMotionListener(this);
}
/** return rotation Matrix representing current rotation of trackball */
public float[] getRotMatrix() {
float[] rotMat = buildMatrix(curQuat);
if(spin){
curQuat = addQuats(lastQuat, curQuat);
}
return rotMat;
}
// deal with Mouse events
final static int EPS2 = 25; //only spin if mouse moved this far
public void mouseReleased(MouseEvent evt){
int dx = startX - evt.getX();
int dy = startY - evt.getY();
spin = (dx*dx + dy*dy > EPS2) ;
}
public void mousePressed(MouseEvent evt){
startX = prevX = evt.getX();
startY = prevY = evt.getY();
spin = false;
}
public void mouseMoved(MouseEvent evt){
}
public void mouseDragged(MouseEvent evt){
int aWidth = evt.getComponent().getSize().width;
int aHeight = evt.getComponent().getSize().height;
int currX = evt.getX();
int currY = evt.getY();
lastQuat =
buildQuaternion((float) (2.0f*prevX-aWidth)/(float) aWidth,
(float) (aHeight-2.0f*prevY)/(float) aHeight,
(float) (2.0f*currX-aWidth)/(float) aWidth,
(float) (aHeight -2.0f*currY)/(float) aHeight
);
curQuat = addQuats(lastQuat, curQuat);
prevX = currX;
prevY = currY;
}
/*
* Ok, simulate a track-ball. Project the points onto the virtual
* trackball, then figure out the axis of rotation, which is the cross
* product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
* Note: This is a deformed trackball-- is a trackball in the center,
* but is deformed into a hyperbolic sheet of rotation away from the
* center. This particular function was chosen after trying out
* several variations.
*
* It is assumed that the arguments to this routine are in the range
* (-1.0 ... 1.0)
*/
public float[] buildQuaternion(float p1x, float p1y, float p2x, float p2y) {
float[] a = new float[3]; /* Axis of rotation */
float phi; /* how much to rotate about axis */
float[] p1 = new float[3];
float[] p2 = new float[3];
float[] d = new float[3];
float t;
if (p1x == p2x && p1y == p2y) {
/* Zero rotation */
float[] q = {0.0f,0.0f,0.0f,1.0f};
return q;
}
/*
* First, figure out z-coordinates for projection of P1 and P2 to
* deformed sphere
*/
vset(p1,p1x,p1y,projectToSphere(trackballSize,p1x,p1y));
vset(p2,p2x,p2y,projectToSphere(trackballSize,p2x,p2y));
/*
* Now, we want the cross product of P1 and P2
*/
vcross(p2,p1,a);
/*
* Figure out how much to rotate around that axis.
*/
vsub(p1,p2,d);
t = vlength(d) / (2.0f*trackballSize);
/*
* Avoid problems with out-of-control values...
*/
if (t > 1.0) t = 1.0f;
if (t < -1.0) t = -1.0f;
phi = 2.0f * (float)Math.asin(t);
return axisToQuat(a,phi);
}
/** Create a unit quaternion that represents the rotation about axis
by theta */
public float[] axisToQuat(float[] axis, float theta){
float[] q = new float[4];
q[3] = (float)Math.cos(theta/2.0f); //scalar part
vnormal(axis);
vcopy(axis,q);
vscale(q,(float) Math.sin(theta/2.0));
return q;
}
public float[] renormalizeQuat(float[] q){
float len = 0.0f;
for (int i = 0; i < q.length; i++){
len += q[i]*q[i];
}
len = (float)Math.sqrt(len);
float[] ans = new float[q.length];
for (int i = 0; i < q.length; i++){
ans[i] = q[i]/len;
}
return ans;
}
/**
* Given two rotations, e1 and e2, expressed as quaternion rotations,
* figure out the equivalent single rotation and stuff it into dest.
*
* This routine also normalizes the result every RENORMCOUNT times it is
* called, to keep error from creeping in.
*
* NOTE: This routine is written so that q1 or q2 may be the same
* as dest (or each other).
*/
private static final int RENORMCOUNT= 97;
private int count=0;
public float[] addQuats(float[] q1, float[] q2){
float[] ans = new float[4];
ans[3] = q2[3]*q1[3] - q2[0]*q1[0] - q2[1]*q1[1] - q2[2]*q1[2];
ans[0] = q2[3]*q1[0] + q2[0]*q1[3] + q2[1]*q1[2] - q2[2]*q1[1];
ans[1] = q2[3]*q1[1] + q2[1]*q1[3] + q2[2]*q1[0] - q2[0]*q1[2];
ans[2] = q2[3]*q1[2] + q2[2]*q1[3] + q2[0]*q1[1] - q2[1]*q1[0];
if (++count > RENORMCOUNT) {
count = 0;
renormalizeQuat(ans);
}
return ans;
}
/**
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
* if we are away from the center of the sphere.
*/
public float projectToSphere(float r, float x, float y){
float z;
float d = (float)Math.sqrt(x*x + y*y);
if (d < r * 0.70710678118654752440f) { /* Inside sphere */
z = (float)Math.sqrt(r*r - d*d);
} else { /* On hyperbola */
float t = r / 1.41421356237309504880f;
z = t*t / d;
}
return z;
}
/*
* Build a rotation matrix, given a quaternion rotation.
*
*/
public float[] buildMatrix(float q[]) {
float[] m = new float[16];
m[0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
m[1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
m[2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
m[3] = 0.0f;
m[4] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
m[5]= 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
m[6] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
m[7] = 0.0f;
m[8] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
m[9] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
m[10] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
m[11] = 0.0f;
m[12] = 0.0f;
m[13] = 0.0f;
m[14] = 0.0f;
m[15] = 1.0f;
return m;
}
/* our own collectio of 3D vector functions */
public static void vzero(float[] v) {
v[0] = 0.0f;
v[1] = 0.0f;
v[2] = 0.0f;
}
public static void vset(float[] v, float x, float y, float z){
v[0] = x;
v[1] = y;
v[2] = z;
}
public static void vsub(float[] src1, float[] src2, float[] dst){
dst[0] = src1[0] - src2[0];
dst[1] = src1[1] - src2[1];
dst[2] = src1[2] - src2[2];
}
public static void vcopy(float[] v1, float[] v2){
for (int i = 0 ; i < 3 ; i++)
v2[i] = v1[i];
}
public static void vcross(float[] v1, float[] v2, float[] cross) {
float[] temp = new float[3];
temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
vcopy(temp, cross);
}
public static float vlength(float[] v) {
return (float) Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
public static void vscale(float[] v, float div) {
v[0] *= div;
v[1] *= div;
v[2] *= div;
}
public static void vnormal(float[] v) {
vscale(v,1.0f/vlength(v));
}
public static float vdot(float[] v1, float[] v2) {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}
public static void vadd(float[] src1, float[] src2, float[] dst){
dst[0] = src1[0] + src2[0];
dst[1] = src1[1] + src2[1];
dst[2] = src1[2] + src2[2];
}
} |