Skip to content

Commit

Permalink
点云相关部分待删除
Browse files Browse the repository at this point in the history
  • Loading branch information
guanshoukui committed Mar 7, 2024
1 parent 7fd1530 commit b9bfda8
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/struct_pb/protoc-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ project(protoc-gen-struct_pb)




set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/bin)
find_package(Protobuf QUIET)
if (Protobuf_FOUND)
Expand Down
52 changes: 49 additions & 3 deletions src/struct_pb/protoc-plugin/FileGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ void FileGenerator::generate_shared_header_code(
}


void FileGenerator::generate_pcl_helper(google::protobuf::io2::Printer *p)
{
if (!file_->options().GetExtension(write_pcl_helper)) {
return;
}
p->PrintRaw(
R"(namespace pcl {
template <bool Is_writing_escape, typename Stream>
inline void to_json_impl(Stream& s, const pcl::PointXYZI& t) {
iguana::to_json<Is_writing_escape>(*(float(*)[8]) & t, s);
}
template <typename It, typename Stream>
IGUANA_INLINE void from_json_impl(pcl::PointXYZI& value, It&& it, It&& end) {
iguana::from_json(*(float(*)[8]) & value, it, end);
}
template <bool Is_writing_escape, typename Stream>
inline void to_json_impl(Stream& s, const pcl::PointCloud<pcl::PointXYZI>& t) {
// iguana::to_json<Is_writing_escape>(*(_Scalar(*)[_Row * _Col]) & t, s);
}
template <typename It, typename Stream>
IGUANA_INLINE void from_json_impl(pcl::PointCloud<pcl::PointXYZI>& value, It&& it, It&& end) {
// iguana::from_json(*(_Scalar(*)[_Row * _Col]) & value, it, end);
}
})");

}
void FileGenerator::generate_eigen_helper(google::protobuf::io2::Printer *p)
{
if (!file_->options().GetExtension(write_eigen_helper)) {
Expand Down Expand Up @@ -109,6 +139,7 @@ void FileGenerator::generate_enum_helper(google::protobuf::io2::Printer *p) {

void FileGenerator::generate_header(google::protobuf::io2::Printer *p) {
auto basename = strip_proto(file_->name());
Formatter format(p);
p->Print(
{{"filename", file_->name()},
{"pb_header_file", basename + ".pb.h"}
Expand All @@ -132,6 +163,15 @@ void FileGenerator::generate_header(google::protobuf::io2::Printer *p) {
#include "$pb_header_file$"
)");

if(file_->options().GetExtension(write_pcl_helper))
{
p->PrintRaw(
R"(#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
)");
}


generate_dependency_includes(p); // 包含的其他头文件
generate_ns_open(p); // 写命名空间
generate_shared_header_code(p);
Expand All @@ -146,6 +186,8 @@ void FileGenerator::generate_header(google::protobuf::io2::Printer *p) {

generate_eigen_helper(p);
generate_enum_helper(p);
generate_pcl_helper(p);

p->Print("// clang-format on\n");
}

Expand All @@ -154,11 +196,15 @@ void FileGenerator::generate_fwd_decls(google::protobuf::io2::Printer *p) {
for (int i = 0; i < file_->message_type_count(); ++i) {
auto m = file_->message_type(i);
auto eigen_name = m->options().GetExtension(eigen_typename);
if (eigen_name.empty()) {
format("struct $1$;\n", resolve_keyword(m->name()));
auto pcl_name = m->options().GetExtension(pcl_typename);
if (!eigen_name.empty()) {
format("using $1$ = $2$;\n", resolve_keyword(m->name()), eigen_name);
}
else if (!pcl_name.empty()) {
format("using $1$ = $2$;\n", resolve_keyword(m->name()), pcl_name);
}
else {
format("using $1$ = $2$;\n", resolve_keyword(m->name()), eigen_name);
format("struct $1$;\n", resolve_keyword(m->name()));
}
// std::cerr << " i= " << i << ", name = " << m->name() << "\n";
}
Expand Down
1 change: 1 addition & 0 deletions src/struct_pb/protoc-plugin/FileGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class FileGenerator : public GeneratorBase {
void generate_message_class2struct_source(google::protobuf::io2::Printer *p);
void generate_message_tostring_source(google::protobuf::io2::Printer *p);
void generate_eigen_helper(google::protobuf::io2::Printer *p);
void generate_pcl_helper(google::protobuf::io2::Printer *p);
void generate_enum_helper(google::protobuf::io2::Printer *p);

private:
Expand Down
32 changes: 17 additions & 15 deletions src/struct_pb/protoc-plugin/MessageGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ void MessageGenerator::generate_struct_definition(
if (!d_->options().GetExtension(eigen_typename).empty()) {
return;
}
// added 跳过pcl类型的定义
if (!d_->options().GetExtension(pcl_typename).empty()) {
return;
}

std::string parent = "";
for (int i = 0; i < d_->field_count(); ++i) {
auto fd = d_->field(i);
if (fd->options().GetExtension(inherits_from)) {
parent = std::string(": public ") + fg_map_.get(fd).cpp_type_name();
parent += (i == 0 ? ":" : ",") + std::string("public ") + fg_map_.get(fd).cpp_type_name();
}
}

Expand Down Expand Up @@ -240,23 +244,22 @@ void MessageGenerator::generate_struct_to_class_to(google::protobuf::io2::Printe

if (std::string eigen_name = d_->options().GetExtension(eigen_typename);
!eigen_name.empty()) {
if (eigen_name == "Eigen::MatrixXd") {
if (eigen_name.find("Eigen::Matrix") != std::string::npos) {
format("result.mutable_data()->CopyFrom({in.data(),in.data()+in.size()});""\n");
format("result.set_col(in.cols());\n");
format("result.set_row(in.rows());\n");
}
else if (eigen_name == "Eigen::Vector3d" ||
eigen_name == "Eigen::Vector2d" ||
eigen_name == "Eigen::Vector4d") {
else if (eigen_name.find("Eigen::Vector") != std::string::npos || eigen_name.find("Eigen::Quaternion") != std::string::npos) {
for (int i = 0; i < d_->field_count(); ++i) {
auto f = d_->field(i);
format("result.set_$1$(in[$2$]);\n", f->name(), std::to_string(i));
format("result.set_$1$(in.$1$());\n", f->name());

}
}
else if (eigen_name == "Eigen::Quaterniond") {
else if (eigen_name.find("pcl::") != std::string::npos) {
for (int i = 0; i < d_->field_count(); ++i) {
auto f = d_->field(i);
format("result.set_$1$(in.$1$());\n", f->name());
// format("result.set_$1$(in.$1$());\n", f->name());
}
}
else {
Expand Down Expand Up @@ -286,21 +289,20 @@ void MessageGenerator::generate_class_to_struct_to(google::protobuf::io2::Printe

if (std::string eigen_name = d_->options().GetExtension(eigen_typename);
!eigen_name.empty()) {
if (eigen_name == "Eigen::MatrixXd") {
if (eigen_name.find("Eigen::Matrix") != std::string::npos) {
format("result = Eigen::Map<Eigen::MatrixXd const>(in.data().begin(), in.row(),in.col());\n");
}
else if (eigen_name == "Eigen::Vector3d" ||
eigen_name == "Eigen::Vector2d" ||
eigen_name == "Eigen::Vector4d") {
else if (eigen_name.find("Eigen::Vector") != std::string::npos || eigen_name.find("Eigen::Quaternion") != std::string::npos) {
for (int i = 0; i < d_->field_count(); ++i) {
auto f = d_->field(i);
format("result[$1$] = in.$2$();\n", std::to_string(i), f->name());
// format("result[$1$] = in.$2$();\n", std::to_string(i), f->name());
format("result.$1$() = in.$1$();\n", f->name());
}
}
else if (eigen_name == "Eigen::Quaterniond") {
else if (eigen_name.find("pcl::") != std::string::npos) {
for (int i = 0; i < d_->field_count(); ++i) {
auto f = d_->field(i);
format("result.$1$() = in.$1$();\n", f->name());
// format("result.$1$() = in.$1$();\n", f->name());
}
}
else {
Expand Down
27 changes: 27 additions & 0 deletions src/struct_pb/protoc-plugin/PrimitiveFieldGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,32 @@ void RepeatedPrimitiveFieldGenerator::generate_deserialization_unpacked_only(
PrimitiveFieldGenerator g(d_, options_);
g.generate_deserialization_only(p, output);
}

// added
void RepeatedPrimitiveFieldGenerator::generate_struct_to_class(
google::protobuf::io2::Printer *p) const {
Formatter format(p);
auto v_class_name = "result.mutable_" + name() + "()";
auto v_struct_name = "in." + name();
int size = d_->options().GetExtension(fix_size);
std::string size_str = size > 0 ? std::to_string(size) : (v_struct_name + ".size()");
p->Print({{"result", v_class_name}, {"in", v_struct_name}, {"cpp_name", cpp_type_name()}, {"size",size_str}},
R"($result$->Reserve($size$);
for(int i = 0;i < $size$;++i) { *$result$->Mutable(i) = $in$[i];}
)");
}
void RepeatedPrimitiveFieldGenerator::generate_class_to_struct(
google::protobuf::io2::Printer *p) const {
auto v_class_name = "in." + name() + "";
auto v_struct_name = "result." + name();
int size = d_->options().GetExtension(fix_size);
std::string size_str = size > 0 ? std::to_string(size) : (v_class_name + "().size()");

p->Print({{"in", v_class_name}, {"result", v_struct_name},{"cpp_name", cpp_type_name()},{"size",size_str}},
R"(for(int i = 0;i < $size$;++i) {$result$[i] = $in$(i);}
)");

}

} // namespace compiler
} // namespace struct_pb
4 changes: 3 additions & 1 deletion src/struct_pb/protoc-plugin/PrimitiveFieldGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class RepeatedPrimitiveFieldGenerator : public FieldGenerator {
const std::string &max_size) const;
void generate_deserialization_unpacked_only(google::protobuf::io2::Printer *p,
const std::string &output) const;

// added
void generate_struct_to_class(google::protobuf::io2::Printer *p) const override;
void generate_class_to_struct(google::protobuf::io2::Printer *p) const override;
private:
bool is_packed() const;
std::string packed_tag() const;
Expand Down
9 changes: 8 additions & 1 deletion src/struct_pb/protoc-plugin/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/examples)

include(../cmake/struct_pb.cmake)

cmake_policy(SET CMP0075 NEW)
find_package(PCL REQUIRED package common)


if (Protobuf_FOUND)

# generate .pb.cc .pb.h
Expand All @@ -19,15 +23,17 @@ if (Protobuf_FOUND)
state.proto
chasis.proto
perception.proto
# pointcloud.proto
)
add_executable(struct_pb_tutorial tutorial.cpp
${PROTO_SRCS}
${PROTO_HDRS})
target_link_libraries(struct_pb_tutorial protobuf::libprotobuf)
target_link_libraries(struct_pb_tutorial protobuf::libprotobuf pcl_common)

target_include_directories(struct_pb_tutorial PUBLIC
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_LIST_DIR}
${PCL_INCLUDE_DIRS}
)

target_protos_struct_pb(struct_pb_tutorial PRIVATE
Expand All @@ -38,6 +44,7 @@ if (Protobuf_FOUND)
state.proto
chasis.proto
perception.proto
# pointcloud.proto
OPTION "namespace=inner_struct,converter_namespace=converter")

endif()
30 changes: 24 additions & 6 deletions src/struct_pb/protoc-plugin/example/base.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,27 @@ message ZFrame
string channel_name = 5;
}

message Vector4f
{
option(eigen_typename) = "Eigen::Vector4f";

float x = 1;
float y = 2;
float z = 3;
float w = 4;
}

message Point4D
message Vector4d
{
option(eigen_typename) = "Eigen::Vector4d";

double x = 1;
double y = 2;
double z = 3;
double i = 4;
double w = 4;
}

message Point3D
message Vector3d
{
option(eigen_typename) = "Eigen::Vector3d";

Expand All @@ -84,15 +93,15 @@ message Point3D
double z = 3;
}

message Point2D
message Vector2d
{
option(eigen_typename) = "Eigen::Vector2d";

double x = 1;
double y = 2;
}

message Quaternion {
message Quaterniond {
option(eigen_typename) = "Eigen::Quaterniond";

double w = 1;
Expand All @@ -101,14 +110,23 @@ message Quaternion {
double z = 4;
}

message Quaternionf {
option(eigen_typename) = "Eigen::Quaternionf";

float w = 1;
float x = 2;
float y = 3;
float z = 4;
}

message PointLLH {
double lon = 1;
double lat = 2;
double height = 3;
}

// hard code, never edit
message Matrix
message MatrixXd
{
option(eigen_typename) = "Eigen::MatrixXd";

Expand Down
4 changes: 2 additions & 2 deletions src/struct_pb/protoc-plugin/example/chasis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import "proto_to_struct.proto";
message ZChassis
{
ZFrame header = 1 [(inherits_from) = true];
Point4D vel = 2;
Point4D spd_status = 3;
Vector4d vel = 2;
Vector4d spd_status = 3;
double steering_percentage = 4;
double brake_percentage = 5;
bool parking_brake = 6;
Expand Down
12 changes: 6 additions & 6 deletions src/struct_pb/protoc-plugin/example/gnss.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ message ZGnss
ZFrame header = 1 [(inherits_from) = true];;
int32 msg_type = 2;
double hdop = 3;
Point3D pos = 4;
Vector3d pos = 4;
int32 status = 5;
int32 sat_num = 6;
double rtk_age = 7;
Point3D std_pos = 8;
Point3D vel = 9;
Vector3d std_pos = 8;
Vector3d vel = 9;
double azi_track = 10;
double speed = 11;
Point3D std_vel = 12;
Point2D dual_antenna_angle =13;
Point2D dual_antenna_std = 14;
Vector3d std_vel = 12;
Vector2d dual_antenna_angle =13;
Vector2d dual_antenna_std = 14;
int32 heading_status = 15;
int32 heading_sat_num = 16;
}
4 changes: 2 additions & 2 deletions src/struct_pb/protoc-plugin/example/imu.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import "proto_to_struct.proto";
message ZImu
{
ZFrame header = 1 [(inherits_from) = true];
Point3D acc = 2; // 加速度 g
Point3D gyr = 3; // 角速度 rad/s
Vector3d acc = 2; // 加速度 g
Vector3d gyr = 3; // 角速度 rad/s
double temper = 4; // 温度 ℃
}

Expand Down
4 changes: 2 additions & 2 deletions src/struct_pb/protoc-plugin/example/perception.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ message ZPercBase
int32 track_id = 1;
string id = 2; // 需要单帧独立
float object_confidence = 3;
repeated Point3D points = 4;
repeated Point3D points_confidence = 5;
repeated Vector3d points = 4;
repeated Vector3d points_confidence = 5;
}

message ZPercLine
Expand Down
Loading

0 comments on commit b9bfda8

Please sign in to comment.