MovieCameraFrame.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/test/unit_test.hpp>
  16. #include <OgreMath.h>
  17. #include <cstring>
  18. #include <vector>
  19. #include "core/MovieCameraFrame.h"
  20. namespace{
  21. // The last frame of the opening movie. It's the camera of the field md1stin, whose background
  22. // the movie ends with: position 28.4846 210.83 3.10751, orientation 0.742188 0.667516
  23. // -0.0403783 -0.0450658, and a vertical field of view of 34.5389 degrees.
  24. const std::vector<uint8_t> LAST_FRAME_OF_THE_OPENING = {
  25. 0xe2, 0x0f, 0x11, 0xfe, 0x00, 0x00, 0xcb, 0xff, 0x52, 0xfe,
  26. 0x16, 0xf0, 0xeb, 0x01, 0xcb, 0x0f, 0x4f, 0xfe, 0x4f, 0xfe,
  27. 0x93, 0xfe, 0xff, 0xff, 0xcf, 0x0c, 0x00, 0x00, 0x69, 0x96,
  28. 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x82, 0x01, 0x00, 0x00
  29. };
  30. }
  31. BOOST_AUTO_TEST_CASE(TestMovieCameraFrameWithoutCameraIsNotValid){
  32. const uint8_t frame[MovieCameraFrame::SIZE] = {};
  33. BOOST_CHECK(!MovieCameraFrame::Parse(frame, 1.0f).valid);
  34. // The projection alone isn't a camera.
  35. uint8_t projection_only[MovieCameraFrame::SIZE] = {};
  36. projection_only[36] = 0x82;
  37. projection_only[37] = 0x01;
  38. BOOST_CHECK(!MovieCameraFrame::Parse(projection_only, 1.0f).valid);
  39. }
  40. BOOST_AUTO_TEST_CASE(TestMovieCameraFrameIdentityRotation){
  41. // Identity rotation (4096 on the diagonal), no translation, focal length 386.
  42. uint8_t data[MovieCameraFrame::SIZE] = {};
  43. const int16_t one = 4096;
  44. for (int i = 0; i < 3; ++ i) std::memcpy(data + (i * 3 + i) * 2, &one, 2);
  45. const uint16_t focal = 386;
  46. std::memcpy(data + 36, &focal, 2);
  47. const MovieCameraFrame frame = MovieCameraFrame::Parse(data, 1.0f);
  48. BOOST_REQUIRE(frame.valid);
  49. BOOST_CHECK_SMALL(frame.position.length(), 0.0001f);
  50. // The field of view the field md1stin has with this focal length.
  51. BOOST_CHECK_CLOSE(frame.fov.valueDegrees(), 34.5389f, 0.01f);
  52. }
  53. BOOST_AUTO_TEST_CASE(TestMovieCameraFrameTranslationIsScaledByTheField){
  54. uint8_t data[MovieCameraFrame::SIZE] = {};
  55. const int16_t one = 4096;
  56. for (int i = 0; i < 3; ++ i) std::memcpy(data + (i * 3 + i) * 2, &one, 2);
  57. const int32_t translation = -128 * 10;
  58. std::memcpy(data + 20, &translation, 4);
  59. const uint16_t focal = 386;
  60. std::memcpy(data + 36, &focal, 2);
  61. // The position is the opposite of the translation, divided by 128 and by the field scale.
  62. BOOST_CHECK_CLOSE(MovieCameraFrame::Parse(data, 1.0f).position.x, 10.0f, 0.01f);
  63. BOOST_CHECK_CLOSE(MovieCameraFrame::Parse(data, 2.0f).position.x, 5.0f, 0.01f);
  64. }
  65. BOOST_AUTO_TEST_CASE(TestMovieCameraFrameLastFrameOfTheOpeningIsTheCameraOfTheField){
  66. const MovieCameraFrame frame = MovieCameraFrame::Parse(LAST_FRAME_OF_THE_OPENING.data(), 1.0f);
  67. BOOST_REQUIRE(frame.valid);
  68. // The values the installer wrote for md1stin, to the precision of the camera data.
  69. BOOST_CHECK_CLOSE(frame.position.x, 28.4846f, 0.1f);
  70. BOOST_CHECK_CLOSE(frame.position.y, 210.83f, 0.1f);
  71. BOOST_CHECK_CLOSE(frame.position.z, 3.10751f, 3.0f);
  72. BOOST_CHECK_CLOSE(frame.fov.valueDegrees(), 34.5389f, 0.01f);
  73. const Ogre::Quaternion expected(0.742188f, 0.667516f, -0.0403783f, -0.0450658f);
  74. BOOST_CHECK(frame.orientation.equals(expected, Ogre::Radian(0.01f)));
  75. }