Skip to content

Conversation

@petrasvestartas
Copy link
Collaborator

@petrasvestartas petrasvestartas commented Apr 9, 2025

Polygon with holes was accidentally skipped during nanobind build system change.
This pull-request bring this method back.

Python API should be the same. From C++ side, output of this function was changed from std::vector<std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>> to std::vector<std::vector<compas::RowMatrixXd>> , considering the first polyline is always the outer boundary.

This example runs both single polygon offset and polygons with holes offset:

@romanarust could you please test this pull-request by first building the package locally

conda create -n cgal -c conda-forge python=3.12
pip install -r requirements-dev.txt
pip install -e .
from compas.geometry import Polygon
from compas_viewer import Viewer

from compas_cgal.straight_skeleton_2 import offset_polygon, offset_polygon_with_holes



def main():
    """Create offset polygons."""

    points = [
        (-1.91, 3.59, 0.0),
        (-5.53, -5.22, 0.0),
        (-0.39, -1.98, 0.0),
        (2.98, -5.51, 0.0),
        (4.83, -2.02, 0.0),
        (9.70, -3.63, 0.0),
        (12.23, 1.25, 0.0),
        (3.42, 0.66, 0.0),
        (2.92, 4.03, 0.0),
        (-1.91, 3.59, 0.0),
    ]
    polygon = Polygon(points)
    offset = 1.5

    offset_polygon_inner = offset_polygon(points, offset)
    offset_polygon_outer = offset_polygon(points, -offset)


    result = offset_polygon_with_holes(offset_polygon_outer[0], offset_polygon_inner, -0.1)


    return offset_polygon_inner, offset_polygon_outer, polygon, result


offset_polygon_inner, offset_polygon_outer, polygon, result = main()

# ==============================================================================
# Visualize
# ==============================================================================

viewer = Viewer()
viewer.scene.add(polygon, show_faces=False)
viewer.config.renderer.show_grid = False

for opolygon in offset_polygon_inner:
    viewer.scene.add(opolygon, linecolor=(1.0, 0.0, 0.0), facecolor=(1.0, 0.0, 0.0, 0.0), show_faces=False)

for opolygon in offset_polygon_outer:
    viewer.scene.add(opolygon, linecolor=(0.0, 0.0, 1.0), facecolor=(0.0, 0.0, 1.0, 0.0), show_faces=False)

for opolygon, holes in result:
    viewer.scene.add(opolygon, linecolor=(0.0, 0.0, 1.0), facecolor=(0.0, 0.0, 1.0, 0.0), show_faces=False)
    for hole in holes:
        viewer.scene.add(hole, linecolor=(0.0, 0.0, 1.0), facecolor=(0.0, 0.0, 1.0, 0.0), show_faces=False)

viewer.show()

image

P.S. If performance is needed we can make in the future a function that offsets multiple polygons with holes.

Copy link
Member

@tomvanmele tomvanmele left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tomvanmele tomvanmele merged commit bc50be9 into compas-dev:main Apr 9, 2025
5 checks passed
@romanarust
Copy link
Member

Thank you so much @petrasvestartas and @tomvanmele for resolving this so quickly!

@romanarust
Copy link
Member

P.S. If performance is needed we can make in the future a function that offsets multiple polygons with holes.

yes, that would indeed help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants