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 /** tracks an equivalence class of geometric objects which are related to each other by transformation matrices */
7 public class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
8
9     private T                  master     = null;
10     private AffineConstraint   constraint = new AffineConstraint.All();
11     private HashMap<T,Matrix>  matrices   = new HashMap<T,Matrix>();
12
13     public BindingGroup(T master) {
14         this.master = master;
15         matrices.put(master, Matrix.ONE);
16     }
17
18     public int size() { return matrices.size(); }
19
20     public void merge(BindingGroup<T> bg, Matrix m) {
21         if (bg==this) {
22             if (m.equalsModuloEpsilon(Matrix.ONE, 0.001f)) return;
23             constraint = constraint.intersect(m.getAffineConstraint(0.001f), 0.001f);
24             return;
25         }
26
27         for(HasBindingGroup hbg : bg.matrices.keySet()) {
28             matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
29             hbg.bindingGroup = this;
30         }
31
32         // FIXME: what if points do not fall on the merged constraint-line?
33         AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master));
34         constraint = constraint.intersect(ac, 0.001f);
35
36         HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
37         for(HasBindingGroup hbg : bg.matrices.keySet())
38             stuff.add(hbg);
39         bg.matrices.clear();
40         bg.master = null;
41         for(HasBindingGroup hbg : stuff)
42             hbg.bindingGroupChanged(this);
43     }
44
45     public Matrix getMatrix(T t) { return matrices.get(t); }
46
47     public Iterator<T> iterator() { return matrices.keySet().iterator(); }
48
49     /** t1 = getMatrix(t1, t2) * t2 */
50     public Matrix getMatrix(T t1, T t2) {
51         //                    t1 = getMatrix(t1) * master
52         // getMatrix(t2)^-1 * t2 =                 master
53         //                    t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2
54         return getMatrix(t1).times(getMatrix(t2).inverse());
55     }
56
57     public AffineConstraint getConstraint(T t) {
58         return constraint.multiply(matrices.get(t));
59     }
60
61     public void unbind(T trem) {
62         if (trem != master) {
63             matrices.remove(trem);
64             return;
65         }
66         if (matrices.size()==1) {
67             master = null;
68             matrices.remove(trem);
69             return;
70         }
71         Iterator<T> it = iterator();
72         T newmaster = it.next();
73         if (newmaster==trem) newmaster = it.next();
74         if (newmaster==trem) throw new Error();
75         HashMap<T,Matrix> newmatrices = new HashMap<T,Matrix>();
76         for(T t : matrices.keySet()) {
77             if (t==trem) continue;
78             newmatrices.put(t, getMatrix(t, newmaster));
79         }
80         master = newmaster;
81         matrices = newmatrices;
82     }
83
84     public boolean contains(HasBindingGroup t) {
85         return matrices.get((T)t) != null;
86     }
87 }