checkpoint
[anneal.git] / src / edu / berkeley / qfat / bind / BindingGroup.java
1 package edu.berkeley.qfat.bind;
2 import edu.berkeley.qfat.geom.*;
3 import javax.media.opengl.*;
4 import java.util.*;
5
6 /**
7  *  An equivalence class of geometric objects whose positions are
8  *  related by affine transformation matrices and are constrained by
9  *  an affine constraint.
10  */
11 class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
12
13     private T                  master     = null;
14     private AffineConstraint   constraint = new AffineConstraint.All();
15     private HashMap<T,Matrix>  matrices   = new HashMap<T,Matrix>();
16
17     public BindingGroup(T master) {
18         this.master = master;
19         matrices.put(master, Matrix.ONE);
20     }
21
22     int size() { return matrices.size(); }
23
24     /** merge another binding group with this one */
25     void merge(BindingGroup<T> bg, Matrix m) {
26         if (bg==this) {
27             if (m.equalsModuloEpsilon(Matrix.ONE, 0.001f)) return;
28             constraint = constraint.intersect(m.getAffineConstraint(0.001f), 0.001f);
29             return;
30         }
31
32         for(HasBindingGroup hbg : bg.matrices.keySet()) {
33             matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
34             hbg.bindingGroup = this;
35         }
36
37         // FIXME: what if points do not fall on the merged constraint-line?
38         AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
39         constraint = constraint.intersect(ac, 0.001f);
40
41         HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
42         for(HasBindingGroup hbg : bg.matrices.keySet())
43             stuff.add(hbg);
44         bg.matrices.clear();
45         bg.master = null;
46         for(HasBindingGroup hbg : stuff)
47             hbg.bindingGroupChanged();
48     }
49
50     public Matrix getMatrix(T t) { return matrices.get(t); }
51
52     public Iterator<T> iterator() { return matrices.keySet().iterator(); }
53
54     /** t1 = getMatrix(t1, t2) * t2 */
55     public Matrix getMatrix(T t1, T t2) {
56         //                    t1 = getMatrix(t1) * master
57         // getMatrix(t2)^-1 * t2 =                 master
58         //                    t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
59         return getMatrix(t1).times(getMatrix(t2).inverse());
60     }
61
62     public AffineConstraint getConstraint(T t) {
63         return constraint.multiply(matrices.get(t));
64     }
65
66     public void unbind(T trem) {
67         if (trem != master) {
68             matrices.remove(trem);
69             return;
70         }
71         if (matrices.size()==1) {
72             master = null;
73             matrices.remove(trem);
74             return;
75         }
76         Iterator<T> it = iterator();
77         T newmaster = it.next();
78         if (newmaster==trem) newmaster = it.next();
79         if (newmaster==trem) throw new Error();
80         HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
81         for(T t : matrices.keySet()) {
82             if (t==trem) continue;
83             newmatrices.put(t, getMatrix(t, newmaster));
84         }
85         master = newmaster;
86         matrices = newmatrices;
87     }
88
89     public boolean contains(HasBindingGroup t) {
90         return matrices.get((T)t) != null;
91     }
92 }