OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::CameraUtils Class Reference

Help divide camera data and camera functions This utility work with camera data by static funtions. More...

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/CameraUtils.hpp>

Public Member Functions

 CameraUtils ()=delete

Static Public Member Functions

static Points project (const Camera &camera, const std::vector< Vec3d > &points)
 Project point throw camera to 2d coordinate into imgui window.
static Slic3r::Point project (const Camera &camera, const Vec3d &point)
static Polygon create_hull2d (const Camera &camera, const GLVolume &volume)
 Create hull around GLVolume in 2d space of camera.
static void ray_from_screen_pos (const Camera &camera, const Vec2d &position, Vec3d &point, Vec3d &direction)
 Create ray(point and direction) for screen coordinate.
static void ray_from_ortho_screen_pos (const Camera &camera, const Vec2d &position, Vec3d &point, Vec3d &direction)
static void ray_from_persp_screen_pos (const Camera &camera, const Vec2d &position, Vec3d &point, Vec3d &direction)
static Vec2d get_z0_position (const Camera &camera, const Vec2d &coor)
 Unproject mouse coordinate to get position in space where z coor is zero Platter surface should be in z == 0.
static Vec3d screen_point (const Camera &camera, const Vec2d &position)
 Create 3d screen point from 2d position.

Detailed Description

Help divide camera data and camera functions This utility work with camera data by static funtions.

Constructor & Destructor Documentation

◆ CameraUtils()

Slic3r::GUI::CameraUtils::CameraUtils ( )
delete

Member Function Documentation

◆ create_hull2d()

Slic3r::Polygon CameraUtils::create_hull2d ( const Camera & camera,
const GLVolume & volume )
static

Create hull around GLVolume in 2d space of camera.

Parameters
cameraProjection params
volumeOutline by 3d object
Returns
Polygon around object
61{
62 std::vector<Vec3d> vertices;
63 const TriangleMesh *hull = volume.convex_hull();
64 if (hull != nullptr) {
65 const indexed_triangle_set &its = hull->its;
66 vertices.reserve(its.vertices.size());
67 // cast vector
68 for (const Vec3f &vertex : its.vertices)
69 vertices.emplace_back(vertex.cast<double>());
70 } else {
71 // Negative volume doesn't have convex hull so use bounding box
72 auto bb = volume.bounding_box();
73 Vec3d &min = bb.min;
74 Vec3d &max = bb.max;
75 vertices = {min,
76 Vec3d(min.x(), min.y(), max.z()),
77 Vec3d(min.x(), max.y(), min.z()),
78 Vec3d(min.x(), max.y(), max.z()),
79 Vec3d(max.x(), min.y(), min.z()),
80 Vec3d(max.x(), min.y(), max.z()),
81 Vec3d(max.x(), max.y(), min.z()),
82 max};
83 }
84
85 const Transform3d &trafoMat =
88 for (Vec3d &vertex : vertices)
89 vertex = trafoMat * vertex.cast<double>();
90
91 Points vertices_2d = project(camera, vertices);
92 return Geometry::convex_hull(vertices_2d);
93}
BoundingBoxf3 bounding_box() const
Definition 3DScene.hpp:267
const TriangleMesh * convex_hull() const
Definition 3DScene.hpp:361
const Geometry::Transformation & get_volume_transformation() const
Definition 3DScene.hpp:306
const Geometry::Transformation & get_instance_transformation() const
Definition 3DScene.hpp:278
static Points project(const Camera &camera, const std::vector< Vec3d > &points)
Project point throw camera to 2d coordinate into imgui window.
Definition CameraUtils.cpp:23
const Transform3d & get_matrix() const
Definition Geometry.hpp:477
indexed_triangle_set its
Definition TriangleMesh.hpp:164
Polygon convex_hull(Points pts)
Definition ConvexHull.cpp:21
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vec3d
Definition Point.hpp:68
Eigen::Transform< double, 3, Eigen::Affine, Eigen::DontAlign > Transform3d
Definition Point.hpp:96
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vec3f
Definition Point.hpp:65
std::vector< Point, PointsAllocator< Point > > Points
Definition Point.hpp:73

References Slic3r::GLVolume::bounding_box(), Slic3r::Geometry::convex_hull(), Slic3r::GLVolume::convex_hull(), Slic3r::GLVolume::get_instance_transformation(), Slic3r::Geometry::Transformation::get_matrix(), Slic3r::GLVolume::get_volume_transformation(), Slic3r::TriangleMesh::its, and project().

Referenced by anonymous_namespace{GLGizmoEmboss.cpp}::calc_fine_position(), and anonymous_namespace{EmbossJob.cpp}::find_closest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_z0_position()

Vec2d CameraUtils::get_z0_position ( const Camera & camera,
const Vec2d & coor )
static

Unproject mouse coordinate to get position in space where z coor is zero Platter surface should be in z == 0.

Parameters
cameraProjection params
coorMouse position
Returns
Position on platter under mouse
130{
131 Vec3d p0, dir;
132 ray_from_screen_pos(camera, coor, p0, dir);
133
134 // is approx zero
135 if ((fabs(dir.z()) - 1e-4) < 0)
136 return Vec2d(std::numeric_limits<double>::max(),
137 std::numeric_limits<double>::max());
138
139 // find position of ray cross plane(z = 0)
140 double t = p0.z() / dir.z();
141 Vec3d p = p0 - t * dir;
142 return Vec2d(p.x(), p.y());
143}
static void ray_from_screen_pos(const Camera &camera, const Vec2d &position, Vec3d &point, Vec3d &direction)
Create ray(point and direction) for screen coordinate.
Definition CameraUtils.cpp:95
Eigen::Matrix< double, 2, 1, Eigen::DontAlign > Vec2d
Definition Point.hpp:67

References ray_from_screen_pos().

Referenced by anonymous_namespace{EmbossJob.cpp}::CreateObjectJob::process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ project() [1/2]

Points CameraUtils::project ( const Camera & camera,
const std::vector< Vec3d > & points )
static

Project point throw camera to 2d coordinate into imgui window.

Parameters
cameraProjection params
pointsPoint to project.
Returns
projected points by camera into coordinate of camera. x(from left to right), y(from top to bottom)
25{
26 Vec4i32 viewport(camera.get_viewport().data());
27
28 // Convert our std::vector to Eigen dynamic matrix.
29 Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::DontAlign>
30 pts(points.size(), 3);
31 for (size_t i = 0; i < points.size(); ++i)
32 pts.block<1, 3>(i, 0) = points[i];
33
34 // Get the projections.
35 Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::DontAlign> projections;
36 igl::project(pts, camera.get_view_matrix().matrix(),
37 camera.get_projection_matrix().matrix(), viewport, projections);
38
39 Points result;
40 result.reserve(points.size());
41 int window_height = viewport[3];
42
43 // convert to points --> loss precision
44 for (int i = 0; i < projections.rows(); ++i) {
45 double x = projections(i, 0);
46 double y = projections(i, 1);
47 // opposit direction o Y
48 result.emplace_back(x, window_height - y);
49 }
50 return result;
51}
Eigen::Matrix< int32_t, 4, 1, Eigen::DontAlign > Vec4i32
Definition Point.hpp:61
const Transform3d & get_projection_matrix() const
Definition Camera.hpp:116
const Transform3d & get_view_matrix() const
Definition Camera.hpp:115
const std::array< int, 4 > & get_viewport() const
Definition Camera.hpp:114

References Slic3r::GUI::Camera::get_projection_matrix(), Slic3r::GUI::Camera::get_view_matrix(), and Slic3r::GUI::Camera::get_viewport().

Referenced by Slic3r::GUI::GLSelectionRectangle::contains(), create_hull2d(), Slic3r::GUI::GLCanvas3D::CenterOfMass::on_left_down(), Slic3r::GUI::GLGizmoTextureDisplacement::on_mouse_adjust_texture(), project(), Slic3r::GUI::GLCanvas3D::CenterOfMass::render_details(), and anonymous_namespace{SurfaceDrag.cpp}::start_dragging().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ project() [2/2]

Slic3r::Point CameraUtils::project ( const Camera & camera,
const Vec3d & point )
static
54{
55 // IMPROVE: do it faster when you need it (inspire in project multi point)
56 return project(camera, std::vector{point}).front();
57}

References project().

Here is the call graph for this function:

◆ ray_from_ortho_screen_pos()

void CameraUtils::ray_from_ortho_screen_pos ( const Camera & camera,
const Vec2d & position,
Vec3d & point,
Vec3d & direction )
static
111{
112 assert(camera.get_type() == Camera::EType::Ortho);
113 Matrix4d modelview = camera.get_view_matrix().matrix();
114 Matrix4d projection = camera.get_projection_matrix().matrix();
115 Vec4i32 viewport(camera.get_viewport().data());
116 igl::unproject(screen_point(camera,position), modelview, projection, viewport, point);
117 direction = camera.get_dir_forward();
118}
static Vec3d screen_point(const Camera &camera, const Vec2d &position)
Create 3d screen point from 2d position.
Definition CameraUtils.cpp:103
Eigen::Matrix< double, 4, 4, Eigen::DontAlign > Matrix4d
Definition Point.hpp:88
Vec3d get_dir_forward() const
Definition Camera.hpp:123
EType get_type() const
Definition Camera.hpp:83
@ Ortho
Definition Camera.hpp:39

References Slic3r::GUI::Camera::get_dir_forward(), Slic3r::GUI::Camera::get_projection_matrix(), Slic3r::GUI::Camera::get_type(), Slic3r::GUI::Camera::get_view_matrix(), Slic3r::GUI::Camera::get_viewport(), Slic3r::GUI::Camera::Ortho, and screen_point().

Referenced by ray_from_screen_pos().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ray_from_persp_screen_pos()

void CameraUtils::ray_from_persp_screen_pos ( const Camera & camera,
const Vec2d & position,
Vec3d & point,
Vec3d & direction )
static
120{
121 assert(camera.get_type() == Camera::EType::Perspective);
122 Matrix4d modelview = camera.get_view_matrix().matrix();
123 Matrix4d projection = camera.get_projection_matrix().matrix();
124 Vec4i32 viewport(camera.get_viewport().data());
125 igl::unproject(screen_point(camera, position), modelview, projection, viewport, point);
126 direction = point - camera.get_position();
127}
@ Perspective
Definition Camera.hpp:40
Vec3d get_position() const
Definition Camera.hpp:126

References Slic3r::GUI::Camera::get_position(), Slic3r::GUI::Camera::get_projection_matrix(), Slic3r::GUI::Camera::get_type(), Slic3r::GUI::Camera::get_view_matrix(), Slic3r::GUI::Camera::get_viewport(), Slic3r::GUI::Camera::Perspective, and screen_point().

Referenced by ray_from_screen_pos().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ray_from_screen_pos()

void CameraUtils::ray_from_screen_pos ( const Camera & camera,
const Vec2d & position,
Vec3d & point,
Vec3d & direction )
static

Create ray(point and direction) for screen coordinate.

Parameters
cameraDefinition of camera
positionPosition on screen(aka mouse position)
pointOUT start of ray
directionOUT direction of ray
95 {
96 switch (camera.get_type()) {
97 case Camera::EType::Ortho: return ray_from_ortho_screen_pos(camera, position, point, direction);
98 case Camera::EType::Perspective: return ray_from_persp_screen_pos(camera, position, point, direction);
99 default: break;
100 }
101}
static void ray_from_persp_screen_pos(const Camera &camera, const Vec2d &position, Vec3d &point, Vec3d &direction)
Definition CameraUtils.cpp:119
static void ray_from_ortho_screen_pos(const Camera &camera, const Vec2d &position, Vec3d &point, Vec3d &direction)
Definition CameraUtils.cpp:110

References Slic3r::GUI::Camera::get_type(), Slic3r::GUI::Camera::Ortho, Slic3r::GUI::Camera::Perspective, ray_from_ortho_screen_pos(), and ray_from_persp_screen_pos().

Referenced by Slic3r::GUI::GLCanvas3D::get_camera_pan_anchor(), get_z0_position(), Slic3r::GUI::MeshRaycaster::line_from_mouse_pos(), Slic3r::GUI::ray_from_camera(), Slic3r::GUI::anonymous_namespace{GLGizmoTextureDisplacement.cpp}::ray_plane_hit(), and Slic3r::GUI::MeshRaycaster::unproject_on_mesh().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ screen_point()

Vec3d CameraUtils::screen_point ( const Camera & camera,
const Vec2d & position )
static

Create 3d screen point from 2d position.

Parameters
cameraDefine camera viewport
positionPosition on screen(aka mouse position)
Returns
Point represented screen coor in 3d
104{
105 double height = camera.get_viewport().data()[3];
106 // Y coordinate has opposit direction
107 return Vec3d(position.x(), height - position.y(), 0.);
108}
coord_t height(const BoundingBox &box)
Definition Arrange.cpp:1185

References Slic3r::GUI::Camera::get_viewport().

Referenced by ray_from_ortho_screen_pos(), and ray_from_persp_screen_pos().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: