public static JointParameters ComputeEdgeParameters(AnnotatedNode from, AnnotatedNode to)
        {
            var result = new JointParameters();

            var dirNext = to.Position - from.Position;

            result.Length = dirNext.Normalize();

            var dotFrom = Math.Abs(Vector3.Normalize(from.Tangent).Dot((Vector3)dirNext));
            var dotTo   = Math.Abs(Vector3.Normalize(to.Tangent).Dot((Vector3)dirNext));

            result.BendRadians = Math.Acos(Math.Min(dotFrom, dotTo));

            var planet = MyGamePruningStructureSandbox.GetClosestPlanet(@from.Position);

            // ReSharper disable once InvertIf
            if (planet?.PositionComp != null)
            {
                var center         = planet.PositionComp.WorldVolume.Center;
                var elevationA     = Vector3D.Distance(from.Position, center);
                var elevationB     = Vector3D.Distance(to.Position, center);
                var deltaElevation = elevationB - elevationA;
                var grade          = deltaElevation / result.Length;
                result.Grade = grade;
            }

            return(result);
        }
示例#2
0
        public PythonAnnotatedNodeWrapper(AnnotatedNode node)
        {
            _node = node;

            data = new PythonDictionary();
            JObject jdata = node.Data;

            foreach (var item in jdata.Properties())
            {
                data[item.Name] = item.Value.ToString();
            }
        }
        /// <summary>
        /// Verifies the given joint against the given definition's constraints.
        /// </summary>
        /// <param name="def">Constraint source</param>
        /// <param name="from">Point one of the edge</param>
        /// <param name="to">Point two of the edge</param>
        /// <param name="errors">destination for all errors that occurred, or null</param>
        /// <returns>true if the joint is valid</returns>
        public static bool VerifyEdge(BendyComponentDefinition def, AnnotatedNode from, AnnotatedNode to, IList <string> errors)
        {
            var jointData = ComputeEdgeParameters(from, to);

            if (jointData.Length > def.Distance.Max)
            {
                if (errors == null)
                {
                    return(false);
                }
                errors.Add($"Too long {jointData.Length:F1} m >= {def.Distance.Max:F1} m");
            }
            else if (jointData.Length < def.Distance.Min)
            {
                if (errors == null)
                {
                    return(false);
                }
                errors.Add($"Too short {jointData.Length:F1} m <= {def.Distance.Min:F1} m");
            }

            if (jointData.BendRadians.HasValue)
            {
                var angle = jointData.BendRadians.Value;
                if (angle > def.MaxAngleRadians)
                {
                    if (errors == null)
                    {
                        return(false);
                    }
                    errors.Add($"Too curvy {angle * 180 / Math.PI:F0}º >= {def.MaxAngleDegrees:F0}º");
                }
            }

            // ReSharper disable once InvertIf
            if (jointData.Grade.HasValue)
            {
                var grade = jointData.Grade.Value;
                // ReSharper disable once InvertIf
                if (Math.Abs(grade) > def.MaxGradeRatio)
                {
                    if (errors == null)
                    {
                        return(false);
                    }
                    errors.Add($"Too steep {grade * 100:F0}% {(grade < 0 ? "<= -" : ">= ")}{def.MaxGradeRatio * 100:F0}%");
                }
            }

            return(errors == null || errors.Count == 0);
        }
        public static AnnotatedNode[] AnnotateNodes(BendyLayer layer, Vector3D[] nodes)
        {
            var res = new AnnotatedNode[nodes.Length];

            for (var i = 0; i < nodes.Length; i++)
            {
                res[i] = new AnnotatedNode {
                    Position = nodes[i]
                }
            }
            ;
            AnnotateNodes(layer, res);
            return(res);
        }
示例#5
0
        // https://leetcode.com/problems/maximum-width-of-binary-tree/solution/
        public int WidthOfBinaryTree(TreeNode root)
        {
            Queue <AnnotatedNode> queue    = new Queue <AnnotatedNode>();
            AnnotatedNode         tempNode = null;
            int count  = 0,
                left   = 0,
                right  = 0,
                result = 0;

            queue.Enqueue(new AnnotatedNode(root, 1));

            while (queue.Count != 0)
            {
                count = queue.Count;
                left  = queue.Peek().position;

                while (count-- > 0)
                {
                    tempNode = queue.Dequeue();

                    if (tempNode.node.left != null)
                    {
                        queue.Enqueue(new AnnotatedNode(tempNode.node.left, tempNode.position * 2));
                    }

                    if (tempNode.node.right != null)
                    {
                        queue.Enqueue(new AnnotatedNode(tempNode.node.right, tempNode.position * 2 + 1));
                    }

                    if (count == 0)
                    {
                        right = tempNode.position;
                    }
                }

                result = result > right - left + 1 ? result : right - left + 1;
            }

            return(result);
        }