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             /*
37             if (master instanceof edu.berkeley.qfat.Mesh.Vertex)
38                 System.err.println(m.times(m));
39             */
40             // FIXME: what if points do not fall on the merged constraint-line?
41             constraint = constraint.intersect(m.getAffineConstraint(epsilon), epsilon);
42             return;
43         }
44
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;
51         }
52
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);
56
57         bg.master = null;
58         for(HasBindingGroup hbg : bg) hbg.bindingGroupChanged();
59         bg.matrices.clear();
60         bg.matrices = null;
61     }
62
63     Matrix getMatrix(T t) { return matrices.get(t); }
64
65     public Iterator<T> iterator() { return matrices.keySet().iterator(); }
66
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());
73     }
74
75     AffineConstraint getAffineConstraint(T t) {
76         return constraint.multiply(matrices.get(t));
77     }
78
79     void unbind(T trem) {
80         if (trem != master) {
81             matrices.remove(trem);
82             return;
83         }
84         if (matrices.size()==1) {
85             master = null;
86             matrices.remove(trem);
87             return;
88         }
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");
93
94         // FIXME: is this correct?
95         constraint = constraint.multiply(getMatrix(newmaster, master));
96
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));
101         }
102         master = newmaster;
103         matrices = newmatrices;
104     }
105
106     boolean contains(HasBindingGroup t) {
107         return matrices.get((T)t) != null;
108     }
109 }