mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-10-24 17:21:11 -06:00
Merge remote-tracking branch 'remotes/origin/lm_lay_flat_round_merged_facets'
This commit is contained in:
commit
3c0d81e7d9
261 changed files with 104991 additions and 213 deletions
|
|
@ -202,7 +202,9 @@ const float GLVolume::SELECTED_OUTSIDE_COLOR[4] = { 0.19f, 0.58f, 1.0f, 1.0f };
|
|||
GLVolume::GLVolume(float r, float g, float b, float a)
|
||||
: m_angle_z(0.0f)
|
||||
, m_scale_factor(1.0f)
|
||||
, m_dirty(true)
|
||||
, m_transformed_bounding_box_dirty(true)
|
||||
, m_transformed_convex_hull_bounding_box_dirty(true)
|
||||
, m_convex_hull(nullptr)
|
||||
, composite_id(-1)
|
||||
, select_group_id(-1)
|
||||
, drag_group_id(-1)
|
||||
|
|
@ -219,8 +221,6 @@ GLVolume::GLVolume(float r, float g, float b, float a)
|
|||
, tverts_range(0, size_t(-1))
|
||||
, qverts_range(0, size_t(-1))
|
||||
{
|
||||
m_world_mat = std::vector<float>(UNIT_MATRIX, std::end(UNIT_MATRIX));
|
||||
|
||||
color[0] = r;
|
||||
color[1] = g;
|
||||
color[2] = b;
|
||||
|
|
@ -264,45 +264,76 @@ const Pointf3& GLVolume::get_origin() const
|
|||
|
||||
void GLVolume::set_origin(const Pointf3& origin)
|
||||
{
|
||||
m_origin = origin;
|
||||
m_dirty = true;
|
||||
if (m_origin != origin)
|
||||
{
|
||||
m_origin = origin;
|
||||
m_transformed_bounding_box_dirty = true;
|
||||
m_transformed_convex_hull_bounding_box_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void GLVolume::set_angle_z(float angle_z)
|
||||
{
|
||||
m_angle_z = angle_z;
|
||||
m_dirty = true;
|
||||
if (m_angle_z != angle_z)
|
||||
{
|
||||
m_angle_z = angle_z;
|
||||
m_transformed_bounding_box_dirty = true;
|
||||
m_transformed_convex_hull_bounding_box_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void GLVolume::set_scale_factor(float scale_factor)
|
||||
{
|
||||
m_scale_factor = scale_factor;
|
||||
m_dirty = true;
|
||||
if (m_scale_factor != scale_factor)
|
||||
{
|
||||
m_scale_factor = scale_factor;
|
||||
m_transformed_bounding_box_dirty = true;
|
||||
m_transformed_convex_hull_bounding_box_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<float>& GLVolume::world_matrix() const
|
||||
void GLVolume::set_convex_hull(const TriangleMesh& convex_hull)
|
||||
{
|
||||
if (m_dirty)
|
||||
{
|
||||
Eigen::Transform<float, 3, Eigen::Affine> m = Eigen::Transform<float, 3, Eigen::Affine>::Identity();
|
||||
m.translate(Eigen::Vector3f((float)m_origin.x, (float)m_origin.y, (float)m_origin.z));
|
||||
m.rotate(Eigen::AngleAxisf(m_angle_z, Eigen::Vector3f::UnitZ()));
|
||||
m.scale(m_scale_factor);
|
||||
::memcpy((void*)m_world_mat.data(), (const void*)m.data(), 16 * sizeof(float));
|
||||
m_dirty = false;
|
||||
}
|
||||
m_convex_hull = &convex_hull;
|
||||
}
|
||||
|
||||
return m_world_mat;
|
||||
std::vector<float> GLVolume::world_matrix() const
|
||||
{
|
||||
std::vector<float> world_mat(UNIT_MATRIX, std::end(UNIT_MATRIX));
|
||||
Eigen::Transform<float, 3, Eigen::Affine> m = Eigen::Transform<float, 3, Eigen::Affine>::Identity();
|
||||
m.translate(Eigen::Vector3f((float)m_origin.x, (float)m_origin.y, (float)m_origin.z));
|
||||
m.rotate(Eigen::AngleAxisf(m_angle_z, Eigen::Vector3f::UnitZ()));
|
||||
m.scale(m_scale_factor);
|
||||
::memcpy((void*)world_mat.data(), (const void*)m.data(), 16 * sizeof(float));
|
||||
return world_mat;
|
||||
}
|
||||
|
||||
BoundingBoxf3 GLVolume::transformed_bounding_box() const
|
||||
{
|
||||
if (m_dirty)
|
||||
if (m_transformed_bounding_box_dirty)
|
||||
{
|
||||
m_transformed_bounding_box = bounding_box.transformed(world_matrix());
|
||||
m_transformed_bounding_box_dirty = false;
|
||||
}
|
||||
|
||||
return m_transformed_bounding_box;
|
||||
}
|
||||
|
||||
BoundingBoxf3 GLVolume::transformed_convex_hull_bounding_box() const
|
||||
{
|
||||
if (m_transformed_convex_hull_bounding_box_dirty)
|
||||
{
|
||||
if ((m_convex_hull != nullptr) && (m_convex_hull->stl.stats.number_of_facets > 0))
|
||||
m_transformed_convex_hull_bounding_box = m_convex_hull->transformed_bounding_box(world_matrix());
|
||||
else
|
||||
m_transformed_convex_hull_bounding_box = bounding_box.transformed(world_matrix());
|
||||
|
||||
m_transformed_convex_hull_bounding_box_dirty = false;
|
||||
}
|
||||
|
||||
return m_transformed_convex_hull_bounding_box;
|
||||
}
|
||||
|
||||
void GLVolume::set_range(double min_z, double max_z)
|
||||
{
|
||||
this->qverts_range.first = 0;
|
||||
|
|
@ -629,6 +660,7 @@ std::vector<int> GLVolumeCollection::load_object(
|
|||
|
||||
if (!model_volume->modifier)
|
||||
{
|
||||
v.set_convex_hull(model_volume->get_convex_hull());
|
||||
v.layer_height_texture = layer_height_texture;
|
||||
if (extruder_id != -1)
|
||||
v.extruder_id = extruder_id;
|
||||
|
|
@ -801,9 +833,9 @@ bool GLVolumeCollection::check_outside_state(const DynamicPrintConfig* config, M
|
|||
|
||||
for (GLVolume* volume : this->volumes)
|
||||
{
|
||||
if ((volume != nullptr) && !volume->is_modifier)
|
||||
if ((volume != nullptr) && !volume->is_modifier && (!volume->is_wipe_tower || (volume->is_wipe_tower && volume->shader_outside_printer_detection_enabled)))
|
||||
{
|
||||
const BoundingBoxf3& bb = volume->transformed_bounding_box();
|
||||
const BoundingBoxf3& bb = volume->transformed_convex_hull_bounding_box();
|
||||
bool contained = print_volume.contains(bb);
|
||||
all_contained &= contained;
|
||||
|
||||
|
|
|
|||
|
|
@ -260,12 +260,16 @@ private:
|
|||
float m_angle_z;
|
||||
// Scale factor of the volume to be rendered.
|
||||
float m_scale_factor;
|
||||
// World matrix of the volume to be rendered.
|
||||
std::vector<float> m_world_mat;
|
||||
// Bounding box of this volume, in unscaled coordinates.
|
||||
mutable BoundingBoxf3 m_transformed_bounding_box;
|
||||
// Whether or not is needed to recalculate the world matrix.
|
||||
mutable bool m_dirty;
|
||||
// Whether or not is needed to recalculate the transformed bounding box.
|
||||
mutable bool m_transformed_bounding_box_dirty;
|
||||
// Pointer to convex hull of the original mesh, if any.
|
||||
const TriangleMesh* m_convex_hull;
|
||||
// Bounding box of this volume, in unscaled coordinates.
|
||||
mutable BoundingBoxf3 m_transformed_convex_hull_bounding_box;
|
||||
// Whether or not is needed to recalculate the transformed convex hull bounding box.
|
||||
mutable bool m_transformed_convex_hull_bounding_box_dirty;
|
||||
|
||||
public:
|
||||
|
||||
|
|
@ -323,13 +327,15 @@ public:
|
|||
void set_origin(const Pointf3& origin);
|
||||
void set_angle_z(float angle_z);
|
||||
void set_scale_factor(float scale_factor);
|
||||
void set_convex_hull(const TriangleMesh& convex_hull);
|
||||
|
||||
int object_idx() const { return this->composite_id / 1000000; }
|
||||
int volume_idx() const { return (this->composite_id / 1000) % 1000; }
|
||||
int instance_idx() const { return this->composite_id % 1000; }
|
||||
|
||||
const std::vector<float>& world_matrix() const;
|
||||
std::vector<float> world_matrix() const;
|
||||
BoundingBoxf3 transformed_bounding_box() const;
|
||||
BoundingBoxf3 transformed_convex_hull_bounding_box() const;
|
||||
|
||||
bool empty() const { return this->indexed_vertex_array.empty(); }
|
||||
bool indexed() const { return this->indexed_vertex_array.indexed(); }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "GLCanvas3D.hpp"
|
||||
|
||||
#include "../../admesh/stl.h"
|
||||
#include "../../libslic3r/libslic3r.h"
|
||||
#include "../../slic3r/GUI/3DScene.hpp"
|
||||
#include "../../slic3r/GUI/GLShader.hpp"
|
||||
|
|
@ -1154,6 +1155,18 @@ bool GLCanvas3D::Gizmos::init()
|
|||
|
||||
m_gizmos.insert(GizmosMap::value_type(Rotate, gizmo));
|
||||
|
||||
gizmo = new GLGizmoFlatten;
|
||||
if (gizmo == nullptr)
|
||||
return false;
|
||||
|
||||
if (!gizmo->init()) {
|
||||
_reset();
|
||||
return false;
|
||||
}
|
||||
|
||||
m_gizmos.insert(GizmosMap::value_type(Flatten, gizmo));
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -1387,22 +1400,46 @@ void GLCanvas3D::Gizmos::set_angle_z(float angle_z)
|
|||
reinterpret_cast<GLGizmoRotate*>(it->second)->set_angle_z(angle_z);
|
||||
}
|
||||
|
||||
void GLCanvas3D::Gizmos::render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const
|
||||
Pointf3 GLCanvas3D::Gizmos::get_flattening_normal() const
|
||||
{
|
||||
if (!m_enabled)
|
||||
return Pointf3(0.f, 0.f, 0.f);
|
||||
|
||||
GizmosMap::const_iterator it = m_gizmos.find(Flatten);
|
||||
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(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(model_object);
|
||||
}
|
||||
|
||||
void GLCanvas3D::Gizmos::render(const GLCanvas3D& canvas, const BoundingBoxf3& box, RenderOrder render_order) const
|
||||
{
|
||||
if (!m_enabled)
|
||||
return;
|
||||
|
||||
::glDisable(GL_DEPTH_TEST);
|
||||
|
||||
if (box.radius() > 0.0)
|
||||
_render_current_gizmo(box);
|
||||
if ((render_order == BeforeBed && dynamic_cast<GLGizmoFlatten*>(_get_current()))
|
||||
|| (render_order == AfterBed && !dynamic_cast<GLGizmoFlatten*>(_get_current()))) {
|
||||
if (box.radius() > 0.0)
|
||||
_render_current_gizmo(box);
|
||||
}
|
||||
|
||||
::glPushMatrix();
|
||||
::glLoadIdentity();
|
||||
if (render_order == AfterBed) {
|
||||
::glPushMatrix();
|
||||
::glLoadIdentity();
|
||||
|
||||
_render_overlay(canvas);
|
||||
_render_overlay(canvas);
|
||||
|
||||
::glPopMatrix();
|
||||
::glPopMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
void GLCanvas3D::Gizmos::render_current_gizmo_for_picking_pass(const BoundingBoxf3& box) const
|
||||
|
|
@ -2170,6 +2207,7 @@ void GLCanvas3D::update_gizmos_data()
|
|||
{
|
||||
m_gizmos.set_scale(model_instance->scaling_factor);
|
||||
m_gizmos.set_angle_z(model_instance->rotation);
|
||||
m_gizmos.set_flattening_data(model_object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2177,6 +2215,7 @@ void GLCanvas3D::update_gizmos_data()
|
|||
{
|
||||
m_gizmos.set_scale(1.0f);
|
||||
m_gizmos.set_angle_z(0.0f);
|
||||
m_gizmos.set_flattening_data(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2215,6 +2254,7 @@ void GLCanvas3D::render()
|
|||
_render_axes(false);
|
||||
}
|
||||
_render_objects();
|
||||
_render_gizmo(Gizmos::RenderOrder::BeforeBed);
|
||||
// textured bed needs to be rendered after objects
|
||||
if (!is_custom_bed)
|
||||
{
|
||||
|
|
@ -2224,7 +2264,7 @@ void GLCanvas3D::render()
|
|||
_render_cutting_plane();
|
||||
_render_warning_texture();
|
||||
_render_legend_texture();
|
||||
_render_gizmo();
|
||||
_render_gizmo(Gizmos::RenderOrder::AfterBed);
|
||||
_render_layer_editing_overlay();
|
||||
|
||||
m_canvas->SwapBuffers();
|
||||
|
|
@ -2760,6 +2800,16 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
|
|||
m_gizmos.start_dragging();
|
||||
m_mouse.drag.gizmo_volume_idx = _get_first_selected_volume_id(selected_object_idx);
|
||||
m_dirty = true;
|
||||
|
||||
if (m_gizmos.get_current_type() == Gizmos::Flatten) {
|
||||
// Rotate the object so the normal points downward:
|
||||
Pointf3 normal = m_gizmos.get_flattening_normal();
|
||||
if (normal.x != 0.f || normal.y != 0.f || normal.z != 0.f) {
|
||||
Pointf3 axis = normal.z > 0.999f ? Pointf3(1, 0, 0) : cross(normal, Pointf3(0.f, 0.f, -1.f));
|
||||
float angle = -acos(-normal.z);
|
||||
m_on_gizmo_rotate_callback.call(angle, axis.x, axis.y, axis.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -3017,6 +3067,10 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
|
|||
}
|
||||
|
||||
_on_move(volume_idxs);
|
||||
|
||||
// force re-selection of the wipe tower, if needed
|
||||
if ((volume_idxs.size() == 1) && m_volumes.volumes[volume_idxs[0]]->is_wipe_tower)
|
||||
select_volume(volume_idxs[0]);
|
||||
}
|
||||
else if (!m_mouse.dragging && (m_hover_volume_id == -1) && !gizmos_overlay_contains_mouse && !m_gizmos.is_dragging() && !is_layers_editing_enabled())
|
||||
{
|
||||
|
|
@ -3243,7 +3297,7 @@ BoundingBoxf3 GLCanvas3D::_selected_volumes_bounding_box() const
|
|||
{
|
||||
for (const GLVolume* volume : selected_volumes)
|
||||
{
|
||||
bb.merge(volume->transformed_bounding_box());
|
||||
bb.merge(volume->transformed_convex_hull_bounding_box());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -3708,9 +3762,9 @@ void GLCanvas3D::_render_volumes(bool fake_colors) const
|
|||
::glDisable(GL_LIGHTING);
|
||||
}
|
||||
|
||||
void GLCanvas3D::_render_gizmo() const
|
||||
void GLCanvas3D::_render_gizmo(Gizmos::RenderOrder render_order) const
|
||||
{
|
||||
m_gizmos.render(*this, _selected_volumes_bounding_box());
|
||||
m_gizmos.render(*this, _selected_volumes_bounding_box(), render_order);
|
||||
}
|
||||
|
||||
float GLCanvas3D::_get_layers_editing_cursor_z_relative() const
|
||||
|
|
|
|||
|
|
@ -338,8 +338,13 @@ public:
|
|||
Undefined,
|
||||
Scale,
|
||||
Rotate,
|
||||
Flatten,
|
||||
Num_Types
|
||||
};
|
||||
enum RenderOrder : unsigned char {
|
||||
BeforeBed,
|
||||
AfterBed
|
||||
};
|
||||
|
||||
private:
|
||||
bool m_enabled;
|
||||
|
|
@ -382,7 +387,10 @@ public:
|
|||
float get_angle_z() const;
|
||||
void set_angle_z(float angle_z);
|
||||
|
||||
void render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const;
|
||||
void set_flattening_data(const ModelObject* model_object);
|
||||
Pointf3 get_flattening_normal() const;
|
||||
|
||||
void render(const GLCanvas3D& canvas, const BoundingBoxf3& box, RenderOrder render_order) const;
|
||||
void render_current_gizmo_for_picking_pass(const BoundingBoxf3& box) const;
|
||||
|
||||
private:
|
||||
|
|
@ -629,7 +637,7 @@ private:
|
|||
void _render_legend_texture() const;
|
||||
void _render_layer_editing_overlay() const;
|
||||
void _render_volumes(bool fake_colors) const;
|
||||
void _render_gizmo() const;
|
||||
void _render_gizmo(Gizmos::RenderOrder render_order) const;
|
||||
|
||||
float _get_layers_editing_cursor_z_relative() const;
|
||||
void _perform_layer_editing_action(wxMouseEvent* evt = nullptr);
|
||||
|
|
|
|||
|
|
@ -2,10 +2,13 @@
|
|||
|
||||
#include "../../libslic3r/Utils.hpp"
|
||||
#include "../../libslic3r/BoundingBox.hpp"
|
||||
#include "../../libslic3r/Model.hpp"
|
||||
#include "../../libslic3r/Geometry.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
namespace Slic3r {
|
||||
namespace GUI {
|
||||
|
|
@ -110,7 +113,7 @@ int GLGizmoBase::get_hover_id() const
|
|||
|
||||
void GLGizmoBase::set_hover_id(int id)
|
||||
{
|
||||
if (id < (int)m_grabbers.size())
|
||||
//if (id < (int)m_grabbers.size())
|
||||
m_hover_id = id;
|
||||
}
|
||||
|
||||
|
|
@ -189,7 +192,7 @@ GLGizmoRotate::GLGizmoRotate()
|
|||
, m_angle_z(0.0f)
|
||||
, m_center(Pointf(0.0, 0.0))
|
||||
, m_radius(0.0f)
|
||||
, m_keep_radius(false)
|
||||
, m_keep_initial_values(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -229,7 +232,7 @@ bool GLGizmoRotate::on_init()
|
|||
|
||||
void GLGizmoRotate::on_set_state()
|
||||
{
|
||||
m_keep_radius = (m_state == On) ? false : true;
|
||||
m_keep_initial_values = (m_state == On) ? false : true;
|
||||
}
|
||||
|
||||
void GLGizmoRotate::on_update(const Pointf& mouse_pos)
|
||||
|
|
@ -255,19 +258,19 @@ void GLGizmoRotate::on_update(const Pointf& mouse_pos)
|
|||
|
||||
void GLGizmoRotate::on_refresh()
|
||||
{
|
||||
m_keep_radius = false;
|
||||
m_keep_initial_values = false;
|
||||
}
|
||||
|
||||
void GLGizmoRotate::on_render(const BoundingBoxf3& box) const
|
||||
{
|
||||
::glDisable(GL_DEPTH_TEST);
|
||||
|
||||
const Pointf3& size = box.size();
|
||||
m_center = box.center();
|
||||
if (!m_keep_radius)
|
||||
if (!m_keep_initial_values)
|
||||
{
|
||||
const Pointf3& size = box.size();
|
||||
m_center = box.center();
|
||||
m_radius = Offset + ::sqrt(sqr(0.5f * size.x) + sqr(0.5f * size.y));
|
||||
m_keep_radius = true;
|
||||
m_keep_initial_values = true;
|
||||
}
|
||||
|
||||
::glLineWidth(2.0f);
|
||||
|
|
@ -502,5 +505,326 @@ void GLGizmoScale::on_render_for_picking(const BoundingBoxf3& box) const
|
|||
render_grabbers();
|
||||
}
|
||||
|
||||
|
||||
GLGizmoFlatten::GLGizmoFlatten()
|
||||
: GLGizmoBase(),
|
||||
m_normal(Pointf3(0.f, 0.f, 0.f))
|
||||
{}
|
||||
|
||||
|
||||
bool GLGizmoFlatten::on_init()
|
||||
{
|
||||
std::string path = resources_dir() + "/icons/overlay/";
|
||||
|
||||
std::string filename = path + "layflat_off.png";
|
||||
if (!m_textures[Off].load_from_file(filename, false))
|
||||
return false;
|
||||
|
||||
filename = path + "layflat_hover.png";
|
||||
if (!m_textures[Hover].load_from_file(filename, false))
|
||||
return false;
|
||||
|
||||
filename = path + "layflat_on.png";
|
||||
if (!m_textures[On].load_from_file(filename, false))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GLGizmoFlatten::on_start_dragging()
|
||||
{
|
||||
if (m_hover_id != -1)
|
||||
m_normal = m_planes[m_hover_id].normal;
|
||||
}
|
||||
|
||||
void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const
|
||||
{
|
||||
// the dragged_offset is a vector measuring where was the object moved
|
||||
// with the gizmo being on. This is reset in set_flattening_data and
|
||||
// does not work correctly when there are multiple copies.
|
||||
if (!m_center) // this is the first bounding box that we see
|
||||
m_center.reset(new Pointf3(box.center().x, box.center().y));
|
||||
Pointf3 dragged_offset = box.center() - *m_center;
|
||||
|
||||
bool blending_was_enabled = ::glIsEnabled(GL_BLEND);
|
||||
bool depth_test_was_enabled = ::glIsEnabled(GL_DEPTH_TEST);
|
||||
::glEnable(GL_BLEND);
|
||||
::glEnable(GL_DEPTH_TEST);
|
||||
|
||||
for (int i=0; i<(int)m_planes.size(); ++i) {
|
||||
if (i == m_hover_id)
|
||||
::glColor4f(0.9f, 0.9f, 0.9f, 0.75f);
|
||||
else
|
||||
::glColor4f(0.9f, 0.9f, 0.9f, 0.5f);
|
||||
|
||||
for (Pointf offset : m_instances_positions) {
|
||||
offset += dragged_offset;
|
||||
::glBegin(GL_POLYGON);
|
||||
for (const auto& vertex : m_planes[i].vertices)
|
||||
::glVertex3f((GLfloat)vertex.x + offset.x, (GLfloat)vertex.y + offset.y, (GLfloat)vertex.z);
|
||||
::glEnd();
|
||||
}
|
||||
}
|
||||
|
||||
if (!blending_was_enabled)
|
||||
::glDisable(GL_BLEND);
|
||||
if (!depth_test_was_enabled)
|
||||
::glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
|
||||
void GLGizmoFlatten::on_render_for_picking(const BoundingBoxf3& box) const
|
||||
{
|
||||
static const GLfloat INV_255 = 1.0f / 255.0f;
|
||||
|
||||
::glDisable(GL_DEPTH_TEST);
|
||||
|
||||
for (unsigned int i = 0; i < m_planes.size(); ++i)
|
||||
{
|
||||
::glColor3f(1.f, 1.f, (254.0f - (float)i) * INV_255);
|
||||
for (const Pointf& offset : m_instances_positions) {
|
||||
::glBegin(GL_POLYGON);
|
||||
for (const auto& vertex : m_planes[i].vertices)
|
||||
::glVertex3f((GLfloat)vertex.x + offset.x, (GLfloat)vertex.y + offset.y, (GLfloat)vertex.z);
|
||||
::glEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO - remove and use Eigen instead
|
||||
static Pointf3 super_rotation(Pointf3 axis, float angle, const Pointf3& point)
|
||||
{
|
||||
axis = normalize(axis);
|
||||
const float& x = axis.x;
|
||||
const float& y = axis.y;
|
||||
const float& z = axis.z;
|
||||
float s = sin(angle);
|
||||
float c = cos(angle);
|
||||
float D = 1-c;
|
||||
float matrix[3][3] = { { c + x*x*D, x*y*D-z*s, x*z*D+y*s },
|
||||
{ y*x*D+z*s, c+y*y*D, y*z*D-x*s },
|
||||
{ z*x*D-y*s, z*y*D+x*s, c+z*z*D } };
|
||||
float in[3] = { (float)point.x, (float)point.y, (float)point.z };
|
||||
float out[3] = { 0, 0, 0 };
|
||||
|
||||
for (unsigned char i=0; i<3; ++i)
|
||||
for (unsigned char j=0; j<3; ++j)
|
||||
out[i] += matrix[i][j] * in[j];
|
||||
|
||||
return Pointf3(out[0], out[1], out[2]);
|
||||
}
|
||||
|
||||
|
||||
void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object)
|
||||
{
|
||||
m_center.release(); // object is not being dragged (this would not be called otherwise) - we must forget about the bounding box position...
|
||||
m_model_object = model_object;
|
||||
|
||||
// ...and save the updated positions of the object instances:
|
||||
if (m_model_object && !m_model_object->instances.empty()) {
|
||||
m_instances_positions.clear();
|
||||
for (const auto* instance : m_model_object->instances)
|
||||
m_instances_positions.emplace_back(instance->offset);
|
||||
}
|
||||
|
||||
if (is_plane_update_necessary())
|
||||
update_planes();
|
||||
}
|
||||
|
||||
void GLGizmoFlatten::update_planes()
|
||||
{
|
||||
TriangleMesh ch;
|
||||
for (const ModelVolume* vol : m_model_object->volumes)
|
||||
ch.merge(vol->get_convex_hull());
|
||||
ch = ch.convex_hull_3d();
|
||||
ch.scale(m_model_object->instances.front()->scaling_factor);
|
||||
ch.rotate_z(m_model_object->instances.front()->rotation);
|
||||
|
||||
m_planes.clear();
|
||||
|
||||
// Now we'll go through all the facets and append Points of facets sharing the same normal:
|
||||
const int num_of_facets = ch.stl.stats.number_of_facets;
|
||||
std::vector<int> facet_queue(num_of_facets, 0);
|
||||
std::vector<bool> facet_visited(num_of_facets, false);
|
||||
int facet_queue_cnt = 0;
|
||||
const stl_normal* normal_ptr = nullptr;
|
||||
while (1) {
|
||||
// Find next unvisited triangle:
|
||||
int facet_idx = 0;
|
||||
for (; facet_idx < num_of_facets; ++ facet_idx)
|
||||
if (!facet_visited[facet_idx]) {
|
||||
facet_queue[facet_queue_cnt ++] = facet_idx;
|
||||
facet_visited[facet_idx] = true;
|
||||
normal_ptr = &ch.stl.facet_start[facet_idx].normal;
|
||||
m_planes.emplace_back();
|
||||
break;
|
||||
}
|
||||
if (facet_idx == num_of_facets)
|
||||
break; // Everything was visited already
|
||||
|
||||
while (facet_queue_cnt > 0) {
|
||||
int facet_idx = facet_queue[-- facet_queue_cnt];
|
||||
const stl_normal* this_normal_ptr = &ch.stl.facet_start[facet_idx].normal;
|
||||
//if (this_normal_ptr->x == normal_ptr->x && this_normal_ptr->y == normal_ptr->y && this_normal_ptr->z == normal_ptr->z) {
|
||||
if (std::abs(this_normal_ptr->x-normal_ptr->x) < 0.001 && std::abs(this_normal_ptr->y-normal_ptr->y) < 0.001 && std::abs(this_normal_ptr->z-normal_ptr->z) < 0.001) {
|
||||
stl_vertex* first_vertex = ch.stl.facet_start[facet_idx].vertex;
|
||||
for (int j=0; j<3; ++j)
|
||||
m_planes.back().vertices.emplace_back(first_vertex[j].x, first_vertex[j].y, first_vertex[j].z);
|
||||
|
||||
facet_visited[facet_idx] = true;
|
||||
for (int j = 0; j < 3; ++ j) {
|
||||
int neighbor_idx = ch.stl.neighbors_start[facet_idx].neighbor[j];
|
||||
if (! facet_visited[neighbor_idx])
|
||||
facet_queue[facet_queue_cnt ++] = neighbor_idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
m_planes.back().normal = Pointf3(normal_ptr->x, normal_ptr->y, normal_ptr->z);
|
||||
|
||||
// if this is a just a very small triangle, remove it to speed up further calculations (it would be rejected anyway):
|
||||
if (m_planes.back().vertices.size() == 3 &&
|
||||
( m_planes.back().vertices[0].distance_to(m_planes.back().vertices[1]) < 1.f
|
||||
|| m_planes.back().vertices[0].distance_to(m_planes.back().vertices[2]) < 1.f))
|
||||
m_planes.pop_back();
|
||||
}
|
||||
|
||||
// Now we'll go through all the polygons, transform the points into xy plane to process them:
|
||||
for (unsigned int polygon_id=0; polygon_id < m_planes.size(); ++polygon_id) {
|
||||
Pointf3s& polygon = m_planes[polygon_id].vertices;
|
||||
const Pointf3& normal = m_planes[polygon_id].normal;
|
||||
|
||||
// We are going to rotate about z and y to flatten the plane
|
||||
float angle_z = 0.f;
|
||||
float angle_y = 0.f;
|
||||
if (std::abs(normal.y) > 0.001)
|
||||
angle_z = -atan2(normal.y, normal.x); // angle to rotate so that normal ends up in xz-plane
|
||||
if (std::abs(normal.x*cos(angle_z)-normal.y*sin(angle_z)) > 0.001)
|
||||
angle_y = - atan2(normal.x*cos(angle_z)-normal.y*sin(angle_z), normal.z); // angle to rotate to make normal point upwards
|
||||
else {
|
||||
// In case it already was in z-direction, we must ensure it is not the wrong way:
|
||||
angle_y = normal.z > 0.f ? 0 : -M_PI;
|
||||
}
|
||||
|
||||
// Rotate all points to the xy plane:
|
||||
for (auto& vertex : polygon) {
|
||||
vertex = super_rotation(Pointf3(0,0,1), angle_z, vertex);
|
||||
vertex = super_rotation(Pointf3(0,1,0), angle_y, vertex);
|
||||
}
|
||||
polygon = Slic3r::Geometry::convex_hull(polygon); // To remove the inner points
|
||||
|
||||
// We will calculate area of the polygon and discard ones that are too small
|
||||
// The limit is more forgiving in case the normal is in the direction of the coordinate axes
|
||||
const float minimal_area = (std::abs(normal.x) > 0.999f || std::abs(normal.y) > 0.999f || std::abs(normal.z) > 0.999f) ? 1.f : 20.f;
|
||||
float& area = m_planes[polygon_id].area;
|
||||
area = 0.f;
|
||||
for (unsigned int i = 0; i < polygon.size(); i++) // Shoelace formula
|
||||
area += polygon[i].x*polygon[i+1 < polygon.size() ? i+1 : 0 ].y - polygon[i+1 < polygon.size() ? i+1 : 0].x*polygon[i].y;
|
||||
area = std::abs(area/2.f);
|
||||
if (area < minimal_area) {
|
||||
m_planes.erase(m_planes.begin()+(polygon_id--));
|
||||
continue;
|
||||
}
|
||||
|
||||
// We will shrink the polygon a little bit so it does not touch the object edges:
|
||||
Pointf3 centroid = std::accumulate(polygon.begin(), polygon.end(), Pointf3(0.f, 0.f, 0.f));
|
||||
centroid.scale(1.f/polygon.size());
|
||||
for (auto& vertex : polygon)
|
||||
vertex = 0.9f*vertex + 0.1f*centroid;
|
||||
|
||||
// Polygon is now simple and convex, we'll round the corners to make them look nicer.
|
||||
// The algorithm takes a vertex, calculates middles of respective sides and moves the vertex
|
||||
// towards their average (controlled by 'aggressivity'). This is repeated k times.
|
||||
// In next iterations, the neighbours are not always taken at the middle (to increase the
|
||||
// rounding effect at the corners, where we need it most).
|
||||
const unsigned int k = 10; // number of iterations
|
||||
const float aggressivity = 0.2f; // agressivity
|
||||
const unsigned int N = polygon.size();
|
||||
std::vector<std::pair<unsigned int, unsigned int>> neighbours;
|
||||
if (k != 0) {
|
||||
Pointf3s points_out(2*k*N); // vector long enough to store the future vertices
|
||||
for (unsigned int j=0; j<N; ++j) {
|
||||
points_out[j*2*k] = polygon[j];
|
||||
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_out[j*2*k+k] = 0.5f * (points_out[j*2*k] + points_out[j==N-1 ? 0 : (j+1)*2*k]);
|
||||
else {
|
||||
float r = 0.2+0.3/(k-1)*i; // the neighbours are not always taken in the middle
|
||||
points_out[neighbours[j].first] = r*points_out[j*2*k] + (1-r) * points_out[neighbours[j].first-1];
|
||||
points_out[neighbours[j].second] = r*points_out[j*2*k] + (1-r) * points_out[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_out[2*k*j] = (1-aggressivity) * points_out[2*k*j] +
|
||||
aggressivity*0.5f*(points_out[neighbours[j].first] + points_out[neighbours[j].second]);
|
||||
|
||||
for (auto& n : neighbours) {
|
||||
++n.first;
|
||||
--n.second;
|
||||
}
|
||||
}
|
||||
polygon = points_out; // replace the coarse polygon with the smooth one that we just created
|
||||
}
|
||||
|
||||
// Transform back to 3D;
|
||||
for (auto& b : polygon) {
|
||||
b.z += 0.1f; // raise a bit above the object surface to avoid flickering
|
||||
b = super_rotation(Pointf3(0,1,0), -angle_y, b);
|
||||
b = super_rotation(Pointf3(0,0,1), -angle_z, b);
|
||||
}
|
||||
}
|
||||
|
||||
// We'll sort the planes by area and only keep the 255 largest ones (because of the picking pass limitations):
|
||||
std::sort(m_planes.rbegin(), m_planes.rend(), [](const PlaneData& a, const PlaneData& b) { return a.area < b.area; });
|
||||
m_planes.resize(std::min((int)m_planes.size(), 255));
|
||||
|
||||
// Planes are finished - let's save what we calculated it from:
|
||||
m_source_data.bounding_boxes.clear();
|
||||
for (const auto& vol : m_model_object->volumes)
|
||||
m_source_data.bounding_boxes.push_back(vol->get_convex_hull().bounding_box());
|
||||
m_source_data.scaling_factor = m_model_object->instances.front()->scaling_factor;
|
||||
m_source_data.rotation = m_model_object->instances.front()->rotation;
|
||||
const float* first_vertex = m_model_object->volumes.front()->get_convex_hull().first_vertex();
|
||||
m_source_data.mesh_first_point = Pointf3(first_vertex[0], first_vertex[1], first_vertex[2]);
|
||||
}
|
||||
|
||||
// Check if the bounding boxes of each volume's convex hull is the same as before
|
||||
// and that scaling and rotation has not changed. In that case we don't have to recalculate it.
|
||||
bool GLGizmoFlatten::is_plane_update_necessary() const
|
||||
{
|
||||
if (m_state != On || !m_model_object || m_model_object->instances.empty())
|
||||
return false;
|
||||
|
||||
if (m_model_object->volumes.size() != m_source_data.bounding_boxes.size()
|
||||
|| m_model_object->instances.front()->scaling_factor != m_source_data.scaling_factor
|
||||
|| m_model_object->instances.front()->rotation != m_source_data.rotation)
|
||||
return true;
|
||||
|
||||
// now compare the bounding boxes:
|
||||
for (unsigned int i=0; i<m_model_object->volumes.size(); ++i)
|
||||
if (m_model_object->volumes[i]->get_convex_hull().bounding_box() != m_source_data.bounding_boxes[i])
|
||||
return true;
|
||||
|
||||
const float* first_vertex = m_model_object->volumes.front()->get_convex_hull().first_vertex();
|
||||
Pointf3 first_point(first_vertex[0], first_vertex[1], first_vertex[2]);
|
||||
if (first_point != m_source_data.mesh_first_point)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Pointf3 GLGizmoFlatten::get_flattening_normal() const {
|
||||
Pointf3 normal = m_normal;
|
||||
normal.rotate(-m_model_object->instances.front()->rotation);
|
||||
m_normal = Pointf3(0.f, 0.f, 0.f);
|
||||
return normal;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace GUI
|
||||
} // namespace Slic3r
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ namespace Slic3r {
|
|||
|
||||
class BoundingBoxf3;
|
||||
class Pointf3;
|
||||
class ModelObject;
|
||||
|
||||
namespace GUI {
|
||||
|
||||
|
|
@ -101,7 +102,7 @@ class GLGizmoRotate : public GLGizmoBase
|
|||
|
||||
mutable Pointf m_center;
|
||||
mutable float m_radius;
|
||||
mutable bool m_keep_radius;
|
||||
mutable bool m_keep_initial_values;
|
||||
|
||||
public:
|
||||
GLGizmoRotate();
|
||||
|
|
@ -149,6 +150,56 @@ protected:
|
|||
virtual void on_render_for_picking(const BoundingBoxf3& box) const;
|
||||
};
|
||||
|
||||
|
||||
class GLGizmoFlatten : public GLGizmoBase
|
||||
{
|
||||
// This gizmo does not use grabbers. The m_hover_id relates to polygon managed by the class itself.
|
||||
|
||||
private:
|
||||
mutable Pointf3 m_normal;
|
||||
|
||||
struct PlaneData {
|
||||
std::vector<Pointf3> vertices;
|
||||
Pointf3 normal;
|
||||
float area;
|
||||
};
|
||||
struct SourceDataSummary {
|
||||
std::vector<BoundingBoxf3> bounding_boxes; // bounding boxes of convex hulls of individual volumes
|
||||
float scaling_factor;
|
||||
float rotation;
|
||||
Pointf3 mesh_first_point;
|
||||
};
|
||||
|
||||
// This holds information to decide whether recalculation is necessary:
|
||||
SourceDataSummary m_source_data;
|
||||
|
||||
std::vector<PlaneData> m_planes;
|
||||
std::vector<Pointf> m_instances_positions;
|
||||
mutable std::unique_ptr<Pointf3> m_center = nullptr;
|
||||
const ModelObject* m_model_object = nullptr;
|
||||
void update_planes();
|
||||
bool is_plane_update_necessary() const;
|
||||
|
||||
public:
|
||||
GLGizmoFlatten();
|
||||
|
||||
void set_flattening_data(const ModelObject* model_object);
|
||||
Pointf3 get_flattening_normal() const;
|
||||
|
||||
protected:
|
||||
bool on_init() override;
|
||||
void on_start_dragging() override;
|
||||
void on_update(const Pointf& mouse_pos) override {};
|
||||
void on_render(const BoundingBoxf3& box) const override;
|
||||
void on_render_for_picking(const BoundingBoxf3& box) const override;
|
||||
void on_set_state() override {
|
||||
if (m_state == On && is_plane_update_necessary())
|
||||
update_planes();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace GUI
|
||||
} // namespace Slic3r
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue