File size: 2,078 Bytes
4b1d354
 
 
 
 
 
f624bf7
4b1d354
22b6029
4b1d354
 
f624bf7
4b1d354
 
f624bf7
4b1d354
 
 
f624bf7
 
 
 
 
 
4b1d354
f624bf7
4b1d354
f624bf7
22b6029
 
f624bf7
 
4b1d354
22b6029
4b1d354
 
22b6029
 
4b1d354
f624bf7
 
 
 
4b1d354
 
 
f624bf7
 
4b1d354
 
f624bf7
 
4b1d354
22b6029
f624bf7
22b6029
f624bf7
22b6029
4b1d354
 
 
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
import gradio as gr
import trimesh
import numpy as np
import tempfile
import os

def reconstruct_perfect_cube(input_file):
    if input_file is None:
        return None, None
    
    try:
        # 1. Load the irregular STL
        mesh = trimesh.load(input_file.name)
        
        # Handle scenes if necessary
        if isinstance(mesh, trimesh.Scene):
            mesh = mesh.dump(concatenate=True)

        # 2. Advanced Reconstruction Logic: Bounding Box
        # Instead of wrapping the broken shape (Convex Hull), 
        # we calculate the 'Oriented Bounding Box'. 
        # This identifies the 'Proper' cube that would perfectly 
        # contain your irregular parts.
        perfect_cube = mesh.bounding_box_oriented

        # 3. Create a temporary path for the output
        temp_dir = tempfile.gettempdir()
        output_filename = "reconstructed_perfect_cube.stl"
        output_path = os.path.join(temp_dir, output_filename)
        
        # 4. Export the perfect solid cube
        perfect_cube.export(output_path)
        
        return output_path, output_path
    
    except Exception as e:
        print(f"Error: {e}")
        return None, None

# --- Gradio UI ---
with gr.Blocks(title="AI 3D Cube Completion", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🧊 Perfect Cube Generator")
    gr.Markdown("This tool analyzes irregular STL data and 'hallucinates' the missing sections to restore a mathematically perfect cube.")
    
    with gr.Row():
        with gr.Column():
            file_input = gr.File(label="Upload Irregular Cube STL", file_types=[".stl"])
            generate_btn = gr.Button("🚀 Generate Full Constructed Cube", variant="primary")
        
        with gr.Column():
            model_viewer = gr.Model3D(label="3D Preview of Reconstructed Cube")
            download_link = gr.File(label="Download Perfect STL File")

    generate_btn.click(
        fn=reconstruct_perfect_cube, 
        inputs=file_input, 
        outputs=[model_viewer, download_link]
    )

if __name__ == "__main__":
    demo.launch()