Program Listing for File interactive_marker_control.hpp

Return to documentation for file (/tmp/ws/src/rviz/rviz_default_plugins/include/rviz_default_plugins/displays/interactive_markers/interactive_marker_control.hpp)

/*
 * Copyright (c) 2011, Willow Garage, Inc.
 * Copyright (c) 2019, Open Source Robotics Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef RVIZ_DEFAULT_PLUGINS__DISPLAYS__INTERACTIVE_MARKERS__INTERACTIVE_MARKER_CONTROL_HPP_
#define RVIZ_DEFAULT_PLUGINS__DISPLAYS__INTERACTIVE_MARKERS__INTERACTIVE_MARKER_CONTROL_HPP_

#ifndef Q_MOC_RUN
#include <memory>
#include <set>
#include <string>
#include <vector>

#include <OgreRay.h>
#include <OgreVector3.h>
#include <OgreQuaternion.h>
#include <OgreSceneManager.h>
#endif

#include <QCursor>

#include <visualization_msgs/msg/interactive_marker_control.hpp>

#include "rviz_common/interaction/forwards.hpp"
#include "rviz_common/interactive_object.hpp"
#include "rviz_common/viewport_mouse_event.hpp"

#include "rviz_default_plugins/displays/marker/markers/marker_base.hpp"
#include "rviz_default_plugins/displays/marker/markers/points_marker.hpp"

namespace Ogre
{
class SceneNode;
}

namespace rviz_common
{
class DisplayContext;
}

namespace rviz_rendering
{
class Line;
}

namespace rviz_default_plugins
{
namespace displays
{
class InteractiveMarker;

class InteractiveMarkerControl
  : public Ogre::SceneManager::Listener,
  public rviz_common::InteractiveObject,
  public std::enable_shared_from_this<InteractiveMarkerControl>
{
public:
  using SharedPtr = std::shared_ptr<InteractiveMarkerControl>;


  InteractiveMarkerControl(
    rviz_common::DisplayContext * context,
    Ogre::SceneNode * reference_node,
    InteractiveMarker * parent);

  virtual ~InteractiveMarkerControl();

  void processMessage(const visualization_msgs::msg::InteractiveMarkerControl & message);

  virtual void enableInteraction(bool enable);

  virtual void handleMouseEvent(rviz_common::ViewportMouseEvent & event);


  virtual void handle3DCursorEvent(
    rviz_common::ViewportMouseEvent event,
    const Ogre::Vector3 & cursor_pos,
    const Ogre::Quaternion & cursor_orientation);


  void interactiveMarkerPoseChanged(
    Ogre::Vector3 int_marker_position,
    Ogre::Quaternion int_marker_orientation);

  inline bool isInteractive()
  {
    return interaction_mode_ != visualization_msgs::msg::InteractiveMarkerControl::NONE;
  }

  void update();

  void setVisible(bool visible);

  bool getVisible();

  enum ControlHighlight
  {
    NO_HIGHLIGHT = 0,
    HOVER_HIGHLIGHT = 3,
    ACTIVE_HIGHLIGHT = 5
  };

  void setHighlight(const ControlHighlight & hl);

  inline InteractiveMarker * getParent()
  {
    return parent_;
  }

  inline const std::string & getName()
  {
    return name_;
  }

  inline const QString & getDescription()
  {
    return description_;
  }

  inline int getInteractionMode()
  {
    return interaction_mode_;
  }

  inline int getOrientationMode()
  {
    return orientation_mode_;
  }

  inline void setShowVisualAids(bool show)
  {
    show_visual_aids_ = show;
  }

protected:
  virtual void preFindVisibleObjects(
    Ogre::SceneManager * source,
    Ogre::SceneManager::IlluminationRenderStage irs,
    Ogre::Viewport * v);

  void updateControlOrientationForViewFacing(Ogre::Viewport * v);


  Ogre::Ray getMouseRayInReferenceFrame(
    const rviz_common::ViewportMouseEvent & event, int x, int y);

  void beginRelativeMouseMotion(const rviz_common::ViewportMouseEvent & event);

  bool getRelativeMouseMotion(const rviz_common::ViewportMouseEvent & event, int & dx, int & dy);

  void rotateXYRelative(const rviz_common::ViewportMouseEvent & event);

  void rotateZRelative(const rviz_common::ViewportMouseEvent & event);

  void moveZAxisRelative(const rviz_common::ViewportMouseEvent & event);

  void moveZAxisWheel(const rviz_common::ViewportMouseEvent & event);

  void moveViewPlane(Ogre::Ray & mouse_ray, const rviz_common::ViewportMouseEvent & event);

  void rotate(Ogre::Ray & mouse_ray);

  void rotate(const Ogre::Vector3 & cursor_position_in_reference_frame);


  void moveRotate(Ogre::Ray & mouse_ray);


  void moveRotate(
    const Ogre::Vector3 & cursor_position_in_reference_frame,
    bool lock_axis = true);


  void movePlane(Ogre::Ray & mouse_ray);

  void movePlane(const Ogre::Vector3 & cursor_position_in_reference_frame);


  void moveAxis(const Ogre::Ray & mouse_ray, const rviz_common::ViewportMouseEvent & event);

  void moveAxis(const Ogre::Vector3 & cursor_position_in_reference_frame);

  void move3D(
    const Ogre::Vector3 & cursor_position_in_reference_frame,
    const Ogre::Quaternion & cursor_orientation_in_reference_frame);

  void rotate3D(
    const Ogre::Vector3 & cursor_position_in_reference_frame,
    const Ogre::Quaternion & cursor_orientation_in_reference_frame);

  void moveRotate3D(
    const Ogre::Vector3 & cursor_position_in_reference_frame,
    const Ogre::Quaternion & cursor_orientation_in_reference_frame);

  bool intersectYzPlane(
    const Ogre::Ray & mouse_ray,
    Ogre::Vector3 & intersection_3d,
    Ogre::Vector2 & intersection_2d,
    float & ray_t);

  bool intersectSomeYzPlane(
    const Ogre::Ray & mouse_ray,
    const Ogre::Vector3 & point_in_plane,
    const Ogre::Quaternion & plane_orientation,
    Ogre::Vector3 & intersection_3d,
    Ogre::Vector2 & intersection_2d,
    float & ray_t);


  bool findClosestPoint(
    const Ogre::Ray & target_ray,
    const Ogre::Ray & mouse_ray,
    Ogre::Vector3 & closest_point);


  void worldToScreen(
    const Ogre::Vector3 & pos_rel_reference,
    const Ogre::Viewport * viewport,
    Ogre::Vector2 & screen_pos);

  void addHighlightPass(markers::S_MaterialPtr materials);

  // Set the highlight color to (a,a,a)
  void setHighlight(float a);

  void recordDraggingInPlaceEvent(rviz_common::ViewportMouseEvent & event);


  void beginMouseMovement(rviz_common::ViewportMouseEvent & event, bool line_visible);

  void handleMouseMovement(rviz_common::ViewportMouseEvent & event);

  void handleMouseWheelMovement(rviz_common::ViewportMouseEvent & event);

  Ogre::Vector3 closestPointOnLineToPoint(
    const Ogre::Vector3 & line_start,
    const Ogre::Vector3 & line_dir,
    const Ogre::Vector3 & test_point);

  void makeMarkers(const visualization_msgs::msg::InteractiveMarkerControl & message);

  void stopDragging(bool force = false);

  virtual inline const QCursor & getCursor() const
  {
    return cursor_;
  }

  bool mouse_dragging_;

  // TODO(jacobperron): Abstract away Ogre (and other references) if possible
  Ogre::Viewport * drag_viewport_;

  std::shared_ptr<rviz_common::ViewportMouseEvent> dragging_in_place_event_;

  rviz_common::DisplayContext * context_;

  rviz_common::interaction::CollObjectHandle coll_object_handle_;

  Ogre::SceneNode * reference_node_;

  Ogre::SceneNode * control_frame_node_;

  Ogre::SceneNode * markers_node_;

  int interaction_mode_;

  int orientation_mode_;

  bool independent_marker_orientation_;


  Ogre::Quaternion control_orientation_;

  bool always_visible_;

  QString description_;

  std::string name_;

  std::vector<markers::MarkerBase::SharedPtr> markers_;

  InteractiveMarker * parent_;

  std::set<Ogre::Pass *> highlight_passes_;

  std::vector<markers::PointsMarker::SharedPtr> points_markers_;


  Ogre::Radian rotation_;


  Ogre::Radian rotation_at_mouse_down_;

  Ogre::Vector3 grab_point_in_reference_frame_;

  Ogre::Quaternion grab_orientation_in_reference_frame_;

  Ogre::Vector3 parent_to_cursor_in_cursor_frame_at_grab_;

  Ogre::Quaternion rotation_cursor_to_parent_at_grab_;

  Qt::KeyboardModifiers modifiers_at_drag_begin_;

  int mouse_x_at_drag_begin_;

  int mouse_y_at_drag_begin_;

  Ogre::Ray mouse_ray_at_drag_begin_;

  double mouse_z_scale_;

  int mouse_relative_to_absolute_x_;

  int mouse_relative_to_absolute_y_;

  // Ogre::Vector3 parent_to_grab_position_; // obsolete now, but left for ABI compatibility

  Ogre::Vector3 parent_position_at_mouse_down_;

  Ogre::Quaternion control_frame_orientation_at_mouse_down_;

  Ogre::Quaternion parent_orientation_at_mouse_down_;

  Ogre::Vector3 rotation_axis_;


  Ogre::Vector3 rotation_center_rel_control_;


  Ogre::Vector3 grab_point_rel_control_;

  bool has_focus_;

  bool interaction_enabled_;

  bool visible_;

  bool view_facing_;

  QCursor cursor_;

  QString status_msg_;

  bool mouse_down_;

  bool show_visual_aids_;

  std::shared_ptr<rviz_rendering::Line> line_;
};  // class InteractiveMarkerControl

}  // namespace displays
}  // namespace rviz_default_plugins

#endif  // RVIZ_DEFAULT_PLUGINS__DISPLAYS__INTERACTIVE_MARKERS__INTERACTIVE_MARKER_CONTROL_HPP_