混合编程之一:c#调用c++pcl库,生成c++动态链接库文件

c#中如何通过引用的方式调用c++编写的dll文件(c#可以直接引用c#库类文件),思路如下:

1.先用c++生成动态链接库的dll文件

2.通过c#库类项目将1生成的dll文件通过DllImport("PCLdll.dll")的方式将其封装

3.将1,2生成的dll文件放入c#项目的bin目录下,直接引用2中dll文件,即可通过实例化对象的方式调用1中封装的所有函数

一:创建PCL动态链接库项目

二、将预编译头(.h)文件删除,新建头文件PNative.h文件,并配置好PCL库环境

在头文件(PNative.h)中输入如下代码:

#pragma once
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>

//extern "C" _declspec(dllexport) int Sum(int a, int b);
extern "C" _declspec(dllexport) int Size(char* msg);
extern "C" _declspec(dllexport) int Cloud_Visual(char* msg);
extern "C" _declspec(dllexport) int loadPCDFile(char* str, double*arr_X, double* arr_Y, double* arr_Z);
extern "C" _declspec(dllexport) int PassThoughPCDFile(char* str, float*arr_X, float* arr_Y, float* arr_Z);

三、编写源文件,生成源文件PNative.cpp,将如下代码写入

#include "PNative.h"
#include <iostream>
#include<cmath> 
#include<pcl/io/pcd_io.h>
#include<pcl/point_types.h>
#include<time.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/keypoints/iss_3d.h>
#include <pcl/io/ply_io.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/features/normal_3d.h>
#include <pcl/sample_consensus/method_types.h> //随机参数估计方法头文件
#include <pcl/sample_consensus/model_types.h>  //模型定义头文件
#include "resolution.h" 
using namespace std;

//创建了一个名为cloud的指针,储存XYZ类型的点云数据
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);


int Size(char* msg)
{

    printf("%s", msg);
    if (pcl::io::loadPLYFile<pcl::PointXYZ>(msg, *cloud) == -1) {
        return(-1);
    }
    return cloud->points.size();
}


int loadPCDFile(char* str, double* arr_X, double* arr_Y, double* arr_Z) {
    //*打开点云文件
    if (pcl::io::loadPLYFile<pcl::PointXYZ>(str, *cloud) == -1) {
        return(-1);
    }

    /*pcl::visualization::CloudViewer viewer("Cloud Viewer");
    viewer.showCloud(cloud);*/

    for (size_t i = 0; i < cloud->points.size(); ++i) {

        arr_X[i] = cloud->points[i].x;
        arr_Y[i] = cloud->points[i].y;
        arr_Z[i] = cloud->points[i].z;

    }

    return 0;
}

int PassThoughPCDFile(char* str, float* arr_X, float* arr_Y, float* arr_Z) {
    //*打开点云文件
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::ExtractIndices<pcl::PointXYZ> extract;
    pcl::io::loadPLYFile<pcl::PointXYZ>(str, *cloud);


    //创建分割时所需要的模型系数对象,coefficients及存储内点的点索引集合对象inliers
    pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
    pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
    // 创建分割对象
    pcl::SACSegmentation<pcl::PointXYZ> seg;
    // 可选择配置,设置模型系数需要优化
    seg.setOptimizeCoefficients(true);
    // 必要的配置,设置分割的模型类型,所用的随机参数估计方法,距离阀值,输入点云
    seg.setModelType(pcl::SACMODEL_PLANE); //设置模型类型
    seg.setMethodType(pcl::SAC_RANSAC);    //设置随机采样一致性方法类型
    seg.setDistanceThreshold(40);        //设定距离阀值,距离阀值决定了点被认为是局内点是必须满足的条件
                                           //表示点到估计模型的距离最大值,

    seg.setInputCloud(cloud);
    //引发分割实现,存储分割结果到点几何inliers及存储平面模型的系数coefficients
    seg.segment(*inliers, *coefficients);

    extract.setInputCloud(cloud);
    extract.setIndices(inliers);
    extract.setNegative(false);

    pcl::PointCloud< pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>());
    extract.filter(*cloud_plane);

    //pcl::visualization::CloudViewer viewer("Cloud Viewer");

    //viewer.showCloud(cloud_plane);

    //while (!viewer.wasStopped())
    //{
    //    //you can also do cool processing here
    //    //FIXME: Note that this is running in a separate thread from viewerPsycho
    //    //and you should guard against race conditions yourself...
    //}


    for (size_t i = 0; i < cloud_plane->points.size(); ++i) {

        arr_X[i] = cloud_plane->points[i].x;
        arr_Y[i] = cloud_plane->points[i].y;
        arr_Z[i] = cloud_plane->points[i].z;
        // std::cout << "      " << cloud->points[i].x << "   " << cloud->points[i].y << "   " << cloud->points[i].z << "   " << std::endl;
    }
    return 0;
}

int Cloud_Visual(char* msg)
{

    if (pcl::io::loadPLYFile<pcl::PointXYZ>(msg, *cloud) == -1) {

        return(-1);
    }

    pcl::visualization::CloudViewer viewer("Cloud Viewer");
    viewer.showCloud(cloud);
    while (!viewer.wasStopped()) {
    }
    return 0;
}

源文件代码用到了resolution.h,头文件,新建resolution.h头文件,代码如下

#pragma once
#include <pcl/io/pcd_io.h>
#include <pcl/search/kdtree.h>

double computeCloudResolution(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& cloud)
{
    double resolution = 0.0;
    int numberOfPoints = 0;
    int nres;
    std::vector<int> indices(2);
    std::vector<float> squaredDistances(2);
    pcl::search::KdTree<pcl::PointXYZ> tree;
    tree.setInputCloud(cloud);

    for (size_t i = 0; i < cloud->size(); ++i)
    {
        if (!pcl_isfinite((*cloud)[i].x))
            continue;

        // Considering the second neighbor since the first is the point itself.
        nres = tree.nearestKSearch(i, 2, indices, squaredDistances);
        if (nres == 2)
        {
            resolution += sqrt(squaredDistances[1]);
            ++numberOfPoints;
        }
    }
    if (numberOfPoints != 0)
        resolution /= numberOfPoints;

    return resolution;
}

四、右击项目,点击生成即可生成c++的dll文件

(73条消息) 混合编程之二:c#调用c++pcl库,生成c#库类文件_chaonian16的博客-CSDN博客

  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值