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 AffineConstraint constraint = new AffineConstraint.All();
10 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
12 public BindingGroup(T master) {
14 matrices.put(master, Matrix.ONE);
17 public int size() { return matrices.size(); }
19 public void merge(BindingGroup<T> bg, Matrix m) {
21 if (m.equalsModuloEpsilon(Matrix.ONE, 0.001f)) return;
22 System.err.println(m.getAffineConstraint(0.001f));
26 for(HasBindingGroup hbg : bg.matrices.keySet()) {
27 matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
28 hbg.bindingGroup = this;
31 // FIXME: what if points do not fall on the merged constraint-line?
32 AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
33 constraint = constraint.intersect(ac, 0.001f);
35 HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
36 for(HasBindingGroup hbg : bg.matrices.keySet())
40 for(HasBindingGroup hbg : stuff)
41 hbg.bindingGroupChanged(this);
44 public Matrix getMatrix(T t) { return matrices.get(t); }
46 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
48 /** t1 = getMatrix(t1, t2) * t2 */
49 public Matrix getMatrix(T t1, T t2) {
50 // t1 = getMatrix(t1) * master
51 // getMatrix(t2)^-1 * t2 = master
52 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
53 return getMatrix(t1).times(getMatrix(t2).inverse());
56 public AffineConstraint getConstraint(T t) {
57 return constraint.multiply(matrices.get(t));
60 public void unbind(T trem) {
62 matrices.remove(trem);
65 if (matrices.size()==1) {
67 matrices.remove(trem);
70 Iterator<T> it = iterator();
71 T newmaster = it.next();
72 if (newmaster==trem) newmaster = it.next();
73 if (newmaster==trem) throw new Error();
74 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
75 for(T t : matrices.keySet()) {
76 if (t==trem) continue;
77 newmatrices.put(t, getMatrix(t, newmaster));
80 matrices = newmatrices;
83 public boolean contains(HasBindingGroup t) {
84 return matrices.get((T)t) != null;