15 #if defined(__APPLE__) && defined(BUILD_GUI)
24 namespace visualization {
32 #if defined(__APPLE__)
36 glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
38 init_status_ = glfwInit();
39 if (init_status_ != GLFW_TRUE) {
40 glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_NULL);
41 init_status_ = glfwInit();
43 if (init_status_ == GLFW_TRUE) init_status_ = glfwGetPlatform();
44 if (init_status_ == GLFW_PLATFORM_NULL) {
54 if (init_status_ != GLFW_FALSE) {
56 init_status_ = GLFW_FALSE;
66 static std::weak_ptr<GLFWContext> singleton;
68 auto res = singleton.lock();
70 res = std::shared_ptr<GLFWContext>(
new GLFWContext());
82 int init_status_ = GLFW_FALSE;
90 #if defined(__APPLE__) && defined(BUILD_GUI)
96 const std::string &window_name ,
101 const bool visible ) {
106 glfwSetWindowPos(
window_, left, top);
110 std::round(
width * pixel_to_screen_coordinate_),
111 std::round(
height * pixel_to_screen_coordinate_));
113 std::round(left * pixel_to_screen_coordinate_),
114 std::round(top * pixel_to_screen_coordinate_));
127 glfwWindowHint(GLFW_SAMPLES, 4);
128 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
129 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
130 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
131 int visible_hint = visible ? 1 : 0;
133 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
136 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
138 glfwWindowHint(GLFW_VISIBLE, visible_hint);
146 glfwSetWindowPos(
window_, left, top);
147 glfwSetWindowUserPointer(
window_,
this);
151 glfwSetWindowSize(
window_, 100, 100);
152 glfwSetWindowPos(
window_, 100, 100);
153 int pixel_width_in_osx, pixel_height_in_osx;
154 glfwGetFramebufferSize(
window_, &pixel_width_in_osx, &pixel_height_in_osx);
155 if (pixel_width_in_osx > 0) {
156 pixel_to_screen_coordinate_ = 100.0 / (double)pixel_width_in_osx;
158 pixel_to_screen_coordinate_ = 1.0;
160 glfwSetWindowSize(
window_, std::round(
width * pixel_to_screen_coordinate_),
161 std::round(
height * pixel_to_screen_coordinate_));
162 glfwSetWindowPos(
window_, std::round(left * pixel_to_screen_coordinate_),
163 std::round(top * pixel_to_screen_coordinate_));
166 auto window_refresh_callback = [](GLFWwindow *window) {
167 static_cast<Visualizer *
>(glfwGetWindowUserPointer(window))
170 glfwSetWindowRefreshCallback(
window_, window_refresh_callback);
172 auto window_resize_callback = [](GLFWwindow *window,
int w,
int h) {
173 static_cast<Visualizer *
>(glfwGetWindowUserPointer(window))
176 glfwSetFramebufferSizeCallback(
window_, window_resize_callback);
178 auto mouse_move_callback = [](GLFWwindow *window,
double x,
double y) {
179 static_cast<Visualizer *
>(glfwGetWindowUserPointer(window))
182 glfwSetCursorPosCallback(
window_, mouse_move_callback);
184 auto mouse_scroll_callback = [](GLFWwindow *window,
double x,
double y) {
185 static_cast<Visualizer *
>(glfwGetWindowUserPointer(window))
188 glfwSetScrollCallback(
window_, mouse_scroll_callback);
190 auto mouse_button_callback = [](GLFWwindow *window,
int button,
int action,
192 static_cast<Visualizer *
>(glfwGetWindowUserPointer(window))
195 glfwSetMouseButtonCallback(
window_, mouse_button_callback);
197 auto key_press_callback = [](GLFWwindow *window,
int key,
int scancode,
198 int action,
int mods) {
199 static_cast<Visualizer *
>(glfwGetWindowUserPointer(window))
202 glfwSetKeyCallback(
window_, key_press_callback);
204 auto window_close_callback = [](GLFWwindow *window) {
205 static_cast<Visualizer *
>(glfwGetWindowUserPointer(window))
208 glfwSetWindowCloseCallback(
window_, window_close_callback);
210 glfwMakeContextCurrent(
window_);
225 int window_width, window_height;
226 glfwGetFramebufferSize(
window_, &window_width, &window_height);
242 glDeleteVertexArrays(1, &
vao_id_);
250 std::function<
bool(
Visualizer *)> callback_func) {
272 glfwMakeContextCurrent(
window_);
276 double extent =
std::max(0.01, boundingbox.GetMaxExtent() * 0.2);
280 std::make_shared<glsl::CoordinateFrameRenderer>();
306 glfwSetWindowShouldClose(
window_, GL_TRUE);
314 glfwMakeContextCurrent(
window_);
320 return !glfwWindowShouldClose(
window_);
327 glfwMakeContextCurrent(
window_);
333 return !glfwWindowShouldClose(
window_);
337 bool reset_bounding_box) {
342 if (!geometry_ptr.get()) {
344 "[AddGeometry] Invalid pointer. Possibly a null pointer or "
345 "None was passed in.");
349 glfwMakeContextCurrent(
window_);
350 std::shared_ptr<glsl::GeometryRenderer> renderer_ptr;
354 renderer_ptr = std::make_shared<glsl::PointCloudRenderer>();
355 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
359 renderer_ptr = std::make_shared<glsl::VoxelGridRenderer>();
360 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
364 renderer_ptr = std::make_shared<glsl::OctreeRenderer>();
365 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
369 renderer_ptr = std::make_shared<glsl::LineSetRenderer>();
370 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
374 renderer_ptr = std::make_shared<glsl::PolylineRenderer>();
375 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
379 renderer_ptr = std::make_shared<glsl::FacetRenderer>();
380 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
384 renderer_ptr = std::make_shared<glsl::TriangleMeshRenderer>();
385 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
389 renderer_ptr = std::make_shared<glsl::HalfEdgeMeshRenderer>();
390 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
394 renderer_ptr = std::make_shared<glsl::ImageRenderer>();
395 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
399 renderer_ptr = std::make_shared<glsl::RGBDImageRenderer>();
400 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
404 renderer_ptr = std::make_shared<glsl::TetraMeshRenderer>();
405 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
409 renderer_ptr = std::make_shared<glsl::OrientedBoundingBoxRenderer>();
410 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
414 renderer_ptr = std::make_shared<glsl::AxisAlignedBoundingBoxRenderer>();
415 if (!renderer_ptr->AddGeometry(geometry_ptr)) {
423 if (reset_bounding_box) {
428 "Add geometry and update bounding box to {}",
434 bool reset_bounding_box) {
438 glfwMakeContextCurrent(
window_);
439 std::shared_ptr<glsl::GeometryRenderer> geometry_renderer_delete =
nullptr;
441 if (geometry_renderer_ptr->GetGeometry() == geometry_ptr)
442 geometry_renderer_delete = geometry_renderer_ptr;
444 if (!geometry_renderer_delete)
return false;
447 if (reset_bounding_box) {
451 "Remove geometry and update bounding box to {}",
460 glfwMakeContextCurrent(
window_);
467 glfwMakeContextCurrent(
window_);
470 if (geometry_ptr ==
nullptr ||
471 renderer_ptr->HasGeometry(geometry_ptr)) {
472 success = (success && renderer_ptr->UpdateGeometry());
492 GLFWmonitor *monitor = glfwGetPrimaryMonitor();
493 if (
const GLFWvidmode *mode = glfwGetVideoMode(monitor)) {
494 glfwSetWindowMonitor(
window_, monitor, 0, 0, mode->width,
495 mode->height, mode->refreshRate);
498 "Internal error: glfwGetVideoMode returns nullptr.");
512 return glfwGetWindowMonitor(
window_) !=
nullptr;
527 utility::LogInfo(
" Ctrl/Cmd + C : Copy current view status into the clipboard.");
536 utility::LogInfo(
" Alt + Enter : Toggle between full screen and windowed mode.");
541 utility::LogInfo(
" Ctrl + +/- : Increase/decrease width of geometry::LineSet.");
543 utility::LogInfo(
" S : Toggle between mesh flat shading and smooth shading.");
static std::shared_ptr< ccMesh > CreateCoordinateFrame(double size=1.0, const Eigen::Vector3d &origin=Eigen::Vector3d(0.0, 0.0, 0.0))
GLFW context, handled as a singleton.
int InitStatus() const
Get the glfwInit status / GLFW_PLATFORM initialized.
static std::shared_ptr< GLFWContext > GetInstance()
Get a shared instance of the GLFW context.
static void GLFWErrorCallback(int error, const char *description)
Defines rendering options for visualizer.
View controller for visualizer.
const ccBBox & GetBoundingBox() const
The main Visualizer class.
virtual bool UpdateGeometry(std::shared_ptr< const ccHObject > geometry_ptr=nullptr)
Function to update geometry.
virtual bool InitRenderOption()
Function to initialize RenderOption.
virtual bool IsFullScreen()
virtual void KeyPressCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
std::function< bool(Visualizer *)> animation_callback_func_
std::vector< std::shared_ptr< glsl::GeometryRenderer > > utility_renderer_ptrs_
bool WaitEvents()
Function to process the event queue and return if the window is closed.
virtual bool InitViewControl()
Function to initialize ViewControl.
virtual bool HasGeometry() const
virtual void WindowRefreshCallback(GLFWwindow *window)
virtual bool InitOpenGL()
Function to initialize OpenGL.
std::shared_ptr< ccMesh > coordinate_frame_mesh_ptr_
virtual bool RemoveGeometry(std::shared_ptr< const ccHObject > geometry_ptr, bool reset_bounding_box=true)
Function to remove geometry from the scene.
virtual void UpdateRender()
Function to inform render needed to be updated.
virtual void MouseScrollCallback(GLFWwindow *window, double x, double y)
Eigen::Vector2i saved_window_pos_
std::unordered_set< std::shared_ptr< const ccHObject > > geometry_ptrs_
virtual void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
std::unique_ptr< RenderOption > render_option_ptr_
virtual void MouseMoveCallback(GLFWwindow *window, double x, double y)
void ResetViewPoint(bool reset_bounding_box=false)
Function to reset view point.
virtual bool AddGeometry(std::shared_ptr< const ccHObject > geometry_ptr, bool reset_bounding_box=true)
Function to add geometry to the scene and create corresponding shaders.
void DestroyVisualizerWindow()
Function to destroy a window.
void Run()
Function to activate the window.
virtual void SetFullScreen(bool fullscreen)
Functions to change between fullscreen and windowed modes.
void Close()
Function to to notify the window to be closed.
std::function< bool(Visualizer *)> animation_callback_func_in_loop_
void RegisterAnimationCallback(std::function< bool(Visualizer *)> callback_func)
Function to register a callback function for animation.
bool CreateVisualizerWindow(const std::string &window_name="CloudViewer", const int width=640, const int height=480, const int left=50, const int top=50, const bool visible=true)
Function to create a window and initialize GLFW.
virtual void UpdateWindowTitle()
virtual void BuildUtilities()
std::shared_ptr< GLFWContext > glfw_context_
Shared GLFW context.
std::unordered_set< std::shared_ptr< glsl::GeometryRenderer > > geometry_renderer_ptrs_
virtual void ToggleFullScreen()
std::unique_ptr< ViewControl > view_control_ptr_
ViewControl & GetViewControl()
Function to retrieve the associated ViewControl.
virtual void WindowResizeCallback(GLFWwindow *window, int w, int h)
virtual void PrintVisualizerHelp()
std::vector< std::shared_ptr< const ccHObject > > utility_ptrs_
virtual bool ClearGeometries()
virtual void WindowCloseCallback(GLFWwindow *window)
Function to notify the window to be closed.
Eigen::Vector2i saved_window_size_
std::shared_ptr< glsl::CoordinateFrameRenderer > coordinate_frame_mesh_renderer_ptr_
static void error(char *msg)
Generic file read and write utility for python interface.