Skip to content

Commit eefaa6b

Browse files
committed
Expose correct morph target cast, fixes vpenades#239
1 parent 984188d commit eefaa6b

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

src/Shared/_Extensions.cs

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ internal static ArraySegment<T> Slice<T>(this T[] array, int offset)
293293
return new ArraySegment<T>(array, offset, array.Length - offset);
294294
}
295295

296+
#if NETSTANDARD2_0
296297
internal static ArraySegment<T> Slice<T>(this ArraySegment<T> array, int offset)
297298
{
298299
return new ArraySegment<T>(array.Array, array.Offset + offset, array.Count - offset);
@@ -302,6 +303,7 @@ internal static ArraySegment<T> Slice<T>(this ArraySegment<T> array, int offset,
302303
{
303304
return new ArraySegment<T>(array.Array, array.Offset + offset, count);
304305
}
306+
#endif
305307

306308
internal static T[] CloneArray<T>(this T[] srcArray)
307309
{
@@ -438,6 +440,70 @@ public static void SanitizeTangents(this IList<Vector4> tangents)
438440
}
439441
}
440442

443+
public static IReadOnlyList<TResult> SelectList<TSource,TResult>(this IReadOnlyList<TSource> collection, Func<TSource,TResult> selector)
444+
{
445+
return new _ListSelect<TSource,TResult>(collection, selector);
446+
}
447+
448+
public static IReadOnlyCollection<TResult> SelectCollection<TSource, TResult>(this IReadOnlyCollection<TSource> collection, Func<TSource, TResult> selector)
449+
{
450+
return new _CollectionSelect<TSource, TResult>(collection, selector);
451+
}
452+
453+
private readonly struct _ListSelect<TSource,TResult> : IReadOnlyList<TResult>
454+
{
455+
public _ListSelect(IReadOnlyList<TSource> list, Func<TSource,TResult> selector)
456+
{
457+
_List = list;
458+
_Selector = selector;
459+
}
460+
461+
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
462+
private readonly IReadOnlyList<TSource> _List;
463+
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
464+
private readonly Func<TSource, TResult> _Selector;
465+
466+
public TResult this[int index] => _Selector(_List[index]);
467+
468+
public int Count => _List.Count;
469+
470+
public IEnumerator<TResult> GetEnumerator()
471+
{
472+
foreach (var item in _List) yield return _Selector(item);
473+
}
474+
475+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
476+
{
477+
foreach (var item in _List) yield return _Selector(item);
478+
}
479+
}
480+
481+
private readonly struct _CollectionSelect<TSource, TResult> : IReadOnlyCollection<TResult>
482+
{
483+
public _CollectionSelect(IReadOnlyCollection<TSource> list, Func<TSource, TResult> selector)
484+
{
485+
_List = list;
486+
_Selector = selector;
487+
}
488+
489+
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
490+
private readonly IReadOnlyCollection<TSource> _List;
491+
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
492+
private readonly Func<TSource, TResult> _Selector;
493+
494+
public int Count => _List.Count;
495+
496+
public IEnumerator<TResult> GetEnumerator()
497+
{
498+
foreach (var item in _List) yield return _Selector(item);
499+
}
500+
501+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
502+
{
503+
foreach (var item in _List) yield return _Selector(item);
504+
}
505+
}
506+
441507
#endregion
442508

443509
#region vertex & index accessors
@@ -777,12 +843,12 @@ private static Byte[] _TryParseBase64Unchecked(string uri, string prefix)
777843
public static string _EscapeStringInternal(this string uri)
778844
{
779845
// https://stackoverflow.com/questions/4396598/whats-the-difference-between-escapeuristring-and-escapedatastring
780-
#pragma warning disable SYSLIB0013 // Type or member is obsolete
846+
#pragma warning disable SYSLIB0013 // Type or member is obsolete
781847
return Uri.EscapeUriString(uri);
782-
#pragma warning restore SYSLIB0013 // Type or member is obsolete
848+
#pragma warning restore SYSLIB0013 // Type or member is obsolete
783849
}
784850

785-
#if NET6_0
851+
#if NET6_0
786852

787853
/// <summary>
788854
/// Creates a new instance of the <see cref="JsonNode"/>.
@@ -794,11 +860,11 @@ public static string _EscapeStringInternal(this string uri)
794860
/// </remarks>
795861
/// <param name="node">The node to clone.</param>
796862
/// <returns>A clone of <paramref name="node"/>.</returns>
797-
#if NET6_0_OR_GREATER
863+
#if NET6_0_OR_GREATER
798864
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(System.Text.Json.Nodes.JsonValue))]
799865
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(System.Text.Json.Nodes.JsonArray))]
800866
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(System.Text.Json.Nodes.JsonObject))]
801-
#endif
867+
#endif
802868
public static System.Text.Json.Nodes.JsonNode DeepClone(this System.Text.Json.Nodes.JsonNode node)
803869
{
804870
// issue tracking both DeepClone and DeepEquals: https://github.com/dotnet/runtime/issues/56592
@@ -807,14 +873,14 @@ public static System.Text.Json.Nodes.JsonNode DeepClone(this System.Text.Json.No
807873

808874
System.Text.Json.Nodes.JsonNode clone = null;
809875

810-
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
876+
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
811877
switch(node)
812878
{
813879
case System.Text.Json.Nodes.JsonValue asValue: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonValue>(asValue); break;
814880
case System.Text.Json.Nodes.JsonArray asArray: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonArray>(asArray); break;
815881
case System.Text.Json.Nodes.JsonObject asObject: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonObject>(asObject); break;
816882
}
817-
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
883+
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
818884

819885
if (clone == null) throw new NotImplementedException();
820886

@@ -823,15 +889,15 @@ public static System.Text.Json.Nodes.JsonNode DeepClone(this System.Text.Json.No
823889
return clone;
824890
}
825891

826-
#endif
892+
#endif
827893

828894
public static bool DeepEquals(this System.Text.Json.Nodes.JsonNode x, System.Text.Json.Nodes.JsonNode y, double precission)
829895
{
830-
#if !NET6_0
896+
#if !NET6_0
831897

832898
return System.Text.Json.Nodes.JsonNode.DeepEquals(x, y);
833899

834-
#else
900+
#else
835901

836902
if (x == y) return true;
837903
if (x == null) return false;
@@ -888,7 +954,7 @@ public static bool DeepEquals(this System.Text.Json.Nodes.JsonNode x, System.Tex
888954

889955
return false;
890956

891-
#endif
957+
#endif
892958
}
893959

894960
#endregion

src/SharpGLTF.Toolkit/Geometry/MorphTargetBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,13 @@ public void SetVertex(TvG meshVertex, TvG morphVertex)
404404
public IReadOnlyCollection<Vector3> Positions => _Positions.Keys;
405405

406406
/// <inheritdoc/>
407-
IReadOnlyCollection<IVertexGeometry> IMorphTargetBuilder.Vertices => (IReadOnlyList<IVertexGeometry>)(IReadOnlyCollection<TvG>)_Vertices.Keys;
407+
IReadOnlyCollection<IVertexGeometry> IMorphTargetBuilder.Vertices => _Vertices.Keys.SelectCollection(item => (IVertexGeometry)item);
408408

409409
/// <inheritdoc/>
410410
IReadOnlyList<IVertexGeometry> IMorphTargetBuilder.GetVertices(Vector3 position)
411411
{
412412
return _Positions.TryGetValue(position, out List<TvG> geos)
413-
? (IReadOnlyList<IVertexGeometry>)geos
413+
? geos.SelectList(item => (IVertexGeometry)item)
414414
: Array.Empty<IVertexGeometry>();
415415
}
416416

0 commit comments

Comments
 (0)