// %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.Text; using Native; public partial class MLWorldCamera { /// /// A structure to encapsulate the camera settings. /// [Serializable] public struct Settings { public Settings(Mode mode, CameraId cameras) { this.mode = mode; this.cameras = cameras; } public Mode Mode => mode; public CameraId Cameras => cameras; [SerializeField] private Mode mode; [SerializeField] private CameraId cameras; public override string ToString() => $"Mode: {mode}, Cameras: {cameras}"; } /// /// A structure to encapsulate per plane info for each camera frame. /// public readonly struct Frame { /// /// Enumeration of camera mode used when capturing a frame. /// public enum Type { /// /// None. /// Unkown, /// /// Frame captured using #MLWorldCameraMode_LowExposure mode. /// LowExposure, /// /// Frame captured using #MLWorldCameraMode_NormalEposure mode. /// NormalExposure, }; internal Frame(NativeBindings.MLWorldCameraFrame nativeFrame) { CameraId = nativeFrame.Id; CameraPose = new Pose(MLConvert.ToUnity(nativeFrame.CameraPose.Position), MLConvert.ToUnity(nativeFrame.CameraPose.Rotation)); CameraIntrinsics = new CameraIntrinsics(nativeFrame.Intrinsics); FrameNumber = nativeFrame.FrameNumber; FrameType = nativeFrame.FrameType; FrameBuffer = new Buffer(nativeFrame.FrameBuffer); MLTime.ConvertSystemTimeToMLTime(nativeFrame.TimeStamp, out TimeStamp); } /// /// Camera Identifier specifies which camera is associated with this frame. /// public readonly CameraId CameraId; /// /// World camera pose in the world co-ordinate system. /// public readonly Pose CameraPose; /// /// Camera intrinsics. /// public readonly CameraIntrinsics CameraIntrinsics; /// /// A 64bit integer to index the frame number associated with this frame. /// public readonly long FrameNumber; /// /// Maps to the world camera mode used for capturing the camera frames. /// public readonly Type FrameType; /// /// Frame buffer data. /// public readonly Buffer FrameBuffer; /// /// Frame timestamp specifies the time at which the frame was captured. /// public readonly MLTime TimeStamp; public override string ToString() => $"Id: {CameraId}, Pose: {CameraPose}, Intrinsics: {CameraIntrinsics}, FrameBufferInfo: {FrameBuffer}, TimeStamp: {TimeStamp}"; /// /// A structure to encapsulate per plane info for each camera frame. /// public readonly struct Buffer { internal Buffer(NativeBindings.MLWorldCameraFrameBuffer nativeFrameBuffer) { Width = nativeFrameBuffer.Width; Height = nativeFrameBuffer.Height; Stride = nativeFrameBuffer.Stride; BytesPerPixel = nativeFrameBuffer.BytesPerPixel; Data = nativeFrameBuffer.Data; DataSize = (int)nativeFrameBuffer.Size; } /// /// 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 int DataSize; /// /// Image data. /// public readonly IntPtr Data; public override string ToString() => $"Width: {Width}, Height: {Height}, Stride: {Stride}, BytesPerPixel: {BytesPerPixel}, Size: {DataSize}"; }; } /// /// World camera intrinsic parameters. /// public readonly struct CameraIntrinsics { internal CameraIntrinsics(in NativeBindings.MLWorldCameraIntrinsics nativeIntrinsics) { Width = nativeIntrinsics.Width; Height = nativeIntrinsics.Height; FocalLength = nativeIntrinsics.FocalLength; PrincipalPoint = nativeIntrinsics.PrincipalPoint; Fov = nativeIntrinsics.Fov; RadialDistortion = nativeIntrinsics.RadialDistortion; TangentialDistortion = nativeIntrinsics.TangentialDistortion; } /// /// 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]. /// public readonly double[] RadialDistortion; /// /// Tangential distortion vector. /// The tangential distortion co-efficients are in the following order: [p1, p2]. /// public readonly double[] TangentialDistortion; public override string ToString() => $"Width:{Width}, Height:{Height}, FocalLength: {FocalLength}, PrincipalPoint: {PrincipalPoint} , FOV: {Fov}, RadialDistortion: {GetArrayAsString(RadialDistortion)}, TangentialDistortion: {GetArrayAsString(TangentialDistortion)}"; private string GetArrayAsString(double[] distortions) { StringBuilder sb = new StringBuilder("["); for (int i = 0; i < distortions.Length; ++i) { var value = distortions[i]; sb.Append($"{value}"); if (i < distortions.Length - 1) sb.Append(", "); } sb.Append("]"); return sb.ToString(); } } } }