【PCL】SolidWorks三维建模STL,OBJ采样⽣成PCD点云数据(附源码)
⽬录&索引
这阵⼦做线结构光视觉检测(钢轨磨耗检测)项⽬的过程中,发现之前的许多知识点在逐渐遗忘,常⾔好记性不如烂笔头,故决定把项⽬中所⽤到的,把笔者认为有价值且对博友能够产⽣帮助的内容,记录分享于此。能⼒有限,如有纰漏,希望博友留下意见与建议。
1 前⾔
磨耗检测项⽬中,配准阶段将结构光采集的数据集映射到标准廓形的数据集上,以识别其特征,进⽽计算分析。
其中,建⽴标准轨头廓形数据模型有两种⽅法,a)分段函数描述;b)三维模型转 PCD(所选⽅案);
选定⽅案之后,查阅三维模型提取点云数据的相关资料时,发现在 SolidWorks 三维模型中提取⽣成点云数据的资料较少,且需要下载相关软件或配置需求过⾼。于是在笔者仔细查阅相关资料后,总结了⼀种较为⽅便且利于没有这⽅⾯基础的⼩⽩阅读完成。
2 准备⼯作(相关软件及库)
1. SolidWorks 等三维建模软件
2. PCL 点云库
3 实现步骤
3.1 三维建模保存 stl ⽹格⽂件
3.2 stl⽹格⽂件转 obj ⽹格⽂件
a) 在⼯具⾥勾选 SW 插件 ScanTo3D,后续格式保存要⽤
b) 以 ScanTo3D ⽹格⽂件打开保存的 stl
c) 另存为 ScanTo3D(*.obj) ⽂件
3.3 利⽤ PCL 采样可执⾏程序,实现 obj ⽹格⽂件转 pcd 点云
a) 接下来就是最重要的两步了,在安装 PCL 的路径下 bin ⽂件夹打开,到pcl_mesh_ 或 pcl_mesh_
b) cmd 运⾏可执⾏采样⽂件(obj ⽂件相同⽬录)
结果显⽰,点云⽂件获取完毕
当前⽬录下⽣成 60kg╱m 钢轨-05.pcd 的⽂件。下采样控制体素点距或投影模型等相关后续必要操作,标准点云模型满⾜需求。如有不明⽩的地⽅,欢迎交流。
5 mesh_sampling 源码(应读者需求,已更新)
/*
* Software License Agreement (BSD License)
*
*  Point Cloud Library (PCL) -
*  Copyright (c) 2010-2011, Willow Garage, Inc.
*
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
error parse new
*  modification, are permitted provided that the following conditions
*  are met:
*
*  * Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*  * Redistributions in binary form must reproduce the above
*    copyright notice, this list of conditions and the following
*    disclaimer in the documentation and/or other materials provided
*    with the distribution.
*  * Neither the name of the copyright holder(s) nor the names of its
*    contributors may be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*
*/
#include<pcl/visualization/pcl_visualizer.h>
#include<pcl/io/pcd_io.h>
#include<pcl/common/transforms.h>
#include<vtkPLYReader.h>
#include<vtkOBJReader.h>
#include<vtkTriangle.h>
#include<vtkTriangleFilter.h>
#include<vtkPolyDataMapper.h>
#include<pcl/filters/voxel_grid.h>
#include<pcl/console/print.h>
#include<pcl/console/parse.h>
inline double
uniform_deviate(int seed)
{
double ran = seed *(1.0/(RAND_MAX +1.0));
return ran;
}
inline void
randomPointTriangle(float a1,float a2,float a3,float b1,float b2,float b3,float c1,float c2,float c3,
Eigen::Vector4f& p)
{
float r1 =static_cast<float>(uniform_deviate(rand()));
float r2 =static_cast<float>(uniform_deviate(rand()));
float r1sqr =sqrtf(r1);
float OneMinR1Sqr =(1- r1sqr);
float OneMinR2 =(1- r2);
a1 *= OneMinR1Sqr;
a2 *= OneMinR1Sqr;
a3 *= OneMinR1Sqr;
b1 *= OneMinR2;
b2 *= OneMinR2;
b3 *= OneMinR2;
c1 = r1sqr *(r2 * c1 + b1)+ a1;
c2 = r1sqr *(r2 * c2 + b2)+ a2;
c3 = r1sqr *(r2 * c3 + b3)+ a3;
p[0]= c1;
p[1]= c2;
p[2]= c3;
p[3]=0;
}
inline void
randPSurface(vtkPolyData * polydata, std::vector<double>* cumulativeAreas,double totalArea, Eigen::Vector4f& p) {
float r =static_cast<float>(uniform_deviate(rand())* totalArea);
std::vector<double>::iterator low = std::lower_bound(cumulativeAreas->begin(), cumulativeAreas->end(), r);
vtkIdType el =vtkIdType(low - cumulativeAreas->begin());
double A[3], B[3], C[3];
vtkIdType npts =0;
vtkIdType *ptIds =NULL;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。