Skip to content

Commit

Permalink
Add case of finite multiline that does not start or end with ray
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbol99 committed Mar 29, 2024
1 parent 25b5da5 commit 4c85cec
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 294 deletions.
127 changes: 54 additions & 73 deletions dist/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ function addToIntPoints(edge, pt, int_points)
arc_length = shapes[0].coord(pt);
}
else {
arc_length = (is_vertex & END_VERTEX$1) && edge.next.arc_length === 0 ? 0 : edge.arc_length + len;
arc_length = (is_vertex & END_VERTEX$1) && edge.next && edge.next.arc_length === 0 ?
0 :
edge.arc_length + len;
}

int_points.push({
Expand Down Expand Up @@ -594,28 +596,32 @@ function filterDuplicatedIntersections(intersections)
function initializeInclusionFlags(int_points)
{
for (let int_point of int_points) {
int_point.edge_before.bvStart = undefined;
int_point.edge_before.bvEnd = undefined;
int_point.edge_before.bv = undefined;
int_point.edge_before.overlap = undefined;
if (int_point.edge_before) {
int_point.edge_before.bvStart = undefined;
int_point.edge_before.bvEnd = undefined;
int_point.edge_before.bv = undefined;
int_point.edge_before.overlap = undefined;
}

int_point.edge_after.bvStart = undefined;
int_point.edge_after.bvEnd = undefined;
int_point.edge_after.bv = undefined;
int_point.edge_after.overlap = undefined;
if (int_point.edge_after) {
int_point.edge_after.bvStart = undefined;
int_point.edge_after.bvEnd = undefined;
int_point.edge_after.bv = undefined;
int_point.edge_after.overlap = undefined;
}
}

for (let int_point of int_points) {
int_point.edge_before.bvEnd = BOUNDARY$1;
int_point.edge_after.bvStart = BOUNDARY$1;
if (int_point.edge_before) int_point.edge_before.bvEnd = BOUNDARY$1;
if (int_point.edge_after) int_point.edge_after.bvStart = BOUNDARY$1;
}
}

function calculateInclusionFlags(int_points, polygon)
{
for (let int_point of int_points) {
int_point.edge_before.setInclusion(polygon);
int_point.edge_after.setInclusion(polygon);
if (int_point.edge_before) int_point.edge_before.setInclusion(polygon);
if (int_point.edge_after) int_point.edge_after.setInclusion(polygon);
}
}

Expand Down Expand Up @@ -740,8 +746,14 @@ function splitByIntersections(polygon, int_points)
}

if (int_point.is_vertex & START_VERTEX$1) { // nothing to split
int_point.edge_before = edge.prev;
int_point.is_vertex = END_VERTEX$1;
if (edge.prev) {
int_point.edge_before = edge.prev; // polygon
int_point.is_vertex = END_VERTEX$1;
}
else { // multiline start vertex
int_point.edge_after = int_point.edge_before;
int_point.edge_before = edge.prev;
}
continue;
}
if (int_point.is_vertex & END_VERTEX$1) { // nothing to split
Expand All @@ -753,7 +765,9 @@ function splitByIntersections(polygon, int_points)
}

for (let int_point of int_points) {
int_point.edge_after = int_point.edge_before.next;
if (int_point.edge_before) {
int_point.edge_after = int_point.edge_before.next;
}
}
}

Expand Down Expand Up @@ -2370,6 +2384,8 @@ class Multiline extends LinkedList {
let edge = new Flatten.Edge(shape);
this.append(edge);
}

this.setArcLength();
}
}
}
Expand Down Expand Up @@ -2408,6 +2424,24 @@ class Multiline extends LinkedList {
return new Multiline(this.toShapes());
}

/**
* Set arc_length property for each of the edges in the face.
* Arc_length of the edge it the arc length from the first edge of the face
*/
setArcLength() {
for (let edge of this) {
this.setOneEdgeArcLength(edge);
}
}

setOneEdgeArcLength(edge) {
if (edge === this.first) {
edge.arc_length = 0.0;
} else {
edge.arc_length = edge.prev.arc_length + edge.prev.length;
}
}

/**
* Split edge and add new vertex, return new edge inserted
* @param {Point} pt - point on edge that will be added as new vertex
Expand Down Expand Up @@ -7768,7 +7802,6 @@ class Polygon {
}
}


// No intersections - return a copy of the original polygon
if (intersections.int_points1.length === 0)
return newPoly;
Expand Down Expand Up @@ -7796,7 +7829,8 @@ class Polygon {

// filter intersections between two edges that got same inclusion flag
for (let int_point1 of intersections.int_points1_sorted) {
if (int_point1.edge_before.bv === int_point1.edge_after.bv) {
if (int_point1.edge_before && int_point1.edge_after &&
int_point1.edge_before.bv === int_point1.edge_after.bv) {
intersections.int_points2[int_point1.id] = -1; // to be filtered out
int_point1.id = -1; // to be filtered out
}
Expand All @@ -7812,13 +7846,13 @@ class Polygon {
intersections.int_points1_sorted = getSortedArray(intersections.int_points1);
intersections.int_points2_sorted = getSortedArray(intersections.int_points2);

// Add 2 new inner edges between intersection points
// Add new inner edges between intersection points
let int_point1_prev;
let int_point1_curr;
for (let i = 1; i < intersections.int_points1_sorted.length; i++) {
int_point1_curr = intersections.int_points1_sorted[i];
int_point1_prev = intersections.int_points1_sorted[i-1];
if (int_point1_curr.edge_before.bv === INSIDE$2) {
if (int_point1_curr.edge_before && int_point1_curr.edge_before.bv === INSIDE$2) {
let edgeFrom = int_point1_prev.edge_after;
let edgeTo = int_point1_curr.edge_before;
let newEdges = multiline.getChain(edgeFrom, edgeTo);
Expand All @@ -7842,59 +7876,6 @@ class Polygon {
return newPoly
}

/**
* Cut face of polygon with a segment between two points and create two new polygons
* Supposed that a segments between points does not intersect any other edge
* @param {Point} pt1
* @param {Point} pt2
* @returns {Polygon[]}
*/
cutFace(pt1, pt2) {
let edge1 = this.findEdgeByPoint(pt1);
let edge2 = this.findEdgeByPoint(pt2);
if (edge1.face !== edge2.face)
return [];

// Cut face into two and create new polygon with two faces
let edgeBefore1 = this.addVertex(pt1, edge1);
edge2 = this.findEdgeByPoint(pt2);
let edgeBefore2 = this.addVertex(pt2, edge2);

let face = edgeBefore1.face;
let newEdge1 = new Flatten.Edge(
new Flatten.Segment(edgeBefore1.end, edgeBefore2.end)
);
let newEdge2 = new Flatten.Edge(
new Flatten.Segment(edgeBefore2.end, edgeBefore1.end)
);

// Swap links
edgeBefore1.next.prev = newEdge2;
newEdge2.next = edgeBefore1.next;

edgeBefore1.next = newEdge1;
newEdge1.prev = edgeBefore1;

edgeBefore2.next.prev = newEdge1;
newEdge1.next = edgeBefore2.next;

edgeBefore2.next = newEdge2;
newEdge2.prev = edgeBefore2;

// Insert new edge to the edges container and 2d index
this.edges.add(newEdge1);
this.edges.add(newEdge2);

// Add two new faces
let face1 = this.addFace(newEdge1, edgeBefore1);
let face2 = this.addFace(newEdge2, edgeBefore2);

// Remove old face
this.faces.delete(face);

return [face1.toPolygon(), face2.toPolygon()];
}

/**
* A special case of cut() function
* The return is a polygon cut with line
Expand Down
Loading

0 comments on commit 4c85cec

Please sign in to comment.