checkpoint
[anneal.git] / src / edu / berkeley / qfat / bind / BindingGroup.java
1 package edu.berkeley.qfat.bind;
2 import edu.berkeley.qfat.geom.*;
3 import java.util.*;
4
5 /**
6  *  An equivalence class of geometric objects whose positions are
7  *  related by affine transformation matrices and are constrained by
8  *  an affine constraint.
9  */
10 class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
11
12     /** the arbitrarily-chosen master of the binding group */
13     private T                  master     = null;
14
15     /** the affine constraint, in master coordinates, of this binding group */
16     private AffineConstraint   constraint = AffineConstraint.ALL;
17
18     /**
19      *  For each member of the binding group, the matrix which must be
20      *  multiplied by the master to get the member's position
21      */
22     private HashMap<T,Matrix>  matrices   = new HashMap<T,Matrix>();
23
24     BindingGroup(T master) {
25         this.master = master;
26         matrices.put(master, Matrix.ONE);
27     }
28
29     /** the size of this binding group */
30     int size() { return matrices.size(); }
31
32     /** merge another binding group into this one */
33     void merge(BindingGroup<T> bg, Matrix m, float epsilon) {
34         if (bg==this) {
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);
38             return;
39         }
40
41         for(HasBindingGroup hbg : bg) {
42             matrices.put((T)hbg, bg.getMatrix((T)hbg).times(m));
43             hbg.bindingGroup = this;
44         }
45
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);
49
50         bg.master = null;
51         for(HasBindingGroup hbg : bg) hbg.bindingGroupChanged();
52         bg.matrices.clear();
53         bg.matrices = null;
54     }
55
56     Matrix getMatrix(T t) { return matrices.get(t); }
57
58     public Iterator<T> iterator() { return matrices.keySet().iterator(); }
59
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());
66     }
67
68     AffineConstraint getAffineConstraint(T t) {
69         return constraint.multiply(matrices.get(t));
70     }
71
72     void unbind(T trem) {
73         if (trem != master) {
74             matrices.remove(trem);
75             return;
76         }
77         if (matrices.size()==1) {
78             master = null;
79             matrices.remove(trem);
80             return;
81         }
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");
86
87         // FIXME: is this correct?
88         constraint = constraint.multiply(getMatrix(newmaster, master));
89
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));
94         }
95         master = newmaster;
96         matrices = newmatrices;
97     }
98
99     boolean contains(HasBindingGroup t) {
100         return matrices.get((T)t) != null;
101     }
102 }