ENH: speed up the progress of loading step

Make the shape triangulation to be parallel

Signed-off-by: salt.wei <salt.wei@bambulab.com>
Change-Id: I0fe68359111b498458d431492841c54d13d7a7df
This commit is contained in:
salt.wei 2022-08-17 15:55:34 +08:00 committed by Lane.Wei
parent 6883c89ed5
commit 440bcc8090

View file

@ -257,22 +257,10 @@ bool load_step(const char *path, Model *model, ImportStepProgressFn proFn, StepI
getNamedSolids(TopLoc_Location{}, "", id, shapeTool, topLevelShapes.Value(iLabel), namedSolids); getNamedSolids(TopLoc_Location{}, "", id, shapeTool, topLevelShapes.Value(iLabel), namedSolids);
} }
ModelObject* new_object = model->add_object(); std::vector<stl_file> stl;
const char *last_slash = strrchr(path, DIR_SEPARATOR); stl.resize(namedSolids.size());
new_object->name.assign((last_slash == nullptr) ? path : last_slash + 1); tbb::parallel_for(tbb::blocked_range<size_t>(0, namedSolids.size()), [&](const tbb::blocked_range<size_t> &range) {
new_object->input_file = path; for (size_t i = range.begin(); i < range.end(); i++) {
for (size_t i = 0; i < namedSolids.size(); ++i) {
if (proFn) {
proFn(LOAD_STEP_STAGE_GET_MESH, i, namedSolids.size(), cb_cancel);
if (cb_cancel) {
model->delete_object(new_object);
shapeTool.reset(nullptr);
application->Close(document);
return false;
}
}
BRepMesh_IncrementalMesh mesh(namedSolids[i].solid, STEP_TRANS_CHORD_ERROR, false, STEP_TRANS_ANGLE_RES, true); BRepMesh_IncrementalMesh mesh(namedSolids[i].solid, STEP_TRANS_CHORD_ERROR, false, STEP_TRANS_ANGLE_RES, true);
// BBS: calculate total number of the nodes and triangles // BBS: calculate total number of the nodes and triangles
int aNbNodes = 0; int aNbNodes = 0;
@ -286,16 +274,14 @@ bool load_step(const char *path, Model *model, ImportStepProgressFn proFn, StepI
} }
} }
if (aNbTriangles == 0) { if (aNbTriangles == 0)
// BBS: No triangulation on the shape. // BBS: No triangulation on the shape.
continue; continue;
}
stl_file stl; stl[i].stats.type = inmemory;
stl.stats.type = inmemory; stl[i].stats.number_of_facets = (uint32_t) aNbTriangles;
stl.stats.number_of_facets = (uint32_t)aNbTriangles; stl[i].stats.original_num_facets = stl[i].stats.number_of_facets;
stl.stats.original_num_facets = stl.stats.number_of_facets; stl_allocate(&stl[i]);
stl_allocate(&stl);
std::vector<Vec3f> points; std::vector<Vec3f> points;
points.reserve(aNbNodes); points.reserve(aNbNodes);
@ -321,41 +307,50 @@ bool load_step(const char *path, Model *model, ImportStepProgressFn proFn, StepI
} }
// BBS: copy triangles // BBS: copy triangles
const TopAbs_Orientation anOrientation = anExpSF.Current().Orientation(); const TopAbs_Orientation anOrientation = anExpSF.Current().Orientation();
Standard_Integer anId[3];
for (Standard_Integer aTriIter = 1; aTriIter <= aTriangulation->NbTriangles(); ++aTriIter) { for (Standard_Integer aTriIter = 1; aTriIter <= aTriangulation->NbTriangles(); ++aTriIter) {
Poly_Triangle aTri = aTriangulation->Triangle(aTriIter); Poly_Triangle aTri = aTriangulation->Triangle(aTriIter);
Standard_Integer anId[3];
aTri.Get(anId[0], anId[1], anId[2]); aTri.Get(anId[0], anId[1], anId[2]);
if (anOrientation == TopAbs_REVERSED) { if (anOrientation == TopAbs_REVERSED)
//BBS: swap 1, 2. std::swap(anId[1], anId[2]);
Standard_Integer aTmpIdx = anId[1];
anId[1] = anId[2];
anId[2] = aTmpIdx;
}
//BBS: Update nodes according to the offset.
anId[0] += aNodeOffset;
anId[1] += aNodeOffset;
anId[2] += aNodeOffset;
// BBS: save triangles facets // BBS: save triangles facets
stl_facet facet; stl_facet facet;
facet.vertex[0] = points[anId[0] - 1].cast<float>(); facet.vertex[0] = points[anId[0] + aNodeOffset - 1].cast<float>();
facet.vertex[1] = points[anId[1] - 1].cast<float>(); facet.vertex[1] = points[anId[1] + aNodeOffset - 1].cast<float>();
facet.vertex[2] = points[anId[2] - 1].cast<float>(); facet.vertex[2] = points[anId[2] + aNodeOffset - 1].cast<float>();
facet.extra[0] = 0; facet.extra[0] = 0;
facet.extra[1] = 0; facet.extra[1] = 0;
stl_normal normal; stl_normal normal;
stl_calculate_normal(normal, &facet); stl_calculate_normal(normal, &facet);
stl_normalize_vector(normal); stl_normalize_vector(normal);
facet.normal = normal; facet.normal = normal;
stl.facet_start[aTriangleOffet + aTriIter - 1] = facet; stl[i].facet_start[aTriangleOffet + aTriIter - 1] = facet;
} }
aNodeOffset += aTriangulation->NbNodes(); aNodeOffset += aTriangulation->NbNodes();
aTriangleOffet += aTriangulation->NbTriangles(); aTriangleOffet += aTriangulation->NbTriangles();
} }
}
});
ModelObject *new_object = model->add_object();
const char * last_slash = strrchr(path, DIR_SEPARATOR);
new_object->name.assign((last_slash == nullptr) ? path : last_slash + 1);
new_object->input_file = path;
for (size_t i = 0; i < stl.size(); i++) {
if (proFn) {
proFn(LOAD_STEP_STAGE_GET_MESH, i, namedSolids.size(), cb_cancel);
if (cb_cancel) {
model->delete_object(new_object);
shapeTool.reset(nullptr);
application->Close(document);
return false;
}
}
TriangleMesh triangle_mesh; TriangleMesh triangle_mesh;
triangle_mesh.from_stl(stl); triangle_mesh.from_stl(stl[i]);
ModelVolume *new_volume = new_object->add_volume(std::move(triangle_mesh)); ModelVolume *new_volume = new_object->add_volume(std::move(triangle_mesh));
new_volume->name = namedSolids[i].name; new_volume->name = namedSolids[i].name;
new_volume->source.input_file = path; new_volume->source.input_file = path;