1st attempt of perl callback from c++ for 3DScene

This commit is contained in:
Enrico Turri 2018-05-15 09:50:01 +02:00
parent 986630c2dc
commit f4303ebdb8
11 changed files with 181 additions and 10 deletions

View File

@ -926,13 +926,15 @@ sub zoom_to_bed {
}
}
sub zoom_to_volume {
my ($self, $volume_idx) = @_;
my $volume = $self->volumes->[$volume_idx];
my $bb = $volume->transformed_bounding_box;
$self->zoom_to_bounding_box($bb);
}
#==============================================================================================================================
#sub zoom_to_volume {
# my ($self, $volume_idx) = @_;
#
# my $volume = $self->volumes->[$volume_idx];
# my $bb = $volume->transformed_bounding_box;
# $self->zoom_to_bounding_box($bb);
#}
#==============================================================================================================================
sub zoom_to_volumes {
my ($self) = @_;

View File

@ -141,6 +141,9 @@ sub new {
$self->{canvas3D}->on_viewport_changed(sub {
$self->{preview3D}->canvas->set_viewport_from_scene($self->{canvas3D});
});
#==============================================================================================================================
Slic3r::GUI::_3DScene::register_on_viewport_changed_callback($self->{canvas3D}, sub { $self->{preview3D}->canvas->set_viewport_from_scene($self->{canvas3D}); });
#==============================================================================================================================
}
# Initialize 2D preview canvas
@ -157,6 +160,9 @@ sub new {
$self->{preview3D}->canvas->on_viewport_changed(sub {
$self->{canvas3D}->set_viewport_from_scene($self->{preview3D}->canvas);
});
#==============================================================================================================================
Slic3r::GUI::_3DScene::register_on_viewport_changed_callback($self->{preview3D}->canvas, sub { $self->{canvas3D}->set_viewport_from_scene($self->{preview3D}->canvas); });
#==============================================================================================================================
$self->{preview_notebook}->AddPage($self->{preview3D}, L('Preview'));
$self->{preview3D_page_idx} = $self->{preview_notebook}->GetPageCount-1;
}

View File

@ -222,6 +222,16 @@ BoundingBox3Base<PointClass>::center() const
}
template Pointf3 BoundingBox3Base<Pointf3>::center() const;
//######################################################################################################################################33
template <class PointClass> coordf_t
BoundingBox3Base<PointClass>::max_size() const
{
PointClass s = size();
return std::max(s.x, std::max(s.y, s.z));
}
template coordf_t BoundingBox3Base<Pointf3>::max_size() const;
//######################################################################################################################################33
// Align a coordinate to a grid. The coordinate may be negative,
// the aligned value will never be bigger than the original one.
static inline coord_t _align_to_grid(const coord_t coord, const coord_t spacing) {

View File

@ -94,6 +94,9 @@ public:
void translate(const Pointf3 &pos) { this->translate(pos.x, pos.y, pos.z); }
void offset(coordf_t delta);
PointClass center() const;
//######################################################################################################################################33
coordf_t max_size() const;
//######################################################################################################################################33
bool contains(const PointClass &point) const {
return BoundingBoxBase<PointClass>::contains(point) && point.z >= this->min.z && point.z <= this->max.z;

View File

@ -1847,6 +1847,11 @@ void _3DScene::set_camera_target(wxGLCanvas* canvas, const Pointf3* target)
s_canvas_mgr.set_camera_target(canvas, target);
}
void _3DScene::register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback)
{
s_canvas_mgr.register_on_viewport_changed_callback(canvas, callback);
}
//void _3DScene::_glew_init()
//{
// glewInit();

View File

@ -575,6 +575,8 @@ public:
static Pointf3 get_camera_target(wxGLCanvas* canvas);
static void set_camera_target(wxGLCanvas* canvas, const Pointf3* target);
static void register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback);
// static void _glew_init();
//##################################################################################################################

View File

@ -6,6 +6,7 @@
#include <iostream>
static const bool TURNTABLE_MODE = true;
static const float GIMBALL_LOCK_THETA_MAX = 180.0f;
namespace Slic3r {
@ -136,6 +137,11 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context)
{
}
GLCanvas3D::~GLCanvas3D()
{
_deregister_callbacks();
}
void GLCanvas3D::set_current()
{
if ((m_canvas != nullptr) && (m_context != nullptr))
@ -175,8 +181,7 @@ void GLCanvas3D::resize(unsigned int w, unsigned int h)
}
// FIXME: calculate a tighter value for depth will improve z-fighting
Pointf3 bb_size = bbox.size();
float depth = 5.0f * (float)std::max(bb_size.x, std::max(bb_size.y, bb_size.z));
float depth = 5.0f * (float)bbox.max_size();
::glOrtho(-w2, w2, -h2, h2, -depth, depth);
break;
@ -331,6 +336,12 @@ BoundingBoxf3 GLCanvas3D::max_bounding_box() const
return bb;
}
void GLCanvas3D::register_on_viewport_changed_callback(void* callback)
{
if (callback != nullptr)
m_on_viewport_changed_callback.register_callback(callback);
}
void GLCanvas3D::on_size(wxSizeEvent& evt)
{
set_dirty(true);
@ -363,7 +374,24 @@ void GLCanvas3D::_zoom_to_volumes()
void GLCanvas3D::_zoom_to_bounding_box(const BoundingBoxf3& bbox)
{
// >>>>>>>>>>>>>>>>>>>> TODO <<<<<<<<<<<<<<<<<<<<<<<<
// Calculate the zoom factor needed to adjust viewport to bounding box.
float zoom = _get_zoom_to_bounding_box_factor(bbox);
if (zoom > 0.0f)
{
set_camera_zoom(zoom);
// center view around bounding box center
set_camera_target(bbox.center());
m_on_viewport_changed_callback.call();
if (is_shown_on_screen())
{
std::pair<int, int> size = _get_canvas_size();
resize((unsigned int)size.first, (unsigned int)size.second);
if (m_canvas != nullptr)
m_canvas->Refresh();
}
}
}
std::pair<int, int> GLCanvas3D::_get_canvas_size() const
@ -376,5 +404,96 @@ std::pair<int, int> GLCanvas3D::_get_canvas_size() const
return ret;
}
float GLCanvas3D::_get_zoom_to_bounding_box_factor(const BoundingBoxf3& bbox) const
{
float max_bb_size = bbox.max_size();
if (max_bb_size == 0.0f)
return -1.0f;
// project the bbox vertices on a plane perpendicular to the camera forward axis
// then calculates the vertices coordinate on this plane along the camera xy axes
// we need the view matrix, we let opengl calculate it(same as done in render sub)
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
if (TURNTABLE_MODE)
{
// Turntable mode is enabled by default.
::glRotatef(-get_camera_theta(), 1.0f, 0.0f, 0.0f); // pitch
::glRotatef(get_camera_phi(), 0.0f, 0.0f, 1.0f); // yaw
}
else
{
// Shift the perspective camera.
Pointf3 camera_pos(0.0, 0.0, -(coordf_t)get_camera_distance());
::glTranslatef((float)camera_pos.x, (float)camera_pos.y, (float)camera_pos.z);
// my @rotmat = quat_to_rotmatrix($self->quat); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< TEMPORARY COMMENTED OUT
// glMultMatrixd_p(@rotmat[0..15]); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< TEMPORARY COMMENTED OUT
}
const Pointf3& target = get_camera_target();
::glTranslatef(-(float)target.x, -(float)target.y, -(float)target.z);
// get the view matrix back from opengl
GLfloat matrix[16];
::glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
// camera axes
Pointf3 right((coordf_t)matrix[0], (coordf_t)matrix[4], (coordf_t)matrix[8]);
Pointf3 up((coordf_t)matrix[1], (coordf_t)matrix[5], (coordf_t)matrix[9]);
Pointf3 forward((coordf_t)matrix[2], (coordf_t)matrix[6], (coordf_t)matrix[10]);
Pointf3 bb_min = bbox.min;
Pointf3 bb_max = bbox.max;
Pointf3 bb_center = bbox.center();
// bbox vertices in world space
std::vector<Pointf3> vertices;
vertices.reserve(8);
vertices.push_back(bb_min);
vertices.emplace_back(bb_max.x, bb_min.y, bb_min.z);
vertices.emplace_back(bb_max.x, bb_max.y, bb_min.z);
vertices.emplace_back(bb_min.x, bb_max.y, bb_min.z);
vertices.emplace_back(bb_min.x, bb_min.y, bb_max.z);
vertices.emplace_back(bb_max.x, bb_min.y, bb_max.z);
vertices.push_back(bb_max);
vertices.emplace_back(bb_min.x, bb_max.y, bb_max.z);
coordf_t max_x = 0.0;
coordf_t max_y = 0.0;
// margin factor to give some empty space around the bbox
coordf_t margin_factor = 1.25;
for (const Pointf3 v : vertices)
{
// project vertex on the plane perpendicular to camera forward axis
Pointf3 pos(v.x - bb_center.x, v.y - bb_center.y, v.z - bb_center.z);
Pointf3 proj_on_plane = pos - dot(pos, forward) * forward;
// calculates vertex coordinate along camera xy axes
coordf_t x_on_plane = dot(proj_on_plane, right);
coordf_t y_on_plane = dot(proj_on_plane, up);
max_x = std::max(max_x, margin_factor * std::abs(x_on_plane));
max_y = std::max(max_y, margin_factor * std::abs(y_on_plane));
}
if ((max_x == 0.0) || (max_y == 0.0))
return -1.0f;
max_x *= 2.0;
max_y *= 2.0;
std::pair<int, int> cvs_size = _get_canvas_size();
return (float)std::min((coordf_t)cvs_size.first / max_x, (coordf_t)cvs_size.second / max_y);
}
void GLCanvas3D::_deregister_callbacks()
{
m_on_viewport_changed_callback.deregister_callback();
}
} // namespace GUI
} // namespace Slic3r

View File

@ -2,6 +2,7 @@
#define slic3r_GLCanvas3D_hpp_
#include "../../libslic3r/BoundingBox.hpp"
#include "../../libslic3r/Utils.hpp"
class wxGLCanvas;
class wxGLContext;
@ -85,8 +86,11 @@ private:
bool m_dirty;
bool m_apply_zoom_to_volumes_filter;
PerlCallback m_on_viewport_changed_callback;
public:
GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context);
~GLCanvas3D();
void set_current();
@ -125,6 +129,8 @@ public:
BoundingBoxf3 volumes_bounding_box() const;
BoundingBoxf3 max_bounding_box() const;
void register_on_viewport_changed_callback(void* callback);
void on_size(wxSizeEvent& evt);
void on_idle(wxIdleEvent& evt);
@ -133,6 +139,9 @@ private:
void _zoom_to_volumes();
void _zoom_to_bounding_box(const BoundingBoxf3& bbox);
std::pair<int, int> _get_canvas_size() const;
float _get_zoom_to_bounding_box_factor(const BoundingBoxf3& bbox) const;
void _deregister_callbacks();
};
} // namespace GUI

View File

@ -289,6 +289,13 @@ void GLCanvas3DManager::set_camera_target(wxGLCanvas* canvas, const Pointf3* tar
it->second->set_camera_target(*target);
}
void GLCanvas3DManager::register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback)
{
CanvasesMap::iterator it = _get_canvas(canvas);
if (it != m_canvases.end())
it->second->register_on_viewport_changed_callback(callback);
}
GLCanvas3DManager::CanvasesMap::iterator GLCanvas3DManager::_get_canvas(wxGLCanvas* canvas)
{
return (canvas == nullptr) ? m_canvases.end() : m_canvases.find(canvas);

View File

@ -85,6 +85,8 @@ public:
Pointf3 get_camera_target(wxGLCanvas* canvas) const;
void set_camera_target(wxGLCanvas* canvas, const Pointf3* target);
void register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback);
private:
CanvasesMap::iterator _get_canvas(wxGLCanvas* canvas);
CanvasesMap::const_iterator _get_canvas(wxGLCanvas* canvas) const;

View File

@ -344,6 +344,12 @@ set_camera_target(canvas, target)
CODE:
_3DScene::set_camera_target((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), target);
void
register_on_viewport_changed_callback(canvas, callback)
SV *canvas;
SV *callback;
CODE:
_3DScene::register_on_viewport_changed_callback((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), (void*)callback);