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 void merge(BindingGroup<T> bg, Matrix m) {
16 if (bg==this) throw new Error();
17 for(HasBindingGroup hbg : bg.matrices.keySet()) {
18 matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
19 hbg.bindingGroup = this;
21 for(HasBindingGroup hbg : bg.matrices.keySet())
22 hbg.bindingGroupChanged(this);
27 public T getMaster() { return master; }
28 public Matrix getMatrix(T t) { return matrices.get(t); }
30 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
32 /** t1 = getMatrix(t1, t2) * t2 */
33 public Matrix getMatrix(T t1, T t2) {
34 // t1 = getMatrix(t1) * master
35 // getMatrix(t2)^-1 * t2 = master
36 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
37 return getMatrix(t1).times(getMatrix(t2).inverse());
40 public void unbind(T trem) {
42 matrices.remove(trem);
45 if (matrices.size()==1) {
47 matrices.remove(trem);
50 Iterator<T> it = iterator();
51 T newmaster = it.next();
52 if (newmaster==trem) newmaster = it.next();
53 if (newmaster==trem) throw new Error();
54 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
55 for(T t : matrices.keySet()) {
56 if (t==trem) continue;
57 newmatrices.put(t, getMatrix(t, newmaster));
60 matrices = newmatrices;
63 public boolean contains(HasBindingGroup t) {
64 return matrices.get((T)t) != null;