File size: 2,688 Bytes
e96a69a
 
 
 
7b3796c
e96a69a
7b3796c
e96a69a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b3796c
e96a69a
 
 
 
 
7b3796c
e96a69a
 
 
 
 
 
 
 
 
 
 
 
7b3796c
e96a69a
 
 
 
 
 
 
7b3796c
e96a69a
 
 
 
 
 
 
7b3796c
e96a69a
 
 
 
 
 
 
 
 
 
 
7b3796c
e96a69a
 
7b3796c
 
e96a69a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import cadquery as cq
import matplotlib.pyplot as plt
import numpy as np
import trimesh
import gradio as gr
from cadquery.occ_impl.exporters import exportShape

# Function to create the CAD model
def create_bird_ear_shell(height, radius, thickness):
    outer_shell = (
        cq.Workplane("XY")
        .lineTo(0, height)                # Vertical height
        .radiusArc((radius, 0), radius)  # Curve to base
        .close()
        .revolve()                       # Revolve to create 3D shape
    )
    inner_shell = (
        cq.Workplane("XY")
        .lineTo(0, height - thickness)
        .radiusArc((radius - thickness, 0), radius - thickness)
        .close()
        .revolve()
    )
    shell = outer_shell.cut(inner_shell)
    return shell

# Function to generate a 2D visualization
def generate_2d_image(height, radius):
    theta = np.linspace(0, 2 * np.pi, 100)
    x_outer = radius * np.cos(theta)
    y_outer = height * np.sin(theta) / max(np.sin(theta))

    plt.figure(figsize=(6, 6))
    plt.plot(x_outer, y_outer, label="Outer Shell", color="blue")
    plt.fill_between(x_outer, y_outer, color="lightblue", alpha=0.5)
    plt.axhline(0, color="black", linewidth=0.5, linestyle="--")
    plt.axvline(0, color="black", linewidth=0.5, linestyle="--")
    plt.title("2D Cross-Section of Ear-Like Bird Shell")
    plt.xlabel("Radius (mm)")
    plt.ylabel("Height (mm)")
    plt.grid()
    plt.legend()
    plt.savefig("2d_visualization.png")
    return "2d_visualization.png"

# Function to generate a 3D STL file
def generate_3d_stl(height, radius, thickness):
    bird_shell_model = create_bird_ear_shell(height, radius, thickness)
    stl_path = "ear_bird_shell.stl"
    with open(stl_path, "wb") as f:
        exportShape(bird_shell_model.val(), "STL", f)
    return stl_path

# Gradio interface functions
def generate_visuals(height, radius, thickness):
    # Generate 2D image
    two_d_image = generate_2d_image(height, radius)
    # Generate 3D STL file
    three_d_model = generate_3d_stl(height, radius, thickness)
    return two_d_image, three_d_model

# Gradio interface
interface = gr.Interface(
    fn=generate_visuals,
    inputs=[
        gr.Number(label="Height (mm)", value=50.0),
        gr.Number(label="Radius (mm)", value=25.0),
        gr.Number(label="Wall Thickness (mm)", value=3.0),
    ],
    outputs=[
        gr.Image(label="2D Visualization"),
        gr.File(label="Download 3D Model (STL)"),
    ],
    title="Ear-Like Bird Shell Simulation",
    description="Input the parameters to create an ear-like bird shell model. You can visualize it in 2D and download the 3D STL file for further use."
)

# Launch the app
interface.launch()