test_primitives.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /**************************************************************************/
  2. /* test_primitives.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_PRIMITIVES_H
  31. #define TEST_PRIMITIVES_H
  32. #include "scene/resources/primitive_meshes.h"
  33. #include "tests/test_macros.h"
  34. namespace TestPrimitives {
  35. TEST_CASE("[SceneTree][Primitive][Capsule] Capsule Primitive") {
  36. Ref<CapsuleMesh> capsule = memnew(CapsuleMesh);
  37. SUBCASE("[SceneTree][Primitive][Capsule] Default values should be valid") {
  38. CHECK_MESSAGE(capsule->get_radius() > 0,
  39. "Radius of default capsule positive.");
  40. CHECK_MESSAGE(capsule->get_height() > 0,
  41. "Height of default capsule positive.");
  42. CHECK_MESSAGE(capsule->get_radial_segments() >= 0,
  43. "Radius Segments of default capsule positive.");
  44. CHECK_MESSAGE(capsule->get_rings() >= 0,
  45. "Number of rings of default capsule positive.");
  46. }
  47. SUBCASE("[SceneTree][Primitive][Capsule] Set properties of the capsule and get them with accessor methods") {
  48. capsule->set_height(7.1f);
  49. capsule->set_radius(1.3f);
  50. capsule->set_radial_segments(16);
  51. capsule->set_rings(32);
  52. CHECK_MESSAGE(capsule->get_radius() == doctest::Approx(1.3f),
  53. "Get/Set radius work with one set.");
  54. CHECK_MESSAGE(capsule->get_height() == doctest::Approx(7.1f),
  55. "Get/Set radius work with one set.");
  56. CHECK_MESSAGE(capsule->get_radial_segments() == 16,
  57. "Get/Set radius work with one set.");
  58. CHECK_MESSAGE(capsule->get_rings() == 32,
  59. "Get/Set radius work with one set.");
  60. }
  61. SUBCASE("[SceneTree][Primitive][Capsule] If set segments negative, default to at least 0") {
  62. capsule->set_radial_segments(-5);
  63. capsule->set_rings(-17);
  64. CHECK_MESSAGE(capsule->get_radial_segments() >= 0,
  65. "Ensure number of radial segments is >= 0.");
  66. CHECK_MESSAGE(capsule->get_rings() >= 0,
  67. "Ensure number of rings is >= 0.");
  68. }
  69. SUBCASE("[SceneTree][Primitive][Capsule] If set height < 2*radius, adjust radius and height to radius=height*0.5") {
  70. capsule->set_radius(1.f);
  71. capsule->set_height(0.5f);
  72. CHECK_MESSAGE(capsule->get_radius() >= capsule->get_height() * 0.5,
  73. "Ensure radius >= height * 0.5 (needed for capsule to exist).");
  74. }
  75. SUBCASE("[Primitive][Capsule] Check mesh is correct") {
  76. Array data{};
  77. data.resize(RS::ARRAY_MAX);
  78. float radius{ 0.5f };
  79. float height{ 4.f };
  80. int num_radial_segments{ 4 };
  81. int num_rings{ 8 };
  82. CapsuleMesh::create_mesh_array(data, radius, height, num_radial_segments, num_rings);
  83. Vector<Vector3> points = data[RS::ARRAY_VERTEX];
  84. SUBCASE("[Primitive][Capsule] Ensure all vertices positions are within bounding radius and height") {
  85. // Get mesh data
  86. // Check all points within radius of capsule
  87. float dist_to_yaxis = 0.f;
  88. for (Vector3 point : points) {
  89. float new_dist_to_y = point.x * point.x + point.z * point.z;
  90. if (new_dist_to_y > dist_to_yaxis)
  91. dist_to_yaxis = new_dist_to_y;
  92. }
  93. CHECK(dist_to_yaxis <= radius * radius);
  94. // Check highest point and lowest point are within height of each other
  95. float max_y{ 0.f };
  96. float min_y{ 0.f };
  97. for (Vector3 point : points) {
  98. if (point.y > max_y)
  99. max_y = point.y;
  100. if (point.y < min_y)
  101. min_y = point.y;
  102. }
  103. CHECK(max_y - min_y <= height);
  104. }
  105. SUBCASE("[Primitive][Capsule] If normal.y == 0, then mesh makes a cylinder.") {
  106. Vector<Vector3> normals = data[RS::ARRAY_NORMAL];
  107. for (int ii = 0; ii < points.size(); ++ii) {
  108. float point_dist_from_yaxis = Math::sqrt(points[ii].x * points[ii].x + points[ii].z * points[ii].z);
  109. Vector3 yaxis_to_point{ points[ii].x / point_dist_from_yaxis, 0.f, points[ii].z / point_dist_from_yaxis };
  110. if (normals[ii].y == 0.f) {
  111. float mag_of_normal = Math::sqrt(normals[ii].x * normals[ii].x + normals[ii].z * normals[ii].z);
  112. Vector3 normalized_normal = normals[ii] / mag_of_normal;
  113. CHECK_MESSAGE(point_dist_from_yaxis == doctest::Approx(radius),
  114. "Points on the tube of the capsule are radius away from y-axis.");
  115. CHECK_MESSAGE(normalized_normal.is_equal_approx(yaxis_to_point),
  116. "Normal points orthogonal from mid cylinder.");
  117. }
  118. }
  119. }
  120. }
  121. } // End capsule tests
  122. TEST_CASE("[SceneTree][Primitive][Box] Box Primitive") {
  123. Ref<BoxMesh> box = memnew(BoxMesh);
  124. SUBCASE("[SceneTree][Primitive][Box] Default values should be valid") {
  125. CHECK(box->get_size().x > 0);
  126. CHECK(box->get_size().y > 0);
  127. CHECK(box->get_size().z > 0);
  128. CHECK(box->get_subdivide_width() >= 0);
  129. CHECK(box->get_subdivide_height() >= 0);
  130. CHECK(box->get_subdivide_depth() >= 0);
  131. }
  132. SUBCASE("[SceneTree][Primitive][Box] Set properties and get them with accessor methods") {
  133. Vector3 size{ 2.1, 3.3, 1.7 };
  134. box->set_size(size);
  135. box->set_subdivide_width(3);
  136. box->set_subdivide_height(2);
  137. box->set_subdivide_depth(4);
  138. CHECK(box->get_size().is_equal_approx(size));
  139. CHECK(box->get_subdivide_width() == 3);
  140. CHECK(box->get_subdivide_height() == 2);
  141. CHECK(box->get_subdivide_depth() == 4);
  142. }
  143. SUBCASE("[SceneTree][Primitive][Box] Set subdivides to negative and ensure they are >= 0") {
  144. box->set_subdivide_width(-2);
  145. box->set_subdivide_height(-2);
  146. box->set_subdivide_depth(-2);
  147. CHECK(box->get_subdivide_width() >= 0);
  148. CHECK(box->get_subdivide_height() >= 0);
  149. CHECK(box->get_subdivide_depth() >= 0);
  150. }
  151. SUBCASE("[Primitive][Box] Check mesh is correct.") {
  152. Array data{};
  153. data.resize(RS::ARRAY_MAX);
  154. Vector3 size{ 0.5f, 1.2f, .9f };
  155. int subdivide_width{ 3 };
  156. int subdivide_height{ 2 };
  157. int subdivide_depth{ 8 };
  158. BoxMesh::create_mesh_array(data, size, subdivide_width, subdivide_height, subdivide_depth);
  159. Vector<Vector3> points = data[RS::ARRAY_VERTEX];
  160. Vector<Vector3> normals = data[RS::ARRAY_NORMAL];
  161. SUBCASE("Only 6 distinct normals.") {
  162. Vector<Vector3> distinct_normals{};
  163. distinct_normals.push_back(normals[0]);
  164. for (const Vector3 &normal : normals) {
  165. bool add_normal{ true };
  166. for (const Vector3 &vec : distinct_normals) {
  167. if (vec.is_equal_approx(normal))
  168. add_normal = false;
  169. }
  170. if (add_normal)
  171. distinct_normals.push_back(normal);
  172. }
  173. CHECK_MESSAGE(distinct_normals.size() == 6,
  174. "There are exactly 6 distinct normals in the mesh data.");
  175. // All normals are orthogonal, or pointing in same direction.
  176. bool normal_correct_direction{ true };
  177. for (int rowIndex = 0; rowIndex < distinct_normals.size(); ++rowIndex) {
  178. for (int colIndex = rowIndex + 1; colIndex < distinct_normals.size(); ++colIndex) {
  179. if (!Math::is_equal_approx(distinct_normals[rowIndex].normalized().dot(distinct_normals[colIndex].normalized()), 0) &&
  180. !Math::is_equal_approx(distinct_normals[rowIndex].normalized().dot(distinct_normals[colIndex].normalized()), 1) &&
  181. !Math::is_equal_approx(distinct_normals[rowIndex].normalized().dot(distinct_normals[colIndex].normalized()), -1)) {
  182. normal_correct_direction = false;
  183. break;
  184. }
  185. }
  186. if (!normal_correct_direction)
  187. break;
  188. }
  189. CHECK_MESSAGE(normal_correct_direction,
  190. "All normals are either orthogonal or colinear.");
  191. }
  192. }
  193. } // End box tests
  194. TEST_CASE("[SceneTree][Primitive][Cylinder] Cylinder Primitive") {
  195. Ref<CylinderMesh> cylinder = memnew(CylinderMesh);
  196. SUBCASE("[SceneTree][Primitive][Cylinder] Default values should be valid") {
  197. CHECK(cylinder->get_top_radius() > 0);
  198. CHECK(cylinder->get_bottom_radius() > 0);
  199. CHECK(cylinder->get_height() > 0);
  200. CHECK(cylinder->get_radial_segments() > 0);
  201. CHECK(cylinder->get_rings() > 0);
  202. }
  203. SUBCASE("[SceneTree][Primitive][Cylinder] Set properties and get them") {
  204. cylinder->set_top_radius(4.3f);
  205. cylinder->set_bottom_radius(1.2f);
  206. cylinder->set_height(9.77f);
  207. cylinder->set_radial_segments(12);
  208. cylinder->set_rings(16);
  209. cylinder->set_cap_top(false);
  210. cylinder->set_cap_bottom(false);
  211. CHECK(cylinder->get_top_radius() == doctest::Approx(4.3f));
  212. CHECK(cylinder->get_bottom_radius() == doctest::Approx(1.2f));
  213. CHECK(cylinder->get_height() == doctest::Approx(9.77f));
  214. CHECK(cylinder->get_radial_segments() == 12);
  215. CHECK(cylinder->get_rings() == 16);
  216. CHECK(!cylinder->is_cap_top());
  217. CHECK(!cylinder->is_cap_bottom());
  218. }
  219. SUBCASE("[SceneTree][Primitive][Cylinder] Ensure num segments is >= 0") {
  220. cylinder->set_radial_segments(-12);
  221. cylinder->set_rings(-16);
  222. CHECK(cylinder->get_radial_segments() >= 0);
  223. CHECK(cylinder->get_rings() >= 0);
  224. }
  225. SUBCASE("[Primitive][Cylinder] Actual cylinder mesh tests (top and bottom radius the same).") {
  226. Array data{};
  227. data.resize(RS::ARRAY_MAX);
  228. real_t radius = .9f;
  229. real_t height = 3.2f;
  230. int radial_segments = 8;
  231. int rings = 5;
  232. bool top_cap = true;
  233. bool bottom_cap = true;
  234. CylinderMesh::create_mesh_array(data, radius, radius, height, radial_segments, rings, top_cap, bottom_cap);
  235. Vector<Vector3> points = data[RS::ARRAY_VERTEX];
  236. Vector<Vector3> normals = data[RS::ARRAY_NORMAL];
  237. SUBCASE("[Primitive][Cylinder] Side points are radius away from y-axis.") {
  238. bool is_radius_correct{ true };
  239. for (int index = 0; index < normals.size(); ++index) {
  240. if (Math::is_equal_approx(normals[index].y, 0)) {
  241. if (!Math::is_equal_approx((points[index] - Vector3(0, points[index].y, 0)).length_squared(), radius * radius)) {
  242. is_radius_correct = false;
  243. break;
  244. }
  245. }
  246. }
  247. CHECK(is_radius_correct);
  248. }
  249. SUBCASE("[Primitive][Cylinder] Only possible normals point in direction of point or in positive/negative y direction.") {
  250. bool is_correct_normals{ true };
  251. for (int index = 0; index < normals.size(); ++index) {
  252. Vector3 yaxis_to_point = points[index] - Vector3(0.f, points[index].y, 0.f);
  253. Vector3 point_to_normal = normals[index].normalized() - yaxis_to_point.normalized();
  254. // std::cout << "<" << point_to_normal.x << ", " << point_to_normal.y << ", " << point_to_normal.z << ">\n";
  255. if (!(point_to_normal.is_equal_approx(Vector3(0, 0, 0))) &&
  256. (!Math::is_equal_approx(Math::abs(normals[index].normalized().y), 1))) {
  257. is_correct_normals = false;
  258. break;
  259. }
  260. }
  261. CHECK(is_correct_normals);
  262. }
  263. SUBCASE("[Primitive][Cylinder] Points on top and bottom are height/2 away from origin.") {
  264. bool is_height_correct{ true };
  265. real_t half_height = 0.5 * height;
  266. for (int index = 0; index < normals.size(); ++index) {
  267. if (Math::is_equal_approx(normals[index].x, 0) &&
  268. Math::is_equal_approx(normals[index].z, 0) &&
  269. normals[index].y > 0) {
  270. if (!Math::is_equal_approx(points[index].y, half_height)) {
  271. is_height_correct = false;
  272. break;
  273. }
  274. }
  275. if (Math::is_equal_approx(normals[index].x, 0) &&
  276. Math::is_equal_approx(normals[index].z, 0) &&
  277. normals[index].y < 0) {
  278. if (!Math::is_equal_approx(points[index].y, -half_height)) {
  279. is_height_correct = false;
  280. break;
  281. }
  282. }
  283. }
  284. CHECK(is_height_correct);
  285. }
  286. SUBCASE("[Primitive][Cylinder] Does mesh obey cap parameters?") {
  287. CylinderMesh::create_mesh_array(data, radius, radius, height, radial_segments, rings, top_cap, false);
  288. points = data[RS::ARRAY_VERTEX];
  289. normals = data[RS::ARRAY_NORMAL];
  290. bool no_bottom_cap{ true };
  291. for (int index = 0; index < normals.size(); ++index) {
  292. if (Math::is_equal_approx(normals[index].x, 0) &&
  293. Math::is_equal_approx(normals[index].z, 0) &&
  294. normals[index].y < 0) {
  295. no_bottom_cap = false;
  296. break;
  297. }
  298. }
  299. CHECK_MESSAGE(no_bottom_cap,
  300. "Check there is no bottom cap.");
  301. CylinderMesh::create_mesh_array(data, radius, radius, height, radial_segments, rings, false, bottom_cap);
  302. points = data[RS::ARRAY_VERTEX];
  303. normals = data[RS::ARRAY_NORMAL];
  304. bool no_top_cap{ true };
  305. for (int index = 0; index < normals.size(); ++index) {
  306. if (Math::is_equal_approx(normals[index].x, 0) &&
  307. Math::is_equal_approx(normals[index].z, 0) &&
  308. normals[index].y > 0) {
  309. no_top_cap = false;
  310. break;
  311. }
  312. }
  313. CHECK_MESSAGE(no_top_cap,
  314. "Check there is no top cap.");
  315. }
  316. }
  317. SUBCASE("[Primitive][Cylinder] Slanted cylinder mesh (top and bottom radius different).") {
  318. Array data{};
  319. data.resize(RS::ARRAY_MAX);
  320. real_t top_radius = 2.f;
  321. real_t bottom_radius = 1.f;
  322. real_t height = 1.f;
  323. int radial_segments = 8;
  324. int rings = 5;
  325. CylinderMesh::create_mesh_array(data, top_radius, bottom_radius, height, radial_segments, rings, false, false);
  326. Vector<Vector3> points = data[RS::ARRAY_VERTEX];
  327. Vector<Vector3> normals = data[RS::ARRAY_NORMAL];
  328. SUBCASE("[Primitive][Cylinder] Side points lie correct distance from y-axis") {
  329. bool is_radius_correct{ true };
  330. for (int index = 0; index < points.size(); ++index) {
  331. real_t radius = ((top_radius - bottom_radius) / height) * (points[index].y - 0.5 * height) + top_radius;
  332. Vector3 distance_to_yaxis = points[index] - Vector3(0.f, points[index].y, 0.f);
  333. if (!Math::is_equal_approx(distance_to_yaxis.length_squared(), radius * radius)) {
  334. is_radius_correct = false;
  335. break;
  336. }
  337. }
  338. CHECK(is_radius_correct);
  339. }
  340. SUBCASE("[Primitive][Cylinder] Normal on side is orthogonal to side tangent vector") {
  341. bool is_normal_correct{ true };
  342. for (int index = 0; index < points.size(); ++index) {
  343. Vector3 yaxis_to_point = points[index] - Vector3(0.f, points[index].y, 0.f);
  344. Vector3 yaxis_to_rb = yaxis_to_point.normalized() * bottom_radius;
  345. Vector3 rb_to_point = yaxis_to_point - yaxis_to_rb;
  346. Vector3 y_to_bottom = -Vector3(0.f, points[index].y + 0.5 * height, 0.f);
  347. Vector3 side_tangent = rb_to_point - y_to_bottom;
  348. if (!Math::is_equal_approx(normals[index].dot(side_tangent), 0)) {
  349. is_normal_correct = false;
  350. break;
  351. }
  352. }
  353. CHECK(is_normal_correct);
  354. }
  355. }
  356. } // End cylinder tests
  357. TEST_CASE("[SceneTree][Primitive][Plane] Plane Primitive") {
  358. Ref<PlaneMesh> plane = memnew(PlaneMesh);
  359. SUBCASE("[SceneTree][Primitive][Plane] Default values should be valid") {
  360. CHECK(plane->get_size().x > 0);
  361. CHECK(plane->get_size().y > 0);
  362. CHECK(plane->get_subdivide_width() >= 0);
  363. CHECK(plane->get_subdivide_depth() >= 0);
  364. CHECK((plane->get_orientation() == PlaneMesh::FACE_X || plane->get_orientation() == PlaneMesh::FACE_Y || plane->get_orientation() == PlaneMesh::FACE_Z));
  365. }
  366. SUBCASE("[SceneTree][Primitive][Plane] Set properties and get them.") {
  367. Size2 size{ 3.2, 1.8 };
  368. Vector3 offset{ -7.3, 0.4, -1.7 };
  369. plane->set_size(size);
  370. plane->set_subdivide_width(15);
  371. plane->set_subdivide_depth(29);
  372. plane->set_center_offset(offset);
  373. plane->set_orientation(PlaneMesh::FACE_X);
  374. CHECK(plane->get_size().is_equal_approx(size));
  375. CHECK(plane->get_subdivide_width() == 15);
  376. CHECK(plane->get_subdivide_depth() == 29);
  377. CHECK(plane->get_center_offset().is_equal_approx(offset));
  378. CHECK(plane->get_orientation() == PlaneMesh::FACE_X);
  379. }
  380. SUBCASE("[SceneTree][Primitive][Plane] Ensure number of segments is >= 0.") {
  381. plane->set_subdivide_width(-15);
  382. plane->set_subdivide_depth(-29);
  383. CHECK(plane->get_subdivide_width() >= 0);
  384. CHECK(plane->get_subdivide_depth() >= 0);
  385. }
  386. }
  387. TEST_CASE("[SceneTree][Primitive][Quad] QuadMesh Primitive") {
  388. Ref<QuadMesh> quad = memnew(QuadMesh);
  389. SUBCASE("[Primitive][Quad] Orientation on initialization is in z direction") {
  390. CHECK(quad->get_orientation() == PlaneMesh::FACE_Z);
  391. }
  392. }
  393. TEST_CASE("[SceneTree][Primitive][Prism] Prism Primitive") {
  394. Ref<PrismMesh> prism = memnew(PrismMesh);
  395. SUBCASE("[Primitive][Prism] There are valid values of properties on initialization.") {
  396. CHECK(prism->get_left_to_right() >= 0);
  397. CHECK(prism->get_size().x >= 0);
  398. CHECK(prism->get_size().y >= 0);
  399. CHECK(prism->get_size().z >= 0);
  400. CHECK(prism->get_subdivide_width() >= 0);
  401. CHECK(prism->get_subdivide_height() >= 0);
  402. CHECK(prism->get_subdivide_depth() >= 0);
  403. }
  404. SUBCASE("[Primitive][Prism] Are able to change prism properties.") {
  405. Vector3 size{ 4.3, 9.1, 0.43 };
  406. prism->set_left_to_right(3.4f);
  407. prism->set_size(size);
  408. prism->set_subdivide_width(36);
  409. prism->set_subdivide_height(5);
  410. prism->set_subdivide_depth(64);
  411. CHECK(prism->get_left_to_right() == doctest::Approx(3.4f));
  412. CHECK(prism->get_size().is_equal_approx(size));
  413. CHECK(prism->get_subdivide_width() == 36);
  414. CHECK(prism->get_subdivide_height() == 5);
  415. CHECK(prism->get_subdivide_depth() == 64);
  416. }
  417. SUBCASE("[Primitive][Prism] Ensure number of segments always >= 0") {
  418. prism->set_subdivide_width(-36);
  419. prism->set_subdivide_height(-5);
  420. prism->set_subdivide_depth(-64);
  421. CHECK(prism->get_subdivide_width() >= 0);
  422. CHECK(prism->get_subdivide_height() >= 0);
  423. CHECK(prism->get_subdivide_depth() >= 0);
  424. }
  425. }
  426. TEST_CASE("[SceneTree][Primitive][Sphere] Sphere Primitive") {
  427. Ref<SphereMesh> sphere = memnew(SphereMesh);
  428. SUBCASE("[Primitive][Sphere] There are valid values of properties on initialization.") {
  429. CHECK(sphere->get_radius() >= 0);
  430. CHECK(sphere->get_height() >= 0);
  431. CHECK(sphere->get_radial_segments() >= 0);
  432. CHECK(sphere->get_rings() >= 0);
  433. }
  434. SUBCASE("[Primitive][Sphere] Are able to change prism properties.") {
  435. sphere->set_radius(3.4f);
  436. sphere->set_height(2.2f);
  437. sphere->set_radial_segments(36);
  438. sphere->set_rings(5);
  439. sphere->set_is_hemisphere(true);
  440. CHECK(sphere->get_radius() == doctest::Approx(3.4f));
  441. CHECK(sphere->get_height() == doctest::Approx(2.2f));
  442. CHECK(sphere->get_radial_segments() == 36);
  443. CHECK(sphere->get_rings() == 5);
  444. CHECK(sphere->get_is_hemisphere());
  445. }
  446. SUBCASE("[Primitive][Sphere] Ensure number of segments always >= 0") {
  447. sphere->set_radial_segments(-36);
  448. sphere->set_rings(-5);
  449. CHECK(sphere->get_radial_segments() >= 0);
  450. CHECK(sphere->get_rings() >= 0);
  451. }
  452. SUBCASE("[Primitive][Sphere] Sphere mesh tests.") {
  453. Array data{};
  454. data.resize(RS::ARRAY_MAX);
  455. real_t radius = 1.1f;
  456. int radial_segments = 8;
  457. int rings = 5;
  458. SphereMesh::create_mesh_array(data, radius, 2 * radius, radial_segments, rings);
  459. Vector<Vector3> points = data[RS::ARRAY_VERTEX];
  460. Vector<Vector3> normals = data[RS::ARRAY_NORMAL];
  461. SUBCASE("[Primitive][Sphere] All points lie radius away from origin.") {
  462. bool is_radius_correct = true;
  463. for (Vector3 point : points) {
  464. if (!Math::is_equal_approx(point.length_squared(), radius * radius)) {
  465. is_radius_correct = false;
  466. break;
  467. }
  468. }
  469. CHECK(is_radius_correct);
  470. }
  471. SUBCASE("[Primitive][Sphere] All normals lie in direction of corresponding point.") {
  472. bool is_normals_correct = true;
  473. for (int index = 0; index < points.size(); ++index) {
  474. if (!Math::is_equal_approx(normals[index].normalized().dot(points[index].normalized()), 1)) {
  475. is_normals_correct = false;
  476. break;
  477. }
  478. }
  479. CHECK(is_normals_correct);
  480. }
  481. }
  482. }
  483. TEST_CASE("[SceneTree][Primitive][Torus] Torus Primitive") {
  484. Ref<TorusMesh> torus = memnew(TorusMesh);
  485. Ref<PrimitiveMesh> prim = memnew(PrimitiveMesh);
  486. SUBCASE("[Primitive][Torus] There are valid values of properties on initialization.") {
  487. CHECK(torus->get_inner_radius() > 0);
  488. CHECK(torus->get_outer_radius() > 0);
  489. CHECK(torus->get_rings() >= 0);
  490. CHECK(torus->get_ring_segments() >= 0);
  491. }
  492. SUBCASE("[Primitive][Torus] Are able to change properties.") {
  493. torus->set_inner_radius(3.2f);
  494. torus->set_outer_radius(9.5f);
  495. torus->set_rings(19);
  496. torus->set_ring_segments(43);
  497. CHECK(torus->get_inner_radius() == doctest::Approx(3.2f));
  498. CHECK(torus->get_outer_radius() == doctest::Approx(9.5f));
  499. CHECK(torus->get_rings() == 19);
  500. CHECK(torus->get_ring_segments() == 43);
  501. }
  502. }
  503. TEST_CASE("[SceneTree][Primitive][TubeTrail] TubeTrail Primitive") {
  504. Ref<TubeTrailMesh> tube = memnew(TubeTrailMesh);
  505. SUBCASE("[Primitive][TubeTrail] There are valid values of properties on initialization.") {
  506. CHECK(tube->get_radius() > 0);
  507. CHECK(tube->get_radial_steps() >= 0);
  508. CHECK(tube->get_sections() >= 0);
  509. CHECK(tube->get_section_length() > 0);
  510. CHECK(tube->get_section_rings() >= 0);
  511. CHECK(tube->get_curve() == nullptr);
  512. CHECK(tube->get_builtin_bind_pose_count() >= 0);
  513. }
  514. SUBCASE("[Primitive][TubeTrail] Are able to change properties.") {
  515. tube->set_radius(7.2f);
  516. tube->set_radial_steps(9);
  517. tube->set_sections(33);
  518. tube->set_section_length(5.5f);
  519. tube->set_section_rings(12);
  520. Ref<Curve> curve = memnew(Curve);
  521. tube->set_curve(curve);
  522. CHECK(tube->get_radius() == doctest::Approx(7.2f));
  523. CHECK(tube->get_section_length() == doctest::Approx(5.5f));
  524. CHECK(tube->get_radial_steps() == 9);
  525. CHECK(tube->get_sections() == 33);
  526. CHECK(tube->get_section_rings() == 12);
  527. CHECK(tube->get_curve() == curve);
  528. }
  529. SUBCASE("[Primitive][TubeTrail] Setting same curve more than once, it remains the same.") {
  530. Ref<Curve> curve = memnew(Curve);
  531. tube->set_curve(curve);
  532. tube->set_curve(curve);
  533. tube->set_curve(curve);
  534. CHECK(tube->get_curve() == curve);
  535. }
  536. SUBCASE("[Primitive][TubeTrail] Setting curve, then changing to different curve.") {
  537. Ref<Curve> curve1 = memnew(Curve);
  538. Ref<Curve> curve2 = memnew(Curve);
  539. tube->set_curve(curve1);
  540. CHECK(tube->get_curve() == curve1);
  541. tube->set_curve(curve2);
  542. CHECK(tube->get_curve() == curve2);
  543. }
  544. SUBCASE("[Primitive][TubeTrail] Assign same curve to two different tube trails") {
  545. Ref<TubeTrailMesh> tube2 = memnew(TubeTrailMesh);
  546. Ref<Curve> curve = memnew(Curve);
  547. tube->set_curve(curve);
  548. tube2->set_curve(curve);
  549. CHECK(tube->get_curve() == curve);
  550. CHECK(tube2->get_curve() == curve);
  551. }
  552. }
  553. TEST_CASE("[SceneTree][Primitive][RibbonTrail] RibbonTrail Primitive") {
  554. Ref<RibbonTrailMesh> ribbon = memnew(RibbonTrailMesh);
  555. SUBCASE("[Primitive][RibbonTrail] There are valid values of properties on initialization.") {
  556. CHECK(ribbon->get_size() > 0);
  557. CHECK(ribbon->get_sections() >= 0);
  558. CHECK(ribbon->get_section_length() > 0);
  559. CHECK(ribbon->get_section_segments() >= 0);
  560. CHECK(ribbon->get_builtin_bind_pose_count() >= 0);
  561. CHECK(ribbon->get_curve() == nullptr);
  562. CHECK((ribbon->get_shape() == RibbonTrailMesh::SHAPE_CROSS ||
  563. ribbon->get_shape() == RibbonTrailMesh::SHAPE_FLAT));
  564. }
  565. SUBCASE("[Primitive][RibbonTrail] Able to change properties.") {
  566. Ref<Curve> curve = memnew(Curve);
  567. ribbon->set_size(4.3f);
  568. ribbon->set_sections(16);
  569. ribbon->set_section_length(1.3f);
  570. ribbon->set_section_segments(9);
  571. ribbon->set_curve(curve);
  572. CHECK(ribbon->get_size() == doctest::Approx(4.3f));
  573. CHECK(ribbon->get_section_length() == doctest::Approx(1.3f));
  574. CHECK(ribbon->get_sections() == 16);
  575. CHECK(ribbon->get_section_segments() == 9);
  576. CHECK(ribbon->get_curve() == curve);
  577. }
  578. SUBCASE("[Primitive][RibbonTrail] Setting same curve more than once, it remains the same.") {
  579. Ref<Curve> curve = memnew(Curve);
  580. ribbon->set_curve(curve);
  581. ribbon->set_curve(curve);
  582. ribbon->set_curve(curve);
  583. CHECK(ribbon->get_curve() == curve);
  584. }
  585. SUBCASE("[Primitive][RibbonTrail] Setting curve, then changing to different curve.") {
  586. Ref<Curve> curve1 = memnew(Curve);
  587. Ref<Curve> curve2 = memnew(Curve);
  588. ribbon->set_curve(curve1);
  589. CHECK(ribbon->get_curve() == curve1);
  590. ribbon->set_curve(curve2);
  591. CHECK(ribbon->get_curve() == curve2);
  592. }
  593. SUBCASE("[Primitive][RibbonTrail] Assign same curve to two different ribbon trails") {
  594. Ref<RibbonTrailMesh> ribbon2 = memnew(RibbonTrailMesh);
  595. Ref<Curve> curve = memnew(Curve);
  596. ribbon->set_curve(curve);
  597. ribbon2->set_curve(curve);
  598. CHECK(ribbon->get_curve() == curve);
  599. CHECK(ribbon2->get_curve() == curve);
  600. }
  601. }
  602. TEST_CASE("[SceneTree][Primitive][Text] Text Primitive") {
  603. Ref<TextMesh> text = memnew(TextMesh);
  604. SUBCASE("[Primitive][Text] There are valid values of properties on initialization.") {
  605. CHECK((text->get_horizontal_alignment() == HORIZONTAL_ALIGNMENT_CENTER ||
  606. text->get_horizontal_alignment() == HORIZONTAL_ALIGNMENT_LEFT ||
  607. text->get_horizontal_alignment() == HORIZONTAL_ALIGNMENT_RIGHT ||
  608. text->get_horizontal_alignment() == HORIZONTAL_ALIGNMENT_FILL));
  609. CHECK((text->get_vertical_alignment() == VERTICAL_ALIGNMENT_BOTTOM ||
  610. text->get_vertical_alignment() == VERTICAL_ALIGNMENT_TOP ||
  611. text->get_vertical_alignment() == VERTICAL_ALIGNMENT_CENTER ||
  612. text->get_vertical_alignment() == VERTICAL_ALIGNMENT_FILL));
  613. CHECK(text->get_font() == nullptr);
  614. CHECK(text->get_font_size() > 0);
  615. CHECK(text->get_line_spacing() >= 0);
  616. CHECK((text->get_autowrap_mode() == TextServer::AUTOWRAP_OFF ||
  617. text->get_autowrap_mode() == TextServer::AUTOWRAP_ARBITRARY ||
  618. text->get_autowrap_mode() == TextServer::AUTOWRAP_WORD ||
  619. text->get_autowrap_mode() == TextServer::AUTOWRAP_WORD_SMART));
  620. CHECK((text->get_text_direction() == TextServer::DIRECTION_AUTO ||
  621. text->get_text_direction() == TextServer::DIRECTION_LTR ||
  622. text->get_text_direction() == TextServer::DIRECTION_RTL));
  623. CHECK((text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_DEFAULT ||
  624. text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_URI ||
  625. text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_FILE ||
  626. text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_EMAIL ||
  627. text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_LIST ||
  628. text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_GDSCRIPT ||
  629. text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_CUSTOM));
  630. CHECK(text->get_structured_text_bidi_override_options().size() >= 0);
  631. CHECK(text->get_width() > 0);
  632. CHECK(text->get_depth() > 0);
  633. CHECK(text->get_curve_step() > 0);
  634. CHECK(text->get_pixel_size() > 0);
  635. }
  636. SUBCASE("[Primitive][Text] Change the properties of the mesh.") {
  637. Ref<Font> font = memnew(Font);
  638. Array options{};
  639. Point2 offset{ 30.8, 104.23 };
  640. text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  641. text->set_vertical_alignment(VERTICAL_ALIGNMENT_BOTTOM);
  642. text->set_text("Hello");
  643. text->set_font(font);
  644. text->set_font_size(12);
  645. text->set_line_spacing(1.7f);
  646. text->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  647. text->set_text_direction(TextServer::DIRECTION_RTL);
  648. text->set_language("French");
  649. text->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_EMAIL);
  650. text->set_structured_text_bidi_override_options(options);
  651. text->set_uppercase(true);
  652. real_t width{ 0.6 };
  653. real_t depth{ 1.7 };
  654. real_t pixel_size{ 2.8 };
  655. real_t curve_step{ 4.8 };
  656. text->set_width(width);
  657. text->set_depth(depth);
  658. text->set_curve_step(curve_step);
  659. text->set_pixel_size(pixel_size);
  660. text->set_offset(offset);
  661. CHECK(text->get_horizontal_alignment() == HORIZONTAL_ALIGNMENT_RIGHT);
  662. CHECK(text->get_vertical_alignment() == VERTICAL_ALIGNMENT_BOTTOM);
  663. CHECK(text->get_text_direction() == TextServer::DIRECTION_RTL);
  664. CHECK(text->get_text() == "Hello");
  665. CHECK(text->get_font() == font);
  666. CHECK(text->get_font_size() == 12);
  667. CHECK(text->get_autowrap_mode() == TextServer::AUTOWRAP_WORD_SMART);
  668. CHECK(text->get_language() == "French");
  669. CHECK(text->get_structured_text_bidi_override() == TextServer::STRUCTURED_TEXT_EMAIL);
  670. CHECK(text->get_structured_text_bidi_override_options() == options);
  671. CHECK(text->is_uppercase() == true);
  672. CHECK(text->get_offset() == offset);
  673. CHECK(text->get_line_spacing() == doctest::Approx(1.7f));
  674. CHECK(text->get_width() == doctest::Approx(width));
  675. CHECK(text->get_depth() == doctest::Approx(depth));
  676. CHECK(text->get_curve_step() == doctest::Approx(curve_step));
  677. CHECK(text->get_pixel_size() == doctest::Approx(pixel_size));
  678. }
  679. SUBCASE("[Primitive][Text] Set objects multiple times.") {
  680. Ref<Font> font = memnew(Font);
  681. Array options{};
  682. Point2 offset{ 30.8, 104.23 };
  683. text->set_font(font);
  684. text->set_font(font);
  685. text->set_font(font);
  686. text->set_structured_text_bidi_override_options(options);
  687. text->set_structured_text_bidi_override_options(options);
  688. text->set_structured_text_bidi_override_options(options);
  689. text->set_offset(offset);
  690. text->set_offset(offset);
  691. text->set_offset(offset);
  692. CHECK(text->get_font() == font);
  693. CHECK(text->get_structured_text_bidi_override_options() == options);
  694. CHECK(text->get_offset() == offset);
  695. }
  696. SUBCASE("[Primitive][Text] Set then change objects.") {
  697. Ref<Font> font1 = memnew(Font);
  698. Ref<Font> font2 = memnew(Font);
  699. Array options1{};
  700. Array options2{};
  701. Point2 offset1{ 30.8, 104.23 };
  702. Point2 offset2{ -30.8, -104.23 };
  703. text->set_font(font1);
  704. text->set_structured_text_bidi_override_options(options1);
  705. text->set_offset(offset1);
  706. CHECK(text->get_font() == font1);
  707. CHECK(text->get_structured_text_bidi_override_options() == options1);
  708. CHECK(text->get_offset() == offset1);
  709. text->set_font(font2);
  710. text->set_structured_text_bidi_override_options(options2);
  711. text->set_offset(offset2);
  712. CHECK(text->get_font() == font2);
  713. CHECK(text->get_structured_text_bidi_override_options() == options2);
  714. CHECK(text->get_offset() == offset2);
  715. }
  716. SUBCASE("[Primitive][Text] Assign same font to two Textmeshes.") {
  717. Ref<TextMesh> text2 = memnew(TextMesh);
  718. Ref<Font> font = memnew(Font);
  719. text->set_font(font);
  720. text2->set_font(font);
  721. CHECK(text->get_font() == font);
  722. CHECK(text2->get_font() == font);
  723. }
  724. }
  725. } // namespace TestPrimitives
  726. #endif // TEST_PRIMITIVES_H