X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2Fgeom%2FBindingGroup.java;h=e64cc17e119efb480ead9cbe830bfb775b732214;hp=ca07312ad88e317eab2cb207b78ab7de3c855306;hb=64c6939d78acfa06e7bc380cb713e3800bf16be5;hpb=bebc4007cb48fe93ca03f97047d3445a881afe9e diff --git a/src/edu/berkeley/qfat/geom/BindingGroup.java b/src/edu/berkeley/qfat/geom/BindingGroup.java index ca07312..e64cc17 100644 --- a/src/edu/berkeley/qfat/geom/BindingGroup.java +++ b/src/edu/berkeley/qfat/geom/BindingGroup.java @@ -2,34 +2,85 @@ package edu.berkeley.qfat.geom; import javax.media.opengl.*; import java.util.*; -public class BindingGroup { +/** tracks an equivalence class of geometric objects which are related to each other by transformation matrices */ +public class BindingGroup implements Iterable { - private T master = null; - private HashMap matrices = new HashMap(); + private T master = null; + private AffineConstraint constraint = new AffineConstraint.All(); + private HashMap matrices = new HashMap(); public BindingGroup(T master) { this.master = master; matrices.put(master, Matrix.ONE); } + public int size() { return matrices.size(); } + public void merge(BindingGroup bg, Matrix m) { + if (bg==this) { + if (m.equalsModuloEpsilon(Matrix.ONE, 0.001f)) return; + System.err.println(m.getAffineConstraint(0.001f)); + return; + } + for(HasBindingGroup hbg : bg.matrices.keySet()) { matrices.put((T)hbg, bg.matrices.get(hbg).times(m)); hbg.bindingGroup = this; } + + // FIXME: what if points do not fall on the merged constraint-line? + AffineConstraint ac = bg.constraint.multiply(getMatrix(master, bg.master)); + constraint = constraint.intersect(ac, 0.001f); + + HashSet stuff = new HashSet(); + for(HasBindingGroup hbg : bg.matrices.keySet()) + stuff.add(hbg); bg.matrices.clear(); bg.master = null; + for(HasBindingGroup hbg : stuff) + hbg.bindingGroupChanged(this); } - public T getMaster() { return master; } public Matrix getMatrix(T t) { return matrices.get(t); } - // t1 = getMatrix(t1) * master - // getMatrix(t2)^-1 * t2 = master - // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2 + public Iterator iterator() { return matrices.keySet().iterator(); } + /** t1 = getMatrix(t1, t2) * t2 */ public Matrix getMatrix(T t1, T t2) { + // t1 = getMatrix(t1) * master + // getMatrix(t2)^-1 * t2 = master + // t1 = getMatrix(t1) * getMatrix(t2)^-1 * t2 return getMatrix(t1).times(getMatrix(t2).inverse()); } + public AffineConstraint getConstraint(T t) { + return constraint.multiply(matrices.get(t)); + } + + public void unbind(T trem) { + if (trem != master) { + matrices.remove(trem); + return; + } + if (matrices.size()==1) { + master = null; + matrices.remove(trem); + return; + } + Iterator it = iterator(); + T newmaster = it.next(); + if (newmaster==trem) newmaster = it.next(); + if (newmaster==trem) throw new Error(); + HashMap newmatrices = new HashMap(); + for(T t : matrices.keySet()) { + if (t==trem) continue; + newmatrices.put(t, getMatrix(t, newmaster)); + } + master = newmaster; + matrices = newmatrices; + } + + public boolean contains(HasBindingGroup t) { + return matrices.get((T)t) != null; + } }