Apollo 10.0
自动驾驶开放平台
convert.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#pragma once
18
19#include <memory>
20
21#include "modules/common_msgs/perception_msgs/perception_obstacle.pb.h"
22
23#include "cyber/cyber.h"
24#include "cyber/time/clock.h"
29
30namespace apollo {
31namespace perception {
32
34 const std::shared_ptr<onboard::CameraFrame> &frame,
35 PerceptionObstacles *obstacles) {
36 static uint32_t seq_num = 0;
37 auto header = obstacles->mutable_header();
38 header->set_timestamp_sec(cyber::Clock::NowInSeconds());
39 header->set_module_name("perception_camera");
40 header->set_sequence_num(seq_num++);
41 // nanosecond
42 header->set_lidar_timestamp(static_cast<uint64_t>(frame->timestamp * 1e9));
43 header->set_camera_timestamp(static_cast<uint64_t>(frame->timestamp * 1e9));
44
45 obstacles->set_error_code(apollo::common::OK);
46
47 for (const auto &obj : frame->detected_objects) {
48 PerceptionObstacle *obstacle = obstacles->add_perception_obstacle();
49 if (!ConvertObjectToPb(obj, obstacle)) {
50 AERROR << "ConvertObjectToPb failed, Object:" << obj->ToString();
51 return false;
52 }
53 }
54 return true;
55}
56
58 const std::shared_ptr<onboard::SensorFrameMessage> &msg,
59 PerceptionObstacles *obstacles) {
60 auto header = obstacles->mutable_header();
61 header->set_timestamp_sec(cyber::Clock::NowInSeconds());
62 header->set_module_name("perception_obstacle");
63 header->set_sequence_num(msg->seq_num_);
64 header->set_lidar_timestamp(msg->lidar_timestamp_);
65 header->set_camera_timestamp(0);
66 header->set_radar_timestamp(0);
67
68 obstacles->set_error_code(apollo::common::ErrorCode::OK);
69 if (msg != nullptr && msg->frame_ != nullptr) {
70 for (const auto &obj : msg->frame_->objects) {
71 PerceptionObstacle *obstacle = obstacles->add_perception_obstacle();
72 if (!ConvertObjectToPb(obj, obstacle)) {
73 AERROR << "ConvertObjectToPb failed, Object:" << obj->ToString();
74 return false;
75 }
76 }
77 }
78 return true;
79}
80
82 const std::shared_ptr<onboard::LidarFrameMessage> &msg,
83 PerceptionObstacles *obstacles) {
84 static uint32_t seq_num = 0;
85 auto header = obstacles->mutable_header();
86 header->set_timestamp_sec(cyber::Clock::NowInSeconds());
87 header->set_module_name("perception_lidar");
88 header->set_sequence_num(seq_num++);
89 header->set_lidar_timestamp(msg->lidar_timestamp_);
90 header->set_camera_timestamp(0);
91 header->set_radar_timestamp(0);
92
93 obstacles->set_error_code(apollo::common::OK);
94
95 int id = 0;
96 for (const auto &obj : msg->lidar_frame_.get()->segmented_objects) {
97 Eigen::Vector3d trans_point(obj->center(0), obj->center(1), obj->center(2));
98 trans_point = msg->lidar_frame_.get()->lidar2world_pose * trans_point;
99 obj->center(0) = trans_point[0];
100 obj->center(1) = trans_point[1];
101 obj->center(2) = trans_point[2];
102
103 for (size_t i = 0; i < obj->polygon.size(); ++i) {
104 auto &pt = obj->polygon[i];
105 Eigen::Vector3d trans_point_polygon(pt.x, pt.y, pt.z);
106 trans_point_polygon =
107 msg->lidar_frame_.get()->lidar2world_pose * trans_point_polygon;
108 pt.x = trans_point_polygon[0];
109 pt.y = trans_point_polygon[1];
110 pt.z = trans_point_polygon[2];
111 }
112
113 base::PointDCloud& cloud_world = (obj->lidar_supplement).cloud_world;
114 cloud_world.clear();
115 cloud_world.resize(obj->lidar_supplement.cloud.size());
116 for (size_t i = 0; i < obj->lidar_supplement.cloud.size(); ++i) {
117 Eigen::Vector3d pt(obj->lidar_supplement.cloud[i].x,
118 obj->lidar_supplement.cloud[i].y,
119 obj->lidar_supplement.cloud[i].z);
120 Eigen::Vector3d pt_world = msg->lidar_frame_.get()->lidar2world_pose * pt;
121 cloud_world[i].x = pt_world(0);
122 cloud_world[i].y = pt_world(1);
123 cloud_world[i].z = pt_world(2);
124 cloud_world[i].intensity = obj->lidar_supplement.cloud[i].intensity;
125 }
126
127 for (size_t i = 0; i < obj->lidar_supplement.cloud.size(); ++i) {
128 cloud_world.SetPointHeight(i,
129 obj->lidar_supplement.cloud.points_height(i));
130 }
131
132 Eigen::Vector3d trans_anchor_point(obj->anchor_point(0),
133 obj->anchor_point(1),
134 obj->anchor_point(2));
135 trans_anchor_point =
136 msg->lidar_frame_.get()->lidar2world_pose * trans_anchor_point;
137 obj->anchor_point(0) = trans_anchor_point[0];
138 obj->anchor_point(1) = trans_anchor_point[1];
139 obj->anchor_point(2) = trans_anchor_point[2];
140
141 obj->track_id = id++;
142 }
143
144 for (const auto &obj : msg->lidar_frame_.get()->segmented_objects) {
145 PerceptionObstacle *obstacle = obstacles->add_perception_obstacle();
146 if (!ConvertObjectToPb(obj, obstacle)) {
147 AERROR << "ConvertObjectToPb failed, Object:" << obj->ToString();
148 return false;
149 }
150 }
151 return true;
152}
153
154} // namespace perception
155} // namespace apollo
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
void resize(const size_t size) override
void SetPointHeight(size_t i, size_t j, float height)
#define AERROR
Definition log.h:44
bool ConvertObjectToPb(const base::ObjectPtr &object_ptr, PerceptionObstacle *pb_msg)
Definition common.h:28
bool ConvertCameraFrame2Obstacles(const std::shared_ptr< onboard::CameraFrame > &frame, PerceptionObstacles *obstacles)
Definition convert.h:33
bool ConvertLidarFrameMessage2Obstacles(const std::shared_ptr< onboard::LidarFrameMessage > &msg, PerceptionObstacles *obstacles)
Definition convert.h:81
bool ConvertSensorFrameMessage2Obstacles(const std::shared_ptr< onboard::SensorFrameMessage > &msg, PerceptionObstacles *obstacles)
Definition convert.h:57
class register implement
Definition arena_queue.h:37