TriangleMeshSlicer

replaced the vectors of vectors by vectors of indices to a continuous memory,
using binary search to get to an index.
This commit is contained in:
bubnikv 2017-03-08 09:47:32 +01:00
parent edd7cabf68
commit 1e6cf0cd98

View file

@ -689,7 +689,7 @@ TriangleMeshSlicer::slice(const std::vector<float> &z, std::vector<Polygons>* la
BOOST_LOG_TRIVIAL(debug) << "TriangleMeshSlicer::_make_loops_do"; BOOST_LOG_TRIVIAL(debug) << "TriangleMeshSlicer::_make_loops_do";
layers->resize(z.size()); layers->resize(z.size());
tbb::parallel_for( tbb::parallel_for(
tbb::blocked_range<size_t>(0, lines.size()), tbb::blocked_range<size_t>(0, z.size()),
[&lines, &layers, this](const tbb::blocked_range<size_t>& range) { [&lines, &layers, this](const tbb::blocked_range<size_t>& range) {
for (size_t line_idx = range.begin(); line_idx < range.end(); ++ line_idx) for (size_t line_idx = range.begin(); line_idx < range.end(); ++ line_idx)
this->make_loops(lines[line_idx], &(*layers)[line_idx]); this->make_loops(lines[line_idx], &(*layers)[line_idx]);
@ -914,18 +914,23 @@ void TriangleMeshSlicer::make_loops(std::vector<IntersectionLine> &lines, Polygo
} }
} }
// build a map of lines by edge_a_id and a_id // Build a map of lines by edge_a_id and a_id.
//FIXME replace the vectors of vectors by vectors of indices to a continuous memory. std::vector<IntersectionLine*> by_edge_a_id;
std::vector<IntersectionLinePtrs> by_edge_a_id(this->mesh->stl.stats.number_of_facets * 3); std::vector<IntersectionLine*> by_a_id;
std::vector<IntersectionLinePtrs> by_a_id(this->mesh->stl.stats.shared_vertices); by_edge_a_id.reserve(lines.size());
by_a_id.reserve(lines.size());
for (IntersectionLines::iterator line = lines.begin(); line != lines.end(); ++ line) { for (IntersectionLines::iterator line = lines.begin(); line != lines.end(); ++ line) {
if (! line->skip) { if (! line->skip) {
if (line->edge_a_id != -1) if (line->edge_a_id != -1)
by_edge_a_id[line->edge_a_id].push_back(&(*line)); by_edge_a_id.push_back(&(*line)); // [line->edge_a_id].push_back();
if (line->a_id != -1) if (line->a_id != -1)
by_a_id[line->a_id].push_back(&(*line)); by_a_id.push_back(&(*line)); // [line->a_id].push_back(&(*line));
} }
} }
auto by_edge_lower = [](const IntersectionLine* il1, const IntersectionLine *il2) { return il1->edge_a_id < il2->edge_a_id; };
auto by_vertex_lower = [](const IntersectionLine* il1, const IntersectionLine *il2) { return il1->a_id < il2->a_id; };
std::sort(by_edge_a_id.begin(), by_edge_a_id.end(), by_edge_lower);
std::sort(by_a_id.begin(), by_a_id.end(), by_vertex_lower);
IntersectionLines::iterator it_line_seed = lines.begin(); IntersectionLines::iterator it_line_seed = lines.begin();
CYCLE: while (1) { CYCLE: while (1) {
@ -949,25 +954,34 @@ void TriangleMeshSlicer::make_loops(std::vector<IntersectionLine> &lines, Polygo
first_line->a.x, first_line->a.y, first_line->b.x, first_line->b.y); first_line->a.x, first_line->a.y, first_line->b.x, first_line->b.y);
*/ */
IntersectionLine key;
for (;;) { for (;;) {
// find a line starting where last one finishes // find a line starting where last one finishes
IntersectionLine* next_line = nullptr; IntersectionLine* next_line = nullptr;
if (last_line->edge_b_id != -1) { if (last_line->edge_b_id != -1) {
IntersectionLinePtrs &candidates = by_edge_a_id[last_line->edge_b_id]; key.edge_a_id = last_line->edge_b_id;
for (IntersectionLinePtrs::iterator lineptr = candidates.begin(); lineptr != candidates.end(); ++ lineptr) auto it_begin = std::lower_bound(by_edge_a_id.begin(), by_edge_a_id.end(), &key, by_edge_lower);
if (! (*lineptr)->skip) { if (it_begin != by_edge_a_id.end()) {
next_line = *lineptr; auto it_end = std::upper_bound(it_begin, by_edge_a_id.end(), &key, by_edge_lower);
for (auto it_line = it_begin; it_line != it_end; ++ it_line)
if (! (*it_line)->skip) {
next_line = *it_line;
break; break;
} }
} }
}
if (next_line == nullptr && last_line->b_id != -1) { if (next_line == nullptr && last_line->b_id != -1) {
IntersectionLinePtrs &candidates = by_a_id[last_line->b_id]; key.a_id = last_line->b_id;
for (IntersectionLinePtrs::iterator lineptr = candidates.begin(); lineptr != candidates.end(); ++ lineptr) auto it_begin = std::lower_bound(by_a_id.begin(), by_a_id.end(), &key, by_vertex_lower);
if (! (*lineptr)->skip) { if (it_begin != by_a_id.end()) {
next_line = *lineptr; auto it_end = std::upper_bound(it_begin, by_a_id.end(), &key, by_vertex_lower);
for (auto it_line = it_begin; it_line != it_end; ++ it_line)
if (! (*it_line)->skip) {
next_line = *it_line;
break; break;
} }
} }
}
if (next_line == nullptr) { if (next_line == nullptr) {
// check whether we closed this loop // check whether we closed this loop
if ((first_line->edge_a_id != -1 && first_line->edge_a_id == last_line->edge_b_id) || if ((first_line->edge_a_id != -1 && first_line->edge_a_id == last_line->edge_b_id) ||