2020-06-25 11:58:51 +00:00
|
|
|
#include "IndexedMesh.hpp"
|
2020-06-16 11:17:06 +00:00
|
|
|
#include "Concurrency.hpp"
|
2020-05-22 11:27:00 +00:00
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
#include <libslic3r/AABBTreeIndirect.hpp>
|
|
|
|
#include <libslic3r/TriangleMesh.hpp>
|
2020-05-22 11:27:00 +00:00
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
#include <numeric>
|
2020-05-22 11:27:00 +00:00
|
|
|
|
2020-05-22 15:21:54 +00:00
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
2020-06-16 11:17:06 +00:00
|
|
|
#include <libslic3r/SLA/Hollowing.hpp>
|
2019-07-18 14:32:04 +00:00
|
|
|
#endif
|
2018-11-08 16:15:10 +00:00
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
namespace Slic3r { namespace sla {
|
2020-05-22 11:27:00 +00:00
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
class IndexedMesh::AABBImpl {
|
2020-05-22 11:27:00 +00:00
|
|
|
private:
|
|
|
|
AABBTreeIndirect::Tree3f m_tree;
|
2020-05-22 16:21:52 +00:00
|
|
|
|
2020-05-22 11:27:00 +00:00
|
|
|
public:
|
2020-05-22 16:21:52 +00:00
|
|
|
void init(const TriangleMesh& tm)
|
2020-05-22 11:27:00 +00:00
|
|
|
{
|
|
|
|
m_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(
|
2020-06-16 11:17:06 +00:00
|
|
|
tm.its.vertices, tm.its.indices);
|
2020-05-22 11:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:21:52 +00:00
|
|
|
void intersect_ray(const TriangleMesh& tm,
|
2020-05-22 11:27:00 +00:00
|
|
|
const Vec3d& s, const Vec3d& dir, igl::Hit& hit)
|
|
|
|
{
|
2020-05-22 16:21:52 +00:00
|
|
|
AABBTreeIndirect::intersect_ray_first_hit(tm.its.vertices,
|
|
|
|
tm.its.indices,
|
2020-05-22 11:27:00 +00:00
|
|
|
m_tree,
|
|
|
|
s, dir, hit);
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:21:52 +00:00
|
|
|
void intersect_ray(const TriangleMesh& tm,
|
2020-05-22 11:27:00 +00:00
|
|
|
const Vec3d& s, const Vec3d& dir, std::vector<igl::Hit>& hits)
|
|
|
|
{
|
2020-05-22 16:21:52 +00:00
|
|
|
AABBTreeIndirect::intersect_ray_all_hits(tm.its.vertices,
|
|
|
|
tm.its.indices,
|
2020-05-22 11:27:00 +00:00
|
|
|
m_tree,
|
|
|
|
s, dir, hits);
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:21:52 +00:00
|
|
|
double squared_distance(const TriangleMesh& tm,
|
2020-05-22 11:27:00 +00:00
|
|
|
const Vec3d& point, int& i, Eigen::Matrix<double, 1, 3>& closest) {
|
|
|
|
size_t idx_unsigned = 0;
|
|
|
|
Vec3d closest_vec3d(closest);
|
|
|
|
double dist = AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
|
2020-06-16 11:17:06 +00:00
|
|
|
tm.its.vertices,
|
|
|
|
tm.its.indices,
|
|
|
|
m_tree, point, idx_unsigned, closest_vec3d);
|
2020-05-22 11:27:00 +00:00
|
|
|
i = int(idx_unsigned);
|
|
|
|
closest = closest_vec3d;
|
|
|
|
return dist;
|
|
|
|
}
|
2019-01-17 15:44:26 +00:00
|
|
|
};
|
2019-01-14 16:28:02 +00:00
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh::IndexedMesh(const TriangleMesh& tmesh)
|
2020-05-23 11:54:41 +00:00
|
|
|
: m_aabb(new AABBImpl()), m_tm(&tmesh)
|
2020-05-22 16:21:52 +00:00
|
|
|
{
|
2020-01-24 13:26:05 +00:00
|
|
|
auto&& bb = tmesh.bounding_box();
|
|
|
|
m_ground_level += bb.min(Z);
|
2020-06-16 11:17:06 +00:00
|
|
|
|
2019-01-14 16:28:02 +00:00
|
|
|
// Build the AABB accelaration tree
|
2020-05-22 11:27:00 +00:00
|
|
|
m_aabb->init(tmesh);
|
2019-01-14 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh::~IndexedMesh() {}
|
2019-01-14 16:28:02 +00:00
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh::IndexedMesh(const IndexedMesh &other):
|
2020-05-22 16:21:52 +00:00
|
|
|
m_tm(other.m_tm), m_ground_level(other.m_ground_level),
|
2019-01-14 16:28:02 +00:00
|
|
|
m_aabb( new AABBImpl(*other.m_aabb) ) {}
|
|
|
|
|
2019-11-04 13:33:29 +00:00
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh &IndexedMesh::operator=(const IndexedMesh &other)
|
2019-01-14 16:28:02 +00:00
|
|
|
{
|
2020-05-22 16:21:52 +00:00
|
|
|
m_tm = other.m_tm;
|
2019-01-14 16:28:02 +00:00
|
|
|
m_ground_level = other.m_ground_level;
|
|
|
|
m_aabb.reset(new AABBImpl(*other.m_aabb)); return *this;
|
|
|
|
}
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh &IndexedMesh::operator=(IndexedMesh &&other) = default;
|
2020-01-24 13:26:05 +00:00
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh::IndexedMesh(IndexedMesh &&other) = default;
|
2020-01-24 13:26:05 +00:00
|
|
|
|
2020-05-23 11:54:41 +00:00
|
|
|
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
const std::vector<Vec3f>& IndexedMesh::vertices() const
|
2020-05-23 11:54:41 +00:00
|
|
|
{
|
|
|
|
return m_tm->its.vertices;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
const std::vector<Vec3i>& IndexedMesh::indices() const
|
2020-05-23 11:54:41 +00:00
|
|
|
{
|
|
|
|
return m_tm->its.indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
const Vec3f& IndexedMesh::vertices(size_t idx) const
|
2020-05-23 11:54:41 +00:00
|
|
|
{
|
|
|
|
return m_tm->its.vertices[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
const Vec3i& IndexedMesh::indices(size_t idx) const
|
2020-05-23 11:54:41 +00:00
|
|
|
{
|
|
|
|
return m_tm->its.indices[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
Vec3d IndexedMesh::normal_by_face_id(int face_id) const {
|
2020-05-23 11:54:41 +00:00
|
|
|
return m_tm->stl.facet_start[face_id].normal.cast<double>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh::hit_result
|
|
|
|
IndexedMesh::query_ray_hit(const Vec3d &s, const Vec3d &dir) const
|
2019-01-14 16:28:02 +00:00
|
|
|
{
|
2019-11-19 13:27:05 +00:00
|
|
|
assert(is_approx(dir.norm(), 1.));
|
2021-03-03 14:19:24 +00:00
|
|
|
igl::Hit hit{-1, -1, 0.f, 0.f, 0.f};
|
2019-01-15 10:09:00 +00:00
|
|
|
hit.t = std::numeric_limits<float>::infinity();
|
2019-11-15 14:48:52 +00:00
|
|
|
|
2020-05-22 15:21:54 +00:00
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
|
|
|
if (! m_holes.empty()) {
|
2020-05-22 16:21:52 +00:00
|
|
|
|
2019-11-19 13:27:05 +00:00
|
|
|
// If there are holes, the hit_results will be made by
|
|
|
|
// query_ray_hits (object) and filter_hits (holes):
|
2019-11-26 10:36:09 +00:00
|
|
|
return filter_hits(query_ray_hits(s, dir));
|
2019-11-19 13:27:05 +00:00
|
|
|
}
|
2020-05-22 15:21:54 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-23 11:54:41 +00:00
|
|
|
m_aabb->intersect_ray(*m_tm, s, dir, hit);
|
2020-05-22 15:21:54 +00:00
|
|
|
hit_result ret(*this);
|
|
|
|
ret.m_t = double(hit.t);
|
|
|
|
ret.m_dir = dir;
|
|
|
|
ret.m_source = s;
|
|
|
|
if(!std::isinf(hit.t) && !std::isnan(hit.t)) {
|
|
|
|
ret.m_normal = this->normal_by_face_id(hit.id);
|
|
|
|
ret.m_face_id = hit.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-01-14 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
std::vector<IndexedMesh::hit_result>
|
|
|
|
IndexedMesh::query_ray_hits(const Vec3d &s, const Vec3d &dir) const
|
2019-11-08 19:18:14 +00:00
|
|
|
{
|
2020-06-25 11:58:51 +00:00
|
|
|
std::vector<IndexedMesh::hit_result> outs;
|
2019-11-08 19:18:14 +00:00
|
|
|
std::vector<igl::Hit> hits;
|
2020-05-23 11:54:41 +00:00
|
|
|
m_aabb->intersect_ray(*m_tm, s, dir, hits);
|
2020-06-16 11:17:06 +00:00
|
|
|
|
2019-11-08 19:18:14 +00:00
|
|
|
// The sort is necessary, the hits are not always sorted.
|
|
|
|
std::sort(hits.begin(), hits.end(),
|
2019-11-11 10:41:14 +00:00
|
|
|
[](const igl::Hit& a, const igl::Hit& b) { return a.t < b.t; });
|
2020-01-09 13:06:39 +00:00
|
|
|
|
|
|
|
// Remove duplicates. They sometimes appear, for example when the ray is cast
|
|
|
|
// along an axis of a cube due to floating-point approximations in igl (?)
|
|
|
|
hits.erase(std::unique(hits.begin(), hits.end(),
|
|
|
|
[](const igl::Hit& a, const igl::Hit& b)
|
2020-06-16 11:17:06 +00:00
|
|
|
{ return a.t == b.t; }),
|
2020-01-09 13:06:39 +00:00
|
|
|
hits.end());
|
|
|
|
|
2019-11-08 19:18:14 +00:00
|
|
|
// Convert the igl::Hit into hit_result
|
|
|
|
outs.reserve(hits.size());
|
|
|
|
for (const igl::Hit& hit : hits) {
|
2020-06-25 11:58:51 +00:00
|
|
|
outs.emplace_back(IndexedMesh::hit_result(*this));
|
2019-11-08 19:18:14 +00:00
|
|
|
outs.back().m_t = double(hit.t);
|
|
|
|
outs.back().m_dir = dir;
|
|
|
|
outs.back().m_source = s;
|
2019-09-26 11:30:22 +00:00
|
|
|
if(!std::isinf(hit.t) && !std::isnan(hit.t)) {
|
2019-11-19 13:27:05 +00:00
|
|
|
outs.back().m_normal = this->normal_by_face_id(hit.id);
|
2019-09-26 11:30:22 +00:00
|
|
|
outs.back().m_face_id = hit.id;
|
|
|
|
}
|
2019-11-08 19:18:14 +00:00
|
|
|
}
|
2019-11-19 13:27:05 +00:00
|
|
|
|
2019-11-08 19:18:14 +00:00
|
|
|
return outs;
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:21:54 +00:00
|
|
|
|
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
2020-06-25 11:58:51 +00:00
|
|
|
IndexedMesh::hit_result IndexedMesh::filter_hits(
|
|
|
|
const std::vector<IndexedMesh::hit_result>& object_hits) const
|
2019-11-19 13:27:05 +00:00
|
|
|
{
|
2019-11-26 10:36:09 +00:00
|
|
|
assert(! m_holes.empty());
|
2019-11-19 13:27:05 +00:00
|
|
|
hit_result out(*this);
|
|
|
|
|
2019-11-26 10:36:09 +00:00
|
|
|
if (object_hits.empty())
|
|
|
|
return out;
|
|
|
|
|
|
|
|
const Vec3d& s = object_hits.front().source();
|
|
|
|
const Vec3d& dir = object_hits.front().direction();
|
|
|
|
|
|
|
|
// A helper struct to save an intersetion with a hole
|
|
|
|
struct HoleHit {
|
|
|
|
HoleHit(float t_p, const Vec3d& normal_p, bool entry_p) :
|
|
|
|
t(t_p), normal(normal_p), entry(entry_p) {}
|
|
|
|
float t;
|
|
|
|
Vec3d normal;
|
|
|
|
bool entry;
|
|
|
|
};
|
|
|
|
std::vector<HoleHit> hole_isects;
|
2020-01-08 16:12:06 +00:00
|
|
|
hole_isects.reserve(m_holes.size());
|
2020-06-16 11:17:06 +00:00
|
|
|
|
2020-01-08 16:12:06 +00:00
|
|
|
auto sf = s.cast<float>();
|
|
|
|
auto dirf = dir.cast<float>();
|
2019-11-26 10:36:09 +00:00
|
|
|
|
|
|
|
// Collect hits on all holes, preserve information about entry/exit
|
|
|
|
for (const sla::DrainHole& hole : m_holes) {
|
|
|
|
std::array<std::pair<float, Vec3d>, 2> isects;
|
2020-01-08 16:12:06 +00:00
|
|
|
if (hole.get_intersections(sf, dirf, isects)) {
|
2020-01-09 13:06:39 +00:00
|
|
|
// Ignore hole hits behind the source
|
2020-01-08 16:12:06 +00:00
|
|
|
if (isects[0].first > 0.f) hole_isects.emplace_back(isects[0].first, isects[0].second, true);
|
|
|
|
if (isects[1].first > 0.f) hole_isects.emplace_back(isects[1].first, isects[1].second, false);
|
2019-11-26 10:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Holes can intersect each other, sort the hits by t
|
|
|
|
std::sort(hole_isects.begin(), hole_isects.end(),
|
|
|
|
[](const HoleHit& a, const HoleHit& b) { return a.t < b.t; });
|
|
|
|
|
|
|
|
// Now inspect the intersections with object and holes, in the order of
|
|
|
|
// increasing distance. Keep track how deep are we nested in mesh/holes and
|
|
|
|
// pick the correct intersection.
|
|
|
|
// This needs to be done twice - first to find out how deep in the structure
|
|
|
|
// the source is, then to pick the correct intersection.
|
|
|
|
int hole_nested = 0;
|
|
|
|
int object_nested = 0;
|
|
|
|
for (int dry_run=1; dry_run>=0; --dry_run) {
|
|
|
|
hole_nested = -hole_nested;
|
|
|
|
object_nested = -object_nested;
|
2019-11-19 13:27:05 +00:00
|
|
|
|
|
|
|
bool is_hole = false;
|
|
|
|
bool is_entry = false;
|
2019-11-26 10:36:09 +00:00
|
|
|
const HoleHit* next_hole_hit = hole_isects.empty() ? nullptr : &hole_isects.front();
|
2019-11-19 13:27:05 +00:00
|
|
|
const hit_result* next_mesh_hit = &object_hits.front();
|
|
|
|
|
|
|
|
while (next_hole_hit || next_mesh_hit) {
|
|
|
|
if (next_hole_hit && next_mesh_hit) // still have hole and obj hits
|
|
|
|
is_hole = (next_hole_hit->t < next_mesh_hit->m_t);
|
|
|
|
else
|
|
|
|
is_hole = next_hole_hit; // one or the other ran out
|
|
|
|
|
|
|
|
// Is this entry or exit hit?
|
|
|
|
is_entry = is_hole ? next_hole_hit->entry : ! next_mesh_hit->is_inside();
|
|
|
|
|
2019-11-26 10:36:09 +00:00
|
|
|
if (! dry_run) {
|
|
|
|
if (! is_hole && hole_nested == 0) {
|
|
|
|
// This is a valid object hit
|
|
|
|
return *next_mesh_hit;
|
|
|
|
}
|
|
|
|
if (is_hole && ! is_entry && object_nested != 0) {
|
|
|
|
// This holehit is the one we seek
|
|
|
|
out.m_t = next_hole_hit->t;
|
|
|
|
out.m_normal = next_hole_hit->normal;
|
|
|
|
out.m_source = s;
|
|
|
|
out.m_dir = dir;
|
|
|
|
return out;
|
|
|
|
}
|
2019-11-19 13:27:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 10:36:09 +00:00
|
|
|
// Increase/decrease the counter
|
|
|
|
(is_hole ? hole_nested : object_nested) += (is_entry ? 1 : -1);
|
2019-11-19 13:27:05 +00:00
|
|
|
|
2019-11-26 10:36:09 +00:00
|
|
|
// Advance the respective pointer
|
2019-11-19 13:27:05 +00:00
|
|
|
if (is_hole && next_hole_hit++ == &hole_isects.back())
|
|
|
|
next_hole_hit = nullptr;
|
|
|
|
if (! is_hole && next_mesh_hit++ == &object_hits.back())
|
|
|
|
next_mesh_hit = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:36:09 +00:00
|
|
|
// if we got here, the ray ended up in infinity
|
2019-11-19 13:27:05 +00:00
|
|
|
return out;
|
|
|
|
}
|
2020-05-22 15:21:54 +00:00
|
|
|
#endif
|
2019-11-19 13:27:05 +00:00
|
|
|
|
2019-01-17 15:44:26 +00:00
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
double IndexedMesh::squared_distance(const Vec3d &p, int& i, Vec3d& c) const {
|
2019-02-26 16:13:33 +00:00
|
|
|
double sqdst = 0;
|
|
|
|
Eigen::Matrix<double, 1, 3> pp = p;
|
|
|
|
Eigen::Matrix<double, 1, 3> cc;
|
2020-05-23 11:54:41 +00:00
|
|
|
sqdst = m_aabb->squared_distance(*m_tm, pp, i, cc);
|
2019-02-26 16:13:33 +00:00
|
|
|
c = cc;
|
|
|
|
return sqdst;
|
|
|
|
}
|
|
|
|
|
2019-01-15 10:09:00 +00:00
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
static bool point_on_edge(const Vec3d& p, const Vec3d& e1, const Vec3d& e2,
|
|
|
|
double eps = 0.05)
|
2018-12-17 14:58:15 +00:00
|
|
|
{
|
|
|
|
using Line3D = Eigen::ParametrizedLine<double, 3>;
|
2020-06-16 11:17:06 +00:00
|
|
|
|
2018-12-17 14:58:15 +00:00
|
|
|
auto line = Line3D::Through(e1, e2);
|
|
|
|
double d = line.distance(p);
|
|
|
|
return std::abs(d) < eps;
|
|
|
|
}
|
|
|
|
|
2019-02-25 12:24:01 +00:00
|
|
|
PointSet normals(const PointSet& points,
|
2020-06-25 11:58:51 +00:00
|
|
|
const IndexedMesh& mesh,
|
2018-12-17 14:58:15 +00:00
|
|
|
double eps,
|
2019-02-26 17:09:33 +00:00
|
|
|
std::function<void()> thr, // throw on cancel
|
2019-09-24 13:15:49 +00:00
|
|
|
const std::vector<unsigned>& pt_indices)
|
2019-02-25 12:24:01 +00:00
|
|
|
{
|
2020-05-22 16:21:52 +00:00
|
|
|
if (points.rows() == 0 || mesh.vertices().empty() || mesh.indices().empty())
|
2018-12-17 14:58:15 +00:00
|
|
|
return {};
|
2019-11-12 15:53:47 +00:00
|
|
|
|
2019-02-26 16:13:33 +00:00
|
|
|
std::vector<unsigned> range = pt_indices;
|
2019-11-12 15:53:47 +00:00
|
|
|
if (range.empty()) {
|
2019-02-26 16:13:33 +00:00
|
|
|
range.resize(size_t(points.rows()), 0);
|
|
|
|
std::iota(range.begin(), range.end(), 0);
|
|
|
|
}
|
2019-11-12 15:53:47 +00:00
|
|
|
|
|
|
|
PointSet ret(range.size(), 3);
|
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
// for (size_t ridx = 0; ridx < range.size(); ++ridx)
|
2020-08-05 13:49:36 +00:00
|
|
|
ccr::for_each(size_t(0), range.size(),
|
|
|
|
[&ret, &mesh, &points, thr, eps, &range](size_t ridx) {
|
2019-11-12 15:53:47 +00:00
|
|
|
thr();
|
2020-08-05 13:49:36 +00:00
|
|
|
unsigned el = range[ridx];
|
2019-11-12 15:53:47 +00:00
|
|
|
auto eidx = Eigen::Index(el);
|
|
|
|
int faceid = 0;
|
|
|
|
Vec3d p;
|
|
|
|
|
|
|
|
mesh.squared_distance(points.row(eidx), faceid, p);
|
|
|
|
|
2020-05-22 16:21:52 +00:00
|
|
|
auto trindex = mesh.indices(faceid);
|
2019-11-12 15:53:47 +00:00
|
|
|
|
2020-05-22 16:21:52 +00:00
|
|
|
const Vec3d &p1 = mesh.vertices(trindex(0)).cast<double>();
|
|
|
|
const Vec3d &p2 = mesh.vertices(trindex(1)).cast<double>();
|
|
|
|
const Vec3d &p3 = mesh.vertices(trindex(2)).cast<double>();
|
2019-11-12 15:53:47 +00:00
|
|
|
|
|
|
|
// We should check if the point lies on an edge of the hosting
|
|
|
|
// triangle. If it does then 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.
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
if (std::abs((p - p1).norm()) < eps) {
|
2019-11-12 15:53:47 +00:00
|
|
|
ic = trindex(0);
|
2020-06-16 11:17:06 +00:00
|
|
|
} else if (std::abs((p - p2).norm()) < eps) {
|
2019-11-12 15:53:47 +00:00
|
|
|
ic = trindex(1);
|
2020-06-16 11:17:06 +00:00
|
|
|
} else if (std::abs((p - p3).norm()) < eps) {
|
2019-11-12 15:53:47 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// vector for the neigboring triangles including the detected one.
|
2020-05-22 22:45:53 +00:00
|
|
|
std::vector<size_t> neigh;
|
2019-11-12 15:53:47 +00:00
|
|
|
if (ic >= 0) { // The point is right on a vertex of the triangle
|
2020-05-22 16:21:52 +00:00
|
|
|
for (size_t n = 0; n < mesh.indices().size(); ++n) {
|
2019-11-12 15:53:47 +00:00
|
|
|
thr();
|
2020-05-22 16:21:52 +00:00
|
|
|
Vec3i ni = mesh.indices(n);
|
2019-11-12 15:53:47 +00:00
|
|
|
if ((ni(X) == ic || ni(Y) == ic || ni(Z) == ic))
|
2020-05-22 22:45:53 +00:00
|
|
|
neigh.emplace_back(n);
|
2019-11-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
} else if (ia >= 0 && ib >= 0) { // the point is on and edge
|
|
|
|
// now get all the neigboring triangles
|
2020-05-22 16:21:52 +00:00
|
|
|
for (size_t n = 0; n < mesh.indices().size(); ++n) {
|
2019-11-12 15:53:47 +00:00
|
|
|
thr();
|
2020-05-22 16:21:52 +00:00
|
|
|
Vec3i ni = mesh.indices(n);
|
2019-11-12 15:53:47 +00:00
|
|
|
if ((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) &&
|
|
|
|
(ni(X) == ib || ni(Y) == ib || ni(Z) == ib))
|
2020-05-22 22:45:53 +00:00
|
|
|
neigh.emplace_back(n);
|
2019-11-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the normals for the neighboring triangles
|
|
|
|
std::vector<Vec3d> neighnorms;
|
|
|
|
neighnorms.reserve(neigh.size());
|
2020-05-22 22:45:53 +00:00
|
|
|
for (size_t &tri_id : neigh)
|
|
|
|
neighnorms.emplace_back(mesh.normal_by_face_id(tri_id));
|
2019-11-12 15:53:47 +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();
|
2019-11-11 10:41:14 +00:00
|
|
|
});
|
2019-11-12 15:53:47 +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
|
|
|
|
// sum up the normals and then normalize the result again.
|
|
|
|
// This unification seems to be enough.
|
|
|
|
Vec3d sumnorm(0, 0, 0);
|
|
|
|
sumnorm = std::accumulate(neighnorms.begin(), lend, sumnorm);
|
|
|
|
sumnorm.normalize();
|
|
|
|
ret.row(long(ridx)) = sumnorm;
|
|
|
|
} else { // point lies safely within its triangle
|
|
|
|
Eigen::Vector3d U = p2 - p1;
|
|
|
|
Eigen::Vector3d V = p3 - p1;
|
|
|
|
ret.row(long(ridx)) = U.cross(V).normalized();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-02 10:57:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2019-09-24 13:15:49 +00:00
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
}} // namespace Slic3r::sla
|