awacke1's picture
Update backup.app.py
7759119 verified
raw
history blame
2.99 kB
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from scipy import spatial
import pymesh
from solid import *
from solid.utils import *
import os
# Function to plot vertices and meshes
def plot_vertices_and_mesh(vertices, faces):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot vertices
ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2])
# Plot faces
for face in faces:
vertices_of_face = vertices[face]
poly = Poly3DCollection([vertices_of_face])
ax.add_collection3d(poly)
st.pyplot(fig)
# Rest of the functions remain the same
# Streamlit UI
st.title("3D Graphics and Geometry with Streamlit")
if st.button('Plot Vertices and Meshes', key='01'):
vertices = np.array([[-3, -3, 0], [+3, -3, 0], [+3, +3, 0], [-3, +3, 0], [+0, +0, +3]])
faces = np.array([[4, 1, 0], [4, 2, 1], [3, 4, 0], [3, 4, 2], [3, 2, 1], [3, 1, 0]], dtype=np.int32)
plot_vertices_and_mesh(vertices, faces)
# Function to plot convex hull
def plot_convex_hull(points):
hull = spatial.ConvexHull(points)
plt.plot(points[:,0], points[:,1], 'o')
for simplex in hull.simplices:
plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
st.pyplot(plt)
# Function to create and save 3D meshes
def create_3d_meshes():
box_a = pymesh.generate_box_mesh([0,0,0], [1,1,1])
pymesh.save_mesh("pymesh_example_01.stl", box_a, ascii=False)
box_b = pymesh.generate_box_mesh([0.4,0.4,0], [0.6,0.6,1])
box_c = pymesh.boolean(box_a, box_b, operation='difference', engine="igl")
pymesh.save_mesh("pymesh_example_02.stl", box_c, ascii=False)
# Function to create and save solid models
def create_solid_models():
d = difference()(cube(size=10, center=True), sphere(r=6.5, segments=300))
scad_render_to_file(d, '/tmp/solidpython_example_01.scad')
c = circle(r=1)
t = translate([2, 0, 0])(c)
e = linear_extrude(height=10, center=True, convexity=10, twist=-500, slices=500)(t)
col = color('lightgreen')(e)
scad_render_to_file(col, 'solidpython_example_02.scad')
# Streamlit UI
st.title("3D Graphics and Geometry with Streamlit")
if st.button('Plot Vertices and Meshes', key='02'):
# Add vertices and faces data here
vertices = np.array([[-3, -3, 0], [+3, -3, 0], [+3, +3, 0], [-3, +3, 0], [+0, +0, +3]])
faces = np.array([[4, 1,0], [4, 2, 1], [3, 4, 0], [3, 4, 2], [3, 2, 1], [3, 1, 0]], dtype=int32)
plot_vertices_and_mesh(vertices, faces)
if st.button('Plot Convex Hull', key='03'):
points = np.array([[0, 0], [-2, 0], [-2, 2], [0, 1.5], [2, 2], [2, 0]])
plot_convex_hull(points)
if st.button('Create and Save 3D Meshes', key='04'):
create_3d_meshes()
st.write("3D meshes saved to files.")
if st.button('Create and Save Solid Models', key='05'):
create_solid_models()
st.write("Solid models saved to files.")