Esempio n. 1
0
        public void TestBarycentric2DCoords()
        {
            var a = new Vector2(0, 0);
            var b = new Vector2(10, 0);
            var c = new Vector2(0, 10);

            //First coord should be large
            var coords = Intersections.Barycentric2DCoords(new Vector2(0.5f, 0.5f), a, b, c);

            Assert.That(coords.x, Is.GreaterThan(0.8));
            Assert.That(coords.x + coords.y + coords.z, Is.EqualTo(1));

            //Second coord should be large
            coords = Intersections.Barycentric2DCoords(new Vector2(9f, 0.5f), a, b, c);
            Assert.That(coords.y, Is.GreaterThan(0.8));
            Assert.That(coords.x + coords.y + coords.z, Is.EqualTo(1));

            //Third coord should be large
            coords = Intersections.Barycentric2DCoords(new Vector2(0.5f, 9f), a, b, c);
            Assert.That(coords.z, Is.GreaterThan(0.8));
            Assert.That(coords.x + coords.y + coords.z, Is.EqualTo(1));

            //Some manual case
            coords = Intersections.Barycentric2DCoords(new Vector2(94, 27), new Vector2(86, 34), new Vector2(119, 23), new Vector2(93, -2));
            //Assert.tha
        }
Esempio n. 2
0
        /*
         * /// <summary>
         * /// Test line-triangle intersection
         * /// </summary>
         * void OnDrawGizmos()
         * {
         *  if(!Application.isPlaying)
         *      return;
         *
         *  DrawPolyline.ForGizmo(new Vector3[] {_handle1.transform.position, _handle2.transform.position, _handle3.transform.position, _handle1.transform.position }, Color.white);
         *
         *  //var screenRay = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(Input.mousePosition);
         *  var screenRay = new Ray(Vector3.zero, Vector3.forward);
         *  Debug.DrawRay(screenRay.origin, screenRay.direction * 1000, Color.yellow);
         *  Vector3 intersectPos;
         *  var result = Intersections.LineTriangleIntersection(screenRay, _handle1.transform.position, _handle2.transform.position,
         *      _handle3.transform.position, out intersectPos);
         *
         *  if(result == 1)
         *  {
         *      Gizmos.color = Color.red;
         *      Gizmos.DrawSphere(intersectPos, 0.1f);
         *  }
         *  else
         *      Debug.Log(result);
         * }
         */

        /// <summary>
        /// Test barycentric interpolation
        /// </summary>
        void OnDrawGizmos()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            _handle1.transform.position = (Vector2)_handle1.transform.position;
            _handle2.transform.position = (Vector2)_handle2.transform.position;
            _handle3.transform.position = (Vector2)_handle3.transform.position;
            _handle4.transform.position = (Vector2)_handle4.transform.position;

            DrawPolyline.ForGizmo(new[]
            {
                _handle1.transform.position, _handle2.transform.position, _handle3.transform.position
            }, Color.white, true);

            Gizmos.color = Color.red;
            Gizmos.DrawSphere(_handle1.transform.position, 0.5f);
            Gizmos.color = Color.green;
            Gizmos.DrawSphere(_handle2.transform.position, 0.5f);
            Gizmos.color = Color.blue;
            Gizmos.DrawSphere(_handle3.transform.position, 0.5f);

            var result = Intersections.Barycentric2DCoords(_handle4.transform.position, _handle1.transform.position,
                                                           _handle2.transform.position, _handle3.transform.position);

            Debug.Log(result);

            var resultColor = new Color(result.x, result.y, result.z);

            Gizmos.color = resultColor;
            Gizmos.DrawSphere(_handle4.transform.position, 0.5f);
        }