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 merge(BindingGroup<T> bg, Matrix m) {
20 if (bg==this) throw new Error();
21 for(HasBindingGroup hbg : bg.matrices.keySet()) {
22 matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
23 hbg.bindingGroup = this;
25 HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
26 for(HasBindingGroup hbg : bg.matrices.keySet())
30 for(HasBindingGroup hbg : stuff)
31 hbg.bindingGroupChanged(this);
34 public T getMaster() { return master; }
35 public Matrix getMatrix(T t) { return matrices.get(t); }
37 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
39 /** t1 = getMatrix(t1, t2) * t2 */
40 public Matrix getMatrix(T t1, T t2) {
41 // t1 = getMatrix(t1) * master
42 // getMatrix(t2)^-1 * t2 = master
43 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
44 return getMatrix(t1).times(getMatrix(t2).inverse());
47 public void unbind(T trem) {
49 matrices.remove(trem);
52 if (matrices.size()==1) {
54 matrices.remove(trem);
57 Iterator<T> it = iterator();
58 T newmaster = it.next();
59 if (newmaster==trem) newmaster = it.next();
60 if (newmaster==trem) throw new Error();
61 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
62 for(T t : matrices.keySet()) {
63 if (t==trem) continue;
64 newmatrices.put(t, getMatrix(t, newmaster));
67 matrices = newmatrices;
70 public boolean contains(HasBindingGroup t) {
71 return matrices.get((T)t) != null;