FeatureMatrixMinMaxScaler.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <FeatureMatrix.h>
  10. #include <FeatureMatrixTransformer.h>
  11. namespace EMotionFX::MotionMatching
  12. {
  13. //! The min/max-scaler can be used to normalize the feature matrix, the query vector and other data.
  14. class MinMaxScaler
  15. : public FeatureMatrixTransformer
  16. {
  17. public:
  18. AZ_RTTI(MinMaxScaler, "{95D5BBA7-6144-4219-82F0-34C2DAB7DD3E}", FeatureMatrixTransformer);
  19. AZ_CLASS_ALLOCATOR_DECL
  20. MinMaxScaler() = default;
  21. bool Fit(const FeatureMatrix& featureMatrix, const Settings& settings = {}) override;
  22. // Normalize and scale the values.
  23. float Transform(float value, FeatureMatrix::Index column) const override;
  24. AZ::Vector2 Transform(const AZ::Vector2& value, FeatureMatrix::Index column) const override;
  25. AZ::Vector3 Transform(const AZ::Vector3& value, FeatureMatrix::Index column) const override;
  26. void Transform(AZStd::span<float> data) const override;
  27. FeatureMatrix Transform(const FeatureMatrix& featureMatrix) const override;
  28. // From normalized and scaled back to the original values.
  29. FeatureMatrix InverseTransform(const FeatureMatrix& featureMatrix) const override;
  30. AZ::Vector2 InverseTransform(const AZ::Vector2& value, FeatureMatrix::Index column) const override;
  31. AZ::Vector3 InverseTransform(const AZ::Vector3& value, FeatureMatrix::Index column) const override;
  32. float InverseTransform(float value, FeatureMatrix::Index column) const override;
  33. const AZStd::vector<float>& GetMin() const { return m_dataMin; }
  34. const AZStd::vector<float>& GetMax() const { return m_dataMax; }
  35. static constexpr float s_epsilon = AZ::Constants::FloatEpsilon;
  36. void SaveMinMaxAsCsv(const char* filename, const AZStd::vector<AZStd::string>& columnNames = {});
  37. private:
  38. AZStd::vector<float> m_dataMin; //!< Minimum value per column seen in the given input feature matrix.
  39. AZStd::vector<float> m_dataMax; //!< Maximum value per column seen in the given input feature matrix.
  40. AZStd::vector<float> m_dataRange; //!< Per column range (m_dataMax[col] - m_dataMin[col]) seen in the given input feature matrix.
  41. bool m_clip = false; //!< Clip transformed values out of feature range to provided feature range.
  42. //! Desired range of the transformed data.
  43. float m_featureMin = 0.0f;
  44. float m_featureMax = 1.0f;
  45. float m_featureRange = 1.0f;
  46. };
  47. } // namespace EMotionFX::MotionMatching