From fb0322f5da0a8c1a1a61aa86bd368beab05d51b3 Mon Sep 17 00:00:00 2001 From: Jiangtao Hu Date: Sun, 24 Dec 2017 14:48:58 -0800 Subject: [PATCH] map: replace CHECK with log error handling. --- modules/common/log.h | 24 ++++++++++++++++ modules/map/hdmap/hdmap_common.cc | 47 ++++++++++++++++++------------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/modules/common/log.h b/modules/common/log.h index d4f0fc97f9..360bd62a4e 100644 --- a/modules/common/log.h +++ b/modules/common/log.h @@ -40,4 +40,28 @@ #define AWARN_EVERY(freq) LOG_EVERY_N(WARNING, freq) #define AERROR_EVERY(freq) LOG_EVERY_N(ERROR, freq) +#define RETURN_IF_NULL(ptr) \ + if (ptr == nullptr) { \ + AWARN << #ptr << " is nullptr."; \ + return; \ + } + +#define RETURN_VAL_IF_NULL(ptr, val) \ + if (ptr == nullptr) { \ + AWARN << #ptr << " is nullptr."; \ + return val; \ + } + +#define RETURN_IF(condition) \ + if (condition) { \ + AWARN << #condition << " is not met."; \ + return; \ + } + +#define RETURN_VAL_IF(condition, val) \ + if (condition) { \ + AWARN << #condition << " is not met."; \ + return val; \ + } + #endif // MODULES_COMMON_LOG_H_ diff --git a/modules/map/hdmap/hdmap_common.cc b/modules/map/hdmap/hdmap_common.cc index 296c23ebae..ad60e0b2e2 100644 --- a/modules/map/hdmap/hdmap_common.cc +++ b/modules/map/hdmap/hdmap_common.cc @@ -41,7 +41,7 @@ const double kDuplicatedPointsEpsilon = 1e-7; const double kEpsilon = 0.1; void RemoveDuplicates(std::vector *points) { - CHECK_NOTNULL(points); + RETURN_IF_NULL(points); int count = 0; const double limit = kDuplicatedPointsEpsilon * kDuplicatedPointsEpsilon; @@ -54,7 +54,8 @@ void RemoveDuplicates(std::vector *points) { } void PointsFromCurve(const Curve &input_curve, std::vector *points) { - CHECK_NOTNULL(points)->clear(); + RETURN_IF_NULL(points); + points->clear(); for (const auto &curve : input_curve.segment()) { if (curve.has_line_segment()) { @@ -62,7 +63,7 @@ void PointsFromCurve(const Curve &input_curve, std::vector *points) { points->emplace_back(point.x(), point.y()); } } else { - LOG(FATAL) << "Can not handle curve type."; + AERROR << "Can not handle curve type."; } } RemoveDuplicates(points); @@ -86,7 +87,7 @@ apollo::common::math::Polygon2d ConvertToPolygon2d(const Polygon &polygon) { void SegmentsFromCurve( const Curve &curve, std::vector *segments) { - CHECK_NOTNULL(segments); + RETURN_IF_NULL(segments); std::vector points; PointsFromCurve(curve, &points); @@ -188,10 +189,15 @@ void LaneInfo::GetWidth(const double s, double *left_width, double LaneInfo::Heading(const double s) const { const double kEpsilon = 0.001; - CHECK(s + kEpsilon >= accumulated_s_.front()) - << "s:" << s << " should be >= " << accumulated_s_.front(); - CHECK(s - kEpsilon <= accumulated_s_.back()) - << "s:" << s << " should be <= " << accumulated_s_.back(); + if (s + kEpsilon < accumulated_s_.front()) { + AERROR << "s:" << s << " should be >= " << accumulated_s_.front(); + return 0.0; + } + if (s - kEpsilon > accumulated_s_.back()) { + AERROR << "s:" << s << " should be <= " << accumulated_s_.back(); + return 0.0; + } + auto iter = std::lower_bound(accumulated_s_.begin(), accumulated_s_.end(), s); int index = std::distance(accumulated_s_.begin(), iter); if (index == 0 || *iter - s <= common::math::kMathEpsilon) { @@ -199,7 +205,6 @@ double LaneInfo::Heading(const double s) const { } else { return common::math::slerp(headings_[index - 1], accumulated_s_[index - 1], headings_[index], accumulated_s_[index], s); - // return headings_[index - 1]; } } @@ -277,7 +282,8 @@ bool LaneInfo::IsOnLane(const apollo::common::math::Box2d &box) const { } PointENU LaneInfo::GetSmoothPoint(double s) const { - CHECK_GE(points_.size(), 2); + PointENU point; + RETURN_VAL_IF(points_.size() < 2, point); if (s <= 0.0) { return PointFromVec2d(points_[0]); } @@ -288,7 +294,7 @@ PointENU LaneInfo::GetSmoothPoint(double s) const { const auto low_itr = std::lower_bound(accumulated_s_.begin(), accumulated_s_.end(), s); - CHECK(low_itr != accumulated_s_.end()); + RETURN_VAL_IF(low_itr == accumulated_s_.end(), point); size_t index = low_itr - accumulated_s_.begin(); double delta_s = *low_itr - s; if (delta_s < apollo::common::math::kMathEpsilon) { @@ -302,18 +308,18 @@ PointENU LaneInfo::GetSmoothPoint(double s) const { double LaneInfo::DistanceTo(const Vec2d &point) const { const auto segment_box = lane_segment_kdtree_->GetNearestObject(point); - CHECK(segment_box != nullptr); + RETURN_VAL_IF_NULL(segment_box, 0.0); return segment_box->DistanceTo(point); } double LaneInfo::DistanceTo(const Vec2d &point, Vec2d *map_point, double *s_offset, int *s_offset_index) const { - CHECK_NOTNULL(map_point); - CHECK_NOTNULL(s_offset); - CHECK_NOTNULL(s_offset_index); + RETURN_VAL_IF_NULL(map_point, 0.0); + RETURN_VAL_IF_NULL(s_offset, 0.0); + RETURN_VAL_IF_NULL(s_offset_index, 0.0); const auto segment_box = lane_segment_kdtree_->GetNearestObject(point); - CHECK(segment_box != nullptr); + RETURN_VAL_IF_NULL(segment_box, 0.0); int index = segment_box->id(); double distance = segments_[index].DistanceTo(point, map_point); *s_offset_index = index; @@ -323,10 +329,11 @@ double LaneInfo::DistanceTo(const Vec2d &point, Vec2d *map_point, } PointENU LaneInfo::GetNearestPoint(const Vec2d &point, double *distance) const { - CHECK_NOTNULL(distance); + PointENU empty_point; + RETURN_VAL_IF_NULL(distance, empty_point); const auto segment_box = lane_segment_kdtree_->GetNearestObject(point); - CHECK(segment_box != nullptr); + RETURN_VAL_IF_NULL(segment_box, empty_point); int index = segment_box->id(); Vec2d nearest_point; *distance = segments_[index].DistanceTo(point, &nearest_point); @@ -336,8 +343,8 @@ PointENU LaneInfo::GetNearestPoint(const Vec2d &point, double *distance) const { bool LaneInfo::GetProjection(const Vec2d &point, double *accumulate_s, double *lateral) const { - CHECK_NOTNULL(accumulate_s); - CHECK_NOTNULL(lateral); + RETURN_VAL_IF_NULL(accumulate_s, false); + RETURN_VAL_IF_NULL(lateral, false); if (segments_.empty()) { return false; -- GitLab