Skip to content

Commit

Permalink
- Added Polygon::reverse method
Browse files Browse the repository at this point in the history
- BooleanOp::subtract - revert second operand
- BooleanOp test & fix
  • Loading branch information
alexbol99 committed Feb 10, 2018
1 parent 2fc74ba commit eedc822
Show file tree
Hide file tree
Showing 42 changed files with 781 additions and 563 deletions.
585 changes: 203 additions & 382 deletions .idea/workspace.xml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion algorithms/boolean_op.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = function (Flatten) {
}

static subtract(polygon1, polygon2) {
let res_poly = BooleanOp.booleanOpBinary(polygon1, polygon2, Flatten.BOOLEAN_SUBTRACT);
let wrk_poly = polygon2.reverse();
let res_poly = BooleanOp.booleanOpBinary(polygon1, wrk_poly, Flatten.BOOLEAN_SUBTRACT);
return res_poly;
}

Expand Down
10 changes: 5 additions & 5 deletions algorithms/distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ module.exports = function(Flatten) {

let dist_tmp, segment_tmp;
[dist_tmp, segment_tmp] = Distance.point2segment(arc.start, seg);
dist_and_segment.push([dist_tmp, segment_tmp.swap()]);
dist_and_segment.push([dist_tmp, segment_tmp.reverse()]);

[dist_tmp, segment_tmp] = Distance.point2segment(arc.end, seg);
dist_and_segment.push([dist_tmp, segment_tmp.swap()]);
dist_and_segment.push([dist_tmp, segment_tmp.reverse()]);

Distance.sort(dist_and_segment);
return dist_and_segment[0];
Expand Down Expand Up @@ -281,7 +281,7 @@ module.exports = function(Flatten) {

let [dist_from_center, shortest_segment_from_center] = Distance.point2line(circle.center, line);
let [dist, shortest_segment] = Distance.point2circle(shortest_segment_from_center.end, circle);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [dist, shortest_segment];
}

Expand Down Expand Up @@ -388,12 +388,12 @@ module.exports = function(Flatten) {

[dist_tmp, segment_tmp] = Distance.point2arc(arc2.start, arc1);
if (segment_tmp.end.on(arc1)) {
dist_and_segment.push([dist_tmp, segment_tmp.swap()]);
dist_and_segment.push([dist_tmp, segment_tmp.reverse()]);
}

[dist_tmp, segment_tmp] = Distance.point2arc(arc2.end, arc1);
if (segment_tmp.end.on(arc1)) {
dist_and_segment.push([dist_tmp, segment_tmp.swap()]);
dist_and_segment.push([dist_tmp, segment_tmp.reverse()]);
}

[dist_tmp, segment_tmp] = Distance.point2point(arc1.start, arc2.start);
Expand Down
12 changes: 10 additions & 2 deletions classes/arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ module.exports = function(Flatten) {

if (shape instanceof Flatten.Point) {
let [dist, shortest_segment] = Distance.point2arc(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [dist, shortest_segment];
}

Expand All @@ -223,7 +223,7 @@ module.exports = function(Flatten) {

if (shape instanceof Flatten.Segment) {
let [dist, shortest_segment] = Distance.segment2arc(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [dist, shortest_segment];
}

Expand Down Expand Up @@ -325,6 +325,14 @@ module.exports = function(Flatten) {
return tangent;
}

/**
* Returns new arc with swapped start and end angles and reversed direction
* @returns {Arc}
*/
reverse() {
return new Arc(this.pc, this.r, this.endAngle, this.startAngle, !this.counterClockwise);
}

static intersectArc2Arc(arc1, arc2) {
var ip = [];

Expand Down
6 changes: 3 additions & 3 deletions classes/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = function(Flatten) {

if (shape instanceof Flatten.Point) {
let [distance, shortest_segment] = point2circle(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [distance, shortest_segment];
}

Expand All @@ -128,13 +128,13 @@ module.exports = function(Flatten) {

if (shape instanceof Flatten.Segment) {
let [distance, shortest_segment] = segment2circle(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [distance, shortest_segment];
}

if (shape instanceof Flatten.Arc) {
let [distance, shortest_segment] = arc2circle(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [distance, shortest_segment];
}

Expand Down
51 changes: 51 additions & 0 deletions classes/face.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,57 @@ module.exports = function (Flatten) {
edges.delete(edge); // delete from PlanarSet of edges and update index
}

/**
* Reverse orientation of the face: first edge become last and vice a verse,
* all edges starts and ends swapped, direction of arcs inverted.
*/
reverse() {
// collect edges in revert order with reverted shapes
let edges = [];
let edge_tmp = this.last;
do {
// reverse shape
edge_tmp.shape = edge_tmp.shape.reverse();
edges.push(edge_tmp);
edge_tmp = edge_tmp.prev;
} while (edge_tmp !== this.last);

// restore linked list
this.first = undefined;
this.last = undefined;
for (let edge of edges) {
if (this.first === undefined) {
edge.prev = edge;
edge.next = edge;
this.first = edge;
this.last = edge;
edge.arc_length = 0;
}
else {
// append to end
edge.prev = this.last;
this.last.next = edge;

// update edge to be last
this.last = edge;

// restore circular links
this.last.next = this.first;
this.first.prev = this.last;

// set arc length
edge.arc_length = edge.prev.arc_length + edge.prev.length;
}
}

// Recalculate orientation, if set
if (this._orientation !== undefined) {
this._orientation = undefined;
this._orientation = this.orientation();
}
}


/**
* 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
Expand Down
8 changes: 4 additions & 4 deletions classes/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,24 @@ module.exports = function(Flatten) {

if (shape instanceof Flatten.Point) {
let [distance, shortest_segment] = Distance.point2line(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [distance, shortest_segment];
}

if (shape instanceof Flatten.Circle) {
let [distance, shortest_segment] = Distance.circle2line(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [distance, shortest_segment];
}

if (shape instanceof Flatten.Segment) {
let [distance, shortest_segment] = Distance.segment2line(shape, this);
return [distance, shortest_segment.swap()];
return [distance, shortest_segment.reverse()];
}

if (shape instanceof Flatten.Arc) {
let [distance, shortest_segment] = Distance.arc2line(shape, this);
return [distance, shortest_segment.swap()];
return [distance, shortest_segment.reverse()];
}

if (shape instanceof Flatten.Polygon) {
Expand Down
11 changes: 9 additions & 2 deletions classes/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ module.exports = function(Flatten) {
return newEdge;
}

reverse() {
for (let face of this.faces) {
face.reverse();
}
return this;
}

/**
* Create new copied instance of the polygon
* @returns {Polygon}
Expand Down Expand Up @@ -173,7 +180,7 @@ module.exports = function(Flatten) {

if (shape instanceof Flatten.Point) {
let [dist, shortest_segment] = Distance.point2polygon(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [dist, shortest_segment];
}

Expand All @@ -182,7 +189,7 @@ module.exports = function(Flatten) {
shape instanceof Flatten.Segment ||
shape instanceof Flatten.Arc) {
let [dist, shortest_segment] = Distance.shape2polygon(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [dist, shortest_segment];
}

Expand Down
4 changes: 2 additions & 2 deletions classes/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = function (Flatten) {

if (shape instanceof Flatten.Point) {
let [dist, shortest_segment] = Distance.point2segment(shape, this);
shortest_segment = shortest_segment.swap();
shortest_segment = shortest_segment.reverse();
return [dist, shortest_segment];
}

Expand Down Expand Up @@ -217,7 +217,7 @@ module.exports = function (Flatten) {
* Returns new segment with swapped start and end points
* @returns {Segment}
*/
swap() {
reverse() {
return new Segment(this.end, this.start);
}

Expand Down
110 changes: 107 additions & 3 deletions docs/Flatten.Arc.html

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Flatten.Edge.html

Large diffs are not rendered by default.

100 changes: 92 additions & 8 deletions docs/Flatten.Face.html

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/Flatten.Polygon.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Flatten.Ray.html

Large diffs are not rendered by default.

146 changes: 73 additions & 73 deletions docs/Flatten.Segment.html

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/algorithms_distance.js.html

Large diffs are not rendered by default.

16 changes: 12 additions & 4 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.

12 changes: 6 additions & 6 deletions docs/classes_circle.js.html

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions docs/classes_line.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.

15 changes: 11 additions & 4 deletions docs/classes_polygon.js.html

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/classes_segment.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_planar_set.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 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/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.

6 changes: 6 additions & 0 deletions test/arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,10 @@ describe('#Flatten.Arc', function() {
let middle = arc.middle();
expect(middle.equalTo(point(5,0))).to.be.true;
});
it('Can reverse arc', function() {
let arc = new Arc(point(), 5, Math.PI/4, 3*Math.PI/4, Flatten.CCW);
let reversed_arc = arc.reverse();
expect(reversed_arc.counterClockwise).to.equal(Flatten.CW);
expect(Flatten.Utils.EQ(arc.sweep,reversed_arc.sweep)).to.be.true;
})
});
46 changes: 46 additions & 0 deletions test/boolean_op.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,50 @@ describe('#Algorithms.Boolean Operations', function() {
expect(poly.faces.size).to.equal(1);
expect(poly.edges.size).to.equal(4);
});
it('Can perform subtract. 2 intersecting polygons', function () {
"use strict";

let polygon1 = new Polygon();
polygon1.addFace( [
point(-10,0),
point(-10, 20),
point(10, 20),
point(10, 0)
]);
let polygon2 = new Polygon();
polygon2.addFace( [
point(5,10),
point(5,30),
point(15,30),
point(15,10)
]);

let poly = subtract(polygon1, polygon2);
expect(poly.faces.size).to.equal(1);
expect(poly.edges.size).to.equal(6);
});
it('Can perform (boolean) intersection. 2 intersecting polygons', function () {
"use strict";

let polygon1 = new Polygon();
polygon1.addFace( [
point(-10,0),
point(-10, 20),
point(10, 20),
point(10, 0)
]);

let polygon2 = new Polygon();
polygon2.addFace( [
point(5,10),
point(5,30),
point(15,30),
point(15,10)
]);

let poly = intersect(polygon1, polygon2);
expect(poly.faces.size).to.equal(1);
expect(poly.edges.size).to.equal(4);
expect([...poly.faces][0].size).to.equal(4);
});
});
22 changes: 22 additions & 0 deletions test/face.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,26 @@ describe('#Flatten.Face', function() {
expect(face.isEmpty()).to.be.true;
expect(face.size).to.equal(0);
});
it('Can reverse face', function () {
"use strict";
let points = [
point(100, 20),
point(200, 20),
point(200, 40),
point(100, 40)
];

let poly = new Polygon();
let face = poly.addFace(points);
expect(face.size).to.equal(4);
expect(face.orientation()).to.equal(Flatten.ORIENTATION.CCW);

let reversed_poly = poly.reverse();
expect(reversed_poly.faces.size).to.equal(1);
expect(reversed_poly.edges.size).to.equal(4);

expect([...reversed_poly.faces][0].size).to.equal(4);
let orientation = [...reversed_poly.faces][0].orientation();
expect(orientation).to.equal(Flatten.ORIENTATION.CW);
});
});

0 comments on commit eedc822

Please sign in to comment.