Skip to content

Commit

Permalink
Use unordered flat containers
Browse files Browse the repository at this point in the history
  • Loading branch information
unageek committed Sep 14, 2024
1 parent 4c8f09c commit 4a4fb62
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Here are the timings (in seconds) for computing the Boolean intersection between
|-------------------|-----------------------:|--------------------------:|---------------:|--------------:|-------------------------:|------------------------:|--------------:|--------------------:|
| **Open** | 4.6 | FAILED | 2.0 | 1.1 | FAILED | FAILED | FAILED | FAILED |
| **Open & closed** | FAILED | 70.5 | 1.3 | 0.7 | FAILED | FAILED | FAILED | FAILED |
| **Closed** | 57.4 | FAILED | 4.7 | 2.2 | 61.0 | 8.9 | 1.7 | 24.5 |
| **Closed** | 57.4 | FAILED | 4.6 | 2.2 | 61.0 | 8.9 | 1.7 | 24.5 |
| **Non-manifold** | FAILED | FAILED | 0.5 | 0.2 | FAILED | FAILED | FAILED | FAILED |

¹ Ran with `KIGUMI_NUM_THREADS=1`. ² `igl::copyleft::cgal::mesh_boolean` with `CGAL::Lazy_exact_nt<mpq_class>` as the
Expand Down
59 changes: 32 additions & 27 deletions include/kigumi/Corefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#include <algorithm>
#include <boost/container/static_vector.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
#include <functional>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -126,16 +127,17 @@ class Corefine {
std::sort(infos_.begin(), infos_.end(), left_fi_less);

std::vector<boost::iterator_range<typename decltype(infos_)::const_iterator>> ranges;
{
auto first = infos_.begin();
while (first != infos_.end()) {
auto fi = first->left_fi;
left_triangulations_.emplace(fi, std::nullopt);

auto last = std::upper_bound(first, infos_.end(), *first, left_fi_less);
ranges.emplace_back(first, last);
first = last;
}
for (auto first = infos_.begin(); first != infos_.end();) {
auto last = std::upper_bound(first, infos_.end(), *first, left_fi_less);
ranges.emplace_back(first, last);
first = last;
}

left_triangulations_.reserve(ranges.size());
for (const auto& range : ranges) {
const auto& any_info = range.front();
auto fi = any_info.left_fi;
left_triangulations_.emplace(fi, std::nullopt);
}

try {
Expand Down Expand Up @@ -166,16 +168,17 @@ class Corefine {
std::sort(infos_.begin(), infos_.end(), right_fi_less);

ranges.clear();
{
auto first = infos_.begin();
while (first != infos_.end()) {
auto fi = first->right_fi;
right_triangulations_.emplace(fi, std::nullopt);

auto last = std::upper_bound(first, infos_.end(), *first, right_fi_less);
ranges.emplace_back(first, last);
first = last;
}
for (auto first = infos_.begin(); first != infos_.end();) {
auto last = std::upper_bound(first, infos_.end(), *first, right_fi_less);
ranges.emplace_back(first, last);
first = last;
}

right_triangulations_.reserve(ranges.size());
for (const auto& range : ranges) {
const auto& any_info = range.front();
auto fi = any_info.right_fi;
right_triangulations_.emplace(fi, std::nullopt);
}

try {
Expand Down Expand Up @@ -244,10 +247,10 @@ class Corefine {
};

template <class OutputIterator>
void get_triangles(
const Triangle_soup& soup, Face_index fi,
const std::unordered_map<Face_index, std::optional<Triangulation>>& triangulations,
const std::vector<std::size_t>& point_ids, OutputIterator tris) const {
void get_triangles(const Triangle_soup& soup, Face_index fi,
const boost::unordered_flat_map<Face_index, std::optional<Triangulation>,
std::hash<Face_index>>& triangulations,
const std::vector<std::size_t>& point_ids, OutputIterator tris) const {
auto it = triangulations.find(fi);
if (it == triangulations.end()) {
const auto& f = soup.face(fi);
Expand Down Expand Up @@ -282,9 +285,11 @@ class Corefine {
}

const Triangle_soup& left_;
std::unordered_map<Face_index, std::optional<Triangulation>> left_triangulations_;
boost::unordered_flat_map<Face_index, std::optional<Triangulation>, std::hash<Face_index>>
left_triangulations_;
const Triangle_soup& right_;
std::unordered_map<Face_index, std::optional<Triangulation>> right_triangulations_;
boost::unordered_flat_map<Face_index, std::optional<Triangulation>, std::hash<Face_index>>
right_triangulations_;
Point_list points_;
std::vector<std::size_t> left_point_ids_;
std::vector<std::size_t> right_point_ids_;
Expand Down

0 comments on commit 4a4fb62

Please sign in to comment.