Path: blob/master/modules/gridmap/tests/test_grid_map.h
59211 views
/**************************************************************************/1/* test_grid_map.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 "../grid_map.h"3334#include "scene/main/scene_tree.h"35#include "scene/main/window.h"36#include "scene/resources/3d/primitive_meshes.h"37#include "tests/test_macros.h"3839namespace TestGridMap {4041TEST_CASE("[SceneTree][GridMap][MeshLibrary] Octant querying") {42Ref<MeshLibrary> mesh_library;43mesh_library.instantiate();4445int item1 = 0;46int item2 = 1;4748{49Vector<int> item_list = mesh_library->get_item_list();50CHECK(item_list.size() == 0);51}5253mesh_library->create_item(item1);54mesh_library->create_item(item2);5556int last_unused_item_id = mesh_library->get_last_unused_item_id();57CHECK(last_unused_item_id == 2);5859mesh_library->set_item_name(item1, "Item1");60mesh_library->set_item_name(item2, "Item2");6162Ref<BoxMesh> box_mesh;63box_mesh.instantiate();64box_mesh->set_size(Vector3(1.0, 1.0, 1.0));6566mesh_library->set_item_mesh(item1, box_mesh);67mesh_library->set_item_mesh(item2, box_mesh);6869{70Vector<int> item_list = mesh_library->get_item_list();71CHECK(item_list.size() == 2);72CHECK(item_list[0] == item1);73CHECK(item_list[1] == item2);74}7576GridMap *grid_map = memnew(GridMap);77grid_map->set_mesh_library(mesh_library);7879SceneTree::get_singleton()->get_root()->add_child(grid_map);8081grid_map->set_octant_size(8);82grid_map->set_cell_size(Vector3(1.0, 1.0, 1.0));8384grid_map->set_cell_item(Vector3i(0, 0, 0), -1);8586CHECK(grid_map->get_used_cells().size() == 0);87CHECK(grid_map->get_used_octants().size() == 0);8889grid_map->set_cell_item(Vector3i(0, 0, 0), item1);90grid_map->set_cell_item(Vector3i(7, 7, 7), item2);9192CHECK(grid_map->get_used_cells().size() == 2);93CHECK(grid_map->get_used_octants().size() == 1);94CHECK(grid_map->get_used_octants()[0] == Vector3i(0, 0, 0));9596grid_map->set_cell_item(Vector3i(8, 8, 8), item2);9798CHECK(grid_map->get_used_cells().size() == 3);99CHECK(grid_map->get_used_octants().size() == 2);100CHECK(grid_map->get_used_octants()[0] == Vector3i(0, 0, 0));101CHECK(grid_map->get_used_octants()[1] == Vector3i(1, 1, 1));102103AABB bounds;104bounds.position = Vector3(0.0, 0.0, 0.0);105bounds.size = Vector3(8.0, 8.0, 8.0);106107CHECK(grid_map->get_octants_in_bounds(bounds).size() == 1);108CHECK(grid_map->get_used_octants_in_bounds(bounds).size() == 1);109CHECK(grid_map->get_octants_in_bounds(bounds)[0] == Vector3i(0, 0, 0));110111// Edge margin is -CMP_EPSILON.112bounds.size = Vector3(8.001, 8.001, 8.001);113CHECK(grid_map->get_octants_in_bounds(bounds).size() == 8);114CHECK(grid_map->get_used_octants_in_bounds(bounds).size() == 2);115116SceneTree::get_singleton()->get_root()->remove_child(grid_map);117118memdelete(grid_map);119}120121} // namespace TestGridMap122123124