コード例 #1
0
        private void AddEdge(int i, Vector3d v)
        {
            v.Unitize();
            NodeInterface ni = new NodeInterface(v, i);

            Edges.Add(ni);
        }
コード例 #2
0
        /// <summary>
        /// Build node data, including edge connectivity, edge vectors, etc.
        /// </summary>
        /// <param name="CalcFrames">Calculate node frames based on the average normal of edge vectors.</param>
        /// <param name="ProjectVectors">Project edge vectors onto the node frame (flatten).</param>
        public void BuildNodeData(bool CalcFrames = false, bool ProjectVectors = false)
        {
            for (int i = 0; i < Nodes.Count; ++i)
            {
                Nodes[i].Edges.Clear();
            }

            for (int e = 0; e < Edges.Count; ++e)
            {
                int ei = Edges[e].Ends.I;
                int ej = Edges[e].Ends.J;

                Vector3d v = new Vector3d(Nodes[ei].Frame.Origin - Nodes[ej].Frame.Origin);
                v.Unitize();

                NodeInterface niI = new NodeInterface(-v, e);
                Nodes[ei].Edges.Add(niI);

                NodeInterface niJ = new NodeInterface(v, e);
                Nodes[ej].Edges.Add(niJ);
            }

            // Calculate foot / end nodes
            for (int i = 0; i < Nodes.Count; ++i)
            {
                if (Nodes[i].Edges.Count == 1)
                {
                    FootNode fn = new FootNode();
                    fn.Frame  = Nodes[i].Frame;
                    fn.Edges  = Nodes[i].Edges;
                    fn.Chains = Nodes[i].Chains;
                    Nodes[i]  = fn;
                }
            }

            if (CalcFrames)
            {
                foreach (Node n in Nodes)
                {
                    n.CalculateAverageFrame();
                }
            }

            if (ProjectVectors)
            {
                foreach (Node n in Nodes)
                {
                    if (n is FootNode)
                    {
                        continue;
                    }
                    n.ProjectEdgeVectors();
                }
            }

            if (EnforceContinuityInPairs)
            {
                foreach (Node n in Nodes)
                {
                    if (n.Edges.Count == 2)
                    {
                        Vector3d av = n.Edges[0].Vector - n.Edges[1].Vector;
                        av.Unitize();

                        n.Edges[0].Vector = av;
                        n.Edges[1].Vector = -av;
                    }
                }
            }
        }
コード例 #3
0
        public static Node Read(XElement elem)
        {
            Node n;

            if (elem.Name != "node")
            {
                throw new Exception("XElement is not a valid Node!");
            }

            XAttribute attr;

            Guid id;

            attr = elem.Attribute("id");
            if (attr != null)
            {
                id = Guid.Parse(attr.Value);
            }

            attr = elem.Attribute("type");
            if (attr == null)
            {
                throw new Exception("XElement does not contain a node type!");
            }
            string type = attr.Value;

            if (type == "FootNode")
            {
                n = new FootNode();
            }
            else if (type == "BranchingNode")
            {
                n = new BranchingNode();
            }
            else
            {
                n = new Node();
            }

            XElement nedges = elem.Element("node_edges");

            XElement[] edges = nedges.Elements("node_edge").ToArray();

            for (int i = 0; i < edges.Length; ++i)
            {
                attr = edges[i].Attribute("index");
                if (attr == null)
                {
                    throw new Exception("NodeInterface needs an index!");
                }

                int    index = Convert.ToInt32(attr.Value);
                double vx, vy, vz;

                attr = edges[i].Attribute("vX");
                if (attr == null)
                {
                    throw new Exception("NodeInterface is missing part of its vector!");
                }
                vx = Convert.ToDouble(attr.Value);

                attr = edges[i].Attribute("vY");
                if (attr == null)
                {
                    throw new Exception("NodeInterface is missing part of its vector!");
                }
                vy = Convert.ToDouble(attr.Value);

                attr = edges[i].Attribute("vZ");
                if (attr == null)
                {
                    throw new Exception("NodeInterface is missing part of its vector!");
                }
                vz = Convert.ToDouble(attr.Value);

                int w;
                attr = edges[i].Attribute("weight");
                if (attr == null)
                {
                    throw new Exception("NodeInterface is missing part of its vector!");
                }
                w = Convert.ToInt32(attr.Value);

                int ud = 0;
                attr = edges[i].Attribute("user_data");
                if (attr != null)
                {
                    ud = Convert.ToInt32(attr.Value);
                }

                NodeInterface ni = new NodeInterface(new Vector3d(vx, vy, vz), index);
                ni.Weight   = w;
                ni.UserData = ud;

                n.Edges.Add(ni);
            }

            if (n is BranchingNode)
            {
                BranchingNode bn       = n as BranchingNode;
                XElement      bweights = elem.Element("branch_weights");
                if (bweights != null)
                {
                    XElement[] bw_list = bweights.Elements("branch_weight").ToArray();
                    for (int i = 0; i < bw_list.Length; ++i)
                    {
                        bn.BranchWeights.Add(Convert.ToInt32(bw_list[i].Value));
                    }
                }
                n = bn;
            }

            return(n);
        }