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 for(HasBindingGroup hbg : bg.matrices.keySet())
24 hbg.bindingGroupChanged(this);
29 public T getMaster() { return master; }
30 public Matrix getMatrix(T t) { return matrices.get(t); }
32 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
34 /** t1 = getMatrix(t1, t2) * t2 */
35 public Matrix getMatrix(T t1, T t2) {
36 // t1 = getMatrix(t1) * master
37 // getMatrix(t2)^-1 * t2 = master
38 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
39 return getMatrix(t1).times(getMatrix(t2).inverse());
42 public void unbind(T trem) {
44 matrices.remove(trem);
47 if (matrices.size()==1) {
49 matrices.remove(trem);
52 Iterator<T> it = iterator();
53 T newmaster = it.next();
54 if (newmaster==trem) newmaster = it.next();
55 if (newmaster==trem) throw new Error();
56 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
57 for(T t : matrices.keySet()) {
58 if (t==trem) continue;
59 newmatrices.put(t, getMatrix(t, newmaster));
62 matrices = newmatrices;
65 public boolean contains(HasBindingGroup t) {
66 return matrices.get((T)t) != null;