1 package edu.berkeley.qfat.bind;
2 import edu.berkeley.qfat.geom.*;
3 import javax.media.opengl.*;
7 * An equivalence class of geometric objects whose positions are
8 * related by affine transformation matrices and are constrained by
9 * an affine constraint.
11 class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
13 private T master = null;
14 private AffineConstraint constraint = new AffineConstraint.All();
15 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
17 public BindingGroup(T master) {
19 matrices.put(master, Matrix.ONE);
22 int size() { return matrices.size(); }
24 /** merge another binding group into this one */
25 void merge(BindingGroup<T> bg, Matrix m, float epsilon) {
27 if (m.equalsModuloEpsilon(Matrix.ONE, epsilon)) return;
28 // FIXME: what if points do not fall on the merged constraint-line?
29 constraint = constraint.intersect(m.getAffineConstraint(epsilon), epsilon);
33 for(HasBindingGroup hbg : bg) {
34 matrices.put((T)hbg, bg.getMatrix((T)hbg).times(m));
35 hbg.bindingGroup = this;
38 // FIXME: what if points do not fall on the merged constraint-line?
39 AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
40 constraint = constraint.intersect(ac, epsilon);
43 for(HasBindingGroup hbg : bg) hbg.bindingGroupChanged();
48 public Matrix getMatrix(T t) { return matrices.get(t); }
50 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
52 /** t1 = getMatrix(t1, t2) * t2 */
53 public Matrix getMatrix(T t1, T t2) {
54 // t1 = getMatrix(t1) * master
55 // getMatrix(t2)^-1 * t2 = master
56 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
57 return getMatrix(t1).times(getMatrix(t2).inverse());
60 public AffineConstraint getAffineConstraint(T t) {
61 return constraint.multiply(matrices.get(t));
66 matrices.remove(trem);
69 if (matrices.size()==1) {
71 matrices.remove(trem);
74 Iterator<T> it = iterator();
75 T newmaster = it.next();
76 if (newmaster==trem) newmaster = it.next();
77 if (newmaster==trem) throw new Error("impossible");
79 // FIXME: is this correct?
80 constraint = constraint.multiply(getMatrix(newmaster, master));
82 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
83 for(T t : matrices.keySet()) {
84 if (t==trem) continue;
85 newmatrices.put(t, getMatrix(t, newmaster));
88 matrices = newmatrices;
91 public boolean contains(HasBindingGroup t) {
92 return matrices.get((T)t) != null;