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

Add helper function to print DebugStrings.

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