Program Listing for File CDisplayWindow3D.h

↰ Return to documentation for file (include/mrpt/gui/CDisplayWindow3D.h)

/*                    _
                     | |    Mobile Robot Programming Toolkit (MRPT)
 _ __ ___  _ __ _ __ | |_
| '_ ` _ \| '__| '_ \| __|          https://www.mrpt.org/
| | | | | | |  | |_) | |_
|_| |_| |_|_|  | .__/ \__|     https://github.com/MRPT/mrpt/
               | |
               |_|

 Copyright (c) 2005-2026, Individual contributors, see AUTHORS file
 See: https://www.mrpt.org/Authors - All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once

#include <mrpt/gui/CBaseGUIWindow.h>
#include <mrpt/img/CImage.h>
#include <mrpt/opengl/ShaderProgramManager.h>
#include <mrpt/system/datetime.h>
#include <mrpt/viz/Scene.h>
#include <mrpt/viz/opengl_fonts.h>

#include <mutex>

namespace mrpt::gui
{
class C3DWindowDialog;
class CMyGLCanvas_DisplayWindow3D;

class CDisplayWindow3D : public mrpt::gui::CBaseGUIWindow
{
 public:
  using Ptr = std::shared_ptr<CDisplayWindow3D>;
  using ConstPtr = std::shared_ptr<const CDisplayWindow3D>;

 protected:
  friend class C3DWindowDialog;
  friend class CMyGLCanvas_DisplayWindow3D;

  mrpt::viz::Scene::Ptr m_3Dscene;
  mutable std::recursive_timed_mutex m_csAccess3DScene;

  void createOpenGLContext();

  mrpt::void_ptr_noncopy m_DisplayDeviceContext;
  mrpt::void_ptr_noncopy m_GLRenderingContext;

  std::string m_grab_imgs_prefix;
  unsigned int m_grab_imgs_idx{0};

  bool m_is_capturing_imgs{false};
  mrpt::img::CImage::Ptr m_last_captured_img;
  mutable std::mutex m_last_captured_img_cs;

  void doRender();

  mrpt::system::TTimeStamp m_lastFullScreen;

  double m_last_FPS = 10.0;
  std::mutex m_last_FPS_mtx;

  void internalSetMinMaxRange();

 public:
  CDisplayWindow3D(
      const std::string& windowCaption = std::string(),
      unsigned int initialWindowWidth = 400,
      unsigned int initialWindowHeight = 300);

  static CDisplayWindow3D::Ptr Create(
      const std::string& windowCaption,
      unsigned int initialWindowWidth = 400,
      unsigned int initialWindowHeight = 300);

  ~CDisplayWindow3D() override;

  mrpt::viz::Scene::Ptr& get3DSceneAndLock();

  void unlockAccess3DScene();

  void forceRepaint();
  void repaint() { forceRepaint(); }
  void updateWindow() { forceRepaint(); }
  float getFOV() const;
  void setMinRange(float new_min);
  void setMaxRange(float new_max);
  void setFOV(float v);
  void resize(unsigned int width, unsigned int height) override;
  void setPos(int x, int y) override;
  void setWindowTitle(const std::string& str) override;
  void setCameraElevationDeg(float deg);
  void setCameraAzimuthDeg(float deg);
  void setCameraPointingToPoint(float x, float y, float z);
  void setCameraZoom(float zoom);
  void setProjectiveModel(bool isProjective);
  float getCameraElevationDeg() const;
  float getCameraAzimuthDeg() const;
  void getCameraPointingToPoint(float& x, float& y, float& z) const;
  float getCameraZoom() const;
  bool isCameraProjective() const;
  void useCameraFromScene(bool useIt = true);

  std::optional<mrpt::math::TLine3D> getLastMousePositionRay() const;

  std::optional<mrpt::img::TPixelCoord> getLastMousePosition() const override;

  void setCursorCross(bool cursorIsCross) override;

  void grabImagesStart(const std::string& grab_imgs_prefix = std::string("video_"));

  void grabImagesStop();

  void captureImagesStart();

  void captureImagesStop();

  bool getLastWindowImage(mrpt::img::CImage& out_img) const;

  mrpt::img::CImage::ConstPtr getLastWindowImagePtr() const;

  std::string grabImageGetNextFile();

  bool isCapturingImgs() const { return m_is_capturing_imgs; }

  void addTextMessage(
      const double x_frac,
      const double y_frac,
      const std::string& text,
      size_t unique_index = 0,
      const mrpt::viz::TFontParams& fontParams = mrpt::viz::TFontParams())
  {
    if (!m_3Dscene)
    {
      return;
    }
    auto gl_view = m_3Dscene->getViewport();
    if (!gl_view)
    {
      return;
    }
    gl_view->addTextMessage(x_frac, y_frac, text, unique_index, fontParams);
  }

  void clearTextMessages()
  {
    if (!m_3Dscene)
    {
      return;
    }
    auto gl_view = m_3Dscene->getViewport();
    if (!gl_view)
    {
      return;
    }
    gl_view->clearTextMessages();
  }

  bool updateTextMessage(const size_t unique_index, const std::string& text)
  {
    if (!m_3Dscene)
    {
      return false;
    }
    auto gl_view = m_3Dscene->getViewport();
    if (!gl_view)
    {
      return false;
    }
    return gl_view->updateTextMessage(unique_index, text);
  }

  double getRenderingFPS() const;

  mrpt::viz::Viewport::Ptr getDefaultViewport();

  void setImageView(const mrpt::img::CImage& img);

  void setImageView(mrpt::img::CImage&& img);

  void sendFunctionToRunOnGUIThread(const std::function<void(void)>& f);

  void sendFunctionWithShaderManager(
      const std::function<void(mrpt::opengl::ShaderProgramManager&)>& f);

  bool is_GL_context_created() const;

  bool wait_for_GL_context(const double timeout_seconds = 1.0) const;

 protected:
  void internal_setRenderingFPS(double FPS);
  void internal_emitGrabImageEvent(const std::string& fil);

};  // End of class def.


class mrptEvent3DWindowGrabImageFile : public mrpt::system::mrptEvent
{
 protected:
  void do_nothing() override {}

 public:
  mrptEvent3DWindowGrabImageFile(CDisplayWindow3D* obj, const std::string& _img_file) :
      source_object(obj), img_file(_img_file)
  {
  }

  CDisplayWindow3D* source_object;
  const std::string& img_file;
};  // End of class def.


class CDisplayWindow3DLocker
{
 public:
  CDisplayWindow3DLocker(CDisplayWindow3D& win, mrpt::viz::Scene::Ptr& out_scene_ptr);
  CDisplayWindow3DLocker(CDisplayWindow3D& win);
  ~CDisplayWindow3DLocker();

 private:
  CDisplayWindow3D& m_win;
};

}  // namespace mrpt::gui