Esempio n. 1
0
    protected override void CheckIfDetectedMarkers()
    {
        base.CheckIfDetectedMarkers();

        for (int i = 0; i < ids.Length; i++)
        {
            Cv2.CornerSubPix(grayedImg, corners[i], new Size(5, 5), new Size(-1, -1), TermCriteria.Both(30, 0.1));

            if (!MarkerManager.IsMarkerRegistered(ids[i]))
            {
                continue;
            }

            MarkerBehaviour m = MarkerManager.GetMarker(ids[i]);

            if (!allDetectedMarkers.ContainsKey(ids[i]))
            {
                m.OnMarkerDetected.Invoke();
                allDetectedMarkers.Add(m.GetMarkerID(), m);
            }

            // m.UpdateMarker(img.Cols, img.Rows, corners[i], rejectedImgPoints[i]);
            m.UpdateMarker(corners[i], calibrationData.GetCameraMatrix(),
                           calibrationData.GetDistortionCoefficients(), grayedImg);
        }
    }
Esempio n. 2
0
    protected override void CheckIfDetectedMarkers()
    {
        base.CheckIfDetectedMarkers();

        for (int i = 0; i < ids.Length; i++)
        {
            Cv2.CornerSubPix(grayedImg, corners[i], new Size(5, 5), new Size(-1, -1), TermCriteria.Both(30, 0.1));

            if (!MarkerManager.IsMarkerRegistered(ids[i]))
            {
                continue;
            }

            MarkerBehaviour m = MarkerManager.GetMarker(ids[i]);

            if (!allDetectedMarkers.ContainsKey(ids[i]))
            {
                Debug.Log("FOUND MARKER: " + m.GetMarkerID());
                m.OnMarkerDetected.Invoke();
                allDetectedMarkers.Add(m.GetMarkerID(), m);
            }

            float rotZ = 0;

            switch (Screen.orientation)
            {
            case ScreenOrientation.Portrait:
                rotZ = 90;
                break;

            case ScreenOrientation.LandscapeLeft:
                rotZ = 180;
                break;

            case ScreenOrientation.LandscapeRight:
                rotZ = 0;
                break;

            case ScreenOrientation.PortraitUpsideDown:
                rotZ = -90;
                break;
            }

            if (!UseCustomCalibration)
            {
                cameraManager.TryGetIntrinsics(out cameraIntrinsics);

                m.UpdateMarker(corners[i], cameraIntrinsics, grayedImg, Vector3.forward * rotZ);
            }
            else
            {
                m.UpdateMarker(corners[i], calibrationData.GetCameraMatrix(), calibrationData.GetDistortionCoefficients(), grayedImg, Vector3.forward * rotZ);
            }
        }
    }
Esempio n. 3
0
        /// <summary>
        /// Draws a village on the map
        /// </summary>
        /// <param name="g">The graphics object</param>
        /// <param name="game">The game location of the village</param>
        /// <param name="mapVillage">Where and how big to draw the village</param>
        private void Paint(Graphics g, Point game, Rectangle mapVillage)
        {
            if (!(game.X >= 0 && game.X < 1000 && game.Y >= 0 && game.Y < 1000))
            {
                return;
            }

            Village    village;
            DrawerBase finalCache = null;

            if (World.Default.Villages.TryGetValue(game, out village))
            {
                Marker marker = _markers.GetMarker(Settings, village);
                if (marker != null)
                {
                    // Paint village icon/shape
                    BackgroundDrawerData mainData = World.Default.Views.GetBackgroundDrawerData(village, marker);
                    if (mainData != null)
                    {
                        finalCache = _drawerFactoryStrategy.CreateVillageDrawer(village.Bonus, mainData, marker);
                        if (finalCache != null)
                        {
                            finalCache.PaintVillage(g, mapVillage);

                            if (_drawerFactoryStrategy.SupportDecorators && village.Type != VillageType.None)
                            {
                                // Paint extra village decorators
                                foreach (DrawerBase decorator in World.Default.Views.GetDecoratorDrawers(_drawerFactoryStrategy, village, mainData))
                                {
                                    decorator.PaintVillage(g, mapVillage);
                                }
                            }
                        }
                    }
                }
            }

            if (finalCache == null)
            {
                PaintNonVillage(g, game, mapVillage);
            }
        }
    protected override void ConcentrateOnTheClosestMarker(int[] markerIds)
    {
        MarkerBehaviour closestMarker   = null;
        float           closestDistance = Mathf.Infinity;

        markersCache.Clear();

        //Perform direction filtering
        if (minDirectionDotProductValue >= -0.9f)
        {
            Vector3    oldPos      = transform.position;
            Quaternion oldRotation = transform.rotation;

            foreach (int i in markerIds)
            {
                MarkerBehaviour m = MarkerManager.GetMarker(i);

                if (m == null)
                {
                    continue;
                }

                Pose targetPose = GetTargetPose(m);

                //Move in the target pose before checking for direction
                transform.SetPositionAndRotation(targetPose.position, targetPose.rotation);

                Vector3 cameraForward = new Vector3(transform.forward.x, 0, transform.forward.z);
                Vector3 markerForward = new Vector3(m.transform.forward.x, 0, m.transform.forward.z);

                float dot = Vector3.Dot(cameraForward, markerForward);

                // Debug.Log("DOT: " + dot);

                if (dot > 0)
                {
                    //store the ones that we need
                    markersCache.Add(m);
                }
                else
                {
                    if (trackingTarget != null && m.GetMarkerID() == trackingTarget.GetMarkerID())
                    {
                        trackingTarget = null;
                    }
                }
            }

            //move back where we were
            transform.SetPositionAndRotation(oldPos, oldRotation);
        }

        // if we didnt find anything just check for the closest one
        if (markersCache.Count == 0 || minDirectionDotProductValue < -0.9f)
        {
            foreach (int i in markerIds)
            {
                MarkerBehaviour m = MarkerManager.GetMarker(i);

                if (m == null)
                {
                    continue;
                }

                markersCache.Add(m);
            }
        }

        // go through the cache and check for the closest marker and make it the target marker
        foreach (MarkerBehaviour m in markersCache)
        {
            //Debug.Log("IDD: " + i);
            if (GetMarkerDistanceFromCamera(m) < closestDistance)
            {
                closestDistance = GetMarkerDistanceFromCamera(m);
                closestMarker   = m;
            }

            // Debug.Log("ID: " + i + " " + "Distance: " + GetMarkerDistanceFromCamera(m));
        }

        if (closestMarker == null)
        {
            return;
        }

        if (trackingTarget == null)
        {
            trackingTarget = closestMarker;
            Debug.Log("here");
            return;
        }

        float d1 = GetMarkerDistanceFromCamera(closestMarker);
        float d2 = GetMarkerDistanceFromCamera(trackingTarget);

        float difference = Mathf.Abs(d1 - d2);

        //Debug.Log(difference);
        if (difference > 0.1)
        {
            trackingTarget = closestMarker;
        }
    }