diff options
Diffstat (limited to 'core/math')
32 files changed, 174 insertions, 627 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index a54804c5dc..f0f160940d 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -106,11 +106,11 @@ void AStar3D::remove_point(int64_t p_id) { bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_MSG(!p_exists, vformat("Can't remove point. Point with id: %d doesn't exist.", p_id)); - for (OAHashMap<int64_t, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { + for (OAHashMap<int64_t, Point *>::Iterator it = p->neighbors.iter(); it.valid; it = p->neighbors.next_iter(it)) { Segment s(p_id, (*it.key)); segments.erase(s); - (*it.value)->neighbours.remove(p->id); + (*it.value)->neighbors.remove(p->id); (*it.value)->unlinked_neighbours.remove(p->id); } @@ -118,7 +118,7 @@ void AStar3D::remove_point(int64_t p_id) { Segment s(p_id, (*it.key)); segments.erase(s); - (*it.value)->neighbours.remove(p->id); + (*it.value)->neighbors.remove(p->id); (*it.value)->unlinked_neighbours.remove(p->id); } @@ -138,10 +138,10 @@ void AStar3D::connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional bool to_exists = points.lookup(p_with_id, b); ERR_FAIL_COND_MSG(!to_exists, vformat("Can't connect points. Point with id: %d doesn't exist.", p_with_id)); - a->neighbours.set(b->id, b); + a->neighbors.set(b->id, b); if (bidirectional) { - b->neighbours.set(a->id, a); + b->neighbors.set(a->id, a); } else { b->unlinked_neighbours.set(a->id, a); } @@ -155,7 +155,7 @@ void AStar3D::connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional if (element) { s.direction |= element->direction; if (s.direction == Segment::BIDIRECTIONAL) { - // Both are neighbours of each other now + // Both are neighbors of each other now a->unlinked_neighbours.remove(b->id); b->unlinked_neighbours.remove(a->id); } @@ -183,9 +183,9 @@ void AStar3D::disconnect_points(int64_t p_id, int64_t p_with_id, bool bidirectio // Erase the directions to be removed s.direction = (element->direction & ~remove_direction); - a->neighbours.remove(b->id); + a->neighbors.remove(b->id); if (bidirectional) { - b->neighbours.remove(a->id); + b->neighbors.remove(a->id); if (element->direction != Segment::BIDIRECTIONAL) { a->unlinked_neighbours.remove(b->id); b->unlinked_neighbours.remove(a->id); @@ -226,7 +226,7 @@ Vector<int64_t> AStar3D::get_point_connections(int64_t p_id) { Vector<int64_t> point_list; - for (OAHashMap<int64_t, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { + for (OAHashMap<int64_t, Point *>::Iterator it = p->neighbors.iter(); it.valid; it = p->neighbors.next_iter(it)) { point_list.push_back((*it.key)); } @@ -327,7 +327,7 @@ bool AStar3D::_solve(Point *begin_point, Point *end_point) { bool found_route = false; - Vector<Point *> open_list; + LocalVector<Point *> open_list; SortArray<Point *, SortPoints> sorter; begin_point->g_score = 0; @@ -335,19 +335,19 @@ bool AStar3D::_solve(Point *begin_point, Point *end_point) { open_list.push_back(begin_point); while (!open_list.is_empty()) { - Point *p = open_list[0]; // The currently processed point + Point *p = open_list[0]; // The currently processed point. if (p == end_point) { found_route = true; break; } - sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list + sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list. open_list.remove_at(open_list.size() - 1); - p->closed_pass = pass; // Mark the point as closed + p->closed_pass = pass; // Mark the point as closed. - for (OAHashMap<int64_t, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - Point *e = *(it.value); // The neighbour point + for (OAHashMap<int64_t, Point *>::Iterator it = p->neighbors.iter(); it.valid; it = p->neighbors.next_iter(it)) { + Point *e = *(it.value); // The neighbor point. if (!e->enabled || e->closed_pass == pass) { continue; @@ -370,9 +370,9 @@ bool AStar3D::_solve(Point *begin_point, Point *end_point) { e->f_score = e->g_score + _estimate_cost(e->id, end_point->id); if (new_point) { // The position of the new points is already known. - sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw()); + sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr()); } else { - sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw()); + sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr()); } } } @@ -794,7 +794,7 @@ bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point) { bool found_route = false; - Vector<AStar3D::Point *> open_list; + LocalVector<AStar3D::Point *> open_list; SortArray<AStar3D::Point *, AStar3D::SortPoints> sorter; begin_point->g_score = 0; @@ -802,19 +802,19 @@ bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point) { open_list.push_back(begin_point); while (!open_list.is_empty()) { - AStar3D::Point *p = open_list[0]; // The currently processed point + AStar3D::Point *p = open_list[0]; // The currently processed point. if (p == end_point) { found_route = true; break; } - sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list + sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list. open_list.remove_at(open_list.size() - 1); - p->closed_pass = astar.pass; // Mark the point as closed + p->closed_pass = astar.pass; // Mark the point as closed. - for (OAHashMap<int64_t, AStar3D::Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - AStar3D::Point *e = *(it.value); // The neighbour point + for (OAHashMap<int64_t, AStar3D::Point *>::Iterator it = p->neighbors.iter(); it.valid; it = p->neighbors.next_iter(it)) { + AStar3D::Point *e = *(it.value); // The neighbor point. if (!e->enabled || e->closed_pass == astar.pass) { continue; @@ -837,9 +837,9 @@ bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point) { e->f_score = e->g_score + _estimate_cost(e->id, end_point->id); if (new_point) { // The position of the new points is already known. - sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw()); + sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr()); } else { - sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw()); + sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr()); } } } diff --git a/core/math/a_star.h b/core/math/a_star.h index a475e4f2fc..fc4bb09f03 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -52,7 +52,7 @@ class AStar3D : public RefCounted { real_t weight_scale = 0; bool enabled = false; - OAHashMap<int64_t, Point *> neighbours = 4u; + OAHashMap<int64_t, Point *> neighbors = 4u; OAHashMap<int64_t, Point *> unlinked_neighbours = 4u; // Used for pathfinding. diff --git a/core/math/a_star_grid_2d.cpp b/core/math/a_star_grid_2d.cpp index 30d50073d7..139dc3afb1 100644 --- a/core/math/a_star_grid_2d.cpp +++ b/core/math/a_star_grid_2d.cpp @@ -265,7 +265,7 @@ AStarGrid2D::Point *AStarGrid2D::_jump(Point *p_from, Point *p_to) { return nullptr; } -void AStarGrid2D::_get_nbors(Point *p_point, List<Point *> &r_nbors) { +void AStarGrid2D::_get_nbors(Point *p_point, LocalVector<Point *> &r_nbors) { bool ts0 = false, td0 = false, ts1 = false, td1 = false, ts2 = false, td2 = false, @@ -378,7 +378,7 @@ bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point) { bool found_route = false; - Vector<Point *> open_list; + LocalVector<Point *> open_list; SortArray<Point *, SortPoints> sorter; p_begin_point->g_score = 0; @@ -394,14 +394,14 @@ bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point) { break; } - sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list. + sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list. open_list.remove_at(open_list.size() - 1); p->closed_pass = pass; // Mark the point as closed. - List<Point *> nbors; + LocalVector<Point *> nbors; _get_nbors(p, nbors); - for (List<Point *>::Element *E = nbors.front(); E; E = E->next()) { - Point *e = E->get(); // The neighbour point. + + for (Point *e : nbors) { real_t weight_scale = 1.0; if (jumping_enabled) { @@ -433,9 +433,9 @@ bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point) { e->f_score = e->g_score + _estimate_cost(e->id, p_end_point->id); if (new_point) { // The position of the new points is already known. - sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw()); + sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr()); } else { - sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw()); + sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr()); } } } diff --git a/core/math/a_star_grid_2d.h b/core/math/a_star_grid_2d.h index 6b9012a5e3..e4e62ec360 100644 --- a/core/math/a_star_grid_2d.h +++ b/core/math/a_star_grid_2d.h @@ -124,7 +124,7 @@ private: // Internal routines. return &points[p_y][p_x]; } - void _get_nbors(Point *p_point, List<Point *> &r_nbors); + void _get_nbors(Point *p_point, LocalVector<Point *> &r_nbors); Point *_jump(Point *p_from, Point *p_to); bool _solve(Point *p_begin_point, Point *p_end_point); diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 39e383fb49..95a4187062 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -36,23 +36,6 @@ #define cofac(row1, col1, row2, col2) \ (rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1]) -void Basis::from_z(const Vector3 &p_z) { - if (Math::abs(p_z.z) > (real_t)Math_SQRT12) { - // choose p in y-z plane - real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2]; - real_t k = 1.0f / Math::sqrt(a); - rows[0] = Vector3(0, -p_z[2] * k, p_z[1] * k); - rows[1] = Vector3(a * k, -p_z[0] * rows[0][2], p_z[0] * rows[0][1]); - } else { - // choose p in x-y plane - real_t a = p_z.x * p_z.x + p_z.y * p_z.y; - real_t k = 1.0f / Math::sqrt(a); - rows[0] = Vector3(-p_z.y * k, p_z.x * k, 0); - rows[1] = Vector3(-p_z.z * rows[0].y, p_z.z * rows[0].x, a * k); - } - rows[2] = p_z; -} - void Basis::invert() { real_t co[3] = { cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1) @@ -256,13 +239,18 @@ void Basis::scale_orthogonal(const Vector3 &p_scale) { Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const { Basis m = *this; Vector3 s = Vector3(-1, -1, -1) + p_scale; + bool sign = signbit(s.x + s.y + s.z); + Basis b = m.orthonormalized(); + s = b.xform_inv(s); Vector3 dots; - Basis b; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { dots[j] += s[i] * abs(m.get_column(i).normalized().dot(b.get_column(j))); } } + if (sign != signbit(dots.x + dots.y + dots.z)) { + dots = -dots; + } m.scale_local(Vector3(1, 1, 1) + dots); return m; } @@ -271,14 +259,6 @@ float Basis::get_uniform_scale() const { return (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f; } -void Basis::make_scale_uniform() { - float l = (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f; - for (int i = 0; i < 3; i++) { - rows[i].normalize(); - rows[i] *= l; - } -} - Basis Basis::scaled_local(const Vector3 &p_scale) const { return (*this) * Basis::from_scale(p_scale); } diff --git a/core/math/basis.h b/core/math/basis.h index b3197dbc84..bbc1d40469 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -56,8 +56,6 @@ struct _NO_DISCARD_ Basis { _FORCE_INLINE_ real_t determinant() const; - void from_z(const Vector3 &p_z); - void rotate(const Vector3 &p_axis, real_t p_angle); Basis rotated(const Vector3 &p_axis, real_t p_angle) const; @@ -101,8 +99,6 @@ struct _NO_DISCARD_ Basis { void scale_orthogonal(const Vector3 &p_scale); Basis scaled_orthogonal(const Vector3 &p_scale) const; - - void make_scale_uniform(); float get_uniform_scale() const; Vector3 get_scale() const; diff --git a/core/math/bvh.h b/core/math/bvh.h index 9de704834b..ea8289607e 100644 --- a/core/math/bvh.h +++ b/core/math/bvh.h @@ -444,9 +444,7 @@ private: params.result_array = nullptr; params.subindex_array = nullptr; - for (unsigned int n = 0; n < changed_items.size(); n++) { - const BVHHandle &h = changed_items[n]; - + for (const BVHHandle &h : changed_items) { // use the expanded aabb for pairing const BOUNDS &expanded_aabb = tree._pairs[h.id()].expanded_aabb; BVHABB_CLASS abb; @@ -465,9 +463,7 @@ private: params.result_count_overall = 0; // might not be needed tree.cull_aabb(params, false); - for (unsigned int i = 0; i < tree._cull_hits.size(); i++) { - uint32_t ref_id = tree._cull_hits[i]; - + for (const uint32_t ref_id : tree._cull_hits) { // don't collide against ourself if (ref_id == changed_item_ref_id) { continue; @@ -784,7 +780,7 @@ private: if (p_thread_safe) { _mutex = p_mutex; - if (_mutex->try_lock() != OK) { + if (!_mutex->try_lock()) { WARN_PRINT("Info : multithread BVH access detected (benign)"); _mutex->lock(); } diff --git a/core/math/bvh_abb.h b/core/math/bvh_abb.h index 32b011bd3b..fb0207e0bd 100644 --- a/core/math/bvh_abb.h +++ b/core/math/bvh_abb.h @@ -87,7 +87,7 @@ struct BVH_ABB { return -neg_max - min; } - POINT calculate_centre() const { + POINT calculate_center() const { return POINT((calculate_size() * 0.5) + min); } diff --git a/core/math/bvh_split.inc b/core/math/bvh_split.inc index 180bbfb511..875abedb70 100644 --- a/core/math/bvh_split.inc +++ b/core/math/bvh_split.inc @@ -25,7 +25,7 @@ void _split_leaf_sort_groups_simple(int &num_a, int &num_b, uint16_t *group_a, u return; } - POINT centre = full_bound.calculate_centre(); + POINT center = full_bound.calculate_center(); POINT size = full_bound.calculate_size(); int order[POINT::AXIS_COUNT]; @@ -43,7 +43,7 @@ void _split_leaf_sort_groups_simple(int &num_a, int &num_b, uint16_t *group_a, u for (int a = 0; a < num_a; a++) { uint32_t ind = group_a[a]; - if (temp_bounds[ind].min.coord[split_axis] > centre.coord[split_axis]) { + if (temp_bounds[ind].min.coord[split_axis] > center.coord[split_axis]) { // add to b group_b[num_b++] = ind; @@ -75,7 +75,7 @@ void _split_leaf_sort_groups_simple(int &num_a, int &num_b, uint16_t *group_a, u for (int a = 0; a < num_a; a++) { uint32_t ind = group_a[a]; - if (temp_bounds[ind].min.coord[split_axis] > centre.coord[split_axis]) { + if (temp_bounds[ind].min.coord[split_axis] > center.coord[split_axis]) { count++; } } @@ -100,7 +100,7 @@ void _split_leaf_sort_groups_simple(int &num_a, int &num_b, uint16_t *group_a, u for (int a = 0; a < num_a; a++) { uint32_t ind = group_a[a]; - if (temp_bounds[ind].min.coord[split_axis] > centre.coord[split_axis]) { + if (temp_bounds[ind].min.coord[split_axis] > center.coord[split_axis]) { // add to b group_b[num_b++] = ind; diff --git a/core/math/color.cpp b/core/math/color.cpp index 5bae8d25d6..3e5fa7b402 100644 --- a/core/math/color.cpp +++ b/core/math/color.cpp @@ -194,7 +194,7 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) { a = p_alpha; if (p_s == 0.0f) { - // Achromatic (grey) + // Achromatic (gray) r = g = b = p_v; return; } diff --git a/core/math/delaunay_3d.h b/core/math/delaunay_3d.h index 8ca38d571a..55923e0133 100644 --- a/core/math/delaunay_3d.h +++ b/core/math/delaunay_3d.h @@ -313,20 +313,20 @@ public: //remove simplex and continue simplex_list.erase(simplex->SE); - for (uint32_t k = 0; k < simplex->grid_positions.size(); k++) { - Vector3i p = simplex->grid_positions[k].pos; - acceleration_grid[p.x][p.y][p.z].erase(simplex->grid_positions[k].E); + for (const GridPos &gp : simplex->grid_positions) { + Vector3i p = gp.pos; + acceleration_grid[p.x][p.y][p.z].erase(gp.E); } memdelete(simplex); } E = N; } - for (uint32_t j = 0; j < triangles.size(); j++) { - if (triangles[j].bad) { + for (const Triangle &triangle : triangles) { + if (triangle.bad) { continue; } - Simplex *new_simplex = memnew(Simplex(triangles[j].triangle[0], triangles[j].triangle[1], triangles[j].triangle[2], i)); + Simplex *new_simplex = memnew(Simplex(triangle.triangle[0], triangle.triangle[1], triangle.triangle[2], i)); circum_sphere_compute(points, new_simplex); new_simplex->SE = simplex_list.push_back(new_simplex); { diff --git a/core/math/dynamic_bvh.cpp b/core/math/dynamic_bvh.cpp index 4452445241..e1315d1c64 100644 --- a/core/math/dynamic_bvh.cpp +++ b/core/math/dynamic_bvh.cpp @@ -36,8 +36,8 @@ void DynamicBVH::_delete_node(Node *p_node) { void DynamicBVH::_recurse_delete_node(Node *p_node) { if (!p_node->is_leaf()) { - _recurse_delete_node(p_node->childs[0]); - _recurse_delete_node(p_node->childs[1]); + _recurse_delete_node(p_node->children[0]); + _recurse_delete_node(p_node->children[1]); } if (p_node == bvh_root) { bvh_root = nullptr; @@ -65,31 +65,31 @@ void DynamicBVH::_insert_leaf(Node *p_root, Node *p_leaf) { } else { if (!p_root->is_leaf()) { do { - p_root = p_root->childs[p_leaf->volume.select_by_proximity( - p_root->childs[0]->volume, - p_root->childs[1]->volume)]; + p_root = p_root->children[p_leaf->volume.select_by_proximity( + p_root->children[0]->volume, + p_root->children[1]->volume)]; } while (!p_root->is_leaf()); } Node *prev = p_root->parent; Node *node = _create_node_with_volume(prev, p_leaf->volume.merge(p_root->volume), nullptr); if (prev) { - prev->childs[p_root->get_index_in_parent()] = node; - node->childs[0] = p_root; + prev->children[p_root->get_index_in_parent()] = node; + node->children[0] = p_root; p_root->parent = node; - node->childs[1] = p_leaf; + node->children[1] = p_leaf; p_leaf->parent = node; do { if (!prev->volume.contains(node->volume)) { - prev->volume = prev->childs[0]->volume.merge(prev->childs[1]->volume); + prev->volume = prev->children[0]->volume.merge(prev->children[1]->volume); } else { break; } node = prev; } while (nullptr != (prev = node->parent)); } else { - node->childs[0] = p_root; + node->children[0] = p_root; p_root->parent = node; - node->childs[1] = p_leaf; + node->children[1] = p_leaf; p_leaf->parent = node; bvh_root = node; } @@ -103,14 +103,14 @@ DynamicBVH::Node *DynamicBVH::_remove_leaf(Node *leaf) { } else { Node *parent = leaf->parent; Node *prev = parent->parent; - Node *sibling = parent->childs[1 - leaf->get_index_in_parent()]; + Node *sibling = parent->children[1 - leaf->get_index_in_parent()]; if (prev) { - prev->childs[parent->get_index_in_parent()] = sibling; + prev->children[parent->get_index_in_parent()] = sibling; sibling->parent = prev; _delete_node(parent); while (prev) { const Volume pb = prev->volume; - prev->volume = prev->childs[0]->volume.merge(prev->childs[1]->volume); + prev->volume = prev->children[0]->volume.merge(prev->children[1]->volume); if (pb.is_not_equal_to(prev->volume)) { prev = prev->parent; } else { @@ -129,8 +129,8 @@ DynamicBVH::Node *DynamicBVH::_remove_leaf(Node *leaf) { void DynamicBVH::_fetch_leaves(Node *p_root, LocalVector<Node *> &r_leaves, int p_depth) { if (p_root->is_internal() && p_depth) { - _fetch_leaves(p_root->childs[0], r_leaves, p_depth - 1); - _fetch_leaves(p_root->childs[1], r_leaves, p_depth - 1); + _fetch_leaves(p_root->children[0], r_leaves, p_depth - 1); + _fetch_leaves(p_root->children[1], r_leaves, p_depth - 1); _delete_node(p_root); } else { r_leaves.push_back(p_root); @@ -195,8 +195,8 @@ void DynamicBVH::_bottom_up(Node **leaves, int p_count) { } Node *n[] = { leaves[minidx[0]], leaves[minidx[1]] }; Node *p = _create_node_with_volume(nullptr, n[0]->volume.merge(n[1]->volume), nullptr); - p->childs[0] = n[0]; - p->childs[1] = n[1]; + p->children[0] = n[0]; + p->children[1] = n[1]; n[0]->parent = p; n[1]->parent = p; leaves[minidx[0]] = p; @@ -241,10 +241,10 @@ DynamicBVH::Node *DynamicBVH::_top_down(Node **leaves, int p_count, int p_bu_thr } Node *node = _create_node_with_volume(nullptr, vol, nullptr); - node->childs[0] = _top_down(&leaves[0], partition, p_bu_threshold); - node->childs[1] = _top_down(&leaves[partition], p_count - partition, p_bu_threshold); - node->childs[0]->parent = node; - node->childs[1]->parent = node; + node->children[0] = _top_down(&leaves[0], partition, p_bu_threshold); + node->children[1] = _top_down(&leaves[partition], p_count - partition, p_bu_threshold); + node->children[0]->parent = node; + node->children[1]->parent = node; return (node); } else { _bottom_up(leaves, p_count); @@ -260,23 +260,23 @@ DynamicBVH::Node *DynamicBVH::_node_sort(Node *n, Node *&r) { if (p > n) { const int i = n->get_index_in_parent(); const int j = 1 - i; - Node *s = p->childs[j]; + Node *s = p->children[j]; Node *q = p->parent; - ERR_FAIL_COND_V(n != p->childs[i], nullptr); + ERR_FAIL_COND_V(n != p->children[i], nullptr); if (q) { - q->childs[p->get_index_in_parent()] = n; + q->children[p->get_index_in_parent()] = n; } else { r = n; } s->parent = n; p->parent = n; n->parent = q; - p->childs[0] = n->childs[0]; - p->childs[1] = n->childs[1]; - n->childs[0]->parent = p; - n->childs[1]->parent = p; - n->childs[i] = p; - n->childs[j] = s; + p->children[0] = n->children[0]; + p->children[1] = n->children[1]; + n->children[0]->parent = p; + n->children[1]->parent = p; + n->children[i] = p; + n->children[j] = s; SWAP(p->volume, n->volume); return (p); } @@ -320,7 +320,7 @@ void DynamicBVH::optimize_incremental(int passes) { Node *node = bvh_root; unsigned bit = 0; while (node->is_internal()) { - node = _node_sort(node, bvh_root)->childs[(opath >> bit) & 1]; + node = _node_sort(node, bvh_root)->children[(opath >> bit) & 1]; bit = (bit + 1) & (sizeof(unsigned) * 8 - 1); } _update(node); @@ -396,8 +396,8 @@ void DynamicBVH::remove(const ID &p_id) { void DynamicBVH::_extract_leaves(Node *p_node, List<ID> *r_elements) { if (p_node->is_internal()) { - _extract_leaves(p_node->childs[0], r_elements); - _extract_leaves(p_node->childs[1], r_elements); + _extract_leaves(p_node->children[0], r_elements); + _extract_leaves(p_node->children[1], r_elements); } else { ID id; id.node = p_node; diff --git a/core/math/dynamic_bvh.h b/core/math/dynamic_bvh.h index 46bf56ae58..21b5340aaa 100644 --- a/core/math/dynamic_bvh.h +++ b/core/math/dynamic_bvh.h @@ -182,21 +182,21 @@ private: Volume volume; Node *parent = nullptr; union { - Node *childs[2]; + Node *children[2]; void *data; }; - _FORCE_INLINE_ bool is_leaf() const { return childs[1] == nullptr; } + _FORCE_INLINE_ bool is_leaf() const { return children[1] == nullptr; } _FORCE_INLINE_ bool is_internal() const { return (!is_leaf()); } _FORCE_INLINE_ int get_index_in_parent() const { ERR_FAIL_COND_V(!parent, 0); - return (parent->childs[1] == this) ? 1 : 0; + return (parent->children[1] == this) ? 1 : 0; } void get_max_depth(int depth, int &maxdepth) { if (is_internal()) { - childs[0]->get_max_depth(depth + 1, maxdepth); - childs[1]->get_max_depth(depth + 1, maxdepth); + children[0]->get_max_depth(depth + 1, maxdepth); + children[1]->get_max_depth(depth + 1, maxdepth); } else { maxdepth = MAX(maxdepth, depth); } @@ -205,7 +205,7 @@ private: // int count_leaves() const { if (is_internal()) { - return childs[0]->count_leaves() + childs[1]->count_leaves(); + return children[0]->count_leaves() + children[1]->count_leaves(); } else { return (1); } @@ -216,8 +216,8 @@ private: } Node() { - childs[0] = nullptr; - childs[1] = nullptr; + children[0] = nullptr; + children[1] = nullptr; } }; @@ -350,8 +350,8 @@ void DynamicBVH::aabb_query(const AABB &p_box, QueryResult &r_result) { stack = aux_stack.ptr(); threshold = aux_stack.size() - 2; } - stack[depth++] = n->childs[0]; - stack[depth++] = n->childs[1]; + stack[depth++] = n->children[0]; + stack[depth++] = n->children[1]; } else { if (r_result(n->data)) { return; @@ -406,8 +406,8 @@ void DynamicBVH::convex_query(const Plane *p_planes, int p_plane_count, const Ve stack = aux_stack.ptr(); threshold = aux_stack.size() - 2; } - stack[depth++] = n->childs[0]; - stack[depth++] = n->childs[1]; + stack[depth++] = n->children[0]; + stack[depth++] = n->children[1]; } else { if (r_result(n->data)) { return; @@ -463,8 +463,8 @@ void DynamicBVH::ray_query(const Vector3 &p_from, const Vector3 &p_to, QueryResu stack = aux_stack.ptr(); threshold = aux_stack.size() - 2; } - stack[depth++] = node->childs[0]; - stack[depth++] = node->childs[1]; + stack[depth++] = node->children[0]; + stack[depth++] = node->children[1]; } else { if (r_result(node->data)) { return; diff --git a/core/math/expression.cpp b/core/math/expression.cpp index da52bb9465..d1ec987d56 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -434,14 +434,13 @@ Error Expression::_get_token(Token &r_token) { } return OK; - } else if (is_ascii_char(cchar) || is_underscore(cchar)) { - String id; - bool first = true; + } else if (is_unicode_identifier_start(cchar)) { + String id = String::chr(cchar); + cchar = GET_CHAR(); - while (is_ascii_char(cchar) || is_underscore(cchar) || (!first && is_digit(cchar))) { + while (is_unicode_identifier_continue(cchar)) { id += String::chr(cchar); cchar = GET_CHAR(); - first = false; } str_ofs--; //go back one diff --git a/core/math/face3.cpp b/core/math/face3.cpp index e53bbf872b..1dff0ee4a6 100644 --- a/core/math/face3.cpp +++ b/core/math/face3.cpp @@ -120,36 +120,6 @@ bool Face3::is_degenerate() const { return (normal.length_squared() < (real_t)CMP_EPSILON2); } -Face3::Side Face3::get_side_of(const Face3 &p_face, ClockDirection p_clock_dir) const { - int over = 0, under = 0; - - Plane plane = get_plane(p_clock_dir); - - for (int i = 0; i < 3; i++) { - const Vector3 &v = p_face.vertex[i]; - - if (plane.has_point(v)) { //coplanar, don't bother - continue; - } - - if (plane.is_point_over(v)) { - over++; - } else { - under++; - } - } - - if (over > 0 && under == 0) { - return SIDE_OVER; - } else if (under > 0 && over == 0) { - return SIDE_UNDER; - } else if (under == 0 && over == 0) { - return SIDE_COPLANAR; - } else { - return SIDE_SPANNING; - } -} - Vector3 Face3::get_random_point_inside() const { real_t a = Math::random(0.0, 1.0); real_t b = Math::random(0.0, 1.0); @@ -164,20 +134,10 @@ Plane Face3::get_plane(ClockDirection p_dir) const { return Plane(vertex[0], vertex[1], vertex[2], p_dir); } -Vector3 Face3::get_median_point() const { - return (vertex[0] + vertex[1] + vertex[2]) / 3.0f; -} - real_t Face3::get_area() const { return vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]).length() * 0.5f; } -ClockDirection Face3::get_clock_dir() const { - Vector3 normal = vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]); - //printf("normal is %g,%g,%g x %g,%g,%g- wtfu is %g\n",tofloat(normal.x),tofloat(normal.y),tofloat(normal.z),tofloat(vertex[0].x),tofloat(vertex[0].y),tofloat(vertex[0].z),tofloat( normal.dot( vertex[0] ) ) ); - return (normal.dot(vertex[0]) >= 0) ? CLOCKWISE : COUNTERCLOCKWISE; -} - bool Face3::intersects_aabb(const AABB &p_aabb) const { /** TEST PLANE **/ if (!p_aabb.intersects_plane(get_plane())) { diff --git a/core/math/face3.h b/core/math/face3.h index 3d87de03dc..3dd47d0226 100644 --- a/core/math/face3.h +++ b/core/math/face3.h @@ -57,19 +57,14 @@ struct _NO_DISCARD_ Face3 { Plane get_plane(ClockDirection p_dir = CLOCKWISE) const; Vector3 get_random_point_inside() const; - Side get_side_of(const Face3 &p_face, ClockDirection p_clock_dir = CLOCKWISE) const; - bool is_degenerate() const; real_t get_area() const; - Vector3 get_median_point() const; Vector3 get_closest_point_to(const Vector3 &p_point) const; bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const; bool intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const; - ClockDirection get_clock_dir() const; ///< todo, test if this is returning the proper clockwisity - void get_support(const Vector3 &p_normal, const Transform3D &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const; void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const; diff --git a/core/math/geometry_2d.cpp b/core/math/geometry_2d.cpp index a0b76d31cb..74cb92539a 100644 --- a/core/math/geometry_2d.cpp +++ b/core/math/geometry_2d.cpp @@ -320,41 +320,6 @@ Vector<Vector<Point2>> Geometry2D::_polypath_offset(const Vector<Point2> &p_poly return polypaths; } -Vector<Point2i> Geometry2D::pack_rects(const Vector<Size2i> &p_sizes, const Size2i &p_atlas_size) { - Vector<stbrp_node> nodes; - nodes.resize(p_atlas_size.width); - - stbrp_context context; - stbrp_init_target(&context, p_atlas_size.width, p_atlas_size.height, nodes.ptrw(), p_atlas_size.width); - - Vector<stbrp_rect> rects; - rects.resize(p_sizes.size()); - - for (int i = 0; i < p_sizes.size(); i++) { - rects.write[i].id = 0; - rects.write[i].w = p_sizes[i].width; - rects.write[i].h = p_sizes[i].height; - rects.write[i].x = 0; - rects.write[i].y = 0; - rects.write[i].was_packed = 0; - } - - int res = stbrp_pack_rects(&context, rects.ptrw(), rects.size()); - if (res == 0) { //pack failed - return Vector<Point2i>(); - } - - Vector<Point2i> ret; - ret.resize(p_sizes.size()); - - for (int i = 0; i < p_sizes.size(); i++) { - Point2i r(rects[i].x, rects[i].y); - ret.write[i] = r; - } - - return ret; -} - Vector<Vector3i> Geometry2D::partial_pack_rects(const Vector<Vector2i> &p_sizes, const Size2i &p_atlas_size) { Vector<stbrp_node> nodes; nodes.resize(p_atlas_size.width); diff --git a/core/math/geometry_2d.h b/core/math/geometry_2d.h index b55aecf85e..0e5702e0af 100644 --- a/core/math/geometry_2d.h +++ b/core/math/geometry_2d.h @@ -464,7 +464,6 @@ public: static Vector<Vector<Vector2>> decompose_polygon_in_convex(Vector<Point2> polygon); static void make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size); - static Vector<Point2i> pack_rects(const Vector<Size2i> &p_sizes, const Size2i &p_atlas_size); static Vector<Vector3i> partial_pack_rects(const Vector<Vector2i> &p_sizes, const Size2i &p_atlas_size); private: diff --git a/core/math/geometry_3d.cpp b/core/math/geometry_3d.cpp index 3ac78e0709..590483bf20 100644 --- a/core/math/geometry_3d.cpp +++ b/core/math/geometry_3d.cpp @@ -141,21 +141,19 @@ real_t Geometry3D::get_closest_distance_between_segments(const Vector3 &p_p0, co void Geometry3D::MeshData::optimize_vertices() { HashMap<int, int> vtx_remap; - for (uint32_t i = 0; i < faces.size(); i++) { - for (uint32_t j = 0; j < faces[i].indices.size(); j++) { - int idx = faces[i].indices[j]; - if (!vtx_remap.has(idx)) { + for (MeshData::Face &face : faces) { + for (int &index : face.indices) { + if (!vtx_remap.has(index)) { int ni = vtx_remap.size(); - vtx_remap[idx] = ni; + vtx_remap[index] = ni; } - - faces[i].indices[j] = vtx_remap[idx]; + index = vtx_remap[index]; } } - for (uint32_t i = 0; i < edges.size(); i++) { - int a = edges[i].vertex_a; - int b = edges[i].vertex_b; + for (MeshData::Edge &edge : edges) { + int a = edge.vertex_a; + int b = edge.vertex_b; if (!vtx_remap.has(a)) { int ni = vtx_remap.size(); @@ -166,8 +164,8 @@ void Geometry3D::MeshData::optimize_vertices() { vtx_remap[b] = ni; } - edges[i].vertex_a = vtx_remap[a]; - edges[i].vertex_b = vtx_remap[b]; + edge.vertex_a = vtx_remap[a]; + edge.vertex_b = vtx_remap[b]; } LocalVector<Vector3> new_vertices; @@ -198,149 +196,6 @@ struct _FaceClassify { _FaceClassify() {} }; -static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) { - // Connect faces, error will occur if an edge is shared between more than 2 faces. - // Clear connections. - - bool error = false; - - for (int i = 0; i < len; i++) { - for (int j = 0; j < 3; j++) { - p_faces[i].links[j].clear(); - } - } - - for (int i = 0; i < len; i++) { - if (p_faces[i].group != p_group) { - continue; - } - for (int j = i + 1; j < len; j++) { - if (p_faces[j].group != p_group) { - continue; - } - - for (int k = 0; k < 3; k++) { - Vector3 vi1 = p_faces[i].face.vertex[k]; - Vector3 vi2 = p_faces[i].face.vertex[(k + 1) % 3]; - - for (int l = 0; l < 3; l++) { - Vector3 vj2 = p_faces[j].face.vertex[l]; - Vector3 vj1 = p_faces[j].face.vertex[(l + 1) % 3]; - - if (vi1.distance_to(vj1) < 0.00001f && - vi2.distance_to(vj2) < 0.00001f) { - if (p_faces[i].links[k].face != -1) { - ERR_PRINT("already linked\n"); - error = true; - break; - } - if (p_faces[j].links[l].face != -1) { - ERR_PRINT("already linked\n"); - error = true; - break; - } - - p_faces[i].links[k].face = j; - p_faces[i].links[k].edge = l; - p_faces[j].links[l].face = i; - p_faces[j].links[l].edge = k; - } - } - if (error) { - break; - } - } - if (error) { - break; - } - } - if (error) { - break; - } - } - - for (int i = 0; i < len; i++) { - p_faces[i].valid = true; - for (int j = 0; j < 3; j++) { - if (p_faces[i].links[j].face == -1) { - p_faces[i].valid = false; - } - } - } - return error; -} - -static bool _group_face(_FaceClassify *p_faces, int len, int p_index, int p_group) { - if (p_faces[p_index].group >= 0) { - return false; - } - - p_faces[p_index].group = p_group; - - for (int i = 0; i < 3; i++) { - ERR_FAIL_INDEX_V(p_faces[p_index].links[i].face, len, true); - _group_face(p_faces, len, p_faces[p_index].links[i].face, p_group); - } - - return true; -} - -Vector<Vector<Face3>> Geometry3D::separate_objects(Vector<Face3> p_array) { - Vector<Vector<Face3>> objects; - - int len = p_array.size(); - - const Face3 *arrayptr = p_array.ptr(); - - Vector<_FaceClassify> fc; - - fc.resize(len); - - _FaceClassify *_fcptr = fc.ptrw(); - - for (int i = 0; i < len; i++) { - _fcptr[i].face = arrayptr[i]; - } - - bool error = _connect_faces(_fcptr, len, -1); - - ERR_FAIL_COND_V_MSG(error, Vector<Vector<Face3>>(), "Invalid geometry."); - - // Group connected faces in separate objects. - - int group = 0; - for (int i = 0; i < len; i++) { - if (!_fcptr[i].valid) { - continue; - } - if (_group_face(_fcptr, len, i, group)) { - group++; - } - } - - // Group connected faces in separate objects. - - for (int i = 0; i < len; i++) { - _fcptr[i].face = arrayptr[i]; - } - - if (group >= 0) { - objects.resize(group); - Vector<Face3> *group_faces = objects.ptrw(); - - for (int i = 0; i < len; i++) { - if (!_fcptr[i].valid) { - continue; - } - if (_fcptr[i].group >= 0 && _fcptr[i].group < group) { - group_faces[_fcptr[i].group].push_back(_fcptr[i].face); - } - } - } - - return objects; -} - /*** GEOMETRY WRAPPER ***/ enum _CellFlags { @@ -748,7 +603,7 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes Vector3 right = p.normal.cross(ref).normalized(); Vector3 up = p.normal.cross(right).normalized(); - Vector3 center = p.center(); + Vector3 center = p.get_center(); // make a quad clockwise LocalVector<Vector3> vertices = { @@ -816,10 +671,10 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes MeshData::Face face; // Add face indices. - for (uint32_t j = 0; j < vertices.size(); j++) { + for (const Vector3 &vertex : vertices) { int idx = -1; for (uint32_t k = 0; k < mesh.vertices.size(); k++) { - if (mesh.vertices[k].distance_to(vertices[j]) < 0.001f) { + if (mesh.vertices[k].distance_to(vertex) < 0.001f) { idx = k; break; } @@ -827,7 +682,7 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes if (idx == -1) { idx = mesh.vertices.size(); - mesh.vertices.push_back(vertices[j]); + mesh.vertices.push_back(vertex); } face.indices.push_back(idx); diff --git a/core/math/geometry_3d.h b/core/math/geometry_3d.h index 6759db5766..99c554fe05 100644 --- a/core/math/geometry_3d.h +++ b/core/math/geometry_3d.h @@ -532,8 +532,6 @@ public: return clipped; } - static Vector<Vector<Face3>> separate_objects(Vector<Face3> p_array); - // Create a "wrap" that encloses the given geometry. static Vector<Face3> wrap_geometry(Vector<Face3> p_array, real_t *p_error = nullptr); diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 0fa82bb8c1..078320d620 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -31,6 +31,7 @@ #ifndef MATH_FUNCS_H #define MATH_FUNCS_H +#include "core/error/error_macros.h" #include "core/math/math_defs.h" #include "core/math/random_pcg.h" #include "core/typedefs.h" @@ -225,6 +226,7 @@ public: } static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) { + ERR_FAIL_COND_V_MSG(p_y == 0, 0, "Division by zero in posmod is undefined. Returning 0 as fallback."); int64_t value = p_x % p_y; if (((value < 0) && (p_y > 0)) || ((value > 0) && (p_y < 0))) { value += p_y; diff --git a/core/math/plane.h b/core/math/plane.h index 3bc7c54b9d..8159f25342 100644 --- a/core/math/plane.h +++ b/core/math/plane.h @@ -47,7 +47,7 @@ struct _NO_DISCARD_ Plane { /* Plane-Point operations */ - _FORCE_INLINE_ Vector3 center() const { return normal * d; } + _FORCE_INLINE_ Vector3 get_center() const { return normal * d; } Vector3 get_any_perpendicular_normal() const; _FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane diff --git a/core/math/quick_hull.cpp b/core/math/quick_hull.cpp index 9488c6bcff..4483f61bc4 100644 --- a/core/math/quick_hull.cpp +++ b/core/math/quick_hull.cpp @@ -383,15 +383,15 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_ if (O->get().plane.is_equal_approx(f.plane)) { //merge and delete edge and contiguous face, while repointing edges (uuugh!) - int ois = O->get().indices.size(); + int o_index_size = O->get().indices.size(); - for (int j = 0; j < ois; j++) { + for (int j = 0; j < o_index_size; j++) { //search a if (O->get().indices[j] == a) { //append the rest - for (int k = 0; k < ois; k++) { - int idx = O->get().indices[(k + j) % ois]; - int idxn = O->get().indices[(k + j + 1) % ois]; + for (int k = 0; k < o_index_size; k++) { + int idx = O->get().indices[(k + j) % o_index_size]; + int idxn = O->get().indices[(k + j + 1) % o_index_size]; if (idx == b && idxn == a) { //already have b! break; } diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index 6a7ee32230..868665cb5c 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -151,7 +151,7 @@ void Transform2D::orthonormalize() { Vector2 y = columns[1]; x.normalize(); - y = (y - x * (x.dot(y))); + y = y - x * x.dot(y); y.normalize(); columns[0] = x; @@ -159,9 +159,9 @@ void Transform2D::orthonormalize() { } Transform2D Transform2D::orthonormalized() const { - Transform2D on = *this; - on.orthonormalize(); - return on; + Transform2D ortho = *this; + ortho.orthonormalize(); + return ortho; } bool Transform2D::is_equal_approx(const Transform2D &p_transform) const { @@ -221,12 +221,6 @@ Transform2D Transform2D::operator*(const Transform2D &p_transform) const { return t; } -Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const { - Transform2D copy = *this; - copy.scale_basis(p_scale); - return copy; -} - Transform2D Transform2D::scaled(const Size2 &p_scale) const { // Equivalent to left multiplication Transform2D copy = *this; @@ -269,39 +263,12 @@ real_t Transform2D::basis_determinant() const { return columns[0].x * columns[1].y - columns[0].y * columns[1].x; } -Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, const real_t p_c) const { - //extract parameters - Vector2 p1 = get_origin(); - Vector2 p2 = p_transform.get_origin(); - - real_t r1 = get_rotation(); - real_t r2 = p_transform.get_rotation(); - - Size2 s1 = get_scale(); - Size2 s2 = p_transform.get_scale(); - - //slerp rotation - Vector2 v1(Math::cos(r1), Math::sin(r1)); - Vector2 v2(Math::cos(r2), Math::sin(r2)); - - real_t dot = v1.dot(v2); - - dot = CLAMP(dot, (real_t)-1.0, (real_t)1.0); - - Vector2 v; - - if (dot > 0.9995f) { - v = v1.lerp(v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues - } else { - real_t angle = p_c * Math::acos(dot); - Vector2 v3 = (v2 - v1 * dot).normalized(); - v = v1 * Math::cos(angle) + v3 * Math::sin(angle); - } - - //construct matrix - Transform2D res(v.angle(), p1.lerp(p2, p_c)); - res.scale_basis(s1.lerp(s2, p_c)); - return res; +Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, const real_t p_weight) const { + return Transform2D( + Math::lerp_angle(get_rotation(), p_transform.get_rotation(), p_weight), + get_scale().lerp(p_transform.get_scale(), p_weight), + Math::lerp_angle(get_skew(), p_transform.get_skew(), p_weight), + get_origin().lerp(p_transform.get_origin(), p_weight)); } void Transform2D::operator*=(const real_t p_val) { diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index 2a0917c63f..4a17a9db37 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -85,7 +85,6 @@ struct _NO_DISCARD_ Transform2D { _FORCE_INLINE_ const Vector2 &get_origin() const { return columns[2]; } _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { columns[2] = p_origin; } - Transform2D basis_scaled(const Size2 &p_scale) const; Transform2D scaled(const Size2 &p_scale) const; Transform2D scaled_local(const Size2 &p_scale) const; Transform2D translated(const Vector2 &p_offset) const; diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp index 4b6921d38b..0da1b8c7ad 100644 --- a/core/math/triangle_mesh.cpp +++ b/core/math/triangle_mesh.cpp @@ -182,90 +182,6 @@ void TriangleMesh::create(const Vector<Vector3> &p_faces, const Vector<int32_t> valid = true; } -Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { - uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); - - enum { - TEST_AABB_BIT = 0, - VISIT_LEFT_BIT = 1, - VISIT_RIGHT_BIT = 2, - VISIT_DONE_BIT = 3, - VISITED_BIT_SHIFT = 29, - NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1, - VISITED_BIT_MASK = ~NODE_IDX_MASK, - - }; - - int n_count = 0; - Vector3 n; - - int level = 0; - - const Triangle *triangleptr = triangles.ptr(); - // const Vector3 *verticesr = vertices.ptr(); - const BVH *bvhptr = bvh.ptr(); - - int pos = bvh.size() - 1; - - stack[0] = pos; - while (true) { - uint32_t node = stack[level] & NODE_IDX_MASK; - const BVH &b = bvhptr[node]; - bool done = false; - - switch (stack[level] >> VISITED_BIT_SHIFT) { - case TEST_AABB_BIT: { - if (!b.aabb.intersects(p_aabb)) { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - } else { - if (b.face_index >= 0) { - const Triangle &s = triangleptr[b.face_index]; - n += s.normal; - n_count++; - - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - - } else { - stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node; - } - } - continue; - } - case VISIT_LEFT_BIT: { - stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node; - level++; - stack[level] = b.left | TEST_AABB_BIT; - continue; - } - case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - level++; - stack[level] = b.right | TEST_AABB_BIT; - continue; - } - case VISIT_DONE_BIT: { - if (level == 0) { - done = true; - break; - } else { - level--; - } - continue; - } - } - - if (done) { - break; - } - } - - if (n_count > 0) { - n /= n_count; - } - - return n; -} - bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index) const { uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); @@ -468,118 +384,6 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V return inters; } -bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const { - uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); - - //p_fully_inside = true; - - enum { - TEST_AABB_BIT = 0, - VISIT_LEFT_BIT = 1, - VISIT_RIGHT_BIT = 2, - VISIT_DONE_BIT = 3, - VISITED_BIT_SHIFT = 29, - NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1, - VISITED_BIT_MASK = ~NODE_IDX_MASK, - - }; - - int level = 0; - - const Triangle *triangleptr = triangles.ptr(); - const Vector3 *vertexptr = vertices.ptr(); - const BVH *bvhptr = bvh.ptr(); - - int pos = bvh.size() - 1; - - stack[0] = pos; - while (true) { - uint32_t node = stack[level] & NODE_IDX_MASK; - const BVH &b = bvhptr[node]; - bool done = false; - - switch (stack[level] >> VISITED_BIT_SHIFT) { - case TEST_AABB_BIT: { - if (!b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count)) { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - } else { - if (b.face_index >= 0) { - const Triangle &s = triangleptr[b.face_index]; - - for (int j = 0; j < 3; ++j) { - const Vector3 &point = vertexptr[s.indices[j]]; - const Vector3 &next_point = vertexptr[s.indices[(j + 1) % 3]]; - Vector3 res; - bool over = true; - for (int i = 0; i < p_plane_count; i++) { - const Plane &p = p_planes[i]; - - if (p.intersects_segment(point, next_point, &res)) { - bool inisde = true; - for (int k = 0; k < p_plane_count; k++) { - if (k == i) { - continue; - } - const Plane &pp = p_planes[k]; - if (pp.is_point_over(res)) { - inisde = false; - break; - } - } - if (inisde) { - return true; - } - } - - if (p.is_point_over(point)) { - over = false; - break; - } - } - if (over) { - return true; - } - } - - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - - } else { - stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node; - } - } - continue; - } - case VISIT_LEFT_BIT: { - stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node; - level++; - stack[level] = b.left | TEST_AABB_BIT; - continue; - } - case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - level++; - stack[level] = b.right | TEST_AABB_BIT; - continue; - } - case VISIT_DONE_BIT: { - if (level == 0) { - done = true; - break; - } else { - level--; - } - continue; - } - } - - if (done) { - break; - } - } - - return false; -} - bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale) const { uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index 728fd600d5..24fc12dda9 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -84,9 +84,7 @@ public: bool is_valid() const; bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr) const; bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr) const; - bool intersect_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const; bool inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale = Vector3(1, 1, 1)) const; - Vector3 get_area_normal(const AABB &p_aabb) const; Vector<Face3> get_faces() const; const Vector<Triangle> &get_triangles() const { return triangles; } diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index d732f1e8b2..ae009fc4ef 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -108,7 +108,9 @@ Vector3 Vector3::octahedron_decode(const Vector2 &p_oct) { } Vector2 Vector3::octahedron_tangent_encode(const float sign) const { + const float bias = 1.0f / 32767.0f; Vector2 res = this->octahedron_encode(); + res.y = MAX(res.y, bias); res.y = res.y * 0.5f + 0.5f; res.y = sign >= 0.0f ? res.y : 1 - res.y; return res; diff --git a/core/math/vector3.h b/core/math/vector3.h index bd8739d024..18943a820f 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -76,6 +76,14 @@ struct _NO_DISCARD_ Vector3 { return x < y ? (y < z ? Vector3::AXIS_Z : Vector3::AXIS_Y) : (x < z ? Vector3::AXIS_Z : Vector3::AXIS_X); } + Vector3 min(const Vector3 &p_vector3) const { + return Vector3(MIN(x, p_vector3.x), MIN(y, p_vector3.y), MIN(z, p_vector3.z)); + } + + Vector3 max(const Vector3 &p_vector3) const { + return Vector3(MAX(x, p_vector3.x), MAX(y, p_vector3.y), MAX(z, p_vector3.z)); + } + _FORCE_INLINE_ real_t length() const; _FORCE_INLINE_ real_t length_squared() const; diff --git a/core/math/vector3i.h b/core/math/vector3i.h index 96f9beef12..53d3829a99 100644 --- a/core/math/vector3i.h +++ b/core/math/vector3i.h @@ -69,6 +69,14 @@ struct _NO_DISCARD_ Vector3i { Vector3i::Axis min_axis_index() const; Vector3i::Axis max_axis_index() const; + Vector3i min(const Vector3i &p_vector3i) const { + return Vector3i(MIN(x, p_vector3i.x), MIN(y, p_vector3i.y), MIN(z, p_vector3i.z)); + } + + Vector3i max(const Vector3i &p_vector3i) const { + return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z)); + } + _FORCE_INLINE_ int64_t length_squared() const; _FORCE_INLINE_ double length() const; diff --git a/core/math/vector4.h b/core/math/vector4.h index 0509261f33..f16b040317 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -68,6 +68,14 @@ struct _NO_DISCARD_ Vector4 { Vector4::Axis min_axis_index() const; Vector4::Axis max_axis_index() const; + Vector4 min(const Vector4 &p_vector4) const { + return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w)); + } + + Vector4 max(const Vector4 &p_vector4) const { + return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w)); + } + _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Vector4 &p_vec4) const; bool is_zero_approx() const; diff --git a/core/math/vector4i.h b/core/math/vector4i.h index d38a9de6f1..b815aa8e76 100644 --- a/core/math/vector4i.h +++ b/core/math/vector4i.h @@ -71,6 +71,14 @@ struct _NO_DISCARD_ Vector4i { Vector4i::Axis min_axis_index() const; Vector4i::Axis max_axis_index() const; + Vector4i min(const Vector4i &p_vector4i) const { + return Vector4i(MIN(x, p_vector4i.x), MIN(y, p_vector4i.y), MIN(z, p_vector4i.z), MIN(w, p_vector4i.w)); + } + + Vector4i max(const Vector4i &p_vector4i) const { + return Vector4i(MAX(x, p_vector4i.x), MAX(y, p_vector4i.y), MAX(z, p_vector4i.z), MAX(w, p_vector4i.w)); + } + _FORCE_INLINE_ int64_t length_squared() const; _FORCE_INLINE_ double length() const; |