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

Feature Request: Add a Polyhedron object, which is constructed from a list of 3d points #941

Open
KilowattSynthesis opened this issue Mar 17, 2025 · 7 comments
Labels
enhancement New feature or request

Comments

@KilowattSynthesis
Copy link

No description provided.

@gumyr gumyr added the enhancement New feature or request label Mar 17, 2025
@gumyr gumyr added this to the Not Gating Release 1.0.0 milestone Mar 17, 2025
@gumyr
Copy link
Owner

gumyr commented Mar 17, 2025

There is an example doing this here: https://build123d.readthedocs.io/en/latest/examples_1.html#platonic-solids

Basically: solid_obj = Solid(Shell(list_of_faces))

What is the application for doing this? It seems quite specialized.

@KilowattSynthesis
Copy link
Author

I did read that example.

Instead of a list of faces, I'm hoping to work directly with a list of points (for the very-obscure irregular shapes). Making a list of faces from a list of points is tough.

Alternatively, a 3d convex hull would also solve this problem.

@gumyr
Copy link
Owner

gumyr commented Mar 17, 2025

The code in that example can be used to make random rocks, you just need to provide the list of points.

from build123d import *
from ocp_vscode import show
import numpy as np
from scipy.spatial import ConvexHull


def generate_rock_points(n_points=100, noise_strength=0.2):
    """Generate a set of 3D points resembling a natural rock shape."""
    # Generate spherical points
    phi = np.random.uniform(0, np.pi, n_points)  # Polar angles
    theta = np.random.uniform(0, 2 * np.pi, n_points)  # Azimuthal angles

    # Convert spherical coordinates to Cartesian (x, y, z)
    x = np.sin(phi) * np.cos(theta)
    y = np.sin(phi) * np.sin(theta)
    z = np.cos(phi)

    # Apply random Gaussian noise for roughness
    x += np.random.normal(0, noise_strength, n_points)
    y += np.random.normal(0, noise_strength, n_points)
    z += np.random.normal(0, noise_strength, n_points)

    points = np.vstack((x, y, z)).T  # Combine into a (n_points, 3) array
    return points


rock_vertices = generate_rock_points()

# Create a convex hull from the vertices
hull = ConvexHull(rock_vertices).simplices.tolist()

# Create faces from the vertex indices
rock_faces = []
for face_vertex_indices in hull:
    corner_vertices = [rock_vertices[i] for i in face_vertex_indices]
    rock_faces.append(Face(Wire.make_polygon(corner_vertices)))

# Create the solid from the Faces
rock = Solid(Shell(rock_faces)).clean()

show(rock)

Image

I guess those few lines of code could be wrapped up into a new object - wouldn't make it easier to use though.

@KilowattSynthesis
Copy link
Author

KilowattSynthesis commented Mar 18, 2025

May be obvious to you, but I want to assure you that, even as someone who uses build123d regularly, the following code is still black magic to me (and thus likely to much of the community):

# Create a convex hull from the vertices
hull = ConvexHull(rock_vertices).simplices.tolist()

# Create faces from the vertex indices
rock_faces = []
for face_vertex_indices in hull:
    corner_vertices = [rock_vertices[i] for i in face_vertex_indices]
    rock_faces.append(Face(Wire.make_polygon(corner_vertices)))

# Create the solid from the Faces
rock = Solid(Shell(rock_faces)).clean()

ConvexHull doesn't even appear in the documentation (other than that specific example). .simplices doesn't appear to be documented anywhere. Using indexes from one place into another list is something I never would have thought to consider.

I guess those few lines of code could be wrapped up into a new object - wouldn't make it easier to use though.

Why do you think it "wouldn't make it easier to use"? Seems pretty obvious to me that it would make it easier to use, especially given the fact that the example you've laid out here relies on undocumented features (or perhaps features only described by a single example).

@jdegenstein
Copy link
Collaborator

ConvexHull doesn't even appear in the documentation

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html

@KilowattSynthesis
Copy link
Author

Ugh the import * got me again. Because everything in these examples magically comes from build123d, I assumed this one did as well.

Anyway, relying on a random feature from scipy seems sorta against the spirit of build123d?

@gumyr
Copy link
Owner

gumyr commented Mar 18, 2025

Adding a Polyhedron object isn't a bad idea I'm just showing you how you can achieve that now without waiting.

One of the most powerful aspects of build123d is that this is a one of 616,406 Python projects (on pypi) which allows users to use a vast collection of open-source content in their projects so "relying on a random feature from scipy" is 100% aligned with the spirit of build123d.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants