test_height_map_shape_3d.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #pragma once
  31. #include "scene/resources/3d/height_map_shape_3d.h"
  32. #include "scene/resources/image_texture.h"
  33. #include "tests/test_macros.h"
  34. #include "tests/test_utils.h"
  35. namespace TestHeightMapShape3D {
  36. TEST_CASE("[SceneTree][HeightMapShape3D] Constructor") {
  37. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  38. CHECK(height_map_shape->get_map_width() == 2);
  39. CHECK(height_map_shape->get_map_depth() == 2);
  40. CHECK(height_map_shape->get_map_data().size() == 4);
  41. CHECK(height_map_shape->get_min_height() == 0.0);
  42. CHECK(height_map_shape->get_max_height() == 0.0);
  43. }
  44. TEST_CASE("[SceneTree][HeightMapShape3D] set_map_width and get_map_width") {
  45. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  46. height_map_shape->set_map_width(10);
  47. CHECK(height_map_shape->get_map_width() == 10);
  48. }
  49. TEST_CASE("[SceneTree][HeightMapShape3D] set_map_depth and get_map_depth") {
  50. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  51. height_map_shape->set_map_depth(15);
  52. CHECK(height_map_shape->get_map_depth() == 15);
  53. }
  54. TEST_CASE("[SceneTree][HeightMapShape3D] set_map_data and get_map_data") {
  55. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  56. Vector<real_t> map_data;
  57. map_data.push_back(1.0);
  58. map_data.push_back(2.0);
  59. height_map_shape->set_map_data(map_data);
  60. CHECK(height_map_shape->get_map_data().size() == 4.0);
  61. CHECK(height_map_shape->get_map_data()[0] == 0.0);
  62. CHECK(height_map_shape->get_map_data()[1] == 0.0);
  63. }
  64. TEST_CASE("[SceneTree][HeightMapShape3D] get_min_height") {
  65. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  66. height_map_shape->set_map_width(3);
  67. height_map_shape->set_map_depth(1);
  68. height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });
  69. CHECK(height_map_shape->get_min_height() == 0.5);
  70. }
  71. TEST_CASE("[SceneTree][HeightMapShape3D] get_max_height") {
  72. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  73. height_map_shape->set_map_width(3);
  74. height_map_shape->set_map_depth(1);
  75. height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });
  76. CHECK(height_map_shape->get_max_height() == 2.0);
  77. }
  78. TEST_CASE("[SceneTree][HeightMapShape3D] update_map_data_from_image") {
  79. // Create a HeightMapShape3D instance.
  80. Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
  81. // Create a mock image with FORMAT_R8 and set its data.
  82. Vector<uint8_t> image_data;
  83. image_data.push_back(0);
  84. image_data.push_back(128);
  85. image_data.push_back(255);
  86. image_data.push_back(64);
  87. Ref<Image> image = memnew(Image);
  88. image->set_data(2, 2, false, Image::FORMAT_R8, image_data);
  89. height_map_shape->update_map_data_from_image(image, 0.0, 10.0);
  90. // Check the map data.
  91. Vector<real_t> expected_map_data = { 0.0, 5.0, 10.0, 2.5 };
  92. Vector<real_t> actual_map_data = height_map_shape->get_map_data();
  93. real_t tolerance = 0.1;
  94. for (int i = 0; i < expected_map_data.size(); ++i) {
  95. CHECK(Math::abs(actual_map_data[i] - expected_map_data[i]) < tolerance);
  96. }
  97. // Check the min and max heights.
  98. CHECK(height_map_shape->get_min_height() == 0.0);
  99. CHECK(height_map_shape->get_max_height() == 10.0);
  100. }
  101. } // namespace TestHeightMapShape3D