Browse Source

completed adding fast sampler.

Vicente Penades 6 years ago
parent
commit
f923730706

+ 20 - 0
src/SharpGLTF.Core/Animations/CubicSamplers.cs

@@ -185,6 +185,16 @@ namespace SharpGLTF.Animations
             return _Sequence.ToDictionary(pair => pair.Item1, pair => pair.Item2);
         }
 
+        public ICurveSampler<Transforms.SparseWeight8> ToFastSampler()
+        {
+            var split = _Sequence
+                .SplitByTime()
+                .Select(item => new SparseCubicSampler(item))
+                .Cast<ICurveSampler<Transforms.SparseWeight8>>();
+
+            return new FastSampler<Transforms.SparseWeight8>(split);
+        }
+
         #endregion
     }
 
@@ -239,6 +249,16 @@ namespace SharpGLTF.Animations
             return _Sequence.ToDictionary(pair => pair.Item1, pair => pair.Item2);
         }
 
+        public ICurveSampler<float[]> ToFastSampler()
+        {
+            var split = _Sequence
+                .SplitByTime()
+                .Select(item => new ArrayCubicSampler(item))
+                .Cast<ICurveSampler<float[]>>();
+
+            return new FastSampler<float[]>(split);
+        }
+
         #endregion
     }
 }

+ 22 - 0
src/SharpGLTF.Core/Animations/LinearSamplers.cs

@@ -191,6 +191,17 @@ namespace SharpGLTF.Animations
             throw new NotImplementedException();
         }
 
+        public ICurveSampler<Transforms.SparseWeight8> ToFastSampler()
+        {
+            var linear = _Linear;
+            var split = _Sequence
+                .SplitByTime()
+                .Select(item => new SparseLinearSampler(item, linear))
+                .Cast<ICurveSampler<Transforms.SparseWeight8>>();
+
+            return new FastSampler<Transforms.SparseWeight8>(split);
+        }
+
         #endregion
     }
 
@@ -246,6 +257,17 @@ namespace SharpGLTF.Animations
             throw new NotImplementedException();
         }
 
+        public ICurveSampler<float[]> ToFastSampler()
+        {
+            var linear = _Linear;
+            var split = _Sequence
+                .SplitByTime()
+                .Select(item => new ArrayLinearSampler(item, linear))
+                .Cast<ICurveSampler<float[]>>();
+
+            return new FastSampler<float[]>(split);
+        }
+
         #endregion
     }
 }

+ 16 - 8
src/SharpGLTF.Core/Animations/SamplerFactory.cs

@@ -347,18 +347,22 @@ namespace SharpGLTF.Animations
             return optimize ? sampler.ToFastSampler() : sampler;
         }
 
-        public static ICurveSampler<Transforms.SparseWeight8> CreateSampler(this IEnumerable<(Single, Transforms.SparseWeight8)> collection, bool isLinear = true)
+        public static ICurveSampler<Transforms.SparseWeight8> CreateSampler(this IEnumerable<(Single, Transforms.SparseWeight8)> collection, bool isLinear = true, bool optimize = false)
         {
             if (collection == null) return null;
 
-            return new SparseLinearSampler(collection, isLinear);
+            var sampler = new SparseLinearSampler(collection, isLinear);
+
+            return optimize ? sampler.ToFastSampler() : sampler;
         }
 
-        public static ICurveSampler<Single[]> CreateSampler(this IEnumerable<(Single, Single[])> collection, bool isLinear = true)
+        public static ICurveSampler<Single[]> CreateSampler(this IEnumerable<(Single, Single[])> collection, bool isLinear = true, bool optimize = false)
         {
             if (collection == null) return null;
 
-            return new ArrayLinearSampler(collection, isLinear);
+            var sampler = new ArrayLinearSampler(collection, isLinear);
+
+            return optimize ? sampler.ToFastSampler() : sampler;
         }
 
         public static ICurveSampler<Vector3> CreateSampler(this IEnumerable<(Single, (Vector3, Vector3, Vector3))> collection, bool optimize = false)
@@ -379,18 +383,22 @@ namespace SharpGLTF.Animations
             return optimize ? sampler.ToFastSampler() : sampler;
         }
 
-        public static ICurveSampler<Transforms.SparseWeight8> CreateSampler(this IEnumerable<(Single, (Transforms.SparseWeight8, Transforms.SparseWeight8, Transforms.SparseWeight8))> collection)
+        public static ICurveSampler<Transforms.SparseWeight8> CreateSampler(this IEnumerable<(Single, (Transforms.SparseWeight8, Transforms.SparseWeight8, Transforms.SparseWeight8))> collection, bool optimize = false)
         {
             if (collection == null) return null;
 
-            return new SparseCubicSampler(collection);
+            var sampler = new SparseCubicSampler(collection);
+
+            return optimize ? sampler.ToFastSampler() : sampler;
         }
 
-        public static ICurveSampler<Single[]> CreateSampler(this IEnumerable<(Single, (Single[], Single[], Single[]))> collection)
+        public static ICurveSampler<Single[]> CreateSampler(this IEnumerable<(Single, (Single[], Single[], Single[]))> collection, bool optimize = false)
         {
             if (collection == null) return null;
 
-            return new ArrayCubicSampler(collection);
+            var sampler = new ArrayCubicSampler(collection);
+
+            return optimize ? sampler.ToFastSampler() : sampler;
         }
 
         #endregion

+ 34 - 50
src/SharpGLTF.Core/Schema2/gltf.Animations.cs

@@ -555,31 +555,27 @@ namespace SharpGLTF.Schema2
             _output = this._CreateOutputAccessor(kv.Item2, expandedCount).LogicalIndex;
         }
 
-        IEnumerable<(Single, Vector3)> IAnimationSampler<Vector3>.GetLinearKeys(bool isolateMemory)
+        IEnumerable<(Single, Vector3)> IAnimationSampler<Vector3>.GetLinearKeys()
         {
             Guard.IsFalse(this.InterpolationMode == AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
             var keys = this.Input.AsScalarArray();
             var frames = this.Output.AsVector3Array();
 
-            return keys
-                .Zip(frames, (key, val) => (key, val))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, val));
         }
 
-        IEnumerable<(Single, Quaternion)> IAnimationSampler<Quaternion>.GetLinearKeys(bool isolateMemory)
+        IEnumerable<(Single, Quaternion)> IAnimationSampler<Quaternion>.GetLinearKeys()
         {
             Guard.IsFalse(this.InterpolationMode == AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
             var keys = this.Input.AsScalarArray();
             var frames = this.Output.AsQuaternionArray();
 
-            return keys
-                .Zip(frames, (key, val) => (key, val))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, val));
         }
 
-        IEnumerable<(Single, SparseWeight8)> IAnimationSampler<SparseWeight8>.GetLinearKeys(bool isolateMemory)
+        IEnumerable<(Single, SparseWeight8)> IAnimationSampler<SparseWeight8>.GetLinearKeys()
         {
             Guard.IsFalse(this.InterpolationMode == AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
@@ -588,12 +584,10 @@ namespace SharpGLTF.Schema2
             var keys = this.Input.AsScalarArray();
             var frames = this.Output.AsMultiArray(dimensions);
 
-            return keys
-                .Zip(frames, (key, val) => (key, SparseWeight8.Create(val)))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, SparseWeight8.Create(val)));
         }
 
-        IEnumerable<(Single, Single[])> IAnimationSampler<Single[]>.GetLinearKeys(bool isolateMemory)
+        IEnumerable<(Single, Single[])> IAnimationSampler<Single[]>.GetLinearKeys()
         {
             Guard.IsFalse(this.InterpolationMode == AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
@@ -602,36 +596,30 @@ namespace SharpGLTF.Schema2
             var keys = this.Input.AsScalarArray();
             var frames = this.Output.AsMultiArray(dimensions);
 
-            return keys
-                .Zip(frames, (key, val) => (key, val))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, val));
         }
 
-        IEnumerable<(Single, (Vector3, Vector3, Vector3))> IAnimationSampler<Vector3>.GetCubicKeys(bool isolateMemory)
+        IEnumerable<(Single, (Vector3, Vector3, Vector3))> IAnimationSampler<Vector3>.GetCubicKeys()
         {
             Guard.IsFalse(this.InterpolationMode != AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
             var keys = this.Input.AsScalarArray();
             var frames = _GroupByThree(this.Output.AsVector3Array());
 
-            return keys
-                .Zip(frames, (key, val) => (key, val))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, val));
         }
 
-        IEnumerable<(Single, (Quaternion, Quaternion, Quaternion))> IAnimationSampler<Quaternion>.GetCubicKeys(bool isolateMemory)
+        IEnumerable<(Single, (Quaternion, Quaternion, Quaternion))> IAnimationSampler<Quaternion>.GetCubicKeys()
         {
             Guard.IsFalse(this.InterpolationMode != AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
             var keys = this.Input.AsScalarArray();
             var frames = _GroupByThree(this.Output.AsQuaternionArray());
 
-            return keys
-                .Zip(frames, (key, val) => (key, val))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, val));
         }
 
-        IEnumerable<(Single, (Single[], Single[], Single[]))> IAnimationSampler<Single[]>.GetCubicKeys(bool isolateMemory)
+        IEnumerable<(Single, (Single[], Single[], Single[]))> IAnimationSampler<Single[]>.GetCubicKeys()
         {
             Guard.IsFalse(this.InterpolationMode != AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
@@ -640,12 +628,10 @@ namespace SharpGLTF.Schema2
             var keys = this.Input.AsScalarArray();
             var frames = _GroupByThree(this.Output.AsMultiArray(dimensions));
 
-            return keys
-                .Zip(frames, (key, val) => (key, val))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, val));
         }
 
-        IEnumerable<(Single, (SparseWeight8, SparseWeight8, SparseWeight8))> IAnimationSampler<SparseWeight8>.GetCubicKeys(bool isolateMemory)
+        IEnumerable<(Single, (SparseWeight8, SparseWeight8, SparseWeight8))> IAnimationSampler<SparseWeight8>.GetCubicKeys()
         {
             Guard.IsFalse(this.InterpolationMode != AnimationInterpolationMode.CUBICSPLINE, nameof(InterpolationMode));
 
@@ -654,9 +640,7 @@ namespace SharpGLTF.Schema2
             var keys = this.Input.AsScalarArray();
             var frames = _GroupByThree(this.Output.AsMultiArray(dimensions));
 
-            return keys
-                .Zip(frames, (key, val) => (key, (SparseWeight8.Create(val.Item1), SparseWeight8.Create(val.Item2), SparseWeight8.Create(val.Item3))))
-                .Isolate(isolateMemory);
+            return keys.Zip(frames, (key, val) => (key, (SparseWeight8.Create(val.Item1), SparseWeight8.Create(val.Item2), SparseWeight8.Create(val.Item3))));
         }
 
         private static IEnumerable<(T, T, T)> _GroupByThree<T>(IEnumerable<T> collection)
@@ -683,9 +667,9 @@ namespace SharpGLTF.Schema2
 
             switch (this.InterpolationMode)
             {
-                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(false, isolateMemory);
-                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(true, isolateMemory);
-                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys(isolateMemory).CreateSampler(isolateMemory);
+                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys().CreateSampler(false, isolateMemory);
+                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys().CreateSampler(true, isolateMemory);
+                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys().CreateSampler(isolateMemory);
             }
 
             throw new NotImplementedException();
@@ -697,37 +681,37 @@ namespace SharpGLTF.Schema2
 
             switch (this.InterpolationMode)
             {
-                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(false, isolateMemory);
-                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(true, isolateMemory);
-                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys(isolateMemory).CreateSampler(isolateMemory);
+                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys().CreateSampler(false, isolateMemory);
+                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys().CreateSampler(true, isolateMemory);
+                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys().CreateSampler(isolateMemory);
             }
 
             throw new NotImplementedException();
         }
 
-        ICurveSampler<Single[]> IAnimationSampler<Single[]>.CreateCurveSampler(bool isolateMemory)
+        ICurveSampler<SparseWeight8> IAnimationSampler<SparseWeight8>.CreateCurveSampler(bool isolateMemory)
         {
-            var xsampler = this as IAnimationSampler<Single[]>;
+            var xsampler = this as IAnimationSampler<SparseWeight8>;
 
             switch (this.InterpolationMode)
             {
-                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(false);
-                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(true);
-                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys(isolateMemory).CreateSampler();
+                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys().CreateSampler(false, isolateMemory);
+                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys().CreateSampler(true, isolateMemory);
+                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys().CreateSampler(isolateMemory);
             }
 
             throw new NotImplementedException();
         }
 
-        ICurveSampler<SparseWeight8> IAnimationSampler<SparseWeight8>.CreateCurveSampler(bool isolateMemory)
+        ICurveSampler<Single[]> IAnimationSampler<Single[]>.CreateCurveSampler(bool isolateMemory)
         {
-            var xsampler = this as IAnimationSampler<SparseWeight8>;
+            var xsampler = this as IAnimationSampler<Single[]>;
 
             switch (this.InterpolationMode)
             {
-                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(false);
-                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys(isolateMemory).CreateSampler(true);
-                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys(isolateMemory).CreateSampler();
+                case AnimationInterpolationMode.STEP: return xsampler.GetLinearKeys().CreateSampler(false, isolateMemory);
+                case AnimationInterpolationMode.LINEAR: return xsampler.GetLinearKeys().CreateSampler(true, isolateMemory);
+                case AnimationInterpolationMode.CUBICSPLINE: return xsampler.GetCubicKeys().CreateSampler(isolateMemory);
             }
 
             throw new NotImplementedException();
@@ -767,9 +751,9 @@ namespace SharpGLTF.Schema2
     {
         AnimationInterpolationMode InterpolationMode { get; }
 
-        IEnumerable<(Single, T)> GetLinearKeys(bool isolateMemory = false);
+        IEnumerable<(Single, T)> GetLinearKeys();
 
-        IEnumerable<(Single, (T, T, T))> GetCubicKeys(bool isolateMemory = false);
+        IEnumerable<(Single, (T, T, T))> GetCubicKeys();
 
         ICurveSampler<T> CreateCurveSampler(bool isolateMemory = false);
     }