混合编程之一: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博客

C#中使用C++PCL(Point Cloud Library)时,需要通过某种方式将C++的PointCloud变量传递到C#中。由于C#C++在内存管理上的差异,通常会使用一些中间技术来传递数据。以下是几种常见的方法: 1. **使用C++/CLI**: C++/CLI是微软提供的一种混合语言,可以在C++.NET之间架起桥梁。你可以在C++/CLI中封装PCL的PointCloud,然后在C#调用这个封装好的类。 ```cpp // C++/CLI 代码 public ref class PointCloudWrapper { private: pcl::PointCloud<pcl::PointXYZ>::Ptr cloud; public: PointCloudWrapper() { cloud = boost::make_shared<pcl::PointCloud<pcl::PointXYZ>>(); } void AddPoint(float x, float y, float z) { pcl::PointXYZ point; point.x = x; point.y = y; point.z = z; cloud->points.push_back(point); } property IntPtr CloudData { IntPtr get() { return System::IntPtr(cloud->points.data()); } } property int PointCount { int get() { return cloud->points.size(); } } }; ``` ```csharp // C# 代码 using System; class Program { static void Main(string[] args) { PointCloudWrapper wrapper = new PointCloudWrapper(); wrapper.AddPoint(1.0f, 2.0f, 3.0f); wrapper.AddPoint(4.0f, 5.0f, 6.0f); IntPtr dataPtr = wrapper.CloudData; int pointCount = wrapper.PointCount; // 处理数据 } } ``` 2. **使用P/Invoke**: 你可以在C++中编写一个DLL,然后在C#中使用P/Invoke调用这个DLL的函数来获取PointCloud的数据。 ```cpp // C++ 代码 (编译成DLL) extern "C" __declspec(dllexport) void GetPointCloudData(float* data, int* size) { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = boost::make_shared<pcl::PointCloud<pcl::PointXYZ>>(); // 添加点 pcl::PointXYZ point; point.x = 1.0f; point.y = 2.0f; point.z = 3.0f; cloud->points.push_back(point); // 复制数据 *size = cloud->points.size(); std::copy(cloud->points.begin(), cloud->points.end(), data); } ``` ```csharp // C# 代码 using System; using System.Runtime.InteropServices; class Program { [DllImport("YourCppDll.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void GetPointCloudData(float[] data, out int size); static void Main(string[] args) { int size; float[] data = new float[1000]; // 假设最大点数为1000 GetPointCloudData(data, out size); // 处理数据 } } ``` 3. **使用中间文件**: 你可以将C++中的PointCloud数据保存到一个文件中,然后在C#中读取这个文件。 ```cpp // C++ 代码 void SavePointCloud(const pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, const std::string& filename) { std::ofstream file(filename, std::ios::binary); file.write(reinterpret_cast<const char*>(cloud->points.data()), cloud->points.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值