From 43230b2709b105ac97a358a2a3e7ca3f45bd2c69 Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Thu, 4 Nov 2021 13:16:56 +0100 Subject: [PATCH] Move projection into separate utils to not slow down translation of files using Camera.hpp --- src/slic3r/CMakeLists.txt | 2 ++ src/slic3r/GUI/Camera.cpp | 29 -------------------- src/slic3r/GUI/Camera.hpp | 2 -- src/slic3r/GUI/CameraUtils.cpp | 35 +++++++++++++++++++++++++ src/slic3r/GUI/CameraUtils.hpp | 30 +++++++++++++++++++++ src/slic3r/GUI/GLSelectionRectangle.cpp | 4 ++- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 5 ++-- 7 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 src/slic3r/GUI/CameraUtils.cpp create mode 100644 src/slic3r/GUI/CameraUtils.hpp diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index 401f5ba5c..e68de0443 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -133,6 +133,8 @@ set(SLIC3R_GUI_SOURCES GUI/3DBed.hpp GUI/Camera.cpp GUI/Camera.hpp + GUI/CameraUtils.cpp + GUI/CameraUtils.hpp GUI/wxExtensions.cpp GUI/wxExtensions.hpp GUI/ExtruderSequenceDialog.cpp diff --git a/src/slic3r/GUI/Camera.cpp b/src/slic3r/GUI/Camera.cpp index b7aaf1999..44b0cd84a 100644 --- a/src/slic3r/GUI/Camera.cpp +++ b/src/slic3r/GUI/Camera.cpp @@ -9,7 +9,6 @@ #endif // ENABLE_CAMERA_STATISTICS #include -#include // projecting points namespace Slic3r { namespace GUI { @@ -493,34 +492,6 @@ void Camera::look_at(const Vec3d& position, const Vec3d& target, const Vec3d& up update_zenit(); } -Points Camera::project(const std::vector &points) const -{ - Vec4i viewport(m_viewport.data()); - - // Convert our std::vector to Eigen dynamic matrix. - Eigen::Matrix - pts(points.size(), 3); - for (size_t i = 0; i < points.size(); ++i) - pts.block<1, 3>(i, 0) = points[i]; - - // Get the projections. - Eigen::Matrix projections; - igl::project(pts, m_view_matrix.matrix(), m_projection_matrix.matrix(), viewport, projections); - - Points result; - result.reserve(points.size()); - int window_height = viewport[3]; - - // Iterate over all points and determine whether they're in the rectangle. - for (int i = 0; i < projections.rows(); ++i) { - double x = projections(i, 0); - double y = projections(i, 1); - // opposit direction o Y - result.emplace_back(x, window_height - y); - } - return result; -} - void Camera::set_default_orientation() { m_zenit = 45.0f; diff --git a/src/slic3r/GUI/Camera.hpp b/src/slic3r/GUI/Camera.hpp index c6727ba54..a61eb44ec 100644 --- a/src/slic3r/GUI/Camera.hpp +++ b/src/slic3r/GUI/Camera.hpp @@ -129,8 +129,6 @@ public: double max_zoom() const { return 250.0; } double min_zoom() const { return 0.7 * calc_zoom_to_bounding_box_factor(m_scene_box); } - // project point throw camera to 2d coordinate - Points project(const std::vector &points) const; private: // returns tight values for nearZ and farZ plane around the given bounding box // the camera MUST be outside of the bounding box in eye coordinate of the given box diff --git a/src/slic3r/GUI/CameraUtils.cpp b/src/slic3r/GUI/CameraUtils.cpp new file mode 100644 index 000000000..e9ecd5acf --- /dev/null +++ b/src/slic3r/GUI/CameraUtils.cpp @@ -0,0 +1,35 @@ +#include "CameraUtils.hpp" +#include // projecting points + +using namespace Slic3r; +using namespace GUI; + +Points CameraUtils::project(const Camera & camera, + const std::vector &points) +{ + Vec4i viewport(camera.get_viewport().data()); + + // Convert our std::vector to Eigen dynamic matrix. + Eigen::Matrix + pts(points.size(), 3); + for (size_t i = 0; i < points.size(); ++i) + pts.block<1, 3>(i, 0) = points[i]; + + // Get the projections. + Eigen::Matrix projections; + igl::project(pts, camera.get_view_matrix().matrix(), + camera.get_projection_matrix().matrix(), viewport, projections); + + Points result; + result.reserve(points.size()); + int window_height = viewport[3]; + + // Iterate over all points and determine whether they're in the rectangle. + for (int i = 0; i < projections.rows(); ++i) { + double x = projections(i, 0); + double y = projections(i, 1); + // opposit direction o Y + result.emplace_back(x, window_height - y); + } + return result; +} diff --git a/src/slic3r/GUI/CameraUtils.hpp b/src/slic3r/GUI/CameraUtils.hpp new file mode 100644 index 000000000..3203991a2 --- /dev/null +++ b/src/slic3r/GUI/CameraUtils.hpp @@ -0,0 +1,30 @@ +#ifndef slic3r_CameraUtils_hpp_ +#define slic3r_CameraUtils_hpp_ + +#include "Camera.hpp" +#include "libslic3r/Point.hpp" + +namespace Slic3r::GUI { + +/// +/// Help divide camera data and camera functions +/// This utility work with camera data by static funtions +/// +class CameraUtils +{ +public: + CameraUtils() = delete; // only static functions + + /// + /// project point throw camera to 2d coordinate into imgui window + /// + /// IN/OUT triangle mesh to be simplified. + /// IN/OUT triangle mesh to be simplified. + /// projected points by camera into coordinate of camera. + /// x(from left to right), y(from top to bottom) + static Points project(const Camera& camera, const std::vector &points); + +}; +} // Slic3r::GUI + +#endif /* slic3r_CameraUtils_hpp_ */ diff --git a/src/slic3r/GUI/GLSelectionRectangle.cpp b/src/slic3r/GUI/GLSelectionRectangle.cpp index a9a12d4b3..1fe8e0d70 100644 --- a/src/slic3r/GUI/GLSelectionRectangle.cpp +++ b/src/slic3r/GUI/GLSelectionRectangle.cpp @@ -1,5 +1,6 @@ #include "GLSelectionRectangle.hpp" #include "Camera.hpp" +#include "CameraUtils.hpp" #include "3DScene.hpp" #include "GLCanvas3D.hpp" #include "GUI_App.hpp" @@ -42,7 +43,8 @@ namespace GUI { BoundingBox rectangle(Points{ Point(m_start_corner.cast()), Point(m_end_corner.cast()) }); // Iterate over all points and determine whether they're in the rectangle. - Points points_2d = wxGetApp().plater()->get_camera().project(points); + const Camera &camera = wxGetApp().plater()->get_camera(); + Points points_2d = CameraUtils::project(camera, points); for (int i = 0; i()); - const Camera camera = wxGetApp().plater()->get_camera(); - Points vertices_2d = camera.project(vertices); + const Camera& camera = wxGetApp().plater()->get_camera(); + Points vertices_2d = CameraUtils::project(camera, vertices); Slic3r::Polygon chull = Geometry::convex_hull(vertices_2d); draw(chull, ImGui::GetColorU32(ImVec4(0.7f, 0.1f, 0.2f, 0.75f)), 3.f); }