Spaces:
Runtime error
Runtime error
Update backup.app.py
Browse files- backup.app.py +37 -37
backup.app.py
CHANGED
@@ -8,78 +8,78 @@ import pymesh
|
|
8 |
from solid import *
|
9 |
from solid.utils import *
|
10 |
import os
|
|
|
11 |
|
12 |
-
# Function to plot vertices and meshes
|
13 |
def plot_vertices_and_mesh(vertices, faces):
|
14 |
fig = plt.figure()
|
15 |
ax = fig.add_subplot(111, projection='3d')
|
16 |
-
|
17 |
-
# Plot vertices
|
18 |
ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2])
|
19 |
-
|
20 |
-
# Plot faces
|
21 |
for face in faces:
|
22 |
vertices_of_face = vertices[face]
|
23 |
poly = Poly3DCollection([vertices_of_face])
|
24 |
ax.add_collection3d(poly)
|
25 |
-
|
26 |
st.pyplot(fig)
|
27 |
|
28 |
-
# Rest of the functions remain the same
|
29 |
-
|
30 |
-
# Streamlit UI
|
31 |
-
st.title("3D Graphics and Geometry with Streamlit")
|
32 |
-
|
33 |
-
if st.button('Plot Vertices and Meshes', key='01'):
|
34 |
-
vertices = np.array([[-3, -3, 0], [+3, -3, 0], [+3, +3, 0], [-3, +3, 0], [+0, +0, +3]])
|
35 |
-
faces = np.array([[4, 1, 0], [4, 2, 1], [3, 4, 0], [3, 4, 2], [3, 2, 1], [3, 1, 0]], dtype=np.int32)
|
36 |
-
plot_vertices_and_mesh(vertices, faces)
|
37 |
-
|
38 |
-
# Function to plot convex hull
|
39 |
def plot_convex_hull(points):
|
40 |
hull = spatial.ConvexHull(points)
|
41 |
-
plt.
|
|
|
42 |
for simplex in hull.simplices:
|
43 |
plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
|
44 |
st.pyplot(plt)
|
45 |
|
46 |
-
# Function to create and save 3D meshes
|
47 |
def create_3d_meshes():
|
48 |
-
|
49 |
-
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
-
# Function to create and save solid models
|
56 |
def create_solid_models():
|
57 |
d = difference()(cube(size=10, center=True), sphere(r=6.5, segments=300))
|
58 |
scad_render_to_file(d, '/tmp/solidpython_example_01.scad')
|
59 |
-
|
60 |
c = circle(r=1)
|
61 |
t = translate([2, 0, 0])(c)
|
62 |
e = linear_extrude(height=10, center=True, convexity=10, twist=-500, slices=500)(t)
|
63 |
col = color('lightgreen')(e)
|
64 |
-
scad_render_to_file(col, 'solidpython_example_02.scad')
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
vertices = np.array([[-3, -3, 0], [+3, -3, 0], [+3, +3, 0], [-3, +3, 0], [+0, +0, +3]])
|
72 |
-
faces = np.array([[4, 1,0], [4, 2, 1], [3, 4, 0], [3, 4, 2], [3, 2, 1], [3, 1, 0]], dtype=int32)
|
73 |
plot_vertices_and_mesh(vertices, faces)
|
74 |
|
75 |
-
|
|
|
76 |
points = np.array([[0, 0], [-2, 0], [-2, 2], [0, 1.5], [2, 2], [2, 0]])
|
77 |
plot_convex_hull(points)
|
78 |
|
79 |
-
|
|
|
80 |
create_3d_meshes()
|
81 |
-
st.write("3D meshes saved to files.")
|
82 |
|
83 |
-
|
|
|
84 |
create_solid_models()
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from solid import *
|
9 |
from solid.utils import *
|
10 |
import os
|
11 |
+
import trimesh
|
12 |
|
|
|
13 |
def plot_vertices_and_mesh(vertices, faces):
|
14 |
fig = plt.figure()
|
15 |
ax = fig.add_subplot(111, projection='3d')
|
|
|
|
|
16 |
ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2])
|
|
|
|
|
17 |
for face in faces:
|
18 |
vertices_of_face = vertices[face]
|
19 |
poly = Poly3DCollection([vertices_of_face])
|
20 |
ax.add_collection3d(poly)
|
|
|
21 |
st.pyplot(fig)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def plot_convex_hull(points):
|
24 |
hull = spatial.ConvexHull(points)
|
25 |
+
plt.figure()
|
26 |
+
plt.plot(points[:, 0], points[:, 1], 'o')
|
27 |
for simplex in hull.simplices:
|
28 |
plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
|
29 |
st.pyplot(plt)
|
30 |
|
|
|
31 |
def create_3d_meshes():
|
32 |
+
# Create a box mesh
|
33 |
+
box_a = trimesh.creation.box(extents=[1, 1, 1])
|
34 |
+
box_a.export('pymesh_example_01.stl')
|
35 |
|
36 |
+
# Create another box mesh and perform a boolean operation
|
37 |
+
box_b = trimesh.creation.box(extents=[0.2, 0.2, 1], translate=[0.4, 0.4, 0])
|
38 |
+
box_c = box_a.difference(box_b)
|
39 |
+
box_c.export('pymesh_example_02.stl')
|
40 |
+
|
41 |
+
st.write("3D meshes saved to files.")
|
42 |
|
|
|
43 |
def create_solid_models():
|
44 |
d = difference()(cube(size=10, center=True), sphere(r=6.5, segments=300))
|
45 |
scad_render_to_file(d, '/tmp/solidpython_example_01.scad')
|
|
|
46 |
c = circle(r=1)
|
47 |
t = translate([2, 0, 0])(c)
|
48 |
e = linear_extrude(height=10, center=True, convexity=10, twist=-500, slices=500)(t)
|
49 |
col = color('lightgreen')(e)
|
50 |
+
scad_render_to_file(col, '/tmp/solidpython_example_02.scad')
|
51 |
+
st.write("Solid models saved to files.")
|
52 |
+
|
53 |
+
st.title("Exploring 3D Graphics and Geometry with Python")
|
54 |
|
55 |
+
st.markdown("""
|
56 |
+
This application demonstrates basic concepts of 3D graphics in Python.
|
57 |
+
You can visualize 3D objects, create convex hulls, generate and save 3D meshes,
|
58 |
+
and explore solid modeling. Each section below offers a different aspect of 3D graphics.
|
59 |
+
""")
|
60 |
|
61 |
+
st.subheader("1. Plot Vertices and Meshes")
|
62 |
+
if st.button('Show 3D Vertices and Meshes'):
|
63 |
vertices = np.array([[-3, -3, 0], [+3, -3, 0], [+3, +3, 0], [-3, +3, 0], [+0, +0, +3]])
|
64 |
+
faces = np.array([[4, 1, 0], [4, 2, 1], [3, 4, 0], [3, 4, 2], [3, 2, 1], [3, 1, 0]], dtype=np.int32)
|
65 |
plot_vertices_and_mesh(vertices, faces)
|
66 |
|
67 |
+
st.subheader("2. Plot Convex Hull")
|
68 |
+
if st.button('Show Convex Hull'):
|
69 |
points = np.array([[0, 0], [-2, 0], [-2, 2], [0, 1.5], [2, 2], [2, 0]])
|
70 |
plot_convex_hull(points)
|
71 |
|
72 |
+
st.subheader("3. Create and Save 3D Meshes")
|
73 |
+
if st.button('Generate 3D Meshes'):
|
74 |
create_3d_meshes()
|
|
|
75 |
|
76 |
+
st.subheader("4. Create and Save Solid Models")
|
77 |
+
if st.button('Generate Solid Models'):
|
78 |
create_solid_models()
|
79 |
+
|
80 |
+
st.markdown("""
|
81 |
+
Developed using Python libraries like matplotlib, scipy, pymesh, and solid,
|
82 |
+
this application serves as an introductory tool for understanding and visualizing
|
83 |
+
3D concepts in Python.
|
84 |
+
""")
|
85 |
+
|