type.h 12.1 KB
Newer Older
1
/******************************************************************************
2
 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include <Eigen/Core>
#include <opencv2/opencv.hpp>

#include <memory>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <limits>

#include "modules/common/log.h"
// #include "modules/perception/obstacle/camera/
//           lane_post_process/common/projector.h"

#ifndef MODULES_PERCEPTION_OBSTACLE_CAMERA_LANE_POST_PROCESS_COMMON_TYPE_H_
#define MODULES_PERCEPTION_OBSTACLE_CAMERA_LANE_POST_PROCESS_COMMON_TYPE_H_

namespace apollo {
namespace perception {

// #ifndef DEBUG
// #define DEBUG true
// #endif

#ifndef M_PI
#define M_PI REAL(3.1415926535897932384626433832795029)
#endif

#ifndef UF_BLOCK_WIDTH
#define UF_BLOCK_WIDTH 32
#endif

#ifndef UF_BLOCK_HEIGHT
#define UF_BLOCK_HEIGHT 16
#endif

#ifndef MAX_GROUP_PREDICTION_MARKER_NUM
#define MAX_GROUP_PREDICTION_MARKER_NUM 10
#endif

#ifndef MAX_POLY_ORDER
#define MAX_POLY_ORDER 3
#endif

#ifndef MAX_LANE_SPATIAL_LABELS
#define MAX_LANE_SPATIAL_LABELS 3
#endif

#ifndef MIN_BETWEEN_LANE_DISTANCE
#define MIN_BETWEEN_LANE_DISTANCE 2.5
#endif

typedef float ScalarType;

const ScalarType kEpsilon = 1e-5;

// define colors for visualization (Blue, Green, Red)
const cv::Scalar kBlack(0, 0, 0);
const cv::Scalar kWhite(255, 255, 255);
const cv::Scalar kGrey(128, 128, 128);
const cv::Scalar kRed(0, 0, 255);
const cv::Scalar kGreen(0, 255, 0);
const cv::Scalar kBlue(255, 0, 0);
const cv::Scalar kYellow(0, 255, 255);
const cv::Scalar kCyan(255, 255, 0);
const cv::Scalar kMagenta(255, 0, 255);
const cv::Scalar kPurple(255, 48, 155);
const cv::Scalar kGreenYellow(47, 255, 173);

// delay time for visualization
const int kDelayTime(0);

enum MarkerShapeType {
  POINT = 0,
  LINE_SEGMENT,
};

enum SpaceType {
  IMAGE = 0,
  VEHICLE,
};

typedef Eigen::Matrix<ScalarType, 2, 1> Vector2D;

enum AssociationMethod {
102
  GREEDY_GROUP_CONNECT = 0,
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
};

struct AssociationParam {
  AssociationMethod method;
  ScalarType min_distance;
  ScalarType max_distance;
  ScalarType distance_weight;
  ScalarType max_deviation_angle;
  ScalarType deviation_angle_weight;
  ScalarType max_relative_orie;
  ScalarType relative_orie_weight;
  ScalarType max_departure_distance;
  ScalarType departure_distance_weight;
  ScalarType min_orientation_estimation_size;

  AssociationParam()
119
      : method(AssociationMethod::GREEDY_GROUP_CONNECT),
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        min_distance(0.0),
        max_distance(100.0),
        distance_weight(0.4),
        max_deviation_angle(static_cast<ScalarType>(M_PI / 6.0)),
        deviation_angle_weight(0.4),
        max_relative_orie(static_cast<ScalarType>(M_PI / 6.0)),
        relative_orie_weight(0.2),
        max_departure_distance(50.0),
        departure_distance_weight(0.4),
        min_orientation_estimation_size(5.0) {}
};

struct Marker {
  MarkerShapeType shape_type;
  int marker_type;
  SpaceType space_type;
  Vector2D pos;
  Vector2D image_pos;
  Vector2D start_pos;
  Vector2D image_start_pos;
  Vector2D orie;
  ScalarType angle;
  int original_id;
  int cc_id;
  int inner_edge_id;
  int cc_edge_ascend_id;
  int cc_edge_descend_id;
  int cc_next_marker_id;
  int lane_id;
  ScalarType confidence;

  cv::Point vis_pos;
  cv::Point vis_start_pos;

  Marker()
      : shape_type(MarkerShapeType::LINE_SEGMENT),
        marker_type(0),
        space_type(SpaceType::IMAGE),
        pos(0.0, 0.0),
        image_pos(0.0, 0.0),
        start_pos(0.0, 0.0),
        image_start_pos(0.0, 0.0),
        orie(0.0, -1.0),
        angle(static_cast<ScalarType>(-M_PI / 2.0)),
        original_id(-1),
        cc_id(-1),
        inner_edge_id(-1),
        cc_edge_ascend_id(-1),
        cc_edge_descend_id(-1),
        cc_next_marker_id(-1),
        lane_id(-1),
        confidence(0.0) {}

  static bool comp(const Marker &a, const Marker &b) {
    assert(a.space_type == b.space_type);
    return (a.space_type == SpaceType::IMAGE) ? a.pos(1) > b.pos(1)
                                              : a.pos(1) < b.pos(1);
  }
};

typedef Eigen::Matrix<ScalarType, 4, 1> Bbox;  // (x_min, y_min, x_max, y_max)

typedef std::vector<std::pair<int, int>> Graph;

enum SpatialLabelType {
  L_0 = 0,
  L_1,
  L_2,
  R_0,
  R_1,
  R_2,
  L_UNKNOWN,
  R_UNKNOWN,
};

enum SemanticLabelType {
  SOLID = 0,
  DASHED,
  PARRELLE,
  UNKNOWN,
};

typedef Eigen::Matrix<ScalarType, MAX_POLY_ORDER + 1, 1> PolyModel;

struct LaneInstance {
  int graph_id;
  ScalarType siz;
  Bbox bounds;
  PolyModel model;
  ScalarType lateral_dist;

  LaneInstance() : graph_id(-1), siz(0), lateral_dist(0) {
    for (int j = 0; j <= MAX_POLY_ORDER; ++j) {
      this->model(j) = 0;
    }
    bounds << 0, 0, 0, 0;
  }

  LaneInstance(int i, ScalarType s, const Bbox &box)
      : graph_id(i), siz(s), lateral_dist(0) {
    for (int j = 0; j <= MAX_POLY_ORDER; ++j) {
      model(j) = 0;
    }
    bounds = box;
  }

226
  static bool CompareSiz(const LaneInstance &a, const LaneInstance &b) {
227 228 229
    return a.siz > b.siz;
  }

230
  static bool CompareBound(const LaneInstance &a, const LaneInstance &b) {
231 232 233
    return a.bounds(0) < b.bounds(0);  // x_min
  }

234
  bool HasOverlap(const LaneInstance &a) {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    return a.bounds(0) <= this->bounds(2) && a.bounds(2) >= this->bounds(0);
  }
};

struct L3CubicCurve {
  float x_start;
  float x_end;
  float a;
  float b;
  float c;
  float d;
};

struct L3LaneInfo {
  int lane_id;
  int left_idx;
  int right_idx;
  float lane_width;
  int carleft_idx;
  int carright_idx;
};

struct LaneObject {
  size_t point_num;
  std::vector<Vector2D> pos;
  std::vector<Vector2D> orie;
  std::vector<Vector2D> image_pos;
  std::vector<ScalarType> confidence;
  SpatialLabelType spatial;
  SemanticLabelType semantic;
  bool is_compensated;

  ScalarType longitude_start;
  ScalarType longitude_end;
  int order;
  PolyModel model;
  ScalarType lateral_distance;

  L3CubicCurve pos_curve;
  L3CubicCurve img_curve;
  L3LaneInfo lane_info;
  double timestamp;
  int seq_num;

  LaneObject()
      : point_num(0),
        spatial(SpatialLabelType::L_0),
        semantic(SemanticLabelType::UNKNOWN),
        is_compensated(false),
        longitude_start(std::numeric_limits<ScalarType>::max()),
        longitude_end(-std::numeric_limits<ScalarType>::max()),
        order(0),
        lateral_distance(0.0) {
    model.setZero();
    pos.reserve(100);
    orie.reserve(100);
    image_pos.reserve(100);
    confidence.reserve(100);
  }

295
  std::string GetSpatialLabel() const {
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    switch (spatial) {
      case SpatialLabelType::L_0:
        return "L0";
      case SpatialLabelType::L_1:
        return "L1";
      case SpatialLabelType::L_2:
        return "L2";
      case SpatialLabelType::R_0:
        return "R0";
      case SpatialLabelType::R_1:
        return "R1";
      case SpatialLabelType::R_2:
        return "R2";
      default:
        AERROR << "unknown lane spatial label.";
        return "unknown spatial label";
    }
  }

  /*
  void copy_to(LaneObject& new_lane_object) {
    new_lane_object.point_num = point_num;
    new_lane_object.spatial = spatial;
    new_lane_object.semantic = semantic;
    new_lane_object.is_compensated = is_compensated;

    for (size_t i = 0; i < new_lane_object.point_num; ++i) {
      new_lane_object.pos.push_back(pos[i]);
      new_lane_object.orie.push_back(orie[i]);
      new_lane_object.image_pos.push_back(image_pos[i]);
      new_lane_object.confidence.push_back(confidence[i]);
    }

    new_lane_object.longitude_start = longitude_start;
    new_lane_object.longitude_end = longitude_end;
    new_lane_object.order = order;
    for (int i = 0; i <= MAX_POLY_ORDER; ++i) {
      new_lane_object.model(i) = model(i);
    }
    new_lane_object.lateral_distance = lateral_distance;

    new_lane_object.pos_curve.a = pos_curve.a;
    new_lane_object.pos_curve.b = pos_curve.b;
    new_lane_object.pos_curve.c = pos_curve.c;
    new_lane_object.pos_curve.d = pos_curve.d;
    new_lane_object.pos_curve.x_start = pos_curve.x_start;
    new_lane_object.pos_curve.x_end = pos_curve.x_end;

    new_lane_object.img_curve.a = img_curve.a;
    new_lane_object.img_curve.b = img_curve.b;
    new_lane_object.img_curve.c = img_curve.c;
    new_lane_object.img_curve.d = img_curve.d;
    new_lane_object.img_curve.x_start = img_curve.x_start;
    new_lane_object.img_curve.x_end = img_curve.x_end;

    new_lane_object.lane_info.lane_id = lane_info.lane_id;
    new_lane_object.lane_info.left_idx = lane_info.left_idx;
    new_lane_object.lane_info.right_idx = lane_info.right_idx;
    new_lane_object.lane_info.lane_width = lane_info.lane_width;
    new_lane_object.lane_info.carleft_idx = lane_info.carleft_idx;
    new_lane_object.lane_info.carright_idx = lane_info.carright_idx;

    new_lane_object.timestamp = timestamp;
    new_lane_object.seq_num = seq_num;
  }
  */

363
  void CopyTo(LaneObject* new_lane_object) {
364 365 366 367 368
    new_lane_object->point_num = point_num;
    new_lane_object->spatial = spatial;
    new_lane_object->semantic = semantic;
    new_lane_object->is_compensated = is_compensated;

369
    for (size_t i = 0; i < new_lane_object->point_num; ++i) {
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
      new_lane_object->pos.push_back(pos[i]);
      new_lane_object->orie.push_back(orie[i]);
      new_lane_object->image_pos.push_back(image_pos[i]);
      new_lane_object->confidence.push_back(confidence[i]);
    }

    new_lane_object->longitude_start = longitude_start;
    new_lane_object->longitude_end = longitude_end;
    new_lane_object->order = order;
    for (int i = 0; i <= MAX_POLY_ORDER; ++i) {
      new_lane_object->model(i) = model(i);
    }
    new_lane_object->lateral_distance = lateral_distance;

    new_lane_object->pos_curve.a = pos_curve.a;
    new_lane_object->pos_curve.b = pos_curve.b;
    new_lane_object->pos_curve.c = pos_curve.c;
    new_lane_object->pos_curve.d = pos_curve.d;
    new_lane_object->pos_curve.x_start = pos_curve.x_start;
    new_lane_object->pos_curve.x_end = pos_curve.x_end;

    new_lane_object->img_curve.a = img_curve.a;
    new_lane_object->img_curve.b = img_curve.b;
    new_lane_object->img_curve.c = img_curve.c;
    new_lane_object->img_curve.d = img_curve.d;
    new_lane_object->img_curve.x_start = img_curve.x_start;
    new_lane_object->img_curve.x_end = img_curve.x_end;

    new_lane_object->lane_info.lane_id = lane_info.lane_id;
    new_lane_object->lane_info.left_idx = lane_info.left_idx;
    new_lane_object->lane_info.right_idx = lane_info.right_idx;
    new_lane_object->lane_info.lane_width = lane_info.lane_width;
    new_lane_object->lane_info.carleft_idx = lane_info.carleft_idx;
    new_lane_object->lane_info.carright_idx = lane_info.carright_idx;

    new_lane_object->timestamp = timestamp;
    new_lane_object->seq_num = seq_num;
  }
};

// struct for L3 Lane information
struct L3LaneLine {
  SpatialLabelType spatial;
  SemanticLabelType semantic;
  L3CubicCurve pos_curve;
  L3CubicCurve img_curve;
};
struct RoadInfo {
  double timestamp;
  double seq_num;
  std::vector<L3LaneLine> lane_line_vec;
  std::vector<L3LaneInfo> lane_vec;
};

/*
struct LaneDebugContent {
    std::vector<LaneObject> lane_objects;
    std::vector<LaneInstance> cur_lane_instances;
};
*/

typedef std::vector<LaneObject> LaneObjects;
typedef std::shared_ptr<LaneObjects> LaneObjectsPtr;
typedef const std::shared_ptr<LaneObjects> LaneObjectsConstPtr;

435 436 437 438
typedef std::vector<LaneInstance> LaneInstances;
typedef std::shared_ptr<LaneInstances> LaneInstancesPtr;
typedef const std::shared_ptr<LaneInstances> LaneInstancesConstPtr;

439 440 441 442 443 444 445
// typedef adu::perception::obstacle::transformer_tool::
//         Projector<ScalarType> Projector;

}  // namespace perception
}  // namespace apollo

#endif  // MODULES_PERCEPTION_OBSTACLE_CAMERA_LANE_POST_PROCESS_COMMON_TYPE_H_