Skip to content

Commit

Permalink
Expose correct morph target cast, fixes #239
Browse files Browse the repository at this point in the history
  • Loading branch information
vpenades committed Jun 5, 2024
1 parent 984188d commit eefaa6b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
88 changes: 77 additions & 11 deletions src/Shared/_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ internal static ArraySegment<T> Slice<T>(this T[] array, int offset)
return new ArraySegment<T>(array, offset, array.Length - offset);
}

#if NETSTANDARD2_0
internal static ArraySegment<T> Slice<T>(this ArraySegment<T> array, int offset)
{
return new ArraySegment<T>(array.Array, array.Offset + offset, array.Count - offset);
Expand All @@ -302,6 +303,7 @@ internal static ArraySegment<T> Slice<T>(this ArraySegment<T> array, int offset,
{
return new ArraySegment<T>(array.Array, array.Offset + offset, count);
}
#endif

internal static T[] CloneArray<T>(this T[] srcArray)
{
Expand Down Expand Up @@ -438,6 +440,70 @@ public static void SanitizeTangents(this IList<Vector4> tangents)
}
}

public static IReadOnlyList<TResult> SelectList<TSource,TResult>(this IReadOnlyList<TSource> collection, Func<TSource,TResult> selector)
{
return new _ListSelect<TSource,TResult>(collection, selector);
}

public static IReadOnlyCollection<TResult> SelectCollection<TSource, TResult>(this IReadOnlyCollection<TSource> collection, Func<TSource, TResult> selector)
{
return new _CollectionSelect<TSource, TResult>(collection, selector);
}

private readonly struct _ListSelect<TSource,TResult> : IReadOnlyList<TResult>
{
public _ListSelect(IReadOnlyList<TSource> list, Func<TSource,TResult> selector)
{
_List = list;
_Selector = selector;
}

[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
private readonly IReadOnlyList<TSource> _List;
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
private readonly Func<TSource, TResult> _Selector;

public TResult this[int index] => _Selector(_List[index]);

public int Count => _List.Count;

public IEnumerator<TResult> GetEnumerator()
{
foreach (var item in _List) yield return _Selector(item);
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (var item in _List) yield return _Selector(item);
}
}

private readonly struct _CollectionSelect<TSource, TResult> : IReadOnlyCollection<TResult>
{
public _CollectionSelect(IReadOnlyCollection<TSource> list, Func<TSource, TResult> selector)
{
_List = list;
_Selector = selector;
}

[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
private readonly IReadOnlyCollection<TSource> _List;
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
private readonly Func<TSource, TResult> _Selector;

public int Count => _List.Count;

public IEnumerator<TResult> GetEnumerator()
{
foreach (var item in _List) yield return _Selector(item);
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (var item in _List) yield return _Selector(item);
}
}

#endregion

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

#if NET6_0
#if NET6_0

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

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

#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
switch(node)
{
case System.Text.Json.Nodes.JsonValue asValue: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonValue>(asValue); break;
case System.Text.Json.Nodes.JsonArray asArray: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonArray>(asArray); break;
case System.Text.Json.Nodes.JsonObject asObject: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonObject>(asObject); break;
}
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code

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

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

#endif
#endif

public static bool DeepEquals(this System.Text.Json.Nodes.JsonNode x, System.Text.Json.Nodes.JsonNode y, double precission)
{
#if !NET6_0
#if !NET6_0

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

#else
#else

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

return false;

#endif
#endif
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/SharpGLTF.Toolkit/Geometry/MorphTargetBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,13 @@ public void SetVertex(TvG meshVertex, TvG morphVertex)
public IReadOnlyCollection<Vector3> Positions => _Positions.Keys;

/// <inheritdoc/>
IReadOnlyCollection<IVertexGeometry> IMorphTargetBuilder.Vertices => (IReadOnlyList<IVertexGeometry>)(IReadOnlyCollection<TvG>)_Vertices.Keys;
IReadOnlyCollection<IVertexGeometry> IMorphTargetBuilder.Vertices => _Vertices.Keys.SelectCollection(item => (IVertexGeometry)item);

/// <inheritdoc/>
IReadOnlyList<IVertexGeometry> IMorphTargetBuilder.GetVertices(Vector3 position)
{
return _Positions.TryGetValue(position, out List<TvG> geos)
? (IReadOnlyList<IVertexGeometry>)geos
? geos.SelectList(item => (IVertexGeometry)item)
: Array.Empty<IVertexGeometry>();
}

Expand Down

0 comments on commit eefaa6b

Please sign in to comment.