Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function create_offset_polygons_with_holes_2 #35

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Added recipe hasher.
* Added `scip` to dev install instructions in README.md
* Added `compas_cgal.straight_skeleton_2.offset_polygon_with_holes`.

### Changed
* Changed name of `compas_cgal.straight_skeleton_2.create_interior_straight_skeleton` to `interior_straight_skeleton`
* Changed name of `compas_cgal.straight_skeleton_2.create_interior_straight_skeleton_with_holes` to `interior_straight_skeleton_with_holes`
* Changed name of `compas_cgal.straight_skeleton_2.create_offset_polygons_2` to `offset_polygon`
* Changed name of `compas_cgal.straight_skeleton_2.create_weighted_offset_polygons_2` to `weighted_offset_polygon`

### Removed

Expand Down
7 changes: 5 additions & 2 deletions docs/api/compas_cgal.straight_skeleton_2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ compas_cgal.straight_skeleton_2
:toctree: generated/
:nosignatures:

create_interior_straight_skeleton
create_interior_straight_skeleton_with_holes
interior_straight_skeleton
interior_straight_skeleton_with_holes
offset_polygon
weighted_offset_polygon
offset_polygon_with_holes
2 changes: 1 addition & 1 deletion docs/examples/polygonal_poinset_reconstruction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Polygonal PointSet Reconstruction
:class: figure-img img-fluid


.. literalinclude:: polygonal_poinset_reconstruction_ransac.py
.. literalinclude:: polygonal_poinset_reconstruction.py
:language: python
4 changes: 2 additions & 2 deletions docs/examples/straight_skeleton_2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton
from compas_cgal.straight_skeleton_2 import interior_straight_skeleton
from compas_viewer import Viewer

points = [
Expand All @@ -15,7 +15,7 @@
]


graph = create_interior_straight_skeleton(points)
graph = interior_straight_skeleton(points)

# ==============================================================================
# Viz
Expand Down
5 changes: 3 additions & 2 deletions docs/examples/straight_skeleton_2_holes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from compas.geometry import Polygon
from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton_with_holes
from compas_viewer import Viewer

from compas_cgal.straight_skeleton_2 import interior_straight_skeleton_with_holes

points = [
(-1.91, 3.59, 0.0),
(-5.53, -5.22, 0.0),
Expand All @@ -24,7 +25,7 @@

polygon = Polygon(points)
holes = [Polygon(hole) for hole in holes]
graph = create_interior_straight_skeleton_with_holes(polygon, holes)
graph = interior_straight_skeleton_with_holes(polygon, holes)

# ==============================================================================
# Viz
Expand Down
11 changes: 6 additions & 5 deletions docs/examples/straight_skeleton_2_offset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from compas.geometry import Polygon
from compas_cgal.straight_skeleton_2 import create_offset_polygons_2
from compas_viewer import Viewer

from compas_cgal.straight_skeleton_2 import offset_polygon

points = [
(-1.91, 3.59, 0.0),
(-5.53, -5.22, 0.0),
Expand All @@ -17,8 +18,8 @@
polygon = Polygon(points)
offset = 1.5

offset_polygons_inner = create_offset_polygons_2(points, offset)
offset_polygons_outer = create_offset_polygons_2(points, -offset)
offset_polygon_inner = offset_polygon(points, offset)
offset_polygon_outer = offset_polygon(points, -offset)

# ==============================================================================
# Viz
Expand All @@ -28,9 +29,9 @@
viewer.scene.add(polygon)
viewer.config.renderer.show_grid = False

for opolygon in offset_polygons_inner:
for opolygon in offset_polygon_inner:
viewer.scene.add(opolygon, linecolor=(1.0, 0.0, 0.0), facecolor=(1.0, 1.0, 1.0, 0.0))
for opolygon in offset_polygons_outer:
for opolygon in offset_polygon_outer:
viewer.scene.add(opolygon, linecolor=(0.0, 0.0, 1.0), facecolor=(1.0, 1.0, 1.0, 0.0))

viewer.show()
5 changes: 3 additions & 2 deletions docs/examples/straight_skeleton_2_offset_weighted.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from compas.geometry import Polygon
from compas_cgal.straight_skeleton_2 import create_weighted_offset_polygons_2
from compas_viewer import Viewer

from compas_cgal.straight_skeleton_2 import weighted_offset_polygons

points = [
(-1.91, 3.59, 0.0),
(-5.53, -5.22, 0.0),
Expand All @@ -20,7 +21,7 @@
distances = [0.1, 0.3, 0.6, 0.1, 0.7, 0.5, 0.2, 0.4, 0.8, 0.2]
weights = [1.0 / d for d in distances]
offset = 1.0
offset_polygons_outer = create_weighted_offset_polygons_2(points, -offset, weights)
offset_polygons_outer = weighted_offset_polygons(points, -offset, weights)

# ==============================================================================
# Viz
Expand Down
85 changes: 69 additions & 16 deletions src/compas_cgal/straight_skeleton_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def graph_from_skeleton_data(points: VerticesNumpy, indices: IntNx1, edges: IntN
return graph


def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]:
"""Compute the skeleton of a polygon.
def interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]:
"""Compute the skeleton of a 2D polygon.

Parameters
----------
Expand All @@ -66,7 +66,7 @@ def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tup
Raises
------
ValueError
If the normal of the polygon is not [0, 0, 1].
If the normal of the polygon is not directed vertically upwards like [0, 0, 1].
"""
points = list(points)
normal = normal_polygon(points, True)
Expand All @@ -79,13 +79,13 @@ def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tup
return points, indices, edges, edge_types


def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]:
"""Compute the skeleton of a polygon with holes.
def interior_straight_skeleton_with_holes(points, holes, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]:
"""Compute the skeleton of a 2D polygon with holes.

Parameters
----------
points : list of point coordinates or :class:`compas.geometry.Polygon`
The points of the polygon.
The points of the 2D polygon.
holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon`
The holes of the polygon.
as_graph : bool, optional
Expand All @@ -99,8 +99,8 @@ def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) -
Raises
------
ValueError
If the normal of the polygon is not [0, 0, 1].
If the normal of a hole is not [0, 0, -1].
If the normal of the polygon is not directed vertically upwards like [0, 0, 1].
If the normal of a hole is not directed vertically downwards like [0, 0, -1].
"""
points = list(points)
normal = normal_polygon(points, True)
Expand All @@ -122,13 +122,13 @@ def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) -
return points, indices, edges, edge_types


def create_offset_polygons_2(points, offset) -> list[Polygon]:
"""Compute the polygon offset.
def offset_polygon(points, offset) -> list[Polygon]:
"""Compute the offset from a 2D polygon.

Parameters
----------
points : list of point coordinates or :class:`compas.geometry.Polygon`
The points of the polygon.
The points of the 2D polygon.
offset : float
The offset distance. If negative, the offset is outside the polygon, otherwise inside.

Expand All @@ -140,7 +140,7 @@ def create_offset_polygons_2(points, offset) -> list[Polygon]:
Raises
------
ValueError
If the normal of the polygon is not [0, 0, 1].
If the normal of the polygon is not directed vertically upwards like [0, 0, 1].
"""
points = list(points)
normal = normal_polygon(points, True)
Expand All @@ -155,13 +155,66 @@ def create_offset_polygons_2(points, offset) -> list[Polygon]:
return [Polygon(points.tolist()) for points in offset_polygons]


def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]:
"""Compute the polygon offset with weights.
def offset_polygon_with_holes(points, holes, offset) -> list[Tuple[Polygon, list[Polygon]]]:
"""Compute the offset from a 2D polygon with holes.

Parameters
----------
points : list of point coordinates or :class:`compas.geometry.Polygon`
The points of the polygon.
The points of the 2D polygon.
holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon`
The holes of the polygon.
offset : float
The offset distance. If negative, the offset is outside the polygon, otherwise inside.

Returns
-------
tomvanmele marked this conversation as resolved.
Show resolved Hide resolved
list of tuple of (:class:`Polygon`, list[:class:`Polygon`])
The polygons with holes.

Raises
------
ValueError
If the normal of the polygon is not directed vertically upwards like [0, 0, 1].
If the normal of a hole is not directed vertically downwards like [0, 0, -1].
"""
points = list(points)
normal = normal_polygon(points, True)
if not TOL.is_allclose(normal, [0, 0, 1]):
raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal))
V = np.asarray(points, dtype=np.float64)

H = []
for i, hole in enumerate(holes):
points = hole
normal_hole = normal_polygon(points, True)
if not TOL.is_allclose(normal_hole, [0, 0, -1]):
raise ValueError("The normal of the hole should be [0, 0, -1]. The normal of the provided {}-th hole is {}".format(i, normal_hole))
hole = np.asarray(points, dtype=np.float64)
H.append(hole)

if offset < 0: # outside
offset_polygons = straight_skeleton_2.create_offset_polygons_2_outer_with_holes(V, H, abs(offset))
else: # inside
offset_polygons = straight_skeleton_2.create_offset_polygons_2_inner_with_holes(V, H, offset)
tomvanmele marked this conversation as resolved.
Show resolved Hide resolved

result = []
for points, holes_np in offset_polygons:
polygon = Polygon(points.tolist())
holes = []
for hole in holes_np:
holes.append(Polygon(hole.tolist()))
result.append((polygon, holes))
return result


def weighted_offset_polygon(points, offset, weights) -> list[Polygon]:
"""Compute the offset from a 2D polygon with weights.

Parameters
----------
points : list of point coordinates or :class:`compas.geometry.Polygon`
The points of the 2D polygon.
offset : float
The offset distance. If negative, the offset is outside the polygon, otherwise inside.
weights : list of float
Expand All @@ -175,7 +228,7 @@ def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]:
Raises
------
ValueError
If the normal of the polygon is not [0, 0, 1].
If the normal of the polygon is not directed vertically upwards like [0, 0, 1].
ValueError
If the number of weights does not match the number of points.
"""
Expand Down
Loading
Loading