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;
37 if (master instanceof edu.berkeley.qfat.Mesh.Vertex)
38 System.err.println(m.times(m));
40 // FIXME: what if points do not fall on the merged constraint-line?
41 constraint = constraint.intersect(m.getAffineConstraint(epsilon), epsilon);
45 // bg.master = m * master
46 // hbg = bg.getMatrix(hbg) * bg.master
47 // hbg = bg.getMatrix(hbg) * m * master
48 for(HasBindingGroup hbg : bg) {
49 matrices.put((T)hbg, bg.getMatrix((T)hbg).times(m));
50 hbg.bindingGroup = this;
53 // FIXME: what if points do not fall on the merged constraint-line?
54 AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
55 constraint = constraint.intersect(ac, epsilon);
58 for(HasBindingGroup hbg : bg) hbg.bindingGroupChanged();
63 Matrix getMatrix(T t) { return matrices.get(t); }
65 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
67 /** t1 = getMatrix(t1, t2) * t2 */
68 Matrix getMatrix(T t1, T t2) {
69 // t1 = getMatrix(t1) * master
70 // getMatrix(t2)^-1 * t2 = master
71 // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
72 return getMatrix(t1).times(getMatrix(t2).inverse());
75 AffineConstraint getAffineConstraint(T t) {
76 return constraint.multiply(matrices.get(t));
81 matrices.remove(trem);
84 if (matrices.size()==1) {
86 matrices.remove(trem);
89 Iterator<T> it = iterator();
90 T newmaster = it.next();
91 if (newmaster==trem) newmaster = it.next();
92 if (newmaster==trem) throw new Error("impossible");
94 // FIXME: is this correct?
95 constraint = constraint.multiply(getMatrix(newmaster, master));
97 HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
98 for(T t : matrices.keySet()) {
99 if (t==trem) continue;
100 newmatrices.put(t, getMatrix(t, newmaster));
103 matrices = newmatrices;
106 boolean contains(HasBindingGroup t) {
107 return matrices.get((T)t) != null;