Steps to use pcl in c#

Imitation is the most needed talent, learn from imitation.

Steps to use pcl in c#

This method is used in pcl, because the CLR method does not work. The CLR (intermediate language support library) needs to know the source file, and the original file of pcl is super complex, and it may not be exported in the CLR export method. So the focus here is the way to export the dynamic library in C language, and don't worry about the others.

One and two are export methods that do not require the transmission of complex parameters, or do not need to be used later.

1. Static library export of c++ library

When exporting a dynamic library, it seems that you cannot include header files in multiple places, otherwise redefinition will occur, which is a little different from the usual running program.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-KnEDk2A2-1665283361727) (redefinition.png)]

Steps: define import and export -> original implementation -> define the export function based on the original implementation, call the "original implementation" function in the function, and pass in the relevant class parameters

  1. Define to prevent redefinition

    #ifndef PCLFUNC_EXPORT_H
    #define PCLFUNC_EXPORT_H
    
    //相关定义
    
    #endif // !PCLFUNC_EXPORT_H
    
  2. definedefine import and export

    #ifndef DLL_IMPORT
    #define TESTDLL_API __declspec(dllexport)
    #else
    #define TESTDLL_API __declspec(dllimport)
    
  3. Originally realized

    //主要写自己的实现
    #include <pcl/io/pcd_io.h>
    #include <pcl/point_types.h>
    #include<string>
    using namespace std;
    using namespace pcl::io;
    
    class  PCLFuncExport
    {
          
          
    public:
    	PCLFuncExport();
    	~PCLFuncExport();
    
    	pcl::PointCloud<pcl::PointXYZ>::Ptr getPclPoint();
    	void setPclPint(string path);
    	int readPcdFile(string path, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud);
    
    private:
    	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
    };
    
  4. Define the exported function based on the original implementation, call the "original implementation" function in the function, and pass in the relevant class parameters

    • Mainly through our own method to call the third-party library pcl library, otherwise we don't know how the third-party library is encapsulated, and the encapsulation of the third-party library may not meet our requirements, so it is more convenient to encapsulate and call in this way , but the classes and objects passing parameters need to be corresponding in c#, which is solved here through the open source pcl c# package
    extern "C" TESTDLL_API  PCLFuncExport* dllGetObj() {
          
           return new PCLFuncExport(); };
    extern "C" TESTDLL_API int dllReadPcd(PCLFuncExport * pcl,string path, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud) {
          
          
    	return pcl->readPcdFile(path, cloud);
    }
    extern "C" TESTDLL_API void dllSetPcl(PCLFuncExport * pcl, string path) {
          
          
    	pcl->setPclPint(path);
    }
    extern "C" TESTDLL_API void dllDeletePclFuncExport(PCLFuncExport * pcl) {
          
          
    	delete pcl;
    }
    

2. Encapsulation call of c# class library

Create a new c# class library project, as shown in the figure
Insert image description here

[DllImport("PCLCppDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public extern static int dllReadPcd(IntPtr pcl, [MarshalAs(UnmanagedType.LPStr)] string path,IntPtr cloud);
  1. Agree on the character set, call stack and clearing method.
  2. String takes[MarshalAs(UnmanagedType.LPStr)] string path
  3. Arrays and classes are directly usedIntPtr
  4. Declare in public, extern and static ways:public extern static

In the class, the first letter is capitalized to call the result. This is the same as the general method, and there is nothing to pay attention to.

public int ReadPcd(IntPtr pcl, string path, IntPtr cloud)
{
    
    
	return dllReadPcd(pcl,path,cloud);
}

3. Its use in c#

Import the package of the class library just now and the dynamic library of pcl, and you can see that the assembly has been added:

Insert image description here

Call the methods of the class library in the program

//调用类库

Using the pcl csharp package

Import package

//增添的命名空间
using PclSharp.IO;
using PclSharp;

Put the dynamic library and class library package into the bin path
Insert image description here

Calling method:

private void button1_Click(object sender, EventArgs e)
        {
    
    
            using (var cloud = new PointCloudOfXYZ())
            using (var writer = new PCDWriter())
            using (var pcdReader = new PCDReader())
            {
    
    
                if (pcdReader.Read("E:/pcl/test/saddle.pcd", cloud) < 0)
                {
    
    
                    MessageBox.Show("没读取成功");
                }
                else
                {
    
    
                    MessageBox.Show("点云数据读取成功");

                }

            }
        }

Complete source code:

using System;
using System.Windows.Forms;

//增添的命名空间
using PclSharp.IO;
using PclSharp;

namespace WindowsFormsApp1
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
    
    
            using (var cloud = new PointCloudOfXYZ())
            using (var writer = new PCDWriter())
            using (var pcdReader = new PCDReader())
            {
    
    
                if (pcdReader.Read("E:/pcl/test/saddle.pcd", cloud) < 0)
                {
    
    
                    MessageBox.Show("没读取成功");
                }
                else
                {
    
    
                    MessageBox.Show("点云数据读取成功");

                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
    
    

        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_42295969/article/details/127222488