stereolabs / zed-unity

ZED SDK Unity plugin
https://www.stereolabs.com/documentation/guides/using-zed-with-unity/introduction.html
MIT License
250 stars 57 forks source link

ZEDMat to Image #15

Closed tomitrescak closed 2 years ago

tomitrescak commented 6 years ago

Hi, that for the useful plugin! I am trying to work with ZED SDK in C# and I made it as far as grabbing the Image into ZEDMat. I am trying now to process the image with EMGUCV, but I have NO Idea how to convert the ZEDMat to anything useful by C#.

I have created a GIST with the CODE: https://gist.github.com/tomitrescak/c31cee9c898ad7def32c4aaef8c1234d

The issue is marked with "???"

Would you be able to help the brother out?

obraun-sl commented 6 years ago

Hi,

ZEDMat provides the public function MatPtr to access the data. Therefore I think you just need to copy the IntPtr from ZEDMat to Emgu.CV.Image using Marshal.copy()

Note that ZEDMat gives RGBA (4 channels) data. Your Emgu.CV.Image should be created in bgra if possible ( I'm not familiar with emgu.cv but since it's based on opencv, should be possible)

MiaoDX commented 6 years ago

@tomitrescak , your example code seems promising, I am wondering:

  1. Is it a plain C# project or an Unity one?
  2. What is the Reference of the using sl;? I try to add sl_mr_core64.dll/sl_unitywrapper.dll from this repo or sl_core64.dll/sl_zed64.dll from the ZED sdk path, both failed: "A reference to ... could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component".
  3. Or you made your own dll?
  4. Could you make it a standalone project?

Looking forward to your reply!

tomitrescak commented 6 years ago

Hi, please see the code below thta controls ZED camera. Also, see screenshot with all the dependencies:

capture

I have also gutted out the Unity package and created standalone SDK, also below:

using Emgu.CV;
using Emgu.CV.Structure;
using sl;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Holoserver.Cameras
{
    public class Zed
    {
        ZEDCamera camera;
        InitParameters initParameters;
        object grabLock = new object();

        ERROR_CODE lastInitStatus = ERROR_CODE.ERROR_CODE_LAST;
        ERROR_CODE previousInitStatus;
        private bool running;
        private int stride;

        List<ZEDMat> mats;
        int matIndex;
        private RuntimeParameters runtimeParameters;

        // properties

        public event Action<Zed> FrameTick;

        public ZEDCamera Camera => this.camera;

        public RuntimeParameters RuntimeParameters { get => runtimeParameters; set => runtimeParameters = value; }

        public Image<Bgra, byte> Left => this.FetchImage(VIEW.LEFT);
        public Image<Bgra, byte> Right => this.FetchImage(VIEW.RIGHT);
        public Image<Bgra, byte> Depth => this.FetchImage(VIEW.DEPTH);
        public Image<Bgra, byte> Confidence => this.FetchImage(VIEW.CONFIDENCE);
        public Image<Bgra, byte> LeftGrey => this.FetchImage(VIEW.LEFT_GREY);
        public Image<Bgra, byte> RightGrey => this.FetchImage(VIEW.RIGHT_GREY);
        public Image<Bgra, byte> SideBySide => this.FetchImage(VIEW.SIDE_BY_SIDE);

        // constructor

        public Zed(string svoPath) : this(null, svoPath) { }

        public Zed(InitParameters parameters = null, string svoPath = null)
        {
            camera = ZEDCamera.GetInstance();

            mats = new List<ZEDMat>();

            if (parameters == null)
            {
                parameters = new InitParameters();
                parameters.resolution = RESOLUTION.HD720;
                parameters.depthMode = DEPTH_MODE.MEDIUM;
                parameters.depthStabilization = true;
                parameters.enableRightSideMeasure = true; // isStereoRig;
                parameters.depthMinimumDistance = 0.2f;
            }

            if (svoPath != null)
            {
                parameters.pathSVO = svoPath;
            }

            this.initParameters = parameters;

            // runtime parameters
            runtimeParameters = new RuntimeParameters()
            {
                sensingMode = SENSING_MODE.FILL,
                enableDepth = true
            };

            // create the camera
            camera.CreateCamera(true);
        }

        public Zed(RESOLUTION resolution, DEPTH_MODE depthMode = DEPTH_MODE.PERFORMANCE, bool stabilisation = false)
            : this(new InitParameters
            {
                resolution = resolution,
                depthMode = depthMode,
                depthStabilization = stabilisation,
                enableRightSideMeasure = true,
                depthMinimumDistance = 0.2f
            })
        { }

        public Image<Bgra, byte> FetchImage(VIEW view)
        {
            if (mats.Count < matIndex + 1)
            {
                mats.Add(new ZEDMat());
            }
            var mat = mats[matIndex++];
            camera.RetrieveImage(mat, view);
            return new Image<Bgra, byte>(camera.ImageWidth, camera.ImageHeight, stride, mat.GetPtr());
        }

        private async Task<ZEDCamera> InitZED()
        {
            return await Task.Run(() =>
            {
                while (lastInitStatus != sl.ERROR_CODE.SUCCESS)
                {
                    lastInitStatus = camera.Init(ref initParameters);
                    if (lastInitStatus != sl.ERROR_CODE.SUCCESS)
                    {
                        lastInitStatus = camera.Init(ref initParameters);
                        previousInitStatus = lastInitStatus;
                    }
                    Thread.Sleep(300);
                }

                // init stride
                stride = 4 * ((camera.ImageWidth * 32 + 31) / 32);

                // ZED has opened
                return camera;

            });
        }

        public void Start()
        {
            var thread = new Thread(StartCamera) { IsBackground = true };
            thread.Start();
        }

        private async void StartCamera()
        {
            await this.InitZED();

            running = true;

            while (running)
            {
                lock (grabLock)
                {
                    sl.ERROR_CODE e = camera.Grab(ref runtimeParameters);

                    // reset mat index
                    matIndex = 0;

                    // call the listener
                    this.FrameTick(this);
                    Thread.Sleep(10);
                }
            }
        }

        public void Stop()
        {
            this.running = false;
        }
    }
}

SDK

//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============

using System.Collections.Generic;
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace sl
{
    /*
     * Unity representation of a sl::Camera
     */
    public class ZEDCamera
    {
        static float Deg2Rad = (float)Math.PI / 180;

        /// <summary>
        /// Type of textures requested
        /// </summary>
        public enum TYPE_VIEW
        {
            RETRIEVE_IMAGE,
            RETRIEVE_MEASURE
        }

        /// <summary>
        /// Informations of requested textures
        /// </summary>
        private struct TextureRequested
        {
            public int type;
            public int option;
        };

        /********* Camera members ********/

        //DLL name
        const string nameDll = "sl_unitywrapper";

        //List of all requested textures
        private List<TextureRequested> texturesRequested;

        //Width of the textures
        private int imageWidth;

        //Height of the textures
        private int imageHeight;

        //Singleton of ZEDCamera
        private static ZEDCamera instance = null;

        //True if the SDK is installed
        private static bool pluginIsReady = true;

        //Mutex for threaded rendering
        private static object _lock = new object();

        //The current resolution
        private RESOLUTION currentResolution;

        //Callback for c++ debug, should not be used in C#
        private delegate void DebugCallback(string message);

        //HD720 default FPS
        private uint fpsMax = 60;
        private ZEDCameraSettingsManager cameraSettingsManager = new ZEDCameraSettingsManager();

        // Baseline of the camera
        private float baseline = 0.0f;
        public float Baseline {
            get { return baseline; }
        }
        /// <summary>
        /// Current field of view
        /// </summary>
        private float fov_H = 0.0f;
        private float fov_V = 0.0f;

        /// <summary>
        /// Current field of view
        /// </summary>
        public float HorizontalFieldOfView {
            get { return fov_H; }
        }

        public float VerticalFieldOfView {
            get { return fov_V; }
        }
        /// <summary>
        /// Information in cache
        /// </summary>
        private CalibrationParameters calibrationParametersRaw;
        private CalibrationParameters calibrationParametersRectified;

        /// <summary>
        /// Camera Model
        /// </summary>
        private sl.MODEL cameraModel;

        /// <summary>
        /// Camera is opened or not
        /// </summary>
        private bool cameraReady = false;

        /// <summary>
        /// Information in cache, call GetInformation to update
        /// </summary>
        public CalibrationParameters CalibrationParametersRaw {
            get { return calibrationParametersRaw; }
        }
        /// <summary>
        /// Information in cache, call GetInformation to update
        /// </summary>
        public CalibrationParameters CalibrationParametersRectified {
            get { return calibrationParametersRectified; }
        }
        /// <summary>
        /// Information in cache, call GetInformation to update
        /// </summary>
        public sl.MODEL CameraModel {
            get { return cameraModel; }
        }

        public bool IsCameraReady {
            get { return cameraReady; }
        }

        public bool IsHmdCompatible {
            get { return cameraModel == sl.MODEL.ZED_M; }
        }
        /// <summary>
        /// DLL needed to run the exe
        /// </summary>
        static private string[] dependenciesNeeded =
        {
            "sl_zed64.dll",
            "sl_core64.dll",
            "sl_input64.dll"
        };

        const int tagZEDCamera = 20;
        /// <summary>
        /// Layer only the ZED is able to see
        /// </summary>
        public static int Tag {
            get { return tagZEDCamera; }
        }
        /// <summary>
        /// Layer only the other cameras apart the ZED are able to see
        /// </summary>
        const int tagOneObject = 12;
        public static int TagOneObject {
            get { return tagOneObject; }
        }

        /// <summary>
        /// Cuurent Plugin Version
        /// </summary>
        public static readonly System.Version PluginVersion = new System.Version(2, 4, 0);

        /******** DLL members ***********/

        [DllImport(nameDll, EntryPoint = "GetRenderEventFunc")]
        private static extern IntPtr GetRenderEventFunc();

        [DllImport(nameDll, EntryPoint = "dllz_register_callback_debuger")]
        private static extern void dllz_register_callback_debuger(DebugCallback callback);

        /*
          * Create functions
          */
        [DllImport(nameDll, EntryPoint = "dllz_create_camera")]
        private static extern System.IntPtr dllz_create_camera(bool verbose);

        /*
        * Opening function (Open camera and create textures)
        */
        [DllImport(nameDll, EntryPoint = "dllz_open")]
        private static extern int dllz_open(ref dll_initParameters parameters, System.Text.StringBuilder svoPath, System.Text.StringBuilder output);

        /*
         * Close function
         */
        [DllImport(nameDll, EntryPoint = "dllz_close")]
        private static extern void dllz_close();

        /*
               * Grab function
               */
        [DllImport(nameDll, EntryPoint = "dllz_grab")]
        private static extern int dllz_grab(ref sl.RuntimeParameters runtimeParameters);

        /*
        * Recording functions
        */
        [DllImport(nameDll, EntryPoint = "dllz_enable_recording")]
        private static extern int dllz_enable_recording(byte[] video_filename, int compresssionMode);

        [DllImport(nameDll, EntryPoint = "dllz_record")]
        private static extern void dllz_record(ref Recording_state state);

        [DllImport(nameDll, EntryPoint = "dllz_disable_recording")]
        private static extern bool dllz_disable_recording();

        /*
        * Texturing functions
        */
        [DllImport(nameDll, EntryPoint = "dllz_retrieve_textures")]
        private static extern void dllz_retrieve_textures();

        [DllImport(nameDll, EntryPoint = "dllz_get_updated_textures_timestamp")]
        private static extern ulong dllz_get_updated_textures_timestamp();

        [DllImport(nameDll, EntryPoint = "dllz_swap_textures")]
        private static extern ulong dllz_swap_textures();

        public int RegisterTextureImageType(int option, IntPtr id, Resolution resolution)
        {
            return dllz_register_texture_image_type(option, id, resolution);
        }

        [DllImport(nameDll, EntryPoint = "dllz_register_texture_image_type")]
        private static extern int dllz_register_texture_image_type(int option, IntPtr id, Resolution resolution);

        [DllImport(nameDll, EntryPoint = "dllz_register_texture_measure_type")]
        private static extern int dllz_register_texture_measure_type(int option, IntPtr id, Resolution resolution);

        [DllImport(nameDll, EntryPoint = "dllz_unregister_texture_measure_type")]
        private static extern int dllz_unregister_texture_measure_type(int option);

        [DllImport(nameDll, EntryPoint = "dllz_unregister_texture_image_type")]
        private static extern int dllz_unregister_texture_image_type(int option);

        [DllImport(nameDll, EntryPoint = "dllz_get_copy_mat_texture_image_type")]
        private static extern IntPtr dllz_get_copy_mat_texture_image_type(int option);

        [DllImport(nameDll, EntryPoint = "dllz_get_copy_mat_texture_measure_type")]
        private static extern IntPtr dllz_get_copy_mat_texture_measure_type(int option);
        /*
        * Self-calibration function
        */
        [DllImport(nameDll, EntryPoint = "dllz_reset_self_calibration")]
        private static extern void dllz_reset_self_calibration();

        [DllImport(nameDll, EntryPoint = "dllz_get_self_calibration_state")]
        private static extern int dllz_get_self_calibration_state();

        /*
         * Camera control functions
         */

        [DllImport(nameDll, EntryPoint = "dllz_set_camera_fps")]
        private static extern void dllz_set_camera_fps(int fps);

        [DllImport(nameDll, EntryPoint = "dllz_get_camera_fps")]
        private static extern float dllz_get_camera_fps();

        [DllImport(nameDll, EntryPoint = "dllz_get_width")]
        private static extern int dllz_get_width();

        [DllImport(nameDll, EntryPoint = "dllz_get_height")]
        private static extern int dllz_get_height();

        [DllImport(nameDll, EntryPoint = "dllz_get_calibration_parameters")]
        private static extern IntPtr dllz_get_calibration_parameters(bool raw);

        [DllImport(nameDll, EntryPoint = "dllz_get_camera_model")]
        private static extern int dllz_get_camera_model();

        [DllImport(nameDll, EntryPoint = "dllz_get_zed_firmware")]
        private static extern int dllz_get_zed_firmware();

        [DllImport(nameDll, EntryPoint = "dllz_get_zed_serial")]
        private static extern int dllz_get_zed_serial();

        [DllImport(nameDll, EntryPoint = "dllz_is_zed_connected")]
        private static extern int dllz_is_zed_connected();

        [DllImport(nameDll, EntryPoint = "dllz_get_camera_timestamp")]
        private static extern ulong dllz_get_camera_timestamp();

        [DllImport(nameDll, EntryPoint = "dllz_get_current_timestamp")]
        private static extern ulong dllz_get_current_timestamp();

        [DllImport(nameDll, EntryPoint = "dllz_get_image_updater_time_stamp")]
        private static extern ulong dllz_get_image_updater_time_stamp();

        [DllImport(nameDll, EntryPoint = "dllz_get_frame_dropped_count")]
        private static extern uint dllz_get_frame_dropped_count();

        [DllImport(nameDll, EntryPoint = "dllz_get_frame_dropped_percent")]
        private static extern float dllz_get_frame_dropped_percent();

        /*
         * SVO control functions
         */

        [DllImport(nameDll, EntryPoint = "dllz_set_svo_position")]
        private static extern void dllz_set_svo_position(int frame);

        [DllImport(nameDll, EntryPoint = "dllz_get_svo_number_of_frames")]
        private static extern int dllz_get_svo_number_of_frames();

        [DllImport(nameDll, EntryPoint = "dllz_get_svo_position")]
        private static extern int dllz_get_svo_position();

        /*
         * Depth Sensing utils functions
         */
        [DllImport(nameDll, EntryPoint = "dllz_set_confidence_threshold")]
        private static extern void dllz_set_confidence_threshold(int threshold);

        [DllImport(nameDll, EntryPoint = "dllz_get_confidence_threshold")]
        private static extern int dllz_get_confidence_threshold();

        [DllImport(nameDll, EntryPoint = "dllz_set_depth_max_range_value")]
        private static extern void dllz_set_depth_max_range_value(float distanceMax);

        [DllImport(nameDll, EntryPoint = "dllz_get_depth_max_range_value")]
        private static extern float dllz_get_depth_max_range_value();

        [DllImport(nameDll, EntryPoint = "dllz_get_depth_value")]
        private static extern float dllz_get_depth_value(uint x, uint y);

        [DllImport(nameDll, EntryPoint = "dllz_get_distance_value")]
        private static extern float dllz_get_distance_value(uint x, uint y);

        [DllImport(nameDll, EntryPoint = "dllz_get_depth_min_range_value")]
        private static extern float dllz_get_depth_min_range_value();

        [DllImport(nameDll, EntryPoint = "dllz_disable_tracking")]
        private static extern void dllz_disable_tracking(System.Text.StringBuilder path);

        [DllImport(nameDll, EntryPoint = "dllz_save_current_area")]
        private static extern int dllz_save_current_area(System.Text.StringBuilder path);

        [DllImport(nameDll, EntryPoint = "dllz_get_position_data")]
        private static extern int dllz_get_position_data(ref Pose pose, int reference_frame);

        [DllImport(nameDll, EntryPoint = "dllz_get_area_export_state")]
        private static extern int dllz_get_area_export_state();
        /*
        * Spatial Mapping functions
        */
        [DllImport(nameDll, EntryPoint = "dllz_enable_spatial_mapping")]
        private static extern int dllz_enable_spatial_mapping(float resolution_meter, float max_range_meter, int saveTexture);

        [DllImport(nameDll, EntryPoint = "dllz_disable_spatial_mapping")]
        private static extern void dllz_disable_spatial_mapping();

        [DllImport(nameDll, EntryPoint = "dllz_pause_spatial_mapping")]
        private static extern void dllz_pause_spatial_mapping(bool status);

        [DllImport(nameDll, EntryPoint = "dllz_request_mesh_async")]
        private static extern void dllz_request_mesh_async();

        [DllImport(nameDll, EntryPoint = "dllz_get_mesh_request_status_async")]
        private static extern int dllz_get_mesh_request_status_async();

        [DllImport(nameDll, EntryPoint = "dllz_update_mesh")]
        private static extern int dllz_update_mesh(int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh);

        [DllImport(nameDll, EntryPoint = "dllz_save_mesh")]
        private static extern bool dllz_save_mesh(string filename, MESH_FILE_FORMAT format);

        [DllImport(nameDll, EntryPoint = "dllz_load_mesh")]
        private static extern bool dllz_load_mesh(string filename, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbMaxSubmesh, int[] textureSize = null);

        [DllImport(nameDll, EntryPoint = "dllz_apply_texture")]
        private static extern bool dllz_apply_texture(int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int[] textureSize, int nbSubmesh);

        [DllImport(nameDll, EntryPoint = "dllz_filter_mesh")]
        private static extern bool dllz_filter_mesh(FILTER meshFilter, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh);

        [DllImport(nameDll, EntryPoint = "dllz_get_spatial_mapping_state")]
        private static extern int dllz_get_spatial_mapping_state();

        [DllImport(nameDll, EntryPoint = "dllz_spatial_mapping_merge_chunks")]
        private static extern void dllz_spatial_mapping_merge_chunks(int numberFaces, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh);

        /*
          * Specific plugin functions
          */
        [DllImport(nameDll, EntryPoint = "dllz_check_plugin")]
        private static extern int dllz_check_plugin(int major, int minor);

        [DllImport(nameDll, EntryPoint = "dllz_set_is_threaded")]
        private static extern void dllz_set_is_threaded();

        [DllImport(nameDll, EntryPoint = "dllz_get_sdk_version")]
        private static extern IntPtr dllz_get_sdk_version();

        [DllImport(nameDll, EntryPoint = "dllz_compute_offset")]
        private static extern void dllz_compute_offset(float[] A, float[] B, int nbVectors, float[] C);

        /*
         * Retreieves used by mat
         */
        [DllImport(nameDll, EntryPoint = "dllz_retrieve_measure")]
        private static extern int dllz_retrieve_measure(System.IntPtr ptr, int type, int mem, sl.Resolution resolution);

        [DllImport(nameDll, EntryPoint = "dllz_retrieve_image")]
        private static extern int dllz_retrieve_image(System.IntPtr ptr, int type, int mem, sl.Resolution resolution);

        /// <summary>
        /// Return a string from a pointer to char
        /// </summary>
        /// <param name="ptr"></param>
        /// <returns>the string</returns>
        private static string PtrToStringUtf8(IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
            {
                return "";
            }
            int len = 0;
            while (Marshal.ReadByte(ptr, len) != 0)
                len++;
            if (len == 0)
            {
                return "";
            }
            byte[] array = new byte[len];
            Marshal.Copy(ptr, array, 0, len);
            return System.Text.Encoding.ASCII.GetString(array);
        }

        /// <summary>
        /// Display a console message from c++
        /// </summary>
        /// <param name="message"></param>
        private static void DebugMethod(string message)
        {
            Debug.WriteLine("[ZED plugin]: " + message);
        }

        /// <summary>
        /// Convert a pointer to char to an array of bytes
        /// </summary>
        /// <param name="ptr"></param>
        /// <returns>The array</returns>
        private static byte[] StringUtf8ToByte(string str)
        {
            byte[] array = System.Text.Encoding.ASCII.GetBytes(str);
            return array;
        }

        /// <summary>
        /// Get the max fps for each resolution, higher fps will cause lower GPU performance
        /// </summary>
        /// <param name="reso"></param>
        /// <returns>The resolution</returns>
        static private uint GetFpsForResolution(RESOLUTION reso)
        {
            if (reso == RESOLUTION.HD1080) return 30;
            else if (reso == RESOLUTION.HD2K) return 15;
            else if (reso == RESOLUTION.HD720) return 60;
            else if (reso == RESOLUTION.VGA) return 100;
            return 30;
        }

        /// <summary>
        /// Check if the plugin is available
        /// </summary>
        public static bool CheckPlugin()
        {
            pluginIsReady = false;
            string env = Environment.GetEnvironmentVariable("ZED_SDK_ROOT_DIR");
            if (env != null)
            {
                bool error = CheckDependencies(System.IO.Directory.GetFiles(env + "\\bin"));
                if (error)
                {
                    Debug.WriteLine("ERROR.SDK_NOT_INSTALLED");
                    return false;
                }
                else
                {

                    try
                    {
                        if (dllz_check_plugin(PluginVersion.Major, PluginVersion.Minor) != 0)
                        {
                            Debug.WriteLine("ERROR.SDK_DEPENDENCIES_ISSUE");
                            return false;
                        }
                    }
                    catch (DllNotFoundException)
                    {

                        Debug.WriteLine("ERROR.SDK_DEPENDENCIES_ISSUE");
                        return false;
                    }
                }
            }
            else
            {
                Debug.WriteLine("ERROR.SDK_NOT_INSTALLED");
                return false;
            }

            return true;
        }

        /// <summary>
        /// Checks if all the dlls ara available and try to call a dummy function from the DLL
        /// </summary>
        /// <param name="filesFound"></param>
        /// <returns></returns>
        static private bool CheckDependencies(string[] filesFound)
        {
            bool isASDKPb = false;
            if (filesFound == null)
            {
                return true;
            }
            foreach (string dependency in dependenciesNeeded)
            {
                bool found = false;
                foreach (string file in filesFound)
                {
                    if (System.IO.Path.GetFileName(file).Equals(dependency))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    isASDKPb = true;
                    Debug.WriteLine("[ZED Plugin ] : " + dependency + " is not found");
                }
            }

            return isASDKPb;
        }

        /// <summary>
        /// Gets an instance of the ZEDCamera
        /// </summary>
        /// <returns>The instance</returns>
        public static ZEDCamera GetInstance()
        {
            lock (_lock)
            {
                if (instance == null)
                {
                    instance = new ZEDCamera();
                    if (CheckPlugin())
                        dllz_register_callback_debuger(new DebugCallback(DebugMethod));
                }
                return instance;
            }
        }

        /// <summary>
        /// Private constructor
        /// </summary>
        private ZEDCamera()
        {
            texturesRequested = new List<TextureRequested>();
        }

        /// <summary>
        /// Create a camera in live mode
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="fps"></param>
        public void CreateCamera(bool verbose)
        {

            //string infoSystem = SystemInfo.graphicsDeviceType.ToString().ToUpper();
            //if (!infoSystem.Equals("DIRECT3D11") && !infoSystem.Equals("OPENGLCORE"))
            //{
            //    throw new Exception("The graphic library [" + infoSystem + "] is not supported");
            //}
            dllz_create_camera(verbose);
        }

        /// <summary>
        /// Close the camera and delete all textures
        /// Once destroyed, you need to recreate a camera to restart again
        /// </summary>
        public void Destroy()
        {

            if (instance != null)
            {
                cameraReady = false;
                dllz_close();
                instance = null;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct dll_initParameters
        {
            public sl.RESOLUTION resolution;
            public int cameraFps;
            public int cameraLinuxID;
            [MarshalAs(UnmanagedType.U1)]
            public bool svoRealTimeMode;
            public UNIT coordinateUnit;
            public COORDINATE_SYSTEM coordinateSystem;
            public sl.DEPTH_MODE depthMode;
            public float depthMinimumDistance;
            [MarshalAs(UnmanagedType.U1)]
            public bool cameraImageFlip;
            [MarshalAs(UnmanagedType.U1)]
            public bool enableRightSideMeasure;
            [MarshalAs(UnmanagedType.U1)]
            public bool cameraDisableSelfCalib;
            public int cameraBufferCountLinux;
            [MarshalAs(UnmanagedType.U1)]
            public bool sdkVerbose;
            public int sdkGPUId;
            [MarshalAs(UnmanagedType.U1)]
            public bool depthStabilization;

            public dll_initParameters(InitParameters init)
            {
                resolution = init.resolution;
                cameraFps = init.cameraFPS;
                svoRealTimeMode = init.svoRealTimeMode;
                coordinateUnit = init.coordinateUnit;
                depthMode = init.depthMode;
                depthMinimumDistance = init.depthMinimumDistance;
                cameraImageFlip = init.cameraImageFlip;
                enableRightSideMeasure = init.enableRightSideMeasure;
                cameraDisableSelfCalib = init.cameraDisableSelfCalib;
                cameraBufferCountLinux = init.cameraBufferCountLinux;
                sdkVerbose = init.sdkVerbose;
                sdkGPUId = init.sdkGPUId;
                cameraLinuxID = init.cameraLinuxID;
                coordinateSystem = init.coordinateSystem;
                depthStabilization = init.depthStabilization;
            }
        }

        /// <summary>
        /// The Init function must be called after the instantiation. The function checks if the ZED camera is plugged and opens it, initialize the projection matix and command buffers to update textures
        /// </summary>
        /// <param name="mode_">defines the quality of the depth map, affects the level of details and also the computation time.</param>
        /// <param name="minDist_">specify the minimum depth information that will be computed, in the unit you previously define.</param>
        /// <param name="self_calib">if set to true, it will disable self-calibration and take the initial calibration parameters without optimizing them</param>
        /// <returns>ERROR_CODE : The error code gives information about the
        /// internal process, if SUCCESS is returned, the camera is ready to use.
        /// Every other code indicates an error and the program should be stopped.
        ///
        /// For more details see sl::zed::ERRCODE.</returns>
        public ERROR_CODE Init(ref InitParameters initParameters)
        {

            currentResolution = initParameters.resolution;
            fpsMax = GetFpsForResolution(currentResolution);
            if (initParameters.cameraFPS == 0)
            {
                initParameters.cameraFPS = (int)fpsMax;
            }

            dll_initParameters initP = new dll_initParameters(initParameters);
            initP.coordinateSystem = COORDINATE_SYSTEM.LEFT_HANDED_Y_UP;

            initParameters.sdkVerboseLogFile = "D:/tmp/sdl_log.txt";
            int v = dllz_open(ref initP, new System.Text.StringBuilder(initParameters.pathSVO, initParameters.pathSVO.Length),
               new System.Text.StringBuilder(initParameters.sdkVerboseLogFile, initParameters.sdkVerboseLogFile.Length));

            if ((ERROR_CODE)v != ERROR_CODE.SUCCESS)
            {
                cameraReady = false;
                return (ERROR_CODE)v;
            }

            imageWidth = dllz_get_width();
            imageHeight = dllz_get_height();

            GetCalibrationParameters(false);
            // FillProjectionMatrix();
            //baseline = calibrationParametersRectified.Trans[0];
            fov_H = calibrationParametersRectified.leftCam.hFOV * Deg2Rad;
            fov_V = calibrationParametersRectified.leftCam.vFOV * Deg2Rad;
            cameraModel = GetCameraModel();
            cameraReady = true;
            return (ERROR_CODE)v;
        }

        /// <summary>
        /// Grab a new image, rectifies it and computes the
        /// disparity map and optionally the depth map.
        /// The grabbing function is typically called in the main loop.
        /// </summary>
        /// <param name="sensingMode">defines the type of disparity map, more info : SENSING_MODE definition</param>
        /// <returns>the function returns false if no problem was encountered,
        /// true otherwise.</returns>
        public sl.ERROR_CODE Grab(ref sl.RuntimeParameters runtimeParameters)
        {
            return (sl.ERROR_CODE)dllz_grab(ref runtimeParameters);
        }

        /// <summary>
        ///  The reset function can be called at any time AFTER the Init function has been called.
        ///  It will reset and calculate again correction for misalignment, convergence and color mismatch.
        ///  It can be called after changing camera parameters without needing to restart your executable.
        /// </summary>
        ///
        /// <returns>ERRCODE : error boolean value : the function returns false if no problem was encountered,
        /// true otherwise.
        /// if no problem was encountered, the camera will use new parameters. Otherwise, it will be the old ones
        ///</returns>
        public void ResetSelfCalibration()
        {
            dllz_reset_self_calibration();
        }

        /// <summary>
        /// Creates a file for recording the current frames.
        /// </summary>
        /// <param name="videoFileName">can be a *.svo file or a *.avi file (detected by the suffix name provided)</param>
        /// <param name="compressionMode">can be one of the sl.SVO_COMPRESSION_MODE enum</param>
        /// <returns>an sl.ERRCODE that defines if file was successfully created and can be filled with images</returns>
        public ERROR_CODE EnableRecording(string videoFileName, SVO_COMPRESSION_MODE compressionMode = SVO_COMPRESSION_MODE.LOSSLESS_BASED)
        {
            return (ERROR_CODE)dllz_enable_recording(StringUtf8ToByte(videoFileName), (int)compressionMode);
        }

        /// <summary>
        /// Record the images, EnableRecording needs to be called before.
        /// </summary>
        public Recording_state Record()
        {
            Recording_state state = new Recording_state();
            dllz_record(ref state);
            return state;
        }

        /// <summary>
        /// Stops the recording and closes the file.
        /// </summary>
        /// <returns></returns>
        public bool DisableRecording()
        {
            return dllz_disable_recording();
        }

        /// <summary>
        /// Set a new frame rate for the camera, or the closest available frame rate.
        /// </summary>
        /// <param name="fps"></param>
        /// <returns></returns>
        public void SetCameraFPS(int fps)
        {
            if (GetFpsForResolution(currentResolution) >= fps)
            {
                fpsMax = (uint)fps;
            }

            dllz_set_camera_fps(fps);
        }

        /// <summary>
        /// Sets the position of the SVO file to a desired frame.
        /// </summary>
        /// <param name="frame"> the number of the desired frame to be decoded.</param>
        /// <returns></returns>
        public void SetSVOPosition(int frame)
        {
            dllz_set_svo_position(frame);
        }

        /// <summary>
        /// Gets the current confidence threshold value for the disparity map (and by extension the depth map).
        /// </summary>
        /// <returns>current filtering value between 0 and 100.</returns>
        public int GetConfidenceThreshold()
        {
            return dllz_get_confidence_threshold();
        }

        /// <summary>
        /// Get the time stamp at the time the frame has been extracted from USB stream. (should be called after a grab())
        /// </summary>
        /// <returns>Current time stamp in ns. -1 is not available(SVO file without compression).
        /// Note that new SVO file from SDK 1.0.0 (with compression) contains the camera time stamp for each frame.</returns>
        public ulong GetCameraTimeStamp()
        {
            return dllz_get_camera_timestamp();
        }

        /// <summary>
        /// Get the current time stamp at the time the function is called. Can be compared to the camera time stamp for synchronization.
        /// Use this function to compare the current time stamp and the camera time stamp, since they have the same reference (Computer start time).
        /// </summary>
        /// <returns>The timestamp</returns>
        public ulong GetCurrentTimeStamp()
        {
            return dllz_get_current_timestamp();
        }

        /// <summary>
        /// Last time stamp from image update, (based on the computer time stamp)
        /// </summary>
        /// <returns>The timestamp</returns>
        public ulong GetImageUpdaterTimeStamp()
        {
            return dllz_get_image_updater_time_stamp();
        }

        /// <summary>
        /// Get the current position of the SVO in the record
        /// </summary>
        /// <returns>The position</returns>
        public int GetSVOPosition()
        {
            return dllz_get_svo_position();
        }

        /// <summary>
        /// Get the number of frames in the SVO file.
        /// </summary>
        /// <returns>SVO Style Only : the total number of frames in the SVO file(-1 if the SDK is not reading a SVO)</returns>
        public int GetSVONumberOfFrames()
        {
            return dllz_get_svo_number_of_frames();
        }

        /// <summary>
        /// Get the closest measurable distance by the camera, according to the camera and the depth map parameters.
        /// </summary>
        /// <returns>The closest depth</returns>
        public float GetDepthMinRangeValue()
        {
            return dllz_get_depth_min_range_value();
        }

        /// <summary>
        /// Returns the current maximum distance of depth/disparity estimation.
        /// </summary>
        /// <returns>The closest depth</returns>
        public float GetDepthMaxRangeValue()
        {
            return dllz_get_depth_max_range_value();
        }

        /// <summary>
        ///  Stop the motion tracking, if you want to restart, call enableTracking().
        /// </summary>
        /// <param name="path">The path to save the area file</param>
        public void DisableTracking(string path = "")
        {
            dllz_disable_tracking(new System.Text.StringBuilder(path, path.Length));
        }

        public sl.ERROR_CODE SaveCurrentArea(string path)
        {
            return (sl.ERROR_CODE)dllz_save_current_area(new System.Text.StringBuilder(path, path.Length));
        }

        /// <summary>
        /// Returns the current state of the area learning saving
        /// </summary>
        /// <returns></returns>
        public sl.AREA_EXPORT_STATE GetAreaExportState()
        {
            return (sl.AREA_EXPORT_STATE)dllz_get_area_export_state();
        }

        /// <summary>
        /// Width of the images returned by the ZED
        /// </summary>
        public int ImageWidth {
            get {
                return imageWidth;
            }
        }

        /// <summary>
        /// Returns the height of the image
        /// </summary>
        public int ImageHeight {
            get {
                return imageHeight;
            }
        }

        /// <summary>
        /// Sets a filtering value for the disparity map (and by extension the depth map). The function should be called before the grab to be taken into account.
        /// </summary>
        /// <param name="threshold"> a value in [1,100]. A lower value means more confidence and precision (but less density), an upper value reduces the filtering (more density, less certainty). Other value means no filtering.
        ///</param>
        public void SetConfidenceThreshold(int threshold)
        {
            dllz_set_confidence_threshold(threshold);
        }

        /// <summary>
        /// Set the maximum distance of depth/disparity estimation (all values after this limit will be reported as TOO_FAR value)
        /// </summary>
        /// <param name="distanceMax"> maximum distance in the defined UNIT</param>
        public void SetDepthMaxRangeValue(float distanceMax)
        {
            dllz_set_depth_max_range_value(distanceMax);
        }

        /// <summary>
        /// Returns the current fps
        /// </summary>
        /// <returns>The current fps</returns>
        public float GetCameraFPS()
        {
            return dllz_get_camera_fps();
        }

        public float GetRequestedCameraFPS()
        {
            return fpsMax;
        }

        public CalibrationParameters GetCalibrationParameters(bool raw = false)
        {

            IntPtr p = dllz_get_calibration_parameters(raw);

            if (p == IntPtr.Zero)
            {
                return new CalibrationParameters();
            }
            CalibrationParameters parameters = (CalibrationParameters)Marshal.PtrToStructure(p, typeof(CalibrationParameters));

            if (raw)
                calibrationParametersRaw = parameters;
            else
                calibrationParametersRectified = parameters;

            return parameters;

        }

        /// <summary>
        /// Gets the zed camera model
        /// </summary>
        /// <returns>The camera model (sl.MODEL)</returns>
        public sl.MODEL GetCameraModel()
        {
            return (sl.MODEL)dllz_get_camera_model();
        }

        /// <summary>
        /// Gets the zed firmware
        /// </summary>
        /// <returns>The firmware</returns>
        public int GetZEDFirmwareVersion()
        {
            return dllz_get_zed_firmware();
        }

        /// <summary>
        /// Gets the zed firmware
        /// </summary>
        /// <returns>The serial number</returns>
        public int GetZEDSerialNumber()
        {
            return dllz_get_zed_serial();
        }

        /// <summary>
        /// Returns the vertical field of view in radians
        /// </summary>
        /// <returns>The field of view</returns>
        public float GetFOV()
        {
            return GetCalibrationParameters(false).leftCam.vFOV * Deg2Rad;
        }

        /// <summary>
        /// Gets the calibration status
        /// </summary>
        /// <returns>The calibration status</returns>
        public ZED_SELF_CALIBRATION_STATE GetSelfCalibrationStatus()
        {
            return (ZED_SELF_CALIBRATION_STATE)dllz_get_self_calibration_state();
        }

        /// <summary>
        /// Compute textures from the ZED, the new textures will not be displayed until an event is sent to the Render Thread.
        /// </summary>
        public void RetrieveTextures()
        {
            dllz_retrieve_textures();
        }

        /// <summary>
        /// swap textures between acquisition and rendering thread
        /// </summary>
        public void SwapTextures()
        {
            dllz_swap_textures();
        }

        public ulong GetImagesTimeStamp()
        {
            return dllz_get_updated_textures_timestamp();
        }

        /// <summary>
        /// Get the number of frame dropped since grab() has been called for the first time
        /// Based on camera time stamp and fps comparison.
        /// </summary>
        /// <returns>number of frame dropped since first grab() call.</returns>
        public uint GetFrameDroppedCount()
        {
            return dllz_get_frame_dropped_count();
        }

        /// <summary>
        /// Get the percentage  of frame dropped since grab() has been called for the first time
        /// </summary>
        /// <returns>number (percentage) of frame dropped.</returns>
        public float GetFrameDroppedPercent()
        {
            return dllz_get_frame_dropped_percent();
        }

        /// <summary>
        /// Set settings of the camera
        /// </summary>
        /// <param name="settings">The setting which will be changed</param>
        /// <param name="value">The value</param>
        /// <param name="usedefault">will set default (or automatic) value if set to true (value (int) will not be taken into account)</param>
        public void SetCameraSettings(CAMERA_SETTINGS settings, int value, bool usedefault = false)
        {
            cameraSettingsManager.SetCameraSettings(settings, value, usedefault);
        }

        /// <summary>
        /// Get the value from a setting of the camera
        /// </summary>
        /// <param name="settings"></param>
        public int GetCameraSettings(CAMERA_SETTINGS settings)
        {
            AssertCameraIsReady();
            return cameraSettingsManager.GetCameraSettings(settings);
        }

        /// <summary>
        /// Load the camera settings (brightness, contrast, hue, saturation, gain, exposure)
        /// </summary>
        /// <param name="path"></param>
        public void LoadCameraSettings(string path)
        {
            cameraSettingsManager.LoadCameraSettings(instance, path);
        }

        /// <summary>
        /// Save the camera settings (brightness, contrast, hue, saturation, gain, exposure)
        /// </summary>
        /// <param name="path"></param>
        public void SaveCameraSettings(string path)
        {
            cameraSettingsManager.SaveCameraSettings(path);
        }

        /// <summary>
        /// Retrirves camera settings from the camera
        /// </summary>
        public void RetrieveCameraSettings()
        {
            cameraSettingsManager.RetrieveSettingsCamera(instance);
        }

        /// <summary>
        /// Returns a copy of the camera settings, cannot be modified
        /// </summary>
        /// <returns></returns>
        public ZEDCameraSettingsManager.CameraSettings GetCameraSettings()
        {
            return cameraSettingsManager.Settings;
        }

        /// <summary>
        /// Return the state of the exposure (true = automatic, false = manual)
        /// </summary>
        /// <returns></returns>
        public bool GetExposureUpdateType()
        {
            return cameraSettingsManager.auto;
        }

        /// <summary>
        /// Return the state of the white balance (true = automatic, false = manual)
        /// </summary>
        /// <returns></returns>
        public bool GetWhiteBalanceUpdateType()
        {
            return cameraSettingsManager.whiteBalanceAuto;
        }

        /// <summary>
        /// Set all the settings registered, to the camera
        /// </summary>
        public void SetCameraSettings()
        {
            cameraSettingsManager.SetSettings(instance);
        }

        /// <summary>
        /// The function checks if ZED cameras are connected, can be called before instantiating a Camera object
        /// </summary>
        /// <remarks> On Windows, only one ZED is accessible so this function will return 1 even if multiple ZED are connected.</remarks>
        /// <returns>the number of connected ZED</returns>
        public static bool IsZedConnected()
        {
            return Convert.ToBoolean(dllz_is_zed_connected());
        }

        /// <summary>
        /// The function return the version of the currently installed ZED SDK
        /// </summary>
        /// <returns>ZED SDK version as a string with the following format : MAJOR.MINOR.PATCH</returns>
        public static string GetSDKVersion()
        {
            return PtrToStringUtf8(dllz_get_sdk_version());
        }

        /// <summary>
        /// Check if camera has been initialized, or if the plugin has been loaded
        /// </summary>
        private void AssertCameraIsReady()
        {
            if (!cameraReady || !pluginIsReady)
                throw new Exception("Camera is not connected, init was not called or a dependency problem occurred");
        }

        /// <summary>
        /// Update the internal version of the mesh and return the sizes of the meshes.
        /// </summary>
        /// <param name="nbVerticesInSubemeshes"> Vector containing the number of vertices in each submeshes. </param>
        /// <param name="nbTrianglesInSubemeshes"> Vector containing the number of triangles in each submeshes. </param>
        /// <param name="nbSubmeshes"> Number of submeshes. </param>
        /// <param name="updatedIndices"> List of the submeshes updated since the last update. </param>
        /// <param name="nbVertices"> Total number of updated vertices for all submeshes. </param>
        /// <param name="nbTriangles"> Total number of updated triangles for all submeshes. </param>
        /// <param name="nbSubmeshMax"> Maximal number of submeshes handle in the script. </param>
        /// <returns></returns>
        public sl.ERROR_CODE UpdateMesh(int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmeshMax)
        {
            sl.ERROR_CODE err = sl.ERROR_CODE.FAILURE;
            err = (sl.ERROR_CODE)dllz_update_mesh(nbVerticesInSubemeshes, nbTrianglesInSubemeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, nbSubmeshMax);
            return err;
        }

        /// <summary>
        /// Starts the mesh generation process in a non blocking thread from the spatial mapping process.
        /// </summary>
        public void RequestMesh()
        {
            dllz_request_mesh_async();
        }

        /// <summary>
        /// Switches the pause status of the data integration mechanism for the spatial mapping.
        /// </summary>
        /// <param name="status"> if true, the integration is paused. If false, the spatial mapping is resumed.</param>
        public void PauseSpatialMapping(bool status)
        {
            dllz_pause_spatial_mapping(status);
        }

        /// <summary>
        /// Returns the mesh generation status, useful to after calling requestMeshAsync.
        /// </summary>
        /// <returns> If true, the integration is paused. If false, the spatial mapping is resumed.</returns>
        public sl.ERROR_CODE GetMeshRequestStatus()
        {
            return (sl.ERROR_CODE)dllz_get_mesh_request_status_async();
        }

        /// <summary>
        /// Save the mesh in a specific file format
        /// </summary>
        /// <param name="filename">the path and filename of the mesh.</param>
        /// <param name="format">defines the file format (extension).</param>
        /// <returns></returns>
        public bool SaveMesh(string filename, MESH_FILE_FORMAT format)
        {
            return dllz_save_mesh(filename, format);
        }

        /// <summary>
        /// Loads a mesh
        /// </summary>
        /// <param name="filename"> The path and filename of the mesh (do not forget the extension). </param>
        /// <param name="nbVerticesInSubemeshes"> Vector containing the number of vertices in each submeshes. </param>
        /// <param name="nbTrianglesInSubemeshes"> Vector containing the number of triangles in each submeshes. </param>
        /// <param name="nbSubmeshes"> Number of submeshes. </param>
        /// <param name="updatedIndices"> List of the submeshes updated since the last update. </param>
        /// <param name="nbVertices"> Total number of updated vertices for all submeshes. </param>
        /// <param name="nbTriangles"> Total number of updated triangles for all submeshes. </param>
        /// <param name="nbSubmeshMax"> Maximal number of submeshes handle in the script. </param>
        /// <param name="textureSize"> Vector containing the size of all the texture (width, height). </param>
        /// <returns></returns>
        public bool LoadMesh(string filename, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmeshMax, int[] textureSize = null)
        {
            return dllz_load_mesh(filename, nbVerticesInSubemeshes, nbTrianglesInSubemeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, nbSubmeshMax, textureSize);
        }

        /// <summary>
        /// Filters a mesh; less triangles
        /// </summary>
        /// <param name="filterParameters"> Parameters for the optional filtering step of a mesh </param>
        /// <param name="nbVerticesInSubemeshes"> Vector containing the number of vertices in each submeshes after filtering. </param>
        /// <param name="nbTrianglesInSubemeshes"> Vector containing the number of triangles in each submeshes after filtering. </param>
        /// <param name="nbSubmeshes"> Number of submeshes after filtering. </param>
        /// <param name="updatedIndices"> List of the submeshes updated by the filter. </param>
        /// <param name="nbVertices"> Total number of updated vertices for all submeshes after filtering. </param>
        /// <param name="nbTriangles"> Total number of updated triangles for all submeshes after filtering. </param>
        /// <param name="nbSubmeshMax"> Maximal number of submeshes handle in the script. </param>
        /// <returns></returns>
        public bool FilterMesh(FILTER filterParameters, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmeshMax)
        {
            return dllz_filter_mesh(filterParameters, nbVerticesInSubemeshes, nbTrianglesInSubemeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, nbSubmeshMax);
        }

        /// Apply the texture on the internal mesh, you will need to call RetrieveMesh with uvs and textures to get the mesh
        /// </summary>
        /// <param name="nbVerticesInSubemeshes"> Vector containing the number of vertices in each textured submeshes. </param>
        /// <param name="nbTrianglesInSubemeshes"> Vector containing the number of triangles in each textured submeshes after filtering. </param>
        /// <param name="nbSubmeshes"> Number of submeshes after filtering. </param>
        /// <param name="updatedIndices"> List of the submeshes updated by the texture mapping. </param>
        /// <param name="nbVertices"> Total number of updated vertices for all submeshes. </param>
        /// <param name="nbTriangles"> Total number of updated triangles for all submeshes. </param>
        /// <param name="textureSize"> Vector containing the size of all the texture (width, height). </param>
        /// <param name="nbSubmeshMax"> Maximal number of submeshes handle in the script. </param>
        /// <returns></returns>
        public bool ApplyTexture(int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int[] textureSize, int nbSubmeshMax)
        {
            return dllz_apply_texture(nbVerticesInSubemeshes, nbTrianglesInSubemeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, textureSize, nbSubmeshMax);
        }

        /// <summary>
        /// Get the current state of spatial mapping
        /// </summary>
        /// <returns></returns>
        public SPATIAL_MAPPING_STATE GetSpatialMappingState()
        {
            return (sl.SPATIAL_MAPPING_STATE)dllz_get_spatial_mapping_state();
        }

        /// <summary>
        /// Reshape the chunks, to get more mesh per chunks and less chunks
        /// </summary>
        /// <param name="numberFaces"></param>
        /// <param name="nbVerticesInSubemeshes"></param>
        /// <param name="nbTrianglesInSubemeshes"></param>
        /// <param name="nbSubmeshes"></param>
        /// <param name="updatedIndices"></param>
        /// <param name="nbVertices"></param>
        /// <param name="nbTriangles"></param>
        /// <param name="nbSubmesh"></param>
        public void MergeChunks(int numberFaces, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh)
        {
            dllz_spatial_mapping_merge_chunks(numberFaces, nbVerticesInSubemeshes, nbTrianglesInSubemeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, nbSubmesh);
        }

        /// <summary>
        /// Retrieves images for a specific mat
        /// </summary>
        /// <param name="mat"></param>
        /// <param name="measure"></param>
        /// <param name="mem"></param>
        /// <param name="resolution"></param>
        /// <returns></returns>
        public sl.ERROR_CODE RetrieveMeasure(sl.ZEDMat mat, sl.MEASURE measure, sl.ZEDMat.MEM mem = sl.ZEDMat.MEM.MEM_CPU, sl.Resolution resolution = new sl.Resolution())
        {
            return (sl.ERROR_CODE)(dllz_retrieve_measure(mat.MatPtr, (int)measure, (int)mem, resolution));
        }

        /// <summary>
        /// Retrieves measures for a specific mat
        /// </summary>
        /// <param name="mat"></param>
        /// <param name="view"></param>
        /// <param name="mem"></param>
        /// <param name="resolution"></param>
        /// <returns></returns>
        public sl.ERROR_CODE RetrieveImage(sl.ZEDMat mat, sl.VIEW view, sl.ZEDMat.MEM mem = sl.ZEDMat.MEM.MEM_CPU, sl.Resolution resolution = new sl.Resolution())
        {
            return (sl.ERROR_CODE)(dllz_retrieve_image(mat.MatPtr, (int)view, (int)mem, resolution));
        }

    }
} // namespace sl

//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============

using System.Runtime.InteropServices;

/// <summary>
/// Stores the camera settings and is used as an interface with the ZED
/// </summary>
public class ZEDCameraSettingsManager {

    const string nameDll = "sl_unitywrapper";
    [DllImport(nameDll, EntryPoint = "dllz_set_camera_settings")]
    private static extern void dllz_set_camera_settings(int mode, int value, int usedefault);

    [DllImport(nameDll, EntryPoint = "dllz_get_camera_settings")]
    private static extern int dllz_get_camera_settings(int mode);

    /// <summary>
    /// Container of the camera settings
    /// </summary>
    public class CameraSettings
    {
        public int[] settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];

        public CameraSettings(int brightness = 4, int contrast = 4, int hue = 0, int saturation = 4, int whiteBalance = -1, int gain = -1, int exposure = -1)
        {
            settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
            settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = brightness;
            settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = contrast;
            settings[(int)sl.CAMERA_SETTINGS.SATURATION] = saturation;
            settings[(int)sl.CAMERA_SETTINGS.HUE] = hue;
            settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = whiteBalance;
            settings[(int)sl.CAMERA_SETTINGS.GAIN] = gain;
            settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = exposure;
        }

        public CameraSettings(CameraSettings other)
        {
            settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
            settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = other.settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS];
            settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = other.settings[(int)sl.CAMERA_SETTINGS.CONTRAST];
            settings[(int)sl.CAMERA_SETTINGS.SATURATION] = other.settings[(int)sl.CAMERA_SETTINGS.SATURATION];
            settings[(int)sl.CAMERA_SETTINGS.HUE] = other.settings[(int)sl.CAMERA_SETTINGS.HUE];
            settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = other.settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE];
            settings[(int)sl.CAMERA_SETTINGS.GAIN] = other.settings[(int)sl.CAMERA_SETTINGS.GAIN];
            settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = other.settings[(int)sl.CAMERA_SETTINGS.EXPOSURE];
        }

        public CameraSettings Clone()
        {
            return new CameraSettings(this);
        }

        public int Brightness
        {
            get
            {
                return settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS];
            }

            set
            {
                settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = value;
            }
        }

        public int Saturation
        {
            get
            {
                return settings[(int)sl.CAMERA_SETTINGS.SATURATION];
            }

            set
            {
                settings[(int)sl.CAMERA_SETTINGS.SATURATION] = value;
            }
        }

        public int Hue
        {
            get
            {
                return settings[(int)sl.CAMERA_SETTINGS.HUE];
            }

            set
            {
                settings[(int)sl.CAMERA_SETTINGS.HUE] = value;
            }
        }

        public int Contrast
        {
            get
            {
                return settings[(int)sl.CAMERA_SETTINGS.CONTRAST];
            }

            set
            {
                settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = value;
            }
        }

        public int Gain
        {
            get
            {
                return settings[(int)sl.CAMERA_SETTINGS.GAIN];
            }

            set
            {
                settings[(int)sl.CAMERA_SETTINGS.GAIN] = value;
            }
        }

        public int Exposure
        {
            get
            {
                return settings[(int)sl.CAMERA_SETTINGS.EXPOSURE];
            }

            set
            {
                settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = value;
            }
        }
        public int WhiteBalance
        {
            get
            {
                return settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE];
            }

            set
            {
                settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = value;
            }
        }

    }
    /// <summary>
    /// Reference to the container
    /// </summary>
    private CameraSettings settings_;
    public CameraSettings Settings
    {
        get { return settings_.Clone(); }
    }

    /// <summary>
    /// Is in auto mode
    /// </summary>
    public bool auto = true;
    public bool whiteBalanceAuto = true;

    public ZEDCameraSettingsManager()
    {
        settings_ = new CameraSettings();
    }

    /// <summary>
    /// Set settings from the container to the camera
    /// </summary>
    /// <param name="zedCamera"></param>
    public void SetSettings(sl.ZEDCamera zedCamera)
    {
        if (zedCamera != null)
        {
            zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, settings_.Brightness, false);
            zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, settings_.Contrast, false);
            zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, settings_.Hue, false);
            zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, settings_.Saturation, false);
            zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, settings_.Gain, false);
            zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, settings_.Exposure, false);
            if (settings_.WhiteBalance != -1)
            {
                zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, settings_.WhiteBalance, false);
            }
        } 
    }

    /// <summary>
    /// Load camera settings from a file, and set them to the container and camera
    /// </summary>
    /// <param name="zedCamera"></param>
    /// <param name="path"></param>
    public void LoadCameraSettings(sl.ZEDCamera zedCamera, string path = "ZED_Settings.conf")
    {

        string[] lines = null;
        try
        {
            lines = System.IO.File.ReadAllLines(path);
        }
        catch (System.Exception)
        {

        }
        if (lines == null) return;

        foreach (string line in lines)
        {
            string[] splittedLine = line.Split('=');
            if (splittedLine.Length == 2)
            {
                string key = splittedLine[0];
                string field = splittedLine[1];

                if (key == "brightness")
                {
                    settings_.Brightness = int.Parse(field);
                }
                else if (key == "contrast")
                {
                    settings_.Contrast = int.Parse(field);
                }
                else if (key == "hue")
                {
                    settings_.Hue = int.Parse(field);
                }
                else if (key == "saturation")
                {
                    settings_.Saturation = int.Parse(field);
                }
                else if (key == "whiteBalance")
                {
                    settings_.WhiteBalance = int.Parse(field);
                }
                else if (key == "gain")
                {
                    settings_.Gain = int.Parse(field);
                }
                else if (key == "exposure")
                {
                    settings_.Exposure = int.Parse(field);
                }
            }
        }
        SetSettings(zedCamera);
        auto = (settings_.Exposure == -1);
        whiteBalanceAuto = (settings_.WhiteBalance == -1);
    }

    /// <summary>
    /// Retrieves settings from the camera
    /// </summary>
    /// <param name="zedCamera"></param>
    public void RetrieveSettingsCamera(sl.ZEDCamera zedCamera)
    {
        if (zedCamera != null)
        {
            settings_.Brightness = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS);
            settings_.Contrast = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST);
            settings_.Hue = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.HUE);
            settings_.Saturation = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.SATURATION);
            settings_.Gain = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.GAIN);
            settings_.Exposure = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE);
            settings_.WhiteBalance = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE);
        } 
    }

    /// <summary>
    /// Set settings of the camera
    /// </summary>
    /// <param name="settings">The setting which will be changed</param>
    /// <param name="value">The value</param>
    /// <param name="usedefault">will set default (or automatic) value if set to true (value (int) will not be taken into account)</param>
    public void SetCameraSettings(sl.CAMERA_SETTINGS settings, int value, bool usedefault = false)
    {
        settings_.settings[(int)settings] = !usedefault && value != -1 ? value : -1;
        dllz_set_camera_settings((int)settings, value, System.Convert.ToInt32(usedefault));
    }

    /// <summary>
    /// Get the value from a setting of the camera
    /// </summary>
    /// <param name="settings"></param>
    public int GetCameraSettings(sl.CAMERA_SETTINGS settings)
    {
        settings_.settings[(int)settings] = dllz_get_camera_settings((int)settings);
        return settings_.settings[(int)settings];
    }

    /// <summary>
    /// Save the camera settings into a file
    /// </summary>
    /// <param name="path"></param>
    public void SaveCameraSettings(string path)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
        {
            file.WriteLine("brightness=" + settings_.Brightness.ToString());
            file.WriteLine("contrast=" + settings_.Contrast.ToString());
            file.WriteLine("hue=" + settings_.Hue.ToString());
            file.WriteLine("saturation=" + settings_.Saturation.ToString());
            file.WriteLine("whiteBalance=" + settings_.WhiteBalance.ToString());
            file.WriteLine("gain=" + settings_.Gain.ToString());
            file.WriteLine("exposure=" + settings_.Exposure.ToString());
            file.Close();
        }
    }
}

//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============

using System.Runtime.InteropServices;

namespace sl
{
    /// <summary>
    /// Resolution of the current camera
    /// </summary>
    public struct Resolution
    {
        public Resolution(uint width, uint height)
        {
            this.width = (System.UIntPtr)width;
            this.height = (System.UIntPtr)height;
        }

        public System.UIntPtr width;
        public System.UIntPtr height;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct Pose
    {
        public bool valid;
        public ulong timestap;
        public int pose_confidence;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct CameraParameters
    {
        /// <summary>
        /// Focal x
        /// </summary>
        public float fx;
        /// <summary>
        /// Focal y
        /// </summary>
        public float fy;
        /// <summary>
        /// Optical center x
        /// </summary>
        public float cx;
        /// <summary>
        /// Optical center y
        /// </summary>
        public float cy;
        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U8, SizeConst = 5)]
        public double[] disto;
        /// <summary>
        /// Vertical field of view after stereo rectification
        /// </summary>
        public float vFOV;
        /// <summary>
        /// Horizontal field of view after stereo rectification
        /// </summary>
        public float hFOV;
        /// <summary>
        /// Diagonal field of view after stereo rectification
        /// </summary>
        public float dFOV;
        public Resolution resolution;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct CalibrationParameters
    {
        /// <summary>
        /// Parameters of the left camera
        /// </summary>
        public CameraParameters leftCam;
        /// <summary>
        /// Parameters of the right camera
        /// </summary>
        public CameraParameters rightCam;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct Recording_state
    {
        /// <summary>
        /// status of current frame. May be true for success or false if frame could not be written in the SVO file
        /// </summary>
        public bool status;
        /// <summary>
        /// compression time for the current frame in ms
        /// </summary>
        public double current_compression_time;
        /// <summary>
        /// compression ratio (% of raw size) for the current frame
        /// </summary>
        public double current_compression_ratio;
        /// <summary>
        /// average compression time in ms since beginning of recording
        /// </summary>
        public double average_compression_time;
        /// <summary>
        /// compression ratio (% of raw size) since beginning of recording
        /// </summary>
        public double average_compression_ratio;
    }

    /// <summary>
    /// Status for self calibration. Since v0.9.3, self-calibration is done in background and start in the sl.ZEDCamera.Init or Reset function.
    /// </summary>
    public enum ZED_SELF_CALIBRATION_STATE
    {
        /// <summary>
        /// Self Calibration has not yet been called (no init called)
        /// </summary>
        SELF_CALIBRATION_NOT_CALLED,
        /// <summary>
        /// Self Calibration is currently running.
        /// </summary>
        SELF_CALIBRATION_RUNNING,
        /// <summary>
        /// Self Calibration has finished running but did not manage to get coherent values. Old Parameters are taken instead.
        /// </summary>
        SELF_CALIBRATION_FAILED,
        /// <summary>
        /// Self Calibration has finished running and did manage to get coherent values. New Parameters are taken.
        /// </summary>
        SELF_CALIBRATION_SUCCESS
    };

    /// <summary>
    /// List available depth computation modes.
    /// </summary>
    public enum DEPTH_MODE
    {
        /// <summary>
        /// This mode does not compute any depth map. Only rectified stereo images will be available.
        /// </summary>
        NONE,
        /// <summary>
        ///  Fastest mode for depth computation.
        /// </summary>
        PERFORMANCE,
        /// <summary>
        /// Balanced quality mode. Depth map is robust in any environment and requires medium resources for computation
        /// </summary>
        MEDIUM,
        /// <summary>
        /// Best quality mode. Requires more compute power.
        /// </summary>
        QUALITY,
        /// <summary>
        /// native depth. Requires more compute power.
        /// </summary>
        ULTRA,
    };

    public enum VIEW_MODE
    {

        /// <summary>
        /// Eyes will display images (left and right) (default)
        /// </summary>
        VIEW_IMAGE,
        /// <summary>
        /// Eyes will display depth (left and right)
        /// </summary>
        VIEW_DEPTH,
        /// <summary>
        /// Eyes will display normals (left and right)
        /// </summary>
        VIEW_NORMALS

    };

    /// <summary>
    /// List error codes in the ZED SDK.
    /// </summary>
    public enum ERROR_CODE
    {
        /// <summary>
        /// Operation success
        /// </summary>
        SUCCESS ,
        /// <summary>
        /// Standard code for unsuccessful behavior
        /// </summary>
        FAILURE,
        /// <summary>
        /// No GPU found or CUDA capability of the device is not supported
        /// </summary>
        NO_GPU_COMPATIBLE,
        /// <summary>
        /// Not enough GPU memory for this depth mode, try a different mode (such as PERFORMANCE).
        /// </summary>
        NOT_ENOUGH_GPUMEM,
        /// <summary>
        /// The ZED camera is not plugged or detected.
        /// </summary>
        CAMERA_NOT_DETECTED,
        /// <summary>
        /// a ZED-M camera is detected but the inertial sensor cannot be opened. Only for ZED-M device
        /// </summary>
        SENSOR_NOT_DETECTED,
        /// <summary>
        /// For Nvidia Jetson X1 only, resolution not yet supported (USB3.0 bandwidth)
        /// </summary>
        INVALID_RESOLUTION,
        /// <summary>
        /// This issue can occurs when the camera FPS cannot be reached, due to a lot of corrupted frames. Try to change USB port.
        /// </summary>
        LOW_USB_BANDWIDTH,
        /// <summary>
        /// ZED calibration file is not found on the host machine. Use ZED Explorer or ZED Calibration to get one
        /// </summary>
        CALIBRATION_FILE_NOT_AVAILABLE, 
        /// <summary>
        /// ZED calibration file is not valid, try to download the factory one or recalibrate your camera using 'ZED Calibration
        /// </summary>
        INVALID_CALIBRATION_FILE, 
        /// <summary>
        /// The provided SVO file is not valid.
        /// </summary>
        INVALID_SVO_FILE,
        /// <summary>
        /// An recorder related error occurred (not enough free storage, invalid file)
        /// </summary>
        SVO_RECORDING_ERROR,
        /// <summary>
        /// The requested coordinate system is not available
        /// </summary>
        INVALID_COORDINATE_SYSTEM, 
        /// <summary>
        /// The firmware of the ZED is out of date. Update to the latest version.
        /// </summary>
        INVALID_FIRMWARE,
        /// <summary>
        ///  An invalid parameter has been set for the function
        /// </summary>
        INVALID_FUNCTION_PARAMETERS,
        /// <summary>
        /// In grab() only, the current call return the same frame as last call. Not a new frame
        /// </summary>
        NOT_A_NEW_FRAME,
        /// <summary>
        /// In grab() only, a CUDA error has been detected in the process. Activate verbose in sl::Camera::open for more info
        /// </summary>
        CUDA_ERROR,
        /// <summary>
        /// In grab() only, ZED SDK is not initialized. Probably a missing call to sl::Camera::open
        /// </summary>
        CAMERA_NOT_INITIALIZED, 
        /// <summary>
        /// Your NVIDIA driver is too old and not compatible with your current CUDA version.
        /// </summary>
        NVIDIA_DRIVER_OUT_OF_DATE, 
        /// <summary>
        /// The call of the function is not valid in the current context. Could be a missing call of sl::Camera::open.
        /// </summary>
        INVALID_FUNCTION_CALL, 
        /// <summary>
        ///  The SDK wasn't able to load its dependencies, the installer should be launched.
        /// </summary>
        CORRUPTED_SDK_INSTALLATION, 
        /// <summary>
        /// The installed SDK is incompatible SDK used to compile the program
        /// </summary>
        INCOMPATIBLE_SDK_VERSION,
        /// <summary>
        /// The given area file does not exist, check the path
        /// </summary>
        INVALID_AREA_FILE, 
        /// <summary>
        /// The area file does not contain enought data to be used or the sl::DEPTH_MODE used during the creation of the area file is different from the one currently set.
        /// </summary>
        INCOMPATIBLE_AREA_FILE,
        /// <summary>
        /// Camera failed to setup
        /// </summary>
        CAMERA_FAILED_TO_SETUP,
        /// <summary>
        /// Your ZED can not be opened, try replugging it to another USB port or flipping the USB-C connector
        /// </summary>
        CAMERA_DETECTION_ISSUE,
        /// <summary>
        /// The Camera is already used by another process
        /// </summary>
        CAMERA_ALREADY_IN_USE, 
        /// <summary>
        /// No GPU found, CUDA is unable to list it. Can be a driver/reboot issue
        /// </summary>
        NO_GPU_DETECTED,
        /// <summary>
        /// Plane not found, either no plane is detected in the scene, at the location or corresponding to the floor, or the floor plane doesn't match the prior given
        /// </summary>
        ERROR_CODE_PLANE_NOT_FOUND, 
        /// <summary>
        ///  end of error code (used before init)
        /// </summary>
        ERROR_CODE_LAST
    };

    /// <summary>
    /// Represents the available resolution
    /// </summary>
    public enum RESOLUTION
    {
        /// <summary>
        /// 2208*1242, supported frame rate : 15 fps
        /// </summary>
        HD2K,
        /// <summary>
        /// 1920*1080, supported frame rates : 15, 30 fps
        /// </summary>
        HD1080,
        /// <summary>
        /// 1280*720, supported frame rates : 15, 30, 60 fps
        /// </summary>
        HD720,
        /// <summary>
        /// 672*376, supported frame rates : 15, 30, 60, 100 fps
        /// </summary>
        VGA
    };

    /// <summary>
    /// List available depth sensing modes.
    /// </summary>
    public enum MODEL
    {
        ZED, /**< ZED camera.*/
        ZED_M /**< ZED M device.*/
    };

    /// <summary>
    /// List available depth sensing modes.
    /// </summary>
    public enum SENSING_MODE
    {
        /// <summary>
        /// This mode outputs ZED standard depth map that preserves edges and depth accuracy.
        /// Applications example: Obstacle detection, Automated navigation, People detection, 3D reconstruction
        /// </summary>
        STANDARD,
        /// <summary>
        /// This mode outputs a smooth and fully dense depth map.
        /// Applications example: AR/VR, Mixed-reality capture, Image post-processing
        /// </summary>
        FILL
    };

    /// <summary>
    /// List available views.
    /// </summary>
    public enum VIEW
    {
        /// <summary>
        /// Left RGBA image, sl::MAT_TYPE_8U_C4.
        /// </summary>
        LEFT,
        /// <summary>
        /// Right RGBA image, sl::MAT_TYPE_8U_C4.
        /// </summary>
        RIGHT,
        /// <summary>
        /// Left GRAY image, sl::MAT_TYPE_8U_C1.
        /// </summary>
        LEFT_GREY,
        /// <summary>
        /// Right GRAY image, sl::MAT_TYPE_8U_C1.
        /// </summary>
        RIGHT_GREY,
        /// <summary>
        /// Left RGBA unrectified image, sl::MAT_TYPE_8U_C4.
        /// </summary>
        LEFT_UNRECTIFIED,
        /// <summary>
        /// Right RGBA unrectified image, sl::MAT_TYPE_8U_C4.
        /// </summary>
        RIGHT_UNRECTIFIED,
        /// <summary>
        /// Left GRAY unrectified image, sl::MAT_TYPE_8U_C1.
        /// </summary>
        LEFT_UNRECTIFIED_GREY,
        /// <summary>
        /// Right GRAY unrectified image, sl::MAT_TYPE_8U_C1.
        /// </summary>
        RIGHT_UNRECTIFIED_GREY,
        /// <summary>
        ///  Left and right image (the image width is therefore doubled) RGBA image, MAT_8U_C4.
        /// </summary>
        SIDE_BY_SIDE,
        /// <summary>
        /// Normalized depth image
        /// </summary>
        DEPTH,
        /// <summary>
        ///  Normalized confidence image, MAT_8U_C4.
        /// </summary>
        CONFIDENCE,
        /// <summary>
        /// Color rendering of the normals, MAT_8U_C4.
        /// </summary>
        NORMALS,
        /// <summary>
        /// Color rendering of the right depth mapped on right sensor, MAT_8U_C4.
        /// </summary>
        DEPTH_RIGHT,
        /// <summary>
        /// Color rendering of the normals mapped on right sensor, MAT_8U_C4.
        /// </summary>
        NORMALS_RIGHT
    };

    /// <summary>
    ///  List available camera settings for the ZED camera (contrast, hue, saturation, gain...).
    /// </summary>
    public enum CAMERA_SETTINGS
    {
        /// <summary>
        /// Defines the brightness control. Affected value should be between 0 and 8
        /// </summary>
        BRIGHTNESS,
        /// <summary>
        /// Defines the contrast control. Affected value should be between 0 and 8
        /// </summary>
        CONTRAST,
        /// <summary>
        /// Defines the hue control. Affected value should be between 0 and 11
        /// </summary>
        HUE,
        /// <summary>
        /// Defines the saturation control. Affected value should be between 0 and 8
        /// </summary>
        SATURATION,
        /// <summary>
        /// Defines the gain control. Affected value should be between 0 and 100 for manual control. If ZED_EXPOSURE is set to -1, the gain is in auto mode too.
        /// </summary>
        GAIN,
        /// <summary>
        /// Defines the exposure control. A -1 value enable the AutoExposure/AutoGain control. Affected value should be between 0 and 100 for manual control. A 0 value only disable auto mode without modifing the last auto values, while a 1 to 100 value disable auto mode and set exposure to chosen value
        /// </summary>
        EXPOSURE,
        /// <summary>
        /// Defines the color temperature control. Affected value should be between 2800 and 6500 with a step of 100. A value of -1 set the AWB ( auto white balance), as the boolean parameter (default) does.
        /// </summary>
        WHITEBALANCE
    };

    /// <summary>
    /// List retrievable measures.
    /// </summary>
    public enum MEASURE
    {
        /// <summary>
        /// Disparity map, 1 channel, FLOAT
        /// </summary>
        DISPARITY,
        /// <summary>
        /// Depth map, 1 channel, FLOAT
        /// </summary>
        DEPTH,
        /// <summary>
        /// Certainty/confidence of the disparity map, 1 channel, FLOAT
        /// </summary>
        CONFIDENCE,
        /// <summary>
        /// 3D coordinates of the image points, 4 channels, FLOAT (the 4th channel may contains the colors)
        /// </summary>
        XYZ,
        /// <summary>
        /// 3D coordinates and Color of the image , 4 channels, FLOAT (the 4th channel encode 4 UCHAR for color in R-G-B-A order)
        /// </summary>
        XYZRGBA,
        /// <summary>
        /// 3D coordinates and Color of the image , 4 channels, FLOAT (the 4th channel encode 4 UCHAR for color in B-G-R-A order)
        /// </summary>
        XYZBGRA,
        /// <summary>
        /// 3D coordinates and Color of the image , 4 channels, FLOAT (the 4th channel encode 4 UCHAR for color in A-R-G-B order)
        /// </summary>
        XYZARGB,
        /// <summary>
        /// 3D coordinates and Color of the image, 4 channels, FLOAT, channel 4 contains color in A-B-G-R order.
        /// </summary>
        XYZABGR,
        /// <summary>
        /// 3D coordinates and Color of the image , 4 channels, FLOAT (the 4th channel encode 4 UCHAR for color in A-B-G-R order)
        /// </summary>
        NORMALS,
        /// <summary>
        /// Disparity map for right sensor,  1 channel, FLOAT.
        /// </summary>
        DISPARITY_RIGHT,
        /// <summary>
        /// Depth map for right sensor,  1 channel, FLOAT.
        /// </summary>
        DEPTH_RIGHT,
        /// <summary>
        /// Point cloud for right sensor, 4 channels, FLOAT, channel 4 is empty.
        /// </summary>
        XYZ_RIGHT,
        /// <summary>
        /// Colored point cloud for right sensor, 4 channels, FLOAT, channel 4 contains color in R-G-B-A order.
        /// </summary>
        XYZRGBA_RIGHT,
        /// <summary>
        ///  Colored point cloud for right sensor, 4 channels, FLOAT, channel 4 contains color in B-G-R-A order.
        /// </summary>
        XYZBGRA_RIGHT,
        /// <summary>
        ///  Colored point cloud for right sensor, 4 channels, FLOAT, channel 4 contains color in A-R-G-B order.
        /// </summary>
        XYZARGB_RIGHT,
        /// <summary>
        /// Colored point cloud for right sensor, 4 channels, FLOAT, channel 4 contains color in A-B-G-R order.
        /// </summary>
        XYZABGR_RIGHT,
        /// <summary>
        ///  Normals vector for right view, 4 channels, FLOAT, channel 4 is empty (set to 0)
        /// </summary>
        NORMALS_RIGHT

    };

    /// <summary>
    /// Only few functions of tracking use this system, the path is the default value
    /// </summary>
    public enum TIME_REFERENCE
    {
        /// <summary>
        /// time when the image was captured on the USB
        /// </summary>
        IMAGE,
        /// <summary>
        /// current time (time of the function call)
        /// </summary>
        CURRENT
    };

    /// <summary>
    /// Reference frame (world or camera) for the tracking and depth sensing
    /// </summary>
    public enum REFERENCE_FRAME
    {
        /// <summary>
        /// The matrix contains the displacement from the first camera to the current one
        /// </summary>
        WORLD,
        /// <summary>
        /// The matrix contains the displacement from the previous camera position to the current one
        /// </summary>
        CAMERA
    };

    /// <summary>
    ///  List the different states of positional tracking.
    /// </summary>
    public enum TRACKING_STATE
    {
        /// <summary>
        /// The tracking is searching a match from the database to relocate at a previously known position
        /// </summary>
        TRACKING_SEARCH,
        /// <summary>
        /// The tracking operates normally, the path should be correct
        /// </summary>
        TRACKING_OK,
        /// <summary>
        /// The tracking is not enabled
        /// </summary>
        TRACKING_OFF
    }

    /// <summary>
    /// List of svo compressions mode available
    /// </summary>
    public enum SVO_COMPRESSION_MODE
    {
        /// <summary>
        /// RAW images, no compression
        /// </summary>
        RAW_BASED,
        /// <summary>
        /// new Lossless, with png/zstd based compression : avg size = 42% of RAW
        /// </summary>
        LOSSLESS_BASED,
        /// <summary>
        /// new Lossy, with jpeg based compression : avg size = 22% of RAW
        /// </summary>
        LOSSY_BASED
    }

    /// <summary>
    /// List of mesh format available
    /// </summary>
    public enum MESH_FILE_FORMAT
    {
        /// <summary>
        /// Contains only vertices and faces.
        /// </summary>
        PLY,
        /// <summary>
        /// Contains only vertices and faces, encoded in binary.
        /// </summary>
        BIN,
        /// <summary>
        /// Contains vertices, normals, faces and textures informations if possible.
        /// </summary>
        OBJ
    }

    /// <summary>
    /// List of filters avvailable for the spatial mapping
    /// </summary>
    public enum FILTER
    {
        /// <summary>
        /// Soft decimation and smoothing.
        /// </summary>
        LOW,
        /// <summary>
        /// Decimate the number of faces and apply a soft smooth.
        /// </summary>
        MEDIUM,
        /// <summary>
        /// Drasticly reduce the number of faces.
        /// </summary>
        HIGH,
    }

    /// <summary>
    /// List of spatial mapping state
    /// </summary>
    public enum SPATIAL_MAPPING_STATE
    {
        /// <summary>
        /// The spatial mapping is initializing.
        /// </summary>
        SPATIAL_MAPPING_STATE_INITIALIZING,
        /// <summary>
        /// The depth and tracking data were correctly integrated in the fusion algorithm.
        /// </summary>
        SPATIAL_MAPPING_STATE_OK,
        /// <summary>
        /// The maximum memory dedicated to the scanning has been reach, the mesh will no longer be updated.
        /// </summary>
        SPATIAL_MAPPING_STATE_NOT_ENOUGH_MEMORY,
        /// <summary>
        /// EnableSpatialMapping() wasn't called (or the scanning was stopped and not relaunched).
        /// </summary>
        SPATIAL_MAPPING_STATE_NOT_ENABLED,
        /// <summary>
        /// Effective FPS is too low to give proper results for spatial mapping. Consider using PERFORMANCES parameters (DEPTH_MODE_PERFORMANCE, low camera resolution (VGA,HD720), spatial mapping low resolution)
        /// </summary>
        SPATIAL_MAPPING_STATE_FPS_TOO_LOW
    }

    /// <summary>
    /// Unit used by the SDK. Prefer using METER with Unity
    /// </summary>
    public enum UNIT
    {
        /// <summary>
        /// International System, 1/1000 METER.
        /// </summary>
        MILLIMETER,
        /// <summary>
        /// International System, 1/100 METER.
        /// </summary>
        CENTIMETER,
        /// <summary>
        /// International System, 1METER.
        /// </summary>
        METER,
        /// <summary>
        ///  Imperial Unit, 1/12 FOOT 
        /// </summary>
        INCH,
        /// <summary>
        ///  Imperial Unit, 1 FOOT
        /// </summary>
        FOOT
    }

    /// <summary>
    /// Parameters that will be fixed for the whole execution life time of the camera.
    /// </summary>
    public class InitParameters
    {
        /// <summary>
        /// Define the chosen ZED resolution 
        /// </summary>
        public sl.RESOLUTION resolution;
        /// <summary>
        /// Requested FPS for this resolution. set as 0 will choose the default FPS for this resolution (see User guide). 
        /// </summary>
        public int cameraFPS;
        /// <summary>
        ///  ONLY for LINUX : if multiple ZEDs are connected. Is not used in Unity
        /// </summary>
        public int cameraLinuxID;
        /// <summary>
        /// Path with filename to the recorded SVO file.
        /// </summary>
        public string pathSVO = "";
        /// <summary>
        /// This mode simulates the live camera and consequently skipped frames if the computation framerate is too slow.
        /// </summary>
        public bool svoRealTimeMode;
        /// <summary>
        ///  Define the unit for all the metric values ( depth, point cloud, tracking, mesh).
        /// </summary>
        public UNIT coordinateUnit;
        /// <summary>
        /// This defines the order and the direction of the axis of the coordinate system. see COORDINATE_SYSTEM for more information.
        /// </summary>
        public COORDINATE_SYSTEM coordinateSystem;
        /// <summary>
        /// Defines the quality of the depth map, affects the level of details and also the computation time.
        /// </summary>
        public sl.DEPTH_MODE depthMode;
        /// <summary>
        ///  Specify the minimum depth value that will be computed, in the UNIT you define.
        /// </summary>
        public float depthMinimumDistance;
        /// <summary>
        ///  Defines if the image are horizontally flipped.
        /// </summary>
        public bool cameraImageFlip;
        /// <summary>
        /// Defines if right MEASURE should be computed (needed for MEASURE_<XXX>_RIGHT)
        /// </summary>
        public bool enableRightSideMeasure;
        /// <summary>
        /// If set to true, it will disable self-calibration and take the optional calibration parameters without optimizing them.
        /// It is advised to leave it as false, so that calibration parameters can be optimized.
        /// </summary>
        public bool cameraDisableSelfCalib;
        /// <summary>
        /// ONLY for LINUX : Set the number of buffers in the internal grabbing process. DO NOT WORK ON UNITY
        /// </summary>
        public int cameraBufferCountLinux;
        /// <summary>
        /// Defines if you want the SDK provides text feedback.
        /// </summary>
        public bool sdkVerbose;
        /// <summary>
        ///  Defines the graphics card on which the computation will be done.
        /// </summary>
        public int sdkGPUId;
        /// <summary>
        /// Store the program outputs into the log file defined by its filename.
        /// </summary>
        public string sdkVerboseLogFile = "";
        /// <summary>
        /// Defines if the depth map should be stabilize.
        /// </summary>
        public bool depthStabilization;

        public InitParameters()
        {
            this.resolution = RESOLUTION.HD720;
            this.cameraFPS = 60;
            this.cameraLinuxID = 0;
            this.pathSVO = "";
            this.svoRealTimeMode = false;
            this.coordinateUnit = UNIT.METER;
            this.coordinateSystem = COORDINATE_SYSTEM.IMAGE;
            this.depthMode = DEPTH_MODE.PERFORMANCE;
            this.depthMinimumDistance = -1;
            this.cameraImageFlip = false;
            this.cameraDisableSelfCalib = false;
            this.cameraBufferCountLinux = 4;
            this.sdkVerbose = false;
            this.sdkGPUId = -1;
            this.sdkVerboseLogFile = "";
            this.enableRightSideMeasure = false;
            this.depthStabilization = true;
        }

    }
    /// <summary>
    /// List of available system of coordinates
    /// </summary>
    public enum COORDINATE_SYSTEM
    {
        /// <summary>
        /// Standard coordinates system in computer vision. Used in OpenCV : see here : http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html 
        /// </summary>
        IMAGE,
        /// <summary>
        /// Left-Handed with Y up and Z forward. Used in Unity with DirectX
        /// </summary>
        LEFT_HANDED_Y_UP,
        /// <summary>
        ///  Right-Handed with Y pointing up and Z backward. Used in OpenGL.
        /// </summary>
        RIGHT_HANDED_Y_UP,
        /// <summary>
        /// Right-Handed with Z pointing up and Y forward. Used in 3DSMax.
        /// </summary>
        RIGHT_HANDED_Z_UP,
        /// <summary>
        /// Left-Handed with Z axis pointing up and X forward. Used in Unreal Engine.
        /// </summary>
        LEFT_HANDED_Z_UP
    }

    /// <summary>
    ///  List the different states of spatial memory area export.
    /// </summary>
    public enum AREA_EXPORT_STATE
    {
        /// <summary>
        ///  The spatial memory file has been successfully created.
        /// </summary>
        AREA_EXPORT_STATE_SUCCESS,
        /// <summary>
        /// The spatial memory is currently written.
        /// </summary>
        AREA_EXPORT_STATE_RUNNING,
        /// <summary>
        /// The spatial memory file exportation has not been called.
        /// </summary>
        AREA_EXPORT_STATE_NOT_STARTED,
        /// <summary>
        /// The spatial memory contains no data, the file is empty.
        /// </summary>
        AREA_EXPORT_STATE_FILE_EMPTY,
        /// <summary>
        ///  The spatial memory file has not been written because of a wrong file name.
        /// </summary>
        AREA_EXPORT_STATE_FILE_ERROR,
        /// <summary>
        /// The spatial memory learning is disable, no file can be created.
        /// </summary>
        AREA_EXPORT_STATE_SPATIAL_MEMORY_DISABLED
    };

    /// <summary>
    /// Runtime parameters used by the grab
    /// </summary>
    [StructLayout(LayoutKind.Explicit)]
    public struct RuntimeParameters {
        /// <summary>
        ///  Defines the algorithm used for depth map computation, more info : \ref SENSING_MODE definition.
        /// </summary>
        [FieldOffset(12)]//In 2.2 the runtime parameters needs 3 int of offset
        public sl.SENSING_MODE sensingMode;
        /// <summary>
        /// Provides 3D measures (point cloud and normals) in the desired reference frame (default is REFERENCE_FRAME_CAMERA)
        /// </summary>
        [FieldOffset(16)]
        public sl.REFERENCE_FRAME measure3DReferenceFrame;
        /// <summary>
        /// Defines if the depth map should be computed.
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(20)]
        public bool enableDepth;
        /// <summary>
        ///  Defines if the point cloud should be computed (including XYZRGBA).
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(21)]
        public bool enablePointCloud;

    }

    /// <summary>
    /// Tracking Frame choices : Left (left sensor), Center (Center of the camera), Right (right sensor)
    /// </summary>
    public enum TRACKING_FRAME
    {
        LEFT_EYE,
        CENTER_EYE,
        RIGHT_EYE
    };

    /// <summary>
    /// Constant Rendering Plane distance (Don't change this)
    /// </summary>
    public enum Constant {
        PLANE_DISTANCE = 10
    };

}// end namespace sl

//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
//Wrapper to the Mat of the ZED
using System;
using System.Runtime.InteropServices;

namespace sl
{

    [StructLayout(LayoutKind.Sequential)]
    public struct char2
    {
        public byte r;
        public byte g;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct char3
    {
        public byte r;
        public byte g;
        public byte b;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct char4
    {
        [MarshalAs(UnmanagedType.U1)]
        public byte r;
        [MarshalAs(UnmanagedType.U1)]
        public byte g;
        [MarshalAs(UnmanagedType.U1)]
        public byte b;
        [MarshalAs(UnmanagedType.U1)]
        public byte a;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct float2
    {
        public float r;
        public float g;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct float3
    {
        public float r;
        public float g;
        public float b;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct float4
    {
        public float r;
        public float g;
        public float b;
        public float a;
    }

    public class ZEDMat
    {
        public enum MAT_TYPE
        {
            MAT_32F_C1, /*!< float 1 channel.*/
            MAT_32F_C2, /*!< float 2 channels.*/
            MAT_32F_C3, /*!< float 3 channels.*/
            MAT_32F_C4, /*!< float 4 channels.*/
            MAT_8U_C1, /*!< unsigned char 1 channel.*/
            MAT_8U_C2, /*!< unsigned char 2 channels.*/
            MAT_8U_C3, /*!< unsigned char 3 channels.*/
            MAT_8U_C4 /*!< unsigned char 4 channels.*/
        };

        public enum COPY_TYPE
        {
            COPY_TYPE_CPU_CPU, /*!< copy data from CPU to CPU.*/
            COPY_TYPE_CPU_GPU, /*!< copy data from CPU to GPU.*/
            COPY_TYPE_GPU_GPU, /*!< copy data from GPU to GPU.*/
            COPY_TYPE_GPU_CPU /*!< copy data from GPU to CPU.*/
        };

        public enum MEM
        {
            MEM_CPU = 1, /*!< CPU Memory (Processor side).*/
            MEM_GPU = 2 /*!< GPU Memory (Graphic card side).*/
        };
        const string nameDll = "sl_unitywrapper";

        [DllImport(nameDll, EntryPoint = "dllz_mat_create_new")]
        private static extern IntPtr dllz_mat_create_new(sl.Resolution resolution, int type, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_create_new_empty")]
        private static extern IntPtr dllz_mat_create_new_empty();

        [DllImport(nameDll, EntryPoint = "dllz_mat_is_init")]
        private static extern bool dllz_mat_is_init(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_free")]
        private static extern bool dllz_mat_free(System.IntPtr ptr, int type);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_infos")]
        private static extern bool dllz_mat_get_infos(System.IntPtr ptr, byte[] buffer);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_float")]
        private static extern int dllz_mat_get_value_float(System.IntPtr ptr, int x, int y, out float value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_float2")]
        private static extern int dllz_mat_get_value_float2(System.IntPtr ptr, int x, int y, out float2 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_float3")]
        private static extern int dllz_mat_get_value_float3(System.IntPtr ptr, int x, int y, out float3 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_float4")]
        private static extern int dllz_mat_get_value_float4(System.IntPtr ptr, int x, int y, out float4 value, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_uchar")]
        private static extern int dllz_mat_get_value_uchar(System.IntPtr ptr, int x, int y, out byte value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_uchar2")]
        private static extern int dllz_mat_get_value_uchar2(System.IntPtr ptr, int x, int y, out char2 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_uchar3")]
        private static extern int dllz_mat_get_value_uchar3(System.IntPtr ptr, int x, int y, out char3 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_get_value_uchar4")]
        private static extern int dllz_mat_get_value_uchar4(System.IntPtr ptr, int x, int y, out char4 value, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_float")]
        private static extern int dllz_mat_set_value_float(System.IntPtr ptr, int x, int y, ref float value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_float2")]
        private static extern int dllz_mat_set_value_float2(System.IntPtr ptr, int x, int y, ref float2 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_float3")]
        private static extern int dllz_mat_set_value_float3(System.IntPtr ptr, int x, int y, ref float3 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_float4")]
        private static extern int dllz_mat_set_value_float4(System.IntPtr ptr, int x, int y, ref float4 value, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_uchar")]
        private static extern int dllz_mat_set_value_uchar(System.IntPtr ptr, int x, int y, ref byte value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_uchar2")]
        private static extern int dllz_mat_set_value_uchar2(System.IntPtr ptr, int x, int y, ref char2 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_uchar3")]
        private static extern int dllz_mat_set_value_uchar3(System.IntPtr ptr, int x, int y, ref char3 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_value_uchar4")]
        private static extern int dllz_mat_set_value_uchar4(System.IntPtr ptr, int x, int y, ref char4 value, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_float")]
        private static extern int dllz_mat_set_to_float(System.IntPtr ptr, ref float value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_float2")]
        private static extern int dllz_mat_set_to_float2(System.IntPtr ptr, ref float2 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_float3")]
        private static extern int dllz_mat_set_to_float3(System.IntPtr ptr, ref float3 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_float4")]
        private static extern int dllz_mat_set_to_float4(System.IntPtr ptr, ref float4 value, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_uchar")]
        private static extern int dllz_mat_set_to_uchar(System.IntPtr ptr,  ref byte value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_uchar2")]          
        private static extern int dllz_mat_set_to_uchar2(System.IntPtr ptr, ref char2 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_uchar3")]          
        private static extern int dllz_mat_set_to_uchar3(System.IntPtr ptr, ref char3 value, int mem);
        [DllImport(nameDll, EntryPoint = "dllz_mat_set_to_uchar4")]
        private static extern int dllz_mat_set_to_uchar4(System.IntPtr ptr, ref char4 value, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_update_cpu_from_gpu")]
        private static extern int dllz_mat_update_cpu_from_gpu(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_update_gpu_from_cpu")]
        private static extern int dllz_mat_update_gpu_from_cpu(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_read")]
        private static extern int dllz_mat_read(System.IntPtr ptr, string filePath);

        [DllImport(nameDll, EntryPoint = "dllz_mat_write")]
        private static extern int dllz_mat_write(System.IntPtr ptr, string filePath);

        [DllImport(nameDll, EntryPoint = "dllz_mat_copy_to")]
        private static extern int dllz_mat_copy_to(System.IntPtr ptr, System.IntPtr dest, int cpyType);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_width")]
        private static extern int dllz_mat_get_width(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_height")]
        private static extern int dllz_mat_get_height(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_channels")]
        private static extern int dllz_mat_get_channels(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_memory_type")]
        private static extern int dllz_mat_get_memory_type(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_pixel_bytes")]
        private static extern int dllz_mat_get_pixel_bytes(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_step")]
        private static extern int dllz_mat_get_step(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_step_bytes")]
        private static extern int dllz_mat_get_step_bytes(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_width_bytes")]
        private static extern int dllz_mat_get_width_bytes(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_is_memory_owner")]
        private static extern bool dllz_mat_is_memory_owner(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_resolution")]
        private static extern sl.Resolution dllz_mat_get_resolution(System.IntPtr ptr);

        [DllImport(nameDll, EntryPoint = "dllz_mat_alloc")]
        private static extern void dllz_mat_alloc(System.IntPtr ptr, int width, int height, int type, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_set_from")]
        private static extern int dllz_mat_set_from(System.IntPtr ptr, System.IntPtr source, int copyType);

        [DllImport(nameDll, EntryPoint = "dllz_mat_get_ptr")]
        private static extern System.IntPtr dllz_mat_get_ptr(System.IntPtr ptr, int mem);

        [DllImport(nameDll, EntryPoint = "dllz_mat_clone")]
        private static extern void dllz_mat_clone(System.IntPtr ptr, System.IntPtr ptrSource);

        private System.IntPtr _matInternalPtr;

        /// <summary>
        /// Returns the internal ptr of a mat
        /// </summary>
        public IntPtr MatPtr
        {
            get { return _matInternalPtr; }
        }

        /// <summary>
        /// Creates an empty mat
        /// </summary>
        public ZEDMat()
        {
            _matInternalPtr = dllz_mat_create_new_empty();
        }

        /// <summary>
        /// Creates a mat from
        /// </summary>
        /// <param name="ptr"></param>
        public ZEDMat(System.IntPtr ptr) 
        {
            if(ptr == IntPtr.Zero)
            {
                throw new Exception("ZED Mat not initialized");
            }
            _matInternalPtr = ptr;
        }

        /// <summary>
        /// Creates a Mat with a resolution
        /// </summary>
        /// <param name="resolution"></param>
        /// <param name="type"></param>
        /// <param name="mem"></param>
        public ZEDMat(sl.Resolution resolution, MAT_TYPE type, MEM mem = MEM.MEM_CPU)
        {
            _matInternalPtr = dllz_mat_create_new(resolution, (int)(type), (int)(mem));
        }

        /// <summary>
        /// Creates a Mat
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="type"></param>
        /// <param name="mem"></param>
        public ZEDMat(uint width, uint height, MAT_TYPE type, MEM mem = MEM.MEM_CPU)
        {
            _matInternalPtr = dllz_mat_create_new(new sl.Resolution(width, height), (int)(type), (int)(mem));
        }

        /// <summary>
        /// Defines whether the Mat is initialized or not.
        /// </summary>
        /// <returns></returns>
        public bool IsInit()
        {
            return dllz_mat_is_init(_matInternalPtr);
        }

        /// <summary>
        /// Free the memory of the Mat
        /// </summary>
        /// <param name="mem"></param>
        public void Free(MEM mem = (MEM.MEM_GPU | MEM.MEM_CPU))
        {
            dllz_mat_free(_matInternalPtr, (int)mem);
            _matInternalPtr = System.IntPtr.Zero;
        }

        /// <summary>
        /// Downloads data from DEVICE (GPU) to HOST (CPU), if possible.
        /// </summary>
        /// <returns></returns>
        public sl.ERROR_CODE UpdateCPUFromGPU()
        {
            return (sl.ERROR_CODE)dllz_mat_update_cpu_from_gpu(_matInternalPtr);
        }

        /// <summary>
        /// Uploads data from HOST (CPU) to DEVICE (GPU), if possible.
        /// </summary>
        /// <returns></returns>
        public sl.ERROR_CODE UpdateGPUFromCPU()
        {
            return (sl.ERROR_CODE)dllz_mat_update_gpu_from_cpu(_matInternalPtr);
        }

        /// <summary>
        /// Return the informations about the Mat
        /// </summary>
        /// <returns></returns>
        public string GetInfos()
        {
            byte[] buf = new byte[300];
            dllz_mat_get_infos(_matInternalPtr, buf);
            return System.Text.Encoding.ASCII.GetString(buf);
        }

        /// <summary>
        /// Copies data an other Mat (deep copy).
        /// </summary>
        /// <param name="dest">dst : the Mat where the data will be copied.</param>
        /// <param name="copyType">cpyType : specify the memories that will be used for the copy.</param>
        /// <returns></returns>
        public sl.ERROR_CODE CopyTo(sl.ZEDMat dest, sl.ZEDMat.COPY_TYPE copyType = COPY_TYPE.COPY_TYPE_CPU_CPU)
        {
            return (sl.ERROR_CODE)dllz_mat_copy_to(_matInternalPtr, dest._matInternalPtr, (int)(copyType));
        }

        /// <summary>
        /// Reads an image from a file (only if \ref MEM_CPU is available on the current
        /// </summary>
        /// <param name="filePath"> file path including the name and extension.</param>
        /// <returns></returns>
        public sl.ERROR_CODE Read(string filePath)
        {
            return (sl.ERROR_CODE)dllz_mat_read(_matInternalPtr, filePath);
        }

        /// <summary>
        ///  Writes the Mat (only if MEM_CPU is available) into a file as an image.
        /// </summary>
        /// <param name="filePath">file path including the name and extension.</param>
        /// <returns></returns>
        public sl.ERROR_CODE Write(string filePath)
        {
            return (sl.ERROR_CODE)dllz_mat_write(_matInternalPtr, filePath);
        }

        /// <summary>
        /// Returns the width of the matrix.
        /// </summary>
        /// <returns></returns>
        public int GetWidth()
        {
            return dllz_mat_get_width(_matInternalPtr);
        }

        /// <summary>
        /// Returns the height of the matrix.
        /// </summary>
        /// <returns></returns>
        public int GetHeight()
        {
            return dllz_mat_get_height(_matInternalPtr);
        }

        /// <summary>
        /// Returns the number of values stored in one pixel.
        /// </summary>
        /// <returns></returns>
        public int GetChannels()
        {
            return dllz_mat_get_channels(_matInternalPtr);
        }

        /// <summary>
        /// Returns the size in bytes of one pixel.
        /// </summary>
        /// <returns></returns>
        public int GetPixelBytes()
        {
            return dllz_mat_get_pixel_bytes(_matInternalPtr);
        }

        /// <summary>
        ///  Returns the memory step in number of elements (the number of values in one pixel row).
        /// </summary>
        /// <returns></returns>
        public int GetStep()
        {
            return dllz_mat_get_step(_matInternalPtr);
        }

        /// <summary>
        /// Returns the memory step in Bytes (the Bytes size of one pixel row).
        /// </summary>
        /// <returns></returns>
        public int GetStepBytes()
        {
            return dllz_mat_get_step_bytes(_matInternalPtr);
        }

        /// <summary>
        /// Returns the size in bytes of a row.
        /// </summary>
        /// <returns></returns>
        public int GetWidthBytes()
        {
            return dllz_mat_get_width_bytes(_matInternalPtr);
        }

        /// <summary>
        /// Returns the type of memory (CPU and/or GPU).
        /// </summary>
        /// <returns></returns>
        public MEM GetMemoryType()
        {
            return (MEM)dllz_mat_get_memory_type(_matInternalPtr);
        }

        /// <summary>
        /// Returns whether the Mat is the owner of the memory it access.
        /// </summary>
        /// <returns></returns>
        public bool IsMemoryOwner()
        {
            return dllz_mat_is_memory_owner(_matInternalPtr);
        }

        public sl.Resolution GetResolution()
        {
            return dllz_mat_get_resolution(_matInternalPtr);
        }

        /// <summary>
        /// Allocates the Mat memory.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="matType"></param>
        /// <param name="mem"></param>
        public void Alloc(uint width, uint height, MAT_TYPE matType, MEM mem = MEM.MEM_CPU)
        {
            dllz_mat_alloc(_matInternalPtr, (int)width, (int)height, (int)matType, (int)mem);
        }

        /// <summary>
        /// Allocates the Mat memory.
        /// </summary>
        /// <param name="resolution"></param>
        /// <param name="matType"></param>
        /// <param name="mem"></param>
        public void Alloc(sl.Resolution resolution, MAT_TYPE matType, MEM mem = MEM.MEM_CPU)
        {
            dllz_mat_alloc(_matInternalPtr, (int)resolution.width, (int)resolution.height, (int)matType, (int)mem);
        }

        /// <summary>
        /// Copies data from an other Mat (deep copy).
        /// </summary>
        /// <param name="src"></param>
        /// <param name="copyType"></param>
        /// <returns></returns>
        public int SetFrom(ZEDMat src, COPY_TYPE copyType = COPY_TYPE.COPY_TYPE_CPU_CPU)
        {
            return dllz_mat_set_from(_matInternalPtr, src._matInternalPtr, (int)copyType);
        }

        public System.IntPtr GetPtr(MEM mem = MEM.MEM_CPU)
        {
            return dllz_mat_get_ptr(_matInternalPtr, (int)mem);
        }

        /// <summary>
        /// Duplicates Mat by copy (deep copy).
        /// </summary>
        /// <param name="source"></param>
        public void Clone(ZEDMat source)
        {
            dllz_mat_clone(_matInternalPtr, source._matInternalPtr);
        }

        /************ GET VALUES *********************/
        // Cannot send values by template, covariant issue with a out needed

        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out float value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_float(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out float2 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_float2(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out float3 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_float3(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out float4 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_float4(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out byte value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_uchar(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out char2 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_uchar2(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out char3 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_uchar3(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /// <summary>
        /// Returns the value of a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE GetValue(int x, int y, out char4 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_get_value_uchar4(_matInternalPtr, x, y, out value, (int)(mem)));
        }
        /***************************************************************************************/

        /************ SET VALUES *********************/
        // Cannot send values by template, covariant issue with a out needed

        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, ref float value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_float(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, ref float2 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_float2(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, ref float3 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_float3(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, float4 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_float4(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, ref byte value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_uchar(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, ref char2 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_uchar2(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, ref char3 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_uchar3(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /// <summary>
        /// Sets a value to a specific point in the matrix.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetValue(int x, int y, ref char4 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_value_uchar4(_matInternalPtr, x, y, ref value, (int)(mem)));
        }
        /***************************************************************************************/

        /************ SET TO *********************/
        // Cannot send values by template, covariant issue with a out needed

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo(ref float value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_float(_matInternalPtr, ref value, (int)(mem)));
        }

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo(ref float2 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_float2(_matInternalPtr, ref value, (int)(mem)));
        }

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo(ref float3 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_float3(_matInternalPtr, ref value, (int)(mem)));
        }

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo(ref float4 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_float4(_matInternalPtr, ref value, (int)(mem)));
        }

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo(ref byte value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_uchar(_matInternalPtr, ref value, (int)(mem)));
        }

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo(ref char2 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_uchar2(_matInternalPtr, ref value, (int)(mem)));
        }

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo(ref char3 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_uchar3(_matInternalPtr, ref value, (int)(mem)));
        }

        /// <summary>
        /// ills the Mat with the given value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mem"></param>
        /// <returns></returns>
        public sl.ERROR_CODE SetTo( ref char4 value, sl.ZEDMat.MEM mem)
        {
            return (sl.ERROR_CODE)(dllz_mat_set_to_uchar4(_matInternalPtr, ref value, (int)(mem)));
        }
        /***************************************************************************************/

    }
}
MiaoDX commented 6 years ago

WoW, that is a big effort! Unfortunately, still, I did not run it successfully.

Additional information: Unable to load DLL“sl_unitywrapper” The specified module could not be found. (Exception from HRESULT: 0x8007007E)


* Then I put `sl_mr_core64.dll` and `sl_unitywrapper.dll` into the Release folder, but the error now becomes

An unhandled exception of type 'System.BadImageFormatException' occurred in ZedTester.exe

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)



* What is more, I have to comment out the `public RuntimeParameters RuntimeParameters { get => runtimeParameters; set => runtimeParameters = value; }` in order to build

I am sure there are some little errors here and there, such as the way to add DLL into the projects, and maybe also the version of the SDK, etc. So I am wondering, could it be possible that you email me (or other ways, maybe set up one repo in your github) one complete/standalone/clean project? Maybe filter out the private codes like the server project above?

Sorry for the trouble, my email addr: miaodx@tju.edu.cn

Thanks ahead!
MiaoDX commented 6 years ago

@tomitrescak It seems that I made it! Use the latest SDK (v2.4.1) with cuda9.1 and the dlls from this repo's v2.4.0 tag. The acquisition seems ok but still, public RuntimeParameters RuntimeParameters { get => runtimeParameters; set => runtimeParameters = value; } needs to be commented out in class Zed.

After some limited search, I found that the following codes can pass the compiler:

        public RuntimeParameters RuntimeParameters
        {
            get { return this.runtimeParameters; }
            set { this.runtimeParameters = value; }
        }
        //Or
        //public RuntimeParameters RuntimeParameters{get; set;}

And it seems that the class Zed has many duplicated codes with https://gist.github.com/tomitrescak/c31cee9c898ad7def32c4aaef8c1234d, so it should be one update? Then is there an example code to use the Zed class? Especially the usage of public event Action<Zed> FrameTick;/this.FrameTick(this);?

tomitrescak commented 6 years ago

@MiaoDX make sure you are building with x64 target

And then observe the magic! ;)

tomitrescak commented 6 years ago

Example:

// Form1.cs
using sl;
using System;
using System.Windows.Forms;
using Holoserver.Cameras;
using System.Diagnostics;

namespace ZedTester
{

    public partial class Form1 : Form
    {

        VIEW view;

        public Form1()
        {
            InitializeComponent();

            // find version
            var versionZED = "[SDK]: " + sl.ZEDCamera.GetSDKVersion().ToString() + " [Plugin]: " + sl.ZEDCamera.PluginVersion.ToString();
            this.versionInfo.Text = versionZED;

            this.Start();
        }

        void Start()
        {
            // start with stored SVO
            var zed = new Zed(@"c:\Projects\files\HD720_SN17600_11-04-52.svo");

            // start with live feed
            // var zed = new Zed();

            // You need to assign the listener that will execute in each camera loop
            // in this case I named it AcquireImages and implemented it below
            zed.FrameTick += this.AcquireImages;
            zed.RuntimeParameters = new RuntimeParameters
            {
                sensingMode = SENSING_MODE.FILL,
                enableDepth = true
            };
            zed.Start();
        }

        private void AcquireImages(Zed zed)
        {
            try
            {
                imageBox1.Invoke((MethodInvoker)delegate
                {
            // you can use method FetchImage or any of the helper methods
            // e.g. zed.Left, zed.Right, zed.Depth ...
            var image = zed.FetchImage(view);
                    imageBox1.Image = image;
                });
            }
            catch { }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Is: " + ZEDCamera.CheckPlugin());
        }

        private void sourceSelection_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (sourceSelection.SelectedIndex)
            {
                case 0:
                    view = sl.VIEW.LEFT;
                    break;
                case 1:
                    view = sl.VIEW.RIGHT;
                    break;
                case 2:
                    view = sl.VIEW.SIDE_BY_SIDE;
                    break;
                case 3:
                    view = sl.VIEW.DEPTH;
                    break;
                case 4:
                    view = sl.VIEW.CONFIDENCE;
                    break;
                case 5:
                    view = sl.VIEW.NORMALS;
                    break;
                case 6:
                    view = sl.VIEW.LEFT_UNRECTIFIED;
                    break;
            }
        }
    }
}

and here is the Form1.Designer.cs

namespace ZedTester
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.versionInfo = new System.Windows.Forms.Label();
            this.errorInfo = new System.Windows.Forms.Label();
            this.sourceSelection = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.imageBox1 = new Emgu.CV.UI.ImageBox();
            ((System.ComponentModel.ISupportInitialize)(this.imageBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // versionInfo
            // 
            this.versionInfo.AutoSize = true;
            this.versionInfo.Location = new System.Drawing.Point(12, 9);
            this.versionInfo.Name = "versionInfo";
            this.versionInfo.Size = new System.Drawing.Size(60, 13);
            this.versionInfo.TabIndex = 1;
            this.versionInfo.Text = "VersionInfo";
            // 
            // errorInfo
            // 
            this.errorInfo.AutoSize = true;
            this.errorInfo.Location = new System.Drawing.Point(12, 31);
            this.errorInfo.Name = "errorInfo";
            this.errorInfo.Size = new System.Drawing.Size(46, 13);
            this.errorInfo.TabIndex = 2;
            this.errorInfo.Text = "No Error";
            // 
            // sourceSelection
            // 
            this.sourceSelection.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.sourceSelection.FormattingEnabled = true;
            this.sourceSelection.Items.AddRange(new object[] {
            "Left",
            "Right",
            "Stereo",
            "Depth",
            "Confidence",
            "Normals",
            "Unrectified"});
            this.sourceSelection.Location = new System.Drawing.Point(313, 12);
            this.sourceSelection.Name = "sourceSelection";
            this.sourceSelection.Size = new System.Drawing.Size(212, 21);
            this.sourceSelection.TabIndex = 4;
            this.sourceSelection.SelectedIndexChanged += new System.EventHandler(this.sourceSelection_SelectedIndexChanged);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(263, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(44, 13);
            this.label1.TabIndex = 6;
            this.label1.Text = "Source:";
            // 
            // imageBox1
            // 
            this.imageBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.imageBox1.Location = new System.Drawing.Point(24, 47);
            this.imageBox1.Name = "imageBox1";
            this.imageBox1.Size = new System.Drawing.Size(962, 605);
            this.imageBox1.TabIndex = 2;
            this.imageBox1.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(998, 664);
            this.Controls.Add(this.imageBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.sourceSelection);
            this.Controls.Add(this.errorInfo);
            this.Controls.Add(this.versionInfo);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.imageBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.Label versionInfo;
        private System.Windows.Forms.Label errorInfo;
        private System.Windows.Forms.ComboBox sourceSelection;
        private System.Windows.Forms.Label label1;
        private Emgu.CV.UI.ImageBox imageBox1;
    }
}
MiaoDX commented 6 years ago

@tomitrescak Well, thanks a lot!!! The this.FrameTick(this); is somewhat amazing!

Another little reminder, if you really want to use the depth for calculation, it should be fetched by public sl.ERROR_CODE RetrieveMeasure(sl.ZEDMat mat, sl.MEASURE measure, sl.ZEDMat.MEM mem = sl.ZEDMat.MEM.MEM_CPU, sl.Resolution resolution = new sl.Resolution()), since the depth in the View is just for view.

In order to use it, we also need to convert the format, aka to 16bit unsigned, like in https://github.com/stereolabs/zed-examples/blob/master/svo%20recording/export/src/main.cpp#L202:

// cpp version
// Convert to 16Bit
cv::Mat depth16;
depth_image_ocv.convertTo(depth16, CV_16UC1);
 cv::imwrite(filename2.str(), depth16);

and my previous tryout https://github.com/MiaoDX/depth_camera/blob/master/zed_camera/ZEDCamera.py#L200:

# python version
self.zed.retrieve_measure(self.image_mat, sl.PyMEASURE.PyMEASURE_DEPTH)
im_measure = np.uint16(self.image_mat.get_data())

Anyway, the hardest part is done, I will try to digest into this myself (of course, it is appreciated if you can just throw me the answer!).

Thanks again!

MiaoDX commented 6 years ago

@tomitrescak Sorry to bother you again -.-

But could you email me one of the SDK exe you used, maybe it should be ZED_SDK_WinSetup_v2.4.0.exe (which is also the unity plugin), instead of v2.4.1 which is provided by the official website? At first, I mixed them but it still can work, but when I change to another computer it complains about the mismatch and terminated at the camera init phase.

My email addr: miaodx@tju.edu.cn

Looking forward to your reply!

And @nesnes @adujardin , I also report this issue (not all legacy SDKs are kept in the website and some other minor issues) to the support@stereolabs.com , hope you devs can make things easier :)

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment otherwise it will be automatically closed in 5 days