1 package edu.berkeley.qfat.bind;
2 import edu.berkeley.qfat.geom.*;
6 * An equivalence class of geometric objects whose positions are
7 * related by affine transformation matrices and are constrained by
8 * an affine constraint.
10 class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
12 /** the arbitrarily-chosen master of the binding group */
13 private T master = null;
15 /** the affine constraint, in master coordinates, of this binding group */
16 private AffineConstraint constraint = AffineConstraint.ALL;
19 * For each member of the binding group, the matrix which must be
20 * multiplied by the master to get the member's position
22 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
24 BindingGroup(T master) {
26 matrices.put(master, Matrix.ONE);
29 /** the size of this binding group */
30 int size() { return matrices.size(); }
32 /** merge another binding group into this one */
33 void merge(BindingGroup<T> bg, Matrix m, float epsilon) {
35 if (m.equalsModuloEpsilon(Matrix.ONE, epsilon)) return;
36 // FIXME: what if points do not fall on the merged constraint-line?
37 constraint = constraint.intersect(m.getAffineConstraint(epsilon), epsilon);
41 for(HasBindingGroup hbg : bg) {
42 matrices.put((T)hbg, bg.getMatrix((T)hbg).times(m));
43 hbg.bindingGroup = this;
46 // FIXME: what if points do not fall on the merged constraint-line?
47 AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
48 constraint = constraint.intersect(ac, epsilon);
51 for(HasBindingGroup hbg : bg) hbg.bindingGroupChanged();
56 Matrix getMatrix(T t) { return matrices.get(t); }
58 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
60 /** t1 = getMatrix(t1, t2) * t2 */
61 Matrix getMatrix(T t1, T t2) {
62 // t1 = getMatrix(t1) * master
63 // getMatrix(t2)^-1 * t2 = master
64 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
65 return getMatrix(t1).times(getMatrix(t2).inverse());
68 AffineConstraint getAffineConstraint(T t) {
69 return constraint.multiply(matrices.get(t));
74 matrices.remove(trem);
77 if (matrices.size()==1) {
79 matrices.remove(trem);
82 Iterator<T> it = iterator();
83 T newmaster = it.next();
84 if (newmaster==trem) newmaster = it.next();
85 if (newmaster==trem) throw new Error("impossible");
87 // FIXME: is this correct?
88 constraint = constraint.multiply(getMatrix(newmaster, master));
90 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
91 for(T t : matrices.keySet()) {
92 if (t==trem) continue;
93 newmatrices.put(t, getMatrix(t, newmaster));
96 matrices = newmatrices;
99 boolean contains(HasBindingGroup t) {
100 return matrices.get((T)t) != null;