FeatureMatrix.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include <iostream>
  9. #include <fstream>
  10. #include <Allocators.h>
  11. #include <FeatureMatrix.h>
  12. #include <FeatureSchema.h>
  13. namespace EMotionFX::MotionMatching
  14. {
  15. AZ_CLASS_ALLOCATOR_IMPL(FeatureMatrix, MotionMatchAllocator)
  16. void FeatureMatrix::Clear()
  17. {
  18. resize(0, 0);
  19. }
  20. void FeatureMatrix::SaveAsCsv(const AZStd::string& filename, const AZStd::vector<AZStd::string>& columnNames) const
  21. {
  22. std::ofstream file(filename.c_str());
  23. // Save column names in the first row
  24. if (!columnNames.empty())
  25. {
  26. for (size_t i = 0; i < columnNames.size(); ++i)
  27. {
  28. if (i != 0)
  29. {
  30. file << ",";
  31. }
  32. file << columnNames[i].c_str();
  33. }
  34. file << "\n";
  35. }
  36. // Save coefficients
  37. #ifdef O3DE_USE_EIGEN
  38. // Force specify precision, else wise values close to 0.0 get rounded to 0.0.
  39. const static Eigen::IOFormat csvFormat(/*Eigen::StreamPrecision|FullPrecision*/8, Eigen::DontAlignCols, ", ", "\n");
  40. file << format(csvFormat);
  41. #endif
  42. }
  43. void FeatureMatrix::SaveAsCsv(const AZStd::string& filename, const FeatureSchema* featureSchema) const
  44. {
  45. const AZStd::vector<AZStd::string> columnNames = featureSchema->CollectColumnNames();
  46. SaveAsCsv(filename, columnNames);
  47. }
  48. AZ::Vector2 FeatureMatrix::GetVector2(Index row, Index startColumn) const
  49. {
  50. return AZ::Vector2(
  51. coeff(row, startColumn + 0),
  52. coeff(row, startColumn + 1));
  53. }
  54. void FeatureMatrix::SetVector2(Index row, Index startColumn, const AZ::Vector2& value)
  55. {
  56. operator()(row, startColumn + 0) = value.GetX();
  57. operator()(row, startColumn + 1) = value.GetY();
  58. }
  59. AZ::Vector3 FeatureMatrix::GetVector3(Index row, Index startColumn) const
  60. {
  61. return AZ::Vector3(
  62. coeff(row, startColumn + 0),
  63. coeff(row, startColumn + 1),
  64. coeff(row, startColumn + 2));
  65. }
  66. void FeatureMatrix::SetVector3(Index row, Index startColumn, const AZ::Vector3& value)
  67. {
  68. operator()(row, startColumn + 0) = value.GetX();
  69. operator()(row, startColumn + 1) = value.GetY();
  70. operator()(row, startColumn + 2) = value.GetZ();
  71. }
  72. size_t FeatureMatrix::CalcMemoryUsageInBytes() const
  73. {
  74. const size_t bytesPerValue = sizeof(O3DE_MM_FLOATTYPE);
  75. const size_t numValues = size();
  76. return numValues * bytesPerValue;
  77. }
  78. } // namespace EMotionFX::MotionMatching