diff options
Diffstat (limited to 'core/math')
43 files changed, 0 insertions, 1041 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index d6d6101402..a85a0e9db9 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -35,7 +35,6 @@ #include "scene/scene_string_names.h" int AStar::get_available_point_id() const { - if (points.empty()) { return 1; } @@ -54,7 +53,6 @@ int AStar::get_available_point_id() const { } void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { - ERR_FAIL_COND(p_id < 0); ERR_FAIL_COND(p_weight_scale < 1); @@ -78,7 +76,6 @@ void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { } Vector3 AStar::get_point_position(int p_id) const { - Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_V(!p_exists, Vector3()); @@ -87,7 +84,6 @@ Vector3 AStar::get_point_position(int p_id) const { } void AStar::set_point_position(int p_id, const Vector3 &p_pos) { - Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND(!p_exists); @@ -96,7 +92,6 @@ void AStar::set_point_position(int p_id, const Vector3 &p_pos) { } real_t AStar::get_point_weight_scale(int p_id) const { - Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_V(!p_exists, 0); @@ -105,7 +100,6 @@ real_t AStar::get_point_weight_scale(int p_id) const { } void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) { - Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND(!p_exists); @@ -115,13 +109,11 @@ void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) { } void AStar::remove_point(int p_id) { - Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND(!p_exists); for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - Segment s(p_id, (*it.key)); segments.erase(s); @@ -130,7 +122,6 @@ void AStar::remove_point(int p_id) { } for (OAHashMap<int, Point *>::Iterator it = p->unlinked_neighbours.iter(); it.valid; it = p->unlinked_neighbours.next_iter(it)) { - Segment s(p_id, (*it.key)); segments.erase(s); @@ -144,7 +135,6 @@ void AStar::remove_point(int p_id) { } void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) { - ERR_FAIL_COND(p_id == p_with_id); Point *a; @@ -182,7 +172,6 @@ void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) { } void AStar::disconnect_points(int p_id, int p_with_id, bool bidirectional) { - Point *a; bool a_exists = points.lookup(p_id, a); ERR_FAIL_COND(!a_exists); @@ -221,12 +210,10 @@ void AStar::disconnect_points(int p_id, int p_with_id, bool bidirectional) { } bool AStar::has_point(int p_id) const { - return points.has(p_id); } Array AStar::get_points() { - Array point_list; for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { @@ -237,7 +224,6 @@ Array AStar::get_points() { } 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, Vector<int>()); @@ -252,7 +238,6 @@ Vector<int> AStar::get_point_connections(int p_id) { } bool AStar::are_points_connected(int p_id, int p_with_id, bool bidirectional) const { - Segment s(p_id, p_with_id); const Set<Segment>::Element *element = segments.find(s); @@ -261,7 +246,6 @@ bool AStar::are_points_connected(int p_id, int p_with_id, bool bidirectional) co } void AStar::clear() { - last_free_id = 0; for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { memdelete(*(it.value)); @@ -285,12 +269,10 @@ void AStar::reserve_space(int p_num_nodes) { } int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const { - int closest_id = -1; real_t closest_dist = 1e20; for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { - if (!p_include_disabled && !(*it.value)->enabled) continue; // Disabled points should not be considered. @@ -305,13 +287,11 @@ int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) co } Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { - bool found = false; real_t closest_dist = 1e20; Vector3 closest_point; for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) { - Point *from_point = nullptr, *to_point = nullptr; points.lookup(E->get().u, from_point); points.lookup(E->get().v, to_point); @@ -328,7 +308,6 @@ Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { Vector3 p = Geometry::get_closest_point_to_segment(p_point, segment); real_t d = p_point.distance_squared_to(p); if (!found || d < closest_dist) { - closest_point = p; closest_dist = d; found = true; @@ -339,7 +318,6 @@ Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { } bool AStar::_solve(Point *begin_point, Point *end_point) { - pass++; if (!end_point->enabled) @@ -355,7 +333,6 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { open_list.push_back(begin_point); while (!open_list.empty()) { - Point *p = open_list[0]; // The currently processed point if (p == end_point) { @@ -368,7 +345,6 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { p->closed_pass = pass; // Mark the point as closed for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - Point *e = *(it.value); // The neighbour point if (!e->enabled || e->closed_pass == pass) { @@ -403,7 +379,6 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { } real_t AStar::_estimate_cost(int p_from_id, int p_to_id) { - if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id); @@ -419,7 +394,6 @@ real_t AStar::_estimate_cost(int p_from_id, int p_to_id) { } real_t AStar::_compute_cost(int p_from_id, int p_to_id) { - if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id); @@ -435,7 +409,6 @@ real_t AStar::_compute_cost(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, Vector<Vector3>()); @@ -484,7 +457,6 @@ Vector<Vector3> AStar::get_point_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, Vector<int>()); @@ -533,7 +505,6 @@ Vector<int> AStar::get_id_path(int p_from_id, int p_to_id) { } void AStar::set_point_disabled(int p_id, bool p_disabled) { - Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND(!p_exists); @@ -542,7 +513,6 @@ void AStar::set_point_disabled(int p_id, bool p_disabled) { } bool AStar::is_point_disabled(int p_id) const { - Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_V(!p_exists, false); @@ -551,7 +521,6 @@ bool AStar::is_point_disabled(int p_id) const { } void AStar::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id); ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0)); ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position); @@ -678,7 +647,6 @@ Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const { } real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) { - if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id); @@ -694,7 +662,6 @@ real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) { } real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) { - if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id); @@ -710,7 +677,6 @@ real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) { } Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { - AStar::Point *a; bool from_exists = astar.points.lookup(p_from_id, a); ERR_FAIL_COND_V(!from_exists, Vector<Vector2>()); @@ -759,7 +725,6 @@ Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { } Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) { - AStar::Point *a; bool from_exists = astar.points.lookup(p_from_id, a); ERR_FAIL_COND_V(!from_exists, Vector<int>()); @@ -808,7 +773,6 @@ Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) { } bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { - astar.pass++; if (!end_point->enabled) @@ -824,7 +788,6 @@ bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { open_list.push_back(begin_point); while (!open_list.empty()) { - AStar::Point *p = open_list[0]; // The currently processed point if (p == end_point) { @@ -837,7 +800,6 @@ bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { p->closed_pass = astar.pass; // Mark the point as closed for (OAHashMap<int, AStar::Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - AStar::Point *e = *(it.value); // The neighbour point if (!e->enabled || e->closed_pass == astar.pass) { @@ -872,7 +834,6 @@ bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { } void AStar2D::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id); ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0)); ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar2D::get_point_position); diff --git a/core/math/a_star.h b/core/math/a_star.h index ffb437ee04..ba1c3033b8 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -41,12 +41,10 @@ */ class AStar : public Reference { - GDCLASS(AStar, Reference); friend class AStar2D; struct Point { - Point() {} int id; diff --git a/core/math/aabb.cpp b/core/math/aabb.cpp index 19d60fea72..588ee84f58 100644 --- a/core/math/aabb.cpp +++ b/core/math/aabb.cpp @@ -33,21 +33,17 @@ #include "core/print_string.h" real_t AABB::get_area() const { - return size.x * size.y * size.z; } bool AABB::operator==(const AABB &p_rval) const { - return ((position == p_rval.position) && (size == p_rval.size)); } bool AABB::operator!=(const AABB &p_rval) const { - return ((position != p_rval.position) || (size != p_rval.size)); } void AABB::merge_with(const AABB &p_aabb) { - Vector3 beg_1, beg_2; Vector3 end_1, end_2; Vector3 min, max; @@ -70,12 +66,10 @@ void AABB::merge_with(const AABB &p_aabb) { } bool AABB::is_equal_approx(const AABB &p_aabb) const { - return position.is_equal_approx(p_aabb.position) && size.is_equal_approx(p_aabb.size); } AABB AABB::intersection(const AABB &p_aabb) const { - Vector3 src_min = position; Vector3 src_max = position + size; Vector3 dst_min = p_aabb.position; @@ -86,7 +80,6 @@ AABB AABB::intersection(const AABB &p_aabb) const { if (src_min.x > dst_max.x || src_max.x < dst_min.x) return AABB(); else { - min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x; max.x = (src_max.x < dst_max.x) ? src_max.x : dst_max.x; } @@ -94,7 +87,6 @@ AABB AABB::intersection(const AABB &p_aabb) const { if (src_min.y > dst_max.y || src_max.y < dst_min.y) return AABB(); else { - min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y; max.y = (src_max.y < dst_max.y) ? src_max.y : dst_max.y; } @@ -102,7 +94,6 @@ AABB AABB::intersection(const AABB &p_aabb) const { if (src_min.z > dst_max.z || src_max.z < dst_min.z) return AABB(); else { - min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z; max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z; } @@ -111,7 +102,6 @@ AABB AABB::intersection(const AABB &p_aabb) const { } bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const { - Vector3 c1, c2; Vector3 end = position + size; real_t near = -1e20; @@ -154,7 +144,6 @@ bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 * } bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const { - real_t min = 0, max = 1; int axis = 0; real_t sign = 0; @@ -168,7 +157,6 @@ bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector real_t csign; if (seg_from < seg_to) { - if (seg_from > box_end || seg_to < box_begin) return false; real_t length = seg_to - seg_from; @@ -177,7 +165,6 @@ bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector csign = -1.0; } else { - if (seg_to > box_end || seg_from < box_begin) return false; real_t length = seg_to - seg_from; @@ -212,7 +199,6 @@ bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector } bool AABB::intersects_plane(const Plane &p_plane) const { - Vector3 points[8] = { Vector3(position.x, position.y, position.z), Vector3(position.x, position.y, position.z + size.z), @@ -228,7 +214,6 @@ bool AABB::intersects_plane(const Plane &p_plane) const { bool under = false; for (int i = 0; i < 8; i++) { - if (p_plane.distance_to(points[i]) > 0) over = true; else @@ -239,7 +224,6 @@ bool AABB::intersects_plane(const Plane &p_plane) const { } Vector3 AABB::get_longest_axis() const { - Vector3 axis(1, 0, 0); real_t max_size = size.x; @@ -255,7 +239,6 @@ Vector3 AABB::get_longest_axis() const { return axis; } int AABB::get_longest_axis_index() const { - int axis = 0; real_t max_size = size.x; @@ -272,7 +255,6 @@ int AABB::get_longest_axis_index() const { } Vector3 AABB::get_shortest_axis() const { - Vector3 axis(1, 0, 0); real_t max_size = size.x; @@ -288,7 +270,6 @@ Vector3 AABB::get_shortest_axis() const { return axis; } int AABB::get_shortest_axis_index() const { - int axis = 0; real_t max_size = size.x; @@ -305,7 +286,6 @@ int AABB::get_shortest_axis_index() const { } AABB AABB::merge(const AABB &p_with) const { - AABB aabb = *this; aabb.merge_with(p_with); return aabb; @@ -316,24 +296,19 @@ AABB AABB::expand(const Vector3 &p_vector) const { return aabb; } AABB AABB::grow(real_t p_by) const { - AABB aabb = *this; aabb.grow_by(p_by); return aabb; } void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { - ERR_FAIL_INDEX(p_edge, 12); switch (p_edge) { - case 0: { - r_from = Vector3(position.x + size.x, position.y, position.z); r_to = Vector3(position.x, position.y, position.z); } break; case 1: { - r_from = Vector3(position.x + size.x, position.y, position.z + size.z); r_to = Vector3(position.x + size.x, position.y, position.z); } break; @@ -343,18 +318,15 @@ void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { } break; case 3: { - r_from = Vector3(position.x, position.y, position.z); r_to = Vector3(position.x, position.y, position.z + size.z); } break; case 4: { - r_from = Vector3(position.x, position.y + size.y, position.z); r_to = Vector3(position.x + size.x, position.y + size.y, position.z); } break; case 5: { - r_from = Vector3(position.x + size.x, position.y + size.y, position.z); r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); } break; @@ -364,31 +336,26 @@ void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { } break; case 7: { - r_from = Vector3(position.x, position.y + size.y, position.z + size.z); r_to = Vector3(position.x, position.y + size.y, position.z); } break; case 8: { - r_from = Vector3(position.x, position.y, position.z + size.z); r_to = Vector3(position.x, position.y + size.y, position.z + size.z); } break; case 9: { - r_from = Vector3(position.x, position.y, position.z); r_to = Vector3(position.x, position.y + size.y, position.z); } break; case 10: { - r_from = Vector3(position.x + size.x, position.y, position.z); r_to = Vector3(position.x + size.x, position.y + size.y, position.z); } break; case 11: { - r_from = Vector3(position.x + size.x, position.y, position.z + size.z); r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); @@ -397,6 +364,5 @@ void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { } AABB::operator String() const { - return String() + position + " - " + size; } diff --git a/core/math/aabb.h b/core/math/aabb.h index f87fced12d..a2dd50f4f3 100644 --- a/core/math/aabb.h +++ b/core/math/aabb.h @@ -47,12 +47,10 @@ public: real_t get_area() const; /// get area _FORCE_INLINE_ bool has_no_area() const { - return (size.x <= 0 || size.y <= 0 || size.z <= 0); } _FORCE_INLINE_ bool has_no_surface() const { - return (size.x <= 0 && size.y <= 0 && size.z <= 0); } @@ -111,7 +109,6 @@ public: }; inline bool AABB::intersects(const AABB &p_aabb) const { - if (position.x >= (p_aabb.position.x + p_aabb.size.x)) return false; if ((position.x + size.x) <= p_aabb.position.x) @@ -129,7 +126,6 @@ inline bool AABB::intersects(const AABB &p_aabb) const { } inline bool AABB::intersects_inclusive(const AABB &p_aabb) const { - if (position.x > (p_aabb.position.x + p_aabb.size.x)) return false; if ((position.x + size.x) < p_aabb.position.x) @@ -147,7 +143,6 @@ inline bool AABB::intersects_inclusive(const AABB &p_aabb) const { } inline bool AABB::encloses(const AABB &p_aabb) const { - Vector3 src_min = position; Vector3 src_max = position + size; Vector3 dst_min = p_aabb.position; @@ -163,7 +158,6 @@ inline bool AABB::encloses(const AABB &p_aabb) const { } Vector3 AABB::get_support(const Vector3 &p_normal) const { - Vector3 half_extents = size * 0.5; Vector3 ofs = position + half_extents; @@ -175,7 +169,6 @@ Vector3 AABB::get_support(const Vector3 &p_normal) const { } Vector3 AABB::get_endpoint(int p_point) const { - switch (p_point) { case 0: return Vector3(position.x, position.y, position.z); @@ -199,7 +192,6 @@ Vector3 AABB::get_endpoint(int p_point) const { } bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const { - Vector3 half_extents = size * 0.5; Vector3 ofs = position + half_extents; @@ -220,7 +212,6 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con int bad_point_counts_negative[3] = { 0 }; for (int k = 0; k < 3; k++) { - for (int i = 0; i < p_point_count; i++) { if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) { bad_point_counts_positive[k]++; @@ -242,7 +233,6 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con } bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const { - Vector3 half_extents = size * 0.5; Vector3 ofs = position + half_extents; @@ -261,7 +251,6 @@ bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const { } bool AABB::has_point(const Vector3 &p_point) const { - if (p_point.x < position.x) return false; if (p_point.y < position.y) @@ -279,7 +268,6 @@ bool AABB::has_point(const Vector3 &p_point) const { } inline void AABB::expand_to(const Vector3 &p_vector) { - Vector3 begin = position; Vector3 end = position + size; @@ -302,7 +290,6 @@ inline void AABB::expand_to(const Vector3 &p_vector) { } void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const { - Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5); Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z); @@ -313,7 +300,6 @@ void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r } inline real_t AABB::get_longest_axis_size() const { - real_t max_size = size.x; if (size.y > max_size) { @@ -328,7 +314,6 @@ inline real_t AABB::get_longest_axis_size() const { } inline real_t AABB::get_shortest_axis_size() const { - real_t max_size = size.x; if (size.y < max_size) { @@ -343,7 +328,6 @@ inline real_t AABB::get_shortest_axis_size() const { } bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const { - real_t divx = 1.0 / p_dir.x; real_t divy = 1.0 / p_dir.y; real_t divz = 1.0 / p_dir.z; @@ -387,7 +371,6 @@ bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real } void AABB::grow_by(real_t p_amount) { - position.x -= p_amount; position.y -= p_amount; position.z -= p_amount; diff --git a/core/math/audio_frame.h b/core/math/audio_frame.h index 4665311059..91f533eafb 100644 --- a/core/math/audio_frame.h +++ b/core/math/audio_frame.h @@ -48,7 +48,6 @@ static inline float undenormalise(volatile float f) { } struct AudioFrame { - //left and right samples float l, r; @@ -105,7 +104,6 @@ struct AudioFrame { } _FORCE_INLINE_ AudioFrame lerp(const AudioFrame &p_b, float p_t) const { - AudioFrame res = *this; res.l += (p_t * (p_b.l - l)); diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 6218b7e248..e6bf6110f7 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -38,16 +38,13 @@ (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1]) void Basis::from_z(const Vector3 &p_z) { - if (Math::abs(p_z.z) > 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.0 / Math::sqrt(a); elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k); elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[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.0 / Math::sqrt(a); @@ -58,7 +55,6 @@ void Basis::from_z(const Vector3 &p_z) { } void Basis::invert() { - real_t co[3] = { cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1) }; @@ -76,7 +72,6 @@ void Basis::invert() { } void Basis::orthonormalize() { - // Gram-Schmidt Process Vector3 x = get_axis(0); @@ -95,7 +90,6 @@ void Basis::orthonormalize() { } Basis Basis::orthonormalized() const { - Basis c = *this; c.orthonormalize(); return c; @@ -120,7 +114,6 @@ bool Basis::is_rotation() const { } bool Basis::is_symmetric() const { - if (!Math::is_equal_approx_ratio(elements[0][1], elements[1][0], UNIT_EPSILON)) return false; if (!Math::is_equal_approx_ratio(elements[0][2], elements[2][0], UNIT_EPSILON)) @@ -132,7 +125,6 @@ bool Basis::is_symmetric() const { } Basis Basis::diagonalize() { - //NOTE: only implemented for symmetric matrices //with the Jacobi iterative method method #ifdef MATH_CHECKS @@ -193,21 +185,18 @@ Basis Basis::diagonalize() { } Basis Basis::inverse() const { - Basis inv = *this; inv.invert(); return inv; } void Basis::transpose() { - SWAP(elements[0][1], elements[1][0]); SWAP(elements[0][2], elements[2][0]); SWAP(elements[1][2], elements[2][1]); } Basis Basis::transposed() const { - Basis tr = *this; tr.transpose(); return tr; @@ -216,7 +205,6 @@ Basis Basis::transposed() const { // Multiplies the matrix from left by the scaling matrix: M -> S.M // See the comment for Basis::rotated for further explanation. void Basis::scale(const Vector3 &p_scale) { - elements[0][0] *= p_scale.x; elements[0][1] *= p_scale.x; elements[0][2] *= p_scale.x; @@ -260,7 +248,6 @@ Basis Basis::scaled_local(const Vector3 &p_scale) const { } Vector3 Basis::get_scale_abs() const { - return Vector3( Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), Vector3(elements[0][1], elements[1][1], elements[2][1]).length(), @@ -341,7 +328,6 @@ void Basis::rotate_local(const Vector3 &p_axis, real_t p_phi) { *this = rotated_local(p_axis, p_phi); } Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_phi) const { - return (*this) * Basis(p_axis, p_phi); } @@ -430,7 +416,6 @@ void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) cons // the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates // around the z-axis by a and so on. Vector3 Basis::get_euler_xyz() const { - // Euler angles in XYZ convention. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // @@ -474,7 +459,6 @@ Vector3 Basis::get_euler_xyz() const { // and similar for other axes. // The current implementation uses XYZ convention (Z is the first rotation). void Basis::set_euler_xyz(const Vector3 &p_euler) { - real_t c, s; c = Math::cos(p_euler.x); @@ -497,7 +481,6 @@ void Basis::set_euler_xyz(const Vector3 &p_euler) { // as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned // as the x, y, and z components of a Vector3 respectively. Vector3 Basis::get_euler_yxz() const { - /* checking this is a bad idea, because obtaining from scaled transform is a valid use case #ifdef MATH_CHECKS ERR_FAIL_COND(!is_rotation()); @@ -546,7 +529,6 @@ Vector3 Basis::get_euler_yxz() const { // and similar for other axes. // The current implementation uses YXZ convention (Z is the first rotation). void Basis::set_euler_yxz(const Vector3 &p_euler) { - real_t c, s; c = Math::cos(p_euler.x); @@ -566,12 +548,10 @@ void Basis::set_euler_yxz(const Vector3 &p_euler) { } bool Basis::is_equal_approx(const Basis &p_basis) const { - return elements[0].is_equal_approx(p_basis.elements[0]) && elements[1].is_equal_approx(p_basis.elements[1]) && elements[2].is_equal_approx(p_basis.elements[2]); } bool Basis::is_equal_approx_ratio(const Basis &a, const Basis &b, real_t p_epsilon) const { - for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (!Math::is_equal_approx_ratio(a.elements[i][j], b.elements[i][j], p_epsilon)) @@ -583,7 +563,6 @@ bool Basis::is_equal_approx_ratio(const Basis &a, const Basis &b, real_t p_epsil } bool Basis::operator==(const Basis &p_matrix) const { - for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (elements[i][j] != p_matrix.elements[i][j]) @@ -595,17 +574,13 @@ bool Basis::operator==(const Basis &p_matrix) const { } bool Basis::operator!=(const Basis &p_matrix) const { - return (!(*this == p_matrix)); } Basis::operator String() const { - String mtx; for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (i != 0 || j != 0) mtx += ", "; @@ -617,7 +592,6 @@ Basis::operator String() const { } Quat Basis::get_quat() const { - #ifdef MATH_CHECKS ERR_FAIL_COND_V_MSG(!is_rotation(), Quat(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quat() or call orthonormalized() instead."); #endif @@ -681,12 +655,10 @@ static const Basis _ortho_bases[24] = { }; int Basis::get_orthogonal_index() const { - //could be sped up if i come up with a way Basis orth = *this; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - real_t v = orth[i][j]; if (v > 0.5) v = 1.0; @@ -700,7 +672,6 @@ int Basis::get_orthogonal_index() const { } for (int i = 0; i < 24; i++) { - if (_ortho_bases[i] == orth) return i; } @@ -709,7 +680,6 @@ int Basis::get_orthogonal_index() const { } void Basis::set_orthogonal_index(int p_index) { - //there only exist 24 orthogonal bases in r3 ERR_FAIL_INDEX(p_index, 24); @@ -794,7 +764,6 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { } void Basis::set_quat(const Quat &p_quat) { - real_t d = p_quat.length_squared(); real_t s = 2.0 / d; real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s; @@ -866,7 +835,6 @@ void Basis::set_diagonal(const Vector3 &p_diag) { } Basis Basis::slerp(const Basis &target, const real_t &t) const { - //consider scale Quat from(*this); Quat to(target); @@ -880,7 +848,6 @@ Basis Basis::slerp(const Basis &target, const real_t &t) const { } void Basis::rotate_sh(real_t *p_values) { - // code by John Hable // http://filmicworlds.com/blog/simple-and-fast-spherical-harmonic-rotation/ // this code is Public Domain diff --git a/core/math/basis.h b/core/math/basis.h index 2924a0ddbd..d870a6b099 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -39,11 +39,9 @@ public: Vector3 elements[3]; _FORCE_INLINE_ const Vector3 &operator[](int axis) const { - return elements[axis]; } _FORCE_INLINE_ Vector3 &operator[](int axis) { - return elements[axis]; } @@ -166,7 +164,6 @@ public: /* create / set */ _FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { - elements[0][0] = xx; elements[0][1] = xy; elements[0][2] = xz; @@ -178,18 +175,15 @@ public: elements[2][2] = zz; } _FORCE_INLINE_ void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) { - set_axis(0, p_x); set_axis(1, p_y); set_axis(2, p_z); } _FORCE_INLINE_ Vector3 get_column(int i) const { - return Vector3(elements[0][i], elements[1][i], elements[2][i]); } _FORCE_INLINE_ Vector3 get_row(int i) const { - return Vector3(elements[i][0], elements[i][1], elements[i][2]); } _FORCE_INLINE_ Vector3 get_main_diagonal() const { @@ -221,7 +215,6 @@ public: elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z); } Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { - set(xx, xy, xz, yx, yy, yz, zx, zy, zz); } @@ -249,7 +242,6 @@ public: } _FORCE_INLINE_ Basis() { - elements[0][0] = 1; elements[0][1] = 0; elements[0][2] = 0; @@ -263,7 +255,6 @@ public: }; _FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) { - set( p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), @@ -271,7 +262,6 @@ _FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) { } _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const { - return Basis( p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), @@ -279,49 +269,42 @@ _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const { } _FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) { - elements[0] += p_matrix.elements[0]; elements[1] += p_matrix.elements[1]; elements[2] += p_matrix.elements[2]; } _FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const { - Basis ret(*this); ret += p_matrix; return ret; } _FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) { - elements[0] -= p_matrix.elements[0]; elements[1] -= p_matrix.elements[1]; elements[2] -= p_matrix.elements[2]; } _FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const { - Basis ret(*this); ret -= p_matrix; return ret; } _FORCE_INLINE_ void Basis::operator*=(real_t p_val) { - elements[0] *= p_val; elements[1] *= p_val; elements[2] *= p_val; } _FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const { - Basis ret(*this); ret *= p_val; return ret; } Vector3 Basis::xform(const Vector3 &p_vector) const { - return Vector3( elements[0].dot(p_vector), elements[1].dot(p_vector), @@ -329,7 +312,6 @@ Vector3 Basis::xform(const Vector3 &p_vector) const { } Vector3 Basis::xform_inv(const Vector3 &p_vector) const { - return Vector3( (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z), (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z), @@ -337,7 +319,6 @@ Vector3 Basis::xform_inv(const Vector3 &p_vector) const { } real_t Basis::determinant() const { - return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) - elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) + elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]); diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp index 5d3ebc9f6d..4b147bd987 100644 --- a/core/math/camera_matrix.cpp +++ b/core/math/camera_matrix.cpp @@ -34,7 +34,6 @@ #include "core/print_string.h" float CameraMatrix::determinant() const { - return matrix[0][3] * matrix[1][2] * matrix[2][1] * matrix[3][0] - matrix[0][2] * matrix[1][3] * matrix[2][1] * matrix[3][0] - matrix[0][3] * matrix[1][1] * matrix[2][2] * matrix[3][0] + matrix[0][1] * matrix[1][3] * matrix[2][2] * matrix[3][0] + matrix[0][2] * matrix[1][1] * matrix[2][3] * matrix[3][0] - matrix[0][1] * matrix[1][2] * matrix[2][3] * matrix[3][0] - @@ -50,29 +49,22 @@ float CameraMatrix::determinant() const { } void CameraMatrix::set_identity() { - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - matrix[i][j] = (i == j) ? 1 : 0; } } } void CameraMatrix::set_zero() { - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - matrix[i][j] = 0; } } } Plane CameraMatrix::xform4(const Plane &p_vec4) const { - Plane ret; ret.normal.x = matrix[0][0] * p_vec4.normal.x + matrix[1][0] * p_vec4.normal.y + matrix[2][0] * p_vec4.normal.z + matrix[3][0] * p_vec4.d; @@ -83,7 +75,6 @@ Plane CameraMatrix::xform4(const Plane &p_vec4) const { } void CameraMatrix::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) { - if (p_flip_fov) { p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect); } @@ -176,7 +167,6 @@ void CameraMatrix::set_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_ }; void CameraMatrix::set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar) { - set_identity(); matrix[0][0] = 2.0 / (p_right - p_left); @@ -189,7 +179,6 @@ void CameraMatrix::set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom } void CameraMatrix::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) { - if (!p_flip_fov) { p_size *= p_aspect; } @@ -198,7 +187,6 @@ void CameraMatrix::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear } void CameraMatrix::set_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far) { - ERR_FAIL_COND(p_right <= p_left); ERR_FAIL_COND(p_top <= p_bottom); ERR_FAIL_COND(p_far <= p_near); @@ -239,7 +227,6 @@ void CameraMatrix::set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, } real_t CameraMatrix::get_z_far() const { - const real_t *matrix = (const real_t *)this->matrix; Plane new_plane = Plane(matrix[3] - matrix[2], matrix[7] - matrix[6], @@ -252,7 +239,6 @@ real_t CameraMatrix::get_z_far() const { return new_plane.d; } real_t CameraMatrix::get_z_near() const { - const real_t *matrix = (const real_t *)this->matrix; Plane new_plane = Plane(matrix[3] + matrix[2], matrix[7] + matrix[6], @@ -264,7 +250,6 @@ real_t CameraMatrix::get_z_near() const { } Vector2 CameraMatrix::get_viewport_half_extents() const { - const real_t *matrix = (const real_t *)this->matrix; ///////--- Near Plane ---/////// Plane near_plane = Plane(matrix[3] + matrix[2], @@ -293,7 +278,6 @@ Vector2 CameraMatrix::get_viewport_half_extents() const { } 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], @@ -323,7 +307,6 @@ void CameraMatrix::get_far_plane_size(real_t &r_width, real_t &r_height) const { } bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const { - Vector<Plane> planes = get_projection_planes(Transform()); const Planes intersections[8][3] = { { PLANE_FAR, PLANE_LEFT, PLANE_TOP }, @@ -337,7 +320,6 @@ bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8point }; for (int i = 0; i < 8; i++) { - Vector3 point; bool res = planes[intersections[i][0]].intersect_3(planes[intersections[i][1]], planes[intersections[i][2]], &point); ERR_FAIL_COND_V(!res, false); @@ -348,7 +330,6 @@ bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8point } Vector<Plane> CameraMatrix::get_projection_planes(const Transform &p_transform) const { - /** Fast Plane Extraction from combined modelview/projection matrices. * References: * https://web.archive.org/web/20011221205252/http://www.markmorley.com/opengl/frustumculling.html @@ -431,14 +412,12 @@ Vector<Plane> CameraMatrix::get_projection_planes(const Transform &p_transform) } CameraMatrix CameraMatrix::inverse() const { - CameraMatrix cm = *this; cm.invert(); return cm; } void CameraMatrix::invert() { - int i, j, k; int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */ real_t pvt_val; /* Value of current pivot element */ @@ -541,12 +520,10 @@ void CameraMatrix::flip_y() { } CameraMatrix::CameraMatrix() { - set_identity(); } CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const { - CameraMatrix new_matrix; for (int j = 0; j < 4; j++) { @@ -562,7 +539,6 @@ CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const { } void CameraMatrix::set_depth_correction(bool p_flip_y) { - real_t *m = &matrix[0][0]; m[0] = 1; @@ -584,7 +560,6 @@ void CameraMatrix::set_depth_correction(bool p_flip_y) { } void CameraMatrix::set_light_bias() { - real_t *m = &matrix[0][0]; m[0] = 0.5; @@ -606,7 +581,6 @@ void CameraMatrix::set_light_bias() { } void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) { - real_t *m = &matrix[0][0]; m[0] = p_rect.size.width; @@ -628,7 +602,6 @@ void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) { } CameraMatrix::operator String() const { - String str; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) @@ -638,20 +611,17 @@ CameraMatrix::operator String() const { } real_t CameraMatrix::get_aspect() const { - Vector2 vp_he = get_viewport_half_extents(); return vp_he.x / vp_he.y; } int CameraMatrix::get_pixels_per_meter(int p_for_pixel_width) const { - Vector3 result = xform(Vector3(1, 0, -1)); return int((result.x * 0.5 + 0.5) * p_for_pixel_width); } bool CameraMatrix::is_orthogonal() const { - return matrix[3][3] == 1.0; } @@ -679,7 +649,6 @@ real_t CameraMatrix::get_fov() const { } void CameraMatrix::make_scale(const Vector3 &p_scale) { - set_identity(); matrix[0][0] = p_scale.x; matrix[1][1] = p_scale.y; @@ -687,7 +656,6 @@ void CameraMatrix::make_scale(const Vector3 &p_scale) { } void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) { - Vector3 min = p_aabb.position; Vector3 max = p_aabb.position + p_aabb.size; @@ -713,7 +681,6 @@ void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) { } CameraMatrix::operator Transform() const { - Transform tr; const real_t *m = &matrix[0][0]; @@ -737,7 +704,6 @@ CameraMatrix::operator Transform() const { } CameraMatrix::CameraMatrix(const Transform &p_transform) { - const Transform &tr = p_transform; real_t *m = &matrix[0][0]; diff --git a/core/math/camera_matrix.h b/core/math/camera_matrix.h index 5420fa2984..49fdecae02 100644 --- a/core/math/camera_matrix.h +++ b/core/math/camera_matrix.h @@ -35,7 +35,6 @@ #include "core/math/transform.h" struct CameraMatrix { - enum Planes { PLANE_NEAR, PLANE_FAR, @@ -62,7 +61,6 @@ struct CameraMatrix { void set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, real_t p_near, real_t p_far, bool p_flip_fov = false); static real_t get_fovy(real_t p_fovx, real_t p_aspect) { - return Math::rad2deg(Math::atan(p_aspect * Math::tan(Math::deg2rad(p_fovx) * 0.5)) * 2.0); } @@ -116,7 +114,6 @@ struct CameraMatrix { }; Vector3 CameraMatrix::xform(const Vector3 &p_vec3) const { - Vector3 ret; ret.x = matrix[0][0] * p_vec3.x + matrix[1][0] * p_vec3.y + matrix[2][0] * p_vec3.z + matrix[3][0]; ret.y = matrix[0][1] * p_vec3.x + matrix[1][1] * p_vec3.y + matrix[2][1] * p_vec3.z + matrix[3][1]; diff --git a/core/math/delaunay_2d.h b/core/math/delaunay_2d.h index 66b2f8f573..d637671686 100644 --- a/core/math/delaunay_2d.h +++ b/core/math/delaunay_2d.h @@ -57,7 +57,6 @@ public: }; static bool circum_circle_contains(const Vector<Vector2> &p_vertices, const Triangle &p_triangle, int p_vertex) { - Vector2 p1 = p_vertices[p_triangle.points[0]]; Vector2 p2 = p_vertices[p_triangle.points[1]]; Vector2 p3 = p_vertices[p_triangle.points[2]]; @@ -89,7 +88,6 @@ public: } static Vector<Triangle> triangulate(const Vector<Vector2> &p_points) { - Vector<Vector2> points = p_points; Vector<Triangle> triangles; @@ -112,7 +110,6 @@ public: triangles.push_back(Triangle(p_points.size() + 0, p_points.size() + 1, p_points.size() + 2)); for (int i = 0; i < p_points.size(); i++) { - Vector<Edge> polygon; for (int j = 0; j < triangles.size(); j++) { @@ -141,7 +138,6 @@ public: } for (int j = 0; j < polygon.size(); j++) { - if (polygon[j].bad) { continue; } diff --git a/core/math/delaunay_3d.h b/core/math/delaunay_3d.h index 0976d8556f..8fdb52556f 100644 --- a/core/math/delaunay_3d.h +++ b/core/math/delaunay_3d.h @@ -55,7 +55,6 @@ class Delaunay3D { }; struct Simplex { - uint32_t points[4]; R128 circum_center_x; R128 circum_center_y; @@ -104,7 +103,6 @@ class Delaunay3D { }; _FORCE_INLINE_ static void circum_sphere_compute(const Vector3 *p_points, Simplex *p_simplex) { - // the only part in the algorithm where there may be precision errors is this one, so ensure that // we do it as maximum precision as possible @@ -164,7 +162,6 @@ class Delaunay3D { } _FORCE_INLINE_ static bool simplex_contains(const Vector3 *p_points, const Simplex &p_simplex, uint32_t p_vertex) { - R128 v_x = p_points[p_vertex].x; R128 v_y = p_points[p_vertex].y; R128 v_z = p_points[p_vertex].z; @@ -179,7 +176,6 @@ class Delaunay3D { } static bool simplex_is_coplanar(const Vector3 *p_points, const Simplex &p_simplex) { - Plane p(p_points[p_simplex.points[0]], p_points[p_simplex.points[1]], p_points[p_simplex.points[2]]); if (ABS(p.distance_to(p_points[p_simplex.points[3]])) < CMP_EPSILON) { return true; @@ -216,7 +212,6 @@ public: }; static Vector<OutputSimplex> tetrahedralize(const Vector<Vector3> &p_points) { - uint32_t point_count = p_points.size(); Vector3 *points = (Vector3 *)memalloc(sizeof(Vector3) * (point_count + 4)); @@ -273,7 +268,6 @@ public: LocalVector<Triangle> triangles; for (uint32_t i = 0; i < point_count; i++) { - bool unique = true; for (uint32_t j = i + 1; j < point_count; j++) { if (points[i].is_equal_approx(points[j])) { @@ -296,7 +290,6 @@ public: Simplex *simplex = E->get(); if (simplex_contains(points, *simplex, i)) { - static const uint32_t triangle_order[4][3] = { { 0, 1, 2 }, { 0, 1, 3 }, @@ -329,7 +322,6 @@ public: uint32_t good_triangles = 0; for (uint32_t j = 0; j < triangles.size(); j++) { - if (triangles[j].bad) { continue; } diff --git a/core/math/disjoint_set.h b/core/math/disjoint_set.h index 32b9875e4c..4d93a0035b 100644 --- a/core/math/disjoint_set.h +++ b/core/math/disjoint_set.h @@ -41,7 +41,6 @@ /* This DisjointSet class uses Find with path compression and Union by rank */ template <typename T, class C = Comparator<T>, class AL = DefaultAllocator> class DisjointSet { - struct Element { T object; Element *parent = nullptr; @@ -103,7 +102,6 @@ typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::insert_or_get(T template <typename T, class C, class AL> void DisjointSet<T, C, AL>::create_union(T a, T b) { - Element *x = insert_or_get(a); Element *y = insert_or_get(b); diff --git a/core/math/expression.cpp b/core/math/expression.cpp index c43831ddee..7bfebded6a 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -110,7 +110,6 @@ const char *Expression::func_name[Expression::FUNC_MAX] = { }; Expression::BuiltinFunc Expression::find_function(const String &p_string) { - for (int i = 0; i < FUNC_MAX; i++) { if (p_string == func_name[i]) return BuiltinFunc(i); @@ -120,15 +119,12 @@ Expression::BuiltinFunc Expression::find_function(const String &p_string) { } String Expression::get_func_name(BuiltinFunc p_func) { - ERR_FAIL_INDEX_V(p_func, FUNC_MAX, String()); return func_name[p_func]; } int Expression::get_func_argument_count(BuiltinFunc p_func) { - switch (p_func) { - case MATH_RANDOMIZE: case MATH_RAND: case MATH_RANDF: @@ -220,194 +216,157 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant r_error.error = Callable::CallError::CALL_OK; switch (p_func) { case MATH_SIN: { - VALIDATE_ARG_NUM(0); *r_return = Math::sin((double)*p_inputs[0]); } break; case MATH_COS: { - VALIDATE_ARG_NUM(0); *r_return = Math::cos((double)*p_inputs[0]); } break; case MATH_TAN: { - VALIDATE_ARG_NUM(0); *r_return = Math::tan((double)*p_inputs[0]); } break; case MATH_SINH: { - VALIDATE_ARG_NUM(0); *r_return = Math::sinh((double)*p_inputs[0]); } break; case MATH_COSH: { - VALIDATE_ARG_NUM(0); *r_return = Math::cosh((double)*p_inputs[0]); } break; case MATH_TANH: { - VALIDATE_ARG_NUM(0); *r_return = Math::tanh((double)*p_inputs[0]); } break; case MATH_ASIN: { - VALIDATE_ARG_NUM(0); *r_return = Math::asin((double)*p_inputs[0]); } break; case MATH_ACOS: { - VALIDATE_ARG_NUM(0); *r_return = Math::acos((double)*p_inputs[0]); } break; case MATH_ATAN: { - VALIDATE_ARG_NUM(0); *r_return = Math::atan((double)*p_inputs[0]); } break; case MATH_ATAN2: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::atan2((double)*p_inputs[0], (double)*p_inputs[1]); } break; case MATH_SQRT: { - VALIDATE_ARG_NUM(0); *r_return = Math::sqrt((double)*p_inputs[0]); } break; case MATH_FMOD: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::fmod((double)*p_inputs[0], (double)*p_inputs[1]); } break; case MATH_FPOSMOD: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]); } break; case MATH_POSMOD: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::posmod((int)*p_inputs[0], (int)*p_inputs[1]); } break; case MATH_FLOOR: { - VALIDATE_ARG_NUM(0); *r_return = Math::floor((double)*p_inputs[0]); } break; case MATH_CEIL: { - VALIDATE_ARG_NUM(0); *r_return = Math::ceil((double)*p_inputs[0]); } break; case MATH_ROUND: { - VALIDATE_ARG_NUM(0); *r_return = Math::round((double)*p_inputs[0]); } break; case MATH_ABS: { - if (p_inputs[0]->get_type() == Variant::INT) { - int64_t i = *p_inputs[0]; *r_return = ABS(i); } else if (p_inputs[0]->get_type() == Variant::FLOAT) { - real_t r = *p_inputs[0]; *r_return = Math::abs(r); } else { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::FLOAT; } } break; case MATH_SIGN: { - if (p_inputs[0]->get_type() == Variant::INT) { - int64_t i = *p_inputs[0]; *r_return = i < 0 ? -1 : (i > 0 ? +1 : 0); } 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 = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::FLOAT; } } break; case MATH_POW: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::pow((double)*p_inputs[0], (double)*p_inputs[1]); } break; case MATH_LOG: { - VALIDATE_ARG_NUM(0); *r_return = Math::log((double)*p_inputs[0]); } break; case MATH_EXP: { - VALIDATE_ARG_NUM(0); *r_return = Math::exp((double)*p_inputs[0]); } break; case MATH_ISNAN: { - VALIDATE_ARG_NUM(0); *r_return = Math::is_nan((double)*p_inputs[0]); } break; case MATH_ISINF: { - VALIDATE_ARG_NUM(0); *r_return = Math::is_inf((double)*p_inputs[0]); } break; case MATH_EASE: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::ease((double)*p_inputs[0], (double)*p_inputs[1]); } break; case MATH_STEP_DECIMALS: { - VALIDATE_ARG_NUM(0); *r_return = Math::step_decimals((double)*p_inputs[0]); } break; case MATH_STEPIFY: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::stepify((double)*p_inputs[0], (double)*p_inputs[1]); } break; case MATH_LERP: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); *r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); } break; case MATH_LERP_ANGLE: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); *r_return = Math::lerp_angle((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); } break; case MATH_INVERSE_LERP: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); *r_return = Math::inverse_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); } break; case MATH_RANGE_LERP: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); @@ -422,14 +381,12 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return = Math::smoothstep((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); } break; case MATH_MOVE_TOWARD: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); *r_return = Math::move_toward((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); } break; case MATH_DECTIME: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); @@ -446,20 +403,17 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return = Math::randf(); } break; case MATH_RANDOM: { - VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]); } break; case MATH_SEED: { - VALIDATE_ARG_NUM(0); uint64_t seed = *p_inputs[0]; Math::seed(seed); } break; case MATH_RANDSEED: { - VALIDATE_ARG_NUM(0); uint64_t seed = *p_inputs[0]; int ret = Math::rand_from_seed(&seed); @@ -470,22 +424,18 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case MATH_DEG2RAD: { - VALIDATE_ARG_NUM(0); *r_return = Math::deg2rad((double)*p_inputs[0]); } break; case MATH_RAD2DEG: { - VALIDATE_ARG_NUM(0); *r_return = Math::rad2deg((double)*p_inputs[0]); } break; case MATH_LINEAR2DB: { - VALIDATE_ARG_NUM(0); *r_return = Math::linear2db((double)*p_inputs[0]); } break; case MATH_DB2LINEAR: { - VALIDATE_ARG_NUM(0); *r_return = Math::db2linear((double)*p_inputs[0]); } break; @@ -516,9 +466,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return = Math::wrapf((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); } break; case LOGIC_MAX: { - if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) { - int64_t a = *p_inputs[0]; int64_t b = *p_inputs[1]; *r_return = MAX(a, b); @@ -534,9 +482,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case LOGIC_MIN: { - if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) { - int64_t a = *p_inputs[0]; int64_t b = *p_inputs[1]; *r_return = MIN(a, b); @@ -551,9 +497,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } } break; case LOGIC_CLAMP: { - if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT && p_inputs[2]->get_type() == Variant::INT) { - int64_t a = *p_inputs[0]; int64_t b = *p_inputs[1]; int64_t c = *p_inputs[2]; @@ -571,15 +515,12 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } } break; case LOGIC_NEAREST_PO2: { - VALIDATE_ARG_NUM(0); int64_t num = *p_inputs[0]; *r_return = next_power_of_2(num); } break; case OBJ_WEAKREF: { - if (p_inputs[0]->get_type() != Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::OBJECT; @@ -588,10 +529,8 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } if (p_inputs[0]->is_ref()) { - REF r = *p_inputs[0]; if (!r.is_valid()) { - return; } @@ -601,7 +540,6 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } else { Object *obj = *p_inputs[0]; if (!obj) { - return; } Ref<WeakRef> wref = memnew(WeakRef); @@ -611,9 +549,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case FUNC_FUNCREF: { - if (p_inputs[0]->get_type() != Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::OBJECT; @@ -621,7 +557,6 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant return; } if (p_inputs[1]->get_type() != Variant::STRING && p_inputs[1]->get_type() != Variant::NODE_PATH) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 1; r_error.expected = Variant::STRING; @@ -638,11 +573,9 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case TYPE_CONVERT: { - VALIDATE_ARG_NUM(1); int type = *p_inputs[1]; if (type < 0 || type >= Variant::VARIANT_MAX) { - r_error_str = RTR("Invalid type argument to convert(), use TYPE_* constants."); r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; @@ -650,31 +583,25 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant return; } else { - *r_return = Variant::construct(Variant::Type(type), p_inputs, 1, r_error); } } break; case TYPE_OF: { - *r_return = p_inputs[0]->get_type(); } break; case TYPE_EXISTS: { - *r_return = ClassDB::class_exists(*p_inputs[0]); } break; case TEXT_CHAR: { - CharType result[2] = { *p_inputs[0], 0 }; *r_return = String(result); } break; case TEXT_ORD: { - if (p_inputs[0]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING; @@ -685,7 +612,6 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant String str = *p_inputs[0]; if (str.length() != 1) { - r_error_str = RTR("Expected a string of length 1 (a character)."); r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; @@ -698,39 +624,33 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case TEXT_STR: { - String str = *p_inputs[0]; *r_return = str; } break; case TEXT_PRINT: { - String str = *p_inputs[0]; print_line(str); } break; case TEXT_PRINTERR: { - String str = *p_inputs[0]; print_error(str); } break; case TEXT_PRINTRAW: { - String str = *p_inputs[0]; OS::get_singleton()->print("%s", str.utf8().get_data()); } break; case VAR_TO_STR: { - String vars; VariantWriter::write_to_string(*p_inputs[0], vars); *r_return = vars; } break; case STR_TO_VAR: { - if (p_inputs[0]->get_type() != Variant::STRING) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; @@ -756,7 +676,6 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case VAR_TO_BYTES: { - PackedByteArray barr; bool full_objects = *p_inputs[1]; int len; @@ -777,7 +696,6 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return = barr; } break; case BYTES_TO_VAR: { - if (p_inputs[0]->get_type() != Variant::PACKED_BYTE_ARRAY) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; @@ -805,7 +723,6 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant } break; case COLORN: { - VALIDATE_ARG_NUM(1); Color color = Color::named(*p_inputs[0]); @@ -826,60 +743,49 @@ static bool _is_number(CharType c) { } Error Expression::_get_token(Token &r_token) { - while (true) { #define GET_CHAR() (str_ofs >= expression.length() ? 0 : expression[str_ofs++]) CharType cchar = GET_CHAR(); switch (cchar) { - case 0: { r_token.type = TK_EOF; return OK; }; case '{': { - r_token.type = TK_CURLY_BRACKET_OPEN; return OK; }; case '}': { - r_token.type = TK_CURLY_BRACKET_CLOSE; return OK; }; case '[': { - r_token.type = TK_BRACKET_OPEN; return OK; }; case ']': { - r_token.type = TK_BRACKET_CLOSE; return OK; }; case '(': { - r_token.type = TK_PARENTHESIS_OPEN; return OK; }; case ')': { - r_token.type = TK_PARENTHESIS_CLOSE; return OK; }; case ',': { - r_token.type = TK_COMMA; return OK; }; case ':': { - r_token.type = TK_COLON; return OK; }; case '$': { - r_token.type = TK_INPUT; int index = 0; do { @@ -898,7 +804,6 @@ Error Expression::_get_token(Token &r_token) { return OK; }; case '=': { - cchar = GET_CHAR(); if (cchar == '=') { r_token.type = TK_OP_EQUAL; @@ -910,7 +815,6 @@ Error Expression::_get_token(Token &r_token) { return OK; }; case '!': { - if (expression[str_ofs] == '=') { r_token.type = TK_OP_NOT_EQUAL; str_ofs++; @@ -920,7 +824,6 @@ Error Expression::_get_token(Token &r_token) { return OK; }; case '>': { - if (expression[str_ofs] == '=') { r_token.type = TK_OP_GREATER_EQUAL; str_ofs++; @@ -933,7 +836,6 @@ Error Expression::_get_token(Token &r_token) { return OK; }; case '<': { - if (expression[str_ofs] == '=') { r_token.type = TK_OP_LESS_EQUAL; str_ofs++; @@ -966,7 +868,6 @@ Error Expression::_get_token(Token &r_token) { return OK; }; case '&': { - if (expression[str_ofs] == '&') { r_token.type = TK_OP_AND; str_ofs++; @@ -976,7 +877,6 @@ Error Expression::_get_token(Token &r_token) { return OK; }; case '|': { - if (expression[str_ofs] == '|') { r_token.type = TK_OP_OR; str_ofs++; @@ -986,22 +886,18 @@ Error Expression::_get_token(Token &r_token) { return OK; }; case '^': { - r_token.type = TK_OP_BIT_XOR; return OK; }; case '~': { - r_token.type = TK_OP_BIT_INVERT; return OK; }; case '"': { - String str; while (true) { - CharType ch = GET_CHAR(); if (ch == 0) { @@ -1022,7 +918,6 @@ Error Expression::_get_token(Token &r_token) { CharType res = 0; switch (next) { - case 'b': res = 8; break; @@ -1049,7 +944,6 @@ Error Expression::_get_token(Token &r_token) { return ERR_PARSE_ERROR; } if (!(_is_number(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) { - _set_error("Malformed hex constant in string"); r_token.type = TK_ERROR; return ERR_PARSE_ERROR; @@ -1091,7 +985,6 @@ Error Expression::_get_token(Token &r_token) { } break; default: { - if (cchar <= 32) { break; } @@ -1114,10 +1007,8 @@ Error Expression::_get_token(Token &r_token) { bool is_float = false; while (true) { - switch (reading) { case READING_INT: { - if (_is_number(c)) { //pass } else if (c == '.') { @@ -1131,9 +1022,7 @@ Error Expression::_get_token(Token &r_token) { } break; case READING_DEC: { - if (_is_number(c)) { - } else if (c == 'e') { reading = READING_EXP; @@ -1143,7 +1032,6 @@ Error Expression::_get_token(Token &r_token) { } break; case READING_EXP: { - if (_is_number(c)) { exp_beg = true; @@ -1175,12 +1063,10 @@ Error Expression::_get_token(Token &r_token) { return OK; } else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') { - String id; bool first = true; while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && _is_number(cchar))) { - id += String::chr(cchar); cchar = GET_CHAR(); first = false; @@ -1220,7 +1106,6 @@ Error Expression::_get_token(Token &r_token) { } else if (id == "self") { r_token.type = TK_SELF; } else { - for (int i = 0; i < Variant::VARIANT_MAX; i++) { if (id == Variant::get_type_name(Variant::Type(i))) { r_token.type = TK_BASIC_TYPE; @@ -1303,7 +1188,6 @@ const char *Expression::token_name[TK_MAX] = { }; Expression::ENode *Expression::_parse_expression() { - Vector<ExpressionNode> expression; while (true) { @@ -1321,7 +1205,6 @@ Expression::ENode *Expression::_parse_expression() { DictionaryNode *dn = alloc_node<DictionaryNode>(); while (true) { - int cofs = str_ofs; _get_token(tk); if (tk.type == TK_CURLY_BRACKET_CLOSE) { @@ -1365,7 +1248,6 @@ Expression::ENode *Expression::_parse_expression() { ArrayNode *an = alloc_node<ArrayNode>(); while (true) { - int cofs = str_ofs; _get_token(tk); if (tk.type == TK_BRACKET_CLOSE) { @@ -1406,7 +1288,6 @@ Expression::ENode *Expression::_parse_expression() { } break; case TK_IDENTIFIER: { - String identifier = tk.value; int cofs = str_ofs; @@ -1419,7 +1300,6 @@ Expression::ENode *Expression::_parse_expression() { func_call->base = self_node; while (true) { - int cofs2 = str_ofs; _get_token(tk); if (tk.type == TK_PARENTHESIS_CLOSE) { @@ -1462,7 +1342,6 @@ Expression::ENode *Expression::_parse_expression() { input->index = input_index; expr = input; } else { - NamedIndexNode *index = alloc_node<NamedIndexNode>(); SelfNode *self_node = alloc_node<SelfNode>(); index->base = self_node; @@ -1472,13 +1351,11 @@ Expression::ENode *Expression::_parse_expression() { } } break; case TK_INPUT: { - InputNode *input = alloc_node<InputNode>(); input->index = tk.value; expr = input; } break; case TK_SELF: { - SelfNode *self = alloc_node<SelfNode>(); expr = self; } break; @@ -1501,7 +1378,6 @@ Expression::ENode *Expression::_parse_expression() { constructor->data_type = bt; while (true) { - int cofs = str_ofs; _get_token(tk); if (tk.type == TK_PARENTHESIS_CLOSE) { @@ -1542,7 +1418,6 @@ Expression::ENode *Expression::_parse_expression() { bifunc->func = BuiltinFunc(int(tk.value)); while (true) { - int cofs = str_ofs; _get_token(tk); if (tk.type == TK_PARENTHESIS_CLOSE) { @@ -1576,7 +1451,6 @@ Expression::ENode *Expression::_parse_expression() { } break; case TK_OP_SUB: { - ExpressionNode e; e.is_op = true; e.op = Variant::OP_NEGATE; @@ -1584,7 +1458,6 @@ Expression::ENode *Expression::_parse_expression() { continue; } break; case TK_OP_NOT: { - ExpressionNode e; e.is_op = true; e.op = Variant::OP_NOT; @@ -1648,7 +1521,6 @@ Expression::ENode *Expression::_parse_expression() { func_call->base = expr; while (true) { - int cofs3 = str_ofs; _get_token(tk); if (tk.type == TK_PARENTHESIS_CLOSE) { @@ -1797,15 +1669,12 @@ Expression::ENode *Expression::_parse_expression() { /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */ while (expression.size() > 1) { - int next_op = -1; int min_priority = 0xFFFFF; bool is_unary = false; for (int i = 0; i < expression.size(); i++) { - if (!expression[i].is_op) { - continue; } @@ -1814,7 +1683,6 @@ Expression::ENode *Expression::_parse_expression() { bool unary = false; switch (expression[i].op) { - case Variant::OP_BIT_NEGATE: priority = 0; unary = true; @@ -1910,17 +1778,14 @@ Expression::ENode *Expression::_parse_expression() { } if (next_op == -1) { - _set_error("Yet another parser bug...."); ERR_FAIL_V(nullptr); } // OK! create operator.. if (is_unary) { - int expr_pos = next_op; while (expression[expr_pos].is_op) { - expr_pos++; if (expr_pos == expression.size()) { //can happen.. @@ -1931,7 +1796,6 @@ Expression::ENode *Expression::_parse_expression() { //consecutively do unary operators for (int i = expr_pos - 1; i >= next_op; i--) { - OperatorNode *op = alloc_node<OperatorNode>(); op->op = expression[i].op; op->nodes[0] = expression[i + 1].node; @@ -1942,7 +1806,6 @@ Expression::ENode *Expression::_parse_expression() { } } else { - if (next_op < 1 || next_op >= (expression.size() - 1)) { _set_error("Parser bug..."); ERR_FAIL_V(nullptr); @@ -1952,7 +1815,6 @@ Expression::ENode *Expression::_parse_expression() { op->op = expression[next_op].op; if (expression[next_op - 1].is_op) { - _set_error("Parser bug..."); ERR_FAIL_V(nullptr); } @@ -1981,7 +1843,6 @@ Expression::ENode *Expression::_parse_expression() { } bool Expression::_compile_expression() { - if (!expression_dirty) return error_set; @@ -2011,10 +1872,8 @@ bool Expression::_compile_expression() { } bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression::ENode *p_node, Variant &r_ret, String &r_error_str) { - switch (p_node->type) { case Expression::ENode::TYPE_INPUT: { - const Expression::InputNode *in = static_cast<const Expression::InputNode *>(p_node); if (in->index < 0 || in->index >= p_inputs.size()) { r_error_str = vformat(RTR("Invalid input %i (not passed) in expression"), in->index); @@ -2023,13 +1882,11 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: r_ret = p_inputs[in->index]; } break; case Expression::ENode::TYPE_CONSTANT: { - const Expression::ConstantNode *c = static_cast<const Expression::ConstantNode *>(p_node); r_ret = c->value; } break; case Expression::ENode::TYPE_SELF: { - if (!p_instance) { r_error_str = RTR("self can't be used because instance is null (not passed)"); return true; @@ -2037,7 +1894,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: r_ret = p_instance; } break; case Expression::ENode::TYPE_OPERATOR: { - const Expression::OperatorNode *op = static_cast<const Expression::OperatorNode *>(p_node); Variant a; @@ -2062,7 +1918,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } break; case Expression::ENode::TYPE_INDEX: { - const Expression::IndexNode *index = static_cast<const Expression::IndexNode *>(p_node); Variant base; @@ -2085,7 +1940,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } break; case Expression::ENode::TYPE_NAMED_INDEX: { - const Expression::NamedIndexNode *index = static_cast<const Expression::NamedIndexNode *>(p_node); Variant base; @@ -2107,7 +1961,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: Array arr; arr.resize(array->array.size()); for (int i = 0; i < array->array.size(); i++) { - Variant value; bool ret = _execute(p_inputs, p_instance, array->array[i], value, r_error_str); @@ -2124,7 +1977,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: Dictionary d; for (int i = 0; i < dictionary->dict.size(); i += 2) { - Variant key; bool ret = _execute(p_inputs, p_instance, dictionary->dict[i + 0], key, r_error_str); @@ -2142,7 +1994,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: r_ret = d; } break; case Expression::ENode::TYPE_CONSTRUCTOR: { - const Expression::ConstructorNode *constructor = static_cast<const Expression::ConstructorNode *>(p_node); Vector<Variant> arr; @@ -2151,7 +2002,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: argp.resize(constructor->arguments.size()); for (int i = 0; i < constructor->arguments.size(); i++) { - Variant value; bool ret = _execute(p_inputs, p_instance, constructor->arguments[i], value, r_error_str); @@ -2171,7 +2021,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } break; case Expression::ENode::TYPE_BUILTIN_FUNC: { - const Expression::BuiltinFuncNode *bifunc = static_cast<const Expression::BuiltinFuncNode *>(p_node); Vector<Variant> arr; @@ -2180,7 +2029,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: argp.resize(bifunc->arguments.size()); for (int i = 0; i < bifunc->arguments.size(); i++) { - Variant value; bool ret = _execute(p_inputs, p_instance, bifunc->arguments[i], value, r_error_str); if (ret) @@ -2199,7 +2047,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } break; case Expression::ENode::TYPE_CALL: { - const Expression::CallNode *call = static_cast<const Expression::CallNode *>(p_node); Variant base; @@ -2214,7 +2061,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: argp.resize(call->arguments.size()); for (int i = 0; i < call->arguments.size(); i++) { - Variant value; ret = _execute(p_inputs, p_instance, call->arguments[i], value, r_error_str); @@ -2238,7 +2084,6 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } Error Expression::parse(const String &p_expression, const Vector<String> &p_input_names) { - if (nodes) { memdelete(nodes); nodes = nullptr; @@ -2266,7 +2111,6 @@ Error Expression::parse(const String &p_expression, const Vector<String> &p_inpu } Variant Expression::execute(Array p_inputs, Object *p_base, bool p_show_error) { - ERR_FAIL_COND_V_MSG(error_set, Variant(), "There was previously a parse error: " + error_str + "."); execution_error = false; @@ -2291,7 +2135,6 @@ String Expression::get_error_text() const { } void Expression::_bind_methods() { - ClassDB::bind_method(D_METHOD("parse", "expression", "input_names"), &Expression::parse, DEFVAL(Vector<String>())); ClassDB::bind_method(D_METHOD("execute", "inputs", "base_instance", "show_error"), &Expression::execute, DEFVAL(Array()), DEFVAL(Variant()), DEFVAL(true)); ClassDB::bind_method(D_METHOD("has_execute_failed"), &Expression::has_execute_failed); @@ -2299,7 +2142,6 @@ void Expression::_bind_methods() { } Expression::~Expression() { - if (nodes) { memdelete(nodes); } diff --git a/core/math/expression.h b/core/math/expression.h index bf710ecdd5..2d67caca44 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -118,7 +118,6 @@ private: static const char *func_name[FUNC_MAX]; struct Input { - Variant::Type type = Variant::NIL; String name; @@ -180,7 +179,6 @@ private: static const char *token_name[TK_MAX]; struct Token { - TokenType type; Variant value; }; @@ -198,7 +196,6 @@ private: bool error_set = true; struct ENode { - enum Type { TYPE_INPUT, TYPE_CONSTANT, @@ -226,7 +223,6 @@ private: }; struct ExpressionNode { - bool is_op; union { Variant::Operator op; @@ -237,7 +233,6 @@ private: ENode *_parse_expression(); struct InputNode : public ENode { - int index; InputNode() { type = TYPE_INPUT; @@ -245,7 +240,6 @@ private: }; struct ConstantNode : public ENode { - Variant value; ConstantNode() { type = TYPE_CONSTANT; @@ -253,7 +247,6 @@ private: }; struct OperatorNode : public ENode { - Variant::Operator op; ENode *nodes[2]; @@ -264,7 +257,6 @@ private: }; struct SelfNode : public ENode { - SelfNode() { type = TYPE_SELF; } diff --git a/core/math/face3.cpp b/core/math/face3.cpp index 74331b391f..e1be4f0acf 100644 --- a/core/math/face3.cpp +++ b/core/math/face3.cpp @@ -33,7 +33,6 @@ #include "core/math/geometry.h" int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_over[3]) const { - ERR_FAIL_COND_V(is_degenerate(), 0); Vector3 above[4]; @@ -43,7 +42,6 @@ int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_ int below_count = 0; for (int i = 0; i < 3; i++) { - if (p_plane.has_point(vertex[i], CMP_EPSILON)) { // point is in plane ERR_FAIL_COND_V(above_count >= 4, 0); @@ -52,7 +50,6 @@ int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_ below[below_count++] = vertex[i]; } else { - if (p_plane.is_point_over(vertex[i])) { //Point is over ERR_FAIL_COND_V(above_count >= 4, 0); @@ -83,13 +80,11 @@ int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_ ERR_FAIL_COND_V(above_count >= 4 && below_count >= 4, 0); //bug in the algo if (above_count >= 3) { - p_res[polygons_created] = Face3(above[0], above[1], above[2]); p_is_point_over[polygons_created] = true; polygons_created++; if (above_count == 4) { - p_res[polygons_created] = Face3(above[2], above[3], above[0]); p_is_point_over[polygons_created] = true; polygons_created++; @@ -97,13 +92,11 @@ int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_ } if (below_count >= 3) { - p_res[polygons_created] = Face3(below[0], below[1], below[2]); p_is_point_over[polygons_created] = false; polygons_created++; if (below_count == 4) { - p_res[polygons_created] = Face3(below[2], below[3], below[0]); p_is_point_over[polygons_created] = false; polygons_created++; @@ -114,29 +107,24 @@ int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_ } bool Face3::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const { - return Geometry::ray_intersects_triangle(p_from, p_dir, vertex[0], vertex[1], vertex[2], p_intersection); } bool Face3::intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const { - return Geometry::segment_intersects_triangle(p_from, p_dir, vertex[0], vertex[1], vertex[2], p_intersection); } bool Face3::is_degenerate() const { - Vector3 normal = vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]); return (normal.length_squared() < 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 @@ -159,7 +147,6 @@ Face3::Side Face3::get_side_of(const Face3 &p_face, ClockDirection p_clock_dir) } Vector3 Face3::get_random_point_inside() const { - real_t a = Math::random(0, 1); real_t b = Math::random(0, 1); if (a > b) { @@ -170,29 +157,24 @@ Vector3 Face3::get_random_point_inside() const { } 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.0; } real_t Face3::get_area() const { - return vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]).length(); } 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())) return false; @@ -228,7 +210,6 @@ bool Face3::intersects_aabb(const AABB &p_aabb) const { }; for (int i = 0; i < 12; i++) { - Vector3 from, to; p_aabb.get_edge(i, from, to); Vector3 e1 = from - to; @@ -253,14 +234,11 @@ bool Face3::intersects_aabb(const AABB &p_aabb) const { } Face3::operator String() const { - return String() + vertex[0] + ", " + vertex[1] + ", " + vertex[2]; } void Face3::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const { - for (int i = 0; i < 3; i++) { - Vector3 v = p_transform.xform(vertex[i]); real_t d = p_normal.dot(v); @@ -273,7 +251,6 @@ void Face3::project_range(const Vector3 &p_normal, const Transform &p_transform, } void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const { - #define _FACE_IS_VALID_SUPPORT_THRESHOLD 0.98 #define _EDGE_IS_VALID_SUPPORT_THRESHOLD 0.05 @@ -284,11 +261,9 @@ void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, V /** TEST FACE AS SUPPORT **/ if (get_plane().normal.dot(n) > _FACE_IS_VALID_SUPPORT_THRESHOLD) { - *p_count = MIN(3, p_max); for (int i = 0; i < *p_count; i++) { - p_vertices[i] = p_transform.xform(vertex[i]); } @@ -301,7 +276,6 @@ void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, V real_t support_max = 0; for (int i = 0; i < 3; i++) { - real_t d = n.dot(vertex[i]); if (i == 0 || d > support_max) { @@ -313,7 +287,6 @@ void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, V /** TEST EDGES AS SUPPORT **/ for (int i = 0; i < 3; i++) { - if (i != vert_support_idx && i + 1 != vert_support_idx) continue; @@ -321,7 +294,6 @@ void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, V real_t dot = (vertex[i] - vertex[(i + 1) % 3]).normalized().dot(n); dot = ABS(dot); if (dot < _EDGE_IS_VALID_SUPPORT_THRESHOLD) { - *p_count = MIN(2, p_max); for (int j = 0; j < *p_count; j++) @@ -336,7 +308,6 @@ void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, V } Vector3 Face3::get_closest_point_to(const Vector3 &p_point) const { - Vector3 edge0 = vertex[1] - vertex[0]; Vector3 edge1 = vertex[2] - vertex[0]; Vector3 v0 = vertex[0] - p_point; diff --git a/core/math/face3.h b/core/math/face3.h index da269028f5..eb2b3b8bd5 100644 --- a/core/math/face3.h +++ b/core/math/face3.h @@ -78,7 +78,6 @@ public: void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const; AABB get_aabb() const { - AABB aabb(vertex[0], Vector3()); aabb.expand_to(vertex[1]); aabb.expand_to(vertex[2]); @@ -98,7 +97,6 @@ public: }; bool Face3::intersects_aabb2(const AABB &p_aabb) const { - Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]); Vector3 half_extents = p_aabb.size * 0.5; @@ -145,17 +143,13 @@ bool Face3::intersects_aabb2(const AABB &p_aabb) const { }; for (int i = 0; i < 12; i++) { - Vector3 from, to; switch (i) { - case 0: { - from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); } break; case 1: { - from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); } break; @@ -165,18 +159,15 @@ bool Face3::intersects_aabb2(const AABB &p_aabb) const { } break; case 3: { - from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); } break; case 4: { - from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 5: { - from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); } break; @@ -186,31 +177,26 @@ bool Face3::intersects_aabb2(const AABB &p_aabb) const { } break; case 7: { - from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 8: { - from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); } break; case 9: { - from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 10: { - from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); } break; case 11: { - from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); @@ -240,7 +226,6 @@ bool Face3::intersects_aabb2(const AABB &p_aabb) const { real_t minT = 1e20, maxT = -1e20; for (int k = 0; k < 3; k++) { - real_t vert_d = axis.dot(vertex[k]); if (vert_d > maxT) diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index f923b62542..3085997225 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -54,13 +54,10 @@ bool Geometry::is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> */ void Geometry::MeshData::optimize_vertices() { - Map<int, int> vtx_remap; for (int i = 0; i < faces.size(); i++) { - for (int j = 0; j < faces[i].indices.size(); j++) { - int idx = faces[i].indices[j]; if (!vtx_remap.has(idx)) { int ni = vtx_remap.size(); @@ -72,7 +69,6 @@ void Geometry::MeshData::optimize_vertices() { } for (int i = 0; i < edges.size(); i++) { - int a = edges[i].a; int b = edges[i].b; @@ -93,7 +89,6 @@ void Geometry::MeshData::optimize_vertices() { new_vertices.resize(vtx_remap.size()); for (int i = 0; i < vertices.size(); i++) { - if (vtx_remap.has(i)) new_vertices.write[vtx_remap[i]] = vertices[i]; } @@ -101,9 +96,7 @@ void Geometry::MeshData::optimize_vertices() { } struct _FaceClassify { - struct _Link { - int face = -1; int edge = -1; void clear() { @@ -126,42 +119,34 @@ static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) { 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.00001 && vi2.distance_to(vj2) < 0.00001) { 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; @@ -184,10 +169,8 @@ static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) { } 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; } @@ -196,14 +179,12 @@ static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) { } 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); } @@ -212,7 +193,6 @@ static bool _group_face(_FaceClassify *p_faces, int len, int p_index, int p_grou } Vector<Vector<Face3>> Geometry::separate_objects(Vector<Face3> p_array) { - Vector<Vector<Face3>> objects; int len = p_array.size(); @@ -226,7 +206,6 @@ Vector<Vector<Face3>> Geometry::separate_objects(Vector<Face3> p_array) { _FaceClassify *_fcptr = fc.ptrw(); for (int i = 0; i < len; i++) { - _fcptr[i].face = arrayptr[i]; } @@ -238,7 +217,6 @@ Vector<Vector<Face3>> Geometry::separate_objects(Vector<Face3> p_array) { int group = 0; for (int i = 0; i < len; i++) { - if (!_fcptr[i].valid) continue; if (_group_face(_fcptr, len, i, group)) { @@ -249,12 +227,10 @@ Vector<Vector<Face3>> Geometry::separate_objects(Vector<Face3> p_array) { // 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(); @@ -262,7 +238,6 @@ Vector<Vector<Face3>> Geometry::separate_objects(Vector<Face3> p_array) { if (!_fcptr[i].valid) continue; if (_fcptr[i].group >= 0 && _fcptr[i].group < group) { - group_faces[_fcptr[i].group].push_back(_fcptr[i].face); } } @@ -299,7 +274,6 @@ enum _CellFlags { }; static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, const Vector3 &voxelsize, const Face3 &p_face) { - AABB aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z)); aabb.position = aabb.position * voxelsize; aabb.size = aabb.size * voxelsize; @@ -308,7 +282,6 @@ static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int return; if (len_x == 1 && len_y == 1 && len_z == 1) { - p_cell_status[x][y][z] = _CELL_SOLID; return; } @@ -337,15 +310,12 @@ static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int int new_len_z; for (int i = 0; i < div_x; i++) { - _SPLIT(i, div_x, x, len_x, new_x, new_len_x); for (int j = 0; j < div_y; j++) { - _SPLIT(j, div_y, y, len_y, new_y, new_len_y); for (int k = 0; k < div_z; k++) { - _SPLIT(k, div_z, z, len_z, new_z, new_len_z); _plot_face(p_cell_status, new_x, new_y, new_z, new_len_x, new_len_y, new_len_z, voxelsize, p_face); @@ -355,14 +325,12 @@ static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int } static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z) { - if (p_cell_status[x][y][z] & 3) return; // Nothing to do, already used and/or visited. p_cell_status[x][y][z] = _CELL_PREV_FIRST; while (true) { - uint8_t &c = p_cell_status[x][y][z]; if ((c & _CELL_STEP_MASK) == _CELL_STEP_NONE) { @@ -416,9 +384,7 @@ static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, uint8_t prev = 0; switch (c & _CELL_STEP_MASK) { - case _CELL_STEP_Y_POS: { - next_y++; prev = _CELL_PREV_Y_NEG; } break; @@ -464,7 +430,6 @@ 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, Vector<Face3> &p_faces) { - ERR_FAIL_INDEX(x, len_x); ERR_FAIL_INDEX(y, len_y); ERR_FAIL_INDEX(z, len_z); @@ -485,7 +450,6 @@ static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, i }; for (int i = 0; i < 6; i++) { - Vector3 face_points[4]; int disp_x = x + ((i % 3) == 0 ? ((i < 3) ? 1 : -1) : 0); int disp_y = y + (((i - 1) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0); @@ -524,7 +488,6 @@ static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, i } Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { - #define _MIN_SIZE 1.0 #define _MAX_LENGTH 20 @@ -534,12 +497,9 @@ Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { AABB global_aabb; for (int i = 0; i < face_count; i++) { - if (i == 0) { - global_aabb = faces[i].get_aabb(); } else { - global_aabb.merge_with(faces[i].get_aabb()); } } @@ -573,15 +533,12 @@ Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { uint8_t ***cell_status = memnew_arr(uint8_t **, div_x); for (int i = 0; i < div_x; i++) { - cell_status[i] = memnew_arr(uint8_t *, div_y); for (int j = 0; j < div_y; j++) { - cell_status[i][j] = memnew_arr(uint8_t, div_z); for (int k = 0; k < div_z; k++) { - cell_status[i][j][k] = 0; } } @@ -590,10 +547,8 @@ Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { // Plot faces into cells. for (int i = 0; i < face_count; i++) { - Face3 f = faces[i]; for (int j = 0; j < 3; j++) { - f.vertex[j] -= global_aabb.position; } _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f); @@ -602,27 +557,21 @@ Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { // Determine which cells connect to the outside by traversing the outside and recursively flood-fill marking. for (int i = 0; i < div_x; i++) { - for (int j = 0; j < div_y; j++) { - _mark_outside(cell_status, i, j, 0, div_x, div_y, div_z); _mark_outside(cell_status, i, j, div_z - 1, div_x, div_y, div_z); } } for (int i = 0; i < div_z; i++) { - for (int j = 0; j < div_y; j++) { - _mark_outside(cell_status, 0, j, i, div_x, div_y, div_z); _mark_outside(cell_status, div_x - 1, j, i, div_x, div_y, div_z); } } for (int i = 0; i < div_x; i++) { - for (int j = 0; j < div_z; j++) { - _mark_outside(cell_status, i, 0, j, div_x, div_y, div_z); _mark_outside(cell_status, i, div_y - 1, j, div_x, div_y, div_z); } @@ -633,11 +582,8 @@ Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { Vector<Face3> wrapped_faces; for (int i = 0; i < div_x; i++) { - for (int j = 0; j < div_y; j++) { - for (int k = 0; k < div_z; k++) { - _build_faces(cell_status, i, j, k, div_x, div_y, div_z, wrapped_faces); } } @@ -649,9 +595,7 @@ Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { Face3 *wrapped_faces_ptr = wrapped_faces.ptrw(); for (int i = 0; i < wrapped_faces_count; i++) { - for (int j = 0; j < 3; j++) { - Vector3 &v = wrapped_faces_ptr[i].vertex[j]; v = v * voxelsize; v += global_aabb.position; @@ -661,9 +605,7 @@ Vector<Face3> Geometry::wrap_geometry(Vector<Face3> p_array, real_t *p_error) { // clean up grid for (int i = 0; i < div_x; i++) { - for (int j = 0; j < div_y; j++) { - memdelete_arr(cell_status[i][j]); } @@ -712,14 +654,12 @@ Vector<Vector<Vector2>> Geometry::decompose_polygon_in_convex(Vector<Point2> pol } Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { - MeshData mesh; #define SUBPLANE_SIZE 1024.0 real_t subplane_size = 1024.0; // Should compute this from the actual plane. for (int i = 0; i < p_planes.size(); i++) { - Plane p = p_planes[i]; Vector3 ref = Vector3(0.0, 1.0, 0.0); @@ -740,7 +680,6 @@ Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { vertices.push_back(center + up * subplane_size + right * subplane_size); for (int j = 0; j < p_planes.size(); j++) { - if (j == i) continue; @@ -754,7 +693,6 @@ Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { break; for (int k = 0; k < vertices.size(); k++) { - int k_n = (k + 1) % vertices.size(); Vector3 edge0_A = vertices[k]; @@ -770,7 +708,6 @@ Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { // Check for different sides and non coplanar. if ((dist0 * dist1) < 0) { - // Calculate intersection. Vector3 rel = edge1_A - edge0_A; @@ -796,19 +733,15 @@ Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { // Add face indices. for (int j = 0; j < vertices.size(); j++) { - int idx = -1; for (int k = 0; k < mesh.vertices.size(); k++) { - if (mesh.vertices[k].distance_to(vertices[j]) < 0.001) { - idx = k; break; } } if (idx == -1) { - idx = mesh.vertices.size(); mesh.vertices.push_back(vertices[j]); } @@ -821,13 +754,11 @@ Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { // Add edge. for (int j = 0; j < face.indices.size(); j++) { - int a = face.indices[j]; int b = face.indices[(j + 1) % face.indices.size()]; bool found = false; for (int k = 0; k < mesh.edges.size(); k++) { - if (mesh.edges[k].a == a && mesh.edges[k].b == b) { found = true; break; @@ -851,7 +782,6 @@ Geometry::MeshData Geometry::build_convex_mesh(const Vector<Plane> &p_planes) { } Vector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) { - Vector<Plane> planes; planes.push_back(Plane(Vector3(1, 0, 0), p_extents.x)); @@ -865,11 +795,9 @@ Vector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) { } Vector<Plane> Geometry::build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis) { - Vector<Plane> planes; for (int i = 0; i < p_sides; i++) { - Vector3 normal; normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides); normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides); @@ -887,7 +815,6 @@ Vector<Plane> Geometry::build_cylinder_planes(real_t p_radius, real_t p_height, } Vector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) { - Vector<Plane> planes; Vector3 axis; @@ -899,7 +826,6 @@ Vector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_l axis_neg[p_axis] = -1.0; for (int i = 0; i < p_lons; i++) { - Vector3 normal; normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_lons); normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_lons); @@ -907,7 +833,6 @@ Vector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_l planes.push_back(Plane(normal, p_radius)); for (int j = 1; j <= p_lats; j++) { - // FIXME: This is stupid. Vector3 angle = normal.lerp(axis, j / (real_t)p_lats).normalized(); Vector3 pos = angle * p_radius; @@ -920,7 +845,6 @@ Vector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int p_l } Vector<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> planes; Vector3 axis; @@ -932,7 +856,6 @@ Vector<Plane> Geometry::build_capsule_planes(real_t p_radius, real_t p_height, i axis_neg[p_axis] = -1.0; for (int i = 0; i < p_sides; i++) { - Vector3 normal; normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides); normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides); @@ -940,7 +863,6 @@ Vector<Plane> Geometry::build_capsule_planes(real_t p_radius, real_t p_height, i planes.push_back(Plane(normal, p_radius)); for (int j = 1; j <= p_lats; j++) { - Vector3 angle = normal.lerp(axis, j / (real_t)p_lats).normalized(); Vector3 pos = axis * p_height * 0.5 + angle * p_radius; planes.push_back(Plane(pos, angle)); @@ -952,7 +874,6 @@ Vector<Plane> Geometry::build_capsule_planes(real_t p_radius, real_t p_height, i } struct _AtlasWorkRect { - Size2i s; Point2i p; int idx; @@ -960,14 +881,12 @@ struct _AtlasWorkRect { }; struct _AtlasWorkRectResult { - Vector<_AtlasWorkRect> result; int max_w; int max_h; }; void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size) { - // Super simple, almost brute force scanline stacking fitter. // It's pretty basic for now, but it tries to make sure that the aspect ratio of the // resulting atlas is somehow square. This is necessary because video cards have limits. @@ -990,7 +909,6 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu Vector<_AtlasWorkRectResult> results; for (int i = 0; i <= 12; i++) { - int w = 1 << i; int max_h = 0; int max_w = 0; @@ -1006,15 +924,12 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu int ofs = 0; int limit_h = 0; for (int j = 0; j < wrects.size(); j++) { - if (ofs + wrects[j].s.width > w) { - ofs = 0; } int from_y = 0; for (int k = 0; k < wrects[j].s.width; k++) { - if (hmax[ofs + k] > from_y) from_y = hmax[ofs + k]; } @@ -1027,7 +942,6 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu limit_h = end_h; for (int k = 0; k < wrects[j].s.width; k++) { - hmax.write[ofs + k] = end_h; } @@ -1054,7 +968,6 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu real_t best_aspect = 1e20; for (int i = 0; i < results.size(); i++) { - real_t h = next_power_of_2(results[i].max_h); real_t w = next_power_of_2(results[i].max_w); real_t aspect = h > w ? h / w : w / h; @@ -1067,7 +980,6 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu r_result.resize(p_rects.size()); for (int i = 0; i < p_rects.size(); i++) { - r_result.write[results[best].result[i].idx] = results[best].result[i].p; } @@ -1075,7 +987,6 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu } Vector<Vector<Point2>> Geometry::_polypaths_do_operation(PolyBooleanOperation p_op, const Vector<Point2> &p_polypath_a, const Vector<Point2> &p_polypath_b, bool is_a_open) { - using namespace ClipperLib; ClipType op = ctUnion; @@ -1135,7 +1046,6 @@ Vector<Vector<Point2>> Geometry::_polypaths_do_operation(PolyBooleanOperation p_ } Vector<Vector<Point2>> Geometry::_polypath_offset(const Vector<Point2> &p_polypath, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) { - using namespace ClipperLib; JoinType jt = jtSquare; @@ -1202,19 +1112,16 @@ Vector<Vector<Point2>> Geometry::_polypath_offset(const Vector<Point2> &p_polypa } Vector<Vector3> Geometry::compute_convex_mesh_points(const Plane *p_planes, int p_plane_count) { - Vector<Vector3> points; // Iterate through every unique combination of any three planes. for (int i = p_plane_count - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { for (int k = j - 1; k >= 0; k--) { - // Find the point where these planes all cross over (if they // do at all). Vector3 convex_shape_point; if (p_planes[i].intersect_3(p_planes[j], p_planes[k], &convex_shape_point)) { - // See if any *other* plane excludes this point because it's // on the wrong side. bool excluded = false; @@ -1241,7 +1148,6 @@ Vector<Vector3> Geometry::compute_convex_mesh_points(const Plane *p_planes, int } Vector<Point2i> Geometry::pack_rects(const Vector<Size2i> &p_sizes, const Size2i &p_atlas_size) { - Vector<stbrp_node> nodes; nodes.resize(p_atlas_size.width); @@ -1277,7 +1183,6 @@ Vector<Point2i> Geometry::pack_rects(const Vector<Size2i> &p_sizes, const Size2i } Vector<Vector3i> Geometry::partial_pack_rects(const Vector<Vector2i> &p_sizes, const Size2i &p_atlas_size) { - Vector<stbrp_node> nodes; nodes.resize(p_atlas_size.width); zeromem(nodes.ptrw(), sizeof(stbrp_node) * nodes.size()); @@ -1314,7 +1219,6 @@ Vector<Vector3i> Geometry::partial_pack_rects(const Vector<Vector2i> &p_sizes, c /* dt of 1d function using squared distance */ static void edt(float *f, int stride, int n) { - float *d = (float *)alloca(sizeof(float) * n + sizeof(int) * n + sizeof(float) * (n + 1)); int *v = (int *)&(d[n]); float *z = (float *)&v[n]; @@ -1351,7 +1255,6 @@ static void edt(float *f, int stride, int n) { #undef square Vector<uint32_t> Geometry::generate_edf(const Vector<bool> &p_voxels, const Vector3i &p_size, bool p_negative) { - uint32_t float_count = p_size.x * p_size.y * p_size.z; ERR_FAIL_COND_V((uint32_t)p_voxels.size() != float_count, Vector<uint32_t>()); @@ -1368,7 +1271,6 @@ Vector<uint32_t> Geometry::generate_edf(const Vector<bool> &p_voxels, const Vect { const bool *voxr = p_voxels.ptr(); for (uint32_t i = 0; i < float_count; i++) { - bool plot = voxr[i]; if (p_negative) { plot = !plot; diff --git a/core/math/geometry.h b/core/math/geometry.h index b90cae3786..b52b081016 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -45,7 +45,6 @@ class Geometry { public: static real_t get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2, Vector2 &c1, Vector2 &c2) { - Vector2 d1 = q1 - p1; // Direction vector of segment S1. Vector2 d2 = q2 - p2; // Direction vector of segment S2. Vector2 r = p1 - p2; @@ -103,7 +102,6 @@ public: } static void get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2, Vector3 &c1, Vector3 &c2) { - // Do the function 'd' as defined by pb. I think is is dot product of some sort. #define d_of(m, n, o, p) ((m.x - n.x) * (o.x - p.x) + (m.y - n.y) * (o.y - p.y) + (m.z - n.z) * (o.z - p.z)) @@ -226,7 +224,6 @@ public: } static inline bool segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = nullptr) { - Vector3 rel = p_to - p_from; Vector3 e1 = p_v1 - p_v0; Vector3 e2 = p_v2 - p_v0; @@ -263,7 +260,6 @@ public: } static inline bool segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 *r_res = nullptr, Vector3 *r_norm = nullptr) { - Vector3 sphere_pos = p_sphere_pos - p_from; Vector3 rel = (p_to - p_from); real_t rel_l = rel.length(); @@ -299,7 +295,6 @@ public: } static inline bool segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, real_t p_height, real_t p_radius, Vector3 *r_res = nullptr, Vector3 *r_norm = nullptr) { - Vector3 rel = (p_to - p_from); real_t rel_l = rel.length(); if (rel_l < CMP_EPSILON) @@ -339,7 +334,6 @@ public: int axis = -1; for (int i = 0; i < 2; i++) { - real_t seg_from = from2D[i]; real_t seg_to = to2D[i]; real_t box_begin = -size[i]; @@ -347,7 +341,6 @@ public: real_t cmin, cmax; if (seg_from < seg_to) { - if (seg_from > box_end || seg_to < box_begin) return false; real_t length = seg_to - seg_from; @@ -355,7 +348,6 @@ public: cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1; } else { - if (seg_to > box_end || seg_from < box_begin) return false; real_t length = seg_to - seg_from; @@ -395,7 +387,6 @@ public: } static bool segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Plane *p_planes, int p_plane_count, Vector3 *p_res, Vector3 *p_norm) { - real_t min = -1e20, max = 1e20; Vector3 rel = p_to - p_from; @@ -409,7 +400,6 @@ public: int min_index = -1; for (int i = 0; i < p_plane_count; i++) { - const Plane &p = p_planes[i]; real_t den = p.normal.dot(dir); @@ -424,7 +414,6 @@ public: if (dist < max) max = dist; } else { - // Front facing plane. if (dist > min) { min = dist; @@ -445,7 +434,6 @@ public: } static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 *p_segment) { - Vector3 p = p_point - p_segment[0]; Vector3 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); @@ -463,7 +451,6 @@ public: } static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 *p_segment) { - Vector3 p = p_point - p_segment[0]; Vector3 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); @@ -476,7 +463,6 @@ public: } static Vector2 get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 *p_segment) { - Vector2 p = p_point - p_segment[0]; Vector2 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); @@ -507,7 +493,6 @@ public: } static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 *p_segment) { - Vector2 p = p_point - p_segment[0]; Vector2 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); @@ -520,7 +505,6 @@ public: } static bool line_intersects_line_2d(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b, Vector2 &r_result) { - // See http://paulbourke.net/geometry/pointlineplane/ const real_t denom = p_dir_b.y * p_dir_a.x - p_dir_b.x * p_dir_a.y; @@ -535,7 +519,6 @@ public: } static bool segment_intersects_segment_2d(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b, Vector2 *r_result) { - Vector2 B = p_to_a - p_from_a; Vector2 C = p_from_b - p_from_a; Vector2 D = p_to_b - p_from_a; @@ -564,7 +547,6 @@ public: } static inline bool point_in_projected_triangle(const Vector3 &p_point, const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) { - Vector3 face_n = (p_v1 - p_v3).cross(p_v1 - p_v2); Vector3 n1 = (p_point - p_v3).cross(p_point - p_v2); @@ -586,7 +568,6 @@ public: } static inline bool triangle_sphere_intersection_test(const Vector3 *p_triangle, const Vector3 &p_normal, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 &r_triangle_contact, Vector3 &r_sphere_contact) { - real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle[0]); if (d > p_sphere_radius || d < -p_sphere_radius) // Not touching the plane of the face, return. @@ -608,7 +589,6 @@ public: const Vector3 verts[4] = { p_triangle[0], p_triangle[1], p_triangle[2], p_triangle[0] }; // for() friendly for (int i = 0; i < 3; i++) { - // Check edge cylinder. Vector3 n1 = verts[i] - verts[i + 1]; @@ -633,7 +613,6 @@ public: real_t sphere_at = n1.dot(n2); if (sphere_at >= 0 && sphere_at < n1.dot(n1)) { - r_triangle_contact = p_sphere_pos - axis * (axis.dot(n2)); r_sphere_contact = p_sphere_pos - axis * p_sphere_radius; // Point inside here. @@ -643,7 +622,6 @@ public: real_t r2 = p_sphere_radius * p_sphere_radius; if (n2.length_squared() < r2) { - Vector3 n = (p_sphere_pos - verts[i + 1]).normalized(); r_triangle_contact = verts[i + 1]; @@ -666,12 +644,10 @@ public: } static inline bool is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) { - return p_point.distance_squared_to(p_circle_pos) <= p_circle_radius * p_circle_radius; } static real_t segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) { - Vector2 line_vec = p_to - p_from; Vector2 vec_to_line = p_from - p_circle_pos; @@ -704,7 +680,6 @@ public: } static inline Vector<Vector3> clip_polygon(const Vector<Vector3> &polygon, const Plane &p_plane) { - enum LocationCache { LOC_INSIDE = 1, LOC_BOUNDARY = 0, @@ -734,11 +709,9 @@ public: } if (outside_count == 0) { - return polygon; // No changes. } else if (inside_count == 0) { - return Vector<Vector3>(); // Empty. } @@ -798,49 +771,40 @@ public: }; static Vector<Vector<Point2>> merge_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) { - return _polypaths_do_operation(OPERATION_UNION, p_polygon_a, p_polygon_b); } static Vector<Vector<Point2>> clip_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) { - return _polypaths_do_operation(OPERATION_DIFFERENCE, p_polygon_a, p_polygon_b); } static Vector<Vector<Point2>> intersect_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) { - return _polypaths_do_operation(OPERATION_INTERSECTION, p_polygon_a, p_polygon_b); } static Vector<Vector<Point2>> exclude_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) { - return _polypaths_do_operation(OPERATION_XOR, p_polygon_a, p_polygon_b); } static Vector<Vector<Point2>> clip_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) { - return _polypaths_do_operation(OPERATION_DIFFERENCE, p_polyline, p_polygon, true); } static Vector<Vector<Point2>> intersect_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) { - return _polypaths_do_operation(OPERATION_INTERSECTION, p_polyline, p_polygon, true); } static Vector<Vector<Point2>> offset_polygon_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) { - return _polypath_offset(p_polygon, p_delta, p_join_type, END_POLYGON); } static Vector<Vector<Point2>> offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) { - ERR_FAIL_COND_V_MSG(p_end_type == END_POLYGON, Vector<Vector<Point2>>(), "Attempt to offset a polyline like a polygon (use offset_polygon_2d instead)."); return _polypath_offset(p_polygon, p_delta, p_join_type, p_end_type); } static Vector<int> triangulate_delaunay_2d(const Vector<Vector2> &p_points) { - Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(p_points); Vector<int> triangles; @@ -853,7 +817,6 @@ public: } static Vector<int> triangulate_polygon(const Vector<Vector2> &p_polygon) { - Vector<int> triangles; if (!Triangulate::triangulate(p_polygon, triangles)) return Vector<int>(); //fail @@ -912,7 +875,6 @@ public: static Vector<Face3> wrap_geometry(Vector<Face3> p_array, real_t *p_error = nullptr); struct MeshData { - struct Face { Plane plane; Vector<int> indices; @@ -921,7 +883,6 @@ public: Vector<Face> faces; struct Edge { - int a, b; }; @@ -933,7 +894,6 @@ public: }; _FORCE_INLINE_ static int get_uv84_normal_bit(const Vector3 &p_vector) { - int lat = Math::fast_ftoi(Math::floor(Math::acos(p_vector.dot(Vector3(0, 1, 0))) * 4.0 / Math_PI + 0.5)); if (lat == 0) { @@ -948,13 +908,11 @@ public: } _FORCE_INLINE_ static int get_uv84_normal_bit_neighbors(int p_idx) { - if (p_idx == 24) { return 1 | 2 | 4 | 8; } else if (p_idx == 25) { return (1 << 23) | (1 << 22) | (1 << 21) | (1 << 20); } else { - int ret = 0; if ((p_idx % 8) == 0) ret |= (1 << (p_idx + 7)); @@ -1143,7 +1101,6 @@ public: return false; _FORCE_INLINE_ static bool triangle_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) { - /* use separating axis theorem to test overlap between triangle and box */ /* need to test for overlap in these directions: */ /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */ diff --git a/core/math/math_fieldwise.cpp b/core/math/math_fieldwise.cpp index a47d4ef7ad..ef2a0c5339 100644 --- a/core/math/math_fieldwise.cpp +++ b/core/math/math_fieldwise.cpp @@ -41,7 +41,6 @@ } Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const String &p_field) { - ERR_FAIL_COND_V(p_target.get_type() != p_source.get_type(), p_target); /* clang-format makes a mess of this macro usage */ diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 7417e64ac1..0c7a97e5c9 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -130,7 +130,6 @@ double Math::stepify(double p_value, double p_step) { } uint32_t Math::larger_prime(uint32_t p_val) { - static const uint32_t primes[] = { 5, 13, @@ -166,7 +165,6 @@ uint32_t Math::larger_prime(uint32_t p_val) { int idx = 0; while (true) { - ERR_FAIL_COND_V(primes[idx] == 0, 0); if (primes[idx] > p_val) return primes[idx]; diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index bd13c82894..33a7d602c3 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -41,7 +41,6 @@ #include <math.h> class Math { - static RandomPCG default_rand; public: @@ -328,7 +327,6 @@ public: } static _ALWAYS_INLINE_ float absf(float g) { - union { float f; uint32_t i; @@ -340,7 +338,6 @@ public: } static _ALWAYS_INLINE_ double absd(double g) { - union { double d; uint64_t i; @@ -352,7 +349,6 @@ public: //this function should be as fast as possible and rounding mode should not matter static _ALWAYS_INLINE_ int fast_ftoi(float a) { - static int b; #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone? @@ -407,7 +403,6 @@ public: } static _ALWAYS_INLINE_ float halfptr_to_float(const uint16_t *h) { - union { uint32_t u32; float f32; @@ -422,7 +417,6 @@ public: } static _ALWAYS_INLINE_ uint16_t make_half_float(float f) { - union { float fv; uint32_t ui; @@ -453,7 +447,6 @@ public: } // check if exponent is <= -15 else if (exp <= 0x38000000) { - /*// store a denorm half-float value or zero exp = (0x38000000 - exp) >> 23; mantissa >>= (14 + exp); diff --git a/core/math/octree.h b/core/math/octree.h index 7d89c50f69..0782a39804 100644 --- a/core/math/octree.h +++ b/core/math/octree.h @@ -68,7 +68,6 @@ private: }; struct PairKey { - union { struct { OctreeElementID A; @@ -78,18 +77,14 @@ private: }; _FORCE_INLINE_ bool operator<(const PairKey &p_pair) const { - return key < p_pair.key; } _FORCE_INLINE_ PairKey(OctreeElementID p_A, OctreeElementID p_B) { - if (p_A < p_B) { - A = p_A; B = p_B; } else { - B = p_A; A = p_B; } @@ -101,7 +96,6 @@ private: struct Element; struct Octant { - // cached for FAST plane check AABB aabb; @@ -122,7 +116,6 @@ private: struct PairData; struct Element { - Octree *octree = nullptr; T *userdata = nullptr; @@ -141,7 +134,6 @@ private: List<PairData *, AL> pair_list; struct OctantOwner { - Octant *octant; typename List<Element *, AL>::Element *E; }; // an element can be in max 8 octants @@ -152,7 +144,6 @@ private: }; struct PairData { - int refcount; bool intersect; Element *A, *B; @@ -179,19 +170,15 @@ private: int pair_count; _FORCE_INLINE_ void _pair_check(PairData *p_pair) { - bool intersect = p_pair->A->aabb.intersects_inclusive(p_pair->B->aabb); if (intersect != p_pair->intersect) { - if (intersect) { - if (pair_callback) { p_pair->ud = pair_callback(pair_callback_userdata, p_pair->A->_id, p_pair->A->userdata, p_pair->A->subindex, p_pair->B->_id, p_pair->B->userdata, p_pair->B->subindex); } pair_count++; } else { - if (unpair_callback) { unpair_callback(pair_callback_userdata, p_pair->A->_id, p_pair->A->userdata, p_pair->A->subindex, p_pair->B->_id, p_pair->B->userdata, p_pair->B->subindex, p_pair->ud); } @@ -203,7 +190,6 @@ private: } _FORCE_INLINE_ void _pair_reference(Element *p_A, Element *p_B) { - if (p_A == p_B || (p_A->userdata == p_B->userdata && p_A->userdata)) return; @@ -215,7 +201,6 @@ private: typename PairMap::Element *E = pair_map.find(key); if (!E) { - PairData pdata; pdata.refcount = 1; pdata.A = p_A; @@ -230,13 +215,11 @@ private: pair_callback(pair_callback_userdata,p_A->userdata,p_B->userdata); */ } else { - E->get().refcount++; } } _FORCE_INLINE_ void _pair_unreference(Element *p_A, Element *p_B) { - if (p_A == p_B) return; @@ -271,24 +254,18 @@ private: } _FORCE_INLINE_ void _element_check_pairs(Element *p_element) { - typename List<PairData *, AL>::Element *E = p_element->pair_list.front(); while (E) { - _pair_check(E->get()); E = E->next(); } } _FORCE_INLINE_ void _optimize() { - while (root && root->children_count < 2 && !root->elements.size() && !(use_pairs && root->pairable_elements.size())) { - Octant *new_root = nullptr; if (root->children_count == 1) { - for (int i = 0; i < 8; i++) { - if (root->children[i]) { new_root = root->children[i]; root->children[i] = nullptr; @@ -314,7 +291,6 @@ private: void _unpair_element(Element *p_element, Octant *p_octant); struct _CullConvexData { - const Plane *planes; int plane_count; const Vector3 *points; @@ -331,12 +307,10 @@ private: void _cull_point(Octant *p_octant, const Vector3 &p_point, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask); void _remove_tree(Octant *p_octant) { - if (!p_octant) return; for (int i = 0; i < 8; i++) { - if (p_octant->children[i]) _remove_tree(p_octant->children[i]); } @@ -380,7 +354,6 @@ T *Octree<T, use_pairs, AL>::get(OctreeElementID p_id) const { template <class T, bool use_pairs, class AL> bool Octree<T, use_pairs, AL>::is_pairable(OctreeElementID p_id) const { - const typename ElementMap::Element *E = element_map.find(p_id); ERR_FAIL_COND_V(!E, false); return E->get().pairable; @@ -388,7 +361,6 @@ bool Octree<T, use_pairs, AL>::is_pairable(OctreeElementID p_id) const { template <class T, bool use_pairs, class AL> int Octree<T, use_pairs, AL>::get_subindex(OctreeElementID p_id) const { - const typename ElementMap::Element *E = element_map.find(p_id); ERR_FAIL_COND_V(!E, -1); return E->get().subindex; @@ -398,7 +370,6 @@ int Octree<T, use_pairs, AL>::get_subindex(OctreeElementID p_id) const { template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_octant) { - real_t element_size = p_element->aabb.get_longest_axis_size() * 1.01; // avoid precision issues if (p_octant->aabb.size.x / OCTREE_DIVISOR < element_size) { @@ -409,11 +380,9 @@ void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_oct owner.octant = p_octant; if (use_pairs && p_element->pairable) { - p_octant->pairable_elements.push_back(p_element); owner.E = p_octant->pairable_elements.back(); } else { - p_octant->elements.push_back(p_element); owner.E = p_octant->elements.back(); } @@ -428,11 +397,9 @@ void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_oct } if (use_pairs && p_octant->children_count > 0) { - pass++; //elements below this only get ONE reference added for (int i = 0; i < 8; i++) { - if (p_octant->children[i]) { _pair_element(p_element, p_octant->children[i]); } @@ -444,7 +411,6 @@ void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_oct bool candidate = p_element->common_parent == nullptr; for (int i = 0; i < 8; i++) { - if (p_octant->children[i]) { /* element exists, go straight to it */ if (p_octant->children[i]->aabb.intersects_inclusive(p_element->aabb)) { @@ -484,13 +450,11 @@ void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_oct } if (candidate && splits > 1) { - p_element->common_parent = p_octant; } } if (use_pairs) { - typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front(); while (E) { @@ -511,14 +475,12 @@ void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_oct template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_ensure_valid_root(const AABB &p_aabb) { - if (!root) { // octre is empty AABB base(Vector3(), Vector3(1.0, 1.0, 1.0) * unit_size); while (!base.encloses(p_aabb)) { - if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) { /* grow towards positive */ base.size *= 2.0; @@ -537,11 +499,9 @@ void Octree<T, use_pairs, AL>::_ensure_valid_root(const AABB &p_aabb) { octant_count++; } else { - AABB base = root->aabb; while (!base.encloses(p_aabb)) { - ERR_FAIL_COND_MSG(base.size.x > OCTREE_SIZE_LIMIT, "Octree upper size limit reached, does the AABB supplied contain NAN?"); Octant *gp = memnew_allocator(Octant, AL); @@ -570,11 +530,9 @@ void Octree<T, use_pairs, AL>::_ensure_valid_root(const AABB &p_aabb) { template <class T, bool use_pairs, class AL> bool Octree<T, use_pairs, AL>::_remove_element_from_octant(Element *p_element, Octant *p_octant, Octant *p_limit) { - bool octant_removed = false; while (true) { - // check all exit conditions if (p_octant == p_limit) // reached limit, nothing to erase, exit @@ -607,7 +565,6 @@ bool Octree<T, use_pairs, AL>::_remove_element_from_octant(Element *p_element, O Octant *parent = p_octant->parent; if (p_octant->children_count == 0 && p_octant->elements.empty() && p_octant->pairable_elements.empty()) { - // erase octant if (p_octant == root) { // won't have a parent, just erase @@ -637,7 +594,6 @@ bool Octree<T, use_pairs, AL>::_remove_element_from_octant(Element *p_element, O template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_unpair_element(Element *p_element, Octant *p_octant) { - // always test pairable typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front(); while (E) { @@ -666,7 +622,6 @@ void Octree<T, use_pairs, AL>::_unpair_element(Element *p_element, Octant *p_oct return; // small optimization for leafs for (int i = 0; i < 8; i++) { - if (p_octant->children[i]) _unpair_element(p_element, p_octant->children[i]); } @@ -674,13 +629,11 @@ void Octree<T, use_pairs, AL>::_unpair_element(Element *p_element, Octant *p_oct template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_pair_element(Element *p_element, Octant *p_octant) { - // always test pairable typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front(); while (E) { - if (E->get()->last_pass != pass) { // only get ONE reference _pair_reference(p_element, E->get()); E->get()->last_pass = pass; @@ -705,7 +658,6 @@ void Octree<T, use_pairs, AL>::_pair_element(Element *p_element, Octant *p_octan return; // small optimization for leafs for (int i = 0; i < 8; i++) { - if (p_octant->children[i]) _pair_element(p_element, p_octant->children[i]); } @@ -713,14 +665,12 @@ void Octree<T, use_pairs, AL>::_pair_element(Element *p_element, Octant *p_octan template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_remove_element(Element *p_element) { - pass++; // will do a new pass for this typename List<typename Element::OctantOwner, AL>::Element *I = p_element->octant_owners.front(); /* FIRST remove going up normally */ for (; I; I = I->next()) { - Octant *o = I->get().octant; if (!use_pairs) // small speedup @@ -734,15 +684,12 @@ void Octree<T, use_pairs, AL>::_remove_element(Element *p_element) { I = p_element->octant_owners.front(); if (use_pairs) { - for (; I; I = I->next()) { - Octant *o = I->get().octant; // erase children pairs, they are erased ONCE even if repeated pass++; for (int i = 0; i < 8; i++) { - if (o->children[i]) _unpair_element(p_element, o->children[i]); } @@ -757,7 +704,6 @@ void Octree<T, use_pairs, AL>::_remove_element(Element *p_element) { p_element->octant_owners.clear(); if (use_pairs) { - int remaining = p_element->pair_list.size(); //p_element->pair_list.clear(); ERR_FAIL_COND(remaining); @@ -766,7 +712,6 @@ void Octree<T, use_pairs, AL>::_remove_element(Element *p_element) { template <class T, bool use_pairs, class AL> OctreeElementID Octree<T, use_pairs, AL>::create(T *p_userdata, const AABB &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) { - // check for AABB validity #ifdef DEBUG_ENABLED ERR_FAIL_COND_V(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15, 0); @@ -806,7 +751,6 @@ OctreeElementID Octree<T, use_pairs, AL>::create(T *p_userdata, const AABB &p_aa template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) { - #ifdef DEBUG_ENABLED // check for AABB validity ERR_FAIL_COND(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15); @@ -827,7 +771,6 @@ void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) { bool new_has_surf = !p_aabb.has_no_surface(); if (old_has_surf != new_has_surf) { - if (old_has_surf) { _remove_element(&e); // removing e.common_parent = nullptr; @@ -850,7 +793,6 @@ void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) { // it still is enclosed in the same AABB it was assigned to if (e.container_aabb.encloses(p_aabb)) { - e.aabb = p_aabb; if (use_pairs) _element_check_pairs(&e); // must check pairs anyway @@ -888,7 +830,6 @@ void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) { pass++; for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F;) { - Octant *o = F->get().octant; typename List<typename Element::OctantOwner, AL>::Element *N = F->next(); @@ -903,7 +844,6 @@ void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) { o->elements.erase(F->get().E); if (_remove_element_from_octant(&e, o, common_parent->parent)) { - owners.erase(F); } @@ -913,13 +853,11 @@ void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) { if (use_pairs) { //unpair child elements in anything that survived for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F; F = F->next()) { - Octant *o = F->get().octant; // erase children pairs, unref ONCE pass++; for (int i = 0; i < 8; i++) { - if (o->children[i]) _unpair_element(&e, o->children[i]); } @@ -933,7 +871,6 @@ void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) { template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::set_pairable(OctreeElementID p_id, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) { - typename ElementMap::Element *E = element_map.find(p_id); ERR_FAIL_COND(!E); @@ -961,14 +898,12 @@ void Octree<T, use_pairs, AL>::set_pairable(OctreeElementID p_id, bool p_pairabl template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::erase(OctreeElementID p_id) { - typename ElementMap::Element *E = element_map.find(p_id); ERR_FAIL_COND(!E); Element &e = E->get(); if (!e.aabb.has_no_surface()) { - _remove_element(&e); } @@ -978,17 +913,14 @@ void Octree<T, use_pairs, AL>::erase(OctreeElementID p_id) { template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p_cull) { - if (*p_cull->result_idx == p_cull->result_max) return; //pointless if (!p_octant->elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) @@ -1000,7 +932,6 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p p_cull->result_array[*p_cull->result_idx] = e->userdata; (*p_cull->result_idx)++; } else { - return; // pointless to continue } } @@ -1008,12 +939,10 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p } if (use_pairs && !p_octant->pairable_elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) @@ -1021,13 +950,10 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p e->last_pass = pass; if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) { - if (*p_cull->result_idx < p_cull->result_max) { - p_cull->result_array[*p_cull->result_idx] = e->userdata; (*p_cull->result_idx)++; } else { - return; // pointless to continue } } @@ -1035,7 +961,6 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p } for (int i = 0; i < 8; i++) { - if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) { _cull_convex(p_octant->children[i], p_cull); } @@ -1044,16 +969,13 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) { - if (*p_result_idx == p_result_max) return; //pointless if (!p_octant->elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) @@ -1061,16 +983,13 @@ void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, e->last_pass = pass; if (p_aabb.intersects_inclusive(e->aabb)) { - if (*p_result_idx < p_result_max) { - p_result_array[*p_result_idx] = e->userdata; if (p_subindex_array) p_subindex_array[*p_result_idx] = e->subindex; (*p_result_idx)++; } else { - return; // pointless to continue } } @@ -1078,11 +997,9 @@ void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, } if (use_pairs && !p_octant->pairable_elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) @@ -1090,15 +1007,12 @@ void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, e->last_pass = pass; if (p_aabb.intersects_inclusive(e->aabb)) { - if (*p_result_idx < p_result_max) { - p_result_array[*p_result_idx] = e->userdata; if (p_subindex_array) p_subindex_array[*p_result_idx] = e->subindex; (*p_result_idx)++; } else { - return; // pointless to continue } } @@ -1106,7 +1020,6 @@ void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, } for (int i = 0; i < 8; i++) { - if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_inclusive(p_aabb)) { _cull_aabb(p_octant->children[i], p_aabb, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask); } @@ -1115,16 +1028,13 @@ void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) { - if (*p_result_idx == p_result_max) return; //pointless if (!p_octant->elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) @@ -1132,16 +1042,13 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ e->last_pass = pass; if (e->aabb.intersects_segment(p_from, p_to)) { - if (*p_result_idx < p_result_max) { - p_result_array[*p_result_idx] = e->userdata; if (p_subindex_array) p_subindex_array[*p_result_idx] = e->subindex; (*p_result_idx)++; } else { - return; // pointless to continue } } @@ -1149,11 +1056,9 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ } if (use_pairs && !p_octant->pairable_elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) @@ -1162,9 +1067,7 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ e->last_pass = pass; if (e->aabb.intersects_segment(p_from, p_to)) { - if (*p_result_idx < p_result_max) { - p_result_array[*p_result_idx] = e->userdata; if (p_subindex_array) p_subindex_array[*p_result_idx] = e->subindex; @@ -1172,7 +1075,6 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ (*p_result_idx)++; } else { - return; // pointless to continue } } @@ -1180,7 +1082,6 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ } for (int i = 0; i < 8; i++) { - if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_segment(p_from, p_to)) { _cull_segment(p_octant->children[i], p_from, p_to, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask); } @@ -1189,16 +1090,13 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_point, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) { - if (*p_result_idx == p_result_max) return; //pointless if (!p_octant->elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) @@ -1206,16 +1104,13 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po e->last_pass = pass; if (e->aabb.has_point(p_point)) { - if (*p_result_idx < p_result_max) { - p_result_array[*p_result_idx] = e->userdata; if (p_subindex_array) p_subindex_array[*p_result_idx] = e->subindex; (*p_result_idx)++; } else { - return; // pointless to continue } } @@ -1223,11 +1118,9 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po } if (use_pairs && !p_octant->pairable_elements.empty()) { - typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); for (; I; I = I->next()) { - Element *e = I->get(); if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) @@ -1236,9 +1129,7 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po e->last_pass = pass; if (e->aabb.has_point(p_point)) { - if (*p_result_idx < p_result_max) { - p_result_array[*p_result_idx] = e->userdata; if (p_subindex_array) p_subindex_array[*p_result_idx] = e->subindex; @@ -1246,7 +1137,6 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po (*p_result_idx)++; } else { - return; // pointless to continue } } @@ -1254,7 +1144,6 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po } for (int i = 0; i < 8; i++) { - //could be optimized.. if (p_octant->children[i] && p_octant->children[i]->aabb.has_point(p_point)) { _cull_point(p_octant->children[i], p_point, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask); @@ -1264,7 +1153,6 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po template <class T, bool use_pairs, class AL> int Octree<T, use_pairs, AL>::cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask) { - if (!root || p_convex.size() == 0) return 0; @@ -1291,7 +1179,6 @@ int Octree<T, use_pairs, AL>::cull_convex(const Vector<Plane> &p_convex, T **p_r template <class T, bool use_pairs, class AL> int Octree<T, use_pairs, AL>::cull_aabb(const AABB &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) { - if (!root) return 0; @@ -1304,7 +1191,6 @@ int Octree<T, use_pairs, AL>::cull_aabb(const AABB &p_aabb, T **p_result_array, template <class T, bool use_pairs, class AL> int Octree<T, use_pairs, AL>::cull_segment(const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) { - if (!root) return 0; @@ -1317,7 +1203,6 @@ int Octree<T, use_pairs, AL>::cull_segment(const Vector3 &p_from, const Vector3 template <class T, bool use_pairs, class AL> int Octree<T, use_pairs, AL>::cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) { - if (!root) return 0; @@ -1330,20 +1215,17 @@ int Octree<T, use_pairs, AL>::cull_point(const Vector3 &p_point, T **p_result_ar template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::set_pair_callback(PairCallback p_callback, void *p_userdata) { - pair_callback = p_callback; pair_callback_userdata = p_userdata; } template <class T, bool use_pairs, class AL> void Octree<T, use_pairs, AL>::set_unpair_callback(UnpairCallback p_callback, void *p_userdata) { - unpair_callback = p_callback; unpair_callback_userdata = p_userdata; } template <class T, bool use_pairs, class AL> Octree<T, use_pairs, AL>::Octree(real_t p_unit_size) { - last_element_id = 1; pass = 1; unit_size = p_unit_size; diff --git a/core/math/plane.cpp b/core/math/plane.cpp index 26ac0aac47..94bdd41bd4 100644 --- a/core/math/plane.cpp +++ b/core/math/plane.cpp @@ -33,12 +33,10 @@ #include "core/math/math_funcs.h" void Plane::set_normal(const Vector3 &p_normal) { - normal = p_normal; } void Plane::normalize() { - real_t l = normal.length(); if (l == 0) { *this = Plane(0, 0, 0, 0); @@ -49,19 +47,16 @@ void Plane::normalize() { } Plane Plane::normalized() const { - Plane p = *this; p.normalize(); return p; } Vector3 Plane::get_any_point() const { - return get_normal() * d; } Vector3 Plane::get_any_perpendicular_normal() const { - static const Vector3 p1 = Vector3(1, 0, 0); static const Vector3 p2 = Vector3(0, 1, 0); Vector3 p; @@ -80,7 +75,6 @@ Vector3 Plane::get_any_perpendicular_normal() const { /* intersections */ bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const { - const Plane &p_plane0 = *this; Vector3 normal0 = p_plane0.normal; Vector3 normal1 = p_plane1.normal; @@ -102,13 +96,11 @@ bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r } bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const { - Vector3 segment = p_dir; real_t den = normal.dot(segment); //printf("den is %i\n",den); if (Math::is_zero_approx(den)) { - return false; } @@ -127,13 +119,11 @@ bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 } bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const { - Vector3 segment = p_begin - p_end; real_t den = normal.dot(segment); //printf("den is %i\n",den); if (Math::is_zero_approx(den)) { - return false; } @@ -141,7 +131,6 @@ bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vec //printf("dist is %i\n",dist); if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) { - return false; } @@ -158,11 +147,9 @@ bool Plane::is_equal_approx_any_side(const Plane &p_plane) const { } bool Plane::is_equal_approx(const Plane &p_plane) const { - return normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d); } Plane::operator String() const { - return normal.operator String() + ", " + rtos(d); } diff --git a/core/math/plane.h b/core/math/plane.h index f4f205465f..017835a6da 100644 --- a/core/math/plane.h +++ b/core/math/plane.h @@ -61,7 +61,6 @@ public: bool intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const; _FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const { - return p_point - normal * distance_to(p_point); } @@ -86,17 +85,14 @@ public: }; bool Plane::is_point_over(const Vector3 &p_point) const { - return (normal.dot(p_point) > d); } real_t Plane::distance_to(const Vector3 &p_point) const { - return (normal.dot(p_point) - d); } bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const { - real_t dist = normal.dot(p_point) - d; dist = ABS(dist); return (dist <= _epsilon); @@ -113,7 +109,6 @@ Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) : } Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) { - if (p_dir == CLOCKWISE) normal = (p_point1 - p_point3).cross(p_point1 - p_point2); else @@ -124,12 +119,10 @@ Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_ } bool Plane::operator==(const Plane &p_plane) const { - return normal == p_plane.normal && d == p_plane.d; } bool Plane::operator!=(const Plane &p_plane) const { - return normal != p_plane.normal || d != p_plane.d; } diff --git a/core/math/quat.cpp b/core/math/quat.cpp index 6fbea70279..f4b708616e 100644 --- a/core/math/quat.cpp +++ b/core/math/quat.cpp @@ -107,7 +107,6 @@ Vector3 Quat::get_euler_yxz() const { } void Quat::operator*=(const Quat &q) { - set(w * q.x + x * q.w + y * q.z - z * q.y, w * q.y + y * q.w + z * q.x - x * q.z, w * q.z + z * q.w + x * q.y - y * q.x, @@ -115,19 +114,16 @@ void Quat::operator*=(const Quat &q) { } Quat Quat::operator*(const Quat &q) const { - Quat r = *this; r *= q; return r; } bool Quat::is_equal_approx(const Quat &p_quat) const { - return Math::is_equal_approx(x, p_quat.x) && Math::is_equal_approx(y, p_quat.y) && Math::is_equal_approx(z, p_quat.z) && Math::is_equal_approx(w, p_quat.w); } real_t Quat::length() const { - return Math::sqrt(length_squared()); } @@ -233,7 +229,6 @@ Quat Quat::cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const } Quat::operator String() const { - return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w); } diff --git a/core/math/quat.h b/core/math/quat.h index 1ca6fe7ce3..64d0f00912 100644 --- a/core/math/quat.h +++ b/core/math/quat.h @@ -149,7 +149,6 @@ public: z = 0; w = 0; } else { - real_t s = Math::sqrt((1.0 + d) * 2.0); real_t rs = 1.0 / s; @@ -191,7 +190,6 @@ void Quat::operator*=(const real_t &s) { } void Quat::operator/=(const real_t &s) { - *this *= 1.0 / s; } diff --git a/core/math/quick_hull.cpp b/core/math/quick_hull.cpp index 7fbb26c377..ace8ac9878 100644 --- a/core/math/quick_hull.cpp +++ b/core/math/quick_hull.cpp @@ -35,12 +35,10 @@ uint32_t QuickHull::debug_stop_after = 0xFFFFFFFF; Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_mesh) { - /* CREATE AABB VOLUME */ AABB aabb; for (int i = 0; i < p_points.size(); i++) { - if (i == 0) { aabb.position = p_points[i]; } else { @@ -57,7 +55,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me Set<Vector3> valid_cache; for (int i = 0; i < p_points.size(); i++) { - Vector3 sp = p_points[i].snapped(Vector3(0.0001, 0.0001, 0.0001)); if (valid_cache.has(sp)) { valid_points.write[i] = false; @@ -78,12 +75,10 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me real_t max = 0, min = 0; for (int i = 0; i < p_points.size(); i++) { - if (!valid_points[i]) continue; real_t d = p_points[i][longest_axis]; if (i == 0 || d < min) { - simplex[0] = i; min = d; } @@ -102,7 +97,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me Vector3 rel12 = p_points[simplex[0]] - p_points[simplex[1]]; for (int i = 0; i < p_points.size(); i++) { - if (!valid_points[i]) continue; @@ -110,7 +104,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me real_t d = Math::abs(n.dot(p_points[simplex[0]]) - n.dot(p_points[i])); if (i == 0 || d > maxd) { - maxd = d; simplex[2] = i; } @@ -124,14 +117,12 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me Plane p(p_points[simplex[0]], p_points[simplex[1]], p_points[simplex[2]]); for (int i = 0; i < p_points.size(); i++) { - if (!valid_points[i]) continue; real_t d = Math::abs(p.distance_to(p_points[i])); if (i == 0 || d > maxd) { - maxd = d; simplex[3] = i; } @@ -152,7 +143,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me List<Face> faces; for (int i = 0; i < 4; i++) { - static const int face_order[4][3] = { { 0, 1, 2 }, { 0, 1, 3 }, @@ -183,7 +173,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me /* COMPUTE AVAILABLE VERTICES */ for (int i = 0; i < p_points.size(); i++) { - if (i == simplex[0]) continue; if (i == simplex[1]) @@ -196,9 +185,7 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me continue; for (List<Face>::Element *E = faces.front(); E; E = E->next()) { - if (E->get().plane.distance_to(p_points[i]) > over_tolerance) { - E->get().points_over.push_back(i); break; } @@ -219,7 +206,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me uint32_t debug_stop = debug_stop_after; while (debug_stop > 0 && faces.back()->get().points_over.size()) { - debug_stop--; Face &f = faces.back()->get(); @@ -228,7 +214,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me real_t next_d = 0; for (int i = 0; i < f.points_over.size(); i++) { - real_t d = f.plane.distance_to(p_points[f.points_over[i]]); if (d > next_d) { @@ -247,9 +232,7 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me Map<Edge, FaceConnect> lit_edges; //create this on the flight, should not be that bad for performance and simplifies code a lot for (List<Face>::Element *E = faces.front(); E; E = E->next()) { - if (E->get().plane.distance_to(v) > 0) { - lit_faces.push_back(E); for (int i = 0; i < 3; i++) { @@ -265,7 +248,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me //left F->get().left = E; } else { - F->get().right = E; } } @@ -276,7 +258,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me List<List<Face>::Element *> new_faces; //new faces for (Map<Edge, FaceConnect>::Element *E = lit_edges.front(); E; E = E->next()) { - FaceConnect &fc = E->get(); if (fc.left && fc.right) { continue; //edge is uninteresting, not on horizont @@ -304,17 +285,14 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me //distribute points into new faces for (List<List<Face>::Element *>::Element *F = lit_faces.front(); F; F = F->next()) { - Face &lf = F->get()->get(); for (int i = 0; i < lf.points_over.size(); i++) { - if (lf.points_over[i] == f.points_over[next]) //do not add current one continue; Vector3 p = p_points[lf.points_over[i]]; for (List<List<Face>::Element *>::Element *E = new_faces.front(); E; E = E->next()) { - Face &f2 = E->get()->get(); if (f2.plane.distance_to(p) > over_tolerance) { f2.points_over.push_back(lf.points_over[i]); @@ -327,7 +305,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me //erase lit faces while (lit_faces.size()) { - faces.erase(lit_faces.front()->get()); lit_faces.pop_front(); } @@ -335,7 +312,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me //put faces that contain no points on the front for (List<List<Face>::Element *>::Element *E = new_faces.front(); E; E = E->next()) { - Face &f2 = E->get()->get(); if (f2.points_over.size() == 0) { faces.move_to_front(E->get()); @@ -352,7 +328,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me List<Geometry::MeshData::Face> ret_faces; for (List<Face>::Element *E = faces.front(); E; E = E->next()) { - Geometry::MeshData::Face f; f.plane = E->get().plane; @@ -363,7 +338,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me List<Geometry::MeshData::Face>::Element *F = ret_faces.push_back(f); for (int i = 0; i < 3; i++) { - uint32_t a = E->get().vertices[i]; uint32_t b = E->get().vertices[(i + 1) % 3]; Edge e(a, b); @@ -376,7 +350,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me //left G->get().left = F; } else { - G->get().right = F; } } @@ -385,11 +358,9 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me //fill faces for (List<Geometry::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) { - Geometry::MeshData::Face &f = E->get(); for (int i = 0; i < f.indices.size(); i++) { - int a = E->get().indices[i]; int b = E->get().indices[(i + 1) % f.indices.size()]; Edge e(a, b); @@ -411,7 +382,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me 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]; if (idx == b && idxn == a) { //already have b! @@ -463,7 +433,6 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_me r_mesh.edges.resize(ret_edges.size()); idx = 0; for (Map<Edge, RetFaceConnect>::Element *E = ret_edges.front(); E; E = E->next()) { - Geometry::MeshData::Edge e; e.a = E->key().vertices[0]; e.b = E->key().vertices[1]; diff --git a/core/math/quick_hull.h b/core/math/quick_hull.h index 89061ab415..29f709febe 100644 --- a/core/math/quick_hull.h +++ b/core/math/quick_hull.h @@ -37,10 +37,8 @@ #include "core/set.h" class QuickHull { - public: struct Edge { - union { uint32_t vertices[2]; uint64_t id; @@ -51,7 +49,6 @@ public: } Edge(int p_vtx_a = 0, int p_vtx_b = 0) { - if (p_vtx_a > p_vtx_b) { SWAP(p_vtx_a, p_vtx_b); } @@ -62,13 +59,11 @@ public: }; struct Face { - Plane plane; uint32_t vertices[3]; Vector<int> points_over; bool operator<(const Face &p_face) const { - return points_over.size() < p_face.points_over.size(); } }; diff --git a/core/math/rect2.cpp b/core/math/rect2.cpp index 12b9904c88..1e26f815ed 100644 --- a/core/math/rect2.cpp +++ b/core/math/rect2.cpp @@ -31,12 +31,10 @@ #include "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D bool Rect2::is_equal_approx(const Rect2 &p_rect) const { - return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size); } bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const { - real_t min = 0, max = 1; int axis = 0; real_t sign = 0; @@ -50,7 +48,6 @@ bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 real_t csign; if (seg_from < seg_to) { - if (seg_from > box_end || seg_to < box_begin) return false; real_t length = seg_to - seg_from; @@ -59,7 +56,6 @@ bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 csign = -1.0; } else { - if (seg_to > box_end || seg_from < box_begin) return false; real_t length = seg_to - seg_from; @@ -94,7 +90,6 @@ bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 } bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const { - //SAT intersection between local and transformed rect2 Vector2 xf_points[4] = { diff --git a/core/math/rect2.h b/core/math/rect2.h index a3f3634bfb..f6274ae32b 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -36,7 +36,6 @@ struct Transform2D; struct Rect2 { - Point2 position; Size2 size; @@ -72,7 +71,6 @@ struct Rect2 { } inline real_t distance_to(const Vector2 &p_point) const { - real_t dist = 0.0; bool inside = true; @@ -108,14 +106,12 @@ struct Rect2 { bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const; inline bool encloses(const Rect2 &p_rect) const { - return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) && ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y)); } _FORCE_INLINE_ bool has_no_area() const { - return (size.x <= 0 || size.y <= 0); } inline Rect2 clip(const Rect2 &p_rect) const { /// return a clipped rect @@ -170,7 +166,6 @@ struct Rect2 { bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; } inline Rect2 grow(real_t p_by) const { - Rect2 g = *this; g.position.x -= p_by; g.position.y -= p_by; @@ -189,7 +184,6 @@ struct Rect2 { } inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const { - Rect2 g = *this; g.position.x -= p_left; g.position.y -= p_top; @@ -200,7 +194,6 @@ struct Rect2 { } _FORCE_INLINE_ Rect2 expand(const Vector2 &p_vector) const { - Rect2 r = *this; r.expand_to(p_vector); return r; @@ -226,7 +219,6 @@ struct Rect2 { } _FORCE_INLINE_ Rect2 abs() const { - return Rect2(Point2(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs()); } @@ -244,7 +236,6 @@ struct Rect2 { }; struct Rect2i { - Point2i position; Size2i size; @@ -269,14 +260,12 @@ struct Rect2i { } inline bool encloses(const Rect2i &p_rect) const { - return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) && ((p_rect.position.y + p_rect.size.y) < (position.y + size.y)); } _FORCE_INLINE_ bool has_no_area() const { - return (size.x <= 0 || size.y <= 0); } inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect @@ -330,7 +319,6 @@ struct Rect2i { bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; } Rect2i grow(int p_by) const { - Rect2i g = *this; g.position.x -= p_by; g.position.y -= p_by; @@ -349,7 +337,6 @@ struct Rect2i { } inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const { - Rect2i g = *this; g.position.x -= p_left; g.position.y -= p_top; @@ -360,14 +347,12 @@ struct Rect2i { } _FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const { - Rect2i r = *this; r.expand_to(p_vector); return r; } inline void expand_to(const Point2i &p_vector) { - Point2i begin = position; Point2i end = position + size; @@ -386,7 +371,6 @@ struct Rect2i { } _FORCE_INLINE_ Rect2i abs() const { - return Rect2i(Point2i(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs()); } diff --git a/core/math/transform.cpp b/core/math/transform.cpp index 82e4005d3e..0f62c8b2c0 100644 --- a/core/math/transform.cpp +++ b/core/math/transform.cpp @@ -35,20 +35,17 @@ #include "core/print_string.h" void Transform::affine_invert() { - basis.invert(); origin = basis.xform(-origin); } Transform Transform::affine_inverse() const { - Transform ret = *this; ret.affine_invert(); return ret; } void Transform::invert() { - basis.transpose(); origin = basis.xform(-origin); } @@ -62,22 +59,18 @@ Transform Transform::inverse() const { } void Transform::rotate(const Vector3 &p_axis, real_t p_phi) { - *this = rotated(p_axis, p_phi); } Transform Transform::rotated(const Vector3 &p_axis, real_t p_phi) const { - return Transform(Basis(p_axis, p_phi), Vector3()) * (*this); } void Transform::rotate_basis(const Vector3 &p_axis, real_t p_phi) { - basis.rotate(p_axis, p_phi); } Transform Transform::looking_at(const Vector3 &p_target, const Vector3 &p_up) const { - Transform t = *this; t.set_look_at(origin, p_target, p_up); return t; @@ -117,7 +110,6 @@ void Transform::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const } Transform Transform::interpolate_with(const Transform &p_transform, real_t p_c) const { - /* not sure if very "efficient" but good enough? */ Vector3 src_scale = basis.get_scale(); @@ -136,20 +128,17 @@ Transform Transform::interpolate_with(const Transform &p_transform, real_t p_c) } void Transform::scale(const Vector3 &p_scale) { - basis.scale(p_scale); origin *= p_scale; } Transform Transform::scaled(const Vector3 &p_scale) const { - Transform t = *this; t.scale(p_scale); return t; } void Transform::scale_basis(const Vector3 &p_scale) { - basis.scale(p_scale); } @@ -157,60 +146,50 @@ void Transform::translate(real_t p_tx, real_t p_ty, real_t p_tz) { translate(Vector3(p_tx, p_ty, p_tz)); } void Transform::translate(const Vector3 &p_translation) { - for (int i = 0; i < 3; i++) { origin[i] += basis[i].dot(p_translation); } } Transform Transform::translated(const Vector3 &p_translation) const { - Transform t = *this; t.translate(p_translation); return t; } void Transform::orthonormalize() { - basis.orthonormalize(); } Transform Transform::orthonormalized() const { - Transform _copy = *this; _copy.orthonormalize(); return _copy; } bool Transform::is_equal_approx(const Transform &p_transform) const { - return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin); } bool Transform::operator==(const Transform &p_transform) const { - return (basis == p_transform.basis && origin == p_transform.origin); } bool Transform::operator!=(const Transform &p_transform) const { - return (basis != p_transform.basis || origin != p_transform.origin); } void Transform::operator*=(const Transform &p_transform) { - origin = xform(p_transform.origin); basis *= p_transform.basis; } Transform Transform::operator*(const Transform &p_transform) const { - Transform t = *this; t *= p_transform; return t; } Transform::operator String() const { - return basis.operator String() + " - " + origin.operator String(); } diff --git a/core/math/transform.h b/core/math/transform.h index c6e3be4c70..7f7e9ce833 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -92,14 +92,12 @@ public: Transform interpolate_with(const Transform &p_transform, real_t p_c) const; _FORCE_INLINE_ Transform inverse_xform(const Transform &t) const { - Vector3 v = t.origin - origin; return Transform(basis.transpose_xform(t.basis), basis.xform(v)); } void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t tx, real_t ty, real_t tz) { - basis.set(xx, xy, xz, yx, yy, yz, zx, zy, zz); origin.x = tx; origin.y = ty; @@ -114,14 +112,12 @@ public: }; _FORCE_INLINE_ Vector3 Transform::xform(const Vector3 &p_vector) const { - return Vector3( basis[0].dot(p_vector) + origin.x, basis[1].dot(p_vector) + origin.y, basis[2].dot(p_vector) + origin.z); } _FORCE_INLINE_ Vector3 Transform::xform_inv(const Vector3 &p_vector) const { - Vector3 v = p_vector - origin; return Vector3( @@ -131,7 +127,6 @@ _FORCE_INLINE_ Vector3 Transform::xform_inv(const Vector3 &p_vector) const { } _FORCE_INLINE_ Plane Transform::xform(const Plane &p_plane) const { - Vector3 point = p_plane.normal * p_plane.d; Vector3 point_dir = point + p_plane.normal; point = xform(point); @@ -144,7 +139,6 @@ _FORCE_INLINE_ Plane Transform::xform(const Plane &p_plane) const { return Plane(normal, d); } _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const { - Vector3 point = p_plane.normal * p_plane.d; Vector3 point_dir = point + p_plane.normal; xform_inv(point); @@ -158,7 +152,6 @@ _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const { } _FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const { - /* http://dev.theomader.com/transform-bounding-boxes/ */ Vector3 min = p_aabb.position; Vector3 max = p_aabb.position + p_aabb.size; @@ -184,7 +177,6 @@ _FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const { } _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { - /* define vertices */ Vector3 vertices[8] = { Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z), @@ -202,7 +194,6 @@ _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { ret.position = xform_inv(vertices[0]); for (int i = 1; i < 8; i++) { - ret.expand_to(xform_inv(vertices[i])); } @@ -210,7 +201,6 @@ _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { } Vector<Vector3> Transform::xform(const Vector<Vector3> &p_array) const { - Vector<Vector3> array; array.resize(p_array.size()); @@ -224,7 +214,6 @@ Vector<Vector3> Transform::xform(const Vector<Vector3> &p_array) const { } Vector<Vector3> Transform::xform_inv(const Vector<Vector3> &p_array) const { - Vector<Vector3> array; array.resize(p_array.size()); diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index ed95baa233..f82d1d99c4 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -38,14 +38,12 @@ void Transform2D::invert() { } Transform2D Transform2D::inverse() const { - Transform2D inv = *this; inv.invert(); return inv; } void Transform2D::affine_invert() { - real_t det = basis_determinant(); #ifdef MATH_CHECKS ERR_FAIL_COND(det == 0); @@ -60,7 +58,6 @@ void Transform2D::affine_invert() { } Transform2D Transform2D::affine_inverse() const { - Transform2D inv = *this; inv.affine_invert(); return inv; @@ -71,13 +68,11 @@ void Transform2D::rotate(real_t p_phi) { } real_t Transform2D::get_skew() const { - real_t det = basis_determinant(); return Math::acos(elements[0].normalized().dot(SGN(det) * elements[1].normalized())) - Math_PI * 0.5; } void Transform2D::set_skew(float p_angle) { - real_t det = basis_determinant(); elements[1] = SGN(det) * elements[0].rotated((Math_PI * 0.5 + p_angle)).normalized() * elements[1].length(); } @@ -103,7 +98,6 @@ void Transform2D::set_rotation(real_t p_rot) { } Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) { - real_t cr = Math::cos(p_rot); real_t sr = Math::sin(p_rot); elements[0][0] = cr; @@ -130,23 +124,19 @@ void Transform2D::scale(const Size2 &p_scale) { elements[2] *= p_scale; } void Transform2D::scale_basis(const Size2 &p_scale) { - elements[0][0] *= p_scale.x; elements[0][1] *= p_scale.y; elements[1][0] *= p_scale.x; elements[1][1] *= p_scale.y; } void Transform2D::translate(real_t p_tx, real_t p_ty) { - translate(Vector2(p_tx, p_ty)); } void Transform2D::translate(const Vector2 &p_translation) { - elements[2] += basis_xform(p_translation); } void Transform2D::orthonormalize() { - // Gram-Schmidt Process Vector2 x = elements[0]; @@ -161,19 +151,16 @@ void Transform2D::orthonormalize() { } Transform2D Transform2D::orthonormalized() const { - Transform2D on = *this; on.orthonormalize(); return on; } bool Transform2D::is_equal_approx(const Transform2D &p_transform) const { - return elements[0].is_equal_approx(p_transform.elements[0]) && elements[1].is_equal_approx(p_transform.elements[1]) && elements[2].is_equal_approx(p_transform.elements[2]); } bool Transform2D::operator==(const Transform2D &p_transform) const { - for (int i = 0; i < 3; i++) { if (elements[i] != p_transform.elements[i]) return false; @@ -183,7 +170,6 @@ bool Transform2D::operator==(const Transform2D &p_transform) const { } bool Transform2D::operator!=(const Transform2D &p_transform) const { - for (int i = 0; i < 3; i++) { if (elements[i] != p_transform.elements[i]) return true; @@ -193,7 +179,6 @@ bool Transform2D::operator!=(const Transform2D &p_transform) const { } void Transform2D::operator*=(const Transform2D &p_transform) { - elements[2] = xform(p_transform.elements[2]); real_t x0, x1, y0, y1; @@ -210,54 +195,46 @@ void Transform2D::operator*=(const Transform2D &p_transform) { } Transform2D Transform2D::operator*(const Transform2D &p_transform) const { - Transform2D t = *this; t *= p_transform; return t; } Transform2D Transform2D::scaled(const Size2 &p_scale) const { - Transform2D copy = *this; copy.scale(p_scale); return copy; } Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const { - Transform2D copy = *this; copy.scale_basis(p_scale); return copy; } Transform2D Transform2D::untranslated() const { - Transform2D copy = *this; copy.elements[2] = Vector2(); return copy; } Transform2D Transform2D::translated(const Vector2 &p_offset) const { - Transform2D copy = *this; copy.translate(p_offset); return copy; } Transform2D Transform2D::rotated(real_t p_phi) const { - Transform2D copy = *this; copy.rotate(p_phi); return copy; } real_t Transform2D::basis_determinant() const { - return elements[0].x * elements[1].y - elements[0].y * elements[1].x; } Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_c) const { - //extract parameters Vector2 p1 = get_origin(); Vector2 p2 = p_transform.get_origin(); @@ -293,6 +270,5 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t } Transform2D::operator String() const { - return String(String() + elements[0] + ", " + elements[1] + ", " + elements[2]); } diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index 459ceed7a9..66958257d7 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -120,7 +120,6 @@ struct Transform2D { operator String() const; Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) { - elements[0][0] = xx; elements[0][1] = xy; elements[1][0] = yx; @@ -137,28 +136,24 @@ struct Transform2D { }; Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const { - return Vector2( tdotx(p_vec), tdoty(p_vec)); } Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const { - return Vector2( elements[0].dot(p_vec), elements[1].dot(p_vec)); } Vector2 Transform2D::xform(const Vector2 &p_vec) const { - return Vector2( tdotx(p_vec), tdoty(p_vec)) + elements[2]; } Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const { - Vector2 v = p_vec - elements[2]; return Vector2( @@ -166,7 +161,6 @@ Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const { elements[1].dot(v)); } Rect2 Transform2D::xform(const Rect2 &p_rect) const { - Vector2 x = elements[0] * p_rect.size.x; Vector2 y = elements[1] * p_rect.size.y; Vector2 pos = xform(p_rect.position); @@ -180,7 +174,6 @@ Rect2 Transform2D::xform(const Rect2 &p_rect) const { } void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) { - elements[0][0] = Math::cos(p_rot) * p_scale.x; elements[1][1] = Math::cos(p_rot) * p_scale.y; elements[1][0] = -Math::sin(p_rot) * p_scale.y; @@ -188,7 +181,6 @@ void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) { } void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew) { - elements[0][0] = Math::cos(p_rot) * p_scale.x; elements[1][1] = Math::cos(p_rot + p_skew) * p_scale.y; elements[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y; @@ -196,7 +188,6 @@ void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale } Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const { - Vector2 ends[4] = { xform_inv(p_rect.position), xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)), @@ -214,7 +205,6 @@ Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const { } Vector<Vector2> Transform2D::xform(const Vector<Vector2> &p_array) const { - Vector<Vector2> array; array.resize(p_array.size()); @@ -228,7 +218,6 @@ Vector<Vector2> Transform2D::xform(const Vector<Vector2> &p_array) const { } Vector<Vector2> Transform2D::xform_inv(const Vector<Vector2> &p_array) const { - Vector<Vector2> array; array.resize(p_array.size()); diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp index 0f7350a260..9a608e3f1b 100644 --- a/core/math/triangle_mesh.cpp +++ b/core/math/triangle_mesh.cpp @@ -33,30 +33,25 @@ #include "core/sort_array.h" int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc) { - if (p_depth > max_depth) { max_depth = p_depth; } if (p_size == 1) { - return p_bb[p_from] - p_bvh; } else if (p_size == 0) { - return -1; } AABB aabb; aabb = p_bb[p_from]->aabb; for (int i = 1; i < p_size; i++) { - aabb.merge_with(p_bb[p_from + i]->aabb); } int li = aabb.get_longest_axis_index(); switch (li) { - case Vector3::AXIS_X: { SortArray<BVH *, BVHCmpX> sort_x; sort_x.nth_element(0, p_size, p_size / 2, &p_bb[p_from]); @@ -90,7 +85,6 @@ int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, in } void TriangleMesh::get_indices(Vector<int> *r_triangles_indices) const { - if (!valid) return; @@ -110,7 +104,6 @@ void TriangleMesh::get_indices(Vector<int> *r_triangles_indices) const { } void TriangleMesh::create(const Vector<Vector3> &p_faces) { - valid = false; int fc = p_faces.size(); @@ -122,7 +115,6 @@ void TriangleMesh::create(const Vector<Vector3> &p_faces) { BVH *bw = bvh.ptrw(); { - //create faces and indices and base bvh //except for the Set for repeated triangles, everything //goes in-place. @@ -132,12 +124,10 @@ void TriangleMesh::create(const Vector<Vector3> &p_faces) { Map<Vector3, int> db; for (int i = 0; i < fc; i++) { - Triangle &f = w[i]; const Vector3 *v = &r[i * 3]; for (int j = 0; j < 3; j++) { - int vidx = -1; Vector3 vs = v[j].snapped(Vector3(0.0001, 0.0001, 0.0001)); Map<Vector3, int>::Element *E = db.find(vs); @@ -174,7 +164,6 @@ void TriangleMesh::create(const Vector<Vector3> &p_faces) { bwptrs.resize(fc); BVH **bwp = bwptrs.ptrw(); for (int i = 0; i < fc; i++) { - bwp[i] = &bw[i]; } @@ -188,7 +177,6 @@ void TriangleMesh::create(const Vector<Vector3> &p_faces) { } Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { - uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); enum { @@ -215,23 +203,18 @@ Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { 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: { - bool valid = b.aabb.intersects(p_aabb); if (!valid) { - 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++; @@ -239,28 +222,24 @@ Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { 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; stack[level + 1] = b.left | TEST_AABB_BIT; level++; continue; } case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; stack[level + 1] = b.right | TEST_AABB_BIT; level++; continue; } case VISIT_DONE_BIT: { - if (level == 0) { done = true; break; @@ -281,7 +260,6 @@ Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { } bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const { - uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); enum { @@ -309,35 +287,28 @@ bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_en 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: { - bool valid = b.aabb.intersects_segment(p_begin, p_end); //bool valid = b.aabb.intersects(ray_aabb); if (!valid) { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; } else { - if (b.face_index >= 0) { - const Triangle &s = triangleptr[b.face_index]; Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]); Vector3 res; if (f3.intersects_segment(p_begin, p_end, &res)) { - real_t nd = n.dot(res); if (nd < d) { - d = nd; r_point = res; r_normal = f3.get_plane().get_normal(); @@ -348,28 +319,24 @@ bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_en 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; stack[level + 1] = b.left | TEST_AABB_BIT; level++; continue; } case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; stack[level + 1] = b.right | TEST_AABB_BIT; level++; continue; } case VISIT_DONE_BIT: { - if (level == 0) { done = true; break; @@ -384,7 +351,6 @@ bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_en } if (inters) { - if (n.dot(r_normal) > 0) r_normal = -r_normal; } @@ -393,7 +359,6 @@ bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_en } bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const { - uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); enum { @@ -421,33 +386,26 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V 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: { - bool valid = b.aabb.intersects_ray(p_begin, p_dir); if (!valid) { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; } else { - if (b.face_index >= 0) { - const Triangle &s = triangleptr[b.face_index]; Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]); Vector3 res; if (f3.intersects_ray(p_begin, p_dir, &res)) { - real_t nd = n.dot(res); if (nd < d) { - d = nd; r_point = res; r_normal = f3.get_plane().get_normal(); @@ -458,28 +416,24 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V 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; stack[level + 1] = b.left | TEST_AABB_BIT; level++; continue; } case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; stack[level + 1] = b.right | TEST_AABB_BIT; level++; continue; } case VISIT_DONE_BIT: { - if (level == 0) { done = true; break; @@ -494,7 +448,6 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V } if (inters) { - if (n.dot(r_normal) > 0) r_normal = -r_normal; } @@ -528,23 +481,18 @@ bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_cou 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: { - bool valid = b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count); if (!valid) { - 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) { @@ -582,28 +530,24 @@ bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_cou 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; stack[level + 1] = b.left | TEST_AABB_BIT; level++; continue; } case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; stack[level + 1] = b.right | TEST_AABB_BIT; level++; continue; } case VISIT_DONE_BIT: { - if (level == 0) { done = true; break; @@ -646,25 +590,21 @@ bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, 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: { - bool intersects = scale.xform(b.aabb).intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count); if (!intersects) return false; bool inside = scale.xform(b.aabb).inside_convex_shape(p_planes, p_plane_count); if (inside) { - 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) { @@ -679,28 +619,24 @@ bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_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; stack[level + 1] = b.left | TEST_AABB_BIT; level++; continue; } case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; stack[level + 1] = b.right | TEST_AABB_BIT; level++; continue; } case VISIT_DONE_BIT: { - if (level == 0) { done = true; break; @@ -718,12 +654,10 @@ bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, } bool TriangleMesh::is_valid() const { - return valid; } Vector<Face3> TriangleMesh::get_faces() const { - if (!valid) return Vector<Face3>(); @@ -745,7 +679,6 @@ Vector<Face3> TriangleMesh::get_faces() const { } TriangleMesh::TriangleMesh() { - valid = false; max_depth = 0; } diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index 64704477cc..86412cf725 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -35,11 +35,9 @@ #include "core/reference.h" class TriangleMesh : public Reference { - GDCLASS(TriangleMesh, Reference); struct Triangle { - Vector3 normal; int indices[3]; }; @@ -48,7 +46,6 @@ class TriangleMesh : public Reference { Vector<Vector3> vertices; struct BVH { - AABB aabb; Vector3 center; //used for sorting int left; @@ -58,24 +55,18 @@ class TriangleMesh : public Reference { }; struct BVHCmpX { - bool operator()(const BVH *p_left, const BVH *p_right) const { - return p_left->center.x < p_right->center.x; } }; struct BVHCmpY { - bool operator()(const BVH *p_left, const BVH *p_right) const { - return p_left->center.y < p_right->center.y; } }; struct BVHCmpZ { - bool operator()(const BVH *p_left, const BVH *p_right) const { - return p_left->center.z < p_right->center.z; } }; diff --git a/core/math/triangulate.cpp b/core/math/triangulate.cpp index ae278b034d..c7b838fd10 100644 --- a/core/math/triangulate.cpp +++ b/core/math/triangulate.cpp @@ -31,7 +31,6 @@ #include "triangulate.h" real_t Triangulate::get_area(const Vector<Vector2> &contour) { - int n = contour.size(); const Vector2 *c = &contour[0]; diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index f46badd19e..d06f64b40b 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -31,25 +31,20 @@ #include "vector2.h" real_t Vector2::angle() const { - return Math::atan2(y, x); } real_t Vector2::length() const { - return Math::sqrt(x * x + y * y); } real_t Vector2::length_squared() const { - return x * x + y * y; } void Vector2::normalize() { - real_t l = x * x + y * y; if (l != 0) { - l = Math::sqrt(l); x /= l; y /= l; @@ -57,7 +52,6 @@ void Vector2::normalize() { } Vector2 Vector2::normalized() const { - Vector2 v = *this; v.normalize(); return v; @@ -69,52 +63,42 @@ bool Vector2::is_normalized() const { } real_t Vector2::distance_to(const Vector2 &p_vector2) const { - return Math::sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y)); } real_t Vector2::distance_squared_to(const Vector2 &p_vector2) const { - return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y); } real_t Vector2::angle_to(const Vector2 &p_vector2) const { - return Math::atan2(cross(p_vector2), dot(p_vector2)); } real_t Vector2::angle_to_point(const Vector2 &p_vector2) const { - return Math::atan2(y - p_vector2.y, x - p_vector2.x); } real_t Vector2::dot(const Vector2 &p_other) const { - return x * p_other.x + y * p_other.y; } real_t Vector2::cross(const Vector2 &p_other) const { - return x * p_other.y - y * p_other.x; } Vector2 Vector2::sign() const { - return Vector2(SGN(x), SGN(y)); } Vector2 Vector2::floor() const { - return Vector2(Math::floor(x), Math::floor(y)); } Vector2 Vector2::ceil() const { - return Vector2(Math::ceil(x), Math::ceil(y)); } Vector2 Vector2::round() const { - return Vector2(Math::round(x), Math::round(y)); } @@ -139,18 +123,15 @@ Vector2 Vector2::project(const Vector2 &p_b) const { } Vector2 Vector2::snapped(const Vector2 &p_by) const { - return Vector2( Math::stepify(x, p_by.x), Math::stepify(y, p_by.y)); } Vector2 Vector2::clamped(real_t p_len) const { - real_t l = length(); Vector2 v = *this; if (l > 0 && p_len < l) { - v /= l; v *= p_len; } @@ -159,7 +140,6 @@ Vector2 Vector2::clamped(real_t p_len) const { } Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const { - Vector2 p0 = p_pre_a; Vector2 p1 = *this; Vector2 p2 = p_b; @@ -210,65 +190,52 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const { /* Vector2i */ Vector2i Vector2i::operator+(const Vector2i &p_v) const { - return Vector2i(x + p_v.x, y + p_v.y); } void Vector2i::operator+=(const Vector2i &p_v) { - x += p_v.x; y += p_v.y; } Vector2i Vector2i::operator-(const Vector2i &p_v) const { - return Vector2i(x - p_v.x, y - p_v.y); } void Vector2i::operator-=(const Vector2i &p_v) { - x -= p_v.x; y -= p_v.y; } Vector2i Vector2i::operator*(const Vector2i &p_v1) const { - return Vector2i(x * p_v1.x, y * p_v1.y); }; Vector2i Vector2i::operator*(const int &rvalue) const { - return Vector2i(x * rvalue, y * rvalue); }; void Vector2i::operator*=(const int &rvalue) { - x *= rvalue; y *= rvalue; }; Vector2i Vector2i::operator/(const Vector2i &p_v1) const { - return Vector2i(x / p_v1.x, y / p_v1.y); }; Vector2i Vector2i::operator/(const int &rvalue) const { - return Vector2i(x / rvalue, y / rvalue); }; void Vector2i::operator/=(const int &rvalue) { - x /= rvalue; y /= rvalue; }; Vector2i Vector2i::operator-() const { - return Vector2i(-x, -y); } bool Vector2i::operator==(const Vector2i &p_vec2) const { - return x == p_vec2.x && y == p_vec2.y; } bool Vector2i::operator!=(const Vector2i &p_vec2) const { - return x != p_vec2.x || y != p_vec2.y; } diff --git a/core/math/vector2.h b/core/math/vector2.h index 5a3e6a0660..5aa40d45f7 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -37,7 +37,6 @@ struct Vector2i; struct Vector2 { - enum Axis { AXIS_X, AXIS_Y, @@ -123,13 +122,11 @@ struct Vector2 { real_t angle() const; _FORCE_INLINE_ Vector2 abs() const { - return Vector2(Math::abs(x), Math::abs(y)); } Vector2 rotated(real_t p_by) const; Vector2 tangent() const { - return Vector2(y, -x); } @@ -150,81 +147,65 @@ struct Vector2 { }; _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const { - return p_vec - *this * (dot(p_vec) - p_d); } _FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) { - return p_vec * p_scalar; } _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const { - return Vector2(x + p_v.x, y + p_v.y); } _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) { - x += p_v.x; y += p_v.y; } _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const { - return Vector2(x - p_v.x, y - p_v.y); } _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) { - x -= p_v.x; y -= p_v.y; } _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const { - return Vector2(x * p_v1.x, y * p_v1.y); }; _FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const { - return Vector2(x * rvalue, y * rvalue); }; _FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) { - x *= rvalue; y *= rvalue; }; _FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const { - return Vector2(x / p_v1.x, y / p_v1.y); }; _FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const { - return Vector2(x / rvalue, y / rvalue); }; _FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) { - x /= rvalue; y /= rvalue; }; _FORCE_INLINE_ Vector2 Vector2::operator-() const { - return Vector2(-x, -y); } _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const { - return x == p_vec2.x && y == p_vec2.y; } _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const { - return x != p_vec2.x || y != p_vec2.y; } Vector2 Vector2::lerp(const Vector2 &p_b, real_t p_t) const { - Vector2 res = *this; res.x += (p_t * (p_b.x - x)); @@ -253,7 +234,6 @@ typedef Vector2 Point2; /* INTEGER STUFF */ struct Vector2i { - enum Axis { AXIS_X, AXIS_Y, diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index 353b2acd16..8acbe31f35 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -33,12 +33,10 @@ #include "core/math/basis.h" void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) { - *this = Basis(p_axis, p_phi).xform(*this); } Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const { - Vector3 r = *this; r.rotate(p_axis, p_phi); return r; @@ -49,35 +47,29 @@ void Vector3::set_axis(int p_axis, real_t p_value) { coord[p_axis] = p_value; } real_t Vector3::get_axis(int p_axis) const { - ERR_FAIL_INDEX_V(p_axis, 3, 0); return operator[](p_axis); } int Vector3::min_axis() const { - return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2); } int Vector3::max_axis() const { - return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0); } void Vector3::snap(Vector3 p_val) { - x = Math::stepify(x, p_val.x); y = Math::stepify(y, p_val.y); z = Math::stepify(z, p_val.z); } Vector3 Vector3::snapped(Vector3 p_val) const { - Vector3 v = *this; v.snap(p_val); return v; } Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const { - Vector3 p0 = p_pre_a; Vector3 p1 = *this; Vector3 p2 = p_b; @@ -109,7 +101,6 @@ Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, } Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const { - Vector3 p0 = p_pre_a; Vector3 p1 = *this; Vector3 p2 = p_b; @@ -135,7 +126,6 @@ Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const { } Basis Vector3::outer(const Vector3 &p_b) const { - Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z); Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z); Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z); @@ -150,11 +140,9 @@ Basis Vector3::to_diagonal_matrix() const { } bool Vector3::is_equal_approx(const Vector3 &p_v) const { - return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z); } Vector3::operator String() const { - return (rtos(x) + ", " + rtos(y) + ", " + rtos(z)); } diff --git a/core/math/vector3.h b/core/math/vector3.h index 7131063e04..5fc412628f 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -38,7 +38,6 @@ class Basis; struct Vector3 { - enum Axis { AXIS_X, AXIS_Y, @@ -56,12 +55,10 @@ struct Vector3 { }; _FORCE_INLINE_ const real_t &operator[](int p_axis) const { - return coord[p_axis]; } _FORCE_INLINE_ real_t &operator[](int p_axis) { - return coord[p_axis]; } @@ -166,7 +163,6 @@ struct Vector3 { }; Vector3 Vector3::cross(const Vector3 &p_b) const { - Vector3 ret( (y * p_b.z) - (z * p_b.y), (z * p_b.x) - (x * p_b.z), @@ -176,37 +172,30 @@ Vector3 Vector3::cross(const Vector3 &p_b) const { } real_t Vector3::dot(const Vector3 &p_b) const { - return x * p_b.x + y * p_b.y + z * p_b.z; } Vector3 Vector3::abs() const { - return Vector3(Math::abs(x), Math::abs(y), Math::abs(z)); } Vector3 Vector3::sign() const { - return Vector3(SGN(x), SGN(y), SGN(z)); } Vector3 Vector3::floor() const { - return Vector3(Math::floor(x), Math::floor(y), Math::floor(z)); } Vector3 Vector3::ceil() const { - return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z)); } Vector3 Vector3::round() const { - return Vector3(Math::round(x), Math::round(y), Math::round(z)); } Vector3 Vector3::lerp(const Vector3 &p_b, real_t p_t) const { - return Vector3( x + (p_t * (p_b.x - x)), y + (p_t * (p_b.y - y)), @@ -219,12 +208,10 @@ Vector3 Vector3::slerp(const Vector3 &p_b, real_t p_t) const { } real_t Vector3::distance_to(const Vector3 &p_b) const { - return (p_b - *this).length(); } real_t Vector3::distance_squared_to(const Vector3 &p_b) const { - return (p_b - *this).length_squared(); } @@ -241,7 +228,6 @@ Vector3 Vector3::project(const Vector3 &p_b) const { } real_t Vector3::angle_to(const Vector3 &p_b) const { - return Math::atan2(cross(p_b).length(), dot(p_b)); } @@ -254,7 +240,6 @@ Vector3 Vector3::direction_to(const Vector3 &p_b) const { /* Operators */ Vector3 &Vector3::operator+=(const Vector3 &p_v) { - x += p_v.x; y += p_v.y; z += p_v.z; @@ -262,36 +247,30 @@ Vector3 &Vector3::operator+=(const Vector3 &p_v) { } Vector3 Vector3::operator+(const Vector3 &p_v) const { - return Vector3(x + p_v.x, y + p_v.y, z + p_v.z); } Vector3 &Vector3::operator-=(const Vector3 &p_v) { - x -= p_v.x; y -= p_v.y; z -= p_v.z; return *this; } Vector3 Vector3::operator-(const Vector3 &p_v) const { - return Vector3(x - p_v.x, y - p_v.y, z - p_v.z); } Vector3 &Vector3::operator*=(const Vector3 &p_v) { - x *= p_v.x; y *= p_v.y; z *= p_v.z; return *this; } Vector3 Vector3::operator*(const Vector3 &p_v) const { - return Vector3(x * p_v.x, y * p_v.y, z * p_v.z); } Vector3 &Vector3::operator/=(const Vector3 &p_v) { - x /= p_v.x; y /= p_v.y; z /= p_v.z; @@ -299,12 +278,10 @@ Vector3 &Vector3::operator/=(const Vector3 &p_v) { } Vector3 Vector3::operator/(const Vector3 &p_v) const { - return Vector3(x / p_v.x, y / p_v.y, z / p_v.z); } Vector3 &Vector3::operator*=(real_t p_scalar) { - x *= p_scalar; y *= p_scalar; z *= p_scalar; @@ -312,17 +289,14 @@ Vector3 &Vector3::operator*=(real_t p_scalar) { } _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) { - return p_vec * p_scalar; } Vector3 Vector3::operator*(real_t p_scalar) const { - return Vector3(x * p_scalar, y * p_scalar, z * p_scalar); } Vector3 &Vector3::operator/=(real_t p_scalar) { - x /= p_scalar; y /= p_scalar; z /= p_scalar; @@ -330,27 +304,22 @@ Vector3 &Vector3::operator/=(real_t p_scalar) { } Vector3 Vector3::operator/(real_t p_scalar) const { - return Vector3(x / p_scalar, y / p_scalar, z / p_scalar); } Vector3 Vector3::operator-() const { - return Vector3(-x, -y, -z); } bool Vector3::operator==(const Vector3 &p_v) const { - return x == p_v.x && y == p_v.y && z == p_v.z; } bool Vector3::operator!=(const Vector3 &p_v) const { - return x != p_v.x || y != p_v.y || z != p_v.z; } bool Vector3::operator<(const Vector3 &p_v) const { - if (Math::is_equal_approx(x, p_v.x)) { if (Math::is_equal_approx(y, p_v.y)) return z < p_v.z; @@ -362,7 +331,6 @@ bool Vector3::operator<(const Vector3 &p_v) const { } bool Vector3::operator>(const Vector3 &p_v) const { - if (Math::is_equal_approx(x, p_v.x)) { if (Math::is_equal_approx(y, p_v.y)) return z > p_v.z; @@ -374,7 +342,6 @@ bool Vector3::operator>(const Vector3 &p_v) const { } bool Vector3::operator<=(const Vector3 &p_v) const { - if (Math::is_equal_approx(x, p_v.x)) { if (Math::is_equal_approx(y, p_v.y)) return z <= p_v.z; @@ -386,7 +353,6 @@ bool Vector3::operator<=(const Vector3 &p_v) const { } bool Vector3::operator>=(const Vector3 &p_v) const { - if (Math::is_equal_approx(x, p_v.x)) { if (Math::is_equal_approx(y, p_v.y)) return z >= p_v.z; @@ -398,17 +364,14 @@ bool Vector3::operator>=(const Vector3 &p_v) const { } _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) { - return p_a.cross(p_b); } _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) { - return p_a.dot(p_b); } real_t Vector3::length() const { - real_t x2 = x * x; real_t y2 = y * y; real_t z2 = z * z; @@ -417,7 +380,6 @@ real_t Vector3::length() const { } real_t Vector3::length_squared() const { - real_t x2 = x * x; real_t y2 = y * y; real_t z2 = z * z; @@ -426,7 +388,6 @@ real_t Vector3::length_squared() const { } void Vector3::normalize() { - real_t lengthsq = length_squared(); if (lengthsq == 0) { x = y = z = 0; @@ -439,7 +400,6 @@ void Vector3::normalize() { } Vector3 Vector3::normalized() const { - Vector3 v = *this; v.normalize(); return v; @@ -451,12 +411,10 @@ bool Vector3::is_normalized() const { } Vector3 Vector3::inverse() const { - return Vector3(1.0 / x, 1.0 / y, 1.0 / z); } void Vector3::zero() { - x = y = z = 0; } diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp index 8a4ddf03b9..e621d5493a 100644 --- a/core/math/vector3i.cpp +++ b/core/math/vector3i.cpp @@ -35,21 +35,17 @@ void Vector3i::set_axis(int p_axis, int32_t p_value) { 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 index 60e5b94c12..5ecd3228b2 100644 --- a/core/math/vector3i.h +++ b/core/math/vector3i.h @@ -35,7 +35,6 @@ #include "core/ustring.h" struct Vector3i { - enum Axis { AXIS_X, AXIS_Y, @@ -53,12 +52,10 @@ struct Vector3i { }; _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]; } @@ -109,19 +106,16 @@ struct Vector3i { }; 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; @@ -129,36 +123,30 @@ Vector3i &Vector3i::operator+=(const Vector3i &p_v) { } 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; @@ -166,12 +154,10 @@ Vector3i &Vector3i::operator/=(const Vector3i &p_v) { } 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; @@ -179,17 +165,14 @@ Vector3i &Vector3i::operator*=(int32_t p_scalar) { } _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; @@ -197,27 +180,22 @@ Vector3i &Vector3i::operator/=(int32_t p_scalar) { } 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; @@ -229,7 +207,6 @@ bool Vector3i::operator<(const Vector3i &p_v) const { } bool Vector3i::operator>(const Vector3i &p_v) const { - if (x == p_v.x) { if (y == p_v.y) return z > p_v.z; @@ -241,7 +218,6 @@ bool Vector3i::operator>(const Vector3i &p_v) const { } bool Vector3i::operator<=(const Vector3i &p_v) const { - if (x == p_v.x) { if (y == p_v.y) return z <= p_v.z; @@ -253,7 +229,6 @@ bool Vector3i::operator<=(const Vector3i &p_v) const { } bool Vector3i::operator>=(const Vector3i &p_v) const { - if (x == p_v.x) { if (y == p_v.y) return z >= p_v.z; @@ -265,7 +240,6 @@ bool Vector3i::operator>=(const Vector3i &p_v) const { } void Vector3i::zero() { - x = y = z = 0; } |