type.h 14.8 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 226 227 228 229 230 231 232 233 234 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 295 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 363 364 365 366 367 368 369 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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
 *
 * 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,
  POLYNOMIAL,
};

enum SpaceType {
  IMAGE = 0,
  EGO_CAR,
  IPM,
  VEHICLE,
};

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

enum AssociationMethod {
  GREEDY_SEARCH = 0,
  GREEDY_SLIDING_WINDOW,
  GREEDY_GROUP_CONNECT,
};

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()
      : method(AssociationMethod::GREEDY_SEARCH),
        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;
  }

  static bool compare_siz(const LaneInstance &a, const LaneInstance &b) {
    return a.siz > b.siz;
  }

  static bool compare_bound(const LaneInstance &a, const LaneInstance &b) {
    return a.bounds(0) < b.bounds(0);  // x_min
  }

  bool has_overlap(const LaneInstance &a) {
    return a.bounds(0) <= this->bounds(2) && a.bounds(2) >= this->bounds(0);
  }
};

struct CCLanePoint {
  Vector2D pos;
  Vector2D orie;
  ScalarType angle;
  Vector2D image_pos;
  ScalarType confidence;
  int frame_id;
  ScalarType score;
  int point_id;
  Eigen::Matrix<ScalarType, 1, MAX_POLY_ORDER + 1> power_x;

  CCLanePoint()
      : pos(0.0, 0.0),
        orie(1.0, 0.0),
        angle(0.0),
        image_pos(0.0, 0.0),
        confidence(1.0),
        frame_id(-1),
        score(0.0),
        point_id(-1) {
    power_x.setZero();
  }

  static bool compare_score(const CCLanePoint &a, const CCLanePoint &b) {
    return a.score > b.score;
  }
};

struct CCLaneSegment {
  ScalarType start;
  ScalarType end;
  std::vector<std::shared_ptr<CCLanePoint>> points;
  PolyModel model;
  int order;

  CCLaneSegment()
      : start(std::numeric_limits<ScalarType>::max()),
        end(-std::numeric_limits<ScalarType>::max()) {
    model.setZero();
    order = 0;
  }

  void add_point(const std::shared_ptr<CCLanePoint> &p) {
    points.push_back(p);
    start = std::min(start, p->pos(0));
    end = std::max(end, p->pos(0));
  }
};

struct CCLaneTrack {
  SpaceType space_type;
  std::vector<CCLaneSegment> segments;
  size_t tot_point_num;
  Eigen::Matrix<ScalarType, Eigen::Dynamic, 1> y_values;
  ScalarType lateral_dist;
  Bbox bbox;

  CCLaneTrack() : space_type(SpaceType::EGO_CAR) {
    tot_point_num = 0;
    segments.clear();
    lateral_dist = 0.0;
    bbox << std::numeric_limits<ScalarType>::max(),  // x_min
        std::numeric_limits<ScalarType>::max(),      // y_min
        -std::numeric_limits<ScalarType>::max(),     // x_max
        -std::numeric_limits<ScalarType>::max();     // y_max
  }

  Bbox compute_bound() {
    bbox(0) = std::numeric_limits<ScalarType>::max();   // x_min
    bbox(1) = std::numeric_limits<ScalarType>::max();   // y_min
    bbox(2) = -std::numeric_limits<ScalarType>::max();  // x_max
    bbox(3) = -std::numeric_limits<ScalarType>::max();  // y_max

    for (auto it_segment = segments.begin(); it_segment != segments.end();
         ++it_segment) {
      for (size_t i = 0; i < it_segment->points.size(); ++i) {
        bbox(0) = std::min(it_segment->points[i]->pos(0), bbox(0));
        bbox(1) = std::min(it_segment->points[i]->pos(1), bbox(1));
        bbox(2) = std::max(it_segment->points[i]->pos(0), bbox(2));
        bbox(3) = std::max(it_segment->points[i]->pos(1), bbox(3));
      }
    }
    return bbox;
  }

  ScalarType size() const {
    return std::max(bbox(2) - bbox(0), bbox(3) - bbox(1));
  }

  ScalarType x_min() const {
    return bbox(0);
  }
  ScalarType y_min() const {
    return bbox(1);
  }
  ScalarType x_max() const {
    return bbox(2);
  }
  ScalarType y_max() const {
    return bbox(3);
  }
};

typedef std::shared_ptr<CCLaneTrack> CCLaneTrackPtr;
typedef const std::shared_ptr<CCLaneTrack> CCLaneTrackConstPtr;

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);
  }

  std::string get_spatial_label() const {
    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;
  }
  */

  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;

480
    for (size_t i = 0; i < new_lane_object->point_num; ++i) {
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
      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;

// typedef adu::perception::obstacle::transformer_tool::
//         Projector<ScalarType> Projector;

}  // namespace perception
}  // namespace apollo

#endif  // MODULES_PERCEPTION_OBSTACLE_CAMERA_LANE_POST_PROCESS_COMMON_TYPE_H_