예제 #1
0
    // Update is called once per frame
    void Update()
    {
        // new Vector2 はsource Textureでのピクセル位置


        float srcW = source.width;
        float srcH = source.height;


        // src texture uv position
        Vector2[] uvs = new Vector2[4];
        uvs[0] = new Vector2(P1.localPosition.x / 2, P1.localPosition.y / 1) / 10;
        uvs[1] = new Vector2(P2.localPosition.x / 2, P2.localPosition.y / 1) / 10;
        uvs[2] = new Vector2(P3.localPosition.x / 2, P3.localPosition.y / 1) / 10;
        uvs[3] = new Vector2(P4.localPosition.x / 2, P4.localPosition.y / 1) / 10;

        Color32[] colors = Homography.GetTransformedColors(source, dstWidth, dstHeight,
                                                           new Vector2(uvs[0].x * srcW, uvs[0].y * srcH),
                                                           new Vector2(uvs[1].x * srcW, uvs[1].y * srcH),
                                                           new Vector2(uvs[2].x * srcW, uvs[2].y * srcH),
                                                           new Vector2(uvs[3].x * srcW, uvs[3].y * srcH));
        if (target == null)
        {
            target = new Texture2D(dstWidth, dstHeight);
        }
        target.SetPixels32(colors, 0);
        target.Apply();

        targetRender.material.mainTexture = target;
    }
예제 #2
0
    private void Start()
    {
        double[,] hm = Homography.CalcHomographyMatrix(s, d);

        string log = "";

        for (int i = 0; i < hm.GetLength(0); i++)
        {
            for (int j = 0; j < hm.GetLength(1); j++)
            {
                log += hm[i, j] + " ";
            }
            log += "\n";
        }

        Debug.Log("Calculated Homography Matrix : \n\n" + log);

        // TEST
        double[,] xy = new double[3, 1] {
            { s[2, 0] }, { s[2, 1] }, { 1 }
        };
        Homography.CalcProjection(hm, xy, true);
        // TEST
        double[,] uv = new double[3, 1] {
            { d[1, 0] }, { d[1, 1] }, { 1 }
        };
        Homography.CalcInverseProjection(hm, uv, true);
    }
    private void CalculatePointMatchesAndErrors(double[,] hm, double[,] s, double[,] d, List <double[, ]> P, List <double[, ]> actualP)
    {
        string log = "";

        for (int i = 0; i < hm.GetLength(0); i++)
        {
            for (int j = 0; j < hm.GetLength(1); j++)
            {
                log += hm[i, j] + " ";
            }
            log += "\n";
        }

        Debug.Log("Calculated Homography Matrix : \n\n" + log);
        Debug.Log("Calculating Projection of points...");

        int k = 0;

        foreach (double[,] xy in P)
        {
            double[,] uv = actualP[k++];
            var res = Homography.CalcProjection(hm, xy, true);     // projection
            Debug.Log("Error : %" + CalcProjectionError(res, uv)); // error
        }
    }
예제 #4
0
        public void ComputeEqualization()
        {
            Vector2[] destination = ScaledEqualizationAnchors();

            Vector3[] homography = Homography.Find(Homography.Quad, destination);
            this.EqualizationMatrix = Projection.Bimber(homography);
        }
예제 #5
0
    public void ApplyProjection(HImage img)
    {
        Debug.Log("Applying Projection to image '" + img.name + "'");
        double[,] r  = ImgToDoubleArr(refImg);
        double[,] d  = ImgToDoubleArr(img);
        double[,] hm = Homography.CalcHomographyMatrix(r, d);


        Vector3 objPoint = augmentedObject.transform.localPosition;

        double[,] p = new double[, ] {
            { objPoint.x }, { objPoint.y }, { 1 }
        };
        double[,] uv = Homography.CalcProjection(hm, p, true);

        Transform newOrigin = new GameObject("NewOrigin").transform;

        newOrigin.SetParent(img.origin);


        //obj.localPosition = new Vector3((float)uv[0, 0], (float)uv[1, 0], 0);


        // project origin
        double[,] o = new double[3, 1] {
            { 0 }, { 0 }, { 1 }
        };
        double[,] or            = Homography.CalcProjection(hm, o, true);
        newOrigin.localPosition = new Vector3((float)or[0, 0], (float)or[1, 0], 0);

        // find angle
        Vector3 refDir  = refImg.origin.transform.position - objPoint;
        Vector3 projDir = newOrigin.localPosition - (new Vector3((float)uv[0, 0], (float)uv[1, 0], 0));

        print("refdir : " + refDir + " , projdir : " + projDir);

        //float zAngle = Vector2.SignedAngle(new Vector2(refDir.x, refDir.y), new Vector2(projDir.x, projDir.y));
        float zAngle = Vector2.SignedAngle(refDir, projDir);

        Debug.Log("Angle : " + zAngle);

        // distance between object and origin
        float dist1 = Mathf.Sqrt(Mathf.Pow((float)(p[0, 0]), 2) + Mathf.Pow((float)(p[1, 0]), 2));
        // distance between projected object and projected origin
        float dist2       = Mathf.Sqrt(Mathf.Pow((float)(uv[0, 0] - or[0, 0]), 2) + Mathf.Pow((float)(uv[1, 0] - or[1, 0]), 2));
        float scaleFactor = dist2 / dist1;

        Debug.Log("Scale Factor : " + scaleFactor);


        //newOrigin.localScale *= scaleFactor;
        //newOrigin.rotation = Quaternion.Euler(0, 0, zAngle);

        // projected object
        Transform obj = Instantiate(augmentedObject, img.origin);

        obj.localScale *= scaleFactor;
        obj.RotateAround(newOrigin.transform.position, Vector3.forward, zAngle);
        obj.localPosition = new Vector3((float)uv[0, 0], (float)uv[1, 0], 0);
    }
예제 #6
0
    public static double[,] CalcInverseProjection(double[,] hm, double[,] uv, bool log)
    {
        double[,] match = Homography.ApplyInverseHomography(hm, uv);
        if (log)
        {
            Debug.Log("Applying Inverse Homography...");
            Debug.Log("(u,v) : " + uv[0, 0] + " , " + uv[1, 0] + " , " + uv[2, 0]);
            Debug.Log("(x,y) : " + match[0, 0] + " , " + match[1, 0] + " , " + match[2, 0]);
        }

        return(match);
    }
예제 #7
0
    public static double[,] CalcProjection(double[,] hm, double[,] xy, bool log)
    {
        double[,] match = Homography.ApplyHomography(hm, xy);
        if (log)
        {
            Debug.Log("Applying Homography...");
            Debug.Log("(x,y) : " + xy[0, 0] + " , " + xy[1, 0] + " , " + xy[2, 0]);
            Debug.Log("(u,v) : " + match[0, 0] + " , " + match[1, 0] + " , " + match[2, 0]);
        }

        return(match);
    }
예제 #8
0
    public void FindHomographyTest3()
    {
        var stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();

        var srcList = new Point2 <float> [4];
        var dstList = new Point2 <float> [4];

        srcList[0] = new Point2 <float>(10, 10);
        srcList[1] = new(100, 10);
        srcList[2] = new(100, 150);
        srcList[3] = new(10, 150);

        dstList[0] = new Point2 <float>(11, 11);
        dstList[1] = new Point2 <float>(500, 11);
        dstList[2] = new Point2 <float>(500, 200);
        dstList[3] = new Point2 <float>(11, 200);

        var h**o = Homography.Find(srcList, dstList);

        stopWatch.Stop();
        Console.WriteLine($"=====test3 stop{stopWatch.ElapsedMilliseconds}=====");

        Console.WriteLine(h**o);

        {
            var result = h**o.Translate(100, 10);
            Assert.IsTrue(Math.Abs(result.X - 500) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 11) < 0.001);
        }

        {
            var result = h**o.Translate(100, 150);
            Assert.IsTrue(Math.Abs(result.X - 500) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 200) < 0.001);
        }


        {
            var   result = h**o.Translate((100 + 10) / 2.0f, (150 + 10) / 2.0f);
            float dstx   = (500 + 11) / 2.0f;
            float dsty   = (200 + 11) / 2.0f;
            Console.WriteLine("result.X" + result.X);
            Console.WriteLine("result.Y" + result.Y);

            Console.WriteLine("dstx" + dstx);
            Console.WriteLine("dsty" + dsty);

            Assert.IsTrue(Math.Abs(result.X - dstx) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - dsty) < 0.001);
        }
    }
예제 #9
0
    public void ChunkTestCamelCase()
    {
        var srcList = new List <Point2 <float> >(4);
        var dstList = new List <Point2 <float> >(4);

        srcList.Add(new Point2 <float>(-152, 394));
        srcList.Add(new Point2 <float>(218, 521));
        srcList.Add(new Point2 <float>(223, -331));
        srcList.Add(new Point2 <float>(-163, -219));

        dstList.Add(new Point2 <float>(-666, 431));
        dstList.Add(new Point2 <float>(500, 300));
        dstList.Add(new Point2 <float>(480, -308));
        dstList.Add(new Point2 <float>(-580, -280));

        var stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();

        var h**o = Homography.Find(srcList, dstList);

        var chunk = new Chunk()
        {
            Id = Guid.NewGuid(), Homography = h**o
        };

        string json = JsonSerializer.Serialize(chunk, new JsonSerializerOptions()
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        });

        Debug.WriteLine(json);

        var restore = JsonSerializer.Deserialize <Chunk>(json, new JsonSerializerOptions()
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        });

        var homo2 = restore?.Homography;

        if (homo2 is null)
        {
            Assert.IsNotNull(homo2);
        }

        Assert.IsTrue(h**o.Elements.Count == homo2?.Elements.Count);

        for (int i = 0; i < h**o.Elements.Count; i++)
        {
            Assert.IsTrue(Math.Abs(h**o.Elements[i] - homo2.Elements[i]) < 0.001);
        }
    }
예제 #10
0
    public void FindHomographyTest00()
    {
        var srcList = new List <Point2 <double> >(4);
        var dstList = new List <Point2 <double> >(4);

        srcList.Add(new Point2 <double>(-152, 394));
        srcList.Add(new Point2 <double>(218, 521));
        srcList.Add(new Point2 <double>(223, -331));
        srcList.Add(new Point2 <double>(-163, -219));

        dstList.Add(new Point2 <double>(-666, 431));
        dstList.Add(new Point2 <double>(500, 300));
        dstList.Add(new Point2 <double>(480, -308));
        dstList.Add(new Point2 <double>(-580, -280));

        var stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();

        var h**o = Homography.Find(srcList, dstList);

        Console.WriteLine($"=====test4 stop{stopWatch.ElapsedMilliseconds}=====");

        {
            var result = h**o.Translate(-152, 394);
            Assert.IsTrue(Math.Abs(result.X - -666) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 431) < 0.001);
        }

        {
            var result = h**o.Translate(218, 521);
            Assert.IsTrue(Math.Abs(result.X - 500) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 300) < 0.001);
        }

        {
            var result = h**o.Translate(223, -331);
            Assert.IsTrue(Math.Abs(result.X - 480) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - -308) < 0.001);
        }


        var mathNetMat = h**o.ToMathNetMatrix();

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Assert.IsTrue(Math.Abs(mathNetMat[i, j] - h**o[i, j]) < 0.001);
            }
        }
    }
예제 #11
0
    public void FindHomographyTest01()
    {
        var srcList = new List <Point2 <float> >(4);
        var dstList = new List <Point2 <float> >(4);

        srcList.Add(new Point2 <float>(-152, 394));
        srcList.Add(new(218, 521));
        srcList.Add(new(223, -331));
        srcList.Add(new(-163, -219));

        dstList.Add(new Point2 <float>(-666, 431));
        dstList.Add(new Point2 <float>(500, 300));
        dstList.Add(new Point2 <float>(480, -308));
        dstList.Add(new Point2 <float>(-580, -280));

        var srcArray = srcList.ToArray();
        var dstArray = dstList.ToArray();

        var stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();

        var h**o = Homography.Find(srcArray, dstArray);

        Console.WriteLine($"=====test4 stop{stopWatch.ElapsedMilliseconds}=====");

        {
            var result = h**o.Translate(-152, 394);
            Assert.IsTrue(Math.Abs(result.X - -666) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 431) < 0.001);
        }

        {
            var result = h**o.Translate(218, 521);
            Assert.IsTrue(Math.Abs(result.X - 500) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 300) < 0.001);
        }

        {
            var result = h**o.Translate(223, -331);
            Assert.IsTrue(Math.Abs(result.X - 480) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - -308) < 0.001);
        }

        Console.WriteLine(h**o);
    }
예제 #12
0
    void OnRenderObject()
    {
        Vector3[] pts = Homography.screenPointsToWorld(cam, _userScreenPoints, 5.0f);
        _mesh.vertices = pts;

        // for( int i = 0; i < 4; i++ ){
        //  Vector3 pnt = new Vector3( _userScreenPoints[i].x, _userScreenPoints[i].y, cam.nearClipPlane );
        //  _mesh.vertices[i] = cam.ScreenToWorldPoint( pnt );
        // }

        Matrix4x4 wtc          = cam.worldToCameraMatrix;
        Matrix4x4 proj         = cam.projectionMatrix;
        Matrix4x4 adjustedProj = GL.GetGPUProjectionMatrix(proj, true);

        _material.SetMatrix("_Matrix", adjustedProj * wtc);

        _material.SetPass(0);

        Graphics.DrawMeshNow(_mesh, Matrix4x4.zero);
    }
예제 #13
0
    public MessageParserResult ParseOscMessages(List <OSCMessage> oscMessages, Homography homography)
    {
        // Clear updated marker list, containing updates have been processed in prev frame
        _result.updatedMarkerList.Clear();

        foreach (OSCMessage msg in oscMessages)
        {
            int     eventType     = int.Parse(msg.Data[0].ToString());
            int     uniqueId      = int.Parse(msg.Data[1].ToString());
            int     sessionId     = int.Parse(msg.Data[2].ToString());
            Vector2 pixelPosition = new Vector2(int.Parse(msg.Data[3].ToString()), int.Parse(msg.Data[4].ToString()));
            Vector2 worldPosition = homography.GetWorldPosition(pixelPosition);

            GamePiece newDice = new GamePiece(sessionId, uniqueId, pixelPosition, worldPosition, 0f);

            HandleEvent(eventType, newDice);
        }

        return(_result);
    }
예제 #14
0
    public void Single()
    {
        var srcList = new List <Point2 <float> >(4);
        var dstList = new List <Point2 <float> >(4);

        srcList.Add(new Point2 <float>(-152, 394));
        srcList.Add(new Point2 <float>(218, 521));
        srcList.Add(new Point2 <float>(223, -331));
        srcList.Add(new Point2 <float>(-163, -219));

        dstList.Add(new Point2 <float>(-666, 431));
        dstList.Add(new Point2 <float>(500, 300));
        dstList.Add(new Point2 <float>(480, -308));
        dstList.Add(new Point2 <float>(-580, -280));

        var stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();

        var h**o = Homography.Find(srcList, dstList);

        var json = JsonSerializer.Serialize(h**o);

        try
        {
            var homo2 = JsonSerializer.Deserialize <HomographyMatrix <float> >(json);

            Assert.IsTrue(h**o.Elements.Count == homo2?.Elements.Count);

            for (int i = 0; i < h**o.Elements.Count; i++)
            {
                Assert.IsTrue(Math.Abs(h**o.Elements[i] - homo2.Elements[i]) < 0.001);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Assert.IsTrue(false);
            throw;
        }
    }
예제 #15
0
    public void UpdateHomography()
    {
        if (HomographyPoints.Length >= 4)
        {
            // Vector3[] userPoints = new Vector3[4];
            for (int i = 0; i < 4; i++)
            {
                userPoints[i] = new Vector3(
                    HomographyPoints[i].localPosition.x / HomographySpace.rect.width,
                    HomographyPoints[i].localPosition.y / HomographySpace.rect.height,
                    0);
            }
        }

        Vector3[] desiredPoints = new Vector3[4] {
            new Vector3(0, 0, 0),
            new Vector3(1, 0, 0),
            new Vector3(1, 1, 0),
            new Vector3(0, 1, 0)
        };

        HomographyMatrix = Homography.CalcHomographyMatrix(ref userPoints);
        HomographyMaterial.SetFloatArray("_InvHomography", HomographyMatrix);

        HomographyMaterial.SetFloat("_MirrorX", mirrorX ? 2.0f : 1.0f);
        HomographyMaterial.SetFloat("_MirrorY", mirrorY ? 2.0f : 1.0f);

        // PRUEBA PABLO

        /*
         * var newAspect  = Vector3.Distance(userPoints[0],userPoints[2]) / Vector3.Distance(userPoints[1],userPoints[3]);
         * Debug.Log(aspect);
         * if (GetComponent<AspectRatioFitter>())
         * {
         *  GetComponent<AspectRatioFitter>().aspectRatio = newAspect;
         *  GetComponent<AspectRatioFitter>().aspectMode = (newAspect > 1f)
         *      ? AspectRatioFitter.AspectMode.WidthControlsHeight
         *      : AspectRatioFitter.AspectMode.HeightControlsWidth;
         * }
         */
    }
예제 #16
0
    public void Bench2()
    {
        var srcList = new List <Point2 <double> >(4);
        var dstList = new List <Point2 <double> >(4);

        srcList.Add(new Point2 <double>(-152, 394));
        srcList.Add(new Point2 <double>(218, 521));
        srcList.Add(new Point2 <double>(223, -331));
        srcList.Add(new Point2 <double>(-163, -219));

        dstList.Add(new Point2 <double>(-666, 431));
        dstList.Add(new Point2 <double>(500, 300));
        dstList.Add(new Point2 <double>(480, -308));
        dstList.Add(new Point2 <double>(-580, -280));
        var h**o = Homography.Find(srcList, dstList);

        for (int i = 0; i < 100000; i++)
        {
            var result = h**o.Translate(-152, 394);
        }
    }
예제 #17
0
    public void Copy3()
    {
        var srcList = new List <Point2 <double> >(4);
        var dstList = new List <Point2 <double> >(4);

        srcList.Add(new Point2 <double>(-152, 394));
        srcList.Add(new Point2 <double>(218, 521));
        srcList.Add(new Point2 <double>(223, -331));
        srcList.Add(new Point2 <double>(-163, -219));
        dstList.Add(new Point2 <double>(-666, 431));
        dstList.Add(new Point2 <double>(500, 300));
        dstList.Add(new Point2 <double>(480, -308));
        dstList.Add(new Point2 <double>(-580, -280));

        var h**o = Homography.Find(srcList, dstList);

        var homo2 = Homography.Create(h**o.Elements);

        for (int i = 0; i < h**o.Elements.Count; i++)
        {
            Assert.IsTrue(Math.Abs(h**o.Elements[i] - homo2.Elements[i]) < 0.001);
        }
    }
예제 #18
0
    private void Awake()
    {
        // make only one instance of AtmoTracker
        if (Instance == null)
        {
            Instance = this;

            DontDestroyOnLoad(gameObject);

            _sessionIdList = new List <int>();
            _markerDict    = new Dictionary <int, GamePiece>();

            Homography        = new Homography();
            _messageParser    = new AtmoTuioMessageParser();
            _oscControl       = new OSCControl(_messageParser.Port);
            _atmoEventHandler = new AtmoEventHandler(_sessionIdList, _markerDict, TriggerAtmoEvent);
        }

        // destroy this gameObject if it's not the first instance
        else if (Instance != this)
        {
            Destroy(gameObject);
        }
    }
예제 #19
0
    public MessageParserResult ParseOscMessages(List <OSCMessage> oscMessages, Homography homography)
    {
        MessageParserResult result = new MessageParserResult();
        bool noOrUnknownMessage    = true;

        // parse messages in bundle
        foreach (OSCMessage msg in oscMessages)
        {
            string messageType = msg.Data[0].ToString();

            if (msg.Address != "/tile")
            {
                int i = 0;
                continue;
            }
            noOrUnknownMessage = false;

            switch (messageType)
            {
            case "alive":
                msg.Data.RemoveAt(0);
                result.sessionIdList = msg.Data.OfType <int>().ToList();
                break;

            case "set":
                string gamePieceType = msg.Data[1].ToString();

                int uniqueId = (int)msg.Data[2];
                // sessionId is the same as uniqueId for now
                int sessionId = uniqueId;

                int side = (int)msg.Data[3];

                // transform raw to world position
                Vector2 rawPosition   = new Vector2((float)msg.Data[4], (float)msg.Data[5]);
                Vector2 worldPosition = homography.GetWorldPosition(rawPosition);

                // 0-1
                float width = (float)msg.Data[6];
                float angle = (float)msg.Data[7] * 360;

                GamePiece gamePiece = new GamePiece(sessionId, uniqueId, rawPosition, worldPosition, angle, width, side, gamePieceType);

                // check if it's the first or repeated SET of the same sessionId based on that add or edit it in the dictinoray
                if (result.updatedMarkerList.ContainsKey(sessionId))
                {
                    result.updatedMarkerList[sessionId] = gamePiece;
                }
                else
                {
                    result.updatedMarkerList.Add(sessionId, gamePiece);
                }

                break;

            case "fseq":
                break;
            }
        }

        if (noOrUnknownMessage)
        {
            result.sessionIdList = _prevSessionIdList;
        }

        // save sessionIdList
        _prevSessionIdList = result.sessionIdList;

        return(result);
    }
예제 #20
0
    void replacePoint(Vector2d add)
    {
        if (currentPoint == null)
        {
            return;
        }

        curPointPos = new Vector3d(curPointPos.x + add.x, curPointPos.y + add.y, curPointPos.z);
        updateCurPoint();

        switch (BlendWarpManager.warpingMode)
        {
        case BlendWarpManager.WarpingMode.CornersRowCol_Move:
        {
            Vector2d[] srcCorners = new Vector2d[4];
            srcCorners[0] = getPointFromGrid(0, 0);
            srcCorners[1] = getPointFromGrid(rowCounts - 1, colCounts - 1);
            srcCorners[2] = getPointFromGrid(rowCounts - 1, 0);
            srcCorners[3] = getPointFromGrid(0, colCounts - 1);

            setPointOnGrid(curRow, curCol, curPointPos);

            Vector2d[] dstCorners = new Vector2d[4];
            dstCorners[0] = getPointFromGrid(0, 0);
            dstCorners[1] = getPointFromGrid(rowCounts - 1, colCounts - 1);
            dstCorners[2] = getPointFromGrid(rowCounts - 1, 0);
            dstCorners[3] = getPointFromGrid(0, colCounts - 1);

            //lineRenderers[curRow].SetPosition(curCol, currentPoint.transform.position);
            //  lineRenderers[rowCounts + curCol].SetPosition(curRow, currentPoint.transform.position);



            //gantolebis amoxsna srcCorners gadadis dstCorners wveroebshi
            double[] matrix = new double[16];
            Homography.FindHomography(ref srcCorners, ref dstCorners, ref matrix);

            for (int p = 0; p < rowCounts; p++)
            {
                for (int q = 0; q < colCounts; q++)
                {
                    if ((p == 0 && q == 0) || (p == 0 && q == colCounts - 1) || (p == rowCounts - 1 && q == 0) || (p == rowCounts - 1 && q == colCounts - 1))
                    {
                        continue;
                    }
                    Vector2d initialPos = getPointFromGrid(p, q);
                    Vector2d posi       = BlendWarp_Functions.TransformedPoint(initialPos, matrix);
                    setPointOnGrid(p, q, posi);
                }
            }
        }
        break;

        case BlendWarpManager.WarpingMode.Row_Move:
        {
            for (int i = 0; i < colCounts; i++)
            {
                Vector2d posi = getPointFromGrid(curRow, i);
                posi = new Vector2d((posi.x + add.x), (posi.y + add.y));
                setPointOnGrid(curRow, i, posi);
            }
        }
        break;

        case BlendWarpManager.WarpingMode.Col_Move:
        {
            for (int i = 0; i < rowCounts; i++)
            {
                Vector2d posi = getPointFromGrid(i, curCol);
                posi = new Vector2d((posi.x + add.x), (posi.y + add.y));
                setPointOnGrid(i, curCol, posi);
            }
        }
        break;

        case BlendWarpManager.WarpingMode.SinglePoint_Move:
        {
            Vector2d posi = getPointFromGrid(curRow, curCol);
            posi = new Vector2d((posi.x + add.x), (posi.y + add.y));
            setPointOnGrid(curRow, curCol, posi);
        }
        break;
        }

        updateMesh();
        updateGrid();
    }
    //  1.4, 1.5, 1.6
    private void Start()
    {
        //  1.4
        ///////////////////////////////////////////////////////////////////
        List <double[, ]> P  = new List <double[, ]>(); // points (x, y)
        List <double[, ]> aP = new List <double[, ]>(); // actual point correspondences (u, v)

        double[,] hm1 = Homography.CalcHomographyMatrix(s, d1);
        double[,] hm2 = Homography.CalcHomographyMatrix(s, d2);
        double[,] hm3 = Homography.CalcHomographyMatrix(s, d3);

        P.Add(new double[3, 1] {
            { 900 }, { 100 }, { 1 }
        });
        P.Add(new double[3, 1] {
            { 800 }, { 300 }, { 1 }
        });
        P.Add(new double[3, 1] {
            { 700 }, { 400 }, { 1 }
        });

        /// Image 1
        Debug.Log("Projecting Image 1");
        aP.Add(new double[3, 1] {
            { 2612 }, { 681 }, { 1 }
        });
        aP.Add(new double[3, 1] {
            { 2379 }, { 1151 }, { 1 }
        });
        aP.Add(new double[3, 1] {
            { 2145 }, { 1381 }, { 1 }
        });

        CalculatePointMatchesAndErrors(hm1, s, d1, P, aP);

        /// Image 2
        Debug.Log("Projecting Image 2");
        aP = new List <double[, ]>(); // actual point correspondences (u, v)

        aP.Add(new double[3, 1] {
            { 2612 }, { 476 }, { 1 }
        });
        aP.Add(new double[3, 1] {
            { 2396 }, { 948 }, { 1 }
        });
        aP.Add(new double[3, 1] {
            { 2160 }, { 988 }, { 1 }
        });

        CalculatePointMatchesAndErrors(hm2, s, d2, P, aP);


        /// Image 3
        Debug.Log("Projecting Image 3");
        aP = new List <double[, ]>(); // actual point correspondences (u, v)

        aP.Add(new double[3, 1] {
            { 2664 }, { 712 }, { 1 }
        });
        aP.Add(new double[3, 1] {
            { 2444 }, { 1152 }, { 1 }
        });
        aP.Add(new double[3, 1] {
            { 2240 }, { 1376 }, { 1 }
        });

        CalculatePointMatchesAndErrors(hm3, s, d3, P, aP);

        //  1.5
        /////////////////////////////////////////////////////////////////
        double[,] p1 = new double[3, 1] {
            { 7.5f }, { 5.5f }, { 1 }
        };
        double[,] p2 = new double[3, 1] {
            { 6.3f }, { 3.3f }, { 1 }
        };
        double[,] p3 = new double[3, 1] {
            { 0.1f }, { 0.1f }, { 1 }
        };

        Debug.Log("Projection For Image 1");
        Homography.CalcProjection(hm1, p1, true);
        Homography.CalcProjection(hm1, p2, true);
        Homography.CalcProjection(hm1, p3, true);
        Debug.Log("Projection For Image 2");
        Homography.CalcProjection(hm2, p1, true);
        Homography.CalcProjection(hm2, p2, true);
        Homography.CalcProjection(hm2, p3, true);
        Debug.Log("Projection For Image 3");
        Homography.CalcProjection(hm3, p1, true);
        Homography.CalcProjection(hm3, p2, true);
        Homography.CalcProjection(hm3, p3, true);

        //  1.6
        /////////////////////////////////////////////////////////////////
        double[,] i1 = new double[3, 1] {
            { 500 }, { 400 }, { 1 }
        };
        double[,] i2 = new double[3, 1] {
            { 86 }, { 167 }, { 1 }
        };
        double[,] i3 = new double[3, 1] {
            { 10 }, { 10 }, { 1 }
        };

        Debug.Log("Inverse Projection For Image 1");
        Homography.CalcInverseProjection(hm1, i1, true);
        Homography.CalcInverseProjection(hm1, i2, true);
        Homography.CalcInverseProjection(hm1, i3, true);

        Debug.Log("Inverse Projection For Image 2");
        Homography.CalcInverseProjection(hm2, i1, true);
        Homography.CalcInverseProjection(hm2, i2, true);
        Homography.CalcInverseProjection(hm2, i3, true);

        Debug.Log("Inverse Projection For Image 3");
        Homography.CalcInverseProjection(hm3, i1, true);
        Homography.CalcInverseProjection(hm3, i2, true);
        Homography.CalcInverseProjection(hm3, i3, true);
    }
    public MessageParserResult ParseOscMessages(List <OSCMessage> oscMessages, Homography homography)
    {
        MessageParserResult result  = new MessageParserResult();
        bool thereWasNo2DobjMessage = true;

        // parse messages in bundle
        foreach (OSCMessage msg in oscMessages)
        {
            string messageType = msg.Data[0].ToString();

            if (msg.Address != "/tuio/2Dobj")
            {
                continue;
            }
            thereWasNo2DobjMessage = false;

            switch (messageType)
            {
            case "alive":
                msg.Data.RemoveAt(0);
                result.sessionIdList = msg.Data.OfType <int>().ToList();
                break;

            case "set":

                int     sessionId         = (int)msg.Data[1];
                int     uniqueId          = (int)msg.Data[2];
                Vector2 flattenedPosition = new Vector2((float)msg.Data[3], (float)msg.Data[4]);
                Vector2 worldPosition     = homography.GetWorldPosition(flattenedPosition);
                float   angle             = -Mathf.Rad2Deg * (float)msg.Data[5];

                GamePiece marker = new GamePiece(sessionId, uniqueId, flattenedPosition, worldPosition, angle);

                // check if its the first or repeated SET of the same sessionId based on that add or edit it in the dictinoray
                if (result.updatedMarkerList.ContainsKey(sessionId))
                {
                    result.updatedMarkerList[sessionId] = marker;
                }
                else
                {
                    result.updatedMarkerList.Add(sessionId, marker);
                }

                break;

            case "fseq":
                break;
            }
        }

        // Check if there were 2DObject messages and not just unused ones
        // If there were no 2DObject, return prev_sessionIdList as we donát have better info
        if (thereWasNo2DobjMessage)
        {
            result.sessionIdList = _prevSessionIdList;
        }

        _prevSessionIdList = result.sessionIdList;

        return(result);
    }
예제 #23
0
    public void FindHomographyTest00()
    {
        var srcList = new List <Point2 <float> >(4);
        var dstList = new List <Point2 <float> >(4);

        srcList.Add(new Point2 <float>(-152, 394));
        srcList.Add(new Point2 <float>(218, 521));
        srcList.Add(new Point2 <float>(223, -331));
        srcList.Add(new Point2 <float>(-163, -219));

        dstList.Add(new Point2 <float>(-666, 431));
        dstList.Add(new Point2 <float>(500, 300));
        dstList.Add(new Point2 <float>(480, -308));
        dstList.Add(new Point2 <float>(-580, -280));

        var stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();

        var h**o = Homography.Find(srcList, dstList);

        var json = JsonSerializer.Serialize(h**o);

        try
        {
            var homo2 = JsonSerializer.Deserialize <HomographyMatrix <float> >(json);

            Assert.IsTrue(h**o.Elements.Count == homo2?.Elements.Count);

            for (int i = 0; i < h**o.Elements.Count; i++)
            {
                Assert.IsTrue(Math.Abs(h**o.Elements[i] - homo2.Elements[i]) < 0.001);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

        Console.WriteLine($"=====test4 stop{stopWatch.ElapsedMilliseconds}=====");

        {
            var result = h**o.Translate(-152, 394);
            Assert.IsTrue(Math.Abs(result.X - -666) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 431) < 0.001);
        }

        {
            var result = h**o.Translate(218, 521);
            Assert.IsTrue(Math.Abs(result.X - 500) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - 300) < 0.001);
        }

        {
            var result = h**o.Translate(223, -331);
            Assert.IsTrue(Math.Abs(result.X - 480) < 0.001);
            Assert.IsTrue(Math.Abs(result.Y - -308) < 0.001);
        }

        Console.WriteLine(h**o);
    }