提交 ec5f9f09 编写于 作者: A Aaron Xiao 提交者: Dong Li

Add helper function to print DebugStrings.

上级 0ff0b6a1
...@@ -19,12 +19,11 @@ ...@@ -19,12 +19,11 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include <sstream>
#include <utility> #include <utility>
#include "modules/common/log.h" #include "modules/common/log.h"
#include "modules/common/math/math_utils.h" #include "modules/common/math/math_utils.h"
#include "modules/common/util/string_util.h"
namespace apollo { namespace apollo {
namespace common { namespace common {
...@@ -579,15 +578,10 @@ Polygon2d Polygon2d::ExpandByDistance(const double distance) const { ...@@ -579,15 +578,10 @@ Polygon2d Polygon2d::ExpandByDistance(const double distance) const {
} }
std::string Polygon2d::DebugString() const { std::string Polygon2d::DebugString() const {
std::ostringstream sout; return util::StrCat(
sout << "polygon2d ( num_points = " << num_points_ << " points = ("; "polygon2d ( num_points = ", num_points_,
for (const auto &pt : points_) { " points = (", util::PrintDebugStringIter(points_), " ) ",
sout << " " << pt.DebugString(); is_convex_ ? "convex" : "non-convex", " area = ", area_, " )");
}
sout << " ) " << (is_convex_ ? "convex" : "non-convex")
<< " area = " << area_ << " )";
sout.flush();
return sout.str();
} }
} // namespace math } // namespace math
......
...@@ -54,7 +54,7 @@ bool ProjectByXSlow(const std::vector<Vec2d> &points, double x, ...@@ -54,7 +54,7 @@ bool ProjectByXSlow(const std::vector<Vec2d> &points, double x,
TEST(Polygon2dTest, polygon_IsPointIn) { TEST(Polygon2dTest, polygon_IsPointIn) {
const Polygon2d poly1(Box2d::CreateAABox({0, 0}, {1, 1})); const Polygon2d poly1(Box2d::CreateAABox({0, 0}, {1, 1}));
EXPECT_EQ(poly1.DebugString(), EXPECT_EQ(poly1.DebugString(),
"polygon2d ( num_points = 4 points = ( vec2d ( x = 1 y = 0 ) " "polygon2d ( num_points = 4 points = (vec2d ( x = 1 y = 0 ) "
"vec2d ( x = 1 y = 1 ) vec2d ( x = 0 y = 1 ) vec2d ( x = 0 y = " "vec2d ( x = 1 y = 1 ) vec2d ( x = 0 y = 1 ) vec2d ( x = 0 y = "
"0 ) ) convex area = 1 )"); "0 ) ) convex area = 1 )");
EXPECT_TRUE(poly1.IsPointIn({0.5, 0.5})); EXPECT_TRUE(poly1.IsPointIn({0.5, 0.5}));
......
...@@ -98,6 +98,23 @@ internal::IterPrinter<T*> PrintIter( ...@@ -98,6 +98,23 @@ internal::IterPrinter<T*> PrintIter(
return {array, end, delimiter}; return {array, end, delimiter};
} }
/**
* @brief Make conatiners and iterators printable. Similar to PrintIter but
* output the DebugString().
*/
template <typename Container,
typename Iter = typename Container::const_iterator>
internal::DebugStringIterPrinter<Iter> PrintDebugStringIter(
const Container& container, const std::string& delimiter = " ") {
return {container.begin(), container.end(), delimiter};
}
template <typename Iter>
internal::DebugStringIterPrinter<Iter> PrintDebugStringIter(
const Iter& begin, const Iter& end, const std::string& delimiter = " ") {
return {begin, end, delimiter};
}
} // namespace util } // namespace util
} // namespace common } // namespace common
...@@ -109,6 +126,13 @@ std::ostream& operator<<( ...@@ -109,6 +126,13 @@ std::ostream& operator<<(
return printer.Print(os); return printer.Print(os);
} }
template <typename Iter>
std::ostream& operator<<(
std::ostream& os,
const common::util::internal::DebugStringIterPrinter<Iter>& printer) {
return printer.Print(os);
}
} // namespace apollo } // namespace apollo
#endif // MODULES_COMMON_STRING_UTIL_H_ #endif // MODULES_COMMON_STRING_UTIL_H_
...@@ -61,6 +61,34 @@ class IterPrinter { ...@@ -61,6 +61,34 @@ class IterPrinter {
const std::string& delimiter_; const std::string& delimiter_;
}; };
/**
* @brief Iterator printer which output iter->DebugString().
*/
template <typename Iter>
class DebugStringIterPrinter {
public:
DebugStringIterPrinter(const Iter& begin, const Iter& end,
const std::string& delimiter)
: begin_(begin), end_(end), delimiter_(delimiter) {}
std::ostream& Print(std::ostream& os) const {
for (Iter it = begin_; it != end_; ++it) {
// Print first item without delimiter.
if (it == begin_) {
os << it->DebugString();
} else {
os << delimiter_ << it->DebugString();
}
}
return os;
}
private:
const Iter begin_;
const Iter end_;
const std::string& delimiter_;
};
} // namespace internal } // namespace internal
} // namespace util } // namespace util
} // namespace common } // namespace common
......
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
#include "modules/planning/common/path/path_data.h" #include "modules/planning/common/path/path_data.h"
#include <algorithm> #include <algorithm>
#include <sstream>
#include "modules/common/log.h" #include "modules/common/log.h"
#include "modules/common/util/string_util.h"
#include "modules/planning/common/planning_gflags.h" #include "modules/planning/common/planning_gflags.h"
#include "modules/planning/common/planning_util.h" #include "modules/planning/common/planning_util.h"
#include "modules/planning/math/double.h" #include "modules/planning/math/double.h"
...@@ -96,21 +96,16 @@ void PathData::Clear() { ...@@ -96,21 +96,16 @@ void PathData::Clear() {
} }
std::string PathData::DebugString() const { std::string PathData::DebugString() const {
std::ostringstream sout;
sout << "[" << std::endl;
const auto &path_points = discretized_path_.points(); const auto &path_points = discretized_path_.points();
for (std::size_t i = 0; const auto limit = std::min(
i < path_points.size() && path_points.size(),
i < static_cast<std::size_t>(FLAGS_trajectory_point_num_for_debug); static_cast<size_t>(FLAGS_trajectory_point_num_for_debug));
++i) {
if (i > 0) { return apollo::common::util::StrCat(
sout << "," << std::endl; "[\n",
} apollo::common::util::PrintDebugStringIter(
sout << path_points[i].DebugString(); path_points.begin(), path_points.begin() + limit, ",\n"),
} "]\n");
sout << "]" << std::endl;
sout.flush();
return sout.str();
} }
} // namespace planning } // namespace planning
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <utility> #include <utility>
#include "modules/common/util/string_util.h"
#include "modules/planning/common/path/path_data.h" #include "modules/planning/common/path/path_data.h"
#include "modules/planning/proto/planning.pb.h" #include "modules/planning/proto/planning.pb.h"
...@@ -94,7 +95,6 @@ bool PlanningData::aggregate(const double time_resolution, ...@@ -94,7 +95,6 @@ bool PlanningData::aggregate(const double time_resolution,
if (!path_data_.get_path_point_with_path_s(speed_point.s(), &path_point)) { if (!path_data_.get_path_point_with_path_s(speed_point.s(), &path_point)) {
AERROR << "Fail to get path data with s " << speed_point.s() AERROR << "Fail to get path data with s " << speed_point.s()
<< "path total length " << path_data_.discretized_path().length(); << "path total length " << path_data_.discretized_path().length();
;
return false; return false;
} }
...@@ -110,10 +110,9 @@ bool PlanningData::aggregate(const double time_resolution, ...@@ -110,10 +110,9 @@ bool PlanningData::aggregate(const double time_resolution,
} }
std::string PlanningData::DebugString() const { std::string PlanningData::DebugString() const {
std::ostringstream ss; return apollo::common::util::StrCat(
ss << "path_data:" << path_data_.DebugString(); "path_data:", path_data_.DebugString(),
ss << "speed_data:" << speed_data_.DebugString(); "speed_data:", speed_data_.DebugString());
return ss.str();
} }
} // namespace planning } // namespace planning
......
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
#include "modules/planning/common/speed/speed_data.h" #include "modules/planning/common/speed/speed_data.h"
#include <algorithm> #include <algorithm>
#include <sstream>
#include <utility> #include <utility>
#include "modules/common/util/string_util.h"
#include "modules/planning/common/planning_gflags.h" #include "modules/planning/common/planning_gflags.h"
#include "modules/planning/common/planning_util.h" #include "modules/planning/common/planning_util.h"
#include "modules/planning/math/double.h" #include "modules/planning/math/double.h"
...@@ -93,19 +93,14 @@ void SpeedData::Clear() { ...@@ -93,19 +93,14 @@ void SpeedData::Clear() {
} }
std::string SpeedData::DebugString() const { std::string SpeedData::DebugString() const {
std::ostringstream sout; const auto limit = std::min(
sout << "[" << std::endl; speed_vector_.size(),
for (int i = 0; i < static_cast<int>(speed_vector_.size()) && static_cast<size_t>(FLAGS_trajectory_point_num_for_debug));
i < FLAGS_trajectory_point_num_for_debug; return apollo::common::util::StrCat(
++i) { "[\n",
if (i > 0) { apollo::common::util::PrintDebugStringIter(
sout << "," << std::endl; speed_vector_.begin(), speed_vector_.begin() + limit, ",\n"),
} "]\n");
sout << speed_vector_[i].DebugString();
}
sout << "]" << std::endl;
sout.flush();
return sout.str();
} }
std::uint32_t SpeedData::find_index(const double t) const { std::uint32_t SpeedData::find_index(const double t) const {
......
...@@ -16,6 +16,7 @@ cc_library( ...@@ -16,6 +16,7 @@ cc_library(
"//modules/common:log", "//modules/common:log",
"//modules/common/math", "//modules/common/math",
"//modules/common/proto:path_point_proto", "//modules/common/proto:path_point_proto",
"//modules/common/util:string_util",
"//modules/map/pnc_map:pnc_path", "//modules/map/pnc_map:pnc_path",
"//modules/planning/common:planning_gflags", "//modules/planning/common:planning_gflags",
"//modules/planning/math:double", "//modules/planning/math:double",
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include "modules/planning/reference_line/reference_line.h" #include "modules/planning/reference_line/reference_line.h"
#include <algorithm> #include <algorithm>
#include <sstream>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
...@@ -31,6 +30,7 @@ ...@@ -31,6 +30,7 @@
#include "modules/common/log.h" #include "modules/common/log.h"
#include "modules/common/math/angle.h" #include "modules/common/math/angle.h"
#include "modules/common/math/linear_interpolation.h" #include "modules/common/math/linear_interpolation.h"
#include "modules/common/util/string_util.h"
#include "modules/planning/common/planning_gflags.h" #include "modules/planning/common/planning_gflags.h"
#include "modules/planning/math/double.h" #include "modules/planning/math/double.h"
...@@ -244,14 +244,13 @@ bool ReferenceLine::is_on_road(const common::SLPoint& sl_point) const { ...@@ -244,14 +244,13 @@ bool ReferenceLine::is_on_road(const common::SLPoint& sl_point) const {
} }
std::string ReferenceLine::DebugString() const { std::string ReferenceLine::DebugString() const {
std::ostringstream ss; const auto limit = std::min(
ss << "point num:" << reference_points_.size(); reference_points_.size(),
for (int32_t i = 0; i < static_cast<int32_t>(reference_points_.size()) && static_cast<size_t>(FLAGS_trajectory_point_num_for_debug));
i < FLAGS_trajectory_point_num_for_debug; return apollo::common::util::StrCat(
++i) { "point num:", reference_points_.size(),
ss << reference_points_[i].DebugString(); apollo::common::util::PrintDebugStringIter(
} reference_points_.begin(), reference_points_.begin() + limit, ""));
return ss.str();
} }
} // namespace planning } // namespace planning
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册