Skip to content

Commit 1c02f80

Browse files
committed
split internal extensions
1 parent 0b51bef commit 1c02f80

File tree

4 files changed

+142
-126
lines changed

4 files changed

+142
-126
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
using System.Text;
5+
6+
namespace SharpGLTF.Schema2
7+
{
8+
/// <summary>
9+
/// Extensions used internally.
10+
/// </summary>
11+
static partial class _Schema2Extensions
12+
{
13+
#region morph weights
14+
15+
public static void SetMorphWeights(this IList<Double> list, int maxCount, Transforms.SparseWeight8 weights)
16+
{
17+
while (list.Count > maxCount) list.RemoveAt(list.Count - 1);
18+
while (list.Count < maxCount) list.Add(0);
19+
20+
if (list.Count > 0)
21+
{
22+
for (int i = 0; i < list.Count; ++i)
23+
{
24+
list[i] = 0;
25+
}
26+
27+
foreach (var (index, weight) in weights.GetIndexedWeights())
28+
{
29+
list[index] = weight;
30+
}
31+
}
32+
}
33+
34+
public static void SetMorphWeights(this IList<Double> list, IReadOnlyList<float> weights)
35+
{
36+
if (weights == null) { list.Clear(); return; }
37+
38+
while (list.Count > weights.Count) list.RemoveAt(list.Count - 1);
39+
while (list.Count < weights.Count) list.Add(0);
40+
41+
if (list.Count > 0)
42+
{
43+
for (int i = 0; i < list.Count; ++i)
44+
{
45+
list[i] = weights[i];
46+
}
47+
}
48+
}
49+
50+
#endregion
51+
}
52+
}
Lines changed: 85 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,85 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Numerics;
4-
using System.Text;
5-
6-
namespace SharpGLTF.Schema2
7-
{
8-
/// <summary>
9-
/// Extensions used internally.
10-
/// </summary>
11-
static class _Schema2Extensions
12-
{
13-
#region morph weights
14-
15-
public static void SetMorphWeights(this IList<Double> list, int maxCount, Transforms.SparseWeight8 weights)
16-
{
17-
while (list.Count > maxCount) list.RemoveAt(list.Count - 1);
18-
while (list.Count < maxCount) list.Add(0);
19-
20-
if (list.Count > 0)
21-
{
22-
for (int i = 0; i < list.Count; ++i)
23-
{
24-
list[i] = 0;
25-
}
26-
27-
foreach (var (index, weight) in weights.GetIndexedWeights())
28-
{
29-
list[index] = weight;
30-
}
31-
}
32-
}
33-
34-
public static void SetMorphWeights(this IList<Double> list, IReadOnlyList<float> weights)
35-
{
36-
if (weights == null) { list.Clear(); return; }
37-
38-
while (list.Count > weights.Count) list.RemoveAt(list.Count - 1);
39-
while (list.Count < weights.Count) list.Add(0);
40-
41-
if (list.Count > 0)
42-
{
43-
for (int i = 0; i < list.Count; ++i)
44-
{
45-
list[i] = weights[i];
46-
}
47-
}
48-
}
49-
50-
#endregion
51-
52-
#region nullables
53-
54-
internal static String AsName(this string name)
55-
{
56-
return string.IsNullOrWhiteSpace(name) ? null : name;
57-
}
58-
59-
internal static T AsValue<T>(this T? value, T defval)
60-
where T : struct
61-
{
62-
return value ?? defval;
63-
}
64-
65-
internal static T? AsNullable<T>(this T value, T defval)
66-
where T : struct
67-
{
68-
return value.Equals(defval) ? (T?)null : value;
69-
}
70-
71-
internal static T? AsNullable<T>(this T value, T defval, T minval, T maxval)
72-
where T : struct, IEquatable<T>, IComparable<T>
73-
{
74-
if (value.Equals(defval)) return null;
75-
76-
if (value.CompareTo(minval) < 0) value = minval;
77-
if (value.CompareTo(maxval) > 0) value = maxval;
78-
79-
return value.Equals(defval) ? (T?)null : value;
80-
}
81-
82-
internal static Vector2? AsNullable(this Vector2 value, Vector2 defval, Vector2 minval, Vector2 maxval)
83-
{
84-
if (value.Equals(defval)) return null;
85-
86-
value = Vector2.Min(value, maxval);
87-
value = Vector2.Max(value, minval);
88-
89-
return value.Equals(defval) ? (Vector2?)null : value;
90-
}
91-
92-
internal static Vector3? AsNullable(this Vector3 value, Vector3 defval, Vector3 minval, Vector3 maxval)
93-
{
94-
if (value.Equals(defval)) return null;
95-
96-
value = Vector3.Min(value, maxval);
97-
value = Vector3.Max(value, minval);
98-
99-
return value.Equals(defval) ? (Vector3?)null : value;
100-
}
101-
102-
internal static Vector4? AsNullable(this Vector4 value, Vector4 defval, Vector4 minval, Vector4 maxval)
103-
{
104-
if (value.Equals(defval)) return (Vector4?)null;
105-
106-
value = Vector4.Min(value, maxval);
107-
value = Vector4.Max(value, minval);
108-
109-
return value.Equals(defval) ? (Vector4?)null : value;
110-
}
111-
112-
internal static String AsNullable(this string value)
113-
{
114-
return string.IsNullOrEmpty(value) ? null : value;
115-
}
116-
117-
internal static String AsEmptyNullable(this string value)
118-
{
119-
return string.IsNullOrWhiteSpace(value) ? null : value;
120-
}
121-
122-
#endregion
123-
}
124-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
using System.Text;
5+
6+
namespace SharpGLTF.Schema2
7+
{
8+
/// <summary>
9+
/// Extensions used internally.
10+
/// </summary>
11+
static partial class _Schema2Extensions
12+
{
13+
#region nullables
14+
15+
internal static String AsName(this string name)
16+
{
17+
return string.IsNullOrWhiteSpace(name) ? null : name;
18+
}
19+
20+
internal static T AsValue<T>(this T? value, T defval)
21+
where T : struct
22+
{
23+
return value ?? defval;
24+
}
25+
26+
internal static T? AsNullable<T>(this T value, T defval)
27+
where T : struct
28+
{
29+
return value.Equals(defval) ? (T?)null : value;
30+
}
31+
32+
internal static T? AsNullable<T>(this T value, T defval, T minval, T maxval)
33+
where T : struct, IEquatable<T>, IComparable<T>
34+
{
35+
if (value.Equals(defval)) return null;
36+
37+
if (value.CompareTo(minval) < 0) value = minval;
38+
if (value.CompareTo(maxval) > 0) value = maxval;
39+
40+
return value.Equals(defval) ? (T?)null : value;
41+
}
42+
43+
internal static Vector2? AsNullable(this Vector2 value, Vector2 defval, Vector2 minval, Vector2 maxval)
44+
{
45+
if (value.Equals(defval)) return null;
46+
47+
value = Vector2.Min(value, maxval);
48+
value = Vector2.Max(value, minval);
49+
50+
return value.Equals(defval) ? (Vector2?)null : value;
51+
}
52+
53+
internal static Vector3? AsNullable(this Vector3 value, Vector3 defval, Vector3 minval, Vector3 maxval)
54+
{
55+
if (value.Equals(defval)) return null;
56+
57+
value = Vector3.Min(value, maxval);
58+
value = Vector3.Max(value, minval);
59+
60+
return value.Equals(defval) ? (Vector3?)null : value;
61+
}
62+
63+
internal static Vector4? AsNullable(this Vector4 value, Vector4 defval, Vector4 minval, Vector4 maxval)
64+
{
65+
if (value.Equals(defval)) return (Vector4?)null;
66+
67+
value = Vector4.Min(value, maxval);
68+
value = Vector4.Max(value, minval);
69+
70+
return value.Equals(defval) ? (Vector4?)null : value;
71+
}
72+
73+
internal static String AsNullable(this string value)
74+
{
75+
return string.IsNullOrEmpty(value) ? null : value;
76+
}
77+
78+
internal static String AsEmptyNullable(this string value)
79+
{
80+
return string.IsNullOrWhiteSpace(value) ? null : value;
81+
}
82+
83+
#endregion
84+
}
85+
}

src/SharpGLTF.Ext.3DTiles/SharpGLTF.Ext.3DTiles.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
<ItemGroup>
1010
<Compile Include="..\Shared\Guard.cs" Link="Diagnostics\Guard.cs" />
11-
<Compile Include="..\Shared\_Extensions.cs" Link="_Extensions.cs" />
11+
<Compile Include="..\Shared\_Extensions.cs" Link="_Extensions.cs" />
12+
<Compile Include="..\SharpGLTF.Core\Schema2\_Extensions.Nullables.cs" Link="Schema2\_Extensions.Nullables.cs" />
1213
</ItemGroup>
1314

1415
<ItemGroup>
@@ -19,8 +20,10 @@
1920
<Folder Include="Schema2\Generated\" />
2021
</ItemGroup>
2122

23+
<!--
2224
<ItemGroup>
2325
<PackageReference Include="OneOf" Version="3.0.263" />
2426
</ItemGroup>
27+
-->
2528

2629
</Project>

src/SharpGLTF.Ext.Agi/SharpGLTF.Ext.Agi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<ItemGroup>
1818
<Compile Include="..\Shared\Guard.cs" Link="Diagnostics\Guard.cs" />
1919
<Compile Include="..\Shared\_Extensions.cs" Link="_Extensions.cs" />
20-
<Compile Include="..\SharpGLTF.Core\Schema2\_Extensions.cs" Link="Schema2\_Extensions.cs" />
20+
<Compile Include="..\SharpGLTF.Core\Schema2\_Extensions.Nullables.cs" Link="Schema2\_Extensions.Nullables.cs" />
2121
</ItemGroup>
2222

2323
<ItemGroup>

0 commit comments

Comments
 (0)