Skip to content

Commit

Permalink
fixed save point cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
jian-dong committed Sep 15, 2023
1 parent e0d8263 commit 4e2e201
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
5 changes: 4 additions & 1 deletion include/orbbec_camera/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ void savePointsToPly(std::shared_ptr<ob::Frame> frame, const std::string &fileNa

void saveRGBPointsToPly(std::shared_ptr<ob::Frame> frame, const std::string &fileName);

void soavePointCloudMsgToPly(const sensor_msgs::PointCloud2 &msg, const std::string &fileName);
void saveRGBPointCloudMsgToPly(const sensor_msgs::PointCloud2 &msg, const std::string &fileName);

void saveDepthPointCloudMsgToPly(const sensor_msgs::PointCloud2 &msg, const std::string &fileName);


tf2::Quaternion rotationMatrixToQuaternion(const float rotation[9]);

Expand Down
4 changes: 2 additions & 2 deletions src/ob_camera_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void OBCameraNode::publishDepthPointCloud(const std::shared_ptr<ob::FrameSet>& f
boost::filesystem::create_directory(current_path + "/point_cloud");
}
ROS_INFO_STREAM("Saving point cloud to " << filename);
soavePointCloudMsgToPly(cloud_msg_, filename);
saveDepthPointCloudMsgToPly(cloud_msg_, filename);
}
}

Expand Down Expand Up @@ -601,7 +601,7 @@ void OBCameraNode::publishColoredPointCloud(const std::shared_ptr<ob::FrameSet>&
boost::filesystem::create_directory(current_path + "/point_cloud");
}
ROS_INFO_STREAM("Saving point cloud to " << filename);
soavePointCloudMsgToPly(cloud_msg_, filename);
saveRGBPointCloudMsgToPly(cloud_msg_, filename);
}
}

Expand Down
40 changes: 39 additions & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void saveRGBPointsToPly(std::shared_ptr<ob::Frame> frame, const std::string &fil
fclose(fp);
}

void soavePointCloudMsgToPly(const sensor_msgs::PointCloud2 &msg, const std::string &fileName) {
void saveRGBPointCloudMsgToPly(const sensor_msgs::PointCloud2 &msg, const std::string &fileName) {
FILE *fp = fopen(fileName.c_str(), "wb+");
CHECK_NOTNULL(fp);

Expand Down Expand Up @@ -267,6 +267,44 @@ void soavePointCloudMsgToPly(const sensor_msgs::PointCloud2 &msg, const std::str
fclose(fp);
}

void saveDepthPointCloudMsgToPly(const sensor_msgs::PointCloud2 &msg, const std::string &fileName) {
FILE *fp = fopen(fileName.c_str(), "wb+");
CHECK_NOTNULL(fp);
sensor_msgs::PointCloud2ConstIterator<float> iter_x(msg, "x");
sensor_msgs::PointCloud2ConstIterator<float> iter_y(msg, "y");
sensor_msgs::PointCloud2ConstIterator<float> iter_z(msg, "z");

// First, count the actual number of valid points
size_t valid_points = 0;
for (; iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z) {
if (!std::isnan(*iter_x) && !std::isnan(*iter_y) && !std::isnan(*iter_z)) {
++valid_points;
}
}

// Reset the iterators
iter_x = sensor_msgs::PointCloud2ConstIterator<float>(msg, "x");
iter_y = sensor_msgs::PointCloud2ConstIterator<float>(msg, "y");
iter_z = sensor_msgs::PointCloud2ConstIterator<float>(msg, "z");

fprintf(fp, "ply\n");
fprintf(fp, "format ascii 1.0\n");
fprintf(fp, "element vertex %zu\n", valid_points);
fprintf(fp, "property float x\n");
fprintf(fp, "property float y\n");
fprintf(fp, "property float z\n");
fprintf(fp, "end_header\n");

for (; iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z) {
if (!std::isnan(*iter_x) && !std::isnan(*iter_y) && !std::isnan(*iter_z)) {
fprintf(fp, "%.3f %.3f %.3f\n", *iter_x, *iter_y, *iter_z);
}
}

fflush(fp);
fclose(fp);
}

void savePointsToPly(std::shared_ptr<ob::Frame> frame, const std::string &fileName) {
size_t point_size = frame->dataSize() / sizeof(OBPoint);
FILE *fp = fopen(fileName.c_str(), "wb+");
Expand Down

0 comments on commit 4e2e201

Please sign in to comment.