1 package edu.berkeley.qfat.bind;
2 import edu.berkeley.qfat.geom.*;
3 import javax.media.opengl.*;
6 /** tracks an equivalence class of geometric objects which are related to each other by transformation matrices */
7 public class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
9 private T master = null;
10 private AffineConstraint constraint = new AffineConstraint.All();
11 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
13 public BindingGroup(T master) {
15 matrices.put(master, Matrix.ONE);
18 public int size() { return matrices.size(); }
20 public void merge(BindingGroup<T> bg, Matrix m) {
22 if (m.equalsModuloEpsilon(Matrix.ONE, 0.001f)) return;
23 constraint = constraint.intersect(m.getAffineConstraint(0.001f), 0.001f);
27 for(HasBindingGroup hbg : bg.matrices.keySet()) {
28 matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
29 hbg.bindingGroup = this;
32 // FIXME: what if points do not fall on the merged constraint-line?
33 AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
34 constraint = constraint.intersect(ac, 0.001f);
36 HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
37 for(HasBindingGroup hbg : bg.matrices.keySet())
41 for(HasBindingGroup hbg : stuff)
42 hbg.bindingGroupChanged(this);
45 public Matrix getMatrix(T t) { return matrices.get(t); }
47 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
49 /** t1 = getMatrix(t1, t2) * t2 */
50 public Matrix getMatrix(T t1, T t2) {
51 // t1 = getMatrix(t1) * master
52 // getMatrix(t2)^-1 * t2 = master
53 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
54 return getMatrix(t1).times(getMatrix(t2).inverse());
57 public AffineConstraint getConstraint(T t) {
58 return constraint.multiply(matrices.get(t));
61 public void unbind(T trem) {
63 matrices.remove(trem);
66 if (matrices.size()==1) {
68 matrices.remove(trem);
71 Iterator<T> it = iterator();
72 T newmaster = it.next();
73 if (newmaster==trem) newmaster = it.next();
74 if (newmaster==trem) throw new Error();
75 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
76 for(T t : matrices.keySet()) {
77 if (t==trem) continue;
78 newmatrices.put(t, getMatrix(t, newmaster));
81 matrices = newmatrices;
84 public boolean contains(HasBindingGroup t) {
85 return matrices.get((T)t) != null;