Path: blob/master/tests/scene/test_height_map_shape_3d.h
10277 views
/**************************************************************************/1/* test_height_map_shape_3d.h */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#pragma once3132#include "scene/resources/3d/height_map_shape_3d.h"33#include "scene/resources/image_texture.h"3435#include "tests/test_macros.h"36#include "tests/test_utils.h"3738namespace TestHeightMapShape3D {3940TEST_CASE("[SceneTree][HeightMapShape3D] Constructor") {41Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);42CHECK(height_map_shape->get_map_width() == 2);43CHECK(height_map_shape->get_map_depth() == 2);44CHECK(height_map_shape->get_map_data().size() == 4);45CHECK(height_map_shape->get_min_height() == 0.0);46CHECK(height_map_shape->get_max_height() == 0.0);47}4849TEST_CASE("[SceneTree][HeightMapShape3D] set_map_width and get_map_width") {50Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);51height_map_shape->set_map_width(10);52CHECK(height_map_shape->get_map_width() == 10);53}5455TEST_CASE("[SceneTree][HeightMapShape3D] set_map_depth and get_map_depth") {56Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);57height_map_shape->set_map_depth(15);58CHECK(height_map_shape->get_map_depth() == 15);59}6061TEST_CASE("[SceneTree][HeightMapShape3D] set_map_data and get_map_data") {62Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);63Vector<real_t> map_data;64map_data.push_back(1.0);65map_data.push_back(2.0);66height_map_shape->set_map_data(map_data);67CHECK(height_map_shape->get_map_data().size() == 4.0);68CHECK(height_map_shape->get_map_data()[0] == 0.0);69CHECK(height_map_shape->get_map_data()[1] == 0.0);70}7172TEST_CASE("[SceneTree][HeightMapShape3D] get_min_height") {73Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);74height_map_shape->set_map_width(3);75height_map_shape->set_map_depth(1);76height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });77CHECK(height_map_shape->get_min_height() == 0.5);78}7980TEST_CASE("[SceneTree][HeightMapShape3D] get_max_height") {81Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);82height_map_shape->set_map_width(3);83height_map_shape->set_map_depth(1);84height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });85CHECK(height_map_shape->get_max_height() == 2.0);86}8788TEST_CASE("[SceneTree][HeightMapShape3D] update_map_data_from_image") {89// Create a HeightMapShape3D instance.90Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);9192// Create a mock image with FORMAT_R8 and set its data.93Vector<uint8_t> image_data;94image_data.push_back(0);95image_data.push_back(128);96image_data.push_back(255);97image_data.push_back(64);9899Ref<Image> image = memnew(Image);100image->set_data(2, 2, false, Image::FORMAT_R8, image_data);101102height_map_shape->update_map_data_from_image(image, 0.0, 10.0);103104// Check the map data.105Vector<real_t> expected_map_data = { 0.0, 5.0, 10.0, 2.5 };106Vector<real_t> actual_map_data = height_map_shape->get_map_data();107real_t tolerance = 0.1;108109for (int i = 0; i < expected_map_data.size(); ++i) {110CHECK(Math::abs(actual_map_data[i] - expected_map_data[i]) < tolerance);111}112113// Check the min and max heights.114CHECK(height_map_shape->get_min_height() == 0.0);115CHECK(height_map_shape->get_max_height() == 10.0);116}117118} // namespace TestHeightMapShape3D119120121