public static IGeometryAttributes Merge(this IArray <IGeometryAttributes> gs) { if (gs.Count == 0) { return(GeometryAttributes.Empty); } var first = gs[0]; if (gs.Count == 1) { return(first); } var corners = first.NumCornersPerFace; if (!gs.All(g => g.NumCornersPerFace == corners)) { throw new Exception("Cannot merge meshes with different numbers of corners per faces"); } // Merge all of the attributes of the different geometries // Except: indices, group indexes, subgeo, and instance attributes var attributes = first.VertexAttributes() .Concat(first.CornerAttributes()) .Concat(first.EdgeAttributes()) .Concat(first.NoneAttributes()) .Concat(first.FaceAttributes()) // Skip the index semantic because things get re-ordered .Where(attr => attr.Descriptor.Semantic != Semantic.Index) .ToArray(); // Merge the attributes var others = gs.Skip(1).ToEnumerable(); var attributeList = attributes.Select( attr => attr.Merge(others.Select(g => g.GetAttributeOrDefault(attr.Name)))).ToList(); // Add the renumbered index attribute if (first.GetAttributeIndex() != null) { var vertexOffset = 0; var cornerOffset = 0; var indices = new int[gs.Sum(ga => ga.NumCorners)]; for (var i = 0; i < gs.Count; i++) { var ga = gs[i]; var localIndices = ga.GetAttributeIndex().Data; for (var j = 0; j < localIndices.Count; j++) { indices[cornerOffset + j] = localIndices[j] + vertexOffset; } vertexOffset += ga.NumVertices; cornerOffset += ga.NumCorners; } attributeList.Add(indices.ToIArray().ToIndexAttribute()); } return(attributeList.ToGeometryAttributes()); }
public static void CheckTens(IArray <int> tens) { Assert.IsTrue(tens.SequenceEquals(ArrayToTen.ToIArray())); Assert.AreEqual(0, tens.First()); Assert.AreEqual(9, tens.Last()); Assert.AreEqual(45, tens.Aggregate(0, (a, b) => a + b)); Assert.AreEqual(10, tens.Count); Assert.AreEqual(5, tens[5]); Assert.AreEqual(5, tens.ElementAt(5)); var ones = 1.Repeat(9); var diffs = tens.ZipEachWithNext((x, y) => y - x); Assert.IsTrue(ones.SequenceEquals(diffs)); Assert.IsFalse(ones.SequenceEquals(tens)); var indices = tens.Indices(); Assert.IsTrue(tens.SequenceEquals(indices)); Assert.IsTrue(tens.SequenceEquals(tens.SelectByIndex(indices))); Assert.IsTrue(tens.Reverse().SequenceEquals(tens.SelectByIndex(indices.Reverse()))); var sum = 0; foreach (var x in tens.ToEnumerable()) { sum += x; } foreach (var x in tens.ToEnumerable()) { Console.WriteLine(x.ToString()); } Assert.AreEqual(45, sum); Assert.AreEqual(0, tens.First()); Assert.True(tens.All(x => x < 10)); Assert.True(tens.Any(x => x < 5)); Assert.AreEqual(5, tens.CountWhere(x => x % 2 == 0)); Assert.AreEqual(0, tens.Reverse().Last()); Assert.AreEqual(0, tens.Reverse().Reverse().First()); var split = tens.Split(LinqArray.Create(3, 6)); Assert.AreEqual(3, split.Count); var counts = split.Select(x => x.Count); Assert.True(counts.SequenceEquals(LinqArray.Create(3, 3, 4))); var indices2 = counts.Accumulate((x, y) => x + y); Assert.True(indices2.SequenceEquals(LinqArray.Create(3, 6, 10))); var indices3 = counts.PostAccumulate((x, y) => x + y); Assert.True(indices3.SequenceEquals(LinqArray.Create(0, 3, 6, 10))); var flattened = split.Flatten(); Assert.True(flattened.SequenceEquals(tens)); }
public static bool All(this IArray <bool> self) { return(self.All(x => x)); }