Skip to content

Commit

Permalink
Merge pull request #143 from alexbol99/feature_request_remove_vertex
Browse files Browse the repository at this point in the history
 Add method removeEndVertex(edge) to Polygon
  • Loading branch information
alexbol99 authored Jul 1, 2023
2 parents fc94712 + 9bd6483 commit 5dc7930
Show file tree
Hide file tree
Showing 58 changed files with 4,928 additions and 4,137 deletions.
166 changes: 104 additions & 62 deletions .idea/workspace.xml

Large diffs are not rendered by default.

52 changes: 39 additions & 13 deletions dist/main.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, '__esModule', { value: true });

/**
* Global constant CCW defines counter clockwise direction of arc
* Global constant CCW defines counterclockwise direction of arc
* @type {boolean}
*/
const CCW = true;
Expand Down Expand Up @@ -6586,19 +6586,19 @@ class Face extends CircularLinkedList {
this._box = undefined; // new Box();
this._orientation = undefined;

if (args.length == 0) {
if (args.length === 0) {
return;
}

/* If passed an array it supposed to be:
1) array of shapes that performs close loop or
2) array of points that performs set of vertices
*/
if (args.length == 1) {
if (args.length === 1) {
if (args[0] instanceof Array) {
// let argsArray = args[0];
let shapes = args[0]; // argsArray[0];
if (shapes.length == 0)
if (shapes.length === 0)
return;

/* array of Flatten.Points */
Expand Down Expand Up @@ -6646,7 +6646,7 @@ class Face extends CircularLinkedList {
}
/* Instantiate face from a circle in CCW orientation */
else if (args[0] instanceof Flatten.Circle) {
this.shapes2face(polygon.edges, [args[0].toArc(Flatten.CCW)]);
this.shapes2face(polygon.edges, [args[0].toArc(CCW)]);
}
/* Instantiate face from a box in CCW orientation */
else if (args[0] instanceof Flatten.Box) {
Expand All @@ -6662,7 +6662,7 @@ class Face extends CircularLinkedList {
/* If passed two edges, consider them as start and end of the face loop */
/* THIS METHOD WILL BE USED BY BOOLEAN OPERATIONS */
/* Assume that edges already copied to polygon.edges set in the clip algorithm !!! */
if (args.length == 2 && args[0] instanceof Flatten.Edge && args[1] instanceof Flatten.Edge) {
if (args.length === 2 && args[0] instanceof Flatten.Edge && args[1] instanceof Flatten.Edge) {
this.first = args[0]; // first edge in face or undefined
this.last = args[1]; // last edge in face or undefined
this.last.next = this.first;
Expand Down Expand Up @@ -6794,10 +6794,24 @@ class Face extends CircularLinkedList {
return this;
}

/**
* Merge current edge with the next edge. Given edge will be extended,
* next edge after it will be removed. The distortion of the polygon
* is on the responsibility of the user of this method
* @param {Edge} edge - edge to be extended
* @returns {Face}
*/
merge_with_next_edge(edge) {
edge.shape.end.x = edge.next.shape.end.x;
edge.shape.end.y = edge.next.shape.end.y;
this.remove(edge.next);
return this;
}

/**
* Reverse orientation of the face: first edge become last and vice a verse,
* all edges starts and ends swapped, direction of arcs inverted. If face was oriented
* clockwise, it becomes counter clockwise and vice versa
* clockwise, it becomes counterclockwise and vice versa
*/
reverse() {
// collect edges in revert order with reverted shapes
Expand Down Expand Up @@ -6892,7 +6906,7 @@ class Face extends CircularLinkedList {
* Return face orientation: one of Flatten.ORIENTATION.CCW, Flatten.ORIENTATION.CW, Flatten.ORIENTATION.NOT_ORIENTABLE <br/>
* According to Green theorem the area of a closed curve may be calculated as double integral,
* and the sign of the integral will be defined by the direction of the curve.
* When the integral ("signed area") will be negative, direction is counter clockwise,
* When the integral ("signed area") will be negative, direction is counterclockwise,
* when positive - clockwise and when it is zero, polygon is not orientable.
* See {@link https://mathinsight.org/greens_theorem_find_area}
* @returns {number}
Expand All @@ -6901,11 +6915,11 @@ class Face extends CircularLinkedList {
if (this._orientation === undefined) {
let area = this.signedArea();
if (Flatten.Utils.EQ_0(area)) {
this._orientation = Flatten.ORIENTATION.NOT_ORIENTABLE;
this._orientation = ORIENTATION.NOT_ORIENTABLE;
} else if (Flatten.Utils.LT(area, 0)) {
this._orientation = Flatten.ORIENTATION.CCW;
this._orientation = ORIENTATION.CCW;
} else {
this._orientation = Flatten.ORIENTATION.CW;
this._orientation = ORIENTATION.CW;
}
}
return this._orientation;
Expand All @@ -6915,12 +6929,12 @@ class Face extends CircularLinkedList {
* Returns true if face of the polygon is simple (no self-intersection points found)
* NOTE: this method is incomplete because it does not exclude touching points.
* Self intersection test should check if polygon change orientation in the test point.
* @param {Edges} edges - reference to polygon.edges to provide search index
* @param {PlanarSet} edges - reference to polygon edges to provide search index
* @returns {boolean}
*/
isSimple(edges) {
let ip = Face.getSelfIntersections(this, edges, true);
return ip.length == 0;
return ip.length === 0;
}

static getSelfIntersections(face, edges, exitOnFirst = false) {
Expand Down Expand Up @@ -7019,6 +7033,7 @@ class Face extends CircularLinkedList {
}

}

Flatten.Face = Face;

/**
Expand Down Expand Up @@ -7496,6 +7511,17 @@ class Polygon {
return newEdge;
}

/**
* Merge given edge with next edge and remove vertex between them
* @param {Edge} edge
*/
removeEndVertex(edge) {
const edge_next = edge.next;
if (edge_next === edge) return
edge.face.merge_with_next_edge(edge);
this.edges.delete(edge_next);
}

/**
* Cut polygon with multiline and return array of new polygons
* Multiline should be constructed from a line with intersection point, see notebook:
Expand Down
52 changes: 39 additions & 13 deletions dist/main.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Global constant CCW defines counter clockwise direction of arc
* Global constant CCW defines counterclockwise direction of arc
* @type {boolean}
*/
const CCW = true;
Expand Down Expand Up @@ -6582,19 +6582,19 @@ class Face extends CircularLinkedList {
this._box = undefined; // new Box();
this._orientation = undefined;

if (args.length == 0) {
if (args.length === 0) {
return;
}

/* If passed an array it supposed to be:
1) array of shapes that performs close loop or
2) array of points that performs set of vertices
*/
if (args.length == 1) {
if (args.length === 1) {
if (args[0] instanceof Array) {
// let argsArray = args[0];
let shapes = args[0]; // argsArray[0];
if (shapes.length == 0)
if (shapes.length === 0)
return;

/* array of Flatten.Points */
Expand Down Expand Up @@ -6642,7 +6642,7 @@ class Face extends CircularLinkedList {
}
/* Instantiate face from a circle in CCW orientation */
else if (args[0] instanceof Flatten.Circle) {
this.shapes2face(polygon.edges, [args[0].toArc(Flatten.CCW)]);
this.shapes2face(polygon.edges, [args[0].toArc(CCW)]);
}
/* Instantiate face from a box in CCW orientation */
else if (args[0] instanceof Flatten.Box) {
Expand All @@ -6658,7 +6658,7 @@ class Face extends CircularLinkedList {
/* If passed two edges, consider them as start and end of the face loop */
/* THIS METHOD WILL BE USED BY BOOLEAN OPERATIONS */
/* Assume that edges already copied to polygon.edges set in the clip algorithm !!! */
if (args.length == 2 && args[0] instanceof Flatten.Edge && args[1] instanceof Flatten.Edge) {
if (args.length === 2 && args[0] instanceof Flatten.Edge && args[1] instanceof Flatten.Edge) {
this.first = args[0]; // first edge in face or undefined
this.last = args[1]; // last edge in face or undefined
this.last.next = this.first;
Expand Down Expand Up @@ -6790,10 +6790,24 @@ class Face extends CircularLinkedList {
return this;
}

/**
* Merge current edge with the next edge. Given edge will be extended,
* next edge after it will be removed. The distortion of the polygon
* is on the responsibility of the user of this method
* @param {Edge} edge - edge to be extended
* @returns {Face}
*/
merge_with_next_edge(edge) {
edge.shape.end.x = edge.next.shape.end.x;
edge.shape.end.y = edge.next.shape.end.y;
this.remove(edge.next);
return this;
}

/**
* Reverse orientation of the face: first edge become last and vice a verse,
* all edges starts and ends swapped, direction of arcs inverted. If face was oriented
* clockwise, it becomes counter clockwise and vice versa
* clockwise, it becomes counterclockwise and vice versa
*/
reverse() {
// collect edges in revert order with reverted shapes
Expand Down Expand Up @@ -6888,7 +6902,7 @@ class Face extends CircularLinkedList {
* Return face orientation: one of Flatten.ORIENTATION.CCW, Flatten.ORIENTATION.CW, Flatten.ORIENTATION.NOT_ORIENTABLE <br/>
* According to Green theorem the area of a closed curve may be calculated as double integral,
* and the sign of the integral will be defined by the direction of the curve.
* When the integral ("signed area") will be negative, direction is counter clockwise,
* When the integral ("signed area") will be negative, direction is counterclockwise,
* when positive - clockwise and when it is zero, polygon is not orientable.
* See {@link https://mathinsight.org/greens_theorem_find_area}
* @returns {number}
Expand All @@ -6897,11 +6911,11 @@ class Face extends CircularLinkedList {
if (this._orientation === undefined) {
let area = this.signedArea();
if (Flatten.Utils.EQ_0(area)) {
this._orientation = Flatten.ORIENTATION.NOT_ORIENTABLE;
this._orientation = ORIENTATION.NOT_ORIENTABLE;
} else if (Flatten.Utils.LT(area, 0)) {
this._orientation = Flatten.ORIENTATION.CCW;
this._orientation = ORIENTATION.CCW;
} else {
this._orientation = Flatten.ORIENTATION.CW;
this._orientation = ORIENTATION.CW;
}
}
return this._orientation;
Expand All @@ -6911,12 +6925,12 @@ class Face extends CircularLinkedList {
* Returns true if face of the polygon is simple (no self-intersection points found)
* NOTE: this method is incomplete because it does not exclude touching points.
* Self intersection test should check if polygon change orientation in the test point.
* @param {Edges} edges - reference to polygon.edges to provide search index
* @param {PlanarSet} edges - reference to polygon edges to provide search index
* @returns {boolean}
*/
isSimple(edges) {
let ip = Face.getSelfIntersections(this, edges, true);
return ip.length == 0;
return ip.length === 0;
}

static getSelfIntersections(face, edges, exitOnFirst = false) {
Expand Down Expand Up @@ -7015,6 +7029,7 @@ class Face extends CircularLinkedList {
}

}

Flatten.Face = Face;

/**
Expand Down Expand Up @@ -7492,6 +7507,17 @@ class Polygon {
return newEdge;
}

/**
* Merge given edge with next edge and remove vertex between them
* @param {Edge} edge
*/
removeEndVertex(edge) {
const edge_next = edge.next;
if (edge_next === edge) return
edge.face.merge_with_next_edge(edge);
this.edges.delete(edge_next);
}

/**
* Cut polygon with multiline and return array of new polygons
* Multiline should be constructed from a line with intersection point, see notebook:
Expand Down
Loading

0 comments on commit 5dc7930

Please sign in to comment.