Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
import cadquery as cq
|
| 2 |
import matplotlib.pyplot as plt
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
import trimesh
|
| 5 |
import gradio as gr
|
| 6 |
from cadquery.occ_impl.exporters import exportShape
|
| 7 |
|
|
|
|
|
|
|
| 8 |
def create_bird_ear_shell(height, radius, thickness):
|
| 9 |
try:
|
|
|
|
| 10 |
outer_shell = (
|
| 11 |
cq.Workplane("XY")
|
| 12 |
.lineTo(0, height)
|
|
@@ -14,6 +17,7 @@ def create_bird_ear_shell(height, radius, thickness):
|
|
| 14 |
.close()
|
| 15 |
.revolve()
|
| 16 |
)
|
|
|
|
| 17 |
inner_shell = (
|
| 18 |
cq.Workplane("XY")
|
| 19 |
.lineTo(0, height - thickness)
|
|
@@ -21,11 +25,15 @@ def create_bird_ear_shell(height, radius, thickness):
|
|
| 21 |
.close()
|
| 22 |
.revolve()
|
| 23 |
)
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
print(f"Error in
|
| 27 |
return None
|
| 28 |
|
|
|
|
|
|
|
| 29 |
def generate_2d_image(height, radius):
|
| 30 |
try:
|
| 31 |
theta = np.linspace(0, 2 * np.pi, 100)
|
|
@@ -48,6 +56,8 @@ def generate_2d_image(height, radius):
|
|
| 48 |
print(f"Error in generating 2D image: {e}")
|
| 49 |
return None
|
| 50 |
|
|
|
|
|
|
|
| 51 |
def generate_3d_stl(height, radius, thickness):
|
| 52 |
try:
|
| 53 |
bird_shell_model = create_bird_ear_shell(height, radius, thickness)
|
|
@@ -61,15 +71,42 @@ def generate_3d_stl(height, radius, thickness):
|
|
| 61 |
print(f"Error in generating 3D STL: {e}")
|
| 62 |
return None
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def generate_visuals(height, radius, thickness):
|
| 65 |
two_d_image = generate_2d_image(height, radius)
|
| 66 |
three_d_model = generate_3d_stl(height, radius, thickness)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
three_d_model = "Error: Unable to generate 3D STL file."
|
| 71 |
-
return two_d_image, three_d_model
|
| 72 |
|
|
|
|
| 73 |
interface = gr.Interface(
|
| 74 |
fn=generate_visuals,
|
| 75 |
inputs=[
|
|
@@ -80,9 +117,10 @@ interface = gr.Interface(
|
|
| 80 |
outputs=[
|
| 81 |
gr.Image(label="2D Visualization"),
|
| 82 |
gr.File(label="Download 3D Model (STL)"),
|
|
|
|
| 83 |
],
|
| 84 |
title="Ear-Like Bird Shell Simulation",
|
| 85 |
-
description="Input
|
| 86 |
)
|
| 87 |
|
| 88 |
interface.launch()
|
|
|
|
| 1 |
import cadquery as cq
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
+
from mpl_toolkits.mplot3d import Axes3D
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from cadquery.occ_impl.exporters import exportShape
|
| 7 |
|
| 8 |
+
|
| 9 |
+
# Function to create 3D ear-like shell using CadQuery
|
| 10 |
def create_bird_ear_shell(height, radius, thickness):
|
| 11 |
try:
|
| 12 |
+
# Create outer shell
|
| 13 |
outer_shell = (
|
| 14 |
cq.Workplane("XY")
|
| 15 |
.lineTo(0, height)
|
|
|
|
| 17 |
.close()
|
| 18 |
.revolve()
|
| 19 |
)
|
| 20 |
+
# Create inner shell
|
| 21 |
inner_shell = (
|
| 22 |
cq.Workplane("XY")
|
| 23 |
.lineTo(0, height - thickness)
|
|
|
|
| 25 |
.close()
|
| 26 |
.revolve()
|
| 27 |
)
|
| 28 |
+
# Subtract inner shell from outer to create a hollow shell
|
| 29 |
+
shell = outer_shell.cut(inner_shell)
|
| 30 |
+
return shell
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"Error in shell creation: {e}")
|
| 33 |
return None
|
| 34 |
|
| 35 |
+
|
| 36 |
+
# Function to generate a 2D visualization using Matplotlib
|
| 37 |
def generate_2d_image(height, radius):
|
| 38 |
try:
|
| 39 |
theta = np.linspace(0, 2 * np.pi, 100)
|
|
|
|
| 56 |
print(f"Error in generating 2D image: {e}")
|
| 57 |
return None
|
| 58 |
|
| 59 |
+
|
| 60 |
+
# Function to generate a 3D STL file using CadQuery
|
| 61 |
def generate_3d_stl(height, radius, thickness):
|
| 62 |
try:
|
| 63 |
bird_shell_model = create_bird_ear_shell(height, radius, thickness)
|
|
|
|
| 71 |
print(f"Error in generating 3D STL: {e}")
|
| 72 |
return None
|
| 73 |
|
| 74 |
+
|
| 75 |
+
# Function to create a 3D surface plot using Matplotlib
|
| 76 |
+
def plot_3d_surface(height, radius):
|
| 77 |
+
try:
|
| 78 |
+
fig = plt.figure(figsize=(8, 8))
|
| 79 |
+
ax = fig.add_subplot(111, projection="3d")
|
| 80 |
+
|
| 81 |
+
# Generate dummy data for the 3D surface
|
| 82 |
+
theta = np.linspace(0, 2 * np.pi, 100)
|
| 83 |
+
z = np.linspace(0, height, 50)
|
| 84 |
+
theta, z = np.meshgrid(theta, z)
|
| 85 |
+
x = (radius + 5 * np.cos(z)) * np.cos(theta)
|
| 86 |
+
y = (radius + 5 * np.cos(z)) * np.sin(theta)
|
| 87 |
+
|
| 88 |
+
# Plot the surface
|
| 89 |
+
ax.plot_surface(x, y, z, cmap="viridis", edgecolor="k", alpha=0.8)
|
| 90 |
+
ax.set_title("3D Surface of Shell")
|
| 91 |
+
ax.set_xlabel("X")
|
| 92 |
+
ax.set_ylabel("Y")
|
| 93 |
+
ax.set_zlabel("Height")
|
| 94 |
+
plt.savefig("3d_surface_plot.png")
|
| 95 |
+
return "3d_surface_plot.png"
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"Error in generating 3D plot: {e}")
|
| 98 |
+
return None
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# Main function to generate visuals
|
| 102 |
def generate_visuals(height, radius, thickness):
|
| 103 |
two_d_image = generate_2d_image(height, radius)
|
| 104 |
three_d_model = generate_3d_stl(height, radius, thickness)
|
| 105 |
+
three_d_plot = plot_3d_surface(height, radius)
|
| 106 |
+
return two_d_image, three_d_model, three_d_plot
|
| 107 |
+
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
# Gradio Interface
|
| 110 |
interface = gr.Interface(
|
| 111 |
fn=generate_visuals,
|
| 112 |
inputs=[
|
|
|
|
| 117 |
outputs=[
|
| 118 |
gr.Image(label="2D Visualization"),
|
| 119 |
gr.File(label="Download 3D Model (STL)"),
|
| 120 |
+
gr.Image(label="3D Surface Visualization"),
|
| 121 |
],
|
| 122 |
title="Ear-Like Bird Shell Simulation",
|
| 123 |
+
description="Input parameters to create an ear-like bird shell model. Visualize it in 2D and 3D, and download the 3D STL file."
|
| 124 |
)
|
| 125 |
|
| 126 |
interface.launch()
|