270bc78fb0bcee383ca5561b859c40691d297a16
[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, 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.matrices.keySet()) {
34             matrices.put((T)hbg, bg.matrices.get(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.matrices.keySet())
44             hbg.bindingGroupChanged();
45         bg.matrices.clear();
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 getConstraint(T t) {
61         return constraint.multiply(matrices.get(t));
62     }
63
64     public 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();
78         HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
79         for(T t : matrices.keySet()) {
80             if (t==trem) continue;
81             newmatrices.put(t, getMatrix(t, newmaster));
82         }
83         master = newmaster;
84         matrices = newmatrices;
85     }
86
87     public boolean contains(HasBindingGroup t) {
88         return matrices.get((T)t) != null;
89     }
90 }