Пример #1
0
        public object Clone()
        {
            Trajectory t = (Trajectory)this.MemberwiseClone();
            Dictionary <string, string> keyRelationship = new Dictionary <string, string>();
            string initialId = initial.getID();

            t.initial = (initial != null ? (Node)initial.Clone() : null);
            keyRelationship.Add(initialId, t.initial.getID());
            if (nodes != null)
            {
                t.nodes = new List <Node>();
                foreach (Node n in nodes)
                {
                    if (n.getID().Equals(initialId))
                    {
                        t.nodes.Add(t.initial);
                    }
                    else
                    {
                        string oldId = n.getID();
                        //node clone generates a new Id
                        Node newNode = (Node)n.Clone();
                        t.nodes.Add(newNode);
                        keyRelationship.Add(oldId, newNode.getID());
                    }
                }
            }
            if (sides != null)
            {
                t.sides = new List <Side>();
                foreach (Side s in sides)
                {
                    //Side clone does not generate a new Id
                    Side newSide = new Side(keyRelationship[s.getIDStart()], keyRelationship[s.getIDEnd()]);
                    newSide.setLenght(s.getLength());
                    newSide.setRealLength(s.getRealLength());
                    t.sides.Add(newSide);
                }
            }
            return(t);
        }
        /**
         * Checks consistency between nodes and references in sides. If a node is referenced to but does not exist, it is created on position 400,300.
         * @param scene
         */
        private static void fixMissingNodes(Scene scene)
        {
            Trajectory trajectory = scene.getTrajectory();

            if (trajectory != null)
            {
                List <string> nodeIds = new List <string>();
                // Put all node ids referenced in sides in a single array list
                foreach (Trajectory.Side side in trajectory.getSides())
                {
                    if (!nodeIds.Contains(side.getIDStart()))
                    {
                        nodeIds.Add(side.getIDStart());
                    }
                    if (!nodeIds.Contains(side.getIDEnd()))
                    {
                        nodeIds.Add(side.getIDEnd());
                    }
                }

                // Remove all ids belonging to an existing nodes. The result is a list containing the strings of
                // all missing nodes
                foreach (Trajectory.Node node in trajectory.getNodes())
                {
                    if (nodeIds.Contains(node.getID()))
                    {
                        nodeIds.Remove(node.getID());
                    }
                }

                // Add all missing nodes to the trajectory
                foreach (string missingNodeId in nodeIds)
                {
                    if (trajectory.getNodes().Count > 0)
                    {
                        trajectory.addNode(missingNodeId, 400, 300, 1);
                    }
                }
            }
        }
        /**
         * Ensures that all nodes in a trajectory have a different location. If two nodes are found in the same position, first's x coordinate
         * is incremented by 1 px. This is important as nodes with equal position makes the trajectory algorithm to crash.
         * @param scene
         */
        private static void fixNodesWithSameLocation(Scene scene)
        {
            Trajectory trajectory = scene.getTrajectory();

            if (trajectory != null)
            {
                // Iterate through nodes.
                for (int i = 0; i < trajectory.getNodes().Count; i++)
                {
                    Trajectory.Node node1 = trajectory.getNodes()[i];
                    for (int j = 0; j < trajectory.getNodes().Count; j++)
                    {
                        Trajectory.Node node2 = trajectory.getNodes()[j];
                        if (i != j && node1.getX() == node2.getX() && node1.getY() == node2.getY())
                        {
                            node1.setValues(node1.getX() + 1, node1.getY(), node1.getScale());
                            j = 0;
                        }
                    }
                }
            }
        }
        /**
         * Checks pairs of nodes with duplicate ids. One of the nodes is removed.
         * @param nodes
         */
        private static void fixDuplicateNodes(Scene scene)
        {
            Trajectory trajectory = scene.getTrajectory();

            if (trajectory != null)
            {
                // Iterate through nodes.
                for (int i = 0; i < trajectory.getNodes().Count; i++)
                {
                    Trajectory.Node node1 = trajectory.getNodes()[i];
                    for (int j = 0; j < trajectory.getNodes().Count; j++)
                    {
                        Trajectory.Node node2 = trajectory.getNodes()[j];
                        if (i != j && node1.getID().Equals(node2.getID()))
                        {
                            trajectory.getNodes().RemoveAt(j);
                            i--; j--;
                        }
                    }
                }
            }
        }
Пример #5
0
        public object DOMParse(XmlElement element, params object[] parameters)
        {
            Trajectory trajectory = new Trajectory();

            foreach (XmlElement el in element.SelectNodes("node"))
            {
                string id   = el.GetAttribute("id");
                int    x    = ExParsers.ParseDefault(el.GetAttribute("x"), 0),
                       y    = ExParsers.ParseDefault(el.GetAttribute("y"), 0);
                float scale = ExParsers.ParseDefault(el.GetAttribute("scale"), CultureInfo.InvariantCulture, 1.0f);
                scale = Mathf.Max(0, scale);
                trajectory.addNode(id, x, y, scale);
            }

            foreach (XmlElement el in element.SelectNodes("side"))
            {
                string idStart = el.GetAttribute("idStart");
                string idEnd   = el.GetAttribute("idEnd");
                int    length  = ExParsers.ParseDefault(el.GetAttribute("length"), -1);

                trajectory.addSide(idStart, idEnd, length);
            }

            var initialNode = element.SelectSingleNode("initialnode") as XmlElement;

            if (initialNode != null)
            {
                trajectory.setInitial(initialNode.GetAttribute("id"));
            }

            if (trajectory.getNodes().Count != 0)
            {
                // trajectory.deleteUnconnectedNodes();
                return(trajectory);
            }

            return(null);
        }
        public object DOMParse(XmlElement element, params object[] parameters)
        {
            Trajectory trajectory = new Trajectory();

            foreach (XmlElement el in element.SelectNodes("node"))
            {
                string id   = el.GetAttribute("id") ?? "";
                int    x    = int.Parse(el.GetAttribute("x") ?? "0"),
                       y    = int.Parse(el.GetAttribute("y") ?? "0");
                float scale = float.Parse(el.GetAttribute("scale") ?? "1.0", CultureInfo.InvariantCulture);

                trajectory.addNode(id, x, y, scale);
            }

            foreach (XmlElement el in element.SelectNodes("side"))
            {
                string idStart = el.GetAttribute("idStart") ?? "";
                string idEnd   = el.GetAttribute("idEnd") ?? "";
                int    length  = int.Parse(el.GetAttribute("length") ?? "-1");

                trajectory.addSide(idStart, idEnd, length);
            }

            var initialNode = element.SelectSingleNode("initialnode");

            if (initialNode != null)
            {
                trajectory.setInitial(element.GetAttribute("id") ?? "");
            }

            if (trajectory.getNodes().Count != 0)
            {
                trajectory.deleteUnconnectedNodes();
                return(trajectory);
            }

            return(null);
        }
Пример #7
0
 /**
  * @param trajectory
  *            the trajectory to set
  */
 public void setTrajectory(Trajectory trajectory)
 {
     this.trajectory = trajectory;
 }