test_csg.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**************************************************************************/
  2. /* test_csg.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_CSG_H
  31. #define TEST_CSG_H
  32. #include "../csg.h"
  33. #include "../csg_shape.h"
  34. #include "tests/test_macros.h"
  35. namespace TestCSG {
  36. TEST_CASE("[SceneTree][CSG] CSGPolygon3D") {
  37. SUBCASE("[SceneTree][CSG] CSGPolygon3D: using accurate path tangent for polygon rotation") {
  38. const float polygon_radius = 10.0f;
  39. const Vector3 expected_min_bounds = Vector3(-polygon_radius, -polygon_radius, 0);
  40. const Vector3 expected_max_bounds = Vector3(100 + polygon_radius, polygon_radius, 100);
  41. const AABB expected_aabb = AABB(expected_min_bounds, expected_max_bounds - expected_min_bounds);
  42. Ref<Curve3D> curve;
  43. curve.instantiate();
  44. curve->add_point(
  45. // p_position
  46. Vector3(0, 0, 0),
  47. // p_in
  48. Vector3(),
  49. // p_out
  50. Vector3(0, 0, 60));
  51. curve->add_point(
  52. // p_position
  53. Vector3(100, 0, 100),
  54. // p_in
  55. Vector3(0, 0, -60),
  56. // p_out
  57. Vector3());
  58. Path3D *path = memnew(Path3D);
  59. path->set_curve(curve);
  60. CSGPolygon3D *csg_polygon_3d = memnew(CSGPolygon3D);
  61. SceneTree::get_singleton()->get_root()->add_child(csg_polygon_3d);
  62. csg_polygon_3d->add_child(path);
  63. csg_polygon_3d->set_path_node(csg_polygon_3d->get_path_to(path));
  64. csg_polygon_3d->set_mode(CSGPolygon3D::Mode::MODE_PATH);
  65. PackedVector2Array polygon;
  66. polygon.append(Vector2(-polygon_radius, 0));
  67. polygon.append(Vector2(0, polygon_radius));
  68. polygon.append(Vector2(polygon_radius, 0));
  69. polygon.append(Vector2(0, -polygon_radius));
  70. csg_polygon_3d->set_polygon(polygon);
  71. csg_polygon_3d->set_path_rotation(CSGPolygon3D::PathRotation::PATH_ROTATION_PATH);
  72. csg_polygon_3d->set_path_rotation_accurate(true);
  73. // Minimize the number of extrusions.
  74. // This decreases the number of samples taken from the curve.
  75. // Having fewer samples increases the inaccuracy of the line between samples as an approximation of the tangent of the curve.
  76. // With correct polygon orientation, the bounding box for the given curve should be independent of the number of extrusions.
  77. csg_polygon_3d->set_path_interval_type(CSGPolygon3D::PathIntervalType::PATH_INTERVAL_DISTANCE);
  78. csg_polygon_3d->set_path_interval(1000.0f);
  79. // Call get_brush_faces to force the bounding box to update.
  80. csg_polygon_3d->get_brush_faces();
  81. CHECK(csg_polygon_3d->get_aabb().is_equal_approx(expected_aabb));
  82. // Perform the bounding box check again with a greater number of extrusions.
  83. csg_polygon_3d->set_path_interval(1.0f);
  84. csg_polygon_3d->get_brush_faces();
  85. CHECK(csg_polygon_3d->get_aabb().is_equal_approx(expected_aabb));
  86. csg_polygon_3d->remove_child(path);
  87. SceneTree::get_singleton()->get_root()->remove_child(csg_polygon_3d);
  88. memdelete(csg_polygon_3d);
  89. memdelete(path);
  90. }
  91. }
  92. } // namespace TestCSG
  93. #endif // TEST_CSG_H