convex_hull_numpy

compas.geometry.convex_hull_numpy(points)[source]

Compute the convex hull of a set of points.

Parameters

points (list) – XYZ coordinates of the points.

Returns

tuple – Indices of the points on the hull. Faces of the hull.

Notes

The faces of the hull returned by this function do not necessarily have consistent cycle directions. To obtain a mesh with consistent cycle directions, construct a mesh from the returned vertices, this function should be used in combination with compas.topology.unify_cycles().

Examples

import random

from compas.datastructures import Mesh

from compas.geometry import distance_point_point
from compas.geometry import convex_hull_numpy
from compas.topology import unify_cycles

from compas_viewers import MeshViewer

radius = 5
origin = (0., 0., 0.)
count = 0
points = []

while count < 10:
    x = (random.random() - 0.5) * radius * 2
    y = (random.random() - 0.5) * radius * 2
    z = (random.random() - 0.5) * radius * 2
    pt = x, y, z

    if distance_point_point(origin, pt) <= radius:
        points.append(pt)
        count += 1

vertices, faces = convex_hull_numpy(points)

i_index = {i: index for index, i in enumerate(vertices)}

vertices = [points[index] for index in vertices]
faces = [[i_index[i] for i in face] for face in faces]
faces = unify_cycles(vertices, faces)

mesh = Mesh.from_vertices_and_faces(vertices, faces)

viewer = MeshViewer(mesh)

viewer.setup()
viewer.show()