public void TestRemoveSegment() { var roads = new RoadManager(); var n1 = roads.CreateNodeAt(1, 0); var n2 = roads.CreateNodeAt(0, 1); var n3 = roads.CreateNodeAt(0, -1); var s12 = roads.CreateSegment(n1, n2, dummyRoad); var s23 = roads.CreateSegment(n2, n3, dummyRoad); var s31 = roads.CreateSegment(n3, n1, dummyRoad); Assert.AreEqual(2, n1.Segments.Count()); Assert.AreEqual(2, n2.Segments.Count()); Assert.AreEqual(2, n3.Segments.Count()); Assert.IsTrue(roads.Remove(s31)); CollectionAssert.AreEquivalent(new[] { s12, s23 }, roads.Segments.ToArray()); CollectionAssert.AreEquivalent(new[] { n1, n2, n3 }, roads.Nodes.ToArray()); Assert.AreEqual(1, n1.Segments.Count()); Assert.AreEqual(2, n2.Segments.Count()); Assert.AreEqual(1, n3.Segments.Count()); }
public void TestMergeNodes() { var roads = new RoadManager(); var n1 = roads.CreateNodeAt(0, 0); var n2 = roads.CreateNodeAt(0, 1); var n3 = roads.CreateNodeAt(0, -1); var s12 = roads.CreateSegment(n1, n2, dummyRoad); var s23 = roads.CreateSegment(n2, n3, dummyRoad); var n23 = roads.MergeNodes(n2, n3); Assert.AreEqual(1, roads.Segments.Count()); CollectionAssert.AreEquivalent(new[] { n1, n23 }, roads.Nodes.ToArray()); }
public void TestMergeSegments() { var roads = new RoadManager(); var n1 = roads.CreateNodeAt(0, 0); var n2 = roads.CreateNodeAt(0, 1); var n3 = roads.CreateNodeAt(0, -1); var s12 = roads.CreateSegment(n1, n2, dummyRoad); var s23 = roads.CreateSegment(n2, n3, dummyRoad); var s13 = roads.MergeSegments(n2); Assert.AreEqual(n1, s13.Start.Node); Assert.AreEqual(n3, s13.End.Node); CollectionAssert.AreEquivalent(new[] { s13 }, roads.Segments.ToArray()); }
private IEnumerable <Segment> readSegments(JsonTextReader reader, RoadManager roads, JsonSerializer serializer, List <Node> nodes, List <SegmentDescription> descriptions) { if (reader.Read()) { // start array if (reader.TokenType == JsonToken.StartArray) { do { if (reader.Read()) // inside node { if (reader.TokenType == JsonToken.StartObject) { Node start = null, end = null; Vector3 startTangent = Vector3.zero, endTangent = Vector3.zero; Vector3 startOffset = Vector3.zero, endOffset = Vector3.zero; SegmentDescription description = null; int depth = reader.Depth; do { if (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName) { switch ((string)reader.Value) { case Constants.TAG_SEGMENT_START: readConnection(reader, serializer, roads, nodes, out start, out startTangent, out startOffset); break; case Constants.TAG_SEGMENT_END: readConnection(reader, serializer, roads, nodes, out end, out endTangent, out endOffset); break; case Constants.TAG_SEGMENT_DESCRIPTION: description = descriptions[reader.ReadAsInt32() ?? -1]; break; } } } } while (reader.Depth > depth); if (start != null && end != null) { var seg = roads.CreateSegment(start, end, description); seg.Start.Tangent = startTangent; seg.End.Tangent = endTangent; seg.Start.Offset = startOffset; seg.End.Offset = endOffset; yield return(seg); } else { throw new FormatException("expected start AND end connections for segment"); } } } } while (reader.TokenType != JsonToken.EndArray); } } }