BikeGameObjectDataLogger.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using UnityEngine;
  5. namespace Logging
  6. {
  7. public readonly struct BikeGameObjectDataLog
  8. {
  9. private readonly float timestamp;
  10. private readonly float positionX;
  11. private readonly float positionY;
  12. private readonly float positionZ;
  13. private readonly float rotationX;
  14. private readonly float rotationY;
  15. private readonly float rotationZ;
  16. private readonly float velocityX;
  17. private readonly float velocityY;
  18. private readonly float velocityZ;
  19. public BikeGameObjectDataLog(float timestamp, float positionX, float positionY, float positionZ, float rotationX, float rotationY, float rotationZ, float velocityX, float velocityY, float velocityZ)
  20. {
  21. this.timestamp = timestamp;
  22. this.positionX = positionX;
  23. this.positionY = positionY;
  24. this.positionZ = positionZ;
  25. this.rotationX = rotationX;
  26. this.rotationY = rotationY;
  27. this.rotationZ = rotationZ;
  28. this.velocityX = velocityX;
  29. this.velocityY = velocityY;
  30. this.velocityZ = velocityZ;
  31. }
  32. public string[] Serialize() => new[]
  33. {
  34. timestamp.ToString("F4", CultureInfo.InvariantCulture),
  35. positionX.ToString("F6", CultureInfo.InvariantCulture),
  36. positionY.ToString("F6", CultureInfo.InvariantCulture),
  37. positionZ.ToString("F6", CultureInfo.InvariantCulture),
  38. rotationX.ToString("F6", CultureInfo.InvariantCulture),
  39. rotationY.ToString("F6", CultureInfo.InvariantCulture),
  40. rotationZ.ToString("F6", CultureInfo.InvariantCulture),
  41. velocityX.ToString("F6", CultureInfo.InvariantCulture),
  42. velocityY.ToString("F6", CultureInfo.InvariantCulture),
  43. velocityZ.ToString("F6", CultureInfo.InvariantCulture),
  44. };
  45. }
  46. public class BikeGameObjectDataLogger : SensorDataLogger<BikeGameObjectDataLog>
  47. {
  48. public Rigidbody bike;
  49. private Transform bikeTransform;
  50. public override string Key => "bike_game_object_data";
  51. public override void Start()
  52. {
  53. base.Start();
  54. bikeTransform = bike.transform;
  55. }
  56. private void Update()
  57. {
  58. var pos = bikeTransform.position;
  59. var rot = bikeTransform.rotation.eulerAngles;
  60. var velocity = bike.velocity;
  61. Log(new BikeGameObjectDataLog(Time.time,
  62. pos.x, pos.y, pos.z,
  63. rot.x, rot.y, rot.z,
  64. velocity.x, velocity.y, velocity.z));
  65. }
  66. public override void Log(BikeGameObjectDataLog value)
  67. {
  68. Buffer.Add(value.Serialize());
  69. }
  70. public override IEnumerable<BikeGameObjectDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  71. {
  72. throw new System.NotImplementedException(); //TODO
  73. }
  74. }
  75. }