// Copyright Epic Games, Inc. All Rights Reserved. #include "MovieSceneInterpolatingPointsDrawTask.h" #include "Cache/MovieSceneCachedCurve.h" #include "Channels/MovieSceneDoubleChannel.h" #include "Channels/MovieSceneFloatChannel.h" #include "Channels/MovieScenePiecewiseCurve.h" namespace UE::MovieSceneTools { template struct FMovieSceneInterpolatingPointsDrawTask; template struct FMovieSceneInterpolatingPointsDrawTask; template FMovieSceneInterpolatingPointsDrawTask::FMovieSceneInterpolatingPointsDrawTask( const TSharedRef>& InCachedCurve, const bool bInInvertInterpolatingPointsY, const TFunction, TArray)>& InCallback) : Callback(InCallback) , ScreenSpace(InCachedCurve->GetScreenSpace()) , TickResolution(InCachedCurve->GetTickResolution()) , TimeThreshold(InCachedCurve->GetTimeThreshold()) , ValueThreshold(InCachedCurve->GetValueThreshold()) , PiecewiseCurve(InCachedCurve->GetPiecewiseCurve()) , bInvertInterpolatingPointsY(bInInvertInterpolatingPointsY) { const TArray& Times = InCachedCurve->GetTimes(); const TArray& Values = InCachedCurve->GetValues(); if (!ensureMsgf(Times.Num() > 1, TEXT("Curve paint tasks should only be created for curves with more than one key"))) { SetFlags(ECurvePainterTaskStateFlags::Completed); return; } // Remember key points KeyPoints.Reserve(Times.Num()); const double Sign = bInInvertInterpolatingPointsY ? -1.0 : 1.0; for (int32 DataIndex = 0; DataIndex < Times.Num(); DataIndex++) { KeyPoints.Emplace(Times[DataIndex] / TickResolution, Sign * double(Values[DataIndex].Value)); } InterpolatingPoints = KeyPoints; } template void FMovieSceneInterpolatingPointsDrawTask::RefineFullRangeInterpolatingPoints() { // Make sure there's no concurrency if (!AccessCurvePointsMutex.TryLock()) { return; } const int32 OldSize = InterpolatingPoints.Num(); RefineFullRangeInterpolatingPointsInternal(); const int32 NewSize = InterpolatingPoints.Num(); if (OldSize == NewSize) { InvokeCallback(); SetFlags(ECurvePainterTaskStateFlags::Completed); } AccessCurvePointsMutex.Unlock(); } template void FMovieSceneInterpolatingPointsDrawTask::SetFlags(ECurvePainterTaskStateFlags NewFlags) { StateFlags.store(NewFlags); } template bool FMovieSceneInterpolatingPointsDrawTask::HasAnyFlags(ECurvePainterTaskStateFlags Flags) const { return EnumHasAnyFlags(StateFlags.load(), Flags); } template void FMovieSceneInterpolatingPointsDrawTask::RefineFullRangeInterpolatingPointsInternal() { constexpr float InterpTimes[] = { 0.25f, 0.5f, 0.6f }; for (int32 Index = 0; Index < InterpolatingPoints.Num() - 1; Index++) { const FVector2D& Lower = InterpolatingPoints[Index]; const FVector2D& Upper = InterpolatingPoints[Index + 1]; if ((Upper.X - Lower.X) >= TimeThreshold) { bool bSegmentIsLinear = true; FVector2D Evaluated[UE_ARRAY_COUNT(InterpTimes)] = { FVector2D::ZeroVector }; for (int32 InterpIndex = 0; InterpIndex < UE_ARRAY_COUNT(InterpTimes); ++InterpIndex) { double& EvalTime = Evaluated[InterpIndex].X; EvalTime = FMath::Lerp(Lower.X, Upper.X, InterpTimes[InterpIndex]); double Value = 0.0; PiecewiseCurve->Evaluate(EvalTime * TickResolution, Value); if (bInvertInterpolatingPointsY) { Value = -Value; } const double LinearValue = FMath::Lerp(Lower.Y, Upper.Y, InterpTimes[InterpIndex]); if (bSegmentIsLinear) { bSegmentIsLinear = FMath::IsNearlyEqual(Value, LinearValue, ValueThreshold); } Evaluated[InterpIndex].Y = Value; } if (!bSegmentIsLinear) { // Add the point InterpolatingPoints.Insert(Evaluated, UE_ARRAY_COUNT(Evaluated), Index + 1); --Index; } } } } template void FMovieSceneInterpolatingPointsDrawTask::InvokeCallback() { TArray KeyOffsets; int32 Offset = 0; for (int32 InterpPointIndex = 0; InterpPointIndex < InterpolatingPoints.Num(); InterpPointIndex++) { if (KeyPoints.Num() <= Offset) { KeyOffsets.Add(InterpPointIndex); break; } else if (InterpolatingPoints[InterpPointIndex].X >= KeyPoints[Offset].X) { KeyOffsets.Add(InterpPointIndex); Offset++; } } Callback(InterpolatingPoints, KeyOffsets); } }