Skip to content

Commit

Permalink
KeyFrameReducer に分ける
Browse files Browse the repository at this point in the history
  • Loading branch information
nasshu2916 committed Nov 16, 2024
1 parent a208564 commit 637b999
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
32 changes: 1 addition & 31 deletions Assets/ArtNet/Editor/DmxRecorder/TimelineConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,40 +182,10 @@ public void ThinOutUnchangedFrames()
for (var i = 0; i < ChannelDmxFrameData.Length; i++)
{
var dmxFrameData = ChannelDmxFrameData[i];
if (dmxFrameData.Count == 0) continue;

var latest = dmxFrameData[0];
var newDmxFrameData = new List<KeyFrameData> { dmxFrameData[0] };

for (var j = 1; j < dmxFrameData.Count - 1; j++)
{
var current = dmxFrameData[j];
var next = dmxFrameData[j + 1];
if (IsOmittedFrame(latest, current, next)) continue;

latest = current;
newDmxFrameData.Add(dmxFrameData[j]);
}

newDmxFrameData.Add(dmxFrameData[^1]);
ChannelDmxFrameData[i] = newDmxFrameData;
ChannelDmxFrameData[i] = KeyFrameReducer.Reduce(dmxFrameData);
}
}

private static bool IsOmittedFrame(
KeyFrameData prev,
KeyFrameData current,
KeyFrameData next,
float tolerance = 0.01f)
{
var prevDiff = current.Value - prev.Value;
var nextDiff = next.Value - current.Value;
var prevDiffTime = current.Time - prev.Time;
var nextDiffTime = next.Time - current.Time;

return Math.Abs(prevDiff / prevDiffTime - nextDiff / nextDiffTime) <= tolerance;
}

public IEnumerable<UniverseData> ToUniverseData()
{
var universeData = new List<UniverseData>();
Expand Down
43 changes: 43 additions & 0 deletions Assets/ArtNet/Editor/KeyFrameReducer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;

namespace ArtNet.Editor
{
public static class KeyFrameReducer
{
public static List<KeyFrameData> Reduce(List<KeyFrameData> keyFrameData)
{
if (keyFrameData.Count <= 2) return keyFrameData;

var newDmxFrameData = new List<KeyFrameData> { keyFrameData[0] };
var latest = keyFrameData[0];

for (var i = 1; i < keyFrameData.Count - 1; i++)
{
var current = keyFrameData[i];
var next = keyFrameData[i + 1];
if (IsOmittedFrame(latest, current, next)) continue;

newDmxFrameData.Add(current);
latest = current;
}

newDmxFrameData.Add(keyFrameData[^1]);
return newDmxFrameData;
}

private static bool IsOmittedFrame(
KeyFrameData prev,
KeyFrameData current,
KeyFrameData next,
float tolerance = 0.01f)
{
var prevDiffValue = current.Value - prev.Value;
var prevDiffTime = current.Time - prev.Time;
var nextDiffValue = next.Value - current.Value;
var nextDiffTime = next.Time - current.Time;

return Math.Abs(prevDiffValue / prevDiffTime - nextDiffValue / nextDiffTime) <= tolerance;
}
}
}
3 changes: 3 additions & 0 deletions Assets/ArtNet/Editor/KeyFrameReducer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 637b999

Please sign in to comment.