CARLA
Loading...
Searching...
No Matches
CarlaCameraPublisher.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
8
9#include "carla/Logging.h"
14
15#include <cmath>
16#include <utility>
17
18namespace carla {
19namespace ros2 {
20
24
28
30 std::string base_topic_name, std::string frame_id)
31 : BasePublisher(std::move(base_topic_name), std::move(frame_id)),
32 _impl_image(std::make_shared<PublisherImpl<CarlaCameraImageMsgTraits>>()),
33 _impl_camera_info(std::make_shared<PublisherImpl<CarlaCameraInfoMsgTraits>>()) {
34 if (!_impl_image->Init(GetBaseTopicName() + "/image")) {
35 log_error("CarlaCameraPublisher: failed to initialise image writer for", GetBaseTopicName());
36 }
37 if (!_impl_camera_info->Init(GetBaseTopicName() + "/camera_info")) {
38 log_error("CarlaCameraPublisher: failed to initialise camera_info writer for", GetBaseTopicName());
39 }
40}
41
43
45 // Publish both writers unconditionally so a failure on one does not silence
46 // the other. Image goes first to match the previous per-sensor publishers.
47 const bool image_ok = _impl_image->Publish();
48 const bool info_ok = _impl_camera_info->Publish();
49 return image_ok && info_ok;
50}
51
53 uint32_t height, uint32_t width, const uint8_t *data) const {
54 const size_t size =
55 static_cast<size_t>(height) * static_cast<size_t>(width) * GetChannels();
56 return std::vector<uint8_t>(data, data + size);
57}
58
60 int32_t seconds,
61 uint32_t nanoseconds,
62 uint32_t x_offset,
63 uint32_t y_offset,
64 uint32_t height,
65 uint32_t width,
66 float fov,
67 bool do_rectify) {
68 auto *message = _impl_camera_info->GetMessage();
69 message->header.stamp.sec = seconds;
70 message->header.stamp.nanosec = nanoseconds;
71 message->header.frame_id = GetFrameId();
72
73 const auto k = ComputeIntrinsics(width, height, fov);
74
75 message->height = height;
76 message->width = width;
77 message->distortion_model = "plumb_bob";
78 message->d = {0.0, 0.0, 0.0, 0.0, 0.0};
79 message->k = {k.fx, 0.0, k.cx, 0.0, k.fy, k.cy, 0.0, 0.0, 1.0};
80 message->r = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
81 message->p = {k.fx, 0.0, k.cx, 0.0, 0.0, k.fy, k.cy, 0.0, 0.0, 0.0, 1.0, 0.0};
82 message->binning_x = 0;
83 message->binning_y = 0;
84
85 message->roi.x_offset = x_offset;
86 message->roi.y_offset = y_offset;
87 message->roi.height = height;
88 message->roi.width = width;
89 message->roi.do_rectify = do_rectify;
90
91 return true;
92}
93
95 int32_t seconds,
96 uint32_t nanoseconds,
97 uint32_t height,
98 uint32_t width,
99 const uint8_t *data) {
100 return WriteImage(seconds, nanoseconds, height, width, ComputeImage(height, width, data));
101}
102
104 int32_t seconds,
105 uint32_t nanoseconds,
106 uint32_t height,
107 uint32_t width,
108 std::vector<uint8_t> data) {
109 auto *message = _impl_image->GetMessage();
110 message->header.stamp.sec = seconds;
111 message->header.stamp.nanosec = nanoseconds;
112 message->header.frame_id = GetFrameId();
113
114 message->width = width;
115 message->height = height;
116 message->encoding = GetEncoding();
117 message->is_bigendian = 0;
118 message->step = width * GetChannels() * static_cast<uint32_t>(sizeof(uint8_t));
119 message->data = std::move(data);
120
121 return true;
122}
123
124} // namespace ros2
125} // namespace carla
const std::string & GetBaseTopicName() const noexcept
const std::string & GetFrameId() const noexcept
virtual std::vector< uint8_t > ComputeImage(uint32_t height, uint32_t width, const uint8_t *data) const
std::shared_ptr< PublisherImpl< CarlaCameraInfoMsgTraits > > _impl_camera_info
bool WriteImage(int32_t seconds, uint32_t nanoseconds, uint32_t height, uint32_t width, const uint8_t *data)
virtual std::string GetEncoding() const =0
bool WriteCameraInfo(int32_t seconds, uint32_t nanoseconds, uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
std::shared_ptr< PublisherImpl< CarlaCameraImageMsgTraits > > _impl_image
virtual uint8_t GetChannels() const =0
CarlaCameraPublisher(std::string base_topic_name, std::string frame_id)
CameraIntrinsics ComputeIntrinsics(uint32_t width, uint32_t height, float fov_degrees) noexcept
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
Definition Logging.h:110