#ifndef MYPCL_H_
#define MYPCL_H_

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/common/common.h>
#include <pcl/common/transforms.h>
#include <pcl/filters/crop_box.h>
#include <pcl/compression/octree_pointcloud_compression.h>

#include <mutex>
#include <sstream>
#include <fstream>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string>

namespace ai_matrix
{
    class MyPcl final
    {
    public:
        static MyPcl *getins();

        //点云压缩
        void compressPCD(const pcl::PointCloud<pcl::PointXYZRGB> &cloud, std::stringstream &compressedData);

        //拟合车厢边缘线
        void insert_plane_line(pcl::PointCloud<pcl::PointXYZRGB> &line, float x_min, float x_max, float y, float z, unsigned char r, unsigned char g, unsigned char b, float step = 0.02);

        //拟合装载区域边缘线
        void insert_load_line(pcl::PointCloud<pcl::PointXYZRGB> &line, float x_min, float x_max, float y_min, float y_max, float z, unsigned char r, unsigned char g, unsigned char b, float step = 0.02);

        //拟合限制线
        void insert_bound_line(pcl::PointCloud<pcl::PointXYZRGB> &line, float y_min, float y_max, float z_min, float z_max, float x, unsigned char r, unsigned char g, unsigned char b, float step = 0.02);

        //拟合识别物体边缘框
        void insert_object_line(pcl::PointCloud<pcl::PointXYZRGB> &line, float x_min, float x_max, float y, float z_min, float z_max, unsigned char r, unsigned char g, unsigned char b, float step = 0.02);

        //坐标变换1，有参照物时用
        void coords_convert(const pcl::PointCloud<pcl::PointXYZ> &cloud_in, pcl::PointCloud<pcl::PointXYZ> &cloud_out, const Eigen::Matrix4f &tf, float thetaz = 0);

        //坐标变换2，无参照物时用
        void coords_convert(const pcl::PointCloud<pcl::PointXYZ> &cloud_in, pcl::PointCloud<pcl::PointXYZ> &cloud_out, float thetax = 0, float thetay = 0, float thetaz = 0, float radarz = 0, bool reverse = false);

        //取中心立方体
        void cube_cloud(const pcl::PointCloud<pcl::PointXYZ> &cloud_in, std::vector<int> &indices, const Eigen::Vector3f &min, const Eigen::Vector3f &max);

        //取中心立方体
        void cube_cloud(const pcl::PointCloud<pcl::PointXYZ> &cloud_in, pcl::PointCloud<pcl::PointXYZ> &cloud_out, const Eigen::Vector3f &min, const Eigen::Vector3f &max);

        //取中心立方体
        void cube_cloud(const pcl::PointCloud<pcl::PointXYZ> &cloud_in, pcl::PointCloud<pcl::PointXYZ> &cloud_out, std::vector<int> &indices, const Eigen::Vector3f &min, const Eigen::Vector3f &max);

    private:
        MyPcl() = default;
        MyPcl(const MyPcl &) = delete;
        MyPcl(MyPcl &&) = delete;
        MyPcl &operator=(const MyPcl &) = delete;
        MyPcl &operator=(MyPcl &&) = delete;
        ~MyPcl() = default;

        //定义一个嵌套类，负责释放内存，操作系统自动完成，不用担心内存泄露
        class GarbageCollector
        {
        public:
            ~GarbageCollector()
            {
                if (MyPcl::ins)
                {
                    delete MyPcl::ins;
                    MyPcl::ins = nullptr;
                }
            }
        };

        static GarbageCollector gc;
        static MyPcl *ins;
        static std::mutex mx; //锁，保证线程安全
    };
}

#endif