1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
5 public class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
7 private T master = null;
8 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
10 public BindingGroup(T master) {
12 matrices.put(master, Matrix.ONE);
15 public int size() { return matrices.size(); }
17 public void merge(BindingGroup<T> bg, Matrix m) {
18 if (bg==this) throw new Error();
19 for(HasBindingGroup hbg : bg.matrices.keySet()) {
20 matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
21 hbg.bindingGroup = this;
23 HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
24 for(HasBindingGroup hbg : bg.matrices.keySet())
28 for(HasBindingGroup hbg : stuff)
29 hbg.bindingGroupChanged(this);
32 public T getMaster() { return master; }
33 public Matrix getMatrix(T t) { return matrices.get(t); }
35 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
37 /** t1 = getMatrix(t1, t2) * t2 */
38 public Matrix getMatrix(T t1, T t2) {
39 // t1 = getMatrix(t1) * master
40 // getMatrix(t2)^-1 * t2 = master
41 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
42 return getMatrix(t1).times(getMatrix(t2).inverse());
45 public void unbind(T trem) {
47 matrices.remove(trem);
50 if (matrices.size()==1) {
52 matrices.remove(trem);
55 Iterator<T> it = iterator();
56 T newmaster = it.next();
57 if (newmaster==trem) newmaster = it.next();
58 if (newmaster==trem) throw new Error();
59 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
60 for(T t : matrices.keySet()) {
61 if (t==trem) continue;
62 newmatrices.put(t, getMatrix(t, newmaster));
65 matrices = newmatrices;
68 public boolean contains(HasBindingGroup t) {
69 return matrices.get((T)t) != null;