#include "VSensorLaser.h" 
#include <signal.h> 
#include <direct.h>
#include <thread> 
#pragma comment(lib, "VSensorLaserSDK.lib")
using namespace std;
using namespace VSENSORLASER;
int main()
{
    //获取相对路径
    char cwd[200];
    if (_getcwd(cwd, 200) == nullptr)
    {
        printf("get path failed\n");
    }
    //实例化设备
    VSensorLaser* pVSensor = new VSensorLaser();
    //获取设备列表
    VSensorLaserCameraInfo cameraList[20];
    int num = 0;
    pVSensor->GetDeviceList(cameraList, &num);
    for (int i = 0; i < num; i++)
    {
        printf("device num %d : list[%d] = %s\n", i, i, cameraList[i].CameraName);
    }
    //连接设备，0为连接第一台设备  
    int status = pVSensor->DeviceConnect(0);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("connect device failed status = %d\n", status);
        system("pause");
    }
    //设置曝光时间
    status = pVSensor->SetExposureTime(2.0);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set exposure time failed, status = %d\n", status);
        system("pause");
    }
    //设置投影等级
    status = pVSensor->SetProjectLight(1);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set project light failed, status = %d\n", status);
        system("pause");
    }
    //设置为采集模式
    status = pVSensor->SetCaptureMode(emVSensorLaserCaptureMode::MODE_EXTERNAL);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set capture mode failed, status = %d\n", status);
        system("pause");
    }
	//设置缓存区大小
    status = pVSensor->SetCloudBufferNum(10);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("set cloud buffer num failed, status = %d\n", status);
        system("pause");
    }
    //预留时间进行硬件触发采集
    printf("wait external trigger...\n");
    std::this_thread::sleep_for(std::chrono::milliseconds(5000));
    //获取缓存区触发的点云
    printf("get cloud buffer...\n");
    std::vector<VSensorLaserResult> results;
    status = pVSensor->GetCloudBuffer(results);
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("get cloud buffer failed, status = %d\n", status);
        system("pause");
    }
    else
    {
        printf("getted %zu frames of data\n", results.size());
        //保存无序点云为pcd格式
        for (int i = 0; i < results.size(); i++)
        {
            status = pVSensor->Save3DCloud(string(string(cwd) + "//test" + to_string(i + 1) + ".pcd"), &results[i], emVSensorLaserPointType::POINT_TYPR_DISORDER);
            if (status != CAMERA_STATUS_SUCCESS)
            {
                printf("save 3D points failed, status = %d\n", status);
                system("pause");
            }
        }
    }
    //反初始化设备
    status = pVSensor->DeviceUnInit();
    if (status != CAMERA_STATUS_SUCCESS)
    {
        printf("device release failed, status = %d\n", status);
        system("pause");
    }
    delete pVSensor;
    printf("run success\n");
    system("pause");
    return 0;
}