This commit is contained in:
2026-03-07 01:40:37 +08:00
parent 57a67309bd
commit 4ce0662df1
2 changed files with 39 additions and 22 deletions
+28 -16
View File
@@ -468,14 +468,16 @@ async def process_file_core(
try: try:
mesh_result = mesh_generator.generate_mesh_from_shape(shape) mesh_result = mesh_generator.generate_mesh_from_shape(shape)
tri_mesh = mesh_result.get("trimesh_mesh") # mesh_generator 返回: vertices, faces, points, normals, point_count, vertex_count, face_count
pointcloud = mesh_result.get("pointcloud") or {} vertices = mesh_result.get("vertices", [])
faces = mesh_result.get("faces", [])
if tri_mesh is not None: points = mesh_result.get("points", [])
# 顶点和面转为 JSON 可序列化 normals = mesh_result.get("normals", [])
vertices = tri_mesh.vertices.tolist() point_count = mesh_result.get("point_count", 0)
faces = tri_mesh.faces.tolist() 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", {}) bbox = geometry_data.get("bounding_box", {})
@@ -484,15 +486,19 @@ async def process_file_core(
"file_name": Path(file_path).name, "file_name": Path(file_path).name,
"generated_at": datetime.now().isoformat(), "generated_at": datetime.now().isoformat(),
"quality": "medium", "quality": "medium",
"vertex_count": len(vertices), "vertex_count": vertex_count,
"face_count": len(faces), "face_count": face_count,
"point_count": pointcloud.get("count"), "point_count": point_count,
}, },
"mesh": { "mesh": {
"vertices": vertices, "vertices": vertices,
"faces": faces, "faces": faces,
}, },
"pointcloud": pointcloud, "pointcloud": {
"points": points,
"normals": normals,
"count": point_count
},
"bounding_box": bbox, "bounding_box": bbox,
} }
@@ -504,9 +510,9 @@ async def process_file_core(
) )
# 将简要网格摘要写入内存任务,便于前端展示汇总信息 # 将简要网格摘要写入内存任务,便于前端展示汇总信息
tasks[task_id]["mesh_summary"] = { tasks[task_id]["mesh_summary"] = {
"vertex_count": len(vertices), "vertex_count": vertex_count,
"face_count": len(faces), "face_count": face_count,
"point_count": pointcloud.get("count"), "point_count": point_count,
"quality": "medium", "quality": "medium",
} }
except Exception as mesh_err: except Exception as mesh_err:
@@ -653,8 +659,14 @@ async def process_file_core(
db_session, task_id, "processing", 85, "生成可视化报告" db_session, task_id, "processing", 85, "生成可视化报告"
) )
# 获取点云数据 # 获取点云数据 - mesh_result 直接返回 points 和 normals
pointcloud_data = mesh_result.get("pointcloud", {}) if mesh_result else {} 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( html_file_path = html_generator.generate_and_save_visualization(
geometry_data, geometry_data,
+11 -6
View File
@@ -3,6 +3,7 @@ import logging
import numpy as np import numpy as np
from typing import Dict, List, Optional, Any from typing import Dict, List, Optional, Any
import trimesh import trimesh
from trimesh import sample as trimesh_sample
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Core.TopExp import TopExp_Explorer from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_FACE from OCC.Core.TopAbs import TopAbs_FACE
@@ -23,7 +24,7 @@ class MeshGenerator:
} }
self.quality = self.quality_settings.get(quality, 0.3) 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形状生成点云数据""" """从PythonOCC形状生成点云数据"""
try: try:
# 生成网格 - 使用更精细的网格 # 生成网格 - 使用更精细的网格
@@ -68,7 +69,7 @@ class MeshGenerator:
pnt = face_triangulation.Node(i) pnt = face_triangulation.Node(i)
# 应用位置变换 # 应用位置变换
pnt.Transform(trsf) 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 = [] face_indices = []
@@ -103,8 +104,12 @@ class MeshGenerator:
# 创建Trimesh对象 # 创建Trimesh对象
tri_mesh = trimesh.Trimesh(vertices=vertices, faces=faces, process=True) 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] normals = tri_mesh.face_normals[face_idx]
@@ -116,9 +121,9 @@ class MeshGenerator:
"faces": faces.tolist(), "faces": faces.tolist(),
"points": points.tolist(), "points": points.tolist(),
"normals": normals.tolist(), "normals": normals.tolist(),
"point_count": len(points), "point_count": int(len(points)),
"vertex_count": len(vertices), "vertex_count": int(len(vertices)),
"face_count": len(faces) "face_count": int(len(faces))
} }
except Exception as e: except Exception as e: