internal void UpdateVisualization(ARPointCloud currentPointCloud)
        {
            Func <double, double, double, bool> RangeContains = (low, high, val) => val >= low && val <= high;

            if (Hidden)
            {
                return;
            }

            var min          = Position + Center - Extent / 2;
            var max          = Position + Center + Extent / 2;
            var inlierPoints = new List <SCNVector3>();

            foreach (var point in currentPointCloud.Points)
            {
                var localPoint = ConvertPositionFromNode(point.ToSCNVector3(), null);
                if (RangeContains(min.X, max.X, localPoint.X) &&
                    RangeContains(min.Y, max.Y, localPoint.Y) &&
                    RangeContains(min.Z, max.Z, localPoint.Z))
                {
                    inlierPoints.Add(localPoint);
                }
            }

            var currentPointCloudInliers = inlierPoints.Select(s => s.ToNVector3()).ToArray();

            Geometry?.Dispose();
            Geometry = PointCloud.CreateVisualization(currentPointCloudInliers, Utilities.AppGreen, 12);
        }
Exemple #2
0
        public void Update()
        {
            // Do not update if huaweiAR is not tracking.
            if (ARFrame.GetTrackingState() != ARTrackable.TrackingState.TRACKING)
            {
                m_mesh.Clear();
                return;
            }

            // Fill in the data to draw the point cloud.
            ARPointCloud pointCloud = ARFrame.AcquirePointCloud();

            pointCloud.GetPoints(ref m_points);
            pointCloud.Release();
            if (m_points.Count > 0)
            {
                // Update the mesh indicies array.
                m_pointIndex.Clear();
                for (int i = 0; i < Mathf.Min(m_points.Count, k_maxPointCount); i++)
                {
                    m_pointIndex.Add(i);
                }

                m_mesh.Clear();
                m_mesh.vertices = m_points.ToArray();
                m_mesh.SetIndices(m_pointIndex.ToArray(), MeshTopology.Points, 0);
            }
        }
Exemple #3
0
    IEnumerator Start()
    {
        OnboardingManager mng = FindObjectOfType <OnboardingManager>();

        uiLineRenderer = doorIndicator.transform.Find("UI LineRenderer").GetComponent <UILineRenderer>();
        doorButton     = doorIndicator.transform.Find("DoorButton").GetComponent <RectTransform>();
        rayManager     = FindObjectOfType <ARRaycastManager>();
        cam            = Camera.main;

        // Extract needed placement helper objects from parent
        placementColliderObj = placementHelpers.transform.GetChild(1).gameObject;

        // Get the collider attached to the child object
        placementCollider = placementColliderObj.GetComponentInChildren <Collider>();

        scale = FindObjectOfType <ScalingManager>();

        // The point cloud needs a moment to be created by the pointCloudManager,
        // therefor wait until onboarding is done
        while (!mng.GetComplete())
        {
            yield return(null);
        }

        cloud = FindObjectOfType <ARPointCloud>();
        Debug.Log(cloud);
    }
        private void PointcloudChanged(ARPointCloudChangedEventArgs obj)
        {
            if (!m_generate)
            {
                return;
            }

            List <Vector3> addedPoints = new List <Vector3>();

            for (int i = 0; i < obj.updated.Count; i++)
            {
                ARPointCloud cloud = obj.updated[i];
                if (cloud.positions.HasValue)
                {
                    for (int x = 0; x < cloud.positions.Value.Length; x++)
                    {
                        // only allow points over the confidenceThreshhold
                        if (cloud.confidenceValues.Value[x] > m_confidenceThreshhold)
                        {
                            addedPoints.Add(cloud.positions.Value[x]);
                        }
                    }
                }
            }

            if (addedPoints.Count > 0)
            {
                // only do color calculation if colormode is on
                if (m_includeColors)
                {
                    UpdateCameraTexture();
                    List <Color> pointColors = new List <Color>();

                    for (int i = 0; i < addedPoints.Count; i++)
                    {
                        Vector2 screenpos    = cam.WorldToScreenPoint(addedPoints[i]);
                        Vector2 texturecoord = screenpos;
                        texturecoord.x = Mathf.Clamp01(texturecoord.x / Display.main.renderingWidth);
                        texturecoord.y = Mathf.Clamp01(texturecoord.y / Display.main.renderingHeight);

                        //  texturecoord.y = 1 - texturecoord.y;

                        // flip X
                        texturecoord.x = 1 - texturecoord.x;

                        pointColors.Add(m_cameraTexture.GetPixel((int)(texturecoord.y * m_cameraTexture.width), (int)(texturecoord.x * m_cameraTexture.height)));
                    }

                    m_manager.Populate(addedPoints.ToArray(), pointColors.ToArray());
                }
                else
                {
                    m_manager.Populate(addedPoints.ToArray());
                }
            }
        }
 public void Init(string path, string title)
 {
     pointDensity = PlayerPrefsHandler.Instance.GetFloat("pointDensity", 0.05f);
     filePath     = $"{path}{title}_{j_Prefix()}.json";
     lastFrame    = false;
     JsonFileWriter.WriteDataToFile(path: filePath, text: "", title: "points", lastFrame: false);
     arPointCloud = GameObject.FindGameObjectWithTag("pointCloud").GetComponent <ARPointCloud>();
     ReceivePointCloud();
     recording = true;
 }
        private void CalculateConfidance(ARPointCloud pointCloud)
        {
#if UNITY_EDITOR || UNITY_ANDROID
            foreach (float confidance in pointCloud.confidenceValues.Value)
            {
                threshold = confidance;
            }
#elif UNITY_IOS
            threshold = 0.26f; // since we do not get confidencValue in iOS
#endif
        }
Exemple #7
0
 // Update is called once per frame
 void Update()
 {
     if (placementFinished && arPointCloud == null)
     {
         arPointCloud = arSessionOrigin.trackablesParent.GetComponentInChildren <ARPointCloud>();
     }
     if (arVisualizer == null)
     {
         arVisualizer = arSessionOrigin.trackablesParent.GetComponentInChildren <ARPointCloudParticleVisualizer>();
     }
 }
Exemple #8
0
        public static FeatureHitTestResult?HitTestFromOrigin(this ARSCNView view, SCNVector3 origin, SCNVector3 direction)
        {
            FeatureHitTestResult?result = null;

            ARPointCloud features = null;

            using (var frame = view.Session.CurrentFrame)
            {
                features = frame?.RawFeaturePoints;
            }

            if (features != null)
            {
                var points = features.Points;

                // Determine the point from the whole point cloud which is closest to the hit test ray.
                var closestFeaturePoint = origin;
                var minDistance         = float.MaxValue; // Float.greatestFiniteMagnitude

                for (nuint i = 0; i < features.Count; i++)
                {
                    var feature = points[i];

                    var featurePosition = new SCNVector3((Vector3)feature);
                    var originVector    = origin - featurePosition;

                    var crossProduct = SCNVector3.Cross(originVector, direction);
                    var featureDistanceFromResult = crossProduct.Length;

                    if (featureDistanceFromResult < minDistance)
                    {
                        closestFeaturePoint = featurePosition;
                        minDistance         = featureDistanceFromResult;
                    }
                }

                // Compute the point along the ray that is closest to the selected feature.
                var originToFeature       = closestFeaturePoint - origin;
                var hitTestResult         = origin + (direction * SCNVector3.Dot(direction, originToFeature));
                var hitTestResultDistance = (hitTestResult - origin).Length;

                result = new FeatureHitTestResult
                {
                    Position                   = hitTestResult,
                    DistanceToRayOrigin        = hitTestResultDistance,
                    FeatureHit                 = closestFeaturePoint,
                    FeatureDistanceToHitResult = minDistance
                };
            }

            return(result);
        }
        internal DetectedPointCloud(ARPointCloud referenceObjectPointCloud, NVector3 center, NVector3 extent)
        {
            ReferenceObjectPointCloud = referenceObjectPointCloud;
            Center = center.ToSCNVector3();
            Extent = extent.ToSCNVector3();

            base.Init();

            // Semitransparently visualize the reference object's points.
            var referenceObjectPoints = new SCNNode();

            referenceObjectPoints.Geometry = PointCloud.CreateVisualization(referenceObjectPointCloud.Points, Utilities.AppYellow, 12);
            AddChildNode(referenceObjectPoints);
        }
    private IEnumerable <Model_Point> ConvertPointCloud(ARPointCloud pointCloud)
    {
        if (!pointCloud.positions.HasValue)
        {
            return(null);
        }
        List <Model_Point> ret = new List <Model_Point>();
        var length             = pointCloud.positions.Value;

        foreach (var vector in pointCloud.positions.Value)
        {
            ret.Add(new Model_Point(vector.x, vector.y, vector.z));
        }
        return(ret);
    }
        private void AddPointCloudToList(ARPointCloud pointCloud)
        {
            currIndex = 0;

            foreach (var pos in pointCloud.positions.Value)
            {
                currIndex++;

                if (threshold > 0.25f)
                {
#if UNITY_EDITOR || UNITY_ANDROID
                    updatedPoints.Add(pos);
#elif UNITY_IOS
                    // add alternate point values
                    if (currIndex % 2 == 0)
                    {
                        updatedPoints.Add(pos);
                    }
#endif
                }
            }
        }
Exemple #12
0
 // Use this for initialization
 void Start()
 {
     ParticleSystem = GetComponent <ParticleSystem>();
     PointCloud     = GetComponent <ARPointCloud>();
 }
Exemple #13
0
        public static IList <FeatureHitTestResult> HitTestWithFeatures(this ARSCNView view,
                                                                       CGPoint point,
                                                                       float coneOpeningAngleInDegrees,
                                                                       float minDistance = 0,
                                                                       float maxDistance = float.MaxValue,
                                                                       int maxResults    = 1)
        {
            var results = new List <FeatureHitTestResult>();

            ARPointCloud features = null;

            using (var frame = view.Session.CurrentFrame)
            {
                features = frame?.RawFeaturePoints;
            }

            if (features != null)
            {
                var ray = view.HitTestRayFromScreenPosition(point);
                if (ray.HasValue)
                {
                    var maxAngleInDegrees = Math.Min(coneOpeningAngleInDegrees, 360f) / 2f;
                    var maxAngle          = (maxAngleInDegrees / 180f) * Math.PI;

                    var points = features.Points;
                    for (nuint j = 0; j < features.Count; j++)
                    {
                        var feature = points[j];

                        var featurePosition = new SCNVector3((Vector3)feature);
                        var originToFeature = featurePosition - ray.Value.Origin;

                        var crossProduct = SCNVector3.Cross(originToFeature, ray.Value.Direction);
                        var featureDistanceFromResult = crossProduct.Length;

                        var hitTestResult         = ray.Value.Origin + (ray.Value.Direction * SCNVector3.Dot(ray.Value.Direction, originToFeature));
                        var hitTestResultDistance = (hitTestResult - ray.Value.Origin).Length;

                        if (hitTestResultDistance < minDistance || hitTestResultDistance > maxDistance)
                        {
                            // Skip this feature - it is too close or too far away.
                            continue;
                        }

                        var originToFeatureNormalized = SCNVector3.Normalize(originToFeature);
                        var angleBetweenRayAndFeature = Math.Acos(SCNVector3.Dot(ray.Value.Direction, originToFeatureNormalized));

                        if (angleBetweenRayAndFeature > maxAngle)
                        {
                            // Skip this feature - is outside of the hit test cone.
                            continue;
                        }

                        // All tests passed: Add the hit against this feature to the results.
                        results.Add(new FeatureHitTestResult
                        {
                            Position                   = hitTestResult,
                            DistanceToRayOrigin        = hitTestResultDistance,
                            FeatureHit                 = featurePosition,
                            FeatureDistanceToHitResult = featureDistanceFromResult
                        });
                    }

                    // Sort the results by feature distance to the ray.
                    results = results.OrderBy(result => result.DistanceToRayOrigin).ToList();

                    // Cap the list to maxResults.
                    var cappedResults = new List <FeatureHitTestResult>();
                    var i             = 0;

                    while (i < maxResults && i < results.Count)
                    {
                        cappedResults.Add(results[i]);
                        i += 1;
                    }

                    results = cappedResults;
                }
            }

            return(results);
        }
Exemple #14
0
 void Awake()
 {
     pointCloud       = GetComponent <ARPointCloud>();
     arParticleSystem = GetComponent <ParticleSystem>();
 }
 void Awake()
 {
     pointCloud = GetComponent <ARPointCloud>();
 }