-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ImageDisplay: publish mouse clicks (#1737)
Publish mouse clicks (or dragging) on <image_topic>/mouse_click Co-authored-by: Robert Haschke <rhaschke@techfak.uni-bielefeld.de>
- Loading branch information
1 parent
2fd5e9d
commit 23d9e81
Showing
5 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "rviz/image/mouse_click.h" | ||
#include <QWidget> | ||
#include <ros/names.h> | ||
|
||
|
||
namespace rviz | ||
{ | ||
MouseClick::MouseClick(QWidget* widget, const ros::NodeHandle& nh) : QObject(widget) | ||
{ | ||
img_width_ = img_height_ = win_width_ = win_height_ = 0; | ||
node_handle_ = nh; | ||
topic_name_ok_ = false; | ||
} | ||
|
||
void MouseClick::enable() | ||
{ | ||
if (topic_name_ok_) | ||
{ | ||
publisher_ = node_handle_.advertise<geometry_msgs::PointStamped>(topic_, 1); | ||
parent()->installEventFilter(this); | ||
} | ||
} | ||
|
||
void MouseClick::disable() | ||
{ | ||
parent()->removeEventFilter(this); | ||
publisher_.shutdown(); | ||
} | ||
|
||
bool MouseClick::eventFilter(QObject* obj, QEvent* event) | ||
{ | ||
if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove) | ||
{ | ||
QMouseEvent* me = static_cast<QMouseEvent*>(event); | ||
QPointF windowPos = me->windowPos(); | ||
bool left_button = me->buttons() == Qt::LeftButton; | ||
|
||
if (left_button && img_width_ != 0 && img_height_ != 0 && win_width_ != 0 && win_height_ != 0) | ||
{ | ||
float img_aspect = float(img_width_) / float(img_height_); | ||
float win_aspect = float(win_width_) / float(win_height_); | ||
|
||
int pix_x = -1; | ||
int pix_y = -1; | ||
if (img_aspect > win_aspect) // Window is taller than the image: black bars over and under image. | ||
{ | ||
pix_x = int(float(windowPos.x()) / float(win_width_) * float(img_width_)); | ||
|
||
int resized_img_height = int(float(win_width_) / float(img_width_) * float(img_height_)); | ||
int bias = int((float(win_height_) - float(resized_img_height)) / 2.0); | ||
pix_y = (float(windowPos.y()) - bias) / float(resized_img_height) * float(img_height_); | ||
} | ||
else // Window wider than the image: black bars on the side. | ||
{ | ||
pix_y = int(float(windowPos.y()) / float(win_height_) * float(img_height_)); | ||
|
||
int resized_img_width = int(float(win_height_) / float(img_height_) * float(img_width_)); | ||
int bias = int((float(win_width_) - float(resized_img_width)) / 2.0); | ||
pix_x = (float(windowPos.x()) - bias) / float(resized_img_width) * float(img_width_); | ||
} | ||
|
||
// Publish if clicked point is inside the image. | ||
if (pix_x >= 0 && pix_x < img_width_ && pix_y >= 0 && pix_y < img_height_) | ||
{ | ||
geometry_msgs::PointStamped point_msg; | ||
point_msg.header.stamp = ros::Time::now(); | ||
point_msg.point.x = pix_x; | ||
point_msg.point.y = pix_y; | ||
publisher_.publish(point_msg); | ||
} | ||
} | ||
} | ||
return QObject::eventFilter(obj, event); | ||
} | ||
|
||
void MouseClick::setDimensions(int img_width, int img_height, int win_width, int win_height) | ||
{ | ||
img_width_ = img_width; | ||
img_height_ = img_height; | ||
win_width_ = win_width; | ||
win_height_ = win_height; | ||
} | ||
|
||
void MouseClick::setImageTopic(const QString& topic) | ||
{ | ||
disable(); | ||
|
||
// Build the click full topic name based on the image topic | ||
topic_ = topic.toStdString().append("/mouse_click"); | ||
|
||
std::string error; | ||
topic_name_ok_ = ros::names::validate(topic_, error); | ||
|
||
enable(); | ||
} | ||
|
||
} // namespace rviz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef RVIZ_MOUSE_CLICK_H | ||
#define RVIZ_MOUSE_CLICK_H | ||
|
||
#include <QObject> | ||
|
||
#ifndef Q_MOC_RUN // See: https://bugreports.qt-project.org/browse/QTBUG-22829 | ||
#include <iostream> | ||
#include <string> | ||
#include <boost/shared_ptr.hpp> | ||
|
||
#include <QMouseEvent> | ||
|
||
#include "ros/ros.h" | ||
#include "geometry_msgs/PointStamped.h" | ||
#include "std_msgs/String.h" | ||
|
||
#include "rviz/rviz_export.h" | ||
#include "rviz/display.h" | ||
#endif | ||
|
||
|
||
namespace rviz | ||
{ | ||
/** @brief Class for capturing mouse clicks. | ||
* | ||
* This class handles mouse clicking functionalities integrated into the ImageDisplay. | ||
* It uses a qt event filter to capture mouse clicks, handles image resizing and checks if the click was | ||
* inside or outside the image. It also scales the pixel coordinates to get them w.r.t. the image (not | ||
* the window) size. Mouse clicks image pixel coordinates are published as ros geometry_msgs | ||
* PointStamped. The z component is ignored. The topic name where the mouse clicks are published is | ||
* defined created after the subscribed image topic as: <image_topic>/mouse_click. | ||
*/ | ||
|
||
class RVIZ_EXPORT MouseClick : QObject | ||
{ | ||
public: | ||
MouseClick(QWidget* widget, const ros::NodeHandle& nh); | ||
|
||
void enable(); | ||
void disable(); | ||
|
||
void setDimensions(int img_width, int img_height, int win_width, int win_height); | ||
void setImageTopic(const QString& topic); | ||
|
||
private: | ||
virtual bool eventFilter(QObject* obj, QEvent* event); | ||
|
||
int img_width_, img_height_, win_width_, win_height_; // To assess if the clicks are inside the image. | ||
ros::NodeHandle node_handle_; | ||
ros::Publisher publisher_; | ||
std::string topic_; | ||
bool topic_name_ok_; | ||
}; | ||
|
||
} // namespace rviz | ||
#endif |