Beispiel #1
0
        void UpdateCirclePatternSize()
        {
            if (_state == State.Initiating || _state == State.BlindCalibration)
            {
                _circlePatternSize = defaultCirclesPatternSize;
            }
            else
            {
                const int circlePatternSizeYMin = 7;
                const int desiredPixelsPerCirclePatternSegment = 25;                                               // 50px is recommended, but for a 720p camera, this will give too fex dots.
                float     desiredCirclePatternNumAspect        = _chessPatternSize.x / (_chessPatternSize.y) / 2f; // 3f / 4f / 2f;

                float patternDistance             = Vector3.Distance(_mainCamera.transform.position, _circlePatternTransform.position);
                float patternHeight               = _chessPatternTransform.localScale.y;
                float viewHeightAtPatternPosition = Mathf.Tan(_mainCamera.fieldOfView * Mathf.Deg2Rad * 0.5f) * patternDistance * 2;
                int   circlePatternPixelHeight    = (int)((patternHeight / viewHeightAtPatternPosition) * _cameraTexture.height);
                int   optimalPatternSizeY         = Mathf.Max(circlePatternSizeYMin, Mathf.FloorToInt(circlePatternPixelHeight / (float)desiredPixelsPerCirclePatternSegment));
                int   optimalPatternSizeX         = Mathf.FloorToInt(optimalPatternSizeY * desiredCirclePatternNumAspect);
                _circlePatternSize = TrackingToolsHelper.GetClosestValidPatternSize(new Vector2Int(optimalPatternSizeX, optimalPatternSizeY), TrackingToolsHelper.PatternType.AsymmetricCircleGrid);
            }

            _circlePatternPointCount = _circlePatternSize.x * _circlePatternSize.y;

            if (_circlePointsProjectorRenderImageMat != null && _circlePointsProjectorRenderImageMat.rows() == _circlePatternPointCount)
            {
                return;
            }

            if (_circlePointsProjectorRenderImageMat != null)
            {
                _circlePointsProjectorRenderImageMat.release();
            }
            if (_circlePointsRealModelMat != null)
            {
                _circlePointsRealModelMat.release();
            }
            if (_circlePointsDetectedWorldMat != null)
            {
                _circlePointsDetectedWorldMat.release();
            }
            _circlePointsProjectorRenderImageMat.alloc(_circlePatternPointCount);
            _circlePointsRealModelMat.alloc(_circlePatternPointCount);
            _circlePointsDetectedWorldMat.alloc(_circlePatternPointCount);

            // Render pattern to texture.
            _circlePatternBorderSizeUV = TrackingToolsHelper.RenderPattern(_circlePatternSize, TrackingToolsHelper.PatternType.AsymmetricCircleGrid, 2048, ref _circlePatternTexture, ref _patternRenderMaterial, circlePatternBorder, true);
            _circlePatternBoardMaterial.mainTexture = _circlePatternTexture;

            // Update transform to match.
            float circleTextureAspect = _circlePatternTexture.width / (float)_circlePatternTexture.height;
            float borderProportion    = (_circlePatternSize.y - 1 + 2f) / (_circlePatternSize.y - 1f);              // Asymmetric patttern tiles are half the height.

            _circlePatternTransform.localScale = new Vector3(circleTextureAspect, 1, 0) * _chessPatternTransform.localScale.y * borderProportion;

            if (_state == State.TrackedCalibration || _state == State.Testing)
            {
                _circlePatternTransform.localPosition = -Vector3.right * (_chessCirclePatternCenterOffset / 1000f);
            }
        }
Beispiel #2
0
        void OnDestroy()
        {
            if (_camTexMat != null)
            {
                _camTexMat.release();
            }
            if (_tempTransferTexture)
            {
                Destroy(_tempTransferTexture);
            }
            if (_chessCornersImageMat != null)
            {
                _chessCornersImageMat.release();
            }
            if (_previewMaterial)
            {
                Destroy(_previewMaterial);
            }
            if (_arTexture)
            {
                _arTexture.Release();
            }
            if (_chessPatternTexture)
            {
                _chessPatternTexture.Release();
            }
            if (_patternRenderMaterial)
            {
                Destroy(_patternRenderMaterial);
            }
            if (_extrinsicsCalibrator != null)
            {
                _extrinsicsCalibrator.Release();
            }
            if (_undistortMap1 != null)
            {
                _undistortMap1.release();
            }
            if (_undistortMap2 != null)
            {
                _undistortMap2.release();
            }

            Reset();
        }
Beispiel #3
0
    private void sort(ref MatOfPoint2f fourPoints)
    {
        // the argument needs to contain 4 points precisely
        // sort the bounding box to (topleft, topright, bottomright, bottomleft
        double[]     p1, p2, p3, p4;
        List <Point> points = new List <Point>();

        p1 = new double[2] {
            fourPoints.get(0, 0)[0], fourPoints.get(0, 0)[1]
        };
        p2 = new double[2] {
            fourPoints.get(1, 0)[0], fourPoints.get(1, 0)[1]
        };
        p3 = new double[2] {
            fourPoints.get(2, 0)[0], fourPoints.get(2, 0)[1]
        };
        p4 = new double[2] {
            fourPoints.get(3, 0)[0], fourPoints.get(3, 0)[1]
        };
        print("p1: " + (char)p1[0] + "," + (char)p1[1]);
        print("p2: " + (char)p2[0] + "," + (char)p2[1]);
        print("p3: " + (char)p3[0] + "," + (char)p3[1]);
        print("p4: " + (char)p4[0] + "," + (char)p4[1]);

        if (p1[0] < p2[0] && p1[0] < p3[0] || p1[0] < p4[0] && p1[0] < p3[0] || p1[0] < p2[0] && p1[0] < p4[0])
        {
            if (p1[1] < p2[1] && p1[1] < p3[1] || p1[1] < p4[1] && p1[1] < p3[1] || p1[1] < p2[1] && p1[1] < p4[1])
            {
                points.Add(new Point(p1[0], p1[1]));
                if (p2[1] < p3[1] && p2[1] < p4[1])
                {
                    points.Add(new Point(p2[0], p2[1]));
                    if (p3[0] < p4[0])
                    {
                        points.Add(new Point(p4[0], p4[1]));
                        points.Add(new Point(p3[0], p3[1]));
                    }
                    else
                    {
                        points.Add(new Point(p3[0], p3[1]));
                        points.Add(new Point(p4[0], p4[1]));
                    }
                }
                else if (p3[1] < p2[1] && p3[1] < p4[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    if (p2[0] < p4[0])
                    {
                        points.Add(new Point(p4[0], p4[1]));
                        points.Add(new Point(p2[0], p2[1]));
                    }
                    else
                    {
                        points.Add(new Point(p2[0], p2[1]));
                        points.Add(new Point(p4[0], p4[1]));
                    }
                }
                else if (p4[1] < p2[1] && p4[1] < p3[1])
                {
                    points.Add(new Point(p4[0], p4[1]));
                    if (p2[0] < p3[0])
                    {
                        points.Add(new Point(p3[0], p3[1]));
                        points.Add(new Point(p2[0], p2[1]));
                    }
                    else
                    {
                        points.Add(new Point(p2[0], p2[1]));
                        points.Add(new Point(p3[0], p3[1]));
                    }
                }
            }
            else if (p2[0] < p3[0] && p2[0] < p4[0])
            {
                points.Add(new Point(p2[0], p2[1]));
                if (p3[1] < p4[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    points.Add(new Point(p4[0], p4[1]));
                }
                else
                {
                    points.Add(new Point(p4[0], p4[1]));
                    points.Add(new Point(p3[0], p3[1]));
                }
                points.Add(new Point(p1[0], p1[1]));
            }
            else if (p3[0] < p2[0] && p3[0] < p4[0])
            {
                points.Add(new Point(p3[0], p3[1]));
                if (p2[1] < p4[1])
                {
                    points.Add(new Point(p2[0], p2[1]));
                    points.Add(new Point(p4[0], p4[1]));
                }
                else
                {
                    points.Add(new Point(p4[0], p4[1]));
                    points.Add(new Point(p2[0], p2[1]));
                }
                points.Add(new Point(p1[0], p1[1]));
            }
            else if (p4[0] < p3[0] && p4[0] < p2[0])
            {
                points.Add(new Point(p4[0], p4[1]));
                if (p3[1] < p2[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    points.Add(new Point(p2[0], p2[1]));
                }
                else
                {
                    points.Add(new Point(p2[0], p2[1]));
                    points.Add(new Point(p3[0], p3[1]));
                }
                points.Add(new Point(p1[0], p1[1]));
            }
        }
        else if (p2[0] < p1[0] && p2[0] < p3[0] || p2[0] < p4[0] && p2[0] < p3[0] || p2[0] < p1[0] && p2[0] < p4[0])
        {
            if (p2[1] < p1[1] && p2[1] < p3[1] || p2[1] < p4[1] && p2[1] < p3[1] || p2[1] < p1[1] && p2[1] < p4[1])
            {
                points.Add(new Point(p2[0], p2[1]));
                if (p1[1] < p3[1] && p1[1] < p4[1])
                {
                    points.Add(new Point(p1[0], p1[1]));
                    if (p3[0] < p4[0])
                    {
                        points.Add(new Point(p4[0], p4[1]));
                        points.Add(new Point(p3[0], p3[1]));
                    }
                    else
                    {
                        points.Add(new Point(p3[0], p3[1]));
                        points.Add(new Point(p4[0], p4[1]));
                    }
                }
                else if (p3[1] < p1[1] && p3[1] < p4[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    if (p1[0] < p4[0])
                    {
                        points.Add(new Point(p4[0], p4[1]));
                        points.Add(new Point(p1[0], p1[1]));
                    }
                    else
                    {
                        points.Add(new Point(p1[0], p1[1]));
                        points.Add(new Point(p4[0], p4[1]));
                    }
                }
                else if (p4[1] < p1[1] && p4[1] < p3[1])
                {
                    points.Add(new Point(p4[0], p4[1]));
                    if (p1[0] < p3[0])
                    {
                        points.Add(new Point(p3[0], p3[1]));
                        points.Add(new Point(p1[0], p1[1]));
                    }
                    else
                    {
                        points.Add(new Point(p1[0], p1[1]));
                        points.Add(new Point(p3[0], p3[1]));
                    }
                }
            }
            else if (p1[0] < p3[0] && p1[0] < p4[0])
            {
                points.Add(new Point(p1[0], p1[1]));
                if (p3[1] < p4[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    points.Add(new Point(p4[0], p4[1]));
                }
                else
                {
                    points.Add(new Point(p4[0], p4[1]));
                    points.Add(new Point(p3[0], p3[1]));
                }
                points.Add(new Point(p2[0], p2[1]));
            }
            else if (p3[0] < p1[0] && p3[0] < p4[0])
            {
                points.Add(new Point(p3[0], p3[1]));
                if (p1[1] < p4[1])
                {
                    points.Add(new Point(p1[0], p1[1]));
                    points.Add(new Point(p4[0], p4[1]));
                }
                else
                {
                    points.Add(new Point(p4[0], p4[1]));
                    points.Add(new Point(p1[0], p1[1]));
                }
                points.Add(new Point(p2[0], p2[1]));
            }
            else if (p4[0] < p3[0] && p4[0] < p1[0])
            {
                points.Add(new Point(p4[0], p4[1]));
                if (p3[1] < p1[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    points.Add(new Point(p1[0], p1[1]));
                }
                else
                {
                    points.Add(new Point(p1[0], p1[1]));
                    points.Add(new Point(p3[0], p3[1]));
                }
                points.Add(new Point(p2[0], p2[1]));
            }
        }
        else if (p3[0] < p2[0] && p3[0] < p1[0] || p3[0] < p4[0] && p3[0] < p1[0] || p3[0] < p2[0] && p3[0] < p4[0])
        {
            if (p3[1] < p2[1] && p3[1] < p1[1] || p3[1] < p4[1] && p3[1] < p1[1] || p3[1] < p2[1] && p3[1] < p4[1])
            {
                points.Add(new Point(p3[0], p3[1]));
                if (p2[1] < p1[1] && p2[1] < p4[1])
                {
                    points.Add(new Point(p2[0], p2[1]));
                    if (p1[0] < p4[0])
                    {
                        points.Add(new Point(p4[0], p4[1]));
                        points.Add(new Point(p1[0], p1[1]));
                    }
                    else
                    {
                        points.Add(new Point(p1[0], p1[1]));
                        points.Add(new Point(p4[0], p4[1]));
                    }
                }
                else if (p1[1] < p2[1] && p1[1] < p4[1])
                {
                    points.Add(new Point(p1[0], p1[1]));
                    if (p2[0] < p4[0])
                    {
                        points.Add(new Point(p4[0], p4[1]));
                        points.Add(new Point(p2[0], p2[1]));
                    }
                    else
                    {
                        points.Add(new Point(p2[0], p2[1]));
                        points.Add(new Point(p4[0], p4[1]));
                    }
                }
                else if (p4[1] < p2[1] && p4[1] < p1[1])
                {
                    points.Add(new Point(p4[0], p4[1]));
                    if (p2[0] < p1[0])
                    {
                        points.Add(new Point(p1[0], p1[1]));
                        points.Add(new Point(p2[0], p2[1]));
                    }
                    else
                    {
                        points.Add(new Point(p2[0], p2[1]));
                        points.Add(new Point(p1[0], p1[1]));
                    }
                }
            }
            else if (p2[0] < p1[0] && p2[0] < p4[0])
            {
                points.Add(new Point(p2[0], p2[1]));
                if (p1[1] < p4[1])
                {
                    points.Add(new Point(p1[0], p1[1]));
                    points.Add(new Point(p4[0], p4[1]));
                }
                else
                {
                    points.Add(new Point(p4[0], p4[1]));
                    points.Add(new Point(p1[0], p1[1]));
                }
                points.Add(new Point(p3[0], p3[1]));
            }
            else if (p1[0] < p2[0] && p1[0] < p4[0])
            {
                points.Add(new Point(p1[0], p1[1]));
                if (p2[1] < p4[1])
                {
                    points.Add(new Point(p2[0], p2[1]));
                    points.Add(new Point(p4[0], p4[1]));
                }
                else
                {
                    points.Add(new Point(p4[0], p4[1]));
                    points.Add(new Point(p2[0], p2[1]));
                }
                points.Add(new Point(p3[0], p3[1]));
            }
            else if (p4[0] < p1[0] && p4[0] < p2[0])
            {
                points.Add(new Point(p4[0], p4[1]));
                if (p1[1] < p2[1])
                {
                    points.Add(new Point(p1[0], p1[1]));
                    points.Add(new Point(p2[0], p2[1]));
                }
                else
                {
                    points.Add(new Point(p2[0], p2[1]));
                    points.Add(new Point(p1[0], p1[1]));
                }
                points.Add(new Point(p3[0], p3[1]));
            }
        }
        else if (p4[0] < p2[0] && p4[0] < p3[0] || p4[0] < p1[0] && p4[0] < p3[0] || p4[0] < p2[0] && p4[0] < p1[0])
        {
            if (p4[1] < p2[1] && p4[1] < p3[1] || p4[1] < p1[1] && p4[1] < p3[1] || p4[1] < p2[1] && p4[1] < p1[1])
            {
                points.Add(new Point(p4[0], p4[1]));
                if (p2[1] < p3[1] && p2[1] < p1[1])
                {
                    points.Add(new Point(p2[0], p2[1]));
                    if (p3[0] < p1[0])
                    {
                        points.Add(new Point(p1[0], p1[1]));
                        points.Add(new Point(p3[0], p3[1]));
                    }
                    else
                    {
                        points.Add(new Point(p3[0], p3[1]));
                        points.Add(new Point(p1[0], p1[1]));
                    }
                }
                else if (p3[1] < p2[1] && p3[1] < p1[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    if (p2[0] < p1[0])
                    {
                        points.Add(new Point(p1[0], p1[1]));
                        points.Add(new Point(p2[0], p2[1]));
                    }
                    else
                    {
                        points.Add(new Point(p2[0], p2[1]));
                        points.Add(new Point(p1[0], p1[1]));
                    }
                }
                else if (p1[1] < p2[1] && p1[1] < p3[1])
                {
                    points.Add(new Point(p1[0], p1[1]));
                    if (p2[0] < p3[0])
                    {
                        points.Add(new Point(p3[0], p3[1]));
                        points.Add(new Point(p2[0], p2[1]));
                    }
                    else
                    {
                        points.Add(new Point(p2[0], p2[1]));
                        points.Add(new Point(p3[0], p3[1]));
                    }
                }
            }
            else if (p2[0] < p3[0] && p2[0] < p1[0])
            {
                points.Add(new Point(p2[0], p2[1]));
                if (p3[1] < p1[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    points.Add(new Point(p1[0], p1[1]));
                }
                else
                {
                    points.Add(new Point(p1[0], p1[1]));
                    points.Add(new Point(p3[0], p3[1]));
                }
                points.Add(new Point(p4[0], p4[1]));
            }
            else if (p3[0] < p2[0] && p3[0] < p1[0])
            {
                points.Add(new Point(p3[0], p3[1]));
                if (p2[1] < p1[1])
                {
                    points.Add(new Point(p2[0], p2[1]));
                    points.Add(new Point(p1[0], p1[1]));
                }
                else
                {
                    points.Add(new Point(p1[0], p1[1]));
                    points.Add(new Point(p2[0], p2[1]));
                }
                points.Add(new Point(p4[0], p4[1]));
            }
            else if (p1[0] < p3[0] && p1[0] < p2[0])
            {
                points.Add(new Point(p1[0], p1[1]));
                if (p3[1] < p2[1])
                {
                    points.Add(new Point(p3[0], p3[1]));
                    points.Add(new Point(p2[0], p2[1]));
                }
                else
                {
                    points.Add(new Point(p2[0], p2[1]));
                    points.Add(new Point(p3[0], p3[1]));
                }
                points.Add(new Point(p4[0], p4[1]));
            }
        }
        // MatOfPoint2f pts2 = new MatOfPoint2f();
        fourPoints = new MatOfPoint2f();
        fourPoints.release();
        fourPoints.fromList(points);
    }