Skip to content

Commit

Permalink
Merge pull request mordentral#50 from Stilton0502/Master
Browse files Browse the repository at this point in the history
Fixed Euro Pass snapping because of Engine's new double precision
Already went over in discord as a solution to precision issues
  • Loading branch information
mordentral authored Apr 13, 2024
2 parents b7e01c4 + e89eed2 commit 5c1947c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void FBPEuroLowPassFilter::ResetSmoothingFilter()
DeltaFilter.bFirstTime = true;
}

FVector FBPEuroLowPassFilter::RunFilterSmoothing(const FVector &InRawValue, const float &InDeltaTime)
FVector FBPEuroLowPassFilter::RunFilterSmoothing(const FVector& InRawValue, const float& InDeltaTime)
{
if (InDeltaTime <= 0.0f)
{
Expand All @@ -113,7 +113,7 @@ FVector FBPEuroLowPassFilter::RunFilterSmoothing(const FVector &InRawValue, cons
}

// Calculate the delta, if this is the first time then there is no delta
const FVector Delta = RawFilter.bFirstTime == true ? FVector::ZeroVector : (InRawValue - RawFilter.PreviousRaw) * 1.0f / InDeltaTime;
const FVector Delta = RawFilter.bFirstTime == true ? FVector::ZeroVector : (InRawValue - RawFilter.PreviousRaw) * 1.0 / InDeltaTime;

// Filter the delta to get the estimated
const FVector Estimated = DeltaFilter.Filter(Delta, FVector(DeltaFilter.CalculateAlphaTau(DeltaCutoff, InDeltaTime)));
Expand Down Expand Up @@ -154,11 +154,11 @@ FQuat FBPEuroLowPassFilterQuat::RunFilterSmoothing(const FQuat& InRawValue, cons

if (!RawFilter.bFirstTime)
{
Delta = (NewInVal - RawFilter.PreviousRaw) * (1.0f / InDeltaTime);
Delta = (NewInVal - RawFilter.PreviousRaw) * (1.0 / InDeltaTime);
}


float AlphaTau = DeltaFilter.CalculateAlphaTau(DeltaCutoff, InDeltaTime);
double AlphaTau = DeltaFilter.CalculateAlphaTau(DeltaCutoff, InDeltaTime);
FQuat AlphaTauQ(AlphaTau, AlphaTau, AlphaTau, AlphaTau);
const FQuat Estimated = DeltaFilter.Filter(Delta, AlphaTauQ);

Expand Down Expand Up @@ -198,7 +198,7 @@ FTransform FBPEuroLowPassFilterTrans::RunFilterSmoothing(const FTransform& InRaw
// Calculate the delta, if this is the first time then there is no delta
FTransform Delta = FTransform::Identity;

float Frequency = 1.0f / InDeltaTime;
double Frequency = 1.0 / InDeltaTime;
if (!RawFilter.bFirstTime)
{
Delta.SetLocation((NewInVal.GetLocation() - RawFilter.PreviousRaw.GetLocation()) * Frequency);
Expand All @@ -207,7 +207,7 @@ FTransform FBPEuroLowPassFilterTrans::RunFilterSmoothing(const FTransform& InRaw
}


float AlphaTau = DeltaFilter.CalculateAlphaTau(DeltaCutoff, InDeltaTime);
double AlphaTau = DeltaFilter.CalculateAlphaTau(DeltaCutoff, InDeltaTime);
FTransform AlphaTauQ(FQuat(AlphaTau, AlphaTau, AlphaTau, AlphaTau), FVector(AlphaTau), FVector(AlphaTau));
const FTransform Estimated = DeltaFilter.Filter(Delta, AlphaTauQ);

Expand All @@ -218,4 +218,4 @@ FTransform FBPEuroLowPassFilterTrans::RunFilterSmoothing(const FTransform& InRaw
NewTrans.NormalizeRotation();
// Filter passed value
return NewTrans;
}
}
66 changes: 33 additions & 33 deletions VRExpansionPlugin/Source/VRExpansionPlugin/Public/VRBPDatatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ class FBasicLowPassFilter
if (!bFirstTime)
{
// This is unsafe in non float / float array data types, but I am not going to be using any like that
for (int i = 0; i < sizeof(filterType)/sizeof(float); i++)
for (int i = 0; i < sizeof(filterType) / sizeof(double); i++)
{
((float*)&Result)[i] = ((float*)&InAlpha)[i] * ((float*)&InValue)[i] + (1.0f - ((float*)&InAlpha)[i]) * ((float*)&Previous)[i];
((double*)&Result)[i] = ((double*)&InAlpha)[i] * ((double*)&InValue)[i] + (1.0f - ((double*)&InAlpha)[i]) * ((double*)&Previous)[i];
}
}

Expand All @@ -209,15 +209,15 @@ class FBasicLowPassFilter
/** If this is the first time doing a filter */
bool bFirstTime;

//private:
//private:

const filterType CalculateCutoff(const filterType& InValue, float& MinCutoff, float& CutoffSlope)
const filterType CalculateCutoff(const filterType& InValue, double& MinCutoff, double& CutoffSlope)
{
filterType Result;
// This is unsafe in non float / float array data types, but I am not going to be using any like that
for (int i = 0; i < sizeof(filterType)/sizeof(float); i++)
for (int i = 0; i < sizeof(filterType) / sizeof(double); i++)
{
((float*)&Result)[i] = MinCutoff + CutoffSlope * FMath::Abs(((float*)&InValue)[i]);
((double*)&Result)[i] = MinCutoff + CutoffSlope * FMath::Abs(((double*)&InValue)[i]);
}
return Result;
}
Expand All @@ -226,17 +226,17 @@ class FBasicLowPassFilter
{
filterType Result;
// This is unsafe in non float / float array data types, but I am not going to be using any like that
for (int i = 0; i < sizeof(filterType)/sizeof(float); i++)
for (int i = 0; i < sizeof(filterType) / sizeof(double); i++)
{
((float*)&Result)[i] = CalculateAlphaTau(((float*)&InCutoff)[i], InDeltaTime);
((double*)&Result)[i] = CalculateAlphaTau(((double*)&InCutoff)[i], InDeltaTime);
}
return Result;
}

inline const float CalculateAlphaTau(const float InCutoff, const double InDeltaTime)
inline const double CalculateAlphaTau(const double InCutoff, const double InDeltaTime)
{
const float tau = 1.0 / (2.0f * PI * InCutoff);
return 1.0f / (1.0f + tau / InDeltaTime);
const double tau = 1.0 / (2.0 * PI * InCutoff);
return 1.0 / (1.0 + tau / InDeltaTime);
}
};

Expand All @@ -254,14 +254,14 @@ struct VREXPANSIONPLUGIN_API FBPEuroLowPassFilter

/** Default constructor */
FBPEuroLowPassFilter() :
MinCutoff(0.9f),
DeltaCutoff(1.0f),
CutoffSlope(0.007f),
MinCutoff(0.9),
DeltaCutoff(1.0),
CutoffSlope(0.007),
RawFilter(FVector::ZeroVector),
DeltaFilter(FVector::ZeroVector)
{}

FBPEuroLowPassFilter(const float InMinCutoff, const float InCutoffSlope, const float InDeltaCutoff) :
FBPEuroLowPassFilter(const double InMinCutoff, const double InCutoffSlope, const double InDeltaCutoff) :
MinCutoff(InMinCutoff),
DeltaCutoff(InDeltaCutoff),
CutoffSlope(InCutoffSlope),
Expand All @@ -271,21 +271,21 @@ struct VREXPANSIONPLUGIN_API FBPEuroLowPassFilter

// The smaller the value the less jitter and the more lag with micro movements
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float MinCutoff;
double MinCutoff;

// If latency is too high with fast movements increase this value
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float DeltaCutoff;
double DeltaCutoff;

// This is the magnitude of adjustment
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float CutoffSlope;
double CutoffSlope;


void ResetSmoothingFilter();

/** Smooth vector */
FVector RunFilterSmoothing(const FVector &InRawValue, const float &InDeltaTime);
FVector RunFilterSmoothing(const FVector& InRawValue, const float& InDeltaTime);

private:

Expand All @@ -308,14 +308,14 @@ struct VREXPANSIONPLUGIN_API FBPEuroLowPassFilterQuat

/** Default constructor */
FBPEuroLowPassFilterQuat() :
MinCutoff(0.9f),
DeltaCutoff(1.0f),
CutoffSlope(0.007f),
MinCutoff(0.9),
DeltaCutoff(1.0),
CutoffSlope(0.007),
RawFilter(FQuat::Identity),
DeltaFilter(FQuat::Identity)
{}

FBPEuroLowPassFilterQuat(const float InMinCutoff, const float InCutoffSlope, const float InDeltaCutoff) :
FBPEuroLowPassFilterQuat(const double InMinCutoff, const double InCutoffSlope, const double InDeltaCutoff) :
MinCutoff(InMinCutoff),
DeltaCutoff(InDeltaCutoff),
CutoffSlope(InCutoffSlope),
Expand All @@ -325,15 +325,15 @@ struct VREXPANSIONPLUGIN_API FBPEuroLowPassFilterQuat

// The smaller the value the less jitter and the more lag with micro movements
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float MinCutoff;
double MinCutoff;

// If latency is too high with fast movements increase this value
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float DeltaCutoff;
double DeltaCutoff;

// This is the magnitude of adjustment
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float CutoffSlope;
double CutoffSlope;

void ResetSmoothingFilter();

Expand Down Expand Up @@ -361,14 +361,14 @@ struct VREXPANSIONPLUGIN_API FBPEuroLowPassFilterTrans

/** Default constructor */
FBPEuroLowPassFilterTrans() :
MinCutoff(0.1f),
DeltaCutoff(10.0f),
CutoffSlope(10.0f),
MinCutoff(0.1),
DeltaCutoff(10.0),
CutoffSlope(10.0),
RawFilter(FTransform::Identity),
DeltaFilter(FTransform::Identity)
{}

FBPEuroLowPassFilterTrans(const float InMinCutoff, const float InCutoffSlope, const float InDeltaCutoff) :
FBPEuroLowPassFilterTrans(const double InMinCutoff, const double InCutoffSlope, const double InDeltaCutoff) :
MinCutoff(InMinCutoff),
DeltaCutoff(InDeltaCutoff),
CutoffSlope(InCutoffSlope),
Expand All @@ -378,15 +378,15 @@ struct VREXPANSIONPLUGIN_API FBPEuroLowPassFilterTrans

// The smaller the value the less jitter and the more lag with micro movements
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float MinCutoff;
double MinCutoff;

// If latency is too high with fast movements increase this value
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float DeltaCutoff;
double DeltaCutoff;

// This is the magnitude of adjustment
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "FilterSettings")
float CutoffSlope;
double CutoffSlope;

void ResetSmoothingFilter();

Expand Down

0 comments on commit 5c1947c

Please sign in to comment.