/// <summary> /// Extension method for parsing trajectory. /// </summary> /// <param name="trajectoryNode">Node with trajectory</param> /// <returns>Trajectory from xml.</returns> public static Trajectory ParseTrajectory(this XmlNode trajectoryNode) { Direction direction; if (trajectoryNode.FirstChild.Attributes["direction"] == null) { direction = Direction.CLOCKWISE; } else { string directionString = trajectoryNode.FirstChild.Attributes["direction"].Value; if (directionString.Equals("clockwise")) { direction = Direction.CLOCKWISE; } else { direction = Direction.COUNTERCLOCKWISE; } } Trajectory trajectory = null; switch (trajectoryNode.FirstChild.Name.ToLowerInvariant()) { case "stacionary": int x = trajectoryNode.FirstChild.Attributes["x"].IntValue(); int y = trajectoryNode.FirstChild.Attributes["y"].IntValue(); trajectory = new Stacionary(x, y); break; case "circularorbit": { int radius = trajectoryNode.FirstChild.Attributes["radius"].IntValue(); double initialAngle = trajectoryNode.FirstChild.Attributes["initialAngle"].DoubleValue(); int period = trajectoryNode.FirstChild.Attributes["period"].IntValue(); trajectory = new CircularOrbit(radius, period, direction, initialAngle); } break; case "ellipticorbit": { int a = trajectoryNode.FirstChild.Attributes["a"].IntValue(); int b = trajectoryNode.FirstChild.Attributes["b"].IntValue(); double rotationAngle = trajectoryNode.FirstChild.Attributes["angle"].DoubleValue(); double initialAngle = trajectoryNode.FirstChild.Attributes["initialAngle"].DoubleValue(); int period = trajectoryNode.FirstChild.Attributes["period"].IntValue(); trajectory = new EllipticOrbit(new Point2d(0.0, 0.0), a, b, rotationAngle, period, direction, initialAngle); } break; default: throw new XmlException("Unexpected trajectory."); } return(trajectory); }
public void ParseTrajectoryTest_Stacionary() { //string[] trajectoryParams = { "velocity", "5", "direction", "clockwise" }; string[] pointParams = { "velocity", "5", "direction", "clockwise", "x", "10", "y", "20" }; Stacionary expected = new Stacionary(10, 20); XmlNode trajectoryNode = this.GenerateTrajectoryNode("stacionary", pointParams); Trajectory trajectory = trajectoryNode.ParseTrajectory(); Assert.AreEqual(expected, trajectory); }
/// <summary> /// Metoda pro vytvoreni elementu z trajektorie /// </summary> /// <param name="trajectory">Trajektorie pro zpracovani</param> /// <returns>Element s trajektorii</returns> private static XElement TrajectoryToElement(Trajectory trajectory) { XElement trajectoryElement = new XElement("trajectory"); XElement trajectoryShapeElement = null; if (trajectory is CircularOrbit) { CircularOrbit circOrbit = (CircularOrbit)trajectory; trajectoryShapeElement = new XElement("circularOrbit"); trajectoryShapeElement.Add(new XAttribute("direction", circOrbit.Direction.ToString().ToLower())); trajectoryShapeElement.Add(new XAttribute("period", circOrbit.PeriodInSec)); String initialAngle = Math.Round(MathUtil.RadianToDegree(circOrbit.InitialAngleRad)).ToString("0.0", CultureInfo.InvariantCulture); trajectoryShapeElement.Add(new XAttribute("initialAngle", initialAngle)); trajectoryShapeElement.Add(new XAttribute("radius", circOrbit.Radius)); } else if (trajectory is EllipticOrbit) { EllipticOrbit elliOrbit = (EllipticOrbit)trajectory; trajectoryShapeElement = new XElement("ellipticOrbit"); trajectoryShapeElement.Add(new XAttribute("direction", elliOrbit.Direction.ToString().ToLower())); trajectoryShapeElement.Add(new XAttribute("period", elliOrbit.PeriodInSec)); trajectoryShapeElement.Add(new XAttribute("a", elliOrbit.A)); trajectoryShapeElement.Add(new XAttribute("b", elliOrbit.B)); String angle = Math.Round(MathUtil.RadianToDegree(elliOrbit.RotationAngleInRad)).ToString("0.0", CultureInfo.InvariantCulture); trajectoryShapeElement.Add(new XAttribute("angle", angle)); String initialAngle = Math.Round(MathUtil.RadianToDegree(elliOrbit.InitialAngleRad)).ToString("0.0", CultureInfo.InvariantCulture); trajectoryShapeElement.Add(new XAttribute("initialAngle", initialAngle)); } else if (trajectory is Stacionary) { Stacionary stacOrbit = (Stacionary)trajectory; trajectoryShapeElement = new XElement("stacionary"); trajectoryShapeElement.Add(new XAttribute("x", stacOrbit.X)); trajectoryShapeElement.Add(new XAttribute("y", stacOrbit.Y)); } trajectoryElement.Add(trajectoryShapeElement); return(trajectoryElement); }