+ T t; // triangle to our "left"
+ E prev; // previous half-edge
+ E next; // next half-edge
+ E pair; // partner half-edge
+
+
+ public BindingGroup bg = null;
+
+ public void bind(E e) { bind(e, new M()); }
+ public void bind(E e, M m) {
+ if (e.bg != null) e.bg.merge(this);
+ else if (bg != null) bg.merge(e);
+ else {
+ bg = new BindingGroup();
+ bg.merge(this);
+ bg.merge(e);
+ }
+ }
+
+ public void dobind() {
+ if (bg==null) return;
+ for(E ex : bg.es) {
+ if (ex==this) continue;
+ p1.bind(ex.p1);
+ p2.bind(ex.p2);
+ }
+ }
+
+ public boolean destroyed = false;
+ public void shatter() {
+ if (destroyed) return;
+ /*
+ HashSet<E> eh = new HashSet<E>();
+ eh.add(this);
+ for(E ex = bound_to; ex != this; ex = ex.bound_to) eh.add(ex);
+ E[] es = (E[])eh.toArray(new E[0]);
+ */
+ destroy();
+ pair.shatter();
+ for(E ex = bound_to; ex != this; ex = ex.bound_to) ex.shatter();
+ }
+ public void destroy() {
+ this.destroyed = true;
+ }
+
+ private void sync() {
+ this.prev.next = this;
+ this.next.prev = this;
+ this.pair.pair = this;
+ if (this.next.p1 != p2) throw new Error();
+ if (this.prev.p2 != p1) throw new Error();
+ if (this.p1.e == null) this.p1.e = this;
+ }
+
+ public T makeT() { return t==null ? (t = new T(this)) : t; }
+
+ /** angle between this half-edge and the next */
+ public double angle() {
+ V v1 = next.p2.minus(p2);
+ V v2 = this.p1.minus(p2);
+ return Math.acos(v1.norm().dot(v2.norm()));
+ }
+
+ public void makeAdjacent(E e) {
+ if (this.next == e) return;
+ if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
+ if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
+
+ E freeIncident = p2.getFreeIncident(e, this);
+
+ e.prev.next = freeIncident.next;
+ freeIncident.next.prev = e.prev;
+
+ freeIncident.next = this.next;
+ this.next.prev = freeIncident;
+
+ this.next = e;
+ e.prev = this;
+
+ sync();
+ freeIncident.sync();
+ }
+
+ /** creates an isolated edge out in the middle of space */