2018-12-17 14:58:15 +00:00
|
|
|
#include <cmath>
|
2018-11-02 10:57:57 +00:00
|
|
|
#include "SLA/SLASupportTree.hpp"
|
|
|
|
#include "SLA/SLABoilerPlate.hpp"
|
|
|
|
#include "SLA/SLASpatIndex.hpp"
|
|
|
|
|
|
|
|
// HEAVY headers... takes eternity to compile
|
|
|
|
|
|
|
|
// for concave hull merging decisions
|
|
|
|
#include "SLABoostAdapter.hpp"
|
|
|
|
#include "boost/geometry/index/rtree.hpp"
|
|
|
|
|
|
|
|
#include <igl/ray_mesh_intersect.h>
|
|
|
|
#include <igl/point_mesh_squared_distance.h>
|
2018-12-17 14:58:15 +00:00
|
|
|
#include <igl/remove_duplicate_vertices.h>
|
2018-11-08 16:15:10 +00:00
|
|
|
|
2018-11-02 10:57:57 +00:00
|
|
|
#include "SLASpatIndex.hpp"
|
|
|
|
#include "ClipperUtils.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
namespace sla {
|
|
|
|
|
|
|
|
class SpatIndex::Impl {
|
|
|
|
public:
|
|
|
|
using BoostIndex = boost::geometry::index::rtree< SpatElement,
|
|
|
|
boost::geometry::index::rstar<16, 4> /* ? */ >;
|
|
|
|
|
|
|
|
BoostIndex m_store;
|
|
|
|
};
|
|
|
|
|
|
|
|
SpatIndex::SpatIndex(): m_impl(new Impl()) {}
|
|
|
|
SpatIndex::~SpatIndex() {}
|
|
|
|
|
|
|
|
SpatIndex::SpatIndex(const SpatIndex &cpy): m_impl(new Impl(*cpy.m_impl)) {}
|
|
|
|
SpatIndex::SpatIndex(SpatIndex&& cpy): m_impl(std::move(cpy.m_impl)) {}
|
|
|
|
|
|
|
|
SpatIndex& SpatIndex::operator=(const SpatIndex &cpy)
|
|
|
|
{
|
|
|
|
m_impl.reset(new Impl(*cpy.m_impl));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpatIndex& SpatIndex::operator=(SpatIndex &&cpy)
|
|
|
|
{
|
|
|
|
m_impl.swap(cpy.m_impl);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpatIndex::insert(const SpatElement &el)
|
|
|
|
{
|
|
|
|
m_impl->m_store.insert(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpatIndex::remove(const SpatElement& el)
|
|
|
|
{
|
2018-11-13 16:33:03 +00:00
|
|
|
return m_impl->m_store.remove(el) == 1;
|
2018-11-02 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SpatElement>
|
|
|
|
SpatIndex::query(std::function<bool(const SpatElement &)> fn)
|
|
|
|
{
|
|
|
|
namespace bgi = boost::geometry::index;
|
|
|
|
|
|
|
|
std::vector<SpatElement> ret;
|
|
|
|
m_impl->m_store.query(bgi::satisfies(fn), std::back_inserter(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SpatElement> SpatIndex::nearest(const Vec3d &el, unsigned k = 1)
|
|
|
|
{
|
|
|
|
namespace bgi = boost::geometry::index;
|
|
|
|
std::vector<SpatElement> ret; ret.reserve(k);
|
|
|
|
m_impl->m_store.query(bgi::nearest(el, k), std::back_inserter(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SpatIndex::size() const
|
|
|
|
{
|
|
|
|
return m_impl->m_store.size();
|
|
|
|
}
|
|
|
|
|
2018-12-17 14:58:15 +00:00
|
|
|
bool point_on_edge(const Vec3d& p, const Vec3d& e1, const Vec3d& e2,
|
|
|
|
double eps = 0.05)
|
|
|
|
{
|
|
|
|
using Line3D = Eigen::ParametrizedLine<double, 3>;
|
|
|
|
|
|
|
|
auto line = Line3D::Through(e1, e2);
|
|
|
|
double d = line.distance(p);
|
|
|
|
return std::abs(d) < eps;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Vec> double distance(const Vec& pp1, const Vec& pp2) {
|
|
|
|
auto p = pp2 - pp1;
|
|
|
|
return std::sqrt(p.transpose() * p);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointSet normals(const PointSet& points, const EigenMesh3D& emesh,
|
|
|
|
double eps,
|
|
|
|
std::function<void()> throw_on_cancel) {
|
|
|
|
if(points.rows() == 0 || emesh.V.rows() == 0 || emesh.F.rows() == 0)
|
|
|
|
return {};
|
|
|
|
|
2018-11-12 10:46:38 +00:00
|
|
|
Eigen::VectorXd dists;
|
|
|
|
Eigen::VectorXi I;
|
2018-11-02 10:57:57 +00:00
|
|
|
PointSet C;
|
2018-11-08 16:15:10 +00:00
|
|
|
|
2018-12-17 14:58:15 +00:00
|
|
|
// We need to remove duplicate vertices and have a true index triangle
|
|
|
|
// structure
|
|
|
|
EigenMesh3D mesh;
|
|
|
|
Eigen::VectorXi SVI, SVJ;
|
2018-12-20 11:08:06 +00:00
|
|
|
static const double dEPS = 1e-6;
|
|
|
|
igl::remove_duplicate_vertices(emesh.V, emesh.F, dEPS,
|
2018-12-17 14:58:15 +00:00
|
|
|
mesh.V, SVI, SVJ, mesh.F);
|
|
|
|
|
2018-11-02 10:57:57 +00:00
|
|
|
igl::point_mesh_squared_distance( points, mesh.V, mesh.F, dists, I, C);
|
|
|
|
|
|
|
|
PointSet ret(I.rows(), 3);
|
|
|
|
for(int i = 0; i < I.rows(); i++) {
|
2018-12-17 14:58:15 +00:00
|
|
|
throw_on_cancel();
|
2018-11-02 10:57:57 +00:00
|
|
|
auto idx = I(i);
|
|
|
|
auto trindex = mesh.F.row(idx);
|
|
|
|
|
2018-12-17 14:58:15 +00:00
|
|
|
const Vec3d& p1 = mesh.V.row(trindex(0));
|
|
|
|
const Vec3d& p2 = mesh.V.row(trindex(1));
|
|
|
|
const Vec3d& p3 = mesh.V.row(trindex(2));
|
|
|
|
|
|
|
|
// We should check if the point lies on an edge of the hosting triangle.
|
|
|
|
// If it does than all the other triangles using the same two points
|
|
|
|
// have to be searched and the final normal should be some kind of
|
|
|
|
// aggregation of the participating triangle normals. We should also
|
|
|
|
// consider the cases where the support point lies right on a vertex
|
|
|
|
// of its triangle. The procedure is the same, get the neighbor
|
|
|
|
// triangles and calculate an average normal.
|
|
|
|
|
|
|
|
const Vec3d& p = C.row(i);
|
|
|
|
|
|
|
|
// mark the vertex indices of the edge. ia and ib marks and edge ic
|
|
|
|
// will mark a single vertex.
|
|
|
|
int ia = -1, ib = -1, ic = -1;
|
2018-11-02 10:57:57 +00:00
|
|
|
|
2018-12-17 14:58:15 +00:00
|
|
|
if(std::abs(distance(p, p1)) < eps) {
|
|
|
|
ic = trindex(0);
|
|
|
|
}
|
|
|
|
else if(std::abs(distance(p, p2)) < eps) {
|
|
|
|
ic = trindex(1);
|
|
|
|
}
|
|
|
|
else if(std::abs(distance(p, p3)) < eps) {
|
|
|
|
ic = trindex(2);
|
|
|
|
}
|
|
|
|
else if(point_on_edge(p, p1, p2, eps)) {
|
|
|
|
ia = trindex(0); ib = trindex(1);
|
|
|
|
}
|
|
|
|
else if(point_on_edge(p, p2, p3, eps)) {
|
|
|
|
ia = trindex(1); ib = trindex(2);
|
|
|
|
}
|
|
|
|
else if(point_on_edge(p, p1, p3, eps)) {
|
|
|
|
ia = trindex(0); ib = trindex(2);
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:08:06 +00:00
|
|
|
// vector for the neigboring triangles including the detected one.
|
2018-12-17 14:58:15 +00:00
|
|
|
std::vector<Vec3i> neigh;
|
|
|
|
if(ic >= 0) { // The point is right on a vertex of the triangle
|
|
|
|
for(int n = 0; n < mesh.F.rows(); ++n) {
|
|
|
|
throw_on_cancel();
|
|
|
|
Vec3i ni = mesh.F.row(n);
|
|
|
|
if((ni(X) == ic || ni(Y) == ic || ni(Z) == ic))
|
|
|
|
neigh.emplace_back(ni);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ia >= 0 && ib >= 0) { // the point is on and edge
|
|
|
|
// now get all the neigboring triangles
|
|
|
|
for(int n = 0; n < mesh.F.rows(); ++n) {
|
|
|
|
throw_on_cancel();
|
|
|
|
Vec3i ni = mesh.F.row(n);
|
|
|
|
if((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) &&
|
|
|
|
(ni(X) == ib || ni(Y) == ib || ni(Z) == ib))
|
|
|
|
neigh.emplace_back(ni);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:08:06 +00:00
|
|
|
// Calculate the normals for the neighboring triangles
|
|
|
|
std::vector<Vec3d> neighnorms; neighnorms.reserve(neigh.size());
|
|
|
|
for(const Vec3i& tri : neigh) {
|
|
|
|
const Vec3d& pt1 = mesh.V.row(tri(0));
|
|
|
|
const Vec3d& pt2 = mesh.V.row(tri(1));
|
|
|
|
const Vec3d& pt3 = mesh.V.row(tri(2));
|
|
|
|
Eigen::Vector3d U = pt2 - pt1;
|
|
|
|
Eigen::Vector3d V = pt3 - pt1;
|
|
|
|
neighnorms.emplace_back(U.cross(V).normalized());
|
|
|
|
}
|
|
|
|
|
2019-01-02 14:48:38 +00:00
|
|
|
// Throw out duplicates. They would cause trouble with summing. We will
|
|
|
|
// use std::unique which works on sorted ranges. We will sort by the
|
|
|
|
// coefficient-wise sum of the normals. It should force the same
|
|
|
|
// elements to be consecutive.
|
|
|
|
std::sort(neighnorms.begin(), neighnorms.end(),
|
|
|
|
[](const Vec3d& v1, const Vec3d& v2){
|
|
|
|
return v1.sum() < v2.sum();
|
|
|
|
});
|
|
|
|
|
2018-12-20 11:08:06 +00:00
|
|
|
auto lend = std::unique(neighnorms.begin(), neighnorms.end(),
|
|
|
|
[](const Vec3d& n1, const Vec3d& n2) {
|
|
|
|
// Compare normals for equivalence. This is controvers stuff.
|
|
|
|
auto deq = [](double a, double b) { return std::abs(a-b) < 1e-3; };
|
|
|
|
return deq(n1(X), n2(X)) && deq(n1(Y), n2(Y)) && deq(n1(Z), n2(Z));
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!neighnorms.empty()) { // there were neighbors to count with
|
2018-12-20 14:22:58 +00:00
|
|
|
// sum up the normals and then normalize the result again.
|
2018-12-20 11:08:06 +00:00
|
|
|
// This unification seems to be enough.
|
2018-12-17 14:58:15 +00:00
|
|
|
Vec3d sumnorm(0, 0, 0);
|
2018-12-20 11:08:06 +00:00
|
|
|
sumnorm = std::accumulate(neighnorms.begin(), lend, sumnorm);
|
|
|
|
sumnorm.normalize();
|
2018-12-17 14:58:15 +00:00
|
|
|
ret.row(i) = sumnorm;
|
|
|
|
}
|
|
|
|
else { // point lies safely within its triangle
|
|
|
|
Eigen::Vector3d U = p2 - p1;
|
|
|
|
Eigen::Vector3d V = p3 - p1;
|
|
|
|
ret.row(i) = U.cross(V).normalized();
|
|
|
|
}
|
2018-11-02 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
double ray_mesh_intersect(const Vec3d& s,
|
|
|
|
const Vec3d& dir,
|
|
|
|
const EigenMesh3D& m)
|
|
|
|
{
|
|
|
|
igl::Hit hit;
|
|
|
|
hit.t = std::numeric_limits<float>::infinity();
|
|
|
|
igl::ray_mesh_intersect(s, dir, m.V, m.F, hit);
|
2018-12-12 10:36:02 +00:00
|
|
|
return double(hit.t);
|
2018-11-02 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clustering a set of points by the given criteria
|
|
|
|
ClusteredPoints cluster(
|
|
|
|
const sla::PointSet& points,
|
|
|
|
std::function<bool(const SpatElement&, const SpatElement&)> pred,
|
|
|
|
unsigned max_points = 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace bgi = boost::geometry::index;
|
|
|
|
using Index3D = bgi::rtree< SpatElement, bgi::rstar<16, 4> /* ? */ >;
|
|
|
|
|
|
|
|
// A spatial index for querying the nearest points
|
|
|
|
Index3D sindex;
|
|
|
|
|
|
|
|
// Build the index
|
|
|
|
for(unsigned idx = 0; idx < points.rows(); idx++)
|
|
|
|
sindex.insert( std::make_pair(points.row(idx), idx));
|
|
|
|
|
|
|
|
using Elems = std::vector<SpatElement>;
|
|
|
|
|
|
|
|
// Recursive function for visiting all the points in a given distance to
|
|
|
|
// each other
|
|
|
|
std::function<void(Elems&, Elems&)> group =
|
|
|
|
[&sindex, &group, pred, max_points](Elems& pts, Elems& cluster)
|
|
|
|
{
|
|
|
|
for(auto& p : pts) {
|
|
|
|
std::vector<SpatElement> tmp;
|
|
|
|
|
|
|
|
sindex.query(
|
|
|
|
bgi::satisfies([p, pred](const SpatElement& se) {
|
|
|
|
return pred(p, se);
|
|
|
|
}),
|
|
|
|
std::back_inserter(tmp)
|
|
|
|
);
|
|
|
|
|
|
|
|
auto cmp = [](const SpatElement& e1, const SpatElement& e2){
|
|
|
|
return e1.second < e2.second;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::sort(tmp.begin(), tmp.end(), cmp);
|
|
|
|
|
|
|
|
Elems newpts;
|
|
|
|
std::set_difference(tmp.begin(), tmp.end(),
|
|
|
|
cluster.begin(), cluster.end(),
|
|
|
|
std::back_inserter(newpts), cmp);
|
|
|
|
|
|
|
|
int c = max_points && newpts.size() + cluster.size() > max_points?
|
|
|
|
int(max_points - cluster.size()) : int(newpts.size());
|
|
|
|
|
|
|
|
cluster.insert(cluster.end(), newpts.begin(), newpts.begin() + c);
|
|
|
|
std::sort(cluster.begin(), cluster.end(), cmp);
|
|
|
|
|
|
|
|
if(!newpts.empty() && (!max_points || cluster.size() < max_points))
|
|
|
|
group(newpts, cluster);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Elems> clusters;
|
|
|
|
for(auto it = sindex.begin(); it != sindex.end();) {
|
|
|
|
Elems cluster = {};
|
|
|
|
Elems pts = {*it};
|
|
|
|
group(pts, cluster);
|
|
|
|
|
|
|
|
for(auto& c : cluster) sindex.remove(c);
|
|
|
|
it = sindex.begin();
|
|
|
|
|
|
|
|
clusters.emplace_back(cluster);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClusteredPoints result;
|
|
|
|
for(auto& cluster : clusters) {
|
|
|
|
result.emplace_back();
|
|
|
|
for(auto c : cluster) result.back().emplace_back(c.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
using Segments = std::vector<std::pair<Vec2d, Vec2d>>;
|
|
|
|
|
|
|
|
Segments model_boundary(const EigenMesh3D& emesh, double offs)
|
|
|
|
{
|
|
|
|
Segments ret;
|
|
|
|
Polygons pp;
|
2018-12-12 10:36:02 +00:00
|
|
|
pp.reserve(size_t(emesh.F.rows()));
|
2018-11-02 10:57:57 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < emesh.F.rows(); i++) {
|
|
|
|
auto trindex = emesh.F.row(i);
|
|
|
|
auto& p1 = emesh.V.row(trindex(0));
|
|
|
|
auto& p2 = emesh.V.row(trindex(1));
|
|
|
|
auto& p3 = emesh.V.row(trindex(2));
|
|
|
|
|
|
|
|
Polygon p;
|
|
|
|
p.points.resize(3);
|
|
|
|
p.points[0] = Point::new_scale(p1(X), p1(Y));
|
|
|
|
p.points[1] = Point::new_scale(p2(X), p2(Y));
|
|
|
|
p.points[2] = Point::new_scale(p3(X), p3(Y));
|
|
|
|
p.make_counter_clockwise();
|
|
|
|
pp.emplace_back(p);
|
|
|
|
}
|
|
|
|
|
2018-12-17 14:58:15 +00:00
|
|
|
ExPolygons merged = union_ex(Slic3r::offset(pp, float(scale_(offs))), true);
|
2018-11-02 10:57:57 +00:00
|
|
|
|
|
|
|
for(auto& expoly : merged) {
|
|
|
|
auto lines = expoly.lines();
|
|
|
|
for(Line& l : lines) {
|
|
|
|
Vec2d a(l.a(X) * SCALING_FACTOR, l.a(Y) * SCALING_FACTOR);
|
|
|
|
Vec2d b(l.b(X) * SCALING_FACTOR, l.b(Y) * SCALING_FACTOR);
|
|
|
|
ret.emplace_back(std::make_pair(a, b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//struct SegmentIndex {
|
|
|
|
|
|
|
|
//};
|
|
|
|
|
|
|
|
//using SegmentIndexEl = std::pair<Segment, unsigned>;
|
|
|
|
|
|
|
|
//SegmentIndexEl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|