1 package edu.berkeley.qfat.bind;
2 import edu.berkeley.qfat.geom.*;
3 import javax.media.opengl.*;
7 * An equivalence class of geometric objects whose positions are
8 * related by affine transformation matrices and are constrained by
9 * an affine constraint.
11 class BindingGroup<T extends HasBindingGroup> implements Iterable<T> {
13 private T master = null;
14 private AffineConstraint constraint = new AffineConstraint.All();
15 private HashMap<T,Matrix> matrices = new HashMap<T,Matrix>();
17 public BindingGroup(T master) {
19 matrices.put(master, Matrix.ONE);
22 int size() { return matrices.size(); }
24 /** merge another binding group with this one */
25 void merge(BindingGroup<T> bg, Matrix m) {
27 if (m.equalsModuloEpsilon(Matrix.ONE, 0.001f)) return;
28 constraint = constraint.intersect(m.getAffineConstraint(0.001f), 0.001f);
32 for(HasBindingGroup hbg : bg.matrices.keySet()) {
33 matrices.put((T)hbg, bg.matrices.get(hbg).times(m));
34 hbg.bindingGroup = this;
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);
41 HashSet<HasBindingGroup> stuff = new HashSet<HasBindingGroup>();
42 for(HasBindingGroup hbg : bg.matrices.keySet())
46 for(HasBindingGroup hbg : stuff)
47 hbg.bindingGroupChanged();
50 public Matrix getMatrix(T t) { return matrices.get(t); }
52 public Iterator<T> iterator() { return matrices.keySet().iterator(); }
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());
62 public AffineConstraint getConstraint(T t) {
63 return constraint.multiply(matrices.get(t));
66 public void unbind(T trem) {
68 matrices.remove(trem);
71 if (matrices.size()==1) {
73 matrices.remove(trem);
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));
86 matrices = newmatrices;
89 public boolean contains(HasBindingGroup t) {
90 return matrices.get((T)t) != null;