Skip to content

Commit

Permalink
Fix #5799 (#5800)
Browse files Browse the repository at this point in the history
This works and seems reasonable - some intersections might be
unavoidable and we wouldn't want to throw the entire graph away if it
happens.

...But I don't really know what I'm doing, as so far I failed to track
internal `GraphLayoutCore` logic.
@jeremy-rifkin would appreciate if you could take a look.
  • Loading branch information
OfekShilon committed Dec 3, 2023
1 parent 7680595 commit a33f77a
Showing 1 changed file with 10 additions and 57 deletions.
67 changes: 10 additions & 57 deletions static/graph-layout-core.ts
Expand Up @@ -66,7 +66,7 @@ type EdgeSegment = {
end: EdgeCoordinate;
horizontalOffset: number;
verticalOffset: number;
type: SegmentType; // is this point the end of a horizontal or vertical segment
type: SegmentType;
};

type Edge = {
Expand Down Expand Up @@ -607,62 +607,9 @@ export class GraphLayoutCore {
}
}
}
// Compute subrows/subcolumns
for (const segment of edge.path) {
if (segment.type === SegmentType.Vertical) {
if (segment.start.col !== segment.end.col) {
throw Error('Vertical segment changes column');
}
const col = this.edgeColumns[segment.start.col];
let inserted = false;
for (const tree of col.intervals) {
if (!tree.intersect_any([segment.start.row, segment.end.row])) {
tree.insert([segment.start.row, segment.end.row], segment);
inserted = true;
break;
}
}
if (!inserted) {
const tree = new IntervalTree<EdgeSegment>();
col.intervals.push(tree);
col.subcolumns++;
tree.insert([segment.start.row, segment.end.row], segment);
}
} else {
// horizontal
if (segment.start.row !== segment.end.row) {
throw Error('Horizontal segment changes row');
}
const row = this.edgeRows[segment.start.row];
let inserted = false;
for (const tree of row.intervals) {
if (!tree.intersect_any([segment.start.col, segment.end.col])) {
tree.insert([segment.start.col, segment.end.col], segment);
inserted = true;
break;
}
}
if (!inserted) {
const tree = new IntervalTree<EdgeSegment>();
row.intervals.push(tree);
row.subrows++;
tree.insert([segment.start.col, segment.end.col], segment);
}
}
}
}
}
// Throw everything away and do it all again, but smarter
for (const edgeColumn of this.edgeColumns) {
for (const intervalTree of edgeColumn.intervals) {
intervalTree.clear();
}
}
for (const edgeRow of this.edgeRows) {
for (const intervalTree of edgeRow.intervals) {
intervalTree.clear();
}
}

// Edge kind is the primary heuristic for subrow/column assignment
// For horizontal edges, think of left/vertical/right terminology rotated 90 degrees right
enum EdgeKind {
Expand Down Expand Up @@ -787,7 +734,10 @@ export class GraphLayoutCore {
}
}
if (!inserted) {
throw Error("Vertical segment couldn't be inserted");
const tree = new IntervalTree<EdgeSegment>();
col.intervals.push(tree);
col.subcolumns++;
tree.insert([segment.start.row, segment.end.row], segment);
}
} else {
// Horizontal
Expand All @@ -801,7 +751,10 @@ export class GraphLayoutCore {
}
}
if (!inserted) {
throw Error("Horizontal segment couldn't be inserted");
const tree = new IntervalTree<EdgeSegment>();
row.intervals.push(tree);
row.subrows++;
tree.insert([segment.start.col, segment.end.col], segment);
}
}
}
Expand Down

0 comments on commit a33f77a

Please sign in to comment.