Apollo  6.0
Open source self driving car software
visualization_engine.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 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 
21 #pragma once
22 
23 #include <list>
24 #include <map>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 #include "Eigen/Geometry"
30 #include "opencv2/opencv.hpp"
31 
33 
34 namespace apollo {
35 namespace localization {
36 namespace msf {
37 
43  void set(const Eigen::Translation3d &location,
44  const Eigen::Quaterniond &attitude, const Eigen::Vector3d &std_var,
45  const std::string &description, const double timestamp,
46  const unsigned int frame_id) {
48  this->std_var = std_var;
49  is_has_std = true;
50  }
51 
52  void set(const Eigen::Translation3d &location,
53  const Eigen::Quaterniond &attitude, const std::string &description,
54  const double timestamp, const unsigned int frame_id) {
56  this->attitude = attitude;
57  this->pose = location * attitude;
58  is_has_attitude = true;
59  }
60 
61  void set(const Eigen::Translation3d &location, const Eigen::Vector3d &std_var,
62  const std::string &description, const double timestamp,
63  const unsigned int frame_id) {
65  this->std_var = std_var;
66  is_has_std = true;
67  }
68 
69  void set(const Eigen::Translation3d &location, const std::string &description,
70  const double timestamp, const unsigned int frame_id) {
71  this->attitude = Eigen::Quaterniond::Identity();
72  this->location = location;
73  this->pose = location * attitude;
74  this->description = description;
75  this->timestamp = timestamp;
76  this->frame_id = frame_id;
77  is_valid = true;
78  }
79 
80  Eigen::Translation3d location;
81  Eigen::Quaterniond attitude;
83  Eigen::Vector3d std_var = {0.01, 0.01, 0.01};
84  std::string description;
85  double timestamp = 0;
86  unsigned int frame_id = 0;
87  bool is_valid = false;
88  bool is_has_attitude = false;
89  bool is_has_std = false;
90 
91  public:
92  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
93 };
94 
100  void set(const std::vector<float> &map_resolutions,
101  const unsigned int map_node_size_x,
102  const unsigned int map_node_size_y, const double map_min_x,
103  const double map_min_y, const double map_max_x,
104  const double map_max_y) {
105  this->map_resolutions = map_resolutions;
106  this->map_node_size_x = map_node_size_x;
107  this->map_node_size_y = map_node_size_y;
108  this->map_min_x = map_min_x;
109  this->map_min_y = map_min_y;
110  this->map_max_x = map_max_x;
111  this->map_max_y = map_max_y;
112  }
113 
115  std::vector<float> map_resolutions;
117  unsigned int map_node_size_x = 0;
119  unsigned int map_node_size_y = 0;
121  double map_min_x = 0;
122  double map_min_y = 0;
123  double map_max_x = 0;
124  double map_max_y = 0;
125 };
126 
131 struct MapImageKey {
132  bool operator<(const MapImageKey &key) const;
133 
134  unsigned int level = 0;
135  int zone_id = 0;
136  unsigned int node_north_id = 0;
137  unsigned int node_east_id = 0;
138 };
139 
145  public:
146  typedef std::list<std::pair<MapImageKey, cv::Mat>>::iterator ListIterator;
147 
148  public:
149  explicit MapImageCache(int capacity) : _capacity(capacity) {}
150  bool Get(const MapImageKey &key, cv::Mat *image);
151  void Set(const MapImageKey &key, const cv::Mat &image);
152 
153  private:
154  unsigned int _capacity;
155  std::map<MapImageKey, ListIterator> _map;
156  std::list<std::pair<MapImageKey, cv::Mat>> _list;
157 };
158 
164  public:
166  ~VisualizationEngine() = default;
167 
168  public:
169  bool Init(const std::string &map_folder, const std::string &map_visual_folder,
170  const VisualMapParam &map_param, const unsigned int resolution_id,
171  const int zone_id, const Eigen::Affine3d &extrinsic,
172  const unsigned int loc_info_num = 1);
173  void Visualize(::apollo::common::EigenVector<LocalizatonInfo> &&loc_infos,
175  void SetAutoPlay(bool auto_play);
176 
177  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
178 
179  private:
180  void Preprocess(const std::string &map_folder,
181  const std::string &map_visual_folder);
182  void Draw();
183  void DrawLoc(const cv::Point &bias);
184  void DrawStd(const cv::Point &bias);
185  void DrawCloud(const cv::Point &bias);
186  void DrawTrajectory(const cv::Point &bias);
187  void DrawLegend();
188  void DrawInfo();
189  void DrawTips();
190 
191  void UpdateLevel();
193  void GenerateMutiResolutionImages(const std::vector<std::string> &src_files,
194  const int base_path_length,
195  const std::string &dst_folder);
196  void InitOtherParams(const int x_min, const int y_min, const int x_max,
197  const int y_max, const int level,
198  const std::string &path);
199  bool InitOtherParams(const std::string &params_file);
200 
202  void CloudToMat(const Eigen::Affine3d &cur_pose,
203  const Eigen::Affine3d &velodyne_extrinsic,
205  cv::Mat *cloud_img, cv::Mat *cloud_img_mask);
206  void CoordToImageKey(const Eigen::Vector2d &coord, MapImageKey *key);
208  cv::Point CoordToMapGridIndex(const Eigen::Vector2d &coord,
209  const unsigned int resolution_id,
210  const int stride);
212  cv::Point MapGridIndexToNodeGridIndex(const cv::Point &p);
213 
214  bool LoadImageToCache(const MapImageKey &key);
215 
216  void RotateImg(const cv::Mat &in_img, cv::Mat *out_img, double angle);
217 
218  void SetViewCenter(const double center_x, const double center_y);
219  void UpdateViewCenter(const double move_x, const double move_y);
220  void SetScale(const double scale);
221  void UpdateScale(const double factor);
222  bool UpdateCarLocId();
223  bool UpdateCarLocId(const unsigned int car_loc_id);
224  bool UpdateTrajectoryGroups();
225  void ProcessKey(int key);
226 
227  inline void QuaternionToEuler(const double quaternion[4], double att[3]) {
228  double dcm21 =
229  2 * (quaternion[2] * quaternion[3] + quaternion[0] * quaternion[1]);
230  double dcm20 =
231  2 * (quaternion[1] * quaternion[3] - quaternion[0] * quaternion[2]);
232  double dcm22 =
233  quaternion[0] * quaternion[0] - quaternion[1] * quaternion[1] -
234  quaternion[2] * quaternion[2] + quaternion[3] * quaternion[3];
235  double dcm01 =
236  2 * (quaternion[1] * quaternion[2] - quaternion[0] * quaternion[3]);
237  double dcm11 =
238  quaternion[0] * quaternion[0] - quaternion[1] * quaternion[1] +
239  quaternion[2] * quaternion[2] - quaternion[3] * quaternion[3];
240 
241  att[0] = asin(dcm21); // the angle rotate respect to X
242  att[1] = atan2(-dcm20, dcm22); // the angle rotate respect to Y
243  att[2] = atan2(dcm01, dcm11); // the angle rotate respect to Z
244 
245  return;
246  }
247 
248  private:
249  std::string map_folder_;
250  std::string map_visual_folder_;
251  VisualMapParam map_param_;
252  unsigned int zone_id_ = 50;
253  unsigned int resolution_id_ = 0;
254 
255  std::string image_visual_resolution_path_;
256  std::string image_visual_leaf_path_;
257 
258  MapImageCache map_image_cache_;
259  cv::Point lt_node_index_;
260  cv::Point lt_node_grid_index_;
261 
262  std::string window_name_ = "Local Visualizer";
263  cv::Mat image_window_;
264  cv::Mat big_window_;
265  cv::Mat subMat_[3][3];
266  cv::Mat tips_window_;
267 
268  Eigen::Vector2d _view_center;
269  double cur_scale_ = 1.0;
270  int cur_stride_ = 1;
271  int cur_level_ = 0;
272  int max_level_ = 0;
273  int max_stride_ = 1;
274 
275  bool is_init_ = false;
276  bool follow_car_ = true;
277  bool auto_play_ = false;
278 
279  Eigen::Affine3d car_pose_;
281  cv::Mat cloud_img_;
282  cv::Mat cloud_img_mask_;
283  Eigen::Vector2d cloud_img_lt_coord_;
284  Eigen::Affine3d velodyne_extrinsic_;
285 
286  unsigned int loc_info_num_ = 1;
287  unsigned int car_loc_id_ = 0;
288  unsigned int expected_car_loc_id_ = 0;
290  std::vector<std::map<double, Eigen::Vector2d>> trajectory_groups_;
291 
292  bool is_draw_car_ = true;
293  bool is_draw_trajectory_ = true;
294  bool is_draw_std_ = true;
295  std::vector<cv::Mat> car_img_mats_;
296 };
297 
298 } // namespace msf
299 } // namespace localization
300 } // namespace apollo
std::vector< EigenType, Eigen::aligned_allocator< EigenType > > EigenVector
Definition: eigen_defs.h:32
std::list< std::pair< MapImageKey, cv::Mat > >::iterator ListIterator
Definition: visualization_engine.h:146
The data structure to store info of a localization.
Definition: visualization_engine.h:42
double timestamp
Definition: visualization_engine.h:85
The cache to load map images.
Definition: visualization_engine.h:144
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Eigen::Vector3d std_var
Definition: visualization_engine.h:83
Eigen::Vector2d Vector2d
Definition: base_map_fwd.h:36
bool operator<(const edge &a, const edge &b)
Definition: segment_graph.h:53
Eigen::Vector3d Vector3d
Definition: frame_transform.h:27
Eigen::Affine3d pose
Definition: visualization_engine.h:82
std::string description
Definition: visualization_engine.h:84
std::vector< float > map_resolutions
The pixel resolutions in the map in meters.
Definition: visualization_engine.h:115
MapImageCache(int capacity)
Definition: visualization_engine.h:149
The data structure to store parameters of a map.
Definition: visualization_engine.h:99
unsigned int frame_id
Definition: visualization_engine.h:86
EigenVector< Eigen::Vector3d > EigenVector3dVec
Definition: eigen_defs.h:38
bool is_has_attitude
Definition: visualization_engine.h:88
bool Init(const char *binary_name)
Eigen::Quaterniond attitude
Definition: visualization_engine.h:81
The engine to draw all elements for visualization.
Definition: visualization_engine.h:163
PointXYZIL Point
Definition: types.h:61
bool is_valid
Definition: visualization_engine.h:87
bool is_has_std
Definition: visualization_engine.h:89
Eigen::Affine3d Affine3d
Definition: base_map_fwd.h:34
Eigen::Translation3d location
Definition: visualization_engine.h:80
The key structure of a map image .
Definition: visualization_engine.h:131