Lay flat gizmo improvements - merge adjacent faces, compute and cache convex hull for entire ModelObject, refresh when moved, etc.

This commit is contained in:
Lukas Matena 2018-08-17 15:40:47 +02:00
parent 48b9793d3d
commit f9efcc36b6
5 changed files with 256 additions and 98 deletions

View file

@ -1409,14 +1409,14 @@ Pointf3 GLCanvas3D::Gizmos::get_flattening_normal() const
return (it != m_gizmos.end()) ? reinterpret_cast<GLGizmoFlatten*>(it->second)->get_flattening_normal() : Pointf3(0.f, 0.f, 0.f);
}
void GLCanvas3D::Gizmos::set_flattening_data(std::vector<Pointf3s> vertices_list)
void GLCanvas3D::Gizmos::set_flattening_data(const ModelObject* model_object)
{
if (!m_enabled)
return;
GizmosMap::const_iterator it = m_gizmos.find(Flatten);
if (it != m_gizmos.end())
reinterpret_cast<GLGizmoFlatten*>(it->second)->set_flattening_data(vertices_list);
reinterpret_cast<GLGizmoFlatten*>(it->second)->set_flattening_data(model_object);
}
void GLCanvas3D::Gizmos::render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const
@ -2202,62 +2202,7 @@ void GLCanvas3D::update_gizmos_data()
{
m_gizmos.set_scale(model_instance->scaling_factor);
m_gizmos.set_angle_z(model_instance->rotation);
/////////////////////////////////////////////////////////////////////////
// Following block provides convex hull data to the Flatten gizmo
// It is temporary, it should be optimized and moved elsewhere later
TriangleMesh ch = model_object->mesh().convex_hull_3d();
stl_facet* facet_ptr = ch.stl.facet_start;
std::vector<Pointf3s> points;
const unsigned int k = 20;
const float ratio = 0.2f;
const unsigned int N = 3; // 3 - triangle
while (facet_ptr < ch.stl.facet_start+ch.stl.stats.number_of_facets) {
Pointf3 a = Pointf3(facet_ptr->vertex[1].x - facet_ptr->vertex[0].x, facet_ptr->vertex[1].y - facet_ptr->vertex[0].y, facet_ptr->vertex[1].z - facet_ptr->vertex[0].z);
Pointf3 b = Pointf3(facet_ptr->vertex[2].x - facet_ptr->vertex[0].x, facet_ptr->vertex[2].y - facet_ptr->vertex[0].y, facet_ptr->vertex[2].z - facet_ptr->vertex[0].z);
if (0.5 * sqrt(dot(cross(a, b), cross(a,b))) > 50.f) {
points.emplace_back(Pointf3s(2*k*N));
std::vector<std::pair<unsigned int, unsigned int>> neighbours;
if (k != 0) {
for (unsigned int j=0; j<N; ++j) {
points.back()[j*2*k] = Pointf3(facet_ptr->vertex[j].x, facet_ptr->vertex[j].y, facet_ptr->vertex[j].z);
neighbours.push_back(std::make_pair((int)(j*2*k-k) < 0 ? (N-1)*2*k+k : j*2*k-k, j*2*k+k));
}
for (unsigned int i=0; i<k; ++i) {
// Calculate middle of each edge so that neighbours points to something useful:
for (unsigned int j=0; j<N; ++j)
if (i==0)
points.back()[j*2*k+k] = 0.5f * (points.back()[j*2*k] + points.back()[j==N-1 ? 0 : (j+1)*2*k]);
else {
float r = 0.2+0.3/(k-1)*i;
points.back()[neighbours[j].first] = r*points.back()[j*2*k] + (1-r) * points.back()[neighbours[j].first-1];
points.back()[neighbours[j].second] = r*points.back()[j*2*k] + (1-r) * points.back()[neighbours[j].second+1];
}
// Now we have a triangle and valid neighbours, we can do an iteration:
for (unsigned int j=0; j<N; ++j)
points.back()[2*k*j] = (1-ratio) * points.back()[2*k*j] + ratio * 0.5*(points.back()[neighbours[j].first] + points.back()[neighbours[j].second]);
for (auto& n : neighbours) {
++n.first;
--n.second;
}
}
}
else
for (unsigned int j=0; j<N; ++j)
points.back().emplace_back(Pointf3(facet_ptr->vertex[j].x, facet_ptr->vertex[j].y, facet_ptr->vertex[j].z));
points.back().emplace_back(Pointf3(facet_ptr->normal.x, facet_ptr->normal.y, facet_ptr->normal.z));
}
facet_ptr+=1;
}
m_gizmos.set_flattening_data(points);
////////////////////////////////////////////////////////////////////////
m_gizmos.set_flattening_data(model_object);
}
}
}
@ -2265,7 +2210,7 @@ void GLCanvas3D::update_gizmos_data()
{
m_gizmos.set_scale(1.0f);
m_gizmos.set_angle_z(0.0f);
m_gizmos.set_flattening_data(std::vector<Pointf3s>());
m_gizmos.set_flattening_data(nullptr);
}
}