|
|
d33d2a |
using System;
|
|
|
d33d2a |
using System.Collections.Generic;
|
|
|
d33d2a |
using System.Linq;
|
|
|
d33d2a |
|
|
|
d33d2a |
namespace Contours {
|
|
|
d33d2a |
public enum IntersectionType {
|
|
|
d33d2a |
None,
|
|
|
d33d2a |
Cross,
|
|
|
d33d2a |
Identical,
|
|
|
d33d2a |
Inverted,
|
|
|
d33d2a |
Touch_a0,
|
|
|
d33d2a |
Touch_a1,
|
|
|
d33d2a |
Touch_b0,
|
|
|
d33d2a |
Touch_b1,
|
|
|
d33d2a |
Touch_a0_b0,
|
|
|
d33d2a |
Touch_a0_b1,
|
|
|
d33d2a |
Touch_a1_b0,
|
|
|
d33d2a |
Touch_a1_b1,
|
|
|
d33d2a |
Along_a0_b0_a1_b1,
|
|
|
d33d2a |
Along_a0_b0_b1_a1,
|
|
|
d33d2a |
Along_a0_b1_a1_b0,
|
|
|
d33d2a |
Along_a0_b1_b0_a1,
|
|
|
d33d2a |
Along_b0_a0_a1_b1,
|
|
|
d33d2a |
Along_b0_a0_b1_a1,
|
|
|
d33d2a |
Along_b1_a0_a1_b0,
|
|
|
d33d2a |
Along_b1_a0_b0_a1
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public class Circuit<parent, child=""> where Parent: class where Child: class {</parent,>
|
|
|
d33d2a |
public class Entry {
|
|
|
d33d2a |
readonly Child owner;
|
|
|
d33d2a |
Circuit<parent, child=""> circuit;</parent,>
|
|
|
d33d2a |
Entry previous;
|
|
|
d33d2a |
Entry next;
|
|
|
d33d2a |
|
|
|
d33d2a |
public Entry(Child owner) { this.owner = owner; }
|
|
|
d33d2a |
|
|
|
d33d2a |
public Child getOwner() { return owner; }
|
|
|
d33d2a |
public Circuit<parent, child=""> getCircuit() { return circuit; }</parent,>
|
|
|
d33d2a |
public Parent getParent() {
|
|
|
d33d2a |
Circuit<parent, child=""> circuit = getCircuit();</parent,>
|
|
|
d33d2a |
return circuit == null ? null : circuit.getOwner();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public Entry getPreviousEntry() { return previous; }
|
|
|
d33d2a |
public Entry getNextEntry() { return next; }
|
|
|
d33d2a |
|
|
|
d33d2a |
public Entry getPreviousEntryLinear() {
|
|
|
d33d2a |
Entry e = getPreviousEntry();
|
|
|
d33d2a |
return e == null || e == circuit.last ? null : e;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
public Entry getNextEntryLinear() {
|
|
|
d33d2a |
Entry e = getNextEntry();
|
|
|
d33d2a |
return e == null || e == circuit.first ? null : e;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public Child getPrevious() {
|
|
|
d33d2a |
Entry e = getPreviousEntry();
|
|
|
d33d2a |
return e == null ? null : e.getOwner();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
public Child getNext() {
|
|
|
d33d2a |
Entry e = getNextEntry();
|
|
|
d33d2a |
return e == null ? null : e.getOwner();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public Child getPreviousLinear() {
|
|
|
d33d2a |
Entry e = getPreviousEntryLinear();
|
|
|
d33d2a |
return e == null ? null : e.getOwner();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
public Child getNextLinear() {
|
|
|
d33d2a |
Entry e = getNextEntryLinear();
|
|
|
d33d2a |
return e == null ? null : e.getOwner();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void unlink() {
|
|
|
d33d2a |
if (getCircuit() == null) return;
|
|
|
d33d2a |
previous.next = next;
|
|
|
d33d2a |
next.previous = previous;
|
|
|
d33d2a |
if (previous == next) {
|
|
|
d33d2a |
previous = this;
|
|
|
d33d2a |
next = this;
|
|
|
d33d2a |
if (previous == next) {
|
|
|
d33d2a |
circuit.first = circuit.last = null;
|
|
|
d33d2a |
} else {
|
|
|
d33d2a |
if (circuit.first == this)
|
|
|
d33d2a |
circuit.first = next;
|
|
|
d33d2a |
if (circuit.last == this)
|
|
|
d33d2a |
circuit.last = previous;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
--circuit.count;
|
|
|
d33d2a |
circuit = null;
|
|
|
d33d2a |
previous = null;
|
|
|
d33d2a |
next = null;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void insertBack(Circuit<parent, child=""> circuit) {</parent,>
|
|
|
d33d2a |
if (getCircuit() == circuit) return;
|
|
|
d33d2a |
unlink();
|
|
|
d33d2a |
if (circuit != null) {
|
|
|
d33d2a |
if (circuit.empty()) {
|
|
|
d33d2a |
this.circuit = circuit;
|
|
|
d33d2a |
previous = next = this;
|
|
|
d33d2a |
circuit.first = circuit.last = this;
|
|
|
d33d2a |
++circuit.count;
|
|
|
d33d2a |
} else {
|
|
|
d33d2a |
insertAfterOf(circuit.getLastEntry());
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void insertFront(Circuit<parent, child=""> circuit) {</parent,>
|
|
|
d33d2a |
if (getCircuit() == circuit) return;
|
|
|
d33d2a |
unlink();
|
|
|
d33d2a |
if (circuit != null) {
|
|
|
d33d2a |
if (circuit.empty()) {
|
|
|
d33d2a |
this.circuit = circuit;
|
|
|
d33d2a |
previous = next = this;
|
|
|
d33d2a |
circuit.first = circuit.last = this;
|
|
|
d33d2a |
++circuit.count;
|
|
|
d33d2a |
} else {
|
|
|
d33d2a |
insertBeforeOf(circuit.getFirstEntry());
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void insertAfterOf(Entry entry) {
|
|
|
d33d2a |
if (entry == null || entry.getCircuit() == null) { unlink(); return; }
|
|
|
d33d2a |
if (getCircuit() == entry.getCircuit()) return;
|
|
|
d33d2a |
unlink();
|
|
|
d33d2a |
circuit = entry.getCircuit();
|
|
|
d33d2a |
previous = entry;
|
|
|
d33d2a |
next = entry.next;
|
|
|
d33d2a |
next.previous = this;
|
|
|
d33d2a |
previous.next = this;
|
|
|
d33d2a |
if (circuit.getLastEntry() == entry)
|
|
|
d33d2a |
circuit.last = this;
|
|
|
d33d2a |
++circuit.count;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void insertBeforeOf(Entry entry) {
|
|
|
d33d2a |
if (entry == null || entry.getCircuit() == null) { unlink(); return; }
|
|
|
d33d2a |
if (getCircuit() == entry.getCircuit()) return;
|
|
|
d33d2a |
unlink();
|
|
|
d33d2a |
circuit = entry.getCircuit();
|
|
|
d33d2a |
previous = entry.previous;
|
|
|
d33d2a |
next = entry;
|
|
|
d33d2a |
previous.next = this;
|
|
|
d33d2a |
next.previous = this;
|
|
|
d33d2a |
if (circuit.getFirstEntry() == entry)
|
|
|
d33d2a |
circuit.first = this;
|
|
|
d33d2a |
++circuit.count;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
9f4b34 |
public void swapWith(Entry other) {
|
|
|
d33d2a |
Circuit<parent, child=""> otherCircuit = other.circuit;</parent,>
|
|
|
d33d2a |
Entry otherPrevious = other.previous;
|
|
|
d33d2a |
Entry otherNext = other.next;
|
|
|
d33d2a |
|
|
|
d33d2a |
other.circuit = circuit;
|
|
|
d33d2a |
other.previous = previous;
|
|
|
d33d2a |
other.next = next;
|
|
|
d33d2a |
|
|
|
d33d2a |
if (otherCircuit != null) {
|
|
|
d33d2a |
if (otherCircuit.first == other)
|
|
|
d33d2a |
otherCircuit.first = this;
|
|
|
d33d2a |
if (otherCircuit.last == other)
|
|
|
d33d2a |
otherCircuit.last = this;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
if (circuit != null) {
|
|
|
d33d2a |
if (circuit.first == this)
|
|
|
d33d2a |
circuit.first = other;
|
|
|
d33d2a |
if (circuit.last == this)
|
|
|
d33d2a |
circuit.last = other;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
circuit = otherCircuit;
|
|
|
d33d2a |
previous = otherPrevious;
|
|
|
d33d2a |
next = otherNext;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
readonly Parent owner;
|
|
|
d33d2a |
Entry first;
|
|
|
d33d2a |
Entry last;
|
|
|
d33d2a |
int count = 0;
|
|
|
d33d2a |
|
|
|
d33d2a |
public Circuit(Parent owner) { this.owner = owner; }
|
|
|
d33d2a |
|
|
|
d33d2a |
public Parent getOwner() { return owner; }
|
|
|
d33d2a |
public int getCount() { return count; }
|
|
|
d33d2a |
|
|
|
d33d2a |
public Entry getFirstEntry() { return first; }
|
|
|
d33d2a |
public Entry getLastEntry() { return last; }
|
|
|
d33d2a |
|
|
|
d33d2a |
public Child getFirst() {
|
|
|
d33d2a |
Entry e = getFirstEntry();
|
|
|
d33d2a |
return e == null ? null : e.getOwner();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
public Child getLast() {
|
|
|
d33d2a |
Entry e = getLastEntry();
|
|
|
d33d2a |
return e == null ? null : e.getOwner();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public bool empty() { return getFirstEntry() == null; }
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public class Shape {
|
|
|
d33d2a |
public class Position {
|
|
|
d33d2a |
public int x = 0;
|
|
|
d33d2a |
public int y = 0;
|
|
|
d33d2a |
public readonly Circuit<shape, position="">.Entry shape;</shape,>
|
|
|
d33d2a |
public readonly Circuit<position, link=""> links;</position,>
|
|
|
d33d2a |
|
|
|
d33d2a |
public Position() {
|
|
|
d33d2a |
shape = new Circuit<shape, position="">.Entry(this);</shape,>
|
|
|
d33d2a |
links = new Circuit<position, link="">(this);</position,>
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public Position(int x, int y): this() { this.x = x; this.y = y; }
|
|
|
d33d2a |
|
|
|
d33d2a |
public VectorInt toVectorInt() { return new VectorInt(x, y); }
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public class Contour {
|
|
|
d33d2a |
public readonly Circuit<shape, contour="">.Entry shape;</shape,>
|
|
|
d33d2a |
public readonly Circuit<contour, link=""> forward;</contour,>
|
|
|
d33d2a |
public readonly Circuit<contour, link=""> backward;</contour,>
|
|
|
d33d2a |
|
|
|
d33d2a |
public Contour() {
|
|
|
d33d2a |
shape = new Circuit<shape, contour="">.Entry(this);</shape,>
|
|
|
d33d2a |
forward = new Circuit<contour, link="">(this);</contour,>
|
|
|
d33d2a |
backward = new Circuit<contour, link="">(this);</contour,>
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public class Link {
|
|
|
d33d2a |
public readonly Circuit<shape, link="">.Entry shape;</shape,>
|
|
|
d33d2a |
public readonly Circuit<position, link="">.Entry position;</position,>
|
|
|
d33d2a |
public readonly Circuit<contour, link="">.Entry contour;</contour,>
|
|
|
d33d2a |
|
|
|
d33d2a |
public Link() {
|
|
|
d33d2a |
shape = new Circuit<shape, link="">.Entry(this);</shape,>
|
|
|
d33d2a |
position = new Circuit<position, link="">.Entry(this);</position,>
|
|
|
d33d2a |
contour = new Circuit<contour, link="">.Entry(this);</contour,>
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public bool isForward() {
|
|
|
d33d2a |
Contour c = contour.getParent();
|
|
|
d33d2a |
if (c == null) return false;
|
|
|
d33d2a |
return c == null || contour.getCircuit() == c.forward;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void unlink() {
|
|
|
d33d2a |
contour.unlink();
|
|
|
d33d2a |
position.unlink();
|
|
|
d33d2a |
shape.unlink();
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public static Link create(Position position, Circuit<contour, link=""> contourCircuit) {</contour,>
|
|
|
d33d2a |
Link link = new Link();
|
|
|
d33d2a |
link.shape.insertBack(contourCircuit.getOwner().shape.getParent().links);
|
|
|
d33d2a |
link.position.insertBack(position.links);
|
|
|
d33d2a |
link.contour.insertBack(contourCircuit);
|
|
|
d33d2a |
return link;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public Link createSplitAfter(Position position) {
|
|
|
d33d2a |
Link link = new Link();
|
|
|
d33d2a |
link.shape.insertBack(shape.getParent().links);
|
|
|
d33d2a |
link.position.insertBack(position.links);
|
|
|
d33d2a |
link.contour.insertAfterOf(contour);
|
|
|
d33d2a |
return link;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public Link createSplitAfter(Circuit<position, link="">.Entry position) {</position,>
|
|
|
d33d2a |
Link link = new Link();
|
|
|
d33d2a |
link.shape.insertBack(shape.getParent().links);
|
|
|
d33d2a |
link.position.insertAfterOf(position);
|
|
|
d33d2a |
link.contour.insertAfterOf(contour);
|
|
|
d33d2a |
return link;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public Circuit<shape, position=""> positions;</shape,>
|
|
|
d33d2a |
public Circuit<shape, link=""> links;</shape,>
|
|
|
d33d2a |
public Circuit<shape, contour=""> contours;</shape,>
|
|
|
d33d2a |
|
|
|
d33d2a |
public void addContours(ContourInt contours) {
|
|
|
d33d2a |
foreach(List<vectorint> contour in contours.contours)</vectorint>
|
|
|
d33d2a |
addContour(contour);
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void addContour(ICollection<vectorint> points) {</vectorint>
|
|
|
d33d2a |
if (points.Count < 3)
|
|
|
d33d2a |
return;
|
|
|
d33d2a |
|
|
|
d33d2a |
Contour contour = new Contour();
|
|
|
d33d2a |
contour.shape.insertBack(contours);
|
|
|
d33d2a |
foreach(VectorInt point in points) {
|
|
|
d33d2a |
Position position = new Position(point.x, point.y);
|
|
|
d33d2a |
position.shape.insertBack(positions);
|
|
|
d33d2a |
Link.create(position, contour.forward);
|
|
|
d33d2a |
Link.create(position, contour.backward);
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public static int compare(VectorInt a0, VectorInt a1, VectorInt base0, VectorInt base1) {
|
|
|
d33d2a |
if (base0.x < base1.x && a0.x < a1.x) return -1;
|
|
|
d33d2a |
if (base0.x < base1.x && a1.x < a0.x) return 1;
|
|
|
d33d2a |
if (base1.x < base0.x && a0.x < a1.x) return 1;
|
|
|
d33d2a |
if (base1.x < base0.x && a1.x < a0.x) return -1;
|
|
|
d33d2a |
if (base0.y < base1.x && a0.y < a1.x) return -1;
|
|
|
d33d2a |
if (base0.y < base1.x && a1.y < a0.x) return 1;
|
|
|
d33d2a |
if (base1.y < base0.x && a0.y < a1.x) return 1;
|
|
|
d33d2a |
if (base1.y < base0.x && a1.y < a0.x) return -1;
|
|
|
d33d2a |
return 0;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public static IntersectionType findIntersection(VectorInt a0, VectorInt a1, VectorInt b0, VectorInt b1, out VectorInt c) {
|
|
|
d33d2a |
c = new VectorInt();
|
|
|
d33d2a |
VectorInt da = new VectorInt(a1.x - a0.x, a1.y - a0.y);
|
|
|
d33d2a |
VectorInt db = new VectorInt(b1.x - b0.x, b1.y - b0.y);
|
|
|
d33d2a |
|
|
|
d33d2a |
if (a0.x == b0.x && a0.y == b0.y && a1.x == b1.x && a1.y == b1.y)
|
|
|
d33d2a |
return IntersectionType.Identical;
|
|
|
d33d2a |
|
|
|
d33d2a |
if (a0.x == b1.x && a0.y == b1.y && a1.x == b0.x && a1.y == b0.y)
|
|
|
d33d2a |
return IntersectionType.Inverted;
|
|
|
d33d2a |
|
|
|
d33d2a |
long divider = (long)da.x*(long)db.y - (long)db.x*(long)da.y;
|
|
|
d33d2a |
if (divider == 0) {
|
|
|
d33d2a |
if ((long)da.x*(long)(b0.y - a0.y) != (long)da.y*(long)(b0.x - a0.x))
|
|
|
d33d2a |
return IntersectionType.None;
|
|
|
d33d2a |
|
|
|
d33d2a |
int a0b0 = compare(a0, b0, a0, a1);
|
|
|
d33d2a |
int a0b1 = compare(a0, b1, a0, a1);
|
|
|
d33d2a |
int a1b0 = compare(a1, b0, a0, a1);
|
|
|
d33d2a |
int a1b1 = compare(a1, b1, a0, a1);
|
|
|
d33d2a |
int b0b1 = compare(b0, b1, a0, a1);
|
|
|
d33d2a |
int b0a0 = -a0b0;
|
|
|
d33d2a |
int b0a1 = -a1b0;
|
|
|
d33d2a |
int b1a0 = -a0b1;
|
|
|
d33d2a |
int b1a1 = -a1b1;
|
|
|
d33d2a |
int b1b0 = -b0b1;
|
|
|
d33d2a |
|
|
|
d33d2a |
// a0a1b0b1
|
|
|
d33d2a |
if (a1b0 == 0 && b0b1 < 0)
|
|
|
d33d2a |
return IntersectionType.Touch_a1_b0;
|
|
|
d33d2a |
// a0a1b1b0
|
|
|
d33d2a |
if (a1b1 == 0 && b1b0 < 0)
|
|
|
d33d2a |
return IntersectionType.Touch_a1_b1;
|
|
|
d33d2a |
// b0b1a0a1
|
|
|
d33d2a |
if (b0b1 < 0 && b1a0 == 0)
|
|
|
d33d2a |
return IntersectionType.Touch_a0_b1;
|
|
|
d33d2a |
// b1b0a0a1
|
|
|
d33d2a |
if (b1b0 < 0 && b0a0 == 0)
|
|
|
d33d2a |
return IntersectionType.Touch_a0_b0;
|
|
|
d33d2a |
|
|
|
d33d2a |
if (a0b0 <= 0 && b0a1 <= 0 && a1b1 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_a0_b0_a1_b1;
|
|
|
d33d2a |
if (a0b0 <= 0 && b0b1 <= 0 && b1a1 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_a0_b0_b1_a1;
|
|
|
d33d2a |
if (a0b1 <= 0 && b1a1 <= 0 && a1b0 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_a0_b1_a1_b0;
|
|
|
d33d2a |
if (a0b1 <= 0 && b1b0 <= 0 && b0a1 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_a0_b1_b0_a1;
|
|
|
d33d2a |
if (b0a0 <= 0 && /* a0a1 */ a1b1 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_b0_a0_a1_b1;
|
|
|
d33d2a |
if (b0a0 <= 0 && a0b1 <= 0 && b1a1 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_b0_a0_b1_a1;
|
|
|
d33d2a |
if (b1a0 <= 0 && /* a0a1 */ a1b0 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_b1_a0_a1_b0;
|
|
|
d33d2a |
if (b1a0 <= 0 && a0b0 <= 0 && b0a1 <= 0)
|
|
|
d33d2a |
return IntersectionType.Along_b1_a0_b0_a1;
|
|
|
d33d2a |
|
|
|
d33d2a |
return IntersectionType.None;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
if (a0.x == b0.x && a0.y == b0.y)
|
|
|
d33d2a |
return IntersectionType.Touch_a0_b0;
|
|
|
d33d2a |
if (a0.x == b1.x && a0.y == b1.y)
|
|
|
d33d2a |
return IntersectionType.Touch_a0_b1;
|
|
|
d33d2a |
if (a1.x == b0.x && a1.y == b0.y)
|
|
|
d33d2a |
return IntersectionType.Touch_a1_b0;
|
|
|
d33d2a |
if (a1.x == b1.x && a1.y == b1.y)
|
|
|
d33d2a |
return IntersectionType.Touch_a1_b1;
|
|
|
d33d2a |
|
|
|
d33d2a |
long numeratorX = (long)da.x*((long)b1.y*(long)b0.x - (long)b0.y*(long)b1.x)
|
|
|
d33d2a |
- (long)db.x*((long)a1.y*(long)a0.x - (long)a0.y*(long)a1.x);
|
|
|
d33d2a |
long numeratorY = (long)db.y*((long)a1.x*(long)a0.y - (long)a0.x*(long)a1.y)
|
|
|
d33d2a |
- (long)da.y*((long)b1.x*(long)b0.y - (long)b0.x*(long)b1.y);
|
|
|
d33d2a |
VectorInt p = new VectorInt((int)(numeratorX/divider), (int)(numeratorY/divider));
|
|
|
d33d2a |
if (compare(p, a0, a0, a1) < 0 || compare(p, a1, a0, a1) > 0)
|
|
|
d33d2a |
return IntersectionType.None;
|
|
|
d33d2a |
|
|
|
d33d2a |
if (p.x == a0.x && p.y == a0.y)
|
|
|
d33d2a |
return IntersectionType.Touch_a0;
|
|
|
d33d2a |
if (p.x == a1.x && p.y == a1.y)
|
|
|
d33d2a |
return IntersectionType.Touch_a1;
|
|
|
d33d2a |
if (p.x == b0.x && p.y == b0.y)
|
|
|
d33d2a |
return IntersectionType.Touch_b0;
|
|
|
d33d2a |
if (p.x == b1.x && p.y == b1.y)
|
|
|
d33d2a |
return IntersectionType.Touch_b1;
|
|
|
d33d2a |
|
|
|
d33d2a |
c = p;
|
|
|
d33d2a |
return IntersectionType.Cross;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
public void findIntersections() {
|
|
|
d33d2a |
bool retry = true;
|
|
|
d33d2a |
while(retry) {
|
|
|
d33d2a |
retry = false;
|
|
|
d33d2a |
|
|
|
d33d2a |
// remove empty contours
|
|
|
9f4b34 |
for(Contour contour = contours.getFirst(); !retry && contour != null; contour = contour.shape.getNextLinear()) {
|
|
|
d33d2a |
if (contour.forward.getCount() < 3 || contour.backward.getCount() < 3) {
|
|
|
d33d2a |
while(contour.forward.getFirst() != null)
|
|
|
d33d2a |
contour.forward.getFirst().unlink();
|
|
|
d33d2a |
while(contour.backward.getFirst() != null)
|
|
|
d33d2a |
contour.backward.getFirst().unlink();
|
|
|
d33d2a |
contour.shape.unlink();
|
|
|
d33d2a |
retry = true;
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
if (retry) continue;
|
|
|
d33d2a |
|
|
|
d33d2a |
// remove empty positions
|
|
|
9f4b34 |
for(Position position = positions.getFirst(); !retry && position != null; position = position.shape.getNextLinear()) {
|
|
|
d33d2a |
if (position.links.empty()) {
|
|
|
d33d2a |
position.shape.unlink();
|
|
|
d33d2a |
retry = true;
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
if (retry) continue;
|
|
|
d33d2a |
|
|
|
d33d2a |
// merge positions
|
|
|
9f4b34 |
for(Position positionA = positions.getFirst(); !retry && positionA != null; positionA = positionA.shape.getNextLinear()) {
|
|
|
9f4b34 |
for(Position positionB = positionA.shape.getNextLinear(); !retry && positionB != null; positionB = positionB.shape.getNextLinear()) {
|
|
|
d33d2a |
if (positionA.x == positionB.x && positionA.y == positionB.y) {
|
|
|
d33d2a |
while(positionB.links.getFirst() != null)
|
|
|
d33d2a |
positionB.links.getFirst().position.insertBack(positionA.links);
|
|
|
d33d2a |
positionB.shape.unlink();
|
|
|
d33d2a |
retry = true;
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
if (retry) continue;
|
|
|
d33d2a |
|
|
|
d33d2a |
// remove zero-length links
|
|
|
9f4b34 |
for(Link linkA0 = links.getFirst(); !retry && linkA0 != null; linkA0 = linkA0.shape.getNextLinear()) {
|
|
|
d33d2a |
Link linkA1 = linkA0.contour.getNext();
|
|
|
d33d2a |
if (linkA0.position.getParent() == linkA1.position.getParent()) {
|
|
|
d33d2a |
linkA1.unlink();
|
|
|
d33d2a |
retry = true;
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
if (retry) continue;
|
|
|
d33d2a |
|
|
|
d33d2a |
// check intersections
|
|
|
9f4b34 |
for(Link linkA0 = links.getFirst(); !retry && linkA0 != null; linkA0 = linkA0.shape.getNextLinear()) {
|
|
|
d33d2a |
Link linkA1 = linkA0.contour.getNext();
|
|
|
9f4b34 |
for(Link linkB0 = links.getFirst(); !retry && linkB0 != null; linkB0 = linkB0.shape.getNextLinear()) {
|
|
|
d33d2a |
Link linkB1 = linkB0.contour.getNext();
|
|
|
d33d2a |
VectorInt cross = new VectorInt(0, 0);
|
|
|
d33d2a |
Position position;
|
|
|
d33d2a |
retry = true;
|
|
|
d33d2a |
switch( findIntersection( linkA0.position.getParent().toVectorInt(),
|
|
|
d33d2a |
linkA1.position.getParent().toVectorInt(),
|
|
|
d33d2a |
linkB0.position.getParent().toVectorInt(),
|
|
|
d33d2a |
linkB1.position.getParent().toVectorInt(),
|
|
|
d33d2a |
out cross ))
|
|
|
d33d2a |
{
|
|
|
d33d2a |
case IntersectionType.Cross:
|
|
|
d33d2a |
position = new Position(cross.x, cross.y);
|
|
|
d33d2a |
position.shape.insertBack(positions);
|
|
|
d33d2a |
linkA0.createSplitAfter(position);
|
|
|
d33d2a |
linkB0.createSplitAfter(position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Touch_a0:
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA0.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Touch_a1:
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA1.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Touch_b0:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB0.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Touch_b1:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB1.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_a0_b0_a1_b1:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB0.position);
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA1.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_a0_b0_b1_a1:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB0.position)
|
|
|
d33d2a |
.createSplitAfter(linkB1.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_a0_b1_a1_b0:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB1.position);
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA1.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_a0_b1_b0_a1:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB1.position)
|
|
|
d33d2a |
.createSplitAfter(linkB0.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_b0_a0_a1_b1:
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA0.position)
|
|
|
d33d2a |
.createSplitAfter(linkA1.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_b0_a0_b1_a1:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB1.position);
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA0.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_b1_a0_a1_b0:
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA1.position)
|
|
|
d33d2a |
.createSplitAfter(linkA0.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
case IntersectionType.Along_b1_a0_b0_a1:
|
|
|
d33d2a |
linkA0.createSplitAfter(linkB0.position);
|
|
|
d33d2a |
linkB0.createSplitAfter(linkA0.position);
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
default:
|
|
|
d33d2a |
retry = false;
|
|
|
d33d2a |
break;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
if (retry) continue;
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
9f4b34 |
static bool compareAngle(VectorInt a, VectorInt b, VectorInt c) {
|
|
|
9f4b34 |
int d = a.x*c.y - a.y*c.x;
|
|
|
9f4b34 |
// angle AC < 180 deg
|
|
|
9f4b34 |
if (d > 0)
|
|
|
9f4b34 |
return a.x*b.y >= a.y*b.x && c.x*b.y <= c.y*b.x;
|
|
|
9f4b34 |
// angle AC > 180 deg
|
|
|
9f4b34 |
if (d < 0)
|
|
|
9f4b34 |
return a.x*b.y >= a.y*b.x || c.x*b.y <= c.y*b.x;
|
|
|
9f4b34 |
// angle AC == 180 deg
|
|
|
9f4b34 |
if ((a.x >= 0) != (c.x >= 0) || (a.y >= 0) != (c.y >= 0))
|
|
|
9f4b34 |
return a.x*b.y >= a.y*b.x;
|
|
|
9f4b34 |
// angle AC == 0 deg
|
|
|
9f4b34 |
return true;
|
|
|
9f4b34 |
}
|
|
|
9f4b34 |
|
|
|
9f4b34 |
static bool compareAngle(VectorInt center, VectorInt a, VectorInt b, VectorInt c) {
|
|
|
9f4b34 |
return compareAngle( new VectorInt(a.x - center.x, a.y - center.y),
|
|
|
9f4b34 |
new VectorInt(b.x - center.x, b.y - center.y),
|
|
|
9f4b34 |
new VectorInt(c.x - center.x, c.y - center.y) );
|
|
|
9f4b34 |
}
|
|
|
9f4b34 |
|
|
|
9f4b34 |
void sortLinksAtPosition(Position position) {
|
|
|
9f4b34 |
if (position.links.getCount() < 3) return;
|
|
|
9f4b34 |
Link first = position.links.getFirst();
|
|
|
9f4b34 |
Link linkA = first;
|
|
|
9f4b34 |
while (true) {
|
|
|
9f4b34 |
Link linkB = linkA.position.getNext();
|
|
|
9f4b34 |
Link linkC = linkB.position.getNext();
|
|
|
9f4b34 |
if ( !compareAngle(
|
|
|
9f4b34 |
position.toVectorInt(),
|
|
|
9f4b34 |
linkA.contour.getNext().position.getParent().toVectorInt(),
|
|
|
9f4b34 |
linkB.contour.getNext().position.getParent().toVectorInt(),
|
|
|
9f4b34 |
linkC.contour.getNext().position.getParent().toVectorInt() ))
|
|
|
9f4b34 |
{
|
|
|
9f4b34 |
linkB.position.swapWith(linkC.position);
|
|
|
9f4b34 |
first = linkA = linkC;
|
|
|
9f4b34 |
continue;
|
|
|
9f4b34 |
}
|
|
|
9f4b34 |
linkA = linkB;
|
|
|
9f4b34 |
if (linkA == first) break;
|
|
|
9f4b34 |
};
|
|
|
9f4b34 |
}
|
|
|
9f4b34 |
|
|
|
d33d2a |
void sortLinksAtPositions() {
|
|
|
9f4b34 |
for(Position position = positions.getFirst(); position != null; position = position.shape.getNextLinear())
|
|
|
9f4b34 |
sortLinksAtPosition(position);
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|
|
|
d33d2a |
void optimizeContours() {
|
|
|
d33d2a |
// TODO:
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
}
|
|
|
d33d2a |
|