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 /** tracks an equivalence class of geometric objects which are related to each other by transformation matrices */
6 public class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
7
8     private T                  master   = null;
9     private HashMap<T,Matrix>  matrices = new HashMap<T,Matrix>();
10
11     public BindingGroup(T master) {
12         this.master = master;
13         matrices.put(master, Matrix.ONE);
14     }
15
16     public int size() { return matrices.size(); }
17
18     public void merge(BindingGroup<T> bg, Matrix m) {
19         if (bg==this) throw new Error();
20         for(HasBindingGroup hbg : bg.matrices.keySet()) {
21             matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
22             hbg.bindingGroup = this;
23         }
24
25         Matrix v = getMatrix(bg.master, master);
26
27         HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
28         for(HasBindingGroup hbg : bg.matrices.keySet())
29             stuff.add(hbg);
30         bg.matrices.clear();
31         bg.master = null;
32         for(HasBindingGroup hbg : stuff)
33             hbg.bindingGroupChanged(this);
34     }
35
36     public Matrix getMatrix(T t) { return matrices.get(t); }
37
38     public Iterator<T> iterator() { return matrices.keySet().iterator(); }
39
40     /** t1 = getMatrix(t1, t2) * t2 */
41     public Matrix getMatrix(T t1, T t2) {
42         //                    t1 = getMatrix(t1) * master
43         // getMatrix(t2)^-1 * t2 =                 master
44         //                    t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
45         return getMatrix(t1).times(getMatrix(t2).inverse());
46     }
47
48     public void unbind(T trem) {
49         if (trem != master) {
50             matrices.remove(trem);
51             return;
52         }
53         if (matrices.size()==1) {
54             master = null;
55             matrices.remove(trem);
56             return;
57         }
58         Iterator<T> it = iterator();
59         T newmaster = it.next();
60         if (newmaster==trem) newmaster = it.next();
61         if (newmaster==trem) throw new Error();
62         HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
63         for(T t : matrices.keySet()) {
64             if (t==trem) continue;
65             newmatrices.put(t, getMatrix(t, newmaster));
66         }
67         master = newmaster;
68         matrices = newmatrices;
69     }
70
71     public boolean contains(HasBindingGroup t) {
72         return matrices.get((T)t) != null;
73     }
74 }