Skip to content

Commit

Permalink
Refactored math functions in UCloud9ToolsLibrary (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat authored Apr 13, 2024
1 parent 0996bb0 commit 9cf6a38
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 209 deletions.
Binary file modified Content/Maps/warmup.umap
Binary file not shown.
Binary file modified Content/Modes/BP_Cloud9Default.uasset
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "Cloud9SpringArmComponent.h"
#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"
#include "Cloud9/Tools/Cloud9ToolsLibrary.h"
#include "Cloud9/Tools/Extensions/FVector.h"

UCloud9SpringArmComponent::UCloud9SpringArmComponent()
{
Expand Down Expand Up @@ -112,13 +112,13 @@ void UCloud9SpringArmComponent::UpdateDesiredArmLocation(
LerpTarget += ArmMovementStep * LerpAmount;
RemainingTime -= LerpAmount;

DesiredLoc = UCloud9ToolsLibrary::VInterpTo(PreviousDesiredLoc, LerpTarget, LerpAmount, LocationLag);
DesiredLoc = EFVector::VInterpTo(PreviousDesiredLoc, LerpTarget, LerpAmount, LocationLag);
PreviousDesiredLoc = DesiredLoc;
}
}
else
{
DesiredLoc = UCloud9ToolsLibrary::VInterpTo(PreviousDesiredLoc, DesiredLoc, DeltaTime, LocationLag);
DesiredLoc = EFVector::VInterpTo(PreviousDesiredLoc, DesiredLoc, DeltaTime, LocationLag);
}

if (let FromOrigin = DesiredLoc - ArmOrigin;
Expand Down
5 changes: 3 additions & 2 deletions Source/Cloud9/Contollers/Cloud9MouseController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "Cloud9/Tools/Cloud9ToolsLibrary.h"
#include "Cloud9/Tools/Extensions/APlayerController.h"
#include "Cloud9/Tools/Math.h"
#include "Cloud9/Contollers/Cloud9PlayerController.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"

Expand Down Expand Up @@ -74,11 +75,11 @@ float UCloud9MouseController::GetCameraZoomHeightLevel() const
{
if (let Pawn = GetCloud9Pawn(); IsValid(Pawn))
{
let ZoomHeightLevel = UCloud9ToolsLibrary::InverseLerp(
let ZoomHeightLevel = Math::InverseLerp(
MinCameraZoomHeight,
MaxCameraZoomHeight,
Pawn->GetCameraZoomHeight());
let ZoomAngleLevel = UCloud9ToolsLibrary::InverseLerp(
let ZoomAngleLevel = Math::InverseLerp(
MinCameraZoomAngle,
MaxCameraZoomAngle,
Pawn->GetCameraRotationRoll());
Expand Down
38 changes: 0 additions & 38 deletions Source/Cloud9/Tools/Cloud9ToolsLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,44 +104,6 @@ void UCloud9ToolsLibrary::GetWidthHeightDepth(const FBox& Box, float& Width, flo
Depth = Size.Z;
}

FRotator UCloud9ToolsLibrary::RadiansToDegrees(const FRotator Rotator)
{
return {
FMath::RadiansToDegrees(Rotator.Pitch),
FMath::RadiansToDegrees(Rotator.Yaw),
FMath::RadiansToDegrees(Rotator.Roll)
};
}

FVector UCloud9ToolsLibrary::VInterpTo(
const FVector Current,
const FVector Target,
float DeltaTime,
const FVector InterpSpeed)
{
let ClampLerp = [](auto Current, auto Dist, auto Alpha, auto Target)
{
return Alpha <= 0.0f ? Target : Current + Dist * FMath::Clamp(Alpha, 0.0f, 1.0f);
};

// Distance to reach
let Dist = Target - Current;

// If distance is too small, just set the desired location
if (Dist.SizeSquared() < KINDA_SMALL_NUMBER)
{
return Target;
}

let Alpha = DeltaTime * InterpSpeed;

return {
ClampLerp(Current.X, Dist.X, Alpha.X, Target.X),
ClampLerp(Current.Y, Dist.Y, Alpha.Y, Target.Y),
ClampLerp(Current.Z, Dist.Z, Alpha.Z, Target.Z),
};
}

TArray<FString> UCloud9ToolsLibrary::GetObjectEditorProperties(UClass* Class)
{
if (IsValid(Class))
Expand Down
56 changes: 0 additions & 56 deletions Source/Cloud9/Tools/Cloud9ToolsLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,67 +94,11 @@ class CLOUD9_API UCloud9ToolsLibrary : public UBlueprintFunctionLibrary
return reinterpret_cast<TArray<Cls*>&>(Actors);
}

template <class T, class U>
static T InverseLerp(const T& A, const T& B, const U& X) { return static_cast<T>((X - A) / (B - A)); }

static FBox GetAccurateReferencePoseBounds(const USkeletalMesh* Mesh);

UFUNCTION(BlueprintCallable)
static void GetWidthHeightDepth(const FBox& Box, float& Width, float& Height, float& Depth);

static FRotator RadiansToDegrees(const FRotator Rotator);

static FVector VInterpTo(
const FVector Current,
const FVector Target,
float DeltaTime,
const FVector InterpSpeed);

UFUNCTION(BlueprintCallable)
static TArray<FString> GetObjectEditorProperties(UClass* Class);

// see mathlib.h from cstrike15_src

template <typename InputType, typename OutputType>
static constexpr InputType Select(InputType Value, OutputType A, OutputType B)
{
return Value < 0 ? A : B;
}

template <typename ValueType, typename InRangeType, typename OutRangeType>
static constexpr OutRangeType RemapValue(
ValueType Value,
InRangeType InRangeMin,
InRangeType InRangeMax,
OutRangeType OutRangeMin,
OutRangeType OutRangeMax)
{
if (InRangeMin == InRangeMax)
{
return Select(Value - InRangeMax, OutRangeMax, OutRangeMin);
}

let InRange = static_cast<OutRangeType>(InRangeMax - InRangeMin);
let OutRange = OutRangeMax - OutRangeMin;
return OutRangeMin + OutRange * static_cast<OutRangeType>(Value - InRangeMin) / InRange;
}

template <typename ValueType, typename InRangeType, typename OutRangeType>
static constexpr OutRangeType RemapValueClamped(
ValueType Value,
InRangeType InRangeMin,
InRangeType InRangeMax,
OutRangeType OutRangeMin,
OutRangeType OutRangeMax)
{
if (InRangeMin == InRangeMax)
{
return Select(Value - InRangeMax, OutRangeMax, OutRangeMin);
}

let InRange = static_cast<OutRangeType>(InRangeMax - InRangeMin);
let Temp = FMath::Clamp(static_cast<OutRangeType>(Value - InRangeMin) / InRange, 0.0f, 1.0f);
let OutRange = OutRangeMax - OutRangeMin;
return OutRangeMin + OutRange * Temp;
}
};
36 changes: 11 additions & 25 deletions Source/Cloud9/Tools/Extensions/FRotator.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Alexei Gladkikh
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -27,34 +27,20 @@
#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Tools/Concepts.h"

namespace EFVector
namespace EFRotator
{
struct Normalize
struct ToDegrees
{
template <typename SelfType> requires Concepts::convertiable<SelfType, FVector>
FORCEINLINE FVector operator()(SelfType&& Self) const
template <typename SelfType> requires Concepts::convertiable<SelfType, FRotator>
FORCEINLINE FRotator operator()(SelfType&& Self) const
{
FVector Normalized = Self;
Normalized.Normalize();
return Normalized;
return {
FMath::RadiansToDegrees(Self.Pitch),
FMath::RadiansToDegrees(Self.Yaw),
FMath::RadiansToDegrees(Self.Roll)
};
}

OPERATOR_BODY(Normalize)
OPERATOR_BODY(ToDegrees)
};

FVector VInterpTo(
const FVector Current,
const FVector Target,
float DeltaTime,
const FVector InterpSpeed);

inline FVector Random(FVector Min, FVector Max, FVector Grid)
{
let Vector = FMath::RandPointInBox({Min, Max});
return {
FMath::GridSnap(Vector.X, Grid.X),
FMath::GridSnap(Vector.Y, Grid.Y),
FMath::GridSnap(Vector.Z, Grid.Z),
};
}
}
29 changes: 2 additions & 27 deletions Source/Cloud9/Tools/Extensions/FVector.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Alexei Gladkikh
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand All @@ -23,25 +23,10 @@

#pragma once

#include "Cloud9/Tools/Macro/Operator.h"
#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Tools/Concepts.h"
#include "FVector.h"

namespace EFVector
{
struct Normalize
{
template <typename SelfType> requires Concepts::convertiable<SelfType, FVector>
FORCEINLINE FVector operator()(SelfType&& Self) const
{
FVector Normalized = Self;
Normalized.Normalize();
return Normalized;
}

OPERATOR_BODY(Normalize)
};

FVector VInterpTo(
const FVector Current,
const FVector Target,
Expand Down Expand Up @@ -70,14 +55,4 @@ namespace EFVector
ClampLerp(Current.Z, Dist.Z, Alpha.Z, Target.Z),
};
}

inline FVector Random(FVector Min, FVector Max, FVector Grid)
{
let Vector = FMath::RandPointInBox({Min, Max});
return {
FMath::GridSnap(Vector.X, Grid.X),
FMath::GridSnap(Vector.Y, Grid.Y),
FMath::GridSnap(Vector.Z, Grid.Z),
};
}
}
15 changes: 6 additions & 9 deletions Source/Cloud9/Tools/Extensions/FVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,15 @@ namespace EFVector
OPERATOR_BODY(Normalize)
};

inline FVector Random(FVector Min, FVector Max)
{
return {
FMath::RandRange(Min.X, Max.X),
FMath::RandRange(Min.Y, Max.Y),
FMath::RandRange(Min.Z, Max.Z),
};
}
FVector VInterpTo(
const FVector Current,
const FVector Target,
float DeltaTime,
const FVector InterpSpeed);

inline FVector Random(FVector Min, FVector Max, FVector Grid)
{
let Vector = Random(Min, Max);
let Vector = FMath::RandPointInBox({Min, Max});
return {
FMath::GridSnap(Vector.X, Grid.X),
FMath::GridSnap(Vector.Y, Grid.Y),
Expand Down
88 changes: 43 additions & 45 deletions Source/Cloud9/Tools/Math.h
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// Copyright (c) 2024 Alexei Gladkikh

#pragma once

/**
* std abstraction because UE4 has self APIs to avoid mixin std and UE4 APIs
*/
namespace Concepts
namespace Math
{
template <typename SelfType>
concept incrementable = requires(SelfType Self)
template <class T, class U>
T InverseLerp(const T& A, const T& B, const U& X) { return static_cast<T>((X - A) / (B - A)); }

// see mathlib.h from cstrike15_src

template <typename InputType, typename OutputType>
constexpr InputType Select(InputType Value, OutputType A, OutputType B)
{
++Self;
};
return Value < 0 ? A : B;
}

template <typename SelfType, typename OtherType>
concept plusassingable =
incrementable<SelfType>
&& requires(SelfType Self, OtherType Other)
template <typename ValueType, typename InRangeType, typename OutRangeType>
constexpr OutRangeType RemapValue(
ValueType Value,
InRangeType InRangeMin,
InRangeType InRangeMax,
OutRangeType OutRangeMin,
OutRangeType OutRangeMax)
{
if (InRangeMin == InRangeMax)
{
Self += Other;
};
return Select(Value - InRangeMax, OutRangeMax, OutRangeMin);
}

template <typename SelfType, typename BoundType>
concept dereferencable = requires(SelfType Self)
{
{ *Self } -> std::convertible_to<BoundType>;
};
let InRange = static_cast<OutRangeType>(InRangeMax - InRangeMin);
let OutRange = OutRangeMax - OutRangeMin;
return OutRangeMin + OutRange * static_cast<OutRangeType>(Value - InRangeMin) / InRange;
}

template <typename SelfType, typename BoundType>
concept convertiable = requires(SelfType Self)
template <typename ValueType, typename InRangeType, typename OutRangeType>
constexpr OutRangeType RemapValueClamped(
ValueType Value,
InRangeType InRangeMin,
InRangeType InRangeMax,
OutRangeType OutRangeMin,
OutRangeType OutRangeMax)
{
{ Self } -> std::convertible_to<BoundType>;
};
if (InRangeMin == InRangeMax)
{
return Select(Value - InRangeMax, OutRangeMax, OutRangeMin);
}

let InRange = static_cast<OutRangeType>(InRangeMax - InRangeMin);
let Temp = FMath::Clamp(static_cast<OutRangeType>(Value - InRangeMin) / InRange, 0.0f, 1.0f);
let OutRange = OutRangeMax - OutRangeMin;
return OutRangeMin + OutRange * Temp;
}
}
Loading

0 comments on commit 9cf6a38

Please sign in to comment.