buffers the polygons to increase reliability of joining
| 1555 | |
| 1556 | // buffers the polygons to increase reliability of joining |
| 1557 | std::vector<std::vector<Point3d>> joinAllWithBuffer(const std::vector<std::vector<Point3d>>& polygons, double offset, double tol) { |
| 1558 | std::vector<std::vector<Point3d>> result; |
| 1559 | |
| 1560 | const size_t N = polygons.size(); |
| 1561 | if (N <= 1) { |
| 1562 | return polygons; |
| 1563 | } |
| 1564 | |
| 1565 | std::vector<double> polygonAreas(N, 0.0); |
| 1566 | for (unsigned i = 0; i < N; ++i) { |
| 1567 | auto area = getArea(polygons[i]); |
| 1568 | if (area) { |
| 1569 | polygonAreas[i] = *area; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | std::vector<std::vector<Point3d>> modifiedPolygons; |
| 1574 | |
| 1575 | for (unsigned i = 0; i < N; i++) { |
| 1576 | modifiedPolygons.push_back(*buffer(polygons[i], offset, tol)); |
| 1577 | } |
| 1578 | |
| 1579 | // compute adjacency matrix |
| 1580 | Matrix A(N, N, 0.0); |
| 1581 | for (unsigned i = 0; i < N; ++i) { |
| 1582 | A(i, i) = 1.0; |
| 1583 | for (unsigned j = i + 1; j < N; ++j) { |
| 1584 | if (join(modifiedPolygons[i], modifiedPolygons[j], tol)) { |
| 1585 | A(i, j) = 1.0; |
| 1586 | A(j, i) = 1.0; |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | const std::vector<std::vector<unsigned>> connectedComponents = findConnectedComponents(A); |
| 1592 | for (const std::vector<unsigned>& component : connectedComponents) { |
| 1593 | std::vector<unsigned> orderedComponent(component); |
| 1594 | std::sort(orderedComponent.begin(), orderedComponent.end(), [&polygonAreas](int ia, int ib) { return polygonAreas[ia] > polygonAreas[ib]; }); |
| 1595 | |
| 1596 | std::vector<Point3d> points; |
| 1597 | std::set<unsigned> joinedComponents; |
| 1598 | |
| 1599 | // try to join at most component.size() times |
| 1600 | for (unsigned n = 0; n < component.size(); ++n) { |
| 1601 | |
| 1602 | // loop over polygons to join in order |
| 1603 | for (const unsigned i : orderedComponent) { |
| 1604 | if (points.empty()) { |
| 1605 | points = modifiedPolygons[i]; |
| 1606 | joinedComponents.insert(i); |
| 1607 | } else { |
| 1608 | // if not already joined |
| 1609 | if (joinedComponents.find(i) == joinedComponents.end()) { |
| 1610 | boost::optional<std::vector<Point3d>> joined = join(points, modifiedPolygons[i], tol); |
| 1611 | if (joined) { |
| 1612 | points = *joined; |
| 1613 | joinedComponents.insert(i); |
| 1614 | } |
nothing calls this directly
no test coverage detected