diff options
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/a_star.cpp | 52 | ||||
-rw-r--r-- | core/math/a_star.h | 12 | ||||
-rw-r--r-- | core/math/basis.cpp | 12 | ||||
-rw-r--r-- | core/math/basis.h | 3 | ||||
-rw-r--r-- | core/math/bsp_tree.cpp | 581 | ||||
-rw-r--r-- | core/math/bsp_tree.h | 159 | ||||
-rw-r--r-- | core/math/camera_matrix.cpp | 58 | ||||
-rw-r--r-- | core/math/camera_matrix.h | 19 | ||||
-rw-r--r-- | core/math/expression.cpp | 99 | ||||
-rw-r--r-- | core/math/expression.h | 3 | ||||
-rw-r--r-- | core/math/geometry.cpp | 49 | ||||
-rw-r--r-- | core/math/geometry.h | 16 | ||||
-rw-r--r-- | core/math/rect2.h | 5 | ||||
-rw-r--r-- | core/math/transform.h | 21 | ||||
-rw-r--r-- | core/math/transform_2d.h | 21 | ||||
-rw-r--r-- | core/math/triangle_mesh.cpp | 80 | ||||
-rw-r--r-- | core/math/triangle_mesh.h | 16 | ||||
-rw-r--r-- | core/math/vector2.h | 7 | ||||
-rw-r--r-- | core/math/vector3.cpp | 4 | ||||
-rw-r--r-- | core/math/vector3.h | 10 | ||||
-rw-r--r-- | core/math/vector3i.cpp | 55 | ||||
-rw-r--r-- | core/math/vector3i.h | 272 |
22 files changed, 607 insertions, 947 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index 73f190a330..847d4d8681 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -235,13 +235,13 @@ Array AStar::get_points() { return point_list; } -PoolVector<int> AStar::get_point_connections(int p_id) { +Vector<int> AStar::get_point_connections(int p_id) { Point *p; bool p_exists = points.lookup(p_id, p); - ERR_FAIL_COND_V(!p_exists, PoolVector<int>()); + ERR_FAIL_COND_V(!p_exists, Vector<int>()); - PoolVector<int> point_list; + Vector<int> point_list; for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { point_list.push_back((*it.key)); @@ -431,18 +431,18 @@ float AStar::_compute_cost(int p_from_id, int p_to_id) { return from_point->pos.distance_to(to_point->pos); } -PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { +Vector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { Point *a; bool from_exists = points.lookup(p_from_id, a); - ERR_FAIL_COND_V(!from_exists, PoolVector<Vector3>()); + ERR_FAIL_COND_V(!from_exists, Vector<Vector3>()); Point *b; bool to_exists = points.lookup(p_to_id, b); - ERR_FAIL_COND_V(!to_exists, PoolVector<Vector3>()); + ERR_FAIL_COND_V(!to_exists, Vector<Vector3>()); if (a == b) { - PoolVector<Vector3> ret; + Vector<Vector3> ret; ret.push_back(a->pos); return ret; } @@ -451,7 +451,7 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { Point *end_point = b; bool found_route = _solve(begin_point, end_point); - if (!found_route) return PoolVector<Vector3>(); + if (!found_route) return Vector<Vector3>(); Point *p = end_point; int pc = 1; // Begin point @@ -460,11 +460,11 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { p = p->prev_point; } - PoolVector<Vector3> path; + Vector<Vector3> path; path.resize(pc); { - PoolVector<Vector3>::Write w = path.write(); + Vector3 *w = path.ptrw(); Point *p2 = end_point; int idx = pc - 1; @@ -479,18 +479,18 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { return path; } -PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { +Vector<int> AStar::get_id_path(int p_from_id, int p_to_id) { Point *a; bool from_exists = points.lookup(p_from_id, a); - ERR_FAIL_COND_V(!from_exists, PoolVector<int>()); + ERR_FAIL_COND_V(!from_exists, Vector<int>()); Point *b; bool to_exists = points.lookup(p_to_id, b); - ERR_FAIL_COND_V(!to_exists, PoolVector<int>()); + ERR_FAIL_COND_V(!to_exists, Vector<int>()); if (a == b) { - PoolVector<int> ret; + Vector<int> ret; ret.push_back(a->id); return ret; } @@ -499,7 +499,7 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { Point *end_point = b; bool found_route = _solve(begin_point, end_point); - if (!found_route) return PoolVector<int>(); + if (!found_route) return Vector<int>(); Point *p = end_point; int pc = 1; // Begin point @@ -508,11 +508,11 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { p = p->prev_point; } - PoolVector<int> path; + Vector<int> path; path.resize(pc); { - PoolVector<int>::Write w = path.write(); + int *w = path.ptrw(); p = end_point; int idx = pc - 1; @@ -576,8 +576,8 @@ void AStar::_bind_methods() { ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path); ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path); - BIND_VMETHOD(MethodInfo(Variant::REAL, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id"))); - BIND_VMETHOD(MethodInfo(Variant::REAL, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id"))); + BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id"))); + BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id"))); } AStar::AStar() { @@ -624,7 +624,7 @@ bool AStar2D::has_point(int p_id) const { return astar.has_point(p_id); } -PoolVector<int> AStar2D::get_point_connections(int p_id) { +Vector<int> AStar2D::get_point_connections(int p_id) { return astar.get_point_connections(p_id); } @@ -677,15 +677,15 @@ Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const { return Vector2(p.x, p.y); } -PoolVector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { +Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { - PoolVector3Array pv = astar.get_point_path(p_from_id, p_to_id); + PackedVector3Array pv = astar.get_point_path(p_from_id, p_to_id); int size = pv.size(); - PoolVector2Array path; + PackedVector2Array path; path.resize(size); { - PoolVector<Vector3>::Read r = pv.read(); - PoolVector<Vector2>::Write w = path.write(); + const Vector3 *r = pv.ptr(); + Vector2 *w = path.ptrw(); for (int i = 0; i < size; i++) { Vector3 p = r[i]; w[i] = Vector2(p.x, p.y); @@ -694,7 +694,7 @@ PoolVector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { return path; } -PoolVector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) { +Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) { return astar.get_id_path(p_from_id, p_to_id); } diff --git a/core/math/a_star.h b/core/math/a_star.h index 0b10976932..bfcf0c09d3 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -137,7 +137,7 @@ public: void set_point_weight_scale(int p_id, real_t p_weight_scale); void remove_point(int p_id); bool has_point(int p_id) const; - PoolVector<int> get_point_connections(int p_id); + Vector<int> get_point_connections(int p_id); Array get_points(); void set_point_disabled(int p_id, bool p_disabled = true); @@ -155,8 +155,8 @@ public: int get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const; Vector3 get_closest_position_in_segment(const Vector3 &p_point) const; - PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id); - PoolVector<int> get_id_path(int p_from_id, int p_to_id); + Vector<Vector3> get_point_path(int p_from_id, int p_to_id); + Vector<int> get_id_path(int p_from_id, int p_to_id); AStar(); ~AStar(); @@ -179,7 +179,7 @@ public: void set_point_weight_scale(int p_id, real_t p_weight_scale); void remove_point(int p_id); bool has_point(int p_id) const; - PoolVector<int> get_point_connections(int p_id); + Vector<int> get_point_connections(int p_id); Array get_points(); void set_point_disabled(int p_id, bool p_disabled = true); @@ -197,8 +197,8 @@ public: int get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const; Vector2 get_closest_position_in_segment(const Vector2 &p_point) const; - PoolVector<Vector2> get_point_path(int p_from_id, int p_to_id); - PoolVector<int> get_id_path(int p_from_id, int p_to_id); + Vector<Vector2> get_point_path(int p_from_id, int p_to_id); + Vector<int> get_id_path(int p_from_id, int p_to_id); AStar2D(); ~AStar2D(); diff --git a/core/math/basis.cpp b/core/math/basis.cpp index ddf5f13d55..14079f811d 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -244,6 +244,18 @@ void Basis::scale_local(const Vector3 &p_scale) { *this = scaled_local(p_scale); } +float Basis::get_uniform_scale() const { + return (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0; +} + +void Basis::make_scale_uniform() { + float l = (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0; + for (int i = 0; i < 3; i++) { + elements[i].normalize(); + elements[i] *= l; + } +} + Basis Basis::scaled_local(const Vector3 &p_scale) const { Basis b; b.set_diagonal(p_scale); diff --git a/core/math/basis.h b/core/math/basis.h index 6c3a939d70..0261cf67c6 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -108,6 +108,9 @@ public: void scale_local(const Vector3 &p_scale); Basis scaled_local(const Vector3 &p_scale) const; + void make_scale_uniform(); + float get_uniform_scale() const; + Vector3 get_scale() const; Vector3 get_scale_abs() const; Vector3 get_scale_local() const; diff --git a/core/math/bsp_tree.cpp b/core/math/bsp_tree.cpp deleted file mode 100644 index 7ad907db97..0000000000 --- a/core/math/bsp_tree.cpp +++ /dev/null @@ -1,581 +0,0 @@ -/*************************************************************************/ -/* bsp_tree.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "bsp_tree.h" - -#include "core/error_macros.h" -#include "core/print_string.h" - -void BSP_Tree::from_aabb(const AABB &p_aabb) { - - planes.clear(); - - for (int i = 0; i < 3; i++) { - - Vector3 n; - n[i] = 1; - planes.push_back(Plane(n, p_aabb.position[i] + p_aabb.size[i])); - planes.push_back(Plane(-n, -p_aabb.position[i])); - } - - nodes.clear(); - - for (int i = 0; i < 6; i++) { - - Node n; - n.plane = i; - n.under = (i == 0) ? UNDER_LEAF : i - 1; - n.over = OVER_LEAF; - nodes.push_back(n); - } - - aabb = p_aabb; - error_radius = 0; -} - -Vector<BSP_Tree::Node> BSP_Tree::get_nodes() const { - - return nodes; -} -Vector<Plane> BSP_Tree::get_planes() const { - - return planes; -} - -AABB BSP_Tree::get_aabb() const { - - return aabb; -} - -int BSP_Tree::_get_points_inside(int p_node, const Vector3 *p_points, int *p_indices, const Vector3 &p_center, const Vector3 &p_half_extents, int p_indices_count) const { - - const Node *node = &nodes[p_node]; - const Plane &p = planes[node->plane]; - - Vector3 min( - (p.normal.x > 0) ? -p_half_extents.x : p_half_extents.x, - (p.normal.y > 0) ? -p_half_extents.y : p_half_extents.y, - (p.normal.z > 0) ? -p_half_extents.z : p_half_extents.z); - Vector3 max = -min; - max += p_center; - min += p_center; - - real_t dist_min = p.distance_to(min); - real_t dist_max = p.distance_to(max); - - if ((dist_min * dist_max) < CMP_EPSILON) { //intersection, test point by point - - int under_count = 0; - - //sort points, so the are under first, over last - for (int i = 0; i < p_indices_count; i++) { - - int index = p_indices[i]; - - if (p.is_point_over(p_points[index])) { - - // kind of slow (but cache friendly), should try something else, - // but this is a corner case most of the time - - for (int j = index; j < p_indices_count - 1; j++) - p_indices[j] = p_indices[j + 1]; - - p_indices[p_indices_count - 1] = index; - - } else { - under_count++; - } - } - - int total = 0; - - if (under_count > 0) { - if (node->under == UNDER_LEAF) { - total += under_count; - } else { - total += _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, under_count); - } - } - - if (under_count != p_indices_count) { - if (node->over == OVER_LEAF) { - //total+=0 //if they are over an OVER_LEAF, they are outside the model - } else { - total += _get_points_inside(node->over, p_points, &p_indices[under_count], p_center, p_half_extents, p_indices_count - under_count); - } - } - - return total; - - } else if (dist_min > 0) { //all points over plane - - if (node->over == OVER_LEAF) { - - return 0; // all these points are not visible - } - - return _get_points_inside(node->over, p_points, p_indices, p_center, p_half_extents, p_indices_count); - } else { //all points behind plane - - if (node->under == UNDER_LEAF) { - - return p_indices_count; // all these points are visible - } - return _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, p_indices_count); - } -} - -int BSP_Tree::get_points_inside(const Vector3 *p_points, int p_point_count) const { - - if (nodes.size() == 0) - return 0; - -#if 1 - //this version is easier to debug, and and MUCH faster in real world cases - - int pass_count = 0; - const Node *nodesptr = &nodes[0]; - const Plane *planesptr = &planes[0]; - int node_count = nodes.size(); - - if (node_count == 0) // no nodes! - return 0; - - for (int i = 0; i < p_point_count; i++) { - - const Vector3 &point = p_points[i]; - if (!aabb.has_point(point)) { - continue; - } - - int idx = node_count - 1; - - bool pass = false; - - while (true) { - - if (idx == OVER_LEAF) { - pass = false; - break; - } else if (idx == UNDER_LEAF) { - pass = true; - break; - } - -#ifdef DEBUG_ENABLED - int plane_count = planes.size(); - uint16_t plane = nodesptr[idx].plane; - ERR_FAIL_UNSIGNED_INDEX_V(plane, plane_count, 0); -#endif - - idx = planesptr[nodesptr[idx].plane].is_point_over(point) ? nodes[idx].over : nodes[idx].under; - -#ifdef DEBUG_ENABLED - - ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, 0); -#endif - } - - if (pass) - pass_count++; - } - - return pass_count; - -#else - //this version scales better but it's slower for real world cases - - int *indices = (int *)alloca(p_point_count * sizeof(int)); - AABB bounds; - - for (int i = 0; i < p_point_count; i++) { - - indices[i] = i; - if (i == 0) - bounds.pos = p_points[i]; - else - bounds.expand_to(p_points[i]); - } - - Vector3 half_extents = bounds.size / 2.0; - return _get_points_inside(nodes.size() + 1, p_points, indices, bounds.pos + half_extents, half_extents, p_point_count); -#endif -} - -bool BSP_Tree::point_is_inside(const Vector3 &p_point) const { - - if (!aabb.has_point(p_point)) { - return false; - } - - int node_count = nodes.size(); - - if (node_count == 0) // no nodes! - return false; - - const Node *nodesptr = &nodes[0]; - const Plane *planesptr = &planes[0]; - - int idx = node_count - 1; - - while (true) { - - if (idx == OVER_LEAF) { - return false; - } - if (idx == UNDER_LEAF) { - - return true; - } - -#ifdef DEBUG_ENABLED - int plane_count = planes.size(); - uint16_t plane = nodesptr[idx].plane; - ERR_FAIL_UNSIGNED_INDEX_V(plane, plane_count, false); -#endif - - bool over = planesptr[nodesptr[idx].plane].is_point_over(p_point); - - idx = over ? nodes[idx].over : nodes[idx].under; - -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, false); -#endif - } -} - -static int _bsp_find_best_half_plane(const Face3 *p_faces, const Vector<int> &p_indices, real_t p_tolerance) { - - int ic = p_indices.size(); - const int *indices = p_indices.ptr(); - - int best_plane = -1; - real_t best_plane_cost = 1e20; - - // Loop to find the polygon that best divides the set. - - for (int i = 0; i < ic; i++) { - - const Face3 &f = p_faces[indices[i]]; - Plane p = f.get_plane(); - - int num_over = 0, num_under = 0, num_spanning = 0; - - for (int j = 0; j < ic; j++) { - - if (i == j) - continue; - - const Face3 &g = p_faces[indices[j]]; - int over = 0, under = 0; - - for (int k = 0; k < 3; k++) { - - real_t d = p.distance_to(g.vertex[j]); - - if (Math::abs(d) > p_tolerance) { - - if (d > 0) - over++; - else - under++; - } - } - - if (over && under) - num_spanning++; - else if (over) - num_over++; - else - num_under++; - } - - //real_t split_cost = num_spanning / (real_t) face_count; - real_t relation = Math::abs(num_over - num_under) / (real_t)ic; - - // being honest, i never found a way to add split cost to the mix in a meaninguful way - // in this engine, also, will likely be ignored anyway - - real_t plane_cost = /*split_cost +*/ relation; - - //printf("plane %i, %i over, %i under, %i spanning, cost is %g\n",i,num_over,num_under,num_spanning,plane_cost); - if (plane_cost < best_plane_cost) { - - best_plane = i; - best_plane_cost = plane_cost; - } - } - - return best_plane; -} - -static int _bsp_create_node(const Face3 *p_faces, const Vector<int> &p_indices, Vector<Plane> &p_planes, Vector<BSP_Tree::Node> &p_nodes, real_t p_tolerance) { - - ERR_FAIL_COND_V(p_nodes.size() == BSP_Tree::MAX_NODES, -1); - - // should not reach here - ERR_FAIL_COND_V(p_indices.size() == 0, -1); - - int ic = p_indices.size(); - const int *indices = p_indices.ptr(); - - int divisor_idx = _bsp_find_best_half_plane(p_faces, p_indices, p_tolerance); - - // returned error - ERR_FAIL_COND_V(divisor_idx < 0, -1); - - Vector<int> faces_over; - Vector<int> faces_under; - - Plane divisor_plane = p_faces[indices[divisor_idx]].get_plane(); - - for (int i = 0; i < ic; i++) { - - if (i == divisor_idx) - continue; - - const Face3 &f = p_faces[indices[i]]; - - /* - if (f.get_plane().is_equal_approx(divisor_plane)) - continue; - */ - - int over_count = 0; - int under_count = 0; - - for (int j = 0; j < 3; j++) { - - real_t d = divisor_plane.distance_to(f.vertex[j]); - if (Math::abs(d) > p_tolerance) { - - if (d > 0) - over_count++; - else - under_count++; - } - } - - if (over_count) - faces_over.push_back(indices[i]); - if (under_count) - faces_under.push_back(indices[i]); - } - - uint16_t over_idx = BSP_Tree::OVER_LEAF, under_idx = BSP_Tree::UNDER_LEAF; - - if (faces_over.size() > 0) { //have facess above? - - int idx = _bsp_create_node(p_faces, faces_over, p_planes, p_nodes, p_tolerance); - if (idx >= 0) - over_idx = idx; - } - - if (faces_under.size() > 0) { //have facess above? - - int idx = _bsp_create_node(p_faces, faces_under, p_planes, p_nodes, p_tolerance); - if (idx >= 0) - under_idx = idx; - } - - /* Create the node */ - - // find existing divisor plane - int divisor_plane_idx = -1; - - for (int i = 0; i < p_planes.size(); i++) { - - if (p_planes[i].is_equal_approx(divisor_plane)) { - divisor_plane_idx = i; - break; - } - } - - if (divisor_plane_idx == -1) { - - ERR_FAIL_COND_V(p_planes.size() == BSP_Tree::MAX_PLANES, -1); - divisor_plane_idx = p_planes.size(); - p_planes.push_back(divisor_plane); - } - - BSP_Tree::Node node; - node.plane = divisor_plane_idx; - node.under = under_idx; - node.over = over_idx; - - p_nodes.push_back(node); - - return p_nodes.size() - 1; -} - -BSP_Tree::operator Variant() const { - - Dictionary d; - d["error_radius"] = error_radius; - - Vector<real_t> plane_values; - plane_values.resize(planes.size() * 4); - - for (int i = 0; i < planes.size(); i++) { - - plane_values.write[i * 4 + 0] = planes[i].normal.x; - plane_values.write[i * 4 + 1] = planes[i].normal.y; - plane_values.write[i * 4 + 2] = planes[i].normal.z; - plane_values.write[i * 4 + 3] = planes[i].d; - } - - d["planes"] = plane_values; - - PoolVector<int> dst_nodes; - dst_nodes.resize(nodes.size() * 3); - - for (int i = 0; i < nodes.size(); i++) { - - dst_nodes.set(i * 3 + 0, nodes[i].over); - dst_nodes.set(i * 3 + 1, nodes[i].under); - dst_nodes.set(i * 3 + 2, nodes[i].plane); - } - - d["nodes"] = dst_nodes; - d["aabb"] = aabb; - - return Variant(d); -} - -BSP_Tree::BSP_Tree() { -} - -BSP_Tree::BSP_Tree(const Variant &p_variant) { - - Dictionary d = p_variant; - ERR_FAIL_COND(!d.has("nodes")); - ERR_FAIL_COND(!d.has("planes")); - ERR_FAIL_COND(!d.has("aabb")); - ERR_FAIL_COND(!d.has("error_radius")); - - PoolVector<int> src_nodes = d["nodes"]; - ERR_FAIL_COND(src_nodes.size() % 3); - - if (d["planes"].get_type() == Variant::POOL_REAL_ARRAY) { - - PoolVector<real_t> src_planes = d["planes"]; - int plane_count = src_planes.size(); - ERR_FAIL_COND(plane_count % 4); - planes.resize(plane_count / 4); - - if (plane_count) { - PoolVector<real_t>::Read r = src_planes.read(); - for (int i = 0; i < plane_count / 4; i++) { - - planes.write[i].normal.x = r[i * 4 + 0]; - planes.write[i].normal.y = r[i * 4 + 1]; - planes.write[i].normal.z = r[i * 4 + 2]; - planes.write[i].d = r[i * 4 + 3]; - } - } - - } else { - - planes = d["planes"]; - } - - error_radius = d["error"]; - aabb = d["aabb"]; - - //int node_count = src_nodes.size(); - nodes.resize(src_nodes.size() / 3); - - PoolVector<int>::Read r = src_nodes.read(); - - for (int i = 0; i < nodes.size(); i++) { - - nodes.write[i].over = r[i * 3 + 0]; - nodes.write[i].under = r[i * 3 + 1]; - nodes.write[i].plane = r[i * 3 + 2]; - } -} - -BSP_Tree::BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius) { - - // compute aabb - - int face_count = p_faces.size(); - PoolVector<Face3>::Read faces_r = p_faces.read(); - const Face3 *facesptr = faces_r.ptr(); - - bool first = true; - - Vector<int> indices; - - for (int i = 0; i < face_count; i++) { - - const Face3 &f = facesptr[i]; - - if (f.is_degenerate()) - continue; - - for (int j = 0; j < 3; j++) { - - if (first) { - - aabb.position = f.vertex[0]; - first = false; - } else { - - aabb.expand_to(f.vertex[j]); - } - } - - indices.push_back(i); - } - - ERR_FAIL_COND(aabb.has_no_area()); - - int top = _bsp_create_node(faces_r.ptr(), indices, planes, nodes, aabb.get_longest_axis_size() * 0.0001); - - if (top < 0) { - - nodes.clear(); - planes.clear(); - ERR_FAIL_COND(top < 0); - } - - error_radius = p_error_radius; -} - -BSP_Tree::BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB &p_aabb, real_t p_error_radius) : - nodes(p_nodes), - planes(p_planes), - aabb(p_aabb), - error_radius(p_error_radius) { -} - -BSP_Tree::~BSP_Tree() { -} diff --git a/core/math/bsp_tree.h b/core/math/bsp_tree.h deleted file mode 100644 index 1c8ea380ff..0000000000 --- a/core/math/bsp_tree.h +++ /dev/null @@ -1,159 +0,0 @@ -/*************************************************************************/ -/* bsp_tree.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef BSP_TREE_H -#define BSP_TREE_H - -#include "core/math/aabb.h" -#include "core/math/face3.h" -#include "core/math/plane.h" -#include "core/method_ptrcall.h" -#include "core/pool_vector.h" -#include "core/variant.h" -#include "core/vector.h" - -class BSP_Tree { -public: - enum { - - UNDER_LEAF = 0xFFFF, - OVER_LEAF = 0xFFFE, - MAX_NODES = 0xFFFE, - MAX_PLANES = (1 << 16) - }; - - struct Node { - - uint16_t plane; - uint16_t under; - uint16_t over; - }; - -private: - // thanks to the properties of Vector, - // this class can be assigned and passed around between threads - // with no cost. - - Vector<Node> nodes; - Vector<Plane> planes; - AABB aabb; - real_t error_radius; - - int _get_points_inside(int p_node, const Vector3 *p_points, int *p_indices, const Vector3 &p_center, const Vector3 &p_half_extents, int p_indices_count) const; - - template <class T> - bool _test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const; - -public: - bool is_empty() const { return nodes.size() == 0; } - Vector<Node> get_nodes() const; - Vector<Plane> get_planes() const; - AABB get_aabb() const; - - bool point_is_inside(const Vector3 &p_point) const; - int get_points_inside(const Vector3 *p_points, int p_point_count) const; - template <class T> - bool convex_is_inside(const T &p_convex) const; - - operator Variant() const; - - void from_aabb(const AABB &p_aabb); - - BSP_Tree(); - BSP_Tree(const Variant &p_variant); - BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius = 0); - BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB &p_aabb, real_t p_error_radius = 0); - ~BSP_Tree(); -}; - -template <class T> -bool BSP_Tree::_test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const { - - if (p_current == UNDER_LEAF) - return true; - else if (p_current == OVER_LEAF) - return false; - - bool collided = false; - const Node &n = p_nodes[p_current]; - - const Plane &p = p_planes[n.plane]; - - real_t min, max; - p_convex.project_range(p.normal, min, max); - - bool go_under = min < p.d; - bool go_over = max >= p.d; - - if (go_under && _test_convex(p_nodes, p_planes, n.under, p_convex)) - collided = true; - if (go_over && _test_convex(p_nodes, p_planes, n.over, p_convex)) - collided = true; - - return collided; -} - -template <class T> -bool BSP_Tree::convex_is_inside(const T &p_convex) const { - - int node_count = nodes.size(); - if (node_count == 0) - return false; - const Node *nodes = &this->nodes[0]; - const Plane *planes = &this->planes[0]; - - return _test_convex(nodes, planes, node_count - 1, p_convex); -} - -#ifdef PTRCALL_ENABLED - -template <> -struct PtrToArg<BSP_Tree> { - _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) { - BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr))); - return s; - } - _FORCE_INLINE_ static void encode(BSP_Tree p_val, void *p_ptr) { - Dictionary *d = reinterpret_cast<Dictionary *>(p_ptr); - *d = Variant(p_val); - } -}; - -template <> -struct PtrToArg<const BSP_Tree &> { - _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) { - BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr))); - return s; - } -}; - -#endif - -#endif diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp index 380bae871a..c4981b954b 100644 --- a/core/math/camera_matrix.cpp +++ b/core/math/camera_matrix.cpp @@ -276,6 +276,36 @@ Vector2 CameraMatrix::get_viewport_half_extents() const { return Vector2(res.x, res.y); } +void CameraMatrix::get_far_plane_size(real_t &r_width, real_t &r_height) const { + + const real_t *matrix = (const real_t *)this->matrix; + ///////--- Far Plane ---/////// + Plane far_plane = Plane(matrix[3] - matrix[2], + matrix[7] - matrix[6], + matrix[11] - matrix[10], + -matrix[15] + matrix[14]); + far_plane.normalize(); + + ///////--- Right Plane ---/////// + Plane right_plane = Plane(matrix[3] - matrix[0], + matrix[7] - matrix[4], + matrix[11] - matrix[8], + -matrix[15] + matrix[12]); + right_plane.normalize(); + + Plane top_plane = Plane(matrix[3] - matrix[1], + matrix[7] - matrix[5], + matrix[11] - matrix[9], + -matrix[15] + matrix[13]); + top_plane.normalize(); + + Vector3 res; + far_plane.intersect_3(right_plane, top_plane, &res); + + r_width = res.x; + r_height = res.y; +} + bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const { Vector<Plane> planes = get_projection_planes(Transform()); @@ -485,6 +515,12 @@ void CameraMatrix::invert() { } } +void CameraMatrix::flip_y() { + for (int i = 0; i < 4; i++) { + matrix[1][i] = -matrix[1][i]; + } +} + CameraMatrix::CameraMatrix() { set_identity(); @@ -506,6 +542,28 @@ CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const { return new_matrix; } +void CameraMatrix::set_depth_correction(bool p_flip_y) { + + real_t *m = &matrix[0][0]; + + m[0] = 1; + m[1] = 0.0; + m[2] = 0.0; + m[3] = 0.0; + m[4] = 0.0; + m[5] = p_flip_y ? -1 : 1; + m[6] = 0.0; + m[7] = 0.0; + m[8] = 0.0; + m[9] = 0.0; + m[10] = 0.5; + m[11] = 0.0; + m[12] = 0.0; + m[13] = 0.0; + m[14] = 0.5; + m[15] = 1.0; +} + void CameraMatrix::set_light_bias() { real_t *m = &matrix[0][0]; diff --git a/core/math/camera_matrix.h b/core/math/camera_matrix.h index 2eed6d25d6..60f7d15974 100644 --- a/core/math/camera_matrix.h +++ b/core/math/camera_matrix.h @@ -50,6 +50,7 @@ struct CameraMatrix { void set_identity(); void set_zero(); void set_light_bias(); + void set_depth_correction(bool p_flip_y = true); void set_light_atlas_rect(const Rect2 &p_rect); void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false); void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov, int p_eye, real_t p_intraocular_dist, real_t p_convergence_dist); @@ -74,6 +75,7 @@ struct CameraMatrix { bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const; Vector2 get_viewport_half_extents() const; + void get_far_plane_size(real_t &r_width, real_t &r_height) const; void invert(); CameraMatrix inverse() const; @@ -90,6 +92,23 @@ struct CameraMatrix { int get_pixels_per_meter(int p_for_pixel_width) const; operator Transform() const; + void flip_y(); + + bool operator==(const CameraMatrix &p_cam) const { + for (uint32_t i = 0; i < 4; i++) { + for (uint32_t j = 0; j < 4; j++) { + if (matrix[i][j] != p_cam.matrix[i][j]) { + return false; + } + } + } + return true; + } + + bool operator!=(const CameraMatrix &p_cam) const { + return !(*this == p_cam); + } + CameraMatrix(); CameraMatrix(const Transform &p_transform); ~CameraMatrix(); diff --git a/core/math/expression.cpp b/core/math/expression.cpp index 655098376c..058673b681 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -64,7 +64,6 @@ const char *Expression::func_name[Expression::FUNC_MAX] = { "is_nan", "is_inf", "ease", - "decimals", "step_decimals", "stepify", "lerp", @@ -153,7 +152,6 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) { case MATH_EXP: case MATH_ISNAN: case MATH_ISINF: - case MATH_DECIMALS: case MATH_STEP_DECIMALS: case MATH_SEED: case MATH_RANDSEED: @@ -210,16 +208,16 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) { return 0; } -#define VALIDATE_ARG_NUM(m_arg) \ - if (!p_inputs[m_arg]->is_num()) { \ - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \ - r_error.argument = m_arg; \ - r_error.expected = Variant::REAL; \ - return; \ +#define VALIDATE_ARG_NUM(m_arg) \ + if (!p_inputs[m_arg]->is_num()) { \ + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ + r_error.argument = m_arg; \ + r_error.expected = Variant::FLOAT; \ + return; \ } -void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Variant::CallError &r_error, String &r_error_str) { - r_error.error = Variant::CallError::CALL_OK; +void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Callable::CallError &r_error, String &r_error_str) { + r_error.error = Callable::CallError::CALL_OK; switch (p_func) { case MATH_SIN: { @@ -316,15 +314,15 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant int64_t i = *p_inputs[0]; *r_return = ABS(i); - } else if (p_inputs[0]->get_type() == Variant::REAL) { + } else if (p_inputs[0]->get_type() == Variant::FLOAT) { real_t r = *p_inputs[0]; *r_return = Math::abs(r); } else { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; - r_error.expected = Variant::REAL; + r_error.expected = Variant::FLOAT; } } break; case MATH_SIGN: { @@ -333,15 +331,15 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant int64_t i = *p_inputs[0]; *r_return = i < 0 ? -1 : (i > 0 ? +1 : 0); - } else if (p_inputs[0]->get_type() == Variant::REAL) { + } else if (p_inputs[0]->get_type() == Variant::FLOAT) { real_t r = *p_inputs[0]; *r_return = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0); } else { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; - r_error.expected = Variant::REAL; + r_error.expected = Variant::FLOAT; } } break; case MATH_POW: { @@ -376,11 +374,6 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant VALIDATE_ARG_NUM(1); *r_return = Math::ease((double)*p_inputs[0], (double)*p_inputs[1]); } break; - case MATH_DECIMALS: { - - VALIDATE_ARG_NUM(0); - *r_return = Math::step_decimals((double)*p_inputs[0]); - } break; case MATH_STEP_DECIMALS: { VALIDATE_ARG_NUM(0); @@ -587,7 +580,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant if (p_inputs[0]->get_type() != Variant::OBJECT) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::OBJECT; @@ -621,7 +614,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant if (p_inputs[0]->get_type() != Variant::OBJECT) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::OBJECT; @@ -629,7 +622,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } if (p_inputs[1]->get_type() != Variant::STRING && p_inputs[1]->get_type() != Variant::NODE_PATH) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 1; r_error.expected = Variant::STRING; @@ -651,7 +644,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant if (type < 0 || type >= Variant::VARIANT_MAX) { r_error_str = RTR("Invalid type argument to convert(), use TYPE_* constants."); - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::INT; return; @@ -682,7 +675,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant if (p_inputs[0]->get_type() != Variant::STRING) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING; @@ -694,7 +687,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant if (str.length() != 1) { r_error_str = RTR("Expected a string of length 1 (a character)."); - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING; @@ -739,7 +732,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant case STR_TO_VAR: { if (p_inputs[0]->get_type() != Variant::STRING) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING; @@ -754,7 +747,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant Error err = VariantParser::parse(&ss, *r_return, errs, line); if (err != OK) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING; *r_return = "Parse error at line " + itos(line) + ": " + errs; @@ -764,12 +757,12 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case VAR_TO_BYTES: { - PoolByteArray barr; + PackedByteArray barr; bool full_objects = *p_inputs[1]; int len; Error err = encode_variant(*p_inputs[0], NULL, len, full_objects); if (err) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::NIL; r_error_str = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID)."; @@ -778,32 +771,32 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant barr.resize(len); { - PoolByteArray::Write w = barr.write(); - encode_variant(*p_inputs[0], w.ptr(), len, full_objects); + uint8_t *w = barr.ptrw(); + encode_variant(*p_inputs[0], w, len, full_objects); } *r_return = barr; } break; case BYTES_TO_VAR: { - if (p_inputs[0]->get_type() != Variant::POOL_BYTE_ARRAY) { - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + if (p_inputs[0]->get_type() != Variant::PACKED_BYTE_ARRAY) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; - r_error.expected = Variant::POOL_BYTE_ARRAY; + r_error.expected = Variant::PACKED_BYTE_ARRAY; return; } - PoolByteArray varr = *p_inputs[0]; + PackedByteArray varr = *p_inputs[0]; bool allow_objects = *p_inputs[1]; Variant ret; { - PoolByteArray::Read r = varr.read(); - Error err = decode_variant(ret, r.ptr(), varr.size(), NULL, allow_objects); + const uint8_t *r = varr.ptr(); + Error err = decode_variant(ret, r, varr.size(), NULL, allow_objects); if (err != OK) { r_error_str = RTR("Not enough bytes for decoding bytes, or invalid format."); - r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; - r_error.expected = Variant::POOL_BYTE_ARRAY; + r_error.expected = Variant::PACKED_BYTE_ARRAY; return; } } @@ -1036,8 +1029,7 @@ Error Expression::_get_token(Token &r_token) { case 'f': res = 12; break; case 'r': res = 13; break; case 'u': { - //hexnumbarh - oct is deprecated - + // hex number for (int j = 0; j < 4; j++) { CharType c = GET_CHAR(); @@ -1062,7 +1054,7 @@ Error Expression::_get_token(Token &r_token) { v = c - 'A'; v += 10; } else { - ERR_PRINT("BUG"); + ERR_PRINT("Bug parsing hex constant."); v = 0; } @@ -1071,13 +1063,8 @@ Error Expression::_get_token(Token &r_token) { } } break; - //case '\"': res='\"'; break; - //case '\\': res='\\'; break; - //case '/': res='/'; break; default: { res = next; - //r_err_str="Invalid escape sequence"; - //return ERR_PARSE_ERROR; } break; } @@ -1174,7 +1161,7 @@ Error Expression::_get_token(Token &r_token) { if (is_float) r_token.value = num.to_double(); else - r_token.value = num.to_int(); + r_token.value = num.to_int64(); return OK; } else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') { @@ -2084,10 +2071,10 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: argp.write[i] = &arr[i]; } - Variant::CallError ce; + Callable::CallError ce; r_ret = Variant::construct(constructor->data_type, (const Variant **)argp.ptr(), argp.size(), ce); - if (ce.error != Variant::CallError::CALL_OK) { + if (ce.error != Callable::CallError::CALL_OK) { r_error_str = vformat(RTR("Invalid arguments to construct '%s'"), Variant::get_type_name(constructor->data_type)); return true; } @@ -2112,10 +2099,10 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: argp.write[i] = &arr[i]; } - Variant::CallError ce; + Callable::CallError ce; exec_func(bifunc->func, (const Variant **)argp.ptr(), &r_ret, ce, r_error_str); - if (ce.error != Variant::CallError::CALL_OK) { + if (ce.error != Callable::CallError::CALL_OK) { r_error_str = "Builtin Call Failed. " + r_error_str; return true; } @@ -2147,10 +2134,10 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: argp.write[i] = &arr[i]; } - Variant::CallError ce; + Callable::CallError ce; r_ret = base.call(call->method, (const Variant **)argp.ptr(), argp.size(), ce); - if (ce.error != Variant::CallError::CALL_OK) { + if (ce.error != Callable::CallError::CALL_OK) { r_error_str = vformat(RTR("On call to '%s':"), String(call->method)); return true; } diff --git a/core/math/expression.h b/core/math/expression.h index c5b9d79a16..bbf946bb0a 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -63,7 +63,6 @@ public: MATH_ISNAN, MATH_ISINF, MATH_EASE, - MATH_DECIMALS, MATH_STEP_DECIMALS, MATH_STEPIFY, MATH_LERP, @@ -112,7 +111,7 @@ public: static int get_func_argument_count(BuiltinFunc p_func); static String get_func_name(BuiltinFunc p_func); - static void exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Variant::CallError &r_error, String &r_error_str); + static void exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Callable::CallError &r_error, String &r_error_str); static BuiltinFunc find_function(const String &p_string); private: diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index 7eb48290a8..69c7abfd30 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -214,23 +214,19 @@ static bool _group_face(_FaceClassify *p_faces, int len, int p_index, int p_grou return true; } -PoolVector<PoolVector<Face3> > Geometry::separate_objects(PoolVector<Face3> p_array) { +Vector<Vector<Face3> > Geometry::separate_objects(Vector<Face3> p_array) { - PoolVector<PoolVector<Face3> > objects; + Vector<Vector<Face3> > objects; int len = p_array.size(); - PoolVector<Face3>::Read r = p_array.read(); + const Face3 *arrayptr = p_array.ptr(); - const Face3 *arrayptr = r.ptr(); - - PoolVector<_FaceClassify> fc; + Vector<_FaceClassify> fc; fc.resize(len); - PoolVector<_FaceClassify>::Write fcw = fc.write(); - - _FaceClassify *_fcptr = fcw.ptr(); + _FaceClassify *_fcptr = fc.ptrw(); for (int i = 0; i < len; i++) { @@ -239,7 +235,7 @@ PoolVector<PoolVector<Face3> > Geometry::separate_objects(PoolVector<Face3> p_ar bool error = _connect_faces(_fcptr, len, -1); - ERR_FAIL_COND_V_MSG(error, PoolVector<PoolVector<Face3> >(), "Invalid geometry."); + ERR_FAIL_COND_V_MSG(error, Vector<Vector<Face3> >(), "Invalid geometry."); // Group connected faces in separate objects. @@ -263,8 +259,7 @@ PoolVector<PoolVector<Face3> > Geometry::separate_objects(PoolVector<Face3> p_ar if (group >= 0) { objects.resize(group); - PoolVector<PoolVector<Face3> >::Write obw = objects.write(); - PoolVector<Face3> *group_faces = obw.ptr(); + Vector<Face3> *group_faces = objects.ptrw(); for (int i = 0; i < len; i++) { if (!_fcptr[i].valid) @@ -470,7 +465,7 @@ static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, } } -static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, PoolVector<Face3> &p_faces) { +static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, Vector<Face3> &p_faces) { ERR_FAIL_INDEX(x, len_x); ERR_FAIL_INDEX(y, len_y); @@ -530,14 +525,13 @@ static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, i } } -PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_error) { +Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { #define _MIN_SIZE 1.0 #define _MAX_LENGTH 20 int face_count = p_array.size(); - PoolVector<Face3>::Read facesr = p_array.read(); - const Face3 *faces = facesr.ptr(); + const Face3 *faces = p_array.ptr(); AABB global_aabb; @@ -638,7 +632,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e // Build faces for the inside-outside cell divisors. - PoolVector<Face3> wrapped_faces; + Vector<Face3> wrapped_faces; for (int i = 0; i < div_x; i++) { @@ -654,8 +648,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e // Transform face vertices to global coords. int wrapped_faces_count = wrapped_faces.size(); - PoolVector<Face3>::Write wrapped_facesw = wrapped_faces.write(); - Face3 *wrapped_faces_ptr = wrapped_facesw.ptr(); + Face3 *wrapped_faces_ptr = wrapped_faces.ptrw(); for (int i = 0; i < wrapped_faces_count; i++) { @@ -720,7 +713,7 @@ Vector<Vector<Vector2> > Geometry::decompose_polygon_in_convex(Vector<Point2> po return decomp; } -Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes) { +Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { MeshData mesh; @@ -859,9 +852,9 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes return mesh; } -PoolVector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) { +Vector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) { - PoolVector<Plane> planes; + Vector<Plane> planes; planes.push_back(Plane(Vector3(1, 0, 0), p_extents.x)); planes.push_back(Plane(Vector3(-1, 0, 0), p_extents.x)); @@ -873,9 +866,9 @@ PoolVector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) { return planes; } -PoolVector<Plane> Geometry::build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis) { +Vector<Plane> Geometry::build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis) { - PoolVector<Plane> planes; + Vector<Plane> planes; for (int i = 0; i < p_sides; i++) { @@ -895,9 +888,9 @@ PoolVector<Plane> Geometry::build_cylinder_planes(real_t p_radius, real_t p_heig return planes; } -PoolVector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) { +Vector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) { - PoolVector<Plane> planes; + Vector<Plane> planes; Vector3 axis; axis[p_axis] = 1.0; @@ -928,9 +921,9 @@ PoolVector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int return planes; } -PoolVector<Plane> Geometry::build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { +Vector<Plane> Geometry::build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { - PoolVector<Plane> planes; + Vector<Plane> planes; Vector3 axis; axis[p_axis] = 1.0; diff --git a/core/math/geometry.h b/core/math/geometry.h index b9193242bc..a94d00bf77 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -37,7 +37,7 @@ #include "core/math/triangulate.h" #include "core/math/vector3.h" #include "core/object.h" -#include "core/pool_vector.h" + #include "core/print_string.h" #include "core/vector.h" @@ -899,10 +899,10 @@ public: return (intersections & 1); } - static PoolVector<PoolVector<Face3> > separate_objects(PoolVector<Face3> p_array); + static Vector<Vector<Face3> > separate_objects(Vector<Face3> p_array); // Create a "wrap" that encloses the given geometry. - static PoolVector<Face3> wrap_geometry(PoolVector<Face3> p_array, real_t *p_error = NULL); + static Vector<Face3> wrap_geometry(Vector<Face3> p_array, real_t *p_error = NULL); struct MeshData { @@ -1006,11 +1006,11 @@ public: } static Vector<Vector<Vector2> > decompose_polygon_in_convex(Vector<Point2> polygon); - static MeshData build_convex_mesh(const PoolVector<Plane> &p_planes); - static PoolVector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z); - static PoolVector<Plane> build_box_planes(const Vector3 &p_extents); - static PoolVector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z); - static PoolVector<Plane> build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z); + static MeshData build_convex_mesh(const Vector<Plane> &p_planes); + static Vector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z); + static Vector<Plane> build_box_planes(const Vector3 &p_extents); + static Vector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z); + static Vector<Plane> build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z); static void make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size); diff --git a/core/math/rect2.h b/core/math/rect2.h index 0d2e7eb6e5..e4ea615c22 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -387,6 +387,11 @@ struct Rect2i { size = end - begin; } + _FORCE_INLINE_ Rect2i abs() const { + + return Rect2i(Point2i(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs()); + } + operator String() const { return String(position) + ", " + String(size); } operator Rect2() const { return Rect2(position, size); } diff --git a/core/math/transform.h b/core/math/transform.h index ad397d9c09..c6e3be4c70 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -34,7 +34,6 @@ #include "core/math/aabb.h" #include "core/math/basis.h" #include "core/math/plane.h" -#include "core/pool_vector.h" class Transform { public: @@ -84,8 +83,8 @@ public: _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const; _FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const; - _FORCE_INLINE_ PoolVector<Vector3> xform(const PoolVector<Vector3> &p_array) const; - _FORCE_INLINE_ PoolVector<Vector3> xform_inv(const PoolVector<Vector3> &p_array) const; + _FORCE_INLINE_ Vector<Vector3> xform(const Vector<Vector3> &p_array) const; + _FORCE_INLINE_ Vector<Vector3> xform_inv(const Vector<Vector3> &p_array) const; void operator*=(const Transform &p_transform); Transform operator*(const Transform &p_transform) const; @@ -210,13 +209,13 @@ _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { return ret; } -PoolVector<Vector3> Transform::xform(const PoolVector<Vector3> &p_array) const { +Vector<Vector3> Transform::xform(const Vector<Vector3> &p_array) const { - PoolVector<Vector3> array; + Vector<Vector3> array; array.resize(p_array.size()); - PoolVector<Vector3>::Read r = p_array.read(); - PoolVector<Vector3>::Write w = array.write(); + const Vector3 *r = p_array.ptr(); + Vector3 *w = array.ptrw(); for (int i = 0; i < p_array.size(); ++i) { w[i] = xform(r[i]); @@ -224,13 +223,13 @@ PoolVector<Vector3> Transform::xform(const PoolVector<Vector3> &p_array) const { return array; } -PoolVector<Vector3> Transform::xform_inv(const PoolVector<Vector3> &p_array) const { +Vector<Vector3> Transform::xform_inv(const Vector<Vector3> &p_array) const { - PoolVector<Vector3> array; + Vector<Vector3> array; array.resize(p_array.size()); - PoolVector<Vector3>::Read r = p_array.read(); - PoolVector<Vector3>::Write w = array.write(); + const Vector3 *r = p_array.ptr(); + Vector3 *w = array.ptrw(); for (int i = 0; i < p_array.size(); ++i) { w[i] = xform_inv(r[i]); diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index 367f697ccf..fa43762aa4 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -32,7 +32,6 @@ #define TRANSFORM_2D_H #include "core/math/rect2.h" // also includes vector2, math_funcs, and ustring -#include "core/pool_vector.h" struct Transform2D { // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper": @@ -112,8 +111,8 @@ struct Transform2D { _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const; _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const; _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const; - _FORCE_INLINE_ PoolVector<Vector2> xform(const PoolVector<Vector2> &p_array) const; - _FORCE_INLINE_ PoolVector<Vector2> xform_inv(const PoolVector<Vector2> &p_array) const; + _FORCE_INLINE_ Vector<Vector2> xform(const Vector<Vector2> &p_array) const; + _FORCE_INLINE_ Vector<Vector2> xform_inv(const Vector<Vector2> &p_array) const; operator String() const; @@ -203,13 +202,13 @@ Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const { return new_rect; } -PoolVector<Vector2> Transform2D::xform(const PoolVector<Vector2> &p_array) const { +Vector<Vector2> Transform2D::xform(const Vector<Vector2> &p_array) const { - PoolVector<Vector2> array; + Vector<Vector2> array; array.resize(p_array.size()); - PoolVector<Vector2>::Read r = p_array.read(); - PoolVector<Vector2>::Write w = array.write(); + const Vector2 *r = p_array.ptr(); + Vector2 *w = array.ptrw(); for (int i = 0; i < p_array.size(); ++i) { w[i] = xform(r[i]); @@ -217,13 +216,13 @@ PoolVector<Vector2> Transform2D::xform(const PoolVector<Vector2> &p_array) const return array; } -PoolVector<Vector2> Transform2D::xform_inv(const PoolVector<Vector2> &p_array) const { +Vector<Vector2> Transform2D::xform_inv(const Vector<Vector2> &p_array) const { - PoolVector<Vector2> array; + Vector<Vector2> array; array.resize(p_array.size()); - PoolVector<Vector2>::Read r = p_array.read(); - PoolVector<Vector2>::Write w = array.write(); + const Vector2 *r = p_array.ptr(); + Vector2 *w = array.ptrw(); for (int i = 0; i < p_array.size(); ++i) { w[i] = xform_inv(r[i]); diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp index 53d4ea0a96..01d38cf24e 100644 --- a/core/math/triangle_mesh.cpp +++ b/core/math/triangle_mesh.cpp @@ -89,7 +89,7 @@ int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, in return index; } -void TriangleMesh::get_indices(PoolVector<int> *r_triangles_indices) const { +void TriangleMesh::get_indices(Vector<int> *r_triangles_indices) const { if (!valid) return; @@ -97,10 +97,10 @@ void TriangleMesh::get_indices(PoolVector<int> *r_triangles_indices) const { const int triangles_num = triangles.size(); // Parse vertices indices - PoolVector<Triangle>::Read triangles_read = triangles.read(); + const Triangle *triangles_read = triangles.ptr(); r_triangles_indices->resize(triangles_num * 3); - PoolVector<int>::Write r_indices_write = r_triangles_indices->write(); + int *r_indices_write = r_triangles_indices->ptrw(); for (int i = 0; i < triangles_num; ++i) { r_indices_write[3 * i + 0] = triangles_read[i].indices[0]; @@ -109,7 +109,7 @@ void TriangleMesh::get_indices(PoolVector<int> *r_triangles_indices) const { } } -void TriangleMesh::create(const PoolVector<Vector3> &p_faces) { +void TriangleMesh::create(const Vector<Vector3> &p_faces) { valid = false; @@ -119,7 +119,7 @@ void TriangleMesh::create(const PoolVector<Vector3> &p_faces) { triangles.resize(fc); bvh.resize(fc * 3); //will never be larger than this (todo make better) - PoolVector<BVH>::Write bw = bvh.write(); + BVH *bw = bvh.ptrw(); { @@ -127,8 +127,8 @@ void TriangleMesh::create(const PoolVector<Vector3> &p_faces) { //except for the Set for repeated triangles, everything //goes in-place. - PoolVector<Vector3>::Read r = p_faces.read(); - PoolVector<Triangle>::Write w = triangles.write(); + const Vector3 *r = p_faces.ptr(); + Triangle *w = triangles.ptrw(); Map<Vector3, int> db; for (int i = 0; i < fc; i++) { @@ -164,15 +164,15 @@ void TriangleMesh::create(const PoolVector<Vector3> &p_faces) { } vertices.resize(db.size()); - PoolVector<Vector3>::Write vw = vertices.write(); + Vector3 *vw = vertices.ptrw(); for (Map<Vector3, int>::Element *E = db.front(); E; E = E->next()) { vw[E->get()] = E->key(); } } - PoolVector<BVH *> bwptrs; + Vector<BVH *> bwptrs; bwptrs.resize(fc); - PoolVector<BVH *>::Write bwp = bwptrs.write(); + BVH **bwp = bwptrs.ptrw(); for (int i = 0; i < fc; i++) { bwp[i] = &bw[i]; @@ -180,9 +180,8 @@ void TriangleMesh::create(const PoolVector<Vector3> &p_faces) { max_depth = 0; int max_alloc = fc; - _create_bvh(bw.ptr(), bwp.ptr(), 0, fc, 1, max_depth, max_alloc); + _create_bvh(bw, bwp, 0, fc, 1, max_depth, max_alloc); - bw.release(); //clearup bvh.resize(max_alloc); //resize back valid = true; @@ -208,13 +207,11 @@ Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { int level = 0; - PoolVector<Triangle>::Read trianglesr = triangles.read(); - PoolVector<Vector3>::Read verticesr = vertices.read(); - PoolVector<BVH>::Read bvhr = bvh.read(); + const Triangle *triangleptr = triangles.ptr(); + // const Vector3 *verticesr = vertices.ptr(); + const BVH *bvhptr = bvh.ptr(); - const Triangle *triangleptr = trianglesr.ptr(); int pos = bvh.size() - 1; - const BVH *bvhptr = bvhr.ptr(); stack[0] = pos; while (true) { @@ -304,14 +301,11 @@ bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_en int level = 0; - PoolVector<Triangle>::Read trianglesr = triangles.read(); - PoolVector<Vector3>::Read verticesr = vertices.read(); - PoolVector<BVH>::Read bvhr = bvh.read(); + const Triangle *triangleptr = triangles.ptr(); + const Vector3 *vertexptr = vertices.ptr(); + const BVH *bvhptr = bvh.ptr(); - const Triangle *triangleptr = trianglesr.ptr(); - const Vector3 *vertexptr = verticesr.ptr(); int pos = bvh.size() - 1; - const BVH *bvhptr = bvhr.ptr(); stack[0] = pos; while (true) { @@ -419,14 +413,11 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V int level = 0; - PoolVector<Triangle>::Read trianglesr = triangles.read(); - PoolVector<Vector3>::Read verticesr = vertices.read(); - PoolVector<BVH>::Read bvhr = bvh.read(); + const Triangle *triangleptr = triangles.ptr(); + const Vector3 *vertexptr = vertices.ptr(); + const BVH *bvhptr = bvh.ptr(); - const Triangle *triangleptr = trianglesr.ptr(); - const Vector3 *vertexptr = verticesr.ptr(); int pos = bvh.size() - 1; - const BVH *bvhptr = bvhr.ptr(); stack[0] = pos; while (true) { @@ -529,14 +520,11 @@ bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_cou int level = 0; - PoolVector<Triangle>::Read trianglesr = triangles.read(); - PoolVector<Vector3>::Read verticesr = vertices.read(); - PoolVector<BVH>::Read bvhr = bvh.read(); + const Triangle *triangleptr = triangles.ptr(); + const Vector3 *vertexptr = vertices.ptr(); + const BVH *bvhptr = bvh.ptr(); - const Triangle *triangleptr = trianglesr.ptr(); - const Vector3 *vertexptr = verticesr.ptr(); int pos = bvh.size() - 1; - const BVH *bvhptr = bvhr.ptr(); stack[0] = pos; while (true) { @@ -645,16 +633,13 @@ bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, int level = 0; - PoolVector<Triangle>::Read trianglesr = triangles.read(); - PoolVector<Vector3>::Read verticesr = vertices.read(); - PoolVector<BVH>::Read bvhr = bvh.read(); + const Triangle *triangleptr = triangles.ptr(); + const Vector3 *vertexptr = vertices.ptr(); + const BVH *bvhptr = bvh.ptr(); Transform scale(Basis().scaled(p_scale)); - const Triangle *triangleptr = trianglesr.ptr(); - const Vector3 *vertexptr = verticesr.ptr(); int pos = bvh.size() - 1; - const BVH *bvhptr = bvhr.ptr(); stack[0] = pos; while (true) { @@ -732,18 +717,18 @@ bool TriangleMesh::is_valid() const { return valid; } -PoolVector<Face3> TriangleMesh::get_faces() const { +Vector<Face3> TriangleMesh::get_faces() const { if (!valid) - return PoolVector<Face3>(); + return Vector<Face3>(); - PoolVector<Face3> faces; + Vector<Face3> faces; int ts = triangles.size(); faces.resize(triangles.size()); - PoolVector<Face3>::Write w = faces.write(); - PoolVector<Triangle>::Read r = triangles.read(); - PoolVector<Vector3>::Read rv = vertices.read(); + Face3 *w = faces.ptrw(); + const Triangle *r = triangles.ptr(); + const Vector3 *rv = vertices.ptr(); for (int i = 0; i < ts; i++) { for (int j = 0; j < 3; j++) { @@ -751,7 +736,6 @@ PoolVector<Face3> TriangleMesh::get_faces() const { } } - w.release(); return faces; } diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index 575a78b0b5..fdbfb90465 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -44,8 +44,8 @@ class TriangleMesh : public Reference { int indices[3]; }; - PoolVector<Triangle> triangles; - PoolVector<Vector3> vertices; + Vector<Triangle> triangles; + Vector<Vector3> vertices; struct BVH { @@ -82,7 +82,7 @@ class TriangleMesh : public Reference { int _create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc); - PoolVector<BVH> bvh; + Vector<BVH> bvh; int max_depth; bool valid; @@ -93,13 +93,13 @@ public: bool intersect_convex_shape(const Plane *p_planes, int p_plane_count) const; bool inside_convex_shape(const Plane *p_planes, int p_plane_count, Vector3 p_scale = Vector3(1, 1, 1)) const; Vector3 get_area_normal(const AABB &p_aabb) const; - PoolVector<Face3> get_faces() const; + Vector<Face3> get_faces() const; - PoolVector<Triangle> get_triangles() const { return triangles; } - PoolVector<Vector3> get_vertices() const { return vertices; } - void get_indices(PoolVector<int> *r_triangles_indices) const; + Vector<Triangle> get_triangles() const { return triangles; } + Vector<Vector3> get_vertices() const { return vertices; } + void get_indices(Vector<int> *r_triangles_indices) const; - void create(const PoolVector<Vector3> &p_faces); + void create(const Vector<Vector3> &p_faces); TriangleMesh(); }; diff --git a/core/math/vector2.h b/core/math/vector2.h index 1dec830821..ba5558102f 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -311,10 +311,15 @@ struct Vector2i { bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); } bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); } + bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); } + bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); } + bool operator==(const Vector2i &p_vec2) const; bool operator!=(const Vector2i &p_vec2) const; - real_t get_aspect() const { return width / (real_t)height; } + real_t aspect() const { return width / (real_t)height; } + Vector2i sign() const { return Vector2i(SGN(x), SGN(y)); } + Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); } operator String() const { return String::num(x) + ", " + String::num(y); } diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index 71ff79c0fc..353b2acd16 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -103,7 +103,7 @@ Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, Vector3 out; out = 0.5 * ((p1 * 2.0) + (-p0 + p2) * t + - (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 + + (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 + (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3); return out; } @@ -122,7 +122,7 @@ Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, c Vector3 out; out = 0.5 * ((p1 * 2.0) + (-p0 + p2) * t + - (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 + + (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 + (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3); return out; } diff --git a/core/math/vector3.h b/core/math/vector3.h index 4ad3017109..3bf8644af9 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -32,6 +32,7 @@ #define VECTOR3_H #include "core/math/math_funcs.h" +#include "core/math/vector3i.h" #include "core/ustring.h" class Basis; @@ -147,6 +148,15 @@ struct Vector3 { _FORCE_INLINE_ bool operator>=(const Vector3 &p_v) const; operator String() const; + _FORCE_INLINE_ operator Vector3i() const { + return Vector3i(x, y, z); + } + + _FORCE_INLINE_ Vector3(const Vector3i &p_ivec) { + x = p_ivec.x; + y = p_ivec.y; + z = p_ivec.z; + } _FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) { x = p_x; diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp new file mode 100644 index 0000000000..8a4ddf03b9 --- /dev/null +++ b/core/math/vector3i.cpp @@ -0,0 +1,55 @@ +/*************************************************************************/ +/* vector3i.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "vector3i.h" + +void Vector3i::set_axis(int p_axis, int32_t p_value) { + ERR_FAIL_INDEX(p_axis, 3); + coord[p_axis] = p_value; +} +int32_t Vector3i::get_axis(int p_axis) const { + + ERR_FAIL_INDEX_V(p_axis, 3, 0); + return operator[](p_axis); +} + +int Vector3i::min_axis() const { + + return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2); +} +int Vector3i::max_axis() const { + + return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0); +} + +Vector3i::operator String() const { + + return (itos(x) + ", " + itos(y) + ", " + itos(z)); +} diff --git a/core/math/vector3i.h b/core/math/vector3i.h new file mode 100644 index 0000000000..6f9754d3b9 --- /dev/null +++ b/core/math/vector3i.h @@ -0,0 +1,272 @@ +/*************************************************************************/ +/* vector3i.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef VECTOR3I_H +#define VECTOR3I_H + +#include "core/typedefs.h" +#include "core/ustring.h" + +struct Vector3i { + + enum Axis { + AXIS_X, + AXIS_Y, + AXIS_Z, + }; + + union { + struct { + int32_t x; + int32_t y; + int32_t z; + }; + + int32_t coord[3]; + }; + + _FORCE_INLINE_ const int32_t &operator[](int p_axis) const { + + return coord[p_axis]; + } + + _FORCE_INLINE_ int32_t &operator[](int p_axis) { + + return coord[p_axis]; + } + + void set_axis(int p_axis, int32_t p_value); + int32_t get_axis(int p_axis) const; + + int min_axis() const; + int max_axis() const; + + _FORCE_INLINE_ void zero(); + + _FORCE_INLINE_ Vector3i abs() const; + _FORCE_INLINE_ Vector3i sign() const; + + /* Operators */ + + _FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v); + _FORCE_INLINE_ Vector3i operator+(const Vector3i &p_v) const; + _FORCE_INLINE_ Vector3i &operator-=(const Vector3i &p_v); + _FORCE_INLINE_ Vector3i operator-(const Vector3i &p_v) const; + _FORCE_INLINE_ Vector3i &operator*=(const Vector3i &p_v); + _FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const; + _FORCE_INLINE_ Vector3i &operator/=(const Vector3i &p_v); + _FORCE_INLINE_ Vector3i operator/(const Vector3i &p_v) const; + + _FORCE_INLINE_ Vector3i &operator*=(int32_t p_scalar); + _FORCE_INLINE_ Vector3i operator*(int32_t p_scalar) const; + _FORCE_INLINE_ Vector3i &operator/=(int32_t p_scalar); + _FORCE_INLINE_ Vector3i operator/(int32_t p_scalar) const; + + _FORCE_INLINE_ Vector3i operator-() const; + + _FORCE_INLINE_ bool operator==(const Vector3i &p_v) const; + _FORCE_INLINE_ bool operator!=(const Vector3i &p_v) const; + _FORCE_INLINE_ bool operator<(const Vector3i &p_v) const; + _FORCE_INLINE_ bool operator<=(const Vector3i &p_v) const; + _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const; + _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const; + + operator String() const; + + _FORCE_INLINE_ Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) { + x = p_x; + y = p_y; + z = p_z; + } + _FORCE_INLINE_ Vector3i() { x = y = z = 0; } +}; + +Vector3i Vector3i::abs() const { + + return Vector3i(ABS(x), ABS(y), ABS(z)); +} + +Vector3i Vector3i::sign() const { + + return Vector3i(SGN(x), SGN(y), SGN(z)); +} + +/* Operators */ + +Vector3i &Vector3i::operator+=(const Vector3i &p_v) { + + x += p_v.x; + y += p_v.y; + z += p_v.z; + return *this; +} + +Vector3i Vector3i::operator+(const Vector3i &p_v) const { + + return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z); +} + +Vector3i &Vector3i::operator-=(const Vector3i &p_v) { + + x -= p_v.x; + y -= p_v.y; + z -= p_v.z; + return *this; +} +Vector3i Vector3i::operator-(const Vector3i &p_v) const { + + return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z); +} + +Vector3i &Vector3i::operator*=(const Vector3i &p_v) { + + x *= p_v.x; + y *= p_v.y; + z *= p_v.z; + return *this; +} +Vector3i Vector3i::operator*(const Vector3i &p_v) const { + + return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z); +} + +Vector3i &Vector3i::operator/=(const Vector3i &p_v) { + + x /= p_v.x; + y /= p_v.y; + z /= p_v.z; + return *this; +} + +Vector3i Vector3i::operator/(const Vector3i &p_v) const { + + return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z); +} + +Vector3i &Vector3i::operator*=(int32_t p_scalar) { + + x *= p_scalar; + y *= p_scalar; + z *= p_scalar; + return *this; +} + +_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar, const Vector3i &p_vec) { + + return p_vec * p_scalar; +} + +Vector3i Vector3i::operator*(int32_t p_scalar) const { + + return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar); +} + +Vector3i &Vector3i::operator/=(int32_t p_scalar) { + + x /= p_scalar; + y /= p_scalar; + z /= p_scalar; + return *this; +} + +Vector3i Vector3i::operator/(int32_t p_scalar) const { + + return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar); +} + +Vector3i Vector3i::operator-() const { + + return Vector3i(-x, -y, -z); +} + +bool Vector3i::operator==(const Vector3i &p_v) const { + + return (x == p_v.x && y == p_v.y && z == p_v.z); +} + +bool Vector3i::operator!=(const Vector3i &p_v) const { + + return (x != p_v.x || y != p_v.y || z != p_v.z); +} + +bool Vector3i::operator<(const Vector3i &p_v) const { + + if (x == p_v.x) { + if (y == p_v.y) + return z < p_v.z; + else + return y < p_v.y; + } else { + return x < p_v.x; + } +} + +bool Vector3i::operator>(const Vector3i &p_v) const { + + if (x == p_v.x) { + if (y == p_v.y) + return z > p_v.z; + else + return y > p_v.y; + } else { + return x > p_v.x; + } +} + +bool Vector3i::operator<=(const Vector3i &p_v) const { + + if (x == p_v.x) { + if (y == p_v.y) + return z <= p_v.z; + else + return y < p_v.y; + } else { + return x < p_v.x; + } +} + +bool Vector3i::operator>=(const Vector3i &p_v) const { + + if (x == p_v.x) { + if (y == p_v.y) + return z >= p_v.z; + else + return y > p_v.y; + } else { + return x > p_v.x; + } +} + +void Vector3i::zero() { + + x = y = z = 0; +} + +#endif // VECTOR3I_H |