@@ -37,15 +37,56 @@ def convex_hull_numpy(points):
3737 Indices of the points on the hull.
3838 Faces of the hull.
3939
40- Warning
41- -------
42- This function requires Numpy ands Scipy.
40+ Notes
41+ -----
42+ The faces of the hull returned by this function do not necessarily have consistent
43+ cycle directions. To obtain a mesh with consistent cycle directions, construct
44+ a mesh from the returned vertices, this function should be used in combination
45+ with :func:`compas.topology.unify_cycles`.
4346
4447 Examples
4548 --------
4649 .. code-block:: python
4750
48- #
51+ import random
52+
53+ from compas.datastructures import Mesh
54+
55+ from compas.geometry import distance_point_point
56+ from compas.geometry import convex_hull_numpy
57+ from compas.topology import unify_cycles
58+
59+ from compas.viewers import MeshViewer
60+
61+ radius = 5
62+ origin = (0., 0., 0.)
63+ count = 0
64+ points = []
65+
66+ while count < 10:
67+ x = (random.random() - 0.5) * radius * 2
68+ y = (random.random() - 0.5) * radius * 2
69+ z = (random.random() - 0.5) * radius * 2
70+ pt = x, y, z
71+
72+ if distance_point_point(origin, pt) <= radius:
73+ points.append(pt)
74+ count += 1
75+
76+ vertices, faces = convex_hull_numpy(points)
77+
78+ i_index = {i: index for index, i in enumerate(vertices)}
79+
80+ vertices = [points[index] for index in vertices]
81+ faces = [[i_index[i] for i in face] for face in faces]
82+ faces = unify_cycles(vertices, faces)
83+
84+ mesh = Mesh.from_vertices_and_faces(vertices, faces)
85+
86+ viewer = MeshViewer(mesh)
87+
88+ viewer.setup()
89+ viewer.show()
4990
5091 """
5192 points = asarray (points )
@@ -73,10 +114,18 @@ def convex_hull_xy_numpy(points):
73114
74115 Returns
75116 -------
76- tuple
117+ list
77118 Indices of the points on the hull.
119+ list
78120 Faces of the hull.
79121
122+ Notes
123+ -----
124+ The faces of the hull returned by this function do not necessarily have consistent
125+ cycle directions. To obtain a mesh with consistent cycle directions, construct
126+ a mesh from the returned vertices, this function should be used in combination
127+ with :func:`compas.topology.unify_cycles`.
128+
80129 Examples
81130 --------
82131 .. code-block:: python
@@ -91,9 +140,6 @@ def convex_hull_xy_numpy(points):
91140
92141 points = points [:, :2 ]
93142 hull = ConvexHull (points )
94- # temp = zeros((hull.vertices.shape[0], 1))
95- # temp[:, :-1] = points[hull.vertices]
96- # return temp
97143 return hull .vertices , hull .simplices
98144
99145
@@ -108,19 +154,19 @@ def convex_hull_xy_numpy(points):
108154
109155 import random
110156
111- from compas .geometry . distance import distance_point_point
157+ from compas .geometry import distance_point_point
112158
113159 from compas .datastructures import Mesh
114160 from compas .viewers import MeshViewer
115161
116- from compas .topology import mesh_unify_cycles
162+ from compas .topology import unify_cycles
117163
118164 radius = 5
119165 origin = (0. , 0. , 0. )
120166 count = 0
121167 points = []
122168
123- while count < 10 :
169+ while count < 1000 :
124170 x = (random .random () - 0.5 ) * radius * 2
125171 y = (random .random () - 0.5 ) * radius * 2
126172 z = (random .random () - 0.5 ) * radius * 2
@@ -134,14 +180,16 @@ def convex_hull_xy_numpy(points):
134180
135181 i_index = {i : index for index , i in enumerate (vertices )}
136182
137- mesh = Mesh .from_vertices_and_faces (
138- [points [index ] for index in vertices ],
139- [[i_index [i ] for i in face ] for face in faces [1 :]]
140- )
183+ vertices = [points [index ] for index in vertices ]
184+ faces = [[i_index [i ] for i in face ] for face in faces ]
185+ faces = unify_cycles (vertices , faces )
141186
142- mesh_unify_cycles ( mesh )
187+ mesh = Mesh . from_vertices_and_faces ( vertices , faces )
143188
144189 viewer = MeshViewer (mesh )
145190
191+ viewer .axes_on = False
192+ viewer .grid_on = False
193+
146194 viewer .setup ()
147195 viewer .show ()
0 commit comments