checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / BindingGroup.java
1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
3 import java.util.*;
4
5 public class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
6
7     private T                  master   = null;
8     private HashMap<T,Matrix>  matrices = new HashMap<T,Matrix>();
9
10     public BindingGroup(T master) {
11         this.master = master;
12         matrices.put(master, Matrix.ONE);
13     }
14
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;
20         }
21         for(HasBindingGroup hbg : bg.matrices.keySet())
22             hbg.bindingGroupChanged(this);
23         bg.matrices.clear();
24         bg.master = null;
25     }
26
27     public T getMaster() { return master; }
28     public Matrix getMatrix(T t) { return matrices.get(t); }
29
30     public Iterator<T> iterator() { return matrices.keySet().iterator(); }
31
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());
38     }
39
40     public void unbind(T trem) {
41         if (trem != master) {
42             matrices.remove(trem);
43             return;
44         }
45         if (matrices.size()==1) {
46             master = null;
47             matrices.remove(trem);
48             return;
49         }
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));
58         }
59         master = newmaster;
60         matrices = newmatrices;
61     }
62
63     public boolean contains(HasBindingGroup t) {
64         return matrices.get((T)t) != null;
65     }
66 }