Add new (winner) neighbor index based on measurements

This commit is contained in:
tamasmeszaros 2021-06-02 15:45:11 +02:00
parent b14b000c73
commit c4507842a0
11 changed files with 327 additions and 125 deletions

View file

@ -74,29 +74,6 @@ public:
}
};
/// A very simple range concept implementation with iterator-like objects.
template<class It> class Range
{
It from, to;
public:
// The class is ready for range based for loops.
It begin() const { return from; }
It end() const { return to; }
// The iterator type can be obtained this way.
using Type = It;
Range() = default;
Range(It &&b, It &&e)
: from(std::forward<It>(b)), to(std::forward<It>(e))
{}
// Some useful container-like methods...
inline size_t size() const { return end() - begin(); }
inline bool empty() const { return size() == 0; }
};
template<class C> bool all_of(const C &container)
{
return std::all_of(container.begin(),

View file

@ -5,7 +5,6 @@
#include "libnest2d/tools/benchmark.h"
namespace Slic3r {
namespace meshsplit_detail {
template<class Its, class Enable = void> struct ItsWithNeighborsIndex_ {
@ -58,7 +57,7 @@ std::vector<size_t> its_find_unvisited_neighbors(
size_t facet_idx = pop();
const auto &neighbors = neighbor_index[facet_idx];
for (auto neighbor_idx : neighbors) {
if (neighbor_idx >= 0 && !visited[size_t(neighbor_idx)]) {
if (size_t(neighbor_idx) < visited.size() && !visited[size_t(neighbor_idx)]) {
visited[size_t(neighbor_idx)] = true;
push(stack_el(neighbor_idx));
ret.emplace_back(size_t(neighbor_idx));
@ -111,6 +110,8 @@ void its_split(const Its &m, OutputIt out_it)
// Create a new mesh for the part that was just split off.
indexed_triangle_set mesh;
mesh.indices.reserve(facets.size());
mesh.vertices.reserve(facets.size() * 3);
// Assign the facets to the new mesh.
for (size_t face_id : facets) {

View file

@ -60,11 +60,19 @@ using Matrix3d = Eigen::Matrix<double, 3, 3, Eigen::DontAlign>;
using Matrix4f = Eigen::Matrix<float, 4, 4, Eigen::DontAlign>;
using Matrix4d = Eigen::Matrix<double, 4, 4, Eigen::DontAlign>;
template<int N, class T>
using Transform = Eigen::Transform<float, N, Eigen::Affine, Eigen::DontAlign>;
using Transform2f = Eigen::Transform<float, 2, Eigen::Affine, Eigen::DontAlign>;
using Transform2d = Eigen::Transform<double, 2, Eigen::Affine, Eigen::DontAlign>;
using Transform3f = Eigen::Transform<float, 3, Eigen::Affine, Eigen::DontAlign>;
using Transform3d = Eigen::Transform<double, 3, Eigen::Affine, Eigen::DontAlign>;
// I don't know why Eigen::Transform::Identity() return a const object...
template<int N, class T> Transform<N, T> identity() { return Transform<N, T>::Identity(); }
inline const auto &identity3f = identity<3, float>;
inline const auto &identity3d = identity<3, double>;
inline bool operator<(const Vec2d &lhs, const Vec2d &rhs) { return lhs.x() < rhs.x() || (lhs.x() == rhs.x() && lhs.y() < rhs.y()); }
template<int Options>
@ -494,6 +502,7 @@ namespace cereal {
template<class Archive> void save(Archive& archive, Slic3r::Matrix2f &m) { archive.saveBinary((char*)m.data(), sizeof(float) * 4); }
}
// To be able to use Vec<> and Mat<> in range based for loops:
namespace Eigen {
template<class T, int N, int M>
T* begin(Slic3r::Mat<N, M, T> &mat) { return mat.data(); }

View file

@ -198,14 +198,14 @@ static std::vector<bool> create_exclude_mask(
std::vector<bool> exclude_mask(its.indices.size(), false);
std::vector< std::vector<size_t> > neighbor_index =
create_vertex_faces_index(its);
VertexFaceIndex neighbor_index{its};
auto exclude_neighbors = [&neighbor_index, &exclude_mask](const Vec3i &face)
{
for (int i = 0; i < 3; ++i) {
const std::vector<size_t> &neighbors = neighbor_index[face(i)];
for (size_t fi_n : neighbors) exclude_mask[fi_n] = true;
const auto &neighbors_range = neighbor_index[face(i)];
for (size_t fi_n : neighbors_range)
exclude_mask[fi_n] = true;
}
};

View file

@ -669,24 +669,6 @@ void TriangleMesh::restore_optional()
}
}
std::vector<std::vector<size_t>> create_vertex_faces_index(const indexed_triangle_set &its)
{
std::vector<std::vector<size_t>> index;
if (! its.vertices.empty()) {
size_t res = its.indices.size() / its.vertices.size();
index.assign(its.vertices.size(), reserve_vector<size_t>(res));
for (size_t fi = 0; fi < its.indices.size(); ++fi) {
auto &face = its.indices[fi];
index[face(0)].emplace_back(fi);
index[face(1)].emplace_back(fi);
index[face(2)].emplace_back(fi);
}
}
return index;
}
// Create a mapping from triangle edge into face.
struct EdgeToFace {
// Index of the 1st vertex of the triangle edge. vertex_low <= vertex_high.
@ -1197,49 +1179,81 @@ bool its_is_splittable(const indexed_triangle_set &its)
return its_is_splittable<>(its);
}
void VertexFaceIndex::create(const indexed_triangle_set &its)
{
m_vertex_to_face_start.assign(its.vertices.size() + 1, 0);
// 1) Calculate vertex incidence by scatter.
for (auto &face : its.indices) {
++ m_vertex_to_face_start[face(0) + 1];
++ m_vertex_to_face_start[face(1) + 1];
++ m_vertex_to_face_start[face(2) + 1];
}
// 2) Prefix sum to calculate offsets to m_vertex_faces_all.
for (size_t i = 2; i < m_vertex_to_face_start.size(); ++ i)
m_vertex_to_face_start[i] += m_vertex_to_face_start[i - 1];
// 3) Scatter indices of faces incident to a vertex into m_vertex_faces_all.
m_vertex_faces_all.assign(m_vertex_to_face_start.back(), 0);
for (size_t face_idx = 0; face_idx < its.indices.size(); ++ face_idx) {
auto &face = its.indices[face_idx];
for (int i = 0; i < 3; ++ i)
m_vertex_faces_all[m_vertex_to_face_start[face(i)] ++] = face_idx;
}
// 4) The previous loop modified m_vertex_to_face_start. Revert the change.
for (auto i = int(m_vertex_to_face_start.size()) - 1; i > 0; -- i)
m_vertex_to_face_start[i] = m_vertex_to_face_start[i - 1];
m_vertex_to_face_start.front() = 0;
}
static int get_vertex_index(size_t vertex_index, const stl_triangle_vertex_indices &triangle_indices) {
if (int(vertex_index) == triangle_indices[0]) return 0;
if (int(vertex_index) == triangle_indices[1]) return 1;
if (int(vertex_index) == triangle_indices[2]) return 2;
return -1;
}
static Vec2crd get_edge_indices(int edge_index, const stl_triangle_vertex_indices &triangle_indices)
{
int next_edge_index = (edge_index == 2) ? 0 : edge_index + 1;
coord_t vi0 = triangle_indices[edge_index];
coord_t vi1 = triangle_indices[next_edge_index];
return Vec2crd(vi0, vi1);
}
std::vector<Vec3i> its_create_neighbors_index(const indexed_triangle_set &its)
{
std::vector<Vec3i> out(its.indices.size(), Vec3i(-1, -1, -1));
const std::vector<stl_triangle_vertex_indices> &indices = its.indices;
size_t vertices_size = its.vertices.size();
std::vector<EdgeToFace> edges_map = create_edge_map(its, []{});
// Assign a unique common edge id to touching triangle edges.
for (size_t i = 0; i < edges_map.size(); ++ i) {
EdgeToFace &edge_i = edges_map[i];
if (edge_i.face == -1)
// This edge has been connected to some neighbor already.
continue;
// Unconnected edge. Find its neighbor with the correct orientation.
size_t j;
bool found = false;
for (j = i + 1; j < edges_map.size() && edge_i == edges_map[j]; ++ j)
if (edge_i.face_edge * edges_map[j].face_edge < 0 && edges_map[j].face != -1) {
// Faces touching with opposite oriented edges and none of the edges is connected yet.
found = true;
if (indices.empty() || vertices_size == 0) return {};
auto vertex_triangles = VertexFaceIndex{its};
coord_t no_value = -1;
std::vector<Vec3i> neighbors(indices.size(), Vec3i(no_value, no_value, no_value));
for (const stl_triangle_vertex_indices& triangle_indices : indices) {
coord_t index = &triangle_indices - &indices.front();
Vec3i& neighbor = neighbors[index];
for (int edge_index = 0; edge_index < 3; ++edge_index) {
// check if done
coord_t& neighbor_edge = neighbor[edge_index];
if (neighbor_edge != no_value) continue;
Vec2crd edge_indices = get_edge_indices(edge_index, triangle_indices);
// IMPROVE: use same vector for 2 sides of triangle
const auto &faces_range = vertex_triangles[edge_indices[0]];
for (const size_t &face : faces_range) {
if (int(face) <= index) continue;
const stl_triangle_vertex_indices &face_indices = indices[face];
int vertex_index = get_vertex_index(edge_indices[1], face_indices);
// NOT Contain second vertex?
if (vertex_index < 0) continue;
// Has NOT oposit direction?
if (edge_indices[0] != face_indices[(vertex_index + 1) % 3]) continue;
neighbor_edge = face;
neighbors[face][vertex_index] = index;
break;
}
if (! found) {
//FIXME Vojtech: Trying to find an edge with equal orientation. This smells.
// admesh can assign the same edge ID to more than two facets (which is
// still topologically correct), so we have to search for a duplicate of
// this edge too in case it was already seen in this orientation
for (j = i + 1; j < edges_map.size() && edge_i == edges_map[j]; ++ j)
if (edges_map[j].face != -1) {
// Faces touching with equally oriented edges and none of the edges is connected yet.
found = true;
break;
}
}
if (found) {
EdgeToFace &edge_j = edges_map[j];
out[edge_i.face](std::abs(edge_i.face_edge) - 1) = edge_j.face;
out[edge_j.face](std::abs(edge_j.face_edge) - 1) = edge_i.face;
// Mark the edge as connected.
edge_j.face = -1;
}
}
return out;
return neighbors;
}
} // namespace Slic3r

View file

@ -88,10 +88,30 @@ private:
std::deque<uint32_t> find_unvisited_neighbors(std::vector<unsigned char> &facet_visited) const;
};
// Create an index of faces belonging to each vertex. The returned vector can
// be indexed with vertex indices and contains a list of face indices for each
// vertex.
std::vector<std::vector<size_t>> create_vertex_faces_index(const indexed_triangle_set &its);
// Index of face indices incident with a vertex index.
struct VertexFaceIndex
{
public:
using iterator = std::vector<size_t>::const_iterator;
VertexFaceIndex(const indexed_triangle_set &its) { this->create(its); }
VertexFaceIndex() {}
void create(const indexed_triangle_set &its);
void clear() { m_vertex_to_face_start.clear(); m_vertex_faces_all.clear(); }
// Iterators of face indices incident with the input vertex_id.
iterator begin(size_t vertex_id) const throw() { return m_vertex_faces_all.begin() + m_vertex_to_face_start[vertex_id]; }
iterator end (size_t vertex_id) const throw() { return m_vertex_faces_all.begin() + m_vertex_to_face_start[vertex_id + 1]; }
// Vertex incidence.
size_t count(size_t vertex_id) const throw() { return m_vertex_to_face_start[vertex_id + 1] - m_vertex_to_face_start[vertex_id]; }
const Range<iterator> operator[](size_t vertex_id) const { return {begin(vertex_id), end(vertex_id)}; }
private:
std::vector<size_t> m_vertex_to_face_start;
std::vector<size_t> m_vertex_faces_all;
};
// Index of face indices incident with a vertex index.
struct VertexFaceIndex

View file

@ -306,6 +306,29 @@ IntegerOnly<I, std::vector<T, Args...>> reserve_vector(I capacity)
template<class T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
// A very simple range concept implementation with iterator-like objects.
// This should be replaced by std::ranges::subrange (C++20)
template<class It> class Range
{
It from, to;
public:
// The class is ready for range based for loops.
It begin() const { return from; }
It end() const { return to; }
// The iterator type can be obtained this way.
using iterator = It;
using value_type = typename std::iterator_traits<It>::value_type;
Range() = default;
Range(It b, It e) : from(std::move(b)), to(std::move(e)) {}
// Some useful container-like methods...
inline size_t size() const { return end() - begin(); }
inline bool empty() const { return size() == 0; }
};
} // namespace Slic3r
#endif