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