-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hao Guo <[email protected]>
- Loading branch information
1 parent
bd7071a
commit fbf2e14
Showing
8 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) | ||
project(kdtree_search) | ||
find_package(PCL 1.2 REQUIRED) | ||
include_directories(${PCL_INCLUDE_DIRS}) | ||
link_directories(${PCL_LIBRARY_DIRS}) | ||
add_definitions(${PCL_DEFINITIONS}) | ||
add_executable(kdtree_search kdtree_search.cpp) | ||
target_link_libraries(kdtree_search ${PCL_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <pcl/point_cloud.h> | ||
#include <pcl/kdtree/kdtree_flann.h> | ||
#include <iostream> | ||
#include <vector> | ||
#include <ctime> | ||
|
||
int main (int argc, char**argv) | ||
{ | ||
srand (time (NULL)); | ||
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); | ||
//点云生成 | ||
cloud->width =1000; | ||
cloud->height =1; | ||
cloud->points.resize (cloud->width * cloud->height); | ||
for (size_t i=0; i< cloud->points.size (); ++i) | ||
{ | ||
cloud->points[i].x =1024.0f* rand () / (RAND_MAX +1.0f); | ||
cloud->points[i].y =1024.0f* rand () / (RAND_MAX +1.0f); | ||
cloud->points[i].z =1024.0f* rand () / (RAND_MAX +1.0f); | ||
} | ||
pcl::KdTreeFLANN<pcl::PointXYZ>kdtree; | ||
kdtree.setInputCloud (cloud); | ||
pcl::PointXYZ searchPoint; | ||
searchPoint.x=1024.0f* rand () / (RAND_MAX +1.0f); | ||
searchPoint.y=1024.0f* rand () / (RAND_MAX +1.0f); | ||
searchPoint.z=1024.0f* rand () / (RAND_MAX +1.0f); | ||
// k近邻搜索 | ||
int K =10; | ||
std::vector<int>pointIdxNKNSearch(K); | ||
std::vector<float>pointNKNSquaredDistance(K); | ||
std::cout<<"K nearest neighbor search at ("<<searchPoint.x | ||
<<" "<<searchPoint.y | ||
<<" "<<searchPoint.z | ||
<<") with K="<< K <<std::endl; | ||
if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) >0 ) | ||
{ | ||
for (size_t i=0; i<pointIdxNKNSearch.size (); ++i) | ||
std::cout<<" "<< cloud->points[ pointIdxNKNSearch[i] ].x | ||
<<" "<< cloud->points[pointIdxNKNSearch[i] ].y | ||
<<" "<< cloud->points[pointIdxNKNSearch[i] ].z | ||
<<" (squared distance: "<<pointNKNSquaredDistance[i] <<")"<<std::endl; | ||
} | ||
// 在半径r内搜索近邻 | ||
std::vector<int> pointIdxRadiusSearch; | ||
std::vector<float> pointRadiusSquaredDistance; | ||
float radius =256.0f* rand () / (RAND_MAX +1.0f); | ||
std::cout<<"Neighbors within radius search at ("<<searchPoint.x | ||
<<" "<<searchPoint.y | ||
<<" "<<searchPoint.z | ||
<<") with radius="<< radius <<std::endl; | ||
if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) >0 ) | ||
{ | ||
for (size_t i=0; i<pointIdxRadiusSearch.size (); ++i) | ||
std::cout<<" "<< cloud->points[ pointIdxRadiusSearch[i] ].x | ||
<<" "<< cloud->points[pointIdxRadiusSearch[i] ].y | ||
<<" "<< cloud->points[pointIdxRadiusSearch[i] ].z | ||
<<" (squared distance: "<<pointRadiusSquaredDistance[i] <<")"<<std::endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) | ||
project(point_cloud_compression) | ||
find_package(PCL 1.2 REQUIRED) | ||
include_directories(${PCL_INCLUDE_DIRS}) | ||
link_directories(${PCL_LIBRARY_DIRS}) | ||
add_definitions(${PCL_DEFINITIONS}) | ||
add_executable(point_cloud_compression point_cloud_compression.cpp) | ||
target_link_libraries(point_cloud_compression ${PCL_LIBRARIES}) |
78 changes: 78 additions & 0 deletions
78
第四章/2 point_cloud_compression/source/point_cloud_compression.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <pcl/point_cloud.h> | ||
#include <pcl/point_types.h> | ||
#include <pcl/io/openni_grabber.h> | ||
#include <pcl/visualization/cloud_viewer.h> | ||
#include <pcl/compression/octree_pointcloud_compression.h> | ||
#include <stdio.h> | ||
#include <sstream> | ||
#include <stdlib.h> | ||
|
||
#ifdef WIN32 | ||
# define sleep(x) Sleep((x)*1000) | ||
#endif | ||
|
||
class SimpleOpenNIViewer | ||
{ | ||
public: | ||
SimpleOpenNIViewer () : | ||
viewer (" Point Cloud Compression Example") | ||
{ | ||
} | ||
|
||
void | ||
cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr& cloud) | ||
{ | ||
if (!viewer.wasStopped ()) | ||
{ | ||
// 存储压缩点云的字节流 | ||
std::stringstream compressedData; | ||
// 输出点云 | ||
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloudOut (new pcl::PointCloud<pcl::PointXYZRGBA> ()); | ||
// 压缩点云 | ||
PointCloudEncoder->encodePointCloud (cloud, compressedData); | ||
// 解压缩点云 | ||
PointCloudDecoder->decodePointCloud (compressedData, cloudOut); | ||
//可视化解压缩点云 | ||
viewer.showCloud (cloudOut); | ||
} | ||
} | ||
|
||
void | ||
run () | ||
{ | ||
bool showStatistics=true; | ||
// 压缩选项详见 /io/include/pcl/compression/compression_profiles.h | ||
pcl::octree::compression_Profiles_e compressionProfile=pcl::octree::MED_RES_ONLINE_COMPRESSION_WITH_COLOR; | ||
// 初始化压缩与解压缩对象 | ||
PointCloudEncoder=new pcl::octree::PointCloudCompression<pcl::PointXYZRGBA> (compressionProfile, showStatistics); | ||
PointCloudDecoder=new pcl::octree::PointCloudCompression<pcl::PointXYZRGBA> (); | ||
//创建从 OpenNI获取点云的对象 | ||
pcl::Grabber* interface =new pcl::OpenNIGrabber (); | ||
//建立回调函数 | ||
boost::function<void | ||
(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1); | ||
// 建立回调函数与回调信号之间联系 | ||
boost::signals2::connection c = interface->registerCallback (f); | ||
// 开始接收点云数据流 | ||
interface->start (); | ||
while (!viewer.wasStopped ()) | ||
{ | ||
sleep (1); | ||
} | ||
interface->stop (); | ||
// 删除点云压缩与解压缩对象实例 | ||
delete (PointCloudEncoder); | ||
delete (PointCloudDecoder); | ||
} | ||
pcl::visualization::CloudViewer viewer; | ||
pcl::octree::PointCloudCompression<pcl::PointXYZRGBA>*PointCloudEncoder; | ||
pcl::octree::PointCloudCompression<pcl::PointXYZRGBA>*PointCloudDecoder; | ||
}; | ||
|
||
int | ||
main (int argc, char**argv) | ||
{ | ||
SimpleOpenNIViewer v; | ||
v.run (); | ||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) | ||
project(octree_search) | ||
find_package(PCL 1.2 REQUIRED) | ||
include_directories(${PCL_INCLUDE_DIRS}) | ||
link_directories(${PCL_LIBRARY_DIRS}) | ||
add_definitions(${PCL_DEFINITIONS}) | ||
add_executable(octree_search octree_search.cpp) | ||
target_link_libraries(octree_search ${PCL_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <pcl/point_cloud.h> | ||
#include <pcl/octree/octree.h> | ||
#include <iostream> | ||
#include <vector> | ||
#include <ctime> | ||
|
||
int | ||
main (int argc, char**argv) | ||
{ | ||
srand ((unsigned int) time (NULL)); | ||
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); | ||
// 创建点云数据 | ||
cloud->width =1000; | ||
cloud->height =1; | ||
cloud->points.resize (cloud->width * cloud->height); | ||
for (size_t i=0; i< cloud->points.size (); ++i) | ||
{ | ||
cloud->points[i].x =1024.0f* rand () / (RAND_MAX +1.0f); | ||
cloud->points[i].y =1024.0f* rand () / (RAND_MAX +1.0f); | ||
cloud->points[i].z =1024.0f* rand () / (RAND_MAX +1.0f); | ||
} | ||
float resolution =128.0f; | ||
pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree(resolution); | ||
octree.setInputCloud (cloud); | ||
octree.addPointsFromInputCloud (); | ||
pcl::PointXYZ searchPoint; | ||
searchPoint.x=1024.0f* rand () / (RAND_MAX +1.0f); | ||
searchPoint.y=1024.0f* rand () / (RAND_MAX +1.0f); | ||
searchPoint.z=1024.0f* rand () / (RAND_MAX +1.0f); | ||
// 体素内近邻搜索 | ||
std::vector<int>pointIdxVec; | ||
if (octree.voxelSearch (searchPoint, pointIdxVec)) | ||
{ | ||
std::cout<<"Neighbors within voxel search at ("<<searchPoint.x | ||
<<" "<<searchPoint.y | ||
<<" "<<searchPoint.z<<")" | ||
<<std::endl; | ||
for (size_t i=0; i<pointIdxVec.size (); ++i) | ||
std::cout<<" "<< cloud->points[pointIdxVec[i]].x | ||
<<" "<< cloud->points[pointIdxVec[i]].y | ||
<<" "<< cloud->points[pointIdxVec[i]].z <<std::endl; | ||
} | ||
//K近邻搜索 | ||
int K =10; | ||
std::vector<int>pointIdxNKNSearch; | ||
std::vector<float>pointNKNSquaredDistance; | ||
std::cout<<"K nearest neighbor search at ("<<searchPoint.x | ||
<<" "<<searchPoint.y | ||
<<" "<<searchPoint.z | ||
<<") with K="<< K <<std::endl; | ||
if (octree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) >0) | ||
{ | ||
for (size_t i=0; i<pointIdxNKNSearch.size (); ++i) | ||
std::cout<<" "<< cloud->points[ pointIdxNKNSearch[i] ].x | ||
<<" "<< cloud->points[pointIdxNKNSearch[i] ].y | ||
<<" "<< cloud->points[pointIdxNKNSearch[i] ].z | ||
<<" (squared distance: "<<pointNKNSquaredDistance[i] <<")"<<std::endl; | ||
} | ||
//半径内近邻搜索 | ||
std::vector<int>pointIdxRadiusSearch; | ||
std::vector<float>pointRadiusSquaredDistance; | ||
float radius =256.0f* rand () / (RAND_MAX +1.0f); | ||
std::cout<<"Neighbors within radius search at ("<<searchPoint.x | ||
<<" "<<searchPoint.y | ||
<<" "<<searchPoint.z | ||
<<") with radius="<< radius <<std::endl; | ||
if (octree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) >0) | ||
{ | ||
for (size_t i=0; i<pointIdxRadiusSearch.size (); ++i) | ||
std::cout<<" "<< cloud->points[ pointIdxRadiusSearch[i] ].x | ||
<<" "<< cloud->points[pointIdxRadiusSearch[i] ].y | ||
<<" "<< cloud->points[pointIdxRadiusSearch[i] ].z | ||
<<" (squared distance: "<<pointRadiusSquaredDistance[i] <<")"<<std::endl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) | ||
project(octree_change_detection) | ||
find_package(PCL 1.2 REQUIRED) | ||
include_directories(${PCL_INCLUDE_DIRS}) | ||
link_directories(${PCL_LIBRARY_DIRS}) | ||
add_definitions(${PCL_DEFINITIONS}) | ||
add_executable (octree_change_detection octree_change_detection.cpp) | ||
target_link_libraries (octree_change_detection ${PCL_LIBRARIES}) |
55 changes: 55 additions & 0 deletions
55
第四章/4 octree_change_detection/source/octree_change_detection.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <pcl/point_cloud.h> | ||
#include <pcl/octree/octree.h> | ||
#include <iostream> | ||
#include <vector> | ||
#include <ctime> | ||
|
||
int | ||
main (intargc, char**argv) | ||
{ | ||
srand ((unsignedint) time (NULL)); | ||
// 八叉树分辨率 即体素的大小 | ||
float resolution =32.0f; | ||
// 初始化空间变化检测对象 | ||
pcl::octree::OctreePointCloudChangeDetector<pcl::PointXYZ>octree (resolution); | ||
pcl::PointCloud<pcl::PointXYZ>::PtrcloudA (newpcl::PointCloud<pcl::PointXYZ> ); | ||
//为cloudA创建点云 | ||
cloudA->width =128; | ||
cloudA->height =1; | ||
cloudA->points.resize (cloudA->width *cloudA->height); | ||
for (size_ti=0; i<cloudA->points.size (); ++i) | ||
{ | ||
cloudA->points[i].x =64.0f* rand () / (RAND_MAX +1.0f); | ||
cloudA->points[i].y =64.0f* rand () / (RAND_MAX +1.0f); | ||
cloudA->points[i].z =64.0f* rand () / (RAND_MAX +1.0f); | ||
} | ||
//添加点云到八叉树,建立八叉树 | ||
octree.setInputCloud (cloudA); | ||
octree.addPointsFromInputCloud (); | ||
// 交换八叉树缓存,但是cloudA对应的八叉树仍在内存中 | ||
octree.switchBuffers (); | ||
pcl::PointCloud<pcl::PointXYZ>::PtrcloudB (new pcl::PointCloud<pcl::PointXYZ> ); | ||
// 为cloudB创建点云 | ||
cloudB->width =128; | ||
cloudB->height =1; | ||
cloudB->points.resize (cloudB->width *cloudB->height); | ||
for (size_ti=0; i<cloudB->points.size (); ++i) | ||
{ | ||
cloudB->points[i].x =64.0f* rand () / (RAND_MAX +1.0f); | ||
cloudB->points[i].y =64.0f* rand () / (RAND_MAX +1.0f); | ||
cloudB->points[i].z =64.0f* rand () / (RAND_MAX +1.0f); | ||
} | ||
//添加 cloudB到八叉树 | ||
octree.setInputCloud (cloudB); | ||
octree.addPointsFromInputCloud (); | ||
std::vector<int>newPointIdxVector; | ||
//获取前一cloudA对应的八叉树在cloudB对应八叉树中没有的体素 | ||
octree.getPointIndicesFromNewVoxels (newPointIdxVector); | ||
//打印输出点 | ||
std::cout<<"Output from getPointIndicesFromNewVoxels:"<<std::endl; | ||
for (size_ti=0; i<newPointIdxVector.size (); ++i) | ||
std::cout<<i<<"# Index:"<<newPointIdxVector[i] | ||
<<" Point:"<<cloudB->points[newPointIdxVector[i]].x <<" " | ||
<<cloudB->points[newPointIdxVector[i]].y <<" " | ||
<<cloudB->points[newPointIdxVector[i]].z <<std::endl; | ||
} |