test_height_map_shape_3d.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**************************************************************************/
  2. /* test_height_map_shape_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_HEIGHT_MAP_SHAPE_3D_H
  31. #define TEST_HEIGHT_MAP_SHAPE_3D_H
  32. #include "scene/resources/3d/height_map_shape_3d.h"
  33. #include "scene/resources/image_texture.h"
  34. #include "tests/test_macros.h"
  35. #include "tests/test_utils.h"
  36. namespace TestHeightMapShape3D {
  37. TEST_CASE("[SceneTree][HeightMapShape3D] Constructor") {
  38. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  39. CHECK(height_map_shape->get_map_width() == 2);
  40. CHECK(height_map_shape->get_map_depth() == 2);
  41. CHECK(height_map_shape->get_map_data().size() == 4);
  42. CHECK(height_map_shape->get_min_height() == 0.0);
  43. CHECK(height_map_shape->get_max_height() == 0.0);
  44. }
  45. TEST_CASE("[SceneTree][HeightMapShape3D] set_map_width and get_map_width") {
  46. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  47. height_map_shape->set_map_width(10);
  48. CHECK(height_map_shape->get_map_width() == 10);
  49. }
  50. TEST_CASE("[SceneTree][HeightMapShape3D] set_map_depth and get_map_depth") {
  51. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  52. height_map_shape->set_map_depth(15);
  53. CHECK(height_map_shape->get_map_depth() == 15);
  54. }
  55. TEST_CASE("[SceneTree][HeightMapShape3D] set_map_data and get_map_data") {
  56. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  57. Vector<real_t> map_data;
  58. map_data.push_back(1.0);
  59. map_data.push_back(2.0);
  60. height_map_shape->set_map_data(map_data);
  61. CHECK(height_map_shape->get_map_data().size() == 4.0);
  62. CHECK(height_map_shape->get_map_data()[0] == 0.0);
  63. CHECK(height_map_shape->get_map_data()[1] == 0.0);
  64. }
  65. TEST_CASE("[SceneTree][HeightMapShape3D] get_min_height") {
  66. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  67. height_map_shape->set_map_width(3);
  68. height_map_shape->set_map_depth(1);
  69. height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });
  70. CHECK(height_map_shape->get_min_height() == 0.5);
  71. }
  72. TEST_CASE("[SceneTree][HeightMapShape3D] get_max_height") {
  73. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  74. height_map_shape->set_map_width(3);
  75. height_map_shape->set_map_depth(1);
  76. height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });
  77. CHECK(height_map_shape->get_max_height() == 2.0);
  78. }
  79. TEST_CASE("[SceneTree][HeightMapShape3D] update_map_data_from_image") {
  80. // Create a HeightMapShape3D instance.
  81. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  82. // Create a mock image with FORMAT_R8 and set its data.
  83. Vector<uint8_t> image_data;
  84. image_data.push_back(0);
  85. image_data.push_back(128);
  86. image_data.push_back(255);
  87. image_data.push_back(64);
  88. Ref<Image> image = memnew(Image);
  89. image->set_data(2, 2, false, Image::FORMAT_R8, image_data);
  90. height_map_shape->update_map_data_from_image(image, 0.0, 10.0);
  91. // Check the map data.
  92. Vector<real_t> expected_map_data = { 0.0, 5.0, 10.0, 2.5 };
  93. Vector<real_t> actual_map_data = height_map_shape->get_map_data();
  94. real_t tolerance = 0.1;
  95. for (int i = 0; i < expected_map_data.size(); ++i) {
  96. CHECK(Math::abs(actual_map_data[i] - expected_map_data[i]) < tolerance);
  97. }
  98. // Check the min and max heights.
  99. CHECK(height_map_shape->get_min_height() == 0.0);
  100. CHECK(height_map_shape->get_max_height() == 10.0);
  101. }
  102. } // namespace TestHeightMapShape3D
  103. #endif // TEST_HEIGHT_MAP_SHAPE_3D_H