diff --git a/src/api/routes.py b/src/api/routes.py index 5a212a2..50f36fb 100644 --- a/src/api/routes.py +++ b/src/api/routes.py @@ -468,14 +468,16 @@ async def process_file_core( try: mesh_result = mesh_generator.generate_mesh_from_shape(shape) - tri_mesh = mesh_result.get("trimesh_mesh") - pointcloud = mesh_result.get("pointcloud") or {} - - if tri_mesh is not None: - # 顶点和面转为 JSON 可序列化 - vertices = tri_mesh.vertices.tolist() - faces = tri_mesh.faces.tolist() + # mesh_generator 返回: vertices, faces, points, normals, point_count, vertex_count, face_count + vertices = mesh_result.get("vertices", []) + faces = mesh_result.get("faces", []) + points = mesh_result.get("points", []) + normals = mesh_result.get("normals", []) + point_count = mesh_result.get("point_count", 0) + vertex_count = mesh_result.get("vertex_count", 0) + face_count = mesh_result.get("face_count", 0) + if vertices and faces: # 使用已计算的几何边界框,避免重复计算 bbox = geometry_data.get("bounding_box", {}) @@ -484,15 +486,19 @@ async def process_file_core( "file_name": Path(file_path).name, "generated_at": datetime.now().isoformat(), "quality": "medium", - "vertex_count": len(vertices), - "face_count": len(faces), - "point_count": pointcloud.get("count"), + "vertex_count": vertex_count, + "face_count": face_count, + "point_count": point_count, }, "mesh": { "vertices": vertices, "faces": faces, }, - "pointcloud": pointcloud, + "pointcloud": { + "points": points, + "normals": normals, + "count": point_count + }, "bounding_box": bbox, } @@ -504,9 +510,9 @@ async def process_file_core( ) # 将简要网格摘要写入内存任务,便于前端展示汇总信息 tasks[task_id]["mesh_summary"] = { - "vertex_count": len(vertices), - "face_count": len(faces), - "point_count": pointcloud.get("count"), + "vertex_count": vertex_count, + "face_count": face_count, + "point_count": point_count, "quality": "medium", } except Exception as mesh_err: @@ -653,8 +659,14 @@ async def process_file_core( db_session, task_id, "processing", 85, "生成可视化报告" ) - # 获取点云数据 - pointcloud_data = mesh_result.get("pointcloud", {}) if mesh_result else {} + # 获取点云数据 - mesh_result 直接返回 points 和 normals + pointcloud_data = None + if mesh_result: + pointcloud_data = { + "points": mesh_result.get("points", []), + "normals": mesh_result.get("normals", []), + "point_count": mesh_result.get("point_count", 0) + } html_file_path = html_generator.generate_and_save_visualization( geometry_data, diff --git a/src/core/mesh_generator.py b/src/core/mesh_generator.py index b1b68d9..f2f6b86 100644 --- a/src/core/mesh_generator.py +++ b/src/core/mesh_generator.py @@ -3,6 +3,7 @@ import logging import numpy as np from typing import Dict, List, Optional, Any import trimesh +from trimesh import sample as trimesh_sample from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh from OCC.Core.TopExp import TopExp_Explorer from OCC.Core.TopAbs import TopAbs_FACE @@ -23,7 +24,7 @@ class MeshGenerator: } self.quality = self.quality_settings.get(quality, 0.3) - def generate_mesh_from_shape(self, shape, num_points: int = 50000) -> Dict: + def generate_mesh_from_shape(self, shape, num_points: int = 20000) -> Dict: """从PythonOCC形状生成点云数据""" try: # 生成网格 - 使用更精细的网格 @@ -68,7 +69,7 @@ class MeshGenerator: pnt = face_triangulation.Node(i) # 应用位置变换 pnt.Transform(trsf) - face_vertices.append([pnt.X(), pnt.Y(), pnt.Z()]) + face_vertices.append([float(pnt.X()), float(pnt.Y()), float(pnt.Z())]) # 提取三角形索引 face_indices = [] @@ -103,8 +104,12 @@ class MeshGenerator: # 创建Trimesh对象 tri_mesh = trimesh.Trimesh(vertices=vertices, faces=faces, process=True) + # 根据网格大小动态调整采样点数 + actual_num_points = min(num_points, len(faces) * 2) + logger.info(f"采样点数: {actual_num_points}") + # 在网格表面采样点云 - points, face_idx = trimesh.sample.sample_surface(tri_mesh, num_points) + points, face_idx = trimesh.sample.sample_surface(tri_mesh, actual_num_points) # 获取法向量 normals = tri_mesh.face_normals[face_idx] @@ -116,9 +121,9 @@ class MeshGenerator: "faces": faces.tolist(), "points": points.tolist(), "normals": normals.tolist(), - "point_count": len(points), - "vertex_count": len(vertices), - "face_count": len(faces) + "point_count": int(len(points)), + "vertex_count": int(len(vertices)), + "face_count": int(len(faces)) } except Exception as e: