public static void VerifyVertexIndices(MemoryAccessor memory, uint vertexCount) { Guard.NotNull(memory, nameof(memory)); uint restart_value = 0xff; if (memory.Attribute.Encoding == ENCODING.UNSIGNED_SHORT) { restart_value = 0xffff; } if (memory.Attribute.Encoding == ENCODING.UNSIGNED_INT) { restart_value = 0xffffffff; } memory.AsIntegerArray(); var indices = memory.AsIntegerArray(); for (int i = 0; i < indices.Count; ++i) { var idx = indices[i]; if (idx >= vertexCount) { throw new ArgumentException($"Value[{i}] is out of bounds {vertexCount}.", nameof(memory)); } if (idx == restart_value) { throw new ArgumentException($"Value[{i}] is restart value.", nameof(memory)); } } }
public static IEncodedArray <Vector4> CreateVector4SparseArray(MemoryAccessor bottom, IntegerArray topKeys, MemoryAccessor topValues) { Guard.IsTrue(bottom._Attribute.Dimensions == topValues._Attribute.Dimensions, nameof(topValues)); Guard.IsTrue(topKeys.Count <= bottom._Attribute.ItemsCount, nameof(topKeys)); Guard.IsTrue(topKeys.Count == topValues._Attribute.ItemsCount, nameof(topValues)); Guard.IsTrue(topKeys.All(item => item < (uint)bottom._Attribute.ItemsCount), nameof(topKeys)); return(new SparseArray <Vector4>(bottom.AsVector4Array(), topValues.AsVector4Array(), topKeys)); }
public static IList <Single> CreateScalarSparseArray(MemoryAccessor bottom, IntegerArray topKeys, MemoryAccessor topValues) { Guard.IsTrue(bottom._Attribute.Dimensions == topValues._Attribute.Dimensions, nameof(topValues)); Guard.IsTrue(topKeys.Count <= bottom._Attribute.ItemsCount, nameof(topKeys)); Guard.IsTrue(topKeys.Count == topValues._Attribute.ItemsCount, nameof(topValues)); Guard.IsTrue(topKeys.All(item => item < (uint)bottom._Attribute.ItemsCount), nameof(topKeys)); return(new SparseArray <Single>(bottom.AsScalarArray(), topValues.AsScalarArray(), topKeys)); }
public static void VerifyWeightsSum(MemoryAccessor weights0, MemoryAccessor weights1) { int idx = 0; if (weights1 == null) { if (weights0 == null) { return; } foreach (var item in weights0.GetItemsAsRawBytes()) { if (!_CheckWeightSum(item, weights0.Attribute.Encoding)) { throw new ArgumentException($"Weight Sum invalid at Index {idx}", nameof(weights0)); } ++idx; } return; } // check for WEIGHTS_0 + WEIGHTS_1 if (weights0 == null) { throw new ArgumentNullException(nameof(weights0)); } if (weights0.Attribute.Encoding != weights1.Attribute.Encoding) { throw new ArgumentException("WEIGHTS_0 and WEIGHTS_1 format mismatch.", nameof(weights1.Attribute)); } var len = weights0.Attribute.ItemByteLength; Span <Byte> dst = stackalloc byte[len * 2]; var zip = weights0.GetItemsAsRawBytes() .Zip(weights1.GetItemsAsRawBytes(), (a, b) => (a, b)); foreach (var(a, b) in zip) { a.AsSpan().CopyTo(dst); b.AsSpan().CopyTo(dst.Slice(len)); if (!_CheckWeightSum(dst, weights0.Attribute.Encoding)) { throw new ArgumentException($"Weight Sum invalid at Index {idx}", nameof(weights1)); } ++idx; } }
public static IList <Vector4> CreateColorSparseArray(MemoryAccessor bottom, IntegerArray topKeys, MemoryAccessor topValues, Single defaultW = 1) { Guard.NotNull(bottom, nameof(bottom)); Guard.NotNull(topValues, nameof(topValues)); Guard.IsTrue(bottom._Slicer.Dimensions == topValues._Slicer.Dimensions, nameof(topValues)); Guard.IsTrue(topKeys.Count <= bottom._Slicer.ItemsCount, nameof(topKeys)); Guard.IsTrue(topKeys.Count == topValues._Slicer.ItemsCount, nameof(topValues)); Guard.IsTrue(topKeys.All(item => item < (uint)bottom._Slicer.ItemsCount), nameof(topKeys)); return(new SparseArray <Vector4>(bottom.AsColorArray(defaultW), topValues.AsColorArray(defaultW), topKeys)); }
public static IList <Vector3> CreateVector3SparseArray(MemoryAccessor bottom, IntegerArray topKeys, MemoryAccessor topValues) { Guard.NotNull(bottom, nameof(bottom)); Guard.NotNull(topValues, nameof(topValues)); Guard.IsTrue(bottom._Encoding.Dimensions == topValues._Encoding.Dimensions, nameof(topValues)); Guard.IsTrue(topKeys.Count <= bottom._Encoding.ItemsCount, nameof(topKeys)); Guard.IsTrue(topKeys.Count == topValues._Encoding.ItemsCount, nameof(topValues)); Guard.IsTrue(topKeys.All(item => item < (uint)bottom._Encoding.ItemsCount), nameof(topKeys)); return(new SparseArray <Vector3>(bottom.AsVector3Array(), topValues.AsVector3Array(), topKeys)); }
public static void ValidateWeightsSum(Validation.ValidationContext result, MemoryAccessor weights0, MemoryAccessor weights1) { int idx = 0; if (weights1 == null) { if (weights0 == null) { return; } foreach (var item in weights0.GetItemsAsRawBytes()) { if (!_CheckWeightSum(item, weights0.Attribute.Encoding)) { result.AddDataError($"Weight Sum invalid at Index {idx}"); } ++idx; } return; } if (weights0 == null) { result.AddLinkError(""); return; } var len = weights0.Attribute.ItemByteLength; Span <Byte> dst = stackalloc byte[len * 2]; var zip = weights0.GetItemsAsRawBytes().Zip(weights1.GetItemsAsRawBytes(), (a, b) => (a, b)); foreach (var(a, b) in zip) { a.AsSpan().CopyTo(dst); b.AsSpan().CopyTo(dst.Slice(len)); if (!_CheckWeightSum(dst, weights0.Attribute.Encoding)) { result.AddDataError($"Weight Sum invalid at Index {idx}"); } ++idx; } }
public static void SanitizeWeightsSum(MemoryAccessor weights0, MemoryAccessor weights1) { if (weights1 == null) { if (weights0 == null) { return; } foreach (var item in weights0.GetItemsAsRawBytes()) { _SanitizeWeightSum(item, weights0.Attribute.Encoding); } return; } if (weights0 == null) { return; } var len = weights0.Attribute.ItemByteLength; Span <Byte> dst = stackalloc byte[len * 2]; var zip = weights0.GetItemsAsRawBytes().Zip(weights1.GetItemsAsRawBytes(), (a, b) => (a, b)); foreach (var(a, b) in zip) { a.AsSpan().CopyTo(dst); b.AsSpan().CopyTo(dst.Slice(len)); if (_SanitizeWeightSum(dst, weights0.Attribute.Encoding)) { dst.Slice(0, len).CopyTo(a); dst.Slice(len, len).CopyTo(b); } } }
public static bool HaveOverlappingBuffers(MemoryAccessor a, MemoryAccessor b) { Guard.NotNull(a, nameof(a)); Guard.NotNull(b, nameof(b)); var aa = a._GetBytes(); var bb = b._GetBytes(); if (aa.Array != bb.Array) { return(false); } if (aa.Offset >= bb.Offset + bb.Count) { return(false); } if (bb.Offset >= aa.Offset + aa.Count) { return(false); } return(true); }
public static void VerifyAccessorBounds(MemoryAccessor memory, IReadOnlyList <double> min, IReadOnlyList <double> max) { Guard.NotNull(memory, nameof(memory)); Guard.NotNull(min, nameof(min)); Guard.NotNull(max, nameof(max)); if (min.Count == 0 && max.Count == 0) { return; } var dimensions = memory.Attribute.Dimensions.DimCount(); if (min.Count != dimensions) { throw new ArgumentException($"min size mismatch; expected {dimensions} but found {min.Count}", nameof(min)); } if (max.Count != dimensions) { throw new ArgumentException($"max size mismatch; expected {dimensions} but found {max.Count}", nameof(max)); } for (int i = 0; i < min.Count; ++i) { // if (_min[i] > _max[i]) result.AddError(this, $"min[{i}] is larger than max[{i}]"); } var minimum = min.Select(item => (float)item).ToArray(); var maximum = max.Select(item => (float)item).ToArray(); var xinfo = memory.Attribute; xinfo.Dimensions = DIMENSIONS.SCALAR; memory = new MemoryAccessor(memory.Data, xinfo); var array = new MultiArray(memory.Data, memory.Attribute.ByteOffset, memory.Attribute.ItemsCount, memory.Attribute.ByteStride, dimensions, memory.Attribute.Encoding, memory.Attribute.Normalized); var current = new float[dimensions]; for (int i = 0; i < array.Count; ++i) { array.CopyItemTo(i, current); for (int j = 0; j < current.Length; ++j) { var v = current[j]; // if (!v._IsFinite()) result.AddError(this, $"Item[{j}][{i}] is not a finite number: {v}"); var axisMin = minimum[j]; var axisMax = maximum[j]; if (v < axisMin || v > axisMax) { throw new ArgumentOutOfRangeException(nameof(memory), $"Value[{i}] is out of bounds. {axisMin} <= {v} <= {axisMax}"); } // if (v < min || v > max) result.AddError(this, $"Item[{j}][{i}] is out of bounds. {min} <= {v} <= {max}"); } } }