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 into this one */
25     void merge(BindingGroup<T> bg, Matrix m, float epsilon) {
26         if (bg==this) {
27             if (m.equalsModuloEpsilon(Matrix.ONE, epsilon)) return;
28             // FIXME: what if points do not fall on the merged constraint-line?
29             constraint = constraint.intersect(m.getAffineConstraint(epsilon), epsilon);
30             return;
31         }
32
33         for(HasBindingGroup hbg : bg) {
34             matrices.put((T)hbg, bg.getMatrix((T)hbg).times(m));
35             hbg.bindingGroup = this;
36         }
37
38         // FIXME: what if points do not fall on the merged constraint-line?
39         AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
40         constraint = constraint.intersect(ac, epsilon);
41
42         bg.master = null;
43         for(HasBindingGroup hbg : bg) hbg.bindingGroupChanged();
44         bg.matrices.clear();
45         bg.matrices = null;
46     }
47
48     public Matrix getMatrix(T t) { return matrices.get(t); }
49
50     public Iterator<T> iterator() { return matrices.keySet().iterator(); }
51
52     /** t1 = getMatrix(t1, t2) * t2 */
53     public Matrix getMatrix(T t1, T t2) {
54         //                    t1 = getMatrix(t1) * master
55         // getMatrix(t2)^-1 * t2 =                 master
56         //                    t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
57         return getMatrix(t1).times(getMatrix(t2).inverse());
58     }
59
60     public AffineConstraint getAffineConstraint(T t) {
61         return constraint.multiply(matrices.get(t));
62     }
63
64     void unbind(T trem) {
65         if (trem != master) {
66             matrices.remove(trem);
67             return;
68         }
69         if (matrices.size()==1) {
70             master = null;
71             matrices.remove(trem);
72             return;
73         }
74         Iterator<T> it = iterator();
75         T newmaster = it.next();
76         if (newmaster==trem) newmaster = it.next();
77         if (newmaster==trem) throw new Error("impossible");
78
79         // FIXME: is this correct?
80         constraint = constraint.multiply(getMatrix(newmaster, master));
81
82         HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
83         for(T t : matrices.keySet()) {
84             if (t==trem) continue;
85             newmatrices.put(t, getMatrix(t, newmaster));
86         }
87         master = newmaster;
88         matrices = newmatrices;
89     }
90
91     public boolean contains(HasBindingGroup t) {
92         return matrices.get((T)t) != null;
93     }
94 }