SLATreeSupports generator now takes account for holes and can build supports through them

This commit is contained in:
Lukas Matena 2019-11-26 11:36:09 +01:00
parent bc0db7dc91
commit 73af7c64b8
6 changed files with 103 additions and 79 deletions

View file

@ -263,16 +263,13 @@ EigenMesh3D &EigenMesh3D::operator=(const EigenMesh3D &other)
}
EigenMesh3D::hit_result
EigenMesh3D::query_ray_hit(const Vec3d &s,
const Vec3d &dir,
const std::vector<DrainHole>* holes
) const
EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const
{
assert(is_approx(dir.norm(), 1.));
igl::Hit hit;
hit.t = std::numeric_limits<float>::infinity();
if (! holes) {
if (m_holes.empty()) {
m_aabb->intersect_ray(m_V, m_F, s, dir, hit);
hit_result ret(*this);
ret.m_t = double(hit.t);
@ -286,7 +283,7 @@ EigenMesh3D::query_ray_hit(const Vec3d &s,
else {
// If there are holes, the hit_results will be made by
// query_ray_hits (object) and filter_hits (holes):
return filter_hits(query_ray_hits(s, dir), *holes);
return filter_hits(query_ray_hits(s, dir));
}
}
@ -316,46 +313,59 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const
}
EigenMesh3D::hit_result EigenMesh3D::filter_hits(
const std::vector<EigenMesh3D::hit_result>& object_hits,
const std::vector<DrainHole>& holes) const
const std::vector<EigenMesh3D::hit_result>& object_hits) const
{
assert(! m_holes.empty());
hit_result out(*this);
out.m_t = std::nan("");
if (! holes.empty() && ! object_hits.empty()) {
Vec3d s = object_hits.front().source();
Vec3d dir = object_hits.front().direction();
if (object_hits.empty())
return out;
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;
const Vec3d& s = object_hits.front().source();
const Vec3d& dir = object_hits.front().direction();
// Collect hits on all holes, preserve information about entry/exit
for (const sla::DrainHole& hole : holes) {
std::array<std::pair<float, Vec3d>, 2> isects;
if (hole.get_intersections(s.cast<float>(),
dir.cast<float>(), isects)) {
hole_isects.emplace_back(isects[0].first, isects[0].second, true);
hole_isects.emplace_back(isects[1].first, isects[1].second, false);
}
// 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;
// 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;
if (hole.get_intersections(s.cast<float>(),
dir.cast<float>(), isects)) {
hole_isects.emplace_back(isects[0].first, isects[0].second, true);
hole_isects.emplace_back(isects[1].first, isects[1].second, false);
}
// 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; });
}
// Remove hole hits behind the source
for (int i=0; i<int(hole_isects.size()); ++i)
if (hole_isects[i].t < 0.f)
hole_isects.erase(hole_isects.begin() + (i--));
// Now inspect the intersections with object and holes, keep track how
// deep are we nested in mesh/holes and pick the correct intersection
int hole_nested = 0;
int object_nested = 0;
// 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;
bool is_hole = false;
bool is_entry = false;
const HoleHit* next_hole_hit = &hole_isects.front();
const HoleHit* next_hole_hit = hole_isects.empty() ? nullptr : &hole_isects.front();
const hit_result* next_mesh_hit = &object_hits.front();
while (next_hole_hit || next_mesh_hit) {
@ -367,23 +377,25 @@ EigenMesh3D::hit_result EigenMesh3D::filter_hits(
// Is this entry or exit hit?
is_entry = is_hole ? next_hole_hit->entry : ! next_mesh_hit->is_inside();
if (! is_hole && is_entry && hole_nested == 0) {
// This mesh point is the one we seek
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;
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;
}
}
hole_nested += (is_hole ? (is_entry ? 1 : -1) : 0);
object_nested += (! is_hole ? (is_entry ? 1 : -1) : 0);
// Increase/decrease the counter
(is_hole ? hole_nested : object_nested) += (is_entry ? 1 : -1);
// Advance the pointer
// Advance the respective pointer
if (is_hole && next_hole_hit++ == &hole_isects.back())
next_hole_hit = nullptr;
if (! is_hole && next_mesh_hit++ == &object_hits.back())
@ -391,6 +403,7 @@ EigenMesh3D::hit_result EigenMesh3D::filter_hits(
}
}
// if we got here, the ray ended up in infinity
return out;
}