Skip to content

Commit

Permalink
Boolean op test & fix - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbol99 committed Jan 22, 2018
1 parent a07c9bb commit 897d4c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
11 changes: 9 additions & 2 deletions algorithms/boolean_op.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = function (Flatten) {
BooleanOp.removeNotRelevantChains(wrk_poly, op, intersections.int_points2_sorted);

// add edges of wrk_poly into the edge container of res_poly
BooleanOp.copyEdgesToContainer(res_poly, wrk_poly);
BooleanOp.copyWrkToRes(res_poly, wrk_poly, op, intersections.int_points2);

// swap links from res_poly to wrk_poly and vice versa
BooleanOp.swapLinks(res_poly, wrk_poly, intersections);
Expand Down Expand Up @@ -173,6 +173,7 @@ module.exports = function (Flatten) {
}

static splitByIntersections(polygon, int_points) {
if (!int_points) return;
for (let int_point of int_points) {
let edge = int_point.edge_before;

Expand Down Expand Up @@ -297,6 +298,7 @@ module.exports = function (Flatten) {
}

static removeNotRelevantChains(polygon, op, int_points) {
if (!int_points) return;
for (let i = 0; i < int_points.length; i++) {
// TODO: Support claster of duplicated points with same <x,y> came from different faces

Expand All @@ -321,11 +323,16 @@ module.exports = function (Flatten) {
}
};

static copyEdgesToContainer(res_polygon, wrk_polygon) {
static copyWrkToRes(res_polygon, wrk_polygon, op, int_points) {
for (let face of wrk_polygon.faces) {
for (let edge of face) {
res_polygon.edges.add(edge);
}
// If union - add face from wrk_polygon that is not intersected with res_polygon
if ( op === Flatten.BOOLEAN_UNION &&
int_points && int_points.find((ip) => (ip.face === face)) === undefined) {
res_polygon.addFace(face.first, face.last);
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion test/boolean_op.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('#Algorithms.Boolean Operations', function() {
expect(face.size).to.equal(6);
}
});
it('Can perform boolean op. Case 1 - 2 polygons, union', function () {
it('Can perform union. 2 polygons, intersect', function () {
"use strict";
let poly1 = new Polygon();
poly1.addFace([point(0,0), point(150, 0), point(150,30), point(0, 30)]);
Expand All @@ -49,7 +49,29 @@ describe('#Algorithms.Boolean Operations', function() {
for (let face of poly.faces) {
expect(face.size).to.equal(8);
}
let vertices = poly.vertices;
expect(vertices.find((pt) => pt.equalTo(point(0,0)))).to.be.defined;
expect(vertices.find((pt) => pt.equalTo(point(150,0)))).to.be.defined;
expect(vertices.find((pt) => pt.equalTo(point(150,30)))).to.be.undefined;
expect(vertices.find((pt) => pt.equalTo(point(0,30)))).to.be.defined;
expect(vertices.find((pt) => pt.equalTo(point(100,20)))).to.be.undefined;
expect(vertices.find((pt) => pt.equalTo(point(200,20)))).to.be.defined;
expect(vertices.find((pt) => pt.equalTo(point(200,40)))).to.be.defined;
expect(vertices.find((pt) => pt.equalTo(point(100,40)))).to.be.defined;
});
it('Can perform union. 2 polygons, disjoint', function () {
"use strict";
let poly1 = new Polygon();
poly1.addFace([point(0,0), point(50, 0), point(50,30), point(0, 30)]);
let poly2 = new Polygon();
poly2.addFace([point(100, 50), point(200, 50), point(200, 100), point(100, 100)]);
let poly = union(poly1, poly2);
expect(poly.faces.size).to.equal(2);
for (let face of poly.faces) {
expect(face.size).to.equal(4);
}
});

// it('Can clip segment case 2 - 1 intersections, clip till end', function () {
// "use strict";
// let points = [point(100, 20), point(200, 20), point(200, 40), point(100, 40)];
Expand Down

0 comments on commit 897d4c4

Please sign in to comment.