awacke1 commited on
Commit
1c9c139
·
verified ·
1 Parent(s): 7759119

Update app.py

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