A custom class similar to Unity's Debug.
Ejemplo n.º 1
0
        public bool GetAllCameraMetadataTags(IntPtr cameraMetadataHandle, List <CameraMetadataTag> resultList)
        {
            IntPtr ndkMetadataHandle = IntPtr.Zero;

            ExternApi.ArImageMetadata_getNdkCameraMetadata(m_NativeSession.SessionHandle,
                                                           cameraMetadataHandle, ref ndkMetadataHandle);

            IntPtr          tagsHandle = IntPtr.Zero;
            int             tagsCount  = 0;
            NdkCameraStatus status     = ExternApi.ACameraMetadata_getAllTags(ndkMetadataHandle, ref tagsCount, ref tagsHandle);

            if (status != NdkCameraStatus.Ok)
            {
                ARDebug.LogErrorFormat("ACameraMetadata_getAllTags error with native camera error code: {0}",
                                       status);
                return(false);
            }

            for (int i = 0; i < tagsCount; i++)
            {
                resultList.Add((CameraMetadataTag)Marshal.PtrToStructure(
                                   MarshalingHelper.GetPtrToUnmanagedArrayElement <int>(tagsHandle, i),
                                   typeof(int)));
            }

            return(true);
        }
Ejemplo n.º 2
0
        public bool GetAllCameraMetadataTags(
            IntPtr cameraMetadataHandle, List <CameraMetadataTag> resultList)
        {
            if (InstantPreviewManager.IsProvidingPlatform)
            {
                InstantPreviewManager.LogLimitedSupportMessage("access camera metadata tags");
                return(false);
            }

            IntPtr tagsHandle = IntPtr.Zero;
            int    tagsCount  = 0;

            ExternApi.ArImageMetadata_getAllKeys(
                _nativeSession.SessionHandle, cameraMetadataHandle, ref tagsCount, ref tagsHandle);
            if (tagsCount == 0 || tagsHandle == IntPtr.Zero)
            {
                ARDebug.LogError(
                    "ArImageMetadata_getAllKeys error with empty metadata keys list.");
                return(false);
            }

            for (int i = 0; i < tagsCount; i++)
            {
                resultList.Add((CameraMetadataTag)Marshal.PtrToStructure(
                                   MarshalingHelper.GetPtrToUnmanagedArrayElement <int>(tagsHandle, i),
                                   typeof(int)));
            }

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Check if a ray hit point with the point cloud.
        /// </summary>
        /// <param name="ray">A ray start from tracked position.</param>
        /// <param name="pointcloud">A point cloud to hit aginst.</param>
        /// <param name="hitPoint">The hit point's position.</param>
        private static bool _IsRayIntersectingPoint(Ray ray, PointCloud pointcloud, ref Vector3 hitPoint)
        {
            int pointcloudCount = pointcloud.PointCount;

            if (pointcloudCount == 0)
            {
                ARDebug.LogError("point cloud count is zero");
                return(false);
            }

            float min          = float.MaxValue;
            bool  isPointFound = false;

            for (int i = 0; i < pointcloudCount; ++i)
            {
                Vector3 point = pointcloud.GetPoint(i);
                Vector3 v0    = point - ray.origin;
                Vector3 v1    = ray.direction;
                float   angle = Vector3.Angle(v0, v1);

                const float TOUCH_SEARCH_ANGLE_DEGREE = 5.0f;

                if (angle < TOUCH_SEARCH_ANGLE_DEGREE && angle < min)
                {
                    isPointFound = true;
                    hitPoint     = point;
                }
            }
            return(isPointFound);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets a unity PoseData equivalent to the ApiPoseData.
        /// </summary>
        /// <returns>A unity PoseData equivalent to the ApiPoseData.</returns>
        public UnityTango.PoseData ToUnityType()
        {
            UnityTango.PoseData unityPose = new UnityTango.PoseData();

            unityPose.version    = (uint)version;
            unityPose.timestamp  = timestamp;
            unityPose.statusCode = statusCode.ToUnityType();
            unityPose.frame      = framePair.ToUnityType();
            unityPose.confidence = (uint)confidence;
            unityPose.accuracy   = accuracy;

            if (framePair.baseFrame != ApiCoordinateFrameType.StartOfService)
            {
                ARDebug.LogErrorFormat("apiPlaneData's base frame is not supported.");
                unityPose.translation_x = translation.x;
                unityPose.translation_y = translation.y;
                unityPose.translation_z = translation.z;

                unityPose.orientation_x = orientation.x;
                unityPose.orientation_y = orientation.y;
                unityPose.orientation_z = orientation.z;
                unityPose.orientation_w = orientation.w;
                return(unityPose);
            }


            Matrix4x4 startService_T_plane = Matrix4x4.TRS(translation.ToVector3(),
                                                           orientation.ToQuaternion(), Vector3.one);

            Matrix4x4 unityTransform = Constants.UNITY_WORLD_T_START_SERVICE *
                                       startService_T_plane *
                                       Constants.UNITY_WORLD_T_START_SERVICE.inverse;

            Vector3 position = unityTransform.GetColumn(3);

            unityPose.translation_x = position.x;
            unityPose.translation_y = position.y;
            unityPose.translation_z = position.z;

            Quaternion rotation = Quaternion.LookRotation(unityTransform.GetColumn(2),
                                                          unityTransform.GetColumn(1));

            unityPose.orientation_x = rotation.x;
            unityPose.orientation_y = rotation.y;
            unityPose.orientation_z = rotation.z;
            unityPose.orientation_w = rotation.w;
            return(unityPose);
        }
        public static FeaturePointOrientationMode ToFeaturePointOrientationMode(
            this ApiFeaturePointOrientationMode apiMode)
        {
            switch (apiMode)
            {
            case ApiFeaturePointOrientationMode.Identity:
                return(FeaturePointOrientationMode.Identity);

            case ApiFeaturePointOrientationMode.SurfaceNormal:
                return(FeaturePointOrientationMode.SurfaceNormal);

            default:
                ARDebug.LogError("Invalid value for ApiFeaturePointOrientationMode.");
                return(FeaturePointOrientationMode.Identity);
            }
        }
Ejemplo n.º 6
0
        private bool _UpdateMostRecentApiPlanes()
        {
            IntPtr apiPlanesPtr = IntPtr.Zero;
            int    planeCount   = 0;

            if (TangoClientApi.TangoService_Experimental_getPlanes(ref apiPlanesPtr, ref planeCount).IsTangoFailure())
            {
                for (int i = 0; i < m_mostRecentApiPlanes.Count; i++)
                {
                    ApiPlaneData planeData = m_mostRecentApiPlanes[i];
                    planeData.isValid        = false;
                    m_mostRecentApiPlanes[i] = planeData;
                }

                return(true);
            }

            // The planes api is handling a COM reset, and the current state is not available. Leave the most recent
            // Api planes collection unchanged.
            if (apiPlanesPtr == null)
            {
                return(false);
            }

            // Marshal the most recent planes returned from the Api into m_mostRecentApiPlanes.
            m_mostRecentApiPlanes.Clear();
            MarshalingHelper.AddUnmanagedStructArrayToList <ApiPlaneData>(apiPlanesPtr, planeCount,
                                                                          m_mostRecentApiPlanes);

            if (TangoClientApi.TangoPlaneData_free(apiPlanesPtr, planeCount).IsTangoFailure())
            {
                ARDebug.LogErrorFormat("Failed to deallocate planes from the ARCore API.");
            }

            return(false);
        }
Ejemplo n.º 7
0
        public bool TryGetValues(IntPtr cameraMetadataHandle,
                                 CameraMetadataTag tag, List <CameraMetadataValue> resultList)
        {
            IntPtr ndkMetadataHandle = IntPtr.Zero;

            ExternApi.ArImageMetadata_getNdkCameraMetadata(m_NativeSession.SessionHandle,
                                                           cameraMetadataHandle, ref ndkMetadataHandle);

            resultList.Clear();
            NdkCameraMetadata entry  = new NdkCameraMetadata();
            NdkCameraStatus   status = ExternApi.ACameraMetadata_getConstEntry(ndkMetadataHandle, tag, ref entry);

            if (status != NdkCameraStatus.Ok)
            {
                ARDebug.LogErrorFormat("ACameraMetadata_getConstEntry error with native camera error code: {0}",
                                       status);
                return(false);
            }

            for (int i = 0; i < entry.Count; i++)
            {
                switch (entry.Type)
                {
                case NdkCameraMetadataType.Byte:
                    sbyte byteValue = (sbyte)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <sbyte>(entry.Value, i),
                        typeof(sbyte));
                    resultList.Add(new CameraMetadataValue(byteValue));
                    break;

                case NdkCameraMetadataType.Int32:
                    int intValue = (int)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <int>(entry.Value, i),
                        typeof(int));
                    resultList.Add(new CameraMetadataValue(intValue));
                    break;

                case NdkCameraMetadataType.Float:
                    float floatValue = (float)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <float>(entry.Value, i),
                        typeof(float));
                    resultList.Add(new CameraMetadataValue(floatValue));
                    break;

                case NdkCameraMetadataType.Int64:
                    long longValue = (long)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <long>(entry.Value, i),
                        typeof(long));
                    resultList.Add(new CameraMetadataValue(longValue));
                    break;

                case NdkCameraMetadataType.Double:
                    double doubleValue = (double)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <double>(entry.Value, i),
                        typeof(double));
                    resultList.Add(new CameraMetadataValue(doubleValue));
                    break;

                case NdkCameraMetadataType.Rational:
                    CameraMetadataRational rationalValue = (CameraMetadataRational)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <CameraMetadataRational>(entry.Value, i),
                        typeof(CameraMetadataRational));
                    resultList.Add(new CameraMetadataValue(rationalValue));
                    break;

                default:
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 8
0
        public bool TryGetValues(IntPtr cameraMetadataHandle,
                                 CameraMetadataTag tag, List <CameraMetadataValue> resultList)
        {
            resultList.Clear();
            uint             utag   = (uint)tag;
            ArCameraMetadata entry  = new ArCameraMetadata();
            ApiArStatus      status =
                ExternApi.ArImageMetadata_getConstEntry(_nativeSession.SessionHandle,
                                                        cameraMetadataHandle, utag,
                                                        ref entry);

            if (status != ApiArStatus.Success)
            {
                ARDebug.LogErrorFormat(
                    "ArImageMetadata_getConstEntry error with native camera error code: {0}",
                    status);
                return(false);
            }

            if (entry.Count > _maximumTagCountForWarning && !_warningTags.Contains((int)tag))
            {
                Debug.LogWarningFormat(
                    "TryGetValues for tag {0} has {1} values. Accessing tags with a large " +
                    "number of values may impede performance.", tag, entry.Count);
                _warningTags.Add((int)tag);
            }

            for (int i = 0; i < entry.Count; i++)
            {
                switch (entry.Type)
                {
                case ArCameraMetadataType.Byte:
                    sbyte byteValue = (sbyte)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <sbyte>(entry.Value, i),
                        typeof(sbyte));
                    resultList.Add(new CameraMetadataValue(byteValue));
                    break;

                case ArCameraMetadataType.Int32:
                    int intValue = (int)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <int>(entry.Value, i),
                        typeof(int));
                    resultList.Add(new CameraMetadataValue(intValue));
                    break;

                case ArCameraMetadataType.Float:
                    float floatValue = (float)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <float>(entry.Value, i),
                        typeof(float));
                    resultList.Add(new CameraMetadataValue(floatValue));
                    break;

                case ArCameraMetadataType.Int64:
                    long longValue = (long)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <long>(entry.Value, i),
                        typeof(long));
                    resultList.Add(new CameraMetadataValue(longValue));
                    break;

                case ArCameraMetadataType.Double:
                    double doubleValue = (double)Marshal.PtrToStructure(
                        MarshalingHelper.GetPtrToUnmanagedArrayElement <double>(entry.Value, i),
                        typeof(double));
                    resultList.Add(new CameraMetadataValue(doubleValue));
                    break;

                case ArCameraMetadataType.Rational:
                    CameraMetadataRational rationalValue =
                        (CameraMetadataRational)Marshal.PtrToStructure(
                            MarshalingHelper
                            .GetPtrToUnmanagedArrayElement <CameraMetadataRational>(
                                entry.Value, i),
                            typeof(CameraMetadataRational));
                    resultList.Add(new CameraMetadataValue(rationalValue));
                    break;

                default:
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 9
0
        private void _UpdatePlanes(List <ApiPlaneData> latestPlaneData, List <TrackedPlaneRecord> newPlaneRecords,
                                   List <TrackedPlaneRecord> planeRecords, bool forceUpdate)
        {
            // Add latest planedata to a convenient hash on ID (note: not hashing plane itself to avoid potential boxing
            // of the struct on comparison overloads).
            m_tmpLastestPlanesHash.Clear();
            for (int i = 0; i < latestPlaneData.Count; i++)
            {
                m_tmpLastestPlanesHash.Add(latestPlaneData[i].id);
            }

            // Create or update all planes that appear in latestPlaneData.
            newPlaneRecords.Clear();
            for (int i = 0; i < latestPlaneData.Count; i++)
            {
                int planeRecordIndex = _FindPlaneRecordById(latestPlaneData[i].id, planeRecords);
                if (planeRecordIndex != -1)
                {
                    planeRecords[planeRecordIndex] = new TrackedPlaneRecord(planeRecords[planeRecordIndex].m_plane,
                                                                            latestPlaneData[i], planeRecords[planeRecordIndex].m_updatePlane);
                    planeRecords[planeRecordIndex].m_updatePlane(latestPlaneData[i], null, forceUpdate);
                }
                else
                {
                    Action <ApiPlaneData, TrackedPlane, bool> updatePlane;
                    var trackedPlane = new TrackedPlane(latestPlaneData[i], out updatePlane);
                    newPlaneRecords.Add(new TrackedPlaneRecord(trackedPlane, latestPlaneData[i], updatePlane));
                }
            }

            // Add new planes into plane records
            for (int i = 0; i < newPlaneRecords.Count; i++)
            {
                planeRecords.Add(newPlaneRecords[i]);
            }

            // Invalidate planes that were previously tracking but not present in latestPlaneData.  Delete planes that
            // are invalid or subsumed.
            for (int i = planeRecords.Count - 1; i >= 0; i--)
            {
                if (!m_tmpLastestPlanesHash.Contains(planeRecords[i].m_planeData.id))
                {
                    ApiPlaneData invalidPlane = new ApiPlaneData();
                    invalidPlane.id = planeRecords[i].m_planeData.id;
                    planeRecords[i].m_updatePlane(invalidPlane, null, true);
                    planeRecords.RemoveAt(i);
                }
                else if (planeRecords[i].m_planeData.subsumedBy > -1)
                {
                    int subsumerIndex = _FindPlaneRecordById(planeRecords[i].m_planeData.subsumedBy, planeRecords);
                    if (subsumerIndex == -1)
                    {
                        ARDebug.LogError("Failed to find a record of the subsuming plane.");
                        continue;
                    }

                    planeRecords[i].m_updatePlane(planeRecords[i].m_planeData, planeRecords[subsumerIndex].m_plane,
                                                  true);
                    planeRecords.RemoveAt(i);
                }
            }
        }