Skip to content

Commit

Permalink
Consider region of interest in CameraDisplay
Browse files Browse the repository at this point in the history
This feature was already implemented in Rviz1
  • Loading branch information
AndreasR30 committed Sep 16, 2022
1 parent 7b4ba85 commit 9b30006
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,9 @@ bool CameraDisplay::updateCamera()
setStatus(StatusLevel::Ok, CAM_INFO_STATUS, "OK");

// adjust the image rectangles to fit the zoom & aspect ratio
background_screen_rect_->setCorners(-1.0f * zoom.x, 1.0f * zoom.y, 1.0f * zoom.x, -1.0f * zoom.y);
overlay_screen_rect_->setCorners(-1.0f * zoom.x, 1.0f * zoom.y, 1.0f * zoom.x, -1.0f * zoom.y);
Ogre::Vector4 corners = calculateScreenCorners(info, zoom);
background_screen_rect_->setCorners(corners.x, corners.y, corners.z, corners.w);
overlay_screen_rect_->setCorners(corners.x, corners.y, corners.z, corners.w);

Ogre::AxisAlignedBox aabInf;
aabInf.setInfinite();
Expand Down Expand Up @@ -620,6 +621,28 @@ Ogre::Matrix4 CameraDisplay::calculateProjectionMatrix(
return proj_matrix;
}

static Ogre::Vector4 calculateScreenCorners(
const sensor_msgs::msg::CameraInfo::ConstSharedPtr info,
const Ogre::Vector2 & zoom)
{
float x_corner_start, y_corner_start, x_corner_end, y_corner_end;

if (info->roi.height != 0 || info->roi.width != 0) {
// corners are computed according to roi
x_corner_start = (2.0 * info->roi.x_offset / info->width - 1.0) * zoom.x;
y_corner_start = (-2.0 * info->roi.y_offset / info->height + 1.0) * zoom.y;
x_corner_end = x_corner_start + (2.0 * info->roi.width / info->width) * zoom.x;
y_corner_end = y_corner_start - (2.0 * info->roi.height / info->height) * zoom.y;
} else {
x_corner_start = -1.0f * zoom.x;
y_corner_start = 1.0f * zoom.y;
x_corner_end = 1.0f * zoom.x;
y_corner_end = -1.0f * zoom.y;
}

return {x_corner_start, y_corner_start, x_corner_end, y_corner_end};
}

void CameraDisplay::processMessage(sensor_msgs::msg::Image::ConstSharedPtr msg)
{
texture_->addMessage(msg);
Expand Down

0 comments on commit 9b30006

Please sign in to comment.