Skip to content

Commit

Permalink
Merge pull request #163 from alexbol99/159-cut-polygon-with-multiline
Browse files Browse the repository at this point in the history
Cut polygon with multiline
  • Loading branch information
alexbol99 authored Mar 29, 2024
2 parents b89e303 + 4f967d4 commit 565ae09
Show file tree
Hide file tree
Showing 67 changed files with 1,387 additions and 1,200 deletions.
376 changes: 202 additions & 174 deletions dist/main.cjs

Large diffs are not rendered by default.

376 changes: 201 additions & 175 deletions dist/main.mjs

Large diffs are not rendered by default.

376 changes: 202 additions & 174 deletions dist/main.umd.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Arc.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Box.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Circle.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/CircularLinkedList.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/DE9IM.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/Edge.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Errors.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Face.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Inversion.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Line.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/LinkedList.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Matrix.html

Large diffs are not rendered by default.

116 changes: 100 additions & 16 deletions docs/Multiline.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/PlanarSet.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Point.html

Large diffs are not rendered by default.

232 changes: 24 additions & 208 deletions docs/Polygon.html

Large diffs are not rendered by default.

189 changes: 174 additions & 15 deletions docs/Ray.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Segment.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Shape.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Vector.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/algorithms_boolean_op.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/algorithms_distance.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/algorithms_ray_shooting.js.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/algorithms_relation.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_arc.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_box.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_circle.js.html

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions docs/classes_edge.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_face.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_inversion.js.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/classes_line.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_matrix.js.html

Large diffs are not rendered by default.

39 changes: 33 additions & 6 deletions docs/classes_multiline.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_point.js.html

Large diffs are not rendered by default.

176 changes: 57 additions & 119 deletions docs/classes_polygon.js.html

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions docs/classes_ray.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_segment.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_shape.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_vector.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/data_structures_circular_linked_list.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/data_structures_de9im.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/data_structures_linked_list.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/data_structures_planar_set.js.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/global.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/module-BooleanOperations.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/module-RayShoot.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/module-Relation.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/utils_constants.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/utils_errors.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/utils_utils.js.html

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,12 @@ declare namespace Flatten {
readonly end: Point;
readonly length: number;
readonly box: Box;
readonly isSegment: boolean;
readonly isArc: boolean;
readonly isLine: boolean;
readonly isRay: boolean

// public methods
isSegment() : boolean;
isArc() : boolean;
contains(pt: Point): boolean;
middle(): Point;
pointAtLength(length: number): Point|null;
Expand Down Expand Up @@ -536,7 +538,6 @@ declare namespace Flatten {
addVertex(pt: Point, edge: PolygonEdge): PolygonEdge;
removeEndVertex(edge: Edge): void;
cut(multiline: Multiline): Polygon[];
cutFace(pt1: Point, pt2: Point): [Polygon, Polygon];
cutWithLine(line: Line): Polygon;
findEdgeByPoint(pt: Point): PolygonEdge;
splitToIslands() : Polygon[];
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export {SmartIntersections};
Flatten.BooleanOperations = BooleanOperations;
Flatten.Relations = Relations;

export {CCW, CW, ORIENTATION, INSIDE, OUTSIDE, BOUNDARY} from './src/utils/constants';
export {CCW, CW, ORIENTATION, INSIDE, OUTSIDE, BOUNDARY, OVERLAP_SAME, OVERLAP_OPPOSITE} from './src/utils/constants';

export default Flatten;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flatten-js/core",
"version": "1.4.8",
"version": "1.5.0",
"description": "Javascript library for 2d geometry",
"main": "dist/main.cjs",
"umd:main": "dist/main.umd.js",
Expand Down
43 changes: 32 additions & 11 deletions src/algorithms/intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,19 +424,23 @@ export function intersectArc2Box(arc, box) {
}

export function intersectEdge2Segment(edge, segment) {
return edge.isSegment() ? intersectSegment2Segment(edge.shape, segment) : intersectSegment2Arc(segment, edge.shape);
return edge.isSegment ? intersectSegment2Segment(edge.shape, segment) : intersectSegment2Arc(segment, edge.shape);
}

export function intersectEdge2Arc(edge, arc) {
return edge.isSegment() ? intersectSegment2Arc(edge.shape, arc) : intersectArc2Arc(edge.shape, arc);
return edge.isSegment ? intersectSegment2Arc(edge.shape, arc) : intersectArc2Arc(edge.shape, arc);
}

export function intersectEdge2Line(edge, line) {
return edge.isSegment() ? intersectSegment2Line(edge.shape, line) : intersectLine2Arc(line, edge.shape);
return edge.isSegment ? intersectSegment2Line(edge.shape, line) : intersectLine2Arc(line, edge.shape);
}

export function intersectEdge2Ray(edge, ray) {
return edge.isSegment ? intersectRay2Segment(ray, edge.shape) : intersectRay2Arc(ray, edge.shape);
}

export function intersectEdge2Circle(edge, circle) {
return edge.isSegment() ? intersectSegment2Circle(edge.shape, circle) : intersectArc2Circle(edge.shape, circle);
return edge.isSegment ? intersectSegment2Circle(edge.shape, circle) : intersectArc2Circle(edge.shape, circle);
}

export function intersectSegment2Polygon(segment, polygon) {
Expand Down Expand Up @@ -498,11 +502,19 @@ export function intersectCircle2Polygon(circle, polygon) {
}

export function intersectEdge2Edge(edge1, edge2) {
const shape1 = edge1.shape;
const shape2 = edge2.shape;
return edge1.isSegment() ?
(edge2.isSegment() ? intersectSegment2Segment(shape1, shape2) : intersectSegment2Arc(shape1, shape2)) :
(edge2.isSegment() ? intersectSegment2Arc(shape2, shape1) : intersectArc2Arc(shape1, shape2));
if (edge1.isSegment) {
return intersectEdge2Segment(edge2, edge1.shape)
}
else if (edge1.isArc) {
return intersectEdge2Arc(edge2, edge1.shape)
}
else if (edge1.isLine) {
return intersectEdge2Line(edge2, edge1.shape)
}
else if (edge1.isRay) {
return intersectEdge2Ray(edge2, edge1.shape)
}
return []
}

export function intersectEdge2Polygon(edge, polygon) {
Expand All @@ -515,8 +527,17 @@ export function intersectEdge2Polygon(edge, polygon) {
let resp_edges = polygon.edges.search(edge.shape.box);

for (let resp_edge of resp_edges) {
for (let pt of intersectEdge2Edge(edge, resp_edge)) {
ip.push(pt);
if (resp_edge.isSegment) {
ip = [...ip, ...intersectSegment2Polygon(resp_edge, polygon)]
}
else if (resp_edge.isArc) {
ip = [...ip, ...intersectArc2Polygon(resp_edge, polygon)]
}
else if (resp_edge.isLine) {
ip = [...ip, ...intersectLine2Polygon(resp_edge, polygon)]
}
else if (resp_edge.isRay) {
ip = [...ip, ...intersectRay2Polygon(resp_edge, polygon)]
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/algorithms/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function relateLine2Circle(line,circle) {
denim.I2B = ip_sorted;
denim.I2E = [splitShapes[0], splitShapes[2]];

denim.E2I = new Flatten.Polygon([circle.toArc()]).cut(multiline);
denim.E2I = new Flatten.Polygon([circle.toArc()]).cutWithLine(line);
}

return denim;
Expand Down Expand Up @@ -245,7 +245,7 @@ function relateLine2Box(line, box) {
denim.I2B = ip_sorted;
denim.I2E = [splitShapes[0], splitShapes[2]];

denim.E2I = new Flatten.Polygon(box.toSegments()).cut(multiline);
denim.E2I = new Flatten.Polygon(box.toSegments()).cutWithLine(line);
}
}
return denim;
Expand All @@ -265,7 +265,7 @@ function relateLine2Polygon(line, polygon) {
denim.I2B = [...multiline].slice(1).map( (edge) => edge.bv === Flatten.BOUNDARY ? edge.shape : edge.shape.start );
denim.I2E = [...multiline].filter(edge => edge.bv === Flatten.OUTSIDE).map(edge => edge.shape);

denim.E2I = polygon.cut(multiline);
denim.E2I = polygon.cutWithLine(line);

return denim;
}
Expand Down
12 changes: 10 additions & 2 deletions src/classes/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,22 @@ export class Edge {
return this.shape.box;
}

isSegment() {
get isSegment() {
return this.shape instanceof Flatten.Segment;
}

isArc() {
get isArc() {
return this.shape instanceof Flatten.Arc;
}

get isLine() {
return this.shape instanceof Flatten.Line;
}

get isRay() {
return this.shape instanceof Flatten.Ray
}

/**
* Get middle point of the edge
* @returns {Point}
Expand Down
2 changes: 1 addition & 1 deletion src/classes/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class Line extends Shape {
*/
split(pt) {
if (pt instanceof Flatten.Point) {
return [new Flatten.Ray(pt, this.norm.invert()), new Flatten.Ray(pt, this.norm)]
return [new Flatten.Ray(pt, this.norm), new Flatten.Ray(pt, this.norm)]
}
else {
let multiline = new Flatten.Multiline([this]);
Expand Down
35 changes: 31 additions & 4 deletions src/classes/multiline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import Flatten from '../flatten';
import LinkedList from '../data_structures/linked_list';
import {END_VERTEX, NOT_VERTEX, START_VERTEX} from "../utils/constants";
import {convertToString} from "../utils/attributes";

/**
Expand All @@ -17,10 +16,10 @@ export class Multiline extends LinkedList {
return;
}

if (args.length == 1) {
if (args.length === 1) {
if (args[0] instanceof Array) {
let shapes = args[0];
if (shapes.length == 0)
if (shapes.length === 0)
return;

// TODO: more strict validation:
Expand All @@ -37,6 +36,8 @@ export class Multiline extends LinkedList {
let edge = new Flatten.Edge(shape);
this.append(edge);
}

this.setArcLength()
}
}
}
Expand All @@ -54,7 +55,7 @@ export class Multiline extends LinkedList {
* @returns {Box}
*/
get box() {
return this.edges.reduce( (acc,edge) => acc = acc.merge(edge.box), new Flatten.Box() );
return this.edges.reduce( (acc,edge) => acc.merge(edge.box), new Flatten.Box() );
}

/**
Expand All @@ -75,6 +76,24 @@ export 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 @@ -103,6 +122,14 @@ export class Multiline extends LinkedList {
return newEdge;
}

getChain(edgeFrom, edgeTo) {
let edges = []
for (let edge = edgeFrom; edge !== edgeTo.next; edge = edge.next) {
edges.push(edge)
}
return edges
}

/**
* Split edges of multiline with intersection points and return mutated multiline
* @param {Point[]} ip - array of points to be added as new vertices
Expand Down
Loading

0 comments on commit 565ae09

Please sign in to comment.