mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-12 01:07:57 -06:00
Merge branch 'master' of https://github.com/prusa3d/Slic3r into objects_centering
This commit is contained in:
commit
16bd7325c1
25 changed files with 964 additions and 660 deletions
|
@ -2,6 +2,7 @@
|
|||
#include "GLCanvas3D.hpp"
|
||||
|
||||
#include "admesh/stl.h"
|
||||
#include "polypartition.h"
|
||||
#include "libslic3r/libslic3r.h"
|
||||
#include "libslic3r/ClipperUtils.hpp"
|
||||
#include "libslic3r/PrintConfig.hpp"
|
||||
|
@ -6431,6 +6432,219 @@ void GLCanvas3D::_render_camera_target() const
|
|||
}
|
||||
#endif // ENABLE_SHOW_CAMERA_TARGET
|
||||
|
||||
class TessWrapper {
|
||||
public:
|
||||
static Pointf3s tesselate(const ExPolygon &expoly, double z_, bool flipped_)
|
||||
{
|
||||
z = z_;
|
||||
flipped = flipped_;
|
||||
triangles.clear();
|
||||
intersection_points.clear();
|
||||
std::vector<GLdouble> coords;
|
||||
{
|
||||
size_t num_coords = expoly.contour.points.size();
|
||||
for (const Polygon &poly : expoly.holes)
|
||||
num_coords += poly.points.size();
|
||||
coords.reserve(num_coords * 3);
|
||||
}
|
||||
GLUtesselator *tess = gluNewTess(); // create a tessellator
|
||||
// register callback functions
|
||||
gluTessCallback(tess, GLU_TESS_BEGIN, (void(__stdcall*)(void))tessBeginCB);
|
||||
gluTessCallback(tess, GLU_TESS_END, (void(__stdcall*)(void))tessEndCB);
|
||||
gluTessCallback(tess, GLU_TESS_ERROR, (void(__stdcall*)(void))tessErrorCB);
|
||||
gluTessCallback(tess, GLU_TESS_VERTEX, (void(__stdcall*)())tessVertexCB);
|
||||
gluTessCallback(tess, GLU_TESS_COMBINE, (void (__stdcall*)(void))tessCombineCB);
|
||||
gluTessBeginPolygon(tess, 0); // with NULL data
|
||||
gluTessBeginContour(tess);
|
||||
for (const Point &pt : expoly.contour.points) {
|
||||
coords.emplace_back(unscale<double>(pt[0]));
|
||||
coords.emplace_back(unscale<double>(pt[1]));
|
||||
coords.emplace_back(0.);
|
||||
gluTessVertex(tess, &coords[coords.size() - 3], &coords[coords.size() - 3]);
|
||||
}
|
||||
gluTessEndContour(tess);
|
||||
for (const Polygon &poly : expoly.holes) {
|
||||
gluTessBeginContour(tess);
|
||||
for (const Point &pt : poly.points) {
|
||||
coords.emplace_back(unscale<double>(pt[0]));
|
||||
coords.emplace_back(unscale<double>(pt[1]));
|
||||
coords.emplace_back(0.);
|
||||
gluTessVertex(tess, &coords[coords.size() - 3], &coords[coords.size() - 3]);
|
||||
}
|
||||
gluTessEndContour(tess);
|
||||
}
|
||||
gluTessEndPolygon(tess);
|
||||
gluDeleteTess(tess);
|
||||
return std::move(triangles);
|
||||
}
|
||||
|
||||
private:
|
||||
static void tessBeginCB(GLenum which)
|
||||
{
|
||||
assert(which == GL_TRIANGLES || which == GL_TRIANGLE_FAN || which == GL_TRIANGLE_STRIP);
|
||||
if (!(which == GL_TRIANGLES || which == GL_TRIANGLE_FAN || which == GL_TRIANGLE_STRIP))
|
||||
printf("Co je to za haluz!?\n");
|
||||
primitive_type = which;
|
||||
num_points = 0;
|
||||
}
|
||||
|
||||
static void tessEndCB()
|
||||
{
|
||||
num_points = 0;
|
||||
}
|
||||
|
||||
static void tessVertexCB(const GLvoid *data)
|
||||
{
|
||||
if (data == nullptr)
|
||||
return;
|
||||
const GLdouble *ptr = (const GLdouble*)data;
|
||||
++ num_points;
|
||||
if (num_points == 1) {
|
||||
memcpy(pt0, ptr, sizeof(GLdouble) * 3);
|
||||
} else if (num_points == 2) {
|
||||
memcpy(pt1, ptr, sizeof(GLdouble) * 3);
|
||||
} else {
|
||||
bool flip = flipped;
|
||||
if (primitive_type == GL_TRIANGLE_STRIP && num_points == 4) {
|
||||
flip = !flip;
|
||||
num_points = 2;
|
||||
}
|
||||
triangles.emplace_back(pt0[0], pt0[1], z);
|
||||
if (flip) {
|
||||
triangles.emplace_back(ptr[0], ptr[1], z);
|
||||
triangles.emplace_back(pt1[0], pt1[1], z);
|
||||
} else {
|
||||
triangles.emplace_back(pt1[0], pt1[1], z);
|
||||
triangles.emplace_back(ptr[0], ptr[1], z);
|
||||
}
|
||||
if (primitive_type == GL_TRIANGLE_STRIP) {
|
||||
memcpy(pt0, pt1, sizeof(GLdouble) * 3);
|
||||
memcpy(pt1, ptr, sizeof(GLdouble) * 3);
|
||||
} else if (primitive_type == GL_TRIANGLE_FAN) {
|
||||
memcpy(pt1, ptr, sizeof(GLdouble) * 3);
|
||||
} else {
|
||||
assert(which == GL_TRIANGLES);
|
||||
assert(num_points == 3);
|
||||
num_points = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void tessCombineCB(const GLdouble newVertex[3], const GLdouble *neighborVertex[4], const GLfloat neighborWeight[4], GLdouble **outData)
|
||||
{
|
||||
intersection_points.emplace_back(newVertex[0], newVertex[1], newVertex[2]);
|
||||
*outData = intersection_points.back().data();
|
||||
}
|
||||
|
||||
static void tessErrorCB(GLenum errorCode)
|
||||
{
|
||||
const GLubyte *errorStr;
|
||||
errorStr = gluErrorString(errorCode);
|
||||
printf("Error: %s\n", (const char*)errorStr);
|
||||
}
|
||||
|
||||
static GLenum primitive_type;
|
||||
static GLdouble pt0[3];
|
||||
static GLdouble pt1[3];
|
||||
static int num_points;
|
||||
static Pointf3s triangles;
|
||||
static std::deque<Vec3d> intersection_points;
|
||||
static double z;
|
||||
static bool flipped;
|
||||
};
|
||||
|
||||
GLenum TessWrapper::primitive_type;
|
||||
GLdouble TessWrapper::pt0[3];
|
||||
GLdouble TessWrapper::pt1[3];
|
||||
int TessWrapper::num_points;
|
||||
Pointf3s TessWrapper::triangles;
|
||||
std::deque<Vec3d> TessWrapper::intersection_points;
|
||||
double TessWrapper::z;
|
||||
bool TessWrapper::flipped;
|
||||
|
||||
static Pointf3s triangulate_expolygons(const ExPolygons &polys, coordf_t z, bool flip)
|
||||
{
|
||||
Pointf3s triangles;
|
||||
#if 0
|
||||
for (const ExPolygon& poly : polys) {
|
||||
Polygons poly_triangles;
|
||||
// poly.triangulate() is based on a trapezoidal decomposition implemented in an extremely expensive way by clipping the whole input contour with a polygon!
|
||||
poly.triangulate(&poly_triangles);
|
||||
// poly.triangulate_p2t() is based on the poly2tri library, which is not quite stable, it often ends up in a nice stack overflow!
|
||||
// poly.triangulate_p2t(&poly_triangles);
|
||||
for (const Polygon &t : poly_triangles)
|
||||
if (flip) {
|
||||
triangles.emplace_back(to_3d(unscale(t.points[2]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[1]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[0]), z));
|
||||
} else {
|
||||
triangles.emplace_back(to_3d(unscale(t.points[0]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[1]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[2]), z));
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
// for (const ExPolygon &poly : union_ex(simplify_polygons(to_polygons(polys), true))) {
|
||||
for (const ExPolygon &poly : polys) {
|
||||
append(triangles, TessWrapper::tesselate(poly, z, flip));
|
||||
continue;
|
||||
|
||||
std::list<TPPLPoly> input = expoly_to_polypartition_input(poly);
|
||||
std::list<TPPLPoly> output;
|
||||
// int res = TPPLPartition().Triangulate_MONO(&input, &output);
|
||||
int res = TPPLPartition().Triangulate_EC(&input, &output);
|
||||
if (res == 1) {
|
||||
// Triangulation succeeded. Convert to triangles.
|
||||
size_t num_triangles = 0;
|
||||
for (const TPPLPoly &poly : output)
|
||||
if (poly.GetNumPoints() >= 3)
|
||||
num_triangles += (size_t)poly.GetNumPoints() - 2;
|
||||
triangles.reserve(triangles.size() + num_triangles * 3);
|
||||
for (const TPPLPoly &poly : output) {
|
||||
long num_points = poly.GetNumPoints();
|
||||
if (num_points >= 3) {
|
||||
const TPPLPoint *pt0 = &poly[0];
|
||||
const TPPLPoint *pt1 = nullptr;
|
||||
const TPPLPoint *pt2 = &poly[1];
|
||||
for (long i = 2; i < num_points; ++i) {
|
||||
pt1 = pt2;
|
||||
pt2 = &poly[i];
|
||||
if (flip) {
|
||||
triangles.emplace_back(unscale<double>(pt2->x), unscale<double>(pt2->y), z);
|
||||
triangles.emplace_back(unscale<double>(pt1->x), unscale<double>(pt1->y), z);
|
||||
triangles.emplace_back(unscale<double>(pt0->x), unscale<double>(pt0->y), z);
|
||||
} else {
|
||||
triangles.emplace_back(unscale<double>(pt0->x), unscale<double>(pt0->y), z);
|
||||
triangles.emplace_back(unscale<double>(pt1->x), unscale<double>(pt1->y), z);
|
||||
triangles.emplace_back(unscale<double>(pt2->x), unscale<double>(pt2->y), z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Triangulation by polypartition failed. Use the expensive slow implementation.
|
||||
Polygons poly_triangles;
|
||||
// poly.triangulate() is based on a trapezoidal decomposition implemented in an extremely expensive way by clipping the whole input contour with a polygon!
|
||||
poly.triangulate(&poly_triangles);
|
||||
// poly.triangulate_p2t() is based on the poly2tri library, which is not quite stable, it often ends up in a nice stack overflow!
|
||||
// poly.triangulate_p2t(&poly_triangles);
|
||||
for (const Polygon &t : poly_triangles)
|
||||
if (flip) {
|
||||
triangles.emplace_back(to_3d(unscale(t.points[2]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[1]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[0]), z));
|
||||
} else {
|
||||
triangles.emplace_back(to_3d(unscale(t.points[0]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[1]), z));
|
||||
triangles.emplace_back(to_3d(unscale(t.points[2]), z));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return triangles;
|
||||
}
|
||||
|
||||
void GLCanvas3D::_render_sla_slices() const
|
||||
{
|
||||
if (!m_use_clipping_planes || wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() != ptSLA)
|
||||
|
@ -6448,34 +6662,32 @@ void GLCanvas3D::_render_sla_slices() const
|
|||
{
|
||||
const SLAPrintObject* obj = print_objects[i];
|
||||
|
||||
Pointf3s bottom_obj_triangles;
|
||||
Pointf3s bottom_sup_triangles;
|
||||
Pointf3s top_obj_triangles;
|
||||
Pointf3s top_sup_triangles;
|
||||
|
||||
double shift_z = obj->get_current_elevation();
|
||||
double min_z = clip_min_z - shift_z;
|
||||
double max_z = clip_max_z - shift_z;
|
||||
|
||||
if (m_sla_caps[0].matches(min_z))
|
||||
SlaCap::ObjectIdToTrianglesMap::iterator it_caps_bottom = m_sla_caps[0].triangles.find(i);
|
||||
SlaCap::ObjectIdToTrianglesMap::iterator it_caps_top = m_sla_caps[1].triangles.find(i);
|
||||
{
|
||||
SlaCap::ObjectIdToTrianglesMap::const_iterator it = m_sla_caps[0].triangles.find(i);
|
||||
if (it != m_sla_caps[0].triangles.end())
|
||||
{
|
||||
bottom_obj_triangles = it->second.object;
|
||||
bottom_sup_triangles = it->second.suppports;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_sla_caps[1].matches(max_z))
|
||||
{
|
||||
SlaCap::ObjectIdToTrianglesMap::const_iterator it = m_sla_caps[1].triangles.find(i);
|
||||
if (it != m_sla_caps[1].triangles.end())
|
||||
{
|
||||
top_obj_triangles = it->second.object;
|
||||
top_sup_triangles = it->second.suppports;
|
||||
if (it_caps_bottom == m_sla_caps[0].triangles.end())
|
||||
it_caps_bottom = m_sla_caps[0].triangles.emplace(i, SlaCap::Triangles()).first;
|
||||
if (! m_sla_caps[0].matches(min_z)) {
|
||||
m_sla_caps[0].z = min_z;
|
||||
it_caps_bottom->second.object.clear();
|
||||
it_caps_bottom->second.supports.clear();
|
||||
}
|
||||
if (it_caps_top == m_sla_caps[1].triangles.end())
|
||||
it_caps_top = m_sla_caps[1].triangles.emplace(i, SlaCap::Triangles()).first;
|
||||
if (! m_sla_caps[1].matches(max_z)) {
|
||||
m_sla_caps[1].z = max_z;
|
||||
it_caps_top->second.object.clear();
|
||||
it_caps_top->second.supports.clear();
|
||||
}
|
||||
}
|
||||
Pointf3s &bottom_obj_triangles = it_caps_bottom->second.object;
|
||||
Pointf3s &bottom_sup_triangles = it_caps_bottom->second.supports;
|
||||
Pointf3s &top_obj_triangles = it_caps_top->second.object;
|
||||
Pointf3s &top_sup_triangles = it_caps_top->second.supports;
|
||||
|
||||
const std::vector<SLAPrintObject::Instance>& instances = obj->instances();
|
||||
struct InstanceTransform
|
||||
|
@ -6501,86 +6713,22 @@ void GLCanvas3D::_render_sla_slices() const
|
|||
|
||||
if (it_min_z != index.end())
|
||||
{
|
||||
// calculate model bottom cap
|
||||
if (bottom_obj_triangles.empty() && (it_min_z->second.model_slices_idx < model_slices.size()))
|
||||
{
|
||||
// calculate model bottom cap
|
||||
const ExPolygons& polys = model_slices[it_min_z->second.model_slices_idx];
|
||||
for (const ExPolygon& poly : polys)
|
||||
{
|
||||
Polygons poly_triangles;
|
||||
poly.triangulate(&poly_triangles);
|
||||
for (const Polygon& t : poly_triangles)
|
||||
{
|
||||
for (int v = 2; v >= 0; --v)
|
||||
{
|
||||
bottom_obj_triangles.emplace_back(to_3d(unscale(t.points[v]), min_z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bottom_obj_triangles = triangulate_expolygons(model_slices[it_min_z->second.model_slices_idx], min_z, true);
|
||||
// calculate support bottom cap
|
||||
if (bottom_sup_triangles.empty() && (it_min_z->second.support_slices_idx < support_slices.size()))
|
||||
{
|
||||
// calculate support bottom cap
|
||||
const ExPolygons& polys = support_slices[it_min_z->second.support_slices_idx];
|
||||
for (const ExPolygon& poly : polys)
|
||||
{
|
||||
Polygons poly_triangles;
|
||||
poly.triangulate(&poly_triangles);
|
||||
for (const Polygon& t : poly_triangles)
|
||||
{
|
||||
for (int v = 2; v >= 0; --v)
|
||||
{
|
||||
bottom_sup_triangles.emplace_back(to_3d(unscale(t.points[v]), min_z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_sla_caps[0].triangles.insert(SlaCap::ObjectIdToTrianglesMap::value_type(i, { bottom_obj_triangles, bottom_sup_triangles }));
|
||||
m_sla_caps[0].z = min_z;
|
||||
}
|
||||
bottom_sup_triangles = triangulate_expolygons(support_slices[it_min_z->second.support_slices_idx], min_z, true);
|
||||
}
|
||||
|
||||
if (it_max_z != index.end())
|
||||
{
|
||||
// calculate model top cap
|
||||
if (top_obj_triangles.empty() && (it_max_z->second.model_slices_idx < model_slices.size()))
|
||||
{
|
||||
// calculate model top cap
|
||||
const ExPolygons& polys = model_slices[it_max_z->second.model_slices_idx];
|
||||
for (const ExPolygon& poly : polys)
|
||||
{
|
||||
Polygons poly_triangles;
|
||||
poly.triangulate(&poly_triangles);
|
||||
for (const Polygon& t : poly_triangles)
|
||||
{
|
||||
for (int v = 0; v < 3; ++v)
|
||||
{
|
||||
top_obj_triangles.emplace_back(to_3d(unscale(t.points[v]), max_z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
top_obj_triangles = triangulate_expolygons(model_slices[it_max_z->second.model_slices_idx], max_z, false);
|
||||
// calculate support top cap
|
||||
if (top_sup_triangles.empty() && (it_max_z->second.support_slices_idx < support_slices.size()))
|
||||
{
|
||||
// calculate support top cap
|
||||
const ExPolygons& polys = support_slices[it_max_z->second.support_slices_idx];
|
||||
for (const ExPolygon& poly : polys)
|
||||
{
|
||||
Polygons poly_triangles;
|
||||
poly.triangulate(&poly_triangles);
|
||||
for (const Polygon& t : poly_triangles)
|
||||
{
|
||||
for (int v = 0; v < 3; ++v)
|
||||
{
|
||||
top_sup_triangles.emplace_back(to_3d(unscale(t.points[v]), max_z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_sla_caps[1].triangles.insert(SlaCap::ObjectIdToTrianglesMap::value_type(i, { top_obj_triangles, top_sup_triangles }));
|
||||
m_sla_caps[1].z = max_z;
|
||||
top_sup_triangles = triangulate_expolygons(support_slices[it_max_z->second.support_slices_idx], max_z, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue