ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
GLHelper.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 #include <Eigen/Dense>
11 #include <cmath>
12 
13 namespace cloudViewer {
14 namespace visualization {
15 namespace gl_util {
16 
17 static const std::unordered_map<int, unsigned int> texture_format_map_ = {
18  {1, GL_RED}, {3, GL_RGB}, {4, GL_RGBA}};
19 
20 static const std::unordered_map<int, unsigned int> texture_type_map_ = {
21  {1, GL_UNSIGNED_BYTE}, {2, GL_UNSIGNED_SHORT}, {4, GL_FLOAT}};
22 
23 const std::unordered_map<int, unsigned int> &GetTextureFormatMap() {
24  return texture_format_map_;
25 }
26 
27 const std::unordered_map<int, unsigned int> &GetTextureTypeMap() {
28  return texture_type_map_;
29 }
30 
31 GLMatrix4f LookAt(const Eigen::Vector3d &eye,
32  const Eigen::Vector3d &lookat,
33  const Eigen::Vector3d &up) {
34  Eigen::Vector3d front_dir = (eye - lookat).normalized();
35  Eigen::Vector3d up_dir = up.normalized();
36  Eigen::Vector3d right_dir = up_dir.cross(front_dir).normalized();
37  up_dir = front_dir.cross(right_dir).normalized();
38 
39  Eigen::Matrix4d mat = Eigen::Matrix4d::Zero();
40  mat.block<1, 3>(0, 0) = right_dir.transpose();
41  mat.block<1, 3>(1, 0) = up_dir.transpose();
42  mat.block<1, 3>(2, 0) = front_dir.transpose();
43  mat(0, 3) = -right_dir.dot(eye);
44  mat(1, 3) = -up_dir.dot(eye);
45  mat(2, 3) = -front_dir.dot(eye);
46  mat(3, 3) = 1.0;
47  return mat.cast<GLfloat>();
48 }
49 
50 GLMatrix4f Perspective(double field_of_view_,
51  double aspect,
52  double z_near,
53  double z_far) {
54  Eigen::Matrix4d mat = Eigen::Matrix4d::Zero();
55  double fov_rad = field_of_view_ / 180.0 * M_PI;
56  double tan_half_fov = std::tan(fov_rad / 2.0);
57  mat(0, 0) = 1.0 / aspect / tan_half_fov;
58  mat(1, 1) = 1.0 / tan_half_fov;
59  mat(2, 2) = -(z_far + z_near) / (z_far - z_near);
60  mat(3, 2) = -1.0;
61  mat(2, 3) = -2.0 * z_far * z_near / (z_far - z_near);
62  return mat.cast<GLfloat>();
63 }
64 
65 GLMatrix4f Ortho(double left,
66  double right,
67  double bottom,
68  double top,
69  double z_near,
70  double z_far) {
71  Eigen::Matrix4d mat = Eigen::Matrix4d::Zero();
72  mat(0, 0) = 2.0 / (right - left);
73  mat(1, 1) = 2.0 / (top - bottom);
74  mat(2, 2) = -2.0 / (z_far - z_near);
75  mat(0, 3) = -(right + left) / (right - left);
76  mat(1, 3) = -(top + bottom) / (top - bottom);
77  mat(2, 3) = -(z_far + z_near) / (z_far - z_near);
78  mat(3, 3) = 1.0;
79  return mat.cast<GLfloat>();
80 }
81 
82 Eigen::Vector3d Project(const Eigen::Vector3d &point,
83  const GLMatrix4f &mvp_matrix,
84  const int width,
85  const int height) {
86  Eigen::Vector4d pos = mvp_matrix.cast<double>() *
87  Eigen::Vector4d(point(0), point(1), point(2), 1.0);
88  if (pos(3) == 0.0) {
89  return Eigen::Vector3d::Zero();
90  }
91  pos /= pos(3);
92  return Eigen::Vector3d((pos(0) * 0.5 + 0.5) * (double)width,
93  (pos(1) * 0.5 + 0.5) * (double)height,
94  (1.0 + pos(2)) * 0.5);
95 }
96 
97 Eigen::Vector3d Unproject(const Eigen::Vector3d &screen_point,
98  const GLMatrix4f &mvp_matrix,
99  const int width,
100  const int height) {
101  Eigen::Vector4d point =
102  mvp_matrix.cast<double>().inverse() *
103  Eigen::Vector4d(screen_point(0) / (double)width * 2.0 - 1.0,
104  screen_point(1) / (double)height * 2.0 - 1.0,
105  screen_point(2) * 2.0 - 1.0, 1.0);
106  if (point(3) == 0.0) {
107  return Eigen::Vector3d::Zero();
108  }
109  point /= point(3);
110  return point.block<3, 1>(0, 0);
111 }
112 
114  if (color(0) == 255) {
115  return -1;
116  } else {
117  return ((color(0) * 256 + color(1)) * 256 + color(2)) * 256 + color(3);
118  }
119 }
120 
121 } // namespace gl_util
122 } // namespace visualization
123 } // namespace cloudViewer
constexpr double M_PI
Pi.
Definition: CVConst.h:19
int width
int height
math::float4 color
GLMatrix4f LookAt(const Eigen::Vector3d &eye, const Eigen::Vector3d &lookat, const Eigen::Vector3d &up)
Definition: GLHelper.cpp:31
const std::unordered_map< int, unsigned int > & GetTextureTypeMap()
Definition: GLHelper.cpp:27
GLMatrix4f Perspective(double field_of_view_, double aspect, double z_near, double z_far)
Definition: GLHelper.cpp:50
const std::unordered_map< int, unsigned int > & GetTextureFormatMap()
Definition: GLHelper.cpp:23
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > GLMatrix4f
Definition: GLHelper.h:35
static const std::unordered_map< int, unsigned int > texture_type_map_
Definition: GLHelper.cpp:20
Eigen::Vector3d Unproject(const Eigen::Vector3d &screen_point, const GLMatrix4f &mvp_matrix, const int width, const int height)
Definition: GLHelper.cpp:97
Eigen::Vector3d Project(const Eigen::Vector3d &point, const GLMatrix4f &mvp_matrix, const int width, const int height)
Definition: GLHelper.cpp:82
static const std::unordered_map< int, unsigned int > texture_format_map_
Definition: GLHelper.cpp:17
int ColorCodeToPickIndex(const Eigen::Vector4i &color)
Definition: GLHelper.cpp:113
GLMatrix4f Ortho(double left, double right, double bottom, double top, double z_near, double z_far)
Definition: GLHelper.cpp:65
Generic file read and write utility for python interface.
Eigen::Matrix< Index, 4, 1 > Vector4i
Definition: knncpp.h:31
Definition: lsd.c:149