1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
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> {
8 private T master = null;
9 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
11 public BindingGroup(T master) {
13 matrices.put(master, Matrix.ONE);
16 public int size() { return matrices.size(); }
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;
25 Matrix v = getMatrix(bg.master, master);
27 HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
28 for(HasBindingGroup hbg : bg.matrices.keySet())
32 for(HasBindingGroup hbg : stuff)
33 hbg.bindingGroupChanged(this);
36 public Matrix getMatrix(T t) { return matrices.get(t); }
38 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
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());
48 public void unbind(T trem) {
50 matrices.remove(trem);
53 if (matrices.size()==1) {
55 matrices.remove(trem);
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));
68 matrices = newmatrices;
71 public boolean contains(HasBindingGroup t) {
72 return matrices.get((T)t) != null;