Spaces:
Running
Running
Commit
·
65927d1
1
Parent(s):
4ec61f7
Fix indentation and exception pairing in create_3d_model
Browse files
app.py
CHANGED
@@ -147,28 +147,80 @@ def create_3d_model(buildings: List[Dict]) -> trimesh.Scene:
|
|
147 |
except Exception as e:
|
148 |
if debug: print(f" ↳ skipping: polygon error: {e}")
|
149 |
continue
|
150 |
-
|
151 |
-
# **Extrude + orient + add all in one try/except**
|
152 |
try:
|
153 |
-
#
|
|
|
|
|
|
|
154 |
try:
|
155 |
extruded = trimesh.creation.extrude_polygon(polygon, height, engine="triangle")
|
|
|
156 |
except (ImportError, ValueError) as e:
|
157 |
-
if debug:
|
158 |
-
print(f" ↳ triangle engine error ({e}), falling back to earcut")
|
159 |
-
extruded = trimesh.creation.extrude_polygon(polygon, height, engine="earcut")
|
160 |
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
-
#
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
except Exception as e:
|
174 |
if debug: print(f" ↳ skipping: extrusion/orientation error: {e}")
|
@@ -410,7 +462,6 @@ app = create_gradio_app()
|
|
410 |
# Launch the app
|
411 |
if __name__ == "__main__":
|
412 |
app.launch(
|
413 |
-
share=True, # Creates public link for Hugging Face
|
414 |
server_name="0.0.0.0", # Allow external connections
|
415 |
server_port=7860, # Standard port for Hugging Face
|
416 |
show_error=True,
|
|
|
147 |
except Exception as e:
|
148 |
if debug: print(f" ↳ skipping: polygon error: {e}")
|
149 |
continue
|
150 |
+
# **Extrude + orient + add all in one try/except**
|
|
|
151 |
try:
|
152 |
+
# Try multiple extrusion methods in order of preference
|
153 |
+
extruded = None
|
154 |
+
|
155 |
+
# Method 1: Try triangle engine
|
156 |
try:
|
157 |
extruded = trimesh.creation.extrude_polygon(polygon, height, engine="triangle")
|
158 |
+
if debug: print(" ↳ used triangle engine")
|
159 |
except (ImportError, ValueError) as e:
|
160 |
+
if debug: print(f" ↳ triangle engine error ({e})")
|
|
|
|
|
161 |
|
162 |
+
# Method 2: Try earcut engine
|
163 |
+
if extruded is None:
|
164 |
+
try:
|
165 |
+
extruded = trimesh.creation.extrude_polygon(polygon, height, engine="earcut")
|
166 |
+
if debug: print(" ↳ used earcut engine")
|
167 |
+
except (ImportError, ValueError) as e:
|
168 |
+
if debug: print(f" ↳ earcut engine error ({e})")
|
169 |
|
170 |
+
# Method 3: Manual extrusion fallback
|
171 |
+
if extruded is None:
|
172 |
+
if debug: print(" ↳ using manual extrusion fallback")
|
173 |
+
|
174 |
+
# Get polygon exterior coordinates
|
175 |
+
coords = list(polygon.exterior.coords[:-1]) # Remove duplicate last point
|
176 |
+
|
177 |
+
# Create bottom vertices
|
178 |
+
bottom_vertices = np.array([[x, y, 0] for x, y in coords])
|
179 |
+
# Create top vertices
|
180 |
+
top_vertices = np.array([[x, y, height] for x, y in coords])
|
181 |
+
# Combine all vertices
|
182 |
+
vertices = np.vstack([bottom_vertices, top_vertices])
|
183 |
+
|
184 |
+
# Create faces
|
185 |
+
faces = []
|
186 |
+
n = len(coords)
|
187 |
+
|
188 |
+
# Triangulate bottom face (reversed for correct normal)
|
189 |
+
if n >= 3:
|
190 |
+
bottom_triangles = simple_triangulate_polygon(list(range(n)))
|
191 |
+
for tri in bottom_triangles:
|
192 |
+
faces.append([tri[2], tri[1], tri[0]]) # Reverse for correct normal
|
193 |
+
|
194 |
+
# Triangulate top face
|
195 |
+
if n >= 3:
|
196 |
+
top_triangles = simple_triangulate_polygon(list(range(n)))
|
197 |
+
for tri in top_triangles:
|
198 |
+
faces.append([tri[0] + n, tri[1] + n, tri[2] + n])
|
199 |
+
|
200 |
+
# Side faces
|
201 |
+
for i in range(n):
|
202 |
+
next_i = (i + 1) % n
|
203 |
+
# Two triangles per side
|
204 |
+
faces.append([i, next_i, next_i + n])
|
205 |
+
faces.append([i, next_i + n, i + n])
|
206 |
+
|
207 |
+
extruded = trimesh.Trimesh(vertices=vertices, faces=faces)
|
208 |
+
if debug: print(" ↳ manual extrusion successful")
|
209 |
|
210 |
+
if extruded is not None:
|
211 |
+
if debug:
|
212 |
+
print(f" ↳ extruded mesh vertices={len(extruded.vertices)}, faces={len(extruded.faces)}")
|
213 |
+
|
214 |
+
# orientation fix
|
215 |
+
transform_x = trimesh.transformations.rotation_matrix(np.pi/2, (1, 0, 0))
|
216 |
+
transform_z = trimesh.transformations.rotation_matrix(np.pi, (0, 0, 1))
|
217 |
+
extruded.apply_transform(transform_x)
|
218 |
+
extruded.apply_transform(transform_z)
|
219 |
+
|
220 |
+
scene.add_geometry(extruded)
|
221 |
+
if debug: print(" ↳ added to scene")
|
222 |
+
else:
|
223 |
+
if debug: print(" ↳ all extrusion methods failed")
|
224 |
|
225 |
except Exception as e:
|
226 |
if debug: print(f" ↳ skipping: extrusion/orientation error: {e}")
|
|
|
462 |
# Launch the app
|
463 |
if __name__ == "__main__":
|
464 |
app.launch(
|
|
|
465 |
server_name="0.0.0.0", # Allow external connections
|
466 |
server_port=7860, # Standard port for Hugging Face
|
467 |
show_error=True,
|