mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-06-16 00:42:44 +00:00
* Add runtime display backend detection for Wayland support Add LinuxDisplayBackend utility to detect X11 vs Wayland at runtime using GDK_IS_X11_DISPLAY / GDK_IS_WAYLAND_DISPLAY macros. This is the foundation for removing the forced GDK_BACKEND=x11 and enabling native Wayland support. - New files: LinuxDisplayBackend.hpp/.cpp with get_linux_display_backend(), is_running_on_wayland(), and is_running_on_x11() - Propagate wxHAVE_GDK_X11 / wxHAVE_GDK_WAYLAND from FindGTK3.cmake as compile definitions to libslic3r_gui - No-op on non-Linux platforms (returns Unknown / false) * Fix Phase 1 code quality: pragma once, source ordering, static cache * Make X11 initialization conditional for Wayland support Remove the unconditional GDK_BACKEND=x11 force that blocked native Wayland. Replace with conditional logic: - EGL safety fallback: re-force X11 only when wxUSE_GLCANVAS_EGL is off and WAYLAND_DISPLAY is set, with a warning log - XInitThreads() only called when DISPLAY is set (X11 in use) - __GLX_VENDOR_LIBRARY_NAME only set when DISPLAY is present (GLX-specific) - WEBKIT_DISABLE_COMPOSITING_MODE only set under XWayland (both DISPLAY and WAYLAND_DISPLAY present) - Guard X11/Xlib.h include with __has_include for robustness - Restore display validation to accept either DISPLAY or WAYLAND_DISPLAY This is Phase 2 of the Wayland support plan. * Fix Phase 2: safer EGL macro check, add clarifying comments * Add GLAD2 library and replace GLEW linkage in build system Set up GLAD2 as a static library to replace GLEW for OpenGL loading. GLAD2 supports both GLX and EGL, which is required for Wayland support. - Create src/glad/ with pre-generated GLAD2 sources (GL 4.6 compat) - Add src/glad/CMakeLists.txt building glad as a static library - Wire glad into src/CMakeLists.txt before libvgcode - Modify libvgcode to use shared glad for GL path (keeps local copy only for GLES2/Emscripten) to avoid duplicate symbol conflicts - Replace GLEW::GLEW with glad in libslic3r_gui link libraries Note: GLEW is kept in deps for OpenCSG. Code migration from GL/glew.h to glad/gl.h headers will follow in Phase 3B+3C. * Fix Phase 3A+3D: libvgcode GLAD include, dead files, dlopen dep, OpenGL link var * Migrate from GLEW to GLAD: replace headers and API calls across codebase Replace all #include <GL/glew.h> with <glad/gl.h> across 49 source files. Migrate GLEW API calls to GLAD equivalents: - glewInit/glewExperimental -> gladLoaderLoadGL() - GLEW_EXT_* / GLEW_ARB_* extension checks -> GLAD_GL_EXT_* / GLAD_GL_ARB_* - Remove GLEW-specific EGL/GLX mismatch #error guards (not needed with GLAD) - Replace unavailable EXT symbols with core GL equivalents in GLCanvas3D.cpp (GL_MAX_SAMPLES, glRenderbufferStorageMultisample, glBlitFramebuffer, GL_READ/DRAW_FRAMEBUFFER) - Update log messages from glewInit to gladLoadGL * Fix Phase 3B+3C: remove GLEW find, clean EXT symbols, update attribution - Remove find_package(GLEW) block from root CMakeLists.txt since GLEW is no longer linked by any main application code - Remove "glew" from SLIC3R_STATIC option description - Replace all remaining EXT framebuffer symbols with core equivalents in render_thumbnail_framebuffer_ext and _rectangular_selection_picking_pass - Update AboutDialog credits from GLEW to GLAD * Enable EGL in wxWidgets and add runtime GLX/EGL selection for Wayland - Set wxUSE_GLCANVAS_EGL=ON in wxWidgets build and Flatpak manifest - Add PreferGLX() call on X11 sessions for driver compatibility - Remove Phase 2 safety fallback (EGL is now always compiled in) - Guard SwapBuffers against hidden canvases to prevent Wayland stalls * Fix Phase 4: move PreferGLX to app startup, fix FPS counter guard Move wxGLCanvas::PreferGLX() from OpenGLManager::create_wxglcanvas() (static initializer) to GUI_App::on_init_inner() before any wxGLCanvas is constructed. This prevents a race where SkipPartCanvas could trigger wxGLBackend::Init() before the GLX preference is set. The new location also adds explicit is_running_on_wayland() detection with a warning for unknown backends. Move increment_fps_counter() inside the IsShownOnScreen() guard so FPS is only counted when a frame is actually swapped. * Update GLFW from 3.3.7 to 3.4 for runtime Wayland/X11 backend selection Replace the compile-time GLFW_USE_WAYLAND flag (which locked to a single backend) with GLFW 3.4's GLFW_BUILD_WAYLAND + GLFW_BUILD_X11 flags that build both backends and auto-select at runtime based on the available display server. This enables the CLI thumbnail renderer to work on both Wayland and X11 sessions without separate builds. * wayland: Fix UI call sites that rely on global screen coordinates On Wayland, wxGetMousePosition() returns (0,0) and SetPosition() is a no-op for top-level windows. Fix the highest-impact call sites: - GLCanvas3D: Use cached m_mouse.position from event handlers instead of wxGetMousePosition() + ScreenToClient() in get_local_mouse_position() - Plater: Use event-relative coords via ClientToScreen(e.GetPosition()) instead of wxGetMousePosition() in 3 leave-window handlers - BBLTopbar: Use event.GetPosition() and FindToolByPosition() directly in mouse handlers instead of wxGetMousePosition()/FindToolByCurrentPosition() - Search: Use focus-based dismiss logic on Wayland instead of wxGetMousePosition()-based rect checks in SearchDialog and SearchObjectDialog - GUI_App: Skip SetPosition() in window_pos_restore() on Wayland where it is a no-op; still restore size and maximize state - Button: Position tooltip relative to button widget via ClientToScreen instead of wxGetMousePosition() * Fix SearchDialog Wayland dismiss: guard against search_line focus * flatpak: Add Wayland socket permission for native Wayland support * spec * Fix crash on Wayland when wxWidgets lacks EGL support Restore the safety fallback that forces GDK_BACKEND=x11 when wxWidgets was not built with wxUSE_GLCANVAS_EGL=ON. Without this, the GLX backend tries to access a non-existent X11 display on native Wayland, crashing in wxGLCanvas::IsDisplaySupported() with SIGSEGV at offset 0xe4. Also add a defense-in-depth guard in detect_multisample() that skips the IsDisplaySupported call entirely on Wayland without EGL. Root cause: deps/wxWidgets must be rebuilt after enabling EGL. The compile-time check in OrcaSlicer.cpp detects the mismatch and falls back safely. * Fix EGL detection: use wxHAS_EGL instead of wxUSE_GLCANVAS_EGL wxUSE_GLCANVAS_EGL is a CMake build option, NOT a C++ preprocessor macro. The actual macro defined in wxWidgets setup.h is wxHAS_EGL. All compile-time EGL checks were using the wrong macro, causing the safety fallback to always trigger even with a properly built EGL-enabled wxWidgets. * Fix GL function pointers invalidated on Wayland/EGL gladLoaderLoadGL() dlopen's libGL.so.1 to resolve GL function pointers via dlsym, then immediately dlclose's the handle. On X11/GLX this is fine because the GLX context keeps libGL.so mapped. On Wayland/EGL, nothing else holds libGL.so open, so dlclose unmaps it and all function pointers become dangling — causing SIGSEGV on the first GL call. Fix: on Wayland, use gladLoadGL(eglGetProcAddress) which resolves function pointers through the EGL loader without opening/closing libGL.so. * fix crash on start and various rendering issues * fix crash on close * small refactor * move GPU selection to desktop file * clean up a bit * clean up more * fix appimage error
588 lines
22 KiB
C++
588 lines
22 KiB
C++
#include "MeshUtils.hpp"
|
|
|
|
#include "libslic3r/Tesselate.hpp"
|
|
#include "libslic3r/TriangleMesh.hpp"
|
|
#include "libslic3r/TriangleMeshSlicer.hpp"
|
|
#include "libslic3r/ClipperUtils.hpp"
|
|
#include "libslic3r/Model.hpp"
|
|
#include "libslic3r/CSGMesh/SliceCSGMesh.hpp"
|
|
|
|
#include "libslic3r/libslic3r.h"
|
|
#include "slic3r/GUI/GUI_App.hpp"
|
|
#include "slic3r/GUI/Plater.hpp"
|
|
#include "slic3r/GUI/Camera.hpp"
|
|
#include "slic3r/GUI/CameraUtils.hpp"
|
|
|
|
|
|
#include <glad/gl.h>
|
|
|
|
#include <igl/unproject.h>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
namespace Slic3r {
|
|
namespace GUI {
|
|
|
|
void MeshClipper::set_behaviour(bool fill_cut, double contour_width)
|
|
{
|
|
if (fill_cut != m_fill_cut || ! is_approx(contour_width, m_contour_width))
|
|
m_result.reset();
|
|
m_fill_cut = fill_cut;
|
|
m_contour_width = contour_width;
|
|
}
|
|
|
|
|
|
|
|
void MeshClipper::set_plane(const ClippingPlane& plane)
|
|
{
|
|
if (m_plane != plane) {
|
|
m_plane = plane;
|
|
m_result.reset();
|
|
}
|
|
}
|
|
|
|
|
|
void MeshClipper::set_limiting_plane(const ClippingPlane& plane)
|
|
{
|
|
if (m_limiting_plane != plane) {
|
|
m_limiting_plane = plane;
|
|
m_result.reset();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MeshClipper::set_mesh(const indexed_triangle_set& mesh)
|
|
{
|
|
if (m_mesh.get() != &mesh) {
|
|
m_mesh = &mesh;
|
|
m_result.reset();
|
|
}
|
|
}
|
|
|
|
void MeshClipper::set_mesh(AnyPtr<const indexed_triangle_set> &&ptr)
|
|
{
|
|
if (m_mesh.get() != ptr.get()) {
|
|
m_mesh = std::move(ptr);
|
|
m_result.reset();
|
|
}
|
|
}
|
|
|
|
void MeshClipper::set_negative_mesh(const indexed_triangle_set& mesh)
|
|
{
|
|
if (m_negative_mesh.get() != &mesh) {
|
|
m_negative_mesh = &mesh;
|
|
m_result.reset();
|
|
}
|
|
}
|
|
|
|
void MeshClipper::set_negative_mesh(AnyPtr<const indexed_triangle_set> &&ptr)
|
|
{
|
|
if (m_negative_mesh.get() != ptr.get()) {
|
|
m_negative_mesh = std::move(ptr);
|
|
m_result.reset();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MeshClipper::set_transformation(const Geometry::Transformation& trafo)
|
|
{
|
|
if (! m_trafo.get_matrix().isApprox(trafo.get_matrix())) {
|
|
m_trafo = trafo;
|
|
m_result.reset();
|
|
}
|
|
}
|
|
|
|
void MeshClipper::render_cut(const ColorRGBA& color, const std::vector<size_t>* ignore_idxs)
|
|
{
|
|
if (! m_result)
|
|
recalculate_triangles();
|
|
GLShaderProgram* curr_shader = wxGetApp().get_current_shader();
|
|
if (curr_shader != nullptr)
|
|
curr_shader->stop_using();
|
|
|
|
GLShaderProgram* shader = wxGetApp().get_shader("flat");
|
|
if (shader != nullptr) {
|
|
shader->start_using();
|
|
const Camera& camera = wxGetApp().plater()->get_camera();
|
|
shader->set_uniform("view_model_matrix", camera.get_view_matrix());
|
|
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
|
|
for (size_t i=0; i<m_result->cut_islands.size(); ++i) {
|
|
if (ignore_idxs && std::binary_search(ignore_idxs->begin(), ignore_idxs->end(), i))
|
|
continue;
|
|
CutIsland& isl = m_result->cut_islands[i];
|
|
isl.model.set_color(isl.disabled ? ColorRGBA(0.5f, 0.5f, 0.5f, 1.f) : color);
|
|
isl.model.render();
|
|
}
|
|
shader->stop_using();
|
|
}
|
|
|
|
if (curr_shader != nullptr)
|
|
curr_shader->start_using();
|
|
}
|
|
|
|
|
|
void MeshClipper::render_contour(const ColorRGBA& color, const std::vector<size_t>* ignore_idxs)
|
|
{
|
|
if (! m_result)
|
|
recalculate_triangles();
|
|
|
|
GLShaderProgram* curr_shader = wxGetApp().get_current_shader();
|
|
if (curr_shader != nullptr)
|
|
curr_shader->stop_using();
|
|
|
|
GLShaderProgram* shader = wxGetApp().get_shader("flat");
|
|
if (shader != nullptr) {
|
|
shader->start_using();
|
|
const Camera& camera = wxGetApp().plater()->get_camera();
|
|
shader->set_uniform("view_model_matrix", camera.get_view_matrix());
|
|
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
|
|
for (size_t i=0; i<m_result->cut_islands.size(); ++i) {
|
|
if (ignore_idxs && std::binary_search(ignore_idxs->begin(), ignore_idxs->end(), i))
|
|
continue;
|
|
CutIsland& isl = m_result->cut_islands[i];
|
|
isl.model_expanded.set_color(isl.disabled ? ColorRGBA(1.f, 0.f, 0.f, 1.f) : color);
|
|
isl.model_expanded.render();
|
|
}
|
|
shader->stop_using();
|
|
}
|
|
|
|
if (curr_shader != nullptr)
|
|
curr_shader->start_using();
|
|
}
|
|
|
|
int MeshClipper::is_projection_inside_cut(const Vec3d& point_in) const
|
|
{
|
|
if (!m_result || m_result->cut_islands.empty())
|
|
return -1;
|
|
Vec3d point = m_result->trafo.inverse() * point_in;
|
|
Point pt_2d = Point::new_scale(Vec2d(point.x(), point.y()));
|
|
|
|
for (int i=0; i<int(m_result->cut_islands.size()); ++i) {
|
|
const CutIsland& isl = m_result->cut_islands[i];
|
|
if (isl.expoly_bb.contains(pt_2d) && isl.expoly.contains(pt_2d))
|
|
return i; // TODO: handle intersecting contours
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
bool MeshClipper::has_valid_contour() const
|
|
{
|
|
return m_result && std::any_of(m_result->cut_islands.begin(), m_result->cut_islands.end(), [](const CutIsland& isl) { return !isl.expoly.empty(); });
|
|
}
|
|
|
|
std::vector<Vec3d> MeshClipper::point_per_contour() const {
|
|
std::vector<Vec3d> out;
|
|
if (m_result == std::nullopt) {
|
|
return out;
|
|
}
|
|
assert(m_result);
|
|
for (const auto& isl : m_result->cut_islands) {
|
|
assert(isl.expoly.contour.size() > 2);
|
|
// Now return a point lying inside the contour but not in a hole.
|
|
// We do this by taking a point lying close to the edge, repeating
|
|
// this several times for different edges and distances from them.
|
|
// (We prefer point not extremely close to the border.
|
|
bool done = false;
|
|
Vec2d p;
|
|
size_t i = 1;
|
|
while (i < isl.expoly.contour.size()) {
|
|
const Vec2d& a = unscale(isl.expoly.contour.points[i-1]);
|
|
const Vec2d& b = unscale(isl.expoly.contour.points[i]);
|
|
Vec2d n = (b-a).normalized();
|
|
std::swap(n.x(), n.y());
|
|
n.x() = -1 * n.x();
|
|
double f = 10.;
|
|
while (f > 0.05) {
|
|
p = (0.5*(b+a)) + f * n;
|
|
if (isl.expoly.contains(Point::new_scale(p))) {
|
|
done = true;
|
|
break;
|
|
}
|
|
f = f/10.;
|
|
}
|
|
if (done)
|
|
break;
|
|
i += std::max(size_t(2), isl.expoly.contour.size() / 5);
|
|
}
|
|
// If the above failed, just return the centroid, regardless of whether
|
|
// it is inside the contour or in a hole (we must return something).
|
|
Vec2d c = done ? p : unscale(isl.expoly.contour.centroid());
|
|
out.emplace_back(m_result->trafo * Vec3d(c.x(), c.y(), 0.));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
|
|
void MeshClipper::recalculate_triangles()
|
|
{
|
|
m_result = ClipResult();
|
|
|
|
auto plane_mesh = Eigen::Hyperplane<double, 3>(m_plane.get_normal(), -m_plane.distance(Vec3d::Zero())).transform(m_trafo.get_matrix().inverse());
|
|
const Vec3d up = plane_mesh.normal();
|
|
const float height_mesh = -plane_mesh.offset();
|
|
|
|
// Now do the cutting
|
|
MeshSlicingParams slicing_params;
|
|
slicing_params.trafo.rotate(Eigen::Quaternion<double, Eigen::DontAlign>::FromTwoVectors(up, Vec3d::UnitZ()));
|
|
|
|
ExPolygons expolys;
|
|
|
|
if (m_csgmesh.empty()) {
|
|
if (m_mesh)
|
|
expolys = union_ex(slice_mesh(*m_mesh, height_mesh, slicing_params));
|
|
|
|
if (m_negative_mesh && !m_negative_mesh->empty()) {
|
|
const ExPolygons neg_expolys = union_ex(slice_mesh(*m_negative_mesh, height_mesh, slicing_params));
|
|
expolys = diff_ex(expolys, neg_expolys);
|
|
}
|
|
} else {
|
|
expolys = std::move(csg::slice_csgmesh_ex(range(m_csgmesh), {height_mesh}, MeshSlicingParamsEx{slicing_params}).front());
|
|
}
|
|
|
|
|
|
// Triangulate and rotate the cut into world coords:
|
|
Eigen::Quaterniond q;
|
|
q.setFromTwoVectors(Vec3d::UnitZ(), up);
|
|
Transform3d tr = Transform3d::Identity();
|
|
tr.rotate(q);
|
|
tr = m_trafo.get_matrix() * tr;
|
|
|
|
m_result->trafo = tr;
|
|
|
|
if (m_limiting_plane != ClippingPlane::ClipsNothing())
|
|
{
|
|
// Now remove whatever ended up below the limiting plane (e.g. sinking objects).
|
|
// First transform the limiting plane from world to mesh coords.
|
|
// Note that inverse of tr transforms the plane from world to horizontal.
|
|
const Vec3d normal_old = m_limiting_plane.get_normal().normalized();
|
|
const Vec3d normal_new = (tr.matrix().block<3,3>(0,0).transpose() * normal_old).normalized();
|
|
|
|
// normal_new should now be the plane normal in mesh coords. To find the offset,
|
|
// transform a point and set offset so it belongs to the transformed plane.
|
|
Vec3d pt = Vec3d::Zero();
|
|
const double plane_offset = m_limiting_plane.get_data()[3];
|
|
if (std::abs(normal_old.z()) > 0.5) // normal is normalized, at least one of the coords if larger than sqrt(3)/3 = 0.57
|
|
pt.z() = - plane_offset / normal_old.z();
|
|
else if (std::abs(normal_old.y()) > 0.5)
|
|
pt.y() = - plane_offset / normal_old.y();
|
|
else
|
|
pt.x() = - plane_offset / normal_old.x();
|
|
pt = tr.inverse() * pt;
|
|
const double offset = -(normal_new.dot(pt));
|
|
|
|
if (std::abs(normal_old.dot(m_plane.get_normal().normalized())) > 0.99) {
|
|
// The cuts are parallel, show all or nothing.
|
|
if (normal_old.dot(m_plane.get_normal().normalized()) < 0.0 && offset < height_mesh)
|
|
expolys.clear();
|
|
} else {
|
|
// The cut is a horizontal plane defined by z=height_mesh.
|
|
// ax+by+e=0 is the line of intersection with the limiting plane.
|
|
// Normalized so a^2 + b^2 = 1.
|
|
const double len = std::hypot(normal_new.x(), normal_new.y());
|
|
if (len == 0.)
|
|
return;
|
|
const double a = normal_new.x() / len;
|
|
const double b = normal_new.y() / len;
|
|
const double e = (normal_new.z() * height_mesh + offset) / len;
|
|
|
|
// We need a half-plane to limit the cut. Get angle of the intersecting line.
|
|
double angle = (b != 0.0) ? std::atan(-a / b) : ((a < 0.0) ? -0.5 * M_PI : 0.5 * M_PI);
|
|
if (b > 0) // select correct half-plane
|
|
angle += M_PI;
|
|
|
|
// We'll take a big rectangle above x-axis and rotate and translate
|
|
// it so it lies on our line. This will be the figure to subtract
|
|
// from the cut. The coordinates must not overflow after the transform,
|
|
// make the rectangle a bit smaller.
|
|
const coord_t size = (std::numeric_limits<coord_t>::max()/2 - scale_(std::max(std::abs(e * a), std::abs(e * b)))) / 4;
|
|
Polygons ep {Polygon({Point(-size, 0), Point(size, 0), Point(size, 2*size), Point(-size, 2*size)})};
|
|
ep.front().rotate(angle);
|
|
ep.front().translate(scale_(-e * a), scale_(-e * b));
|
|
expolys = diff_ex(expolys, ep);
|
|
}
|
|
}
|
|
|
|
tr.pretranslate(0.001 * m_plane.get_normal().normalized()); // to avoid z-fighting
|
|
Transform3d tr2 = tr;
|
|
tr2.pretranslate(0.002 * m_plane.get_normal().normalized());
|
|
|
|
|
|
std::vector<Vec2f> triangles2d;
|
|
|
|
for (const ExPolygon& exp : expolys) {
|
|
triangles2d.clear();
|
|
|
|
m_result->cut_islands.push_back(CutIsland());
|
|
CutIsland& isl = m_result->cut_islands.back();
|
|
|
|
if (m_fill_cut) {
|
|
triangles2d = triangulate_expolygon_2f(exp, m_trafo.get_matrix().matrix().determinant() < 0.);
|
|
GLModel::Geometry init_data;
|
|
init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3 };
|
|
init_data.reserve_vertices(triangles2d.size());
|
|
init_data.reserve_indices(triangles2d.size());
|
|
|
|
// vertices + indices
|
|
for (auto it = triangles2d.cbegin(); it != triangles2d.cend(); it = it + 3) {
|
|
init_data.add_vertex((Vec3f)(tr * Vec3d((*(it + 0)).x(), (*(it + 0)).y(), height_mesh)).cast<float>(), (Vec3f)up.cast<float>());
|
|
init_data.add_vertex((Vec3f)(tr * Vec3d((*(it + 1)).x(), (*(it + 1)).y(), height_mesh)).cast<float>(), (Vec3f)up.cast<float>());
|
|
init_data.add_vertex((Vec3f)(tr * Vec3d((*(it + 2)).x(), (*(it + 2)).y(), height_mesh)).cast<float>(), (Vec3f)up.cast<float>());
|
|
const size_t idx = it - triangles2d.cbegin();
|
|
init_data.add_triangle((unsigned int)idx, (unsigned int)idx + 1, (unsigned int)idx + 2);
|
|
}
|
|
|
|
if (!init_data.is_empty())
|
|
isl.model.init_from(std::move(init_data));
|
|
}
|
|
|
|
if (m_contour_width != 0. && ! exp.contour.empty()) {
|
|
triangles2d.clear();
|
|
|
|
// The contours must not scale with the object. Check the scale factor
|
|
// in the respective directions, create a scaled copy of the ExPolygon
|
|
// offset it and then unscale the result again.
|
|
|
|
Transform3d t = tr;
|
|
t.translation() = Vec3d::Zero();
|
|
double scale_x = (t * Vec3d::UnitX()).norm();
|
|
double scale_y = (t * Vec3d::UnitY()).norm();
|
|
|
|
// To prevent overflow after scaling, downscale the input if needed:
|
|
double extra_scale = 1.;
|
|
coord_t limit = coord_t(std::min(std::numeric_limits<coord_t>::max() / (2. * std::max(1., scale_x)), std::numeric_limits<coord_t>::max() / (2. * std::max(1., scale_y))));
|
|
coord_t max_coord = 0;
|
|
for (const Point& pt : exp.contour)
|
|
max_coord = std::max(max_coord, std::max(std::abs(pt.x()), std::abs(pt.y())));
|
|
if (max_coord + m_contour_width >= limit)
|
|
extra_scale = 0.9 * double(limit) / max_coord;
|
|
|
|
ExPolygon exp_copy = exp;
|
|
if (extra_scale != 1.)
|
|
exp_copy.scale(extra_scale);
|
|
exp_copy.scale(scale_x, scale_y);
|
|
|
|
ExPolygons expolys_exp = offset_ex(exp_copy, scale_(m_contour_width));
|
|
expolys_exp = diff_ex(expolys_exp, ExPolygons({exp_copy}));
|
|
|
|
for (ExPolygon& e : expolys_exp) {
|
|
e.scale(1./scale_x, 1./scale_y);
|
|
if (extra_scale != 1.)
|
|
e.scale(1./extra_scale);
|
|
}
|
|
|
|
|
|
triangles2d = triangulate_expolygons_2f(expolys_exp, m_trafo.get_matrix().matrix().determinant() < 0.);
|
|
GLModel::Geometry init_data = GLModel::Geometry();
|
|
init_data.format = { GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3 };
|
|
init_data.reserve_vertices(triangles2d.size());
|
|
init_data.reserve_indices(triangles2d.size());
|
|
|
|
// vertices + indices
|
|
for (auto it = triangles2d.cbegin(); it != triangles2d.cend(); it = it + 3) {
|
|
init_data.add_vertex((Vec3f)(tr2 * Vec3d((*(it + 0)).x(), (*(it + 0)).y(), height_mesh)).cast<float>(), (Vec3f)up.cast<float>());
|
|
init_data.add_vertex((Vec3f)(tr2 * Vec3d((*(it + 1)).x(), (*(it + 1)).y(), height_mesh)).cast<float>(), (Vec3f)up.cast<float>());
|
|
init_data.add_vertex((Vec3f)(tr2 * Vec3d((*(it + 2)).x(), (*(it + 2)).y(), height_mesh)).cast<float>(), (Vec3f)up.cast<float>());
|
|
const size_t idx = it - triangles2d.cbegin();
|
|
init_data.add_triangle((unsigned short)idx, (unsigned short)idx + 1, (unsigned short)idx + 2);
|
|
}
|
|
|
|
if (!init_data.is_empty())
|
|
isl.model_expanded.init_from(std::move(init_data));
|
|
}
|
|
|
|
isl.expoly = std::move(exp);
|
|
isl.expoly_bb = get_extents(isl.expoly);
|
|
|
|
Point centroid_scaled = isl.expoly.contour.centroid();
|
|
Vec3d centroid_world = m_result->trafo * Vec3d(unscale(centroid_scaled).x(), unscale(centroid_scaled).y(), 0.);
|
|
isl.hash = isl.expoly.contour.size() + size_t(std::abs(100.*centroid_world.x())) + size_t(std::abs(100.*centroid_world.y())) + size_t(std::abs(100.*centroid_world.z()));
|
|
}
|
|
|
|
// Now sort the islands so they are in defined order. This is a hack needed by cut gizmo, which sometimes
|
|
// flips the normal of the cut, in which case the contours stay the same but their order may change.
|
|
std::sort(m_result->cut_islands.begin(), m_result->cut_islands.end(), [](const CutIsland& a, const CutIsland& b) {
|
|
return a.hash < b.hash;
|
|
});
|
|
}
|
|
|
|
|
|
Vec3f MeshRaycaster::get_triangle_normal(size_t facet_idx) const
|
|
{
|
|
return m_normals[facet_idx];
|
|
}
|
|
|
|
void MeshRaycaster::line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera, Vec3d& point, Vec3d& direction)
|
|
{
|
|
CameraUtils::ray_from_screen_pos(camera, mouse_pos, point, direction);
|
|
Transform3d inv = trafo.inverse();
|
|
point = inv*point;
|
|
direction = inv.linear()*direction;
|
|
}
|
|
|
|
bool MeshRaycaster::unproject_on_mesh(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera,
|
|
Vec3f& position, Vec3f& normal, const ClippingPlane* clipping_plane,
|
|
size_t* facet_idx, bool sinking_limit) const
|
|
{
|
|
Vec3d point;
|
|
Vec3d direction;
|
|
CameraUtils::ray_from_screen_pos(camera, mouse_pos, point, direction);
|
|
Transform3d inv = trafo.inverse();
|
|
point = inv*point;
|
|
direction = inv.linear()*direction;
|
|
|
|
std::vector<AABBMesh::hit_result> hits = m_emesh.query_ray_hits(point, direction);
|
|
|
|
if (hits.empty())
|
|
return false; // no intersection found
|
|
|
|
unsigned i = 0;
|
|
|
|
// Remove points that are obscured or cut by the clipping plane.
|
|
// Also, remove anything below the bed (sinking objects).
|
|
for (i=0; i<hits.size(); ++i) {
|
|
Vec3d transformed_hit = trafo * hits[i].position();
|
|
if (transformed_hit.z() >= (sinking_limit ? SINKING_Z_THRESHOLD : -std::numeric_limits<double>::max()) &&
|
|
(!clipping_plane || !clipping_plane->is_point_clipped(transformed_hit)))
|
|
break;
|
|
}
|
|
|
|
if (i==hits.size() || (hits.size()-i) % 2 != 0) {
|
|
// All hits are either clipped, or there is an odd number of unclipped
|
|
// hits - meaning the nearest must be from inside the mesh.
|
|
return false;
|
|
}
|
|
|
|
// Now stuff the points in the provided vector and calculate normals if asked about them:
|
|
position = hits[i].position().cast<float>();
|
|
normal = hits[i].normal().cast<float>();
|
|
|
|
if (facet_idx)
|
|
*facet_idx = hits[i].face();
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
bool MeshRaycaster::intersects_line(Vec3d point, Vec3d direction, const Transform3d& trafo) const
|
|
{
|
|
Transform3d trafo_inv = trafo.inverse();
|
|
Vec3d to = trafo_inv * (point + direction);
|
|
point = trafo_inv * point;
|
|
direction = (to-point).normalized();
|
|
|
|
std::vector<AABBMesh::hit_result> hits = m_emesh.query_ray_hits(point, direction);
|
|
std::vector<AABBMesh::hit_result> neg_hits = m_emesh.query_ray_hits(point, -direction);
|
|
|
|
return !hits.empty() || !neg_hits.empty();
|
|
}
|
|
|
|
|
|
std::vector<unsigned> MeshRaycaster::get_unobscured_idxs(const Geometry::Transformation& trafo, const Camera& camera, const std::vector<Vec3f>& points,
|
|
const ClippingPlane* clipping_plane) const
|
|
{
|
|
std::vector<unsigned> out;
|
|
|
|
const Transform3d instance_matrix_no_translation_no_scaling = trafo.get_rotation_matrix();
|
|
Vec3d direction_to_camera = -camera.get_dir_forward();
|
|
Vec3d direction_to_camera_mesh = (instance_matrix_no_translation_no_scaling.inverse() * direction_to_camera).normalized().eval();
|
|
direction_to_camera_mesh = direction_to_camera_mesh.cwiseProduct(trafo.get_scaling_factor());
|
|
const Transform3d inverse_trafo = trafo.get_matrix().inverse();
|
|
|
|
for (size_t i=0; i<points.size(); ++i) {
|
|
const Vec3f& pt = points[i];
|
|
if (clipping_plane && clipping_plane->is_point_clipped(pt.cast<double>()))
|
|
continue;
|
|
|
|
bool is_obscured = false;
|
|
// Cast a ray in the direction of the camera and look for intersection with the mesh:
|
|
std::vector<AABBMesh::hit_result> hits;
|
|
// Offset the start of the ray by EPSILON to account for numerical inaccuracies.
|
|
hits = m_emesh.query_ray_hits((inverse_trafo * pt.cast<double>() + direction_to_camera_mesh * EPSILON),
|
|
direction_to_camera_mesh);
|
|
|
|
if (! hits.empty()) {
|
|
// If the closest hit facet normal points in the same direction as the ray,
|
|
// we are looking through the mesh and should therefore discard the point:
|
|
if (hits.front().normal().dot(direction_to_camera_mesh.cast<double>()) > 0)
|
|
is_obscured = true;
|
|
|
|
// Eradicate all hits that the caller wants to ignore
|
|
for (unsigned j=0; j<hits.size(); ++j) {
|
|
if (clipping_plane && clipping_plane->is_point_clipped(trafo.get_matrix() * hits[j].position())) {
|
|
hits.erase(hits.begin()+j);
|
|
--j;
|
|
}
|
|
}
|
|
|
|
// FIXME: the intersection could in theory be behind the camera, but as of now we only have camera direction.
|
|
// Also, the threshold is in mesh coordinates, not in actual dimensions.
|
|
if (! hits.empty())
|
|
is_obscured = true;
|
|
}
|
|
if (! is_obscured)
|
|
out.push_back(i);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool MeshRaycaster::closest_hit(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera,
|
|
Vec3f& position, Vec3f& normal, const ClippingPlane* clipping_plane, size_t* facet_idx) const
|
|
{
|
|
Vec3d point;
|
|
Vec3d direction;
|
|
line_from_mouse_pos(mouse_pos, trafo, camera, point, direction);
|
|
|
|
const std::vector<AABBMesh::hit_result> hits = m_emesh.query_ray_hits(point, direction.normalized());
|
|
|
|
if (hits.empty())
|
|
return false; // no intersection found
|
|
|
|
size_t hit_id = 0;
|
|
if (clipping_plane != nullptr) {
|
|
while (hit_id < hits.size() && clipping_plane->is_point_clipped(trafo * hits[hit_id].position())) {
|
|
++hit_id;
|
|
}
|
|
}
|
|
|
|
if (hit_id == hits.size())
|
|
return false; // all points are obscured or cut by the clipping plane.
|
|
|
|
const AABBMesh::hit_result& hit = hits[hit_id];
|
|
|
|
position = hit.position().cast<float>();
|
|
normal = hit.normal().cast<float>();
|
|
|
|
if (facet_idx != nullptr)
|
|
*facet_idx = hit.face();
|
|
|
|
return true;
|
|
}
|
|
|
|
Vec3f MeshRaycaster::get_closest_point(const Vec3f& point, Vec3f* normal) const
|
|
{
|
|
int idx = 0;
|
|
Vec3d closest_point;
|
|
Vec3d pointd = point.cast<double>();
|
|
m_emesh.squared_distance(pointd, idx, closest_point);
|
|
if (normal)
|
|
// TODO: consider: get_normal(m_emesh, pointd).cast<float>();
|
|
*normal = m_normals[idx];
|
|
|
|
return closest_point.cast<float>();
|
|
}
|
|
|
|
int MeshRaycaster::get_closest_facet(const Vec3f &point) const
|
|
{
|
|
int facet_idx = 0;
|
|
Vec3d closest_point;
|
|
m_emesh.squared_distance(point.cast<double>(), facet_idx, closest_point);
|
|
return facet_idx;
|
|
}
|
|
|
|
} // namespace GUI
|
|
} // namespace Slic3r
|