Ejemplo n.º 1
0
        /// <summary>
        /// Get the frame pose.
        /// </summary>
        /// <param name="cameraId">The camera id.</param>
        /// <param name="vcamTimestamp">The timestamp of the frame pose.</param>
        /// <param name="outTransform">The transform of the frame pose.</param>
        /// <returns>
        /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
        /// </returns>
        private MLResult InternalGetFramePose(MLCVCameraNativeBindings.CameraID cameraId, ulong vcamTimestamp, out Matrix4x4 outTransform)
        {
            MagicLeapNativeBindings.MLTransform outInternalTransform = new MagicLeapNativeBindings.MLTransform();

            MLResult.Code resultCode = MLCVCameraNativeBindings.MLCVCameraGetFramePose(Handle, headTrackerHandle, cameraId, vcamTimestamp, ref outInternalTransform);
            MLResult      poseResult = MLResult.Create(resultCode);

            if (!poseResult.IsOk)
            {
                MLPluginLog.ErrorFormat("MLCamera.InternalGetFramePose failed to get camera frame pose. Reason: {0}", poseResult);
                outTransform = new Matrix4x4();
            }
            else
            {
                outTransform = MLConvert.ToUnity(outInternalTransform);
            }

            return(poseResult);
        }
            /// <summary>
            /// Sets the offset position/rotation values from the current Perception root and can also set the PCF to be used as the new Perception root to which all MLSnapshotGetTransform will be relative to.
            /// </summary>
            /// <param name="offsetPosition">The offset to set from the root's position.</param>
            /// <param name="offsetRotation">The offset to set from the root's rotation.</param>
            /// <param name="pcf">The PCF to set the root to.</param>
            /// <returns>
            /// MLResult.Result will be <c>MLResult.Code.Ok</c> if operation completed successfully.
            /// MLResult.Result will be <c>MLResult.Code.MLPerceptionNotStarted</c> if unable to retrieve the Perception System.
            /// MLResult.Result will be <c>MLResult.Code.MLPerceptionInvalidPCFRoot</c> if the provided CFUID is not associated with a valid PCF.
            /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the CFUID is not valid.
            /// </returns>
            public static MLResult.Code SetPerceptionRoot(Vector3 offsetPosition, Quaternion offsetRotation, PCF pcf)
            {
                IntPtr cfuidPointer       = IntPtr.Zero;
                IntPtr mlTransformPointer = IntPtr.Zero;

                try
                {
                    if (pcf != null)
                    {
                        cfuidPointer = Marshal.AllocHGlobal(Marshal.SizeOf(pcf.CFUID));
                        Marshal.StructureToPtr(pcf.CFUID, cfuidPointer, false);
                    }

                    MagicLeapNativeBindings.MLTransform rootOffset = new MagicLeapNativeBindings.MLTransform();
                    rootOffset.Position = MLConvert.FromUnity(offsetPosition);
                    rootOffset.Rotation = MLConvert.FromUnity(offsetRotation);

                    mlTransformPointer = Marshal.AllocHGlobal(Marshal.SizeOf(rootOffset));
                    Marshal.StructureToPtr(rootOffset, mlTransformPointer, false);

                    MLResult.Code resultCode = NativeBindings.MLPerceptionSetRootCoordinateFrame(cfuidPointer, mlTransformPointer);

                    Marshal.FreeHGlobal(cfuidPointer);
                    Marshal.FreeHGlobal(mlTransformPointer);

                    return(resultCode);
                }
                catch (EntryPointNotFoundException)
                {
                    MLPluginLog.Error("MLPersistentCoordinateFrames.NativeBindings.SetPerceptionRoot failed. Reason: API symbols not found.");

                    Marshal.FreeHGlobal(cfuidPointer);
                    Marshal.FreeHGlobal(mlTransformPointer);

                    return(MLResult.Code.UnspecifiedFailure);
                }
            }