public Network(Mesh m, bool Branching = false) : this() { for (int i = 0; i < m.TopologyVertices.Count; ++i) { Node n; if (Branching && m.TopologyVertices.ConnectedTopologyVertices(i).Length == 3) { n = new BranchingNode(); } else if (m.TopologyVertices.ConnectedTopologyVertices(i).Length == 1) { n = new FootNode(); } else { n = new Node(); } MeshPoint mp = m.ClosestMeshPoint(m.TopologyVertices[i], MeshMaxDistance); n.Frame = new Plane(mp.Point, m.NormalAt(mp)); Nodes.Add(n); } for (int i = 0; i < m.TopologyEdges.Count; ++i) { Edge e = new Edge(); e.Ends = m.TopologyEdges.GetTopologyVertices(i); Edges.Add(e); } Edges = new HashSet <Edge>(Edges).ToList(); BuildNodeData(false, false); BuildBranchingData(); }
/// <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; } } } }
public static Network Load(string Path) { if (!System.IO.File.Exists(Path)) { throw new Exception("Network::Load: File does not exist!"); } XDocument doc = XDocument.Load(Path); if (doc == null) { throw new Exception("Network::Load: Failed to load file."); } XElement root = doc.Root; if (root.Name != "network") { throw new Exception("Network::Load: Not a valid Network file."); } Network net = new Network(); XElement nodes = root.Element("nodes"); if (nodes != null) { var node_list = nodes.Elements(); foreach (XElement node in node_list) { if (node.Name != "node") { continue; } Node n; string type = node.Attribute("type").Value; if (type == "FootNode") { n = new FootNode(); } else if (type == "BranchingNode") { n = new BranchingNode(); BranchingNode bn = n as BranchingNode; if (bn == null) { continue; } XElement branch_weights = node.Element("branch_weights"); if (branch_weights != null) { var branch_weight_list = branch_weights.Elements(); foreach (XElement bw in branch_weight_list) { if (bw.Name == "branch_weight") { bn.BranchWeights.Add(Convert.ToInt32(bw.Value)); } } } } else { n = new Node(); } XElement node_edges = node.Element("node_edges"); var edge_list = node_edges.Elements("node_edge"); foreach (XElement ne in edge_list) { double x = Convert.ToDouble(ne.Attribute("vX").Value); double y = Convert.ToDouble(ne.Attribute("vY").Value); double z = Convert.ToDouble(ne.Attribute("vZ").Value); n.Edges.Add(new NodeInterface(new Vector3d(x, y, z), Convert.ToInt32(ne.Attribute("index").Value))); } XElement frame = node.Element("frame"); double fx = Convert.ToDouble(frame.Attribute("PosX").Value); double fy = Convert.ToDouble(frame.Attribute("PosY").Value); double fz = Convert.ToDouble(frame.Attribute("PosZ").Value); double fxx = Convert.ToDouble(frame.Attribute("XX").Value); double fxy = Convert.ToDouble(frame.Attribute("XY").Value); double fxz = Convert.ToDouble(frame.Attribute("XZ").Value); double fyx = Convert.ToDouble(frame.Attribute("YX").Value); double fyy = Convert.ToDouble(frame.Attribute("YY").Value); double fyz = Convert.ToDouble(frame.Attribute("YZ").Value); n.Frame = new Plane(new Point3d(fx, fy, fz), new Vector3d(fxx, fxy, fxz), new Vector3d(fyx, fyy, fyz)); net.Nodes.Add(n); } } XElement edges = root.Element("edges"); if (edges != null) { var edge_list = edges.Elements("edge"); foreach (XElement edge in edge_list) { int i = Convert.ToInt32(edge.Attribute("end1").Value); int j = Convert.ToInt32(edge.Attribute("end2").Value); Edge e = new Edge(); e.Ends = new IndexPair(i, j); net.Edges.Add(e); } } return(net); }
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); }