1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
5 public class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
7 public Matrix krank = Matrix.ONE;
9 private T master = null;
10 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
12 public BindingGroup(T master) {
14 matrices.put(master, Matrix.ONE);
17 public int size() { return matrices.size(); }
19 public void setKrank(Matrix k) { krank = krank.times(k); }
20 public void merge(BindingGroup<T> bg, Matrix m) {
21 if (bg==this) throw new Error();
22 for(HasBindingGroup hbg : bg.matrices.keySet()) {
23 matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
24 hbg.bindingGroup = this;
27 Matrix v = getMatrix(bg.master, master);
28 //krank = krank.times(v.inverse().times(bg.krank).times(v));
30 HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
31 for(HasBindingGroup hbg : bg.matrices.keySet())
35 for(HasBindingGroup hbg : stuff)
36 hbg.bindingGroupChanged(this);
39 public T getMaster() { return master; }
40 public Matrix getMatrix(T t) { return matrices.get(t); }
42 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
44 /** t1 = getMatrix(t1, t2) * t2 */
45 public Matrix getMatrix(T t1, T t2) {
46 // t1 = getMatrix(t1) * master
47 // getMatrix(t2)^-1 * t2 = master
48 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
49 return getMatrix(t1).times(getMatrix(t2).inverse());
52 public void unbind(T trem) {
54 matrices.remove(trem);
57 if (matrices.size()==1) {
59 matrices.remove(trem);
62 Iterator<T> it = iterator();
63 T newmaster = it.next();
64 if (newmaster==trem) newmaster = it.next();
65 if (newmaster==trem) throw new Error();
66 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
67 for(T t : matrices.keySet()) {
68 if (t==trem) continue;
69 newmatrices.put(t, getMatrix(t, newmaster));
72 matrices = newmatrices;
75 public boolean contains(HasBindingGroup t) {
76 return matrices.get((T)t) != null;