ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CameraSphereInteractorLogic.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 namespace cloudViewer {
11 namespace visualization {
12 namespace rendering {
13 
15  double min_far_plane)
16  : CameraInteractorLogic(c, min_far_plane) {}
17 
19  float phi = phi_at_mousedown_ - float(M_PI) * dx / float(view_width_);
20  float theta = theta_at_mousedown_ + float(M_PI) * dy / float(view_height_);
21 
22  float dist = r_at_mousedown_;
23  float sin_theta = std::sin(theta);
24  float cos_theta = std::cos(theta);
25  float sin_phi = std::sin(phi);
26  float cos_phi = std::cos(phi);
27  Eigen::Vector3f eye(center_of_rotation_.x() + dist * cos_phi * cos_theta,
28  center_of_rotation_.y() + dist * sin_phi * cos_theta,
29  center_of_rotation_.z() + dist * sin_theta);
30  Eigen::Vector3f up(-cos_phi * sin_theta, -sin_phi * sin_theta, cos_theta);
31  Eigen::Vector3f forward = (center_of_rotation_ - eye).normalized();
32  Eigen::Vector3f left = up.cross(forward);
33  Camera::Transform::MatrixType mm;
34  mm << -left.x(), up.x(), -forward.x(), eye.x(), -left.y(), up.y(),
35  -forward.y(), eye.y(), -left.z(), up.z(), -forward.z(), eye.z(),
36  0.0f, 0.0f, 0.0f, 1.0f;
38  SetMatrix(m);
40 }
41 
44  auto m = camera_->GetModelMatrix();
45  Eigen::Vector3f eye(m(0, 3), m(1, 3), m(2, 3));
46  Eigen::Vector3f up(m(0, 1), m(1, 1), m(2, 1));
47  r_at_mousedown_ = (eye - center_of_rotation_).norm();
48  eye = (eye - center_of_rotation_) / r_at_mousedown_;
49  // Clamp coords to [-1, 1], since floating point error can result it being
50  // a little outside those bounds.
51  float eye_x = std::min(1.0f, std::max(-1.0f, eye.x()));
52  float eye_y = std::min(1.0f, std::max(-1.0f, eye.y()));
53  float eye_z = std::min(1.0f, std::max(-1.0f, eye.z()));
54  float up_z = std::min(1.0f, std::max(-1.0f, up.z()));
55  theta_at_mousedown_ = std::asin(eye_z);
56  if (std::abs(eye_y) < 0.001 && std::abs(eye_x) < 0.001) {
57  float up_x = std::min(1.0f, std::max(-1.0f, up.x()));
58  float up_y = std::min(1.0f, std::max(-1.0f, up.y()));
59  phi_at_mousedown_ = std::atan2(-up_y, -up_x);
60  } else {
61  phi_at_mousedown_ = std::atan2(eye_y, eye_x);
62  }
63  if (up_z < 0.0f) {
64  if (theta_at_mousedown_ > 0) {
65  theta_at_mousedown_ = M_PI - theta_at_mousedown_;
66  } else {
67  theta_at_mousedown_ = -M_PI - theta_at_mousedown_;
68  }
69  phi_at_mousedown_ += float(M_PI);
70  }
71 }
72 
73 } // namespace rendering
74 } // namespace visualization
75 } // namespace cloudViewer
constexpr double M_PI
Pi.
Definition: CVConst.h:19
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: Camera.h:29
virtual void SetModelMatrix(const Transform &view)=0
virtual Transform GetModelMatrix() const =0
int min(int a, int b)
Definition: cutil_math.h:53
__host__ __device__ int2 abs(int2 v)
Definition: cutil_math.h:1267
int max(int a, int b)
Definition: cutil_math.h:48
static double dist(double x1, double y1, double x2, double y2)
Definition: lsd.c:207
Generic file read and write utility for python interface.