// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
//
//
// Copyright (c) 2018-present, Magic Leap, Inc. All Rights Reserved.
//
//
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
namespace UnityEngine.XR.MagicLeap
{
using System;
using System.Runtime.InteropServices;
public partial class MLWorldCamera
{
///
/// See ml_world_camera.h for additional comments.
///
internal class NativeBindings : Native.MagicLeapNativeBindings
{
private const int MaxRadialDistortionCoefficients = 4;
private const int MaxTangentialDistortionCoefficients = 2;
///
/// World camera intrinsic parameters.
///
[StructLayout(LayoutKind.Sequential)]
public readonly struct MLWorldCameraIntrinsics
{
///
/// Camera width.
///
public readonly uint Width;
///
/// Camera height.
///
public readonly uint Height;
///
/// Camera focal length.
///
public readonly Vector2 FocalLength;
///
/// Camera principal point.
///
public readonly Vector2 PrincipalPoint;
///
/// Field of view in degrees.
///
public readonly float Fov;
///
/// Radial distortion vector.
/// The radial distortion co-efficients are in the following order: [k1, k2, k3, k4].
///
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxRadialDistortionCoefficients)]
public readonly double[] RadialDistortion;
///
/// Tangential distortion vector.
/// The tangential distortion co-efficients are in the following order: [p1, p2].
///
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxTangentialDistortionCoefficients)]
public readonly double[] TangentialDistortion;
public override string ToString() => $"Width:{Width}, Height:{Height}, FOV: {Fov}";
}
///
/// A structure to encapsulate the camera settings.
///
[StructLayout(LayoutKind.Sequential)]
public readonly struct MLWorldCameraSettings
{
public MLWorldCameraSettings(in Settings settings)
{
Version = 1;
Mode = (uint)settings.Mode;
Cameras = (uint)settings.Cameras;
}
///
/// Version of this structure.
///
public readonly uint Version;
///
/// World camera mode. If you want to request frames from different camera modes
/// then "OR" the modes of interest to the app.
/// The system may not be able to service all the requested camera modes.
/// This parameter is treated as a hint and data will be provided for
/// the requested camera modes when available.
///
public readonly uint Mode;
///
/// World cameras that need to be enabled.If you want to request frames from different world camera
/// then "OR" the modes of interest to the app.
/// The system may not be able to service all the requested cameras.
/// This parameter is treated as a hint and data will be provided from
/// the requested world cameras when available.
///
public readonly uint Cameras;
};
///
/// A structure to encapsulate per plane info for each camera frame.
///
[StructLayout(LayoutKind.Sequential)]
public readonly struct MLWorldCameraFrameBuffer
{
///
/// Width of the output image in pixels.
///
public readonly uint Width;
///
/// Height of the output image in pixels.
///
public readonly uint Height;
///
/// Stride of the output image in bytes.
///
public readonly uint Stride;
///
/// Number of bytes used to represent a pixel.
///
public readonly uint BytesPerPixel;
///
/// Number of bytes in the frame.
///
public readonly uint Size;
///
/// Buffer data.
///
public readonly IntPtr Data;
public override string ToString() => $"Width: {Width}, Height: {Height}, Stride: {Stride}, BytesPerPixel: {BytesPerPixel}, Size: {Size}";
}
///
/// A structure to encapsulate output data for each camera sensor.
///
[StructLayout(LayoutKind.Sequential)]
public readonly struct MLWorldCameraFrame
{
///
/// Camera Identifier specifies which camera is associated with this frame.
///
public readonly MLWorldCamera.CameraId Id;
///
/// A 64bit integer to index the frame number associated with this frame.
///
public readonly long FrameNumber;
///
/// Frame timestamp specifies the time at which the frame was captured.
///
public readonly long TimeStamp;
///
/// Camera intrinsic parameters.
///
public readonly NativeBindings.MLWorldCameraIntrinsics Intrinsics;
///
/// World camera pose in the world co-ordinate system.
///
public readonly MLTransform CameraPose;
///
/// Frame buffer data.
///
public readonly MLWorldCameraFrameBuffer FrameBuffer;
///
/// Maps to the world camera mode used for capturing the camera frames.
///
public readonly Frame.Type FrameType;
public override string ToString() => $"Id: {Id}, FrameNumber:{FrameNumber}, Intrinsics: {Intrinsics}, FrameBuffer: {FrameBuffer}, FrameType: {FrameType}";
}
///
/// A structure to encapsulate output data for each camera sensor.
///
[StructLayout(LayoutKind.Sequential)]
public readonly struct MLWorldCameraData
{
///
/// Version of this structure.
///
public readonly uint Version;
///
/// Number of camera frames populated.
///
public readonly byte FrameCount;
///
/// Camera frame data.
///
public readonly IntPtr Frames;
public MLWorldCameraData(uint version)
{
Version = version;
FrameCount = 0;
Frames = IntPtr.Zero;
}
};
///
/// Connect to world cameras.
///
[DllImport(MLPerceptionClientDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWorldCameraConnect(in MLWorldCameraSettings settings, out ulong handle);
///
/// Update the world camera settings.
///
[DllImport(MLPerceptionClientDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWorldCameraUpdateSettings(ulong handle, in MLWorldCameraSettings settings);
///
/// Poll for Frames. Returns #MLWorldCameraData with this latest data when available. The memory is owned by the system.
/// Application should copy the data it needs to cache it and then release the memory by calling
/// #MLWorldCameraReleaseCameraData. This is a blocking call. API is not thread safe. If there are no new world camera data frames for a given
/// duration (duration determined by the system) then the API will return MLResult_Timeout.
///
[DllImport(MLPerceptionClientDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWorldCameraGetLatestWorldCameraData(ulong handle, uint timeOutMs, ref IntPtr cameraData);
///
/// Releases specified #MLWorldCameraData object. This function should be called exactly once for each call to
/// #MLWorldCameraGetLatestCameraData.
///
[DllImport(MLPerceptionClientDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWorldCameraReleaseCameraData(ulong handle, IntPtr cameraData);
///
/// Disconnect from world camera. This will disconnect from all the world camera currently connected.
///
[DllImport(MLPerceptionClientDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWorldCameraDisconnect(ulong handle);
}
}
}