|
1 | 1 | import numpy as np
|
| 2 | +from compas.geometry import Polygon |
2 | 3 | from compas.geometry import normal_polygon
|
3 | 4 | from compas.tolerance import TOL
|
4 | 5 |
|
@@ -26,7 +27,120 @@ def create_interior_straight_skeleton(points) -> PolylinesNumpy:
|
26 | 27 | If the normal of the polygon is not [0, 0, 1].
|
27 | 28 | """
|
28 | 29 | points = list(points)
|
29 |
| - if not TOL.is_allclose(normal_polygon(points, True), [0, 0, 1]): |
30 |
| - raise ValueError("Please pass a polygon with a normal vector of [0, 0, 1].") |
| 30 | + normal = normal_polygon(points, True) |
| 31 | + if not TOL.is_allclose(normal, [0, 0, 1]): |
| 32 | + raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal)) |
31 | 33 | V = np.asarray(points, dtype=np.float64)
|
32 | 34 | return straight_skeleton_2.create_interior_straight_skeleton(V)
|
| 35 | + |
| 36 | + |
| 37 | +def create_interior_straight_skeleton_with_holes(points, holes) -> PolylinesNumpy: |
| 38 | + """Compute the skeleton of a polygon with holes. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + points : list of point coordinates or :class:`compas.geometry.Polygon` |
| 43 | + The points of the polygon. |
| 44 | + holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon` |
| 45 | + The holes of the polygon. |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------- |
| 49 | + :attr:`compas_cgal.types.PolylinesNumpy` |
| 50 | + The skeleton of the polygon. |
| 51 | +
|
| 52 | + Raises |
| 53 | + ------ |
| 54 | + ValueError |
| 55 | + If the normal of the polygon is not [0, 0, 1]. |
| 56 | + If the normal of a hole is not [0, 0, -1]. |
| 57 | + """ |
| 58 | + points = list(points) |
| 59 | + normal = normal_polygon(points, True) |
| 60 | + if not TOL.is_allclose(normal, [0, 0, 1]): |
| 61 | + raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal)) |
| 62 | + V = np.asarray(points, dtype=np.float64) |
| 63 | + |
| 64 | + H = [] |
| 65 | + for i, hole in enumerate(holes): |
| 66 | + points = list(hole) |
| 67 | + normal_hole = normal_polygon(points, True) |
| 68 | + if not TOL.is_allclose(normal_hole, [0, 0, -1]): |
| 69 | + raise ValueError("The normal of the hole should be [0, 0, -1]. The normal of the provided {}-th hole is {}".format(i, normal_hole)) |
| 70 | + hole = np.asarray(points, dtype=np.float64) |
| 71 | + H.append(hole) |
| 72 | + return straight_skeleton_2.create_interior_straight_skeleton_with_holes(V, H) |
| 73 | + |
| 74 | + |
| 75 | +def create_offset_polygons_2(points, offset) -> list[Polygon]: |
| 76 | + """Compute the polygon offset. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + points : list of point coordinates or :class:`compas.geometry.Polygon` |
| 81 | + The points of the polygon. |
| 82 | + offset : float |
| 83 | + The offset distance. If negative, the offset is outside the polygon, otherwise inside. |
| 84 | +
|
| 85 | + Returns |
| 86 | + ------- |
| 87 | + list[:class:`Polygon`] |
| 88 | + The offset polygon(s). |
| 89 | +
|
| 90 | + Raises |
| 91 | + ------ |
| 92 | + ValueError |
| 93 | + If the normal of the polygon is not [0, 0, 1]. |
| 94 | + """ |
| 95 | + points = list(points) |
| 96 | + normal = normal_polygon(points, True) |
| 97 | + if not TOL.is_allclose(normal, [0, 0, 1]): |
| 98 | + raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal)) |
| 99 | + V = np.asarray(points, dtype=np.float64) |
| 100 | + offset = float(offset) |
| 101 | + if offset < 0: # outside |
| 102 | + offset_polygons = straight_skeleton_2.create_offset_polygons_2_outer(V, abs(offset))[1:] # first one is box |
| 103 | + else: # inside |
| 104 | + offset_polygons = straight_skeleton_2.create_offset_polygons_2_inner(V, offset) |
| 105 | + return [Polygon(points.tolist()) for points in offset_polygons] |
| 106 | + |
| 107 | + |
| 108 | +def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]: |
| 109 | + """Compute the polygon offset with weights. |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + points : list of point coordinates or :class:`compas.geometry.Polygon` |
| 114 | + The points of the polygon. |
| 115 | + offset : float |
| 116 | + The offset distance. If negative, the offset is outside the polygon, otherwise inside. |
| 117 | + weights : list of float |
| 118 | + The weights for each edge, starting with the edge between the last and the first point. |
| 119 | +
|
| 120 | + Returns |
| 121 | + ------- |
| 122 | + list[:class:`Polygon`] |
| 123 | + The offset polygon(s). |
| 124 | +
|
| 125 | + Raises |
| 126 | + ------ |
| 127 | + ValueError |
| 128 | + If the normal of the polygon is not [0, 0, 1]. |
| 129 | + ValueError |
| 130 | + If the number of weights does not match the number of points. |
| 131 | + """ |
| 132 | + points = list(points) |
| 133 | + normal = normal_polygon(points, True) |
| 134 | + if not TOL.is_allclose(normal, [0, 0, 1]): |
| 135 | + raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal)) |
| 136 | + |
| 137 | + V = np.asarray(points, dtype=np.float64) |
| 138 | + offset = float(offset) |
| 139 | + W = np.asarray(weights, dtype=np.float64) |
| 140 | + if W.shape[0] != V.shape[0]: |
| 141 | + raise ValueError("The number of weights should be equal to the number of points %d != %d." % (W.shape[0], V.shape[0])) |
| 142 | + if offset < 0: |
| 143 | + offset_polygons = straight_skeleton_2.create_weighted_offset_polygons_2_outer(V, abs(offset), W)[1:] |
| 144 | + else: |
| 145 | + offset_polygons = straight_skeleton_2.create_weighted_offset_polygons_2_inner(V, offset, W) |
| 146 | + return [Polygon(points.tolist()) for points in offset_polygons] |
0 commit comments