forked from sascha/godot
Merge pull request #39051 from Xrayez/geometry-split
Split `Geometry` singleton into `Geometry2D` and `Geometry3D`4.0
commit
1620669f4e
@ -0,0 +1,384 @@
|
||||
/*************************************************************************/
|
||||
/* geometry.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "geometry_2d.h"
|
||||
|
||||
#include "thirdparty/misc/clipper.hpp"
|
||||
#include "thirdparty/misc/triangulator.h"
|
||||
#define STB_RECT_PACK_IMPLEMENTATION
|
||||
#include "thirdparty/misc/stb_rect_pack.h"
|
||||
|
||||
#define SCALE_FACTOR 100000.0 // Based on CMP_EPSILON.
|
||||
|
||||
Vector<Vector<Vector2>> Geometry2D::decompose_polygon_in_convex(Vector<Point2> polygon) {
|
||||
Vector<Vector<Vector2>> decomp;
|
||||
List<TriangulatorPoly> in_poly, out_poly;
|
||||
|
||||
TriangulatorPoly inp;
|
||||
inp.Init(polygon.size());
|
||||
for (int i = 0; i < polygon.size(); i++) {
|
||||
inp.GetPoint(i) = polygon[i];
|
||||
}
|
||||
inp.SetOrientation(TRIANGULATOR_CCW);
|
||||
in_poly.push_back(inp);
|
||||
TriangulatorPartition tpart;
|
||||
if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { // Failed.
|
||||
ERR_PRINT("Convex decomposing failed!");
|
||||
return decomp;
|
||||
}
|
||||
|
||||
decomp.resize(out_poly.size());
|
||||
int idx = 0;
|
||||
for (List<TriangulatorPoly>::Element *I = out_poly.front(); I; I = I->next()) {
|
||||
TriangulatorPoly &tp = I->get();
|
||||
|
||||
decomp.write[idx].resize(tp.GetNumPoints());
|
||||
|
||||
for (int64_t i = 0; i < tp.GetNumPoints(); i++) {
|
||||
decomp.write[idx].write[i] = tp.GetPoint(i);
|
||||
}
|
||||
|
||||
idx++;
|
||||
}
|
||||
|
||||
return decomp;
|
||||
}
|
||||
|
||||
struct _AtlasWorkRect {
|
||||
Size2i s;
|
||||
Point2i p;
|
||||
int idx;
|
||||
_FORCE_INLINE_ bool operator<(const _AtlasWorkRect &p_r) const { return s.width > p_r.s.width; };
|
||||
};
|
||||
|
||||
struct _AtlasWorkRectResult {
|
||||
Vector<_AtlasWorkRect> result;
|
||||
int max_w;
|
||||
int max_h;
|
||||
};
|
||||
|
||||
void Geometry2D::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.
|
||||
// On texture size (usually 2048 or 4096), so the more square a texture, the more chances.
|
||||
// It will work in every hardware.
|
||||
// For example, it will prioritize a 1024x1024 atlas (works everywhere) instead of a
|
||||
// 256x8192 atlas (won't work anywhere).
|
||||
|
||||
ERR_FAIL_COND(p_rects.size() == 0);
|
||||
|
||||
Vector<_AtlasWorkRect> wrects;
|
||||
wrects.resize(p_rects.size());
|
||||
for (int i = 0; i < p_rects.size(); i++) {
|
||||
wrects.write[i].s = p_rects[i];
|
||||
wrects.write[i].idx = i;
|
||||
}
|
||||
wrects.sort();
|
||||
int widest = wrects[0].s.width;
|
||||
|
||||
Vector<_AtlasWorkRectResult> results;
|
||||
|
||||
for (int i = 0; i <= 12; i++) {
|
||||
int w = 1 << i;
|
||||
int max_h = 0;
|
||||
int max_w = 0;
|
||||
if (w < widest) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector<int> hmax;
|
||||
hmax.resize(w);
|
||||
for (int j = 0; j < w; j++) {
|
||||
hmax.write[j] = 0;
|
||||
}
|
||||
|
||||
// Place them.
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
wrects.write[j].p.x = ofs;
|
||||
wrects.write[j].p.y = from_y;
|
||||
int end_h = from_y + wrects[j].s.height;
|
||||
int end_w = ofs + wrects[j].s.width;
|
||||
if (ofs == 0) {
|
||||
limit_h = end_h;
|
||||
}
|
||||
|
||||
for (int k = 0; k < wrects[j].s.width; k++) {
|
||||
hmax.write[ofs + k] = end_h;
|
||||
}
|
||||
|
||||
if (end_h > max_h) {
|
||||
max_h = end_h;
|
||||
}
|
||||
|
||||
if (end_w > max_w) {
|
||||
max_w = end_w;
|
||||
}
|
||||
|
||||
if (ofs == 0 || end_h > limit_h) { // While h limit not reached, keep stacking.
|
||||
ofs += wrects[j].s.width;
|
||||
}
|
||||
}
|
||||
|
||||
_AtlasWorkRectResult result;
|
||||
result.result = wrects;
|
||||
result.max_h = max_h;
|
||||
result.max_w = max_w;
|
||||
results.push_back(result);
|
||||
}
|
||||
|
||||
// Find the result with the best aspect ratio.
|
||||
|
||||
int best = -1;
|
||||
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;
|
||||
if (aspect < best_aspect) {
|
||||
best = i;
|
||||
best_aspect = aspect;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
r_size = Size2(results[best].max_w, results[best].max_h);
|
||||
}
|
||||
|
||||
Vector<Vector<Point2>> Geometry2D::_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;
|
||||
|
||||
switch (p_op) {
|
||||
case OPERATION_UNION:
|
||||
op = ctUnion;
|
||||
break;
|
||||
case OPERATION_DIFFERENCE:
|
||||
op = ctDifference;
|
||||
break;
|
||||
case OPERATION_INTERSECTION:
|
||||
op = ctIntersection;
|
||||
break;
|
||||
case OPERATION_XOR:
|
||||
op = ctXor;
|
||||
break;
|
||||
}
|
||||
Path path_a, path_b;
|
||||
|
||||
// Need to scale points (Clipper's requirement for robust computation).
|
||||
for (int i = 0; i != p_polypath_a.size(); ++i) {
|
||||
path_a << IntPoint(p_polypath_a[i].x * SCALE_FACTOR, p_polypath_a[i].y * SCALE_FACTOR);
|
||||
}
|
||||
for (int i = 0; i != p_polypath_b.size(); ++i) {
|
||||
path_b << IntPoint(p_polypath_b[i].x * SCALE_FACTOR, p_polypath_b[i].y * SCALE_FACTOR);
|
||||
}
|
||||
Clipper clp;
|
||||
clp.AddPath(path_a, ptSubject, !is_a_open); // Forward compatible with Clipper 10.0.0.
|
||||
clp.AddPath(path_b, ptClip, true); // Polylines cannot be set as clip.
|
||||
|
||||
Paths paths;
|
||||
|
||||
if (is_a_open) {
|
||||
PolyTree tree; // Needed to populate polylines.
|
||||
clp.Execute(op, tree);
|
||||
OpenPathsFromPolyTree(tree, paths);
|
||||
} else {
|
||||
clp.Execute(op, paths); // Works on closed polygons only.
|
||||
}
|
||||
// Have to scale points down now.
|
||||
Vector<Vector<Point2>> polypaths;
|
||||
|
||||
for (Paths::size_type i = 0; i < paths.size(); ++i) {
|
||||
Vector<Vector2> polypath;
|
||||
|
||||
const Path &scaled_path = paths[i];
|
||||
|
||||
for (Paths::size_type j = 0; j < scaled_path.size(); ++j) {
|
||||
polypath.push_back(Point2(
|
||||
static_cast<real_t>(scaled_path[j].X) / SCALE_FACTOR,
|
||||
static_cast<real_t>(scaled_path[j].Y) / SCALE_FACTOR));
|
||||
}
|
||||
polypaths.push_back(polypath);
|
||||
}
|
||||
return polypaths;
|
||||
}
|
||||
|
||||
Vector<Vector<Point2>> Geometry2D::_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;
|
||||
|
||||
switch (p_join_type) {
|
||||
case JOIN_SQUARE:
|
||||
jt = jtSquare;
|
||||
break;
|
||||
case JOIN_ROUND:
|
||||
jt = jtRound;
|
||||
break;
|
||||
case JOIN_MITER:
|
||||
jt = jtMiter;
|
||||
break;
|
||||
}
|
||||
|
||||
EndType et = etClosedPolygon;
|
||||
|
||||
switch (p_end_type) {
|
||||
case END_POLYGON:
|
||||
et = etClosedPolygon;
|
||||
break;
|
||||
case END_JOINED:
|
||||
et = etClosedLine;
|
||||
break;
|
||||
case END_BUTT:
|
||||
et = etOpenButt;
|
||||
break;
|
||||
case END_SQUARE:
|
||||
et = etOpenSquare;
|
||||
break;
|
||||
case END_ROUND:
|
||||
et = etOpenRound;
|
||||
break;
|
||||
}
|
||||
ClipperOffset co(2.0, 0.25 * SCALE_FACTOR); // Defaults from ClipperOffset.
|
||||
Path path;
|
||||
|
||||
// Need to scale points (Clipper's requirement for robust computation).
|
||||
for (int i = 0; i != p_polypath.size(); ++i) {
|
||||
path << IntPoint(p_polypath[i].x * SCALE_FACTOR, p_polypath[i].y * SCALE_FACTOR);
|
||||
}
|
||||
co.AddPath(path, jt, et);
|
||||
|
||||
Paths paths;
|
||||
co.Execute(paths, p_delta * SCALE_FACTOR); // Inflate/deflate.
|
||||
|
||||
// Have to scale points down now.
|
||||
Vector<Vector<Point2>> polypaths;
|
||||
|
||||
for (Paths::size_type i = 0; i < paths.size(); ++i) {
|
||||
Vector<Vector2> polypath;
|
||||
|
||||
const Path &scaled_path = paths[i];
|
||||
|
||||
for (Paths::size_type j = 0; j < scaled_path.size(); ++j) {
|
||||
polypath.push_back(Point2(
|
||||
static_cast<real_t>(scaled_path[j].X) / SCALE_FACTOR,
|
||||
static_cast<real_t>(scaled_path[j].Y) / SCALE_FACTOR));
|
||||
}
|
||||
polypaths.push_back(polypath);
|
||||
}
|
||||
return polypaths;
|
||||
}
|
||||
|
||||
Vector<Point2i> Geometry2D::pack_rects(const Vector<Size2i> &p_sizes, const Size2i &p_atlas_size) {
|
||||
Vector<stbrp_node> nodes;
|
||||
nodes.resize(p_atlas_size.width);
|
||||
|
||||
stbrp_context context;
|
||||
stbrp_init_target(&context, p_atlas_size.width, p_atlas_size.height, nodes.ptrw(), p_atlas_size.width);
|
||||
|
||||
Vector<stbrp_rect> rects;
|
||||
rects.resize(p_sizes.size());
|
||||
|
||||
for (int i = 0; i < p_sizes.size(); i++) {
|
||||
rects.write[i].id = 0;
|
||||
rects.write[i].w = p_sizes[i].width;
|
||||
rects.write[i].h = p_sizes[i].height;
|
||||
rects.write[i].x = 0;
|
||||
rects.write[i].y = 0;
|
||||
rects.write[i].was_packed = 0;
|
||||
}
|
||||
|
||||
int res = stbrp_pack_rects(&context, rects.ptrw(), rects.size());
|
||||
if (res == 0) { //pack failed
|
||||
return Vector<Point2i>();
|
||||
}
|
||||
|
||||
Vector<Point2i> ret;
|
||||
ret.resize(p_sizes.size());
|
||||
|
||||
for (int i = 0; i < p_sizes.size(); i++) {
|
||||
Point2i r(rects[i].x, rects[i].y);
|
||||
ret.write[i] = r;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vector<Vector3i> Geometry2D::partial_pack_rects(const Vector<Vector2i> &p_sizes, const Size2i &p_atlas_size) {
|
||||
Vector<stbrp_node> nodes;
|
||||
nodes.resize(p_atlas_size.width);
|
||||
zeromem(nodes.ptrw(), sizeof(stbrp_node) * nodes.size());
|
||||
|
||||
stbrp_context context;
|
||||
stbrp_init_target(&context, p_atlas_size.width, p_atlas_size.height, nodes.ptrw(), p_atlas_size.width);
|
||||
|
||||
Vector<stbrp_rect> rects;
|
||||
rects.resize(p_sizes.size());
|
||||
|
||||
for (int i = 0; i < p_sizes.size(); i++) {
|
||||
rects.write[i].id = i;
|
||||
rects.write[i].w = p_sizes[i].width;
|
||||
rects.write[i].h = p_sizes[i].height;
|
||||
rects.write[i].x = 0;
|
||||
rects.write[i].y = 0;
|
||||
rects.write[i].was_packed = 0;
|
||||
}
|
||||
|
||||
stbrp_pack_rects(&context, rects.ptrw(), rects.size());
|
||||
|
||||
Vector<Vector3i> ret;
|
||||
ret.resize(p_sizes.size());
|
||||
|
||||
for (int i = 0; i < p_sizes.size(); i++) {
|
||||
ret.write[rects[i].id] = Vector3i(rects[i].x, rects[i].y, rects[i].was_packed != 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -0,0 +1,398 @@
|
||||
/*************************************************************************/
|
||||
/* geometry_2d.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef GEOMETRY_2D_H
|
||||
#define GEOMETRY_2D_H
|
||||
|
||||
#include "core/math/delaunay_2d.h"
|
||||
#include "core/math/rect2.h"
|
||||
#include "core/math/triangulate.h"
|
||||
#include "core/object.h"
|
||||
#include "core/vector.h"
|
||||
|
||||
class Geometry2D {
|
||||
Geometry2D();
|
||||
|
||||
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;
|
||||
real_t a = d1.dot(d1); // Squared length of segment S1, always nonnegative.
|
||||
real_t e = d2.dot(d2); // Squared length of segment S2, always nonnegative.
|
||||
real_t f = d2.dot(r);
|
||||
real_t s, t;
|
||||
// Check if either or both segments degenerate into points.
|
||||
if (a <= CMP_EPSILON && e <= CMP_EPSILON) {
|
||||
// Both segments degenerate into points.
|
||||
c1 = p1;
|
||||
c2 = p2;
|
||||
return Math::sqrt((c1 - c2).dot(c1 - c2));
|
||||
}
|
||||
if (a <= CMP_EPSILON) {
|
||||
// First segment degenerates into a point.
|
||||
s = 0.0;
|
||||
t = f / e; // s = 0 => t = (b*s + f) / e = f / e
|
||||
t = CLAMP(t, 0.0, 1.0);
|
||||
} else {
|
||||
real_t c = d1.dot(r);
|
||||
if (e <= CMP_EPSILON) {
|
||||
// Second segment degenerates into a point.
|
||||
t = 0.0;
|
||||
s = CLAMP(-c / a, 0.0, 1.0); // t = 0 => s = (b*t - c) / a = -c / a
|
||||
} else {
|
||||
// The general nondegenerate case starts here.
|
||||
real_t b = d1.dot(d2);
|
||||
real_t denom = a * e - b * b; // Always nonnegative.
|
||||
// If segments not parallel, compute closest point on L1 to L2 and
|
||||
// clamp to segment S1. Else pick arbitrary s (here 0).
|
||||
if (denom != 0.0) {
|
||||
s = CLAMP((b * f - c * e) / denom, 0.0, 1.0);
|
||||
} else {
|
||||
s = 0.0;
|
||||
}
|
||||
// Compute point on L2 closest to S1(s) using
|
||||
// t = Dot((P1 + D1*s) - P2,D2) / Dot(D2,D2) = (b*s + f) / e
|
||||
t = (b * s + f) / e;
|
||||
|
||||
//If t in [0,1] done. Else clamp t, recompute s for the new value
|
||||
// of t using s = Dot((P2 + D2*t) - P1,D1) / Dot(D1,D1)= (t*b - c) / a
|
||||
// and clamp s to [0, 1].
|
||||
if (t < 0.0) {
|
||||
t = 0.0;
|
||||
s = CLAMP(-c / a, 0.0, 1.0);
|
||||
} else if (t > 1.0) {
|
||||
t = 1.0;
|
||||
s = CLAMP((b - c) / a, 0.0, 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
c1 = p1 + d1 * s;
|
||||
c2 = p2 + d2 * t;
|
||||
return Math::sqrt((c1 - c2).dot(c1 - c2));
|
||||
}
|
||||
|
||||
static Vector2 get_closest_point_to_segment(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();
|
||||
if (l2 < 1e-20) {
|
||||
return p_segment[0]; // Both points are the same, just give any.
|
||||
}
|
||||
|
||||
real_t d = n.dot(p) / l2;
|
||||
|
||||
if (d <= 0.0) {
|
||||
return p_segment[0]; // Before first point.
|
||||
} else if (d >= 1.0) {
|
||||
return p_segment[1]; // After first point.
|
||||
} else {
|
||||
return p_segment[0] + n * d; // Inside.
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_point_in_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) {
|
||||
Vector2 an = a - s;
|
||||
Vector2 bn = b - s;
|
||||
Vector2 cn = c - s;
|
||||
|
||||
bool orientation = an.cross(bn) > 0;
|
||||
|
||||
if ((bn.cross(cn) > 0) != orientation) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (cn.cross(an) > 0) == orientation;
|
||||
}
|
||||
|
||||
static Vector2 get_closest_point_to_segment_uncapped(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();
|
||||
if (l2 < 1e-20) {
|
||||
return p_segment[0]; // Both points are the same, just give any.
|
||||
}
|
||||
|
||||
real_t d = n.dot(p) / l2;
|
||||
|
||||
return p_segment[0] + n * d; // Inside.
|
||||
}
|
||||
|
||||
static bool line_intersects_line(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;
|
||||
if (Math::is_zero_approx(denom)) { // Parallel?
|
||||
return false;
|
||||
}
|
||||
|
||||
const Vector2 v = p_from_a - p_from_b;
|
||||
const real_t t = (p_dir_b.x * v.y - p_dir_b.y * v.x) / denom;
|
||||
r_result = p_from_a + t * p_dir_a;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool segment_intersects_segment(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;
|
||||
|
||||
real_t ABlen = B.dot(B);
|
||||
if (ABlen <= 0) {
|
||||
return false;
|
||||
}
|
||||
Vector2 Bn = B / ABlen;
|
||||
C = Vector2(C.x * Bn.x + C.y * Bn.y, C.y * Bn.x - C.x * Bn.y);
|
||||
D = Vector2(D.x * Bn.x + D.y * Bn.y, D.y * Bn.x - D.x * Bn.y);
|
||||
|
||||
if ((C.y < 0 && D.y < 0) || (C.y >= 0 && D.y >= 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
real_t ABpos = D.x + (C.x - D.x) * D.y / (D.y - C.y);
|
||||
|
||||
// Fail if segment C-D crosses line A-B outside of segment A-B.
|
||||
if (ABpos < 0 || ABpos > 1.0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// (4) Apply the discovered position to line A-B in the original coordinate system.
|
||||
if (r_result) {
|
||||
*r_result = p_from_a + B * ABpos;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Create a quadratic formula of the form ax^2 + bx + c = 0
|
||||
real_t a, b, c;
|
||||
|
||||
a = line_vec.dot(line_vec);
|
||||
b = 2 * vec_to_line.dot(line_vec);
|
||||
c = vec_to_line.dot(vec_to_line) - p_circle_radius * p_circle_radius;
|
||||
|
||||
// Solve for t.
|
||||
real_t sqrtterm = b * b - 4 * a * c;
|
||||
|
||||
// If the term we intend to square root is less than 0 then the answer won't be real,
|
||||
// so it definitely won't be t in the range 0 to 1.
|
||||
if (sqrtterm < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If we can assume that the line segment starts outside the circle (e.g. for continuous time collision detection)
|
||||
// then the following can be skipped and we can just return the equivalent of res1.
|
||||
sqrtterm = Math::sqrt(sqrtterm);
|
||||
real_t res1 = (-b - sqrtterm) / (2 * a);
|
||||
real_t res2 = (-b + sqrtterm) / (2 * a);
|
||||
|
||||
if (res1 >= 0 && res1 <= 1) {
|
||||
return res1;
|
||||
}
|
||||
if (res2 >= 0 && res2 <= 1) {
|
||||
return res2;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
enum PolyBooleanOperation {
|
||||
OPERATION_UNION,
|
||||
OPERATION_DIFFERENCE,
|
||||
OPERATION_INTERSECTION,
|
||||
OPERATION_XOR
|
||||
};
|
||||
enum PolyJoinType {
|
||||
JOIN_SQUARE,
|
||||
JOIN_ROUND,
|
||||
JOIN_MITER
|
||||
};
|
||||
enum PolyEndType {
|
||||
END_POLYGON,
|
||||
END_JOINED,
|
||||
END_BUTT,
|
||||
END_SQUARE,
|
||||
END_ROUND
|
||||
};
|
||||
|
||||
static Vector<Vector<Point2>> merge_polygons(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(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(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(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(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(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(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(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 instead).");
|
||||
|
||||
return _polypath_offset(p_polygon, p_delta, p_join_type, p_end_type);
|
||||
}
|
||||
|
||||
static Vector<int> triangulate_delaunay(const Vector<Vector2> &p_points) {
|
||||
Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(p_points);
|
||||
Vector<int> triangles;
|
||||
|
||||
for (int i = 0; i < tr.size(); i++) {
|
||||
triangles.push_back(tr[i].points[0]);
|
||||
triangles.push_back(tr[i].points[1]);
|
||||
triangles.push_back(tr[i].points[2]);
|
||||
}
|
||||
return triangles;
|
||||
}
|
||||
|
||||
static Vector<int> triangulate_polygon(const Vector<Vector2> &p_polygon) {
|
||||
Vector<int> triangles;
|
||||
if (!Triangulate::triangulate(p_polygon, triangles)) {
|
||||
return Vector<int>(); //fail
|
||||
}
|
||||
return triangles;
|
||||
}
|
||||
|
||||
static bool is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
|
||||
int c = p_polygon.size();
|
||||
if (c < 3) {
|
||||
return false;
|
||||
}
|
||||
const Vector2 *p = p_polygon.ptr();
|
||||
real_t sum = 0;
|
||||
for (int i = 0; i < c; i++) {
|
||||
const Vector2 &v1 = p[i];
|
||||
const Vector2 &v2 = p[(i + 1) % c];
|
||||
sum += (v2.x - v1.x) * (v2.y + v1.y);
|
||||
}
|
||||
|
||||
return sum > 0.0f;
|
||||
}
|
||||
|
||||
// Alternate implementation that should be faster.
|
||||
static bool is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon) {
|
||||
int c = p_polygon.size();
|
||||
if (c < 3) {
|
||||
return false;
|
||||
}
|
||||
const Vector2 *p = p_polygon.ptr();
|
||||
Vector2 further_away(-1e20, -1e20);
|
||||
Vector2 further_away_opposite(1e20, 1e20);
|
||||
|
||||
for (int i = 0; i < c; i++) {
|
||||
further_away.x = MAX(p[i].x, further_away.x);
|
||||
further_away.y = MAX(p[i].y, further_away.y);
|
||||
further_away_opposite.x = MIN(p[i].x, further_away_opposite.x);
|
||||
further_away_opposite.y = MIN(p[i].y, further_away_opposite.y);
|
||||
}
|
||||
|
||||
// Make point outside that won't intersect with points in segment from p_point.
|
||||
further_away += (further_away - further_away_opposite) * Vector2(1.221313, 1.512312);
|
||||
|
||||
int intersections = 0;
|
||||
for (int i = 0; i < c; i++) {
|
||||
const Vector2 &v1 = p[i];
|
||||
const Vector2 &v2 = p[(i + 1) % c];
|
||||
if (segment_intersects_segment(v1, v2, p_point, further_away, nullptr)) {
|
||||
intersections++;
|
||||
}
|
||||
}
|
||||
|
||||
return (intersections & 1);
|
||||
}
|
||||
|
||||
static real_t vec2_cross(const Point2 &O, const Point2 &A, const Point2 &B) {
|
||||
return (real_t)(A.x - O.x) * (B.y - O.y) - (real_t)(A.y - O.y) * (B.x - O.x);
|
||||
}
|
||||
|
||||
// Returns a list of points on the convex hull in counter-clockwise order.
|
||||
// Note: the last point in the returned list is the same as the first one.
|
||||
static Vector<Point2> convex_hull(Vector<Point2> P) {
|
||||
int n = P.size(), k = 0;
|
||||
Vector<Point2> H;
|
||||
H.resize(2 * n);
|
||||
|
||||
// Sort points lexicographically.
|
||||
P.sort();
|
||||
|
||||
// Build lower hull.
|
||||
for (int i = 0; i < n; ++i) {
|
||||
while (k >= 2 && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0) {
|
||||
k--;
|
||||
}
|
||||
H.write[k++] = P[i];
|
||||
}
|
||||
|
||||
// Build upper hull.
|
||||
for (int i = n - 2, t = k + 1; i >= 0; i--) {
|
||||
while (k >= t && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0) {
|
||||
k--;
|
||||
}
|
||||
H.write[k++] = P[i];
|
||||
}
|
||||
|
||||
H.resize(k);
|
||||
return H;
|
||||
}
|
||||
static Vector<Vector<Vector2>> decompose_polygon_in_convex(Vector<Point2> polygon);
|
||||
|
||||
static void make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size);
|
||||
static Vector<Point2i> pack_rects(const Vector<Size2i> &p_sizes, const Size2i &p_atlas_size);
|
||||
static Vector<Vector3i> partial_pack_rects(const Vector<Vector2i> &p_sizes, const Size2i &p_atlas_size);
|
||||
|
||||
private:
|
||||
static Vector<Vector<Point2>> _polypaths_do_operation(PolyBooleanOperation p_op, const Vector<Point2> &p_polypath_a, const Vector<Point2> &p_polypath_b, bool is_a_open = false);
|
||||
static Vector<Vector<Point2>> _polypath_offset(const Vector<Point2> &p_polypath, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type);
|
||||
};
|
||||
|
||||
#endif // GEOMETRY_2D_H
|
||||
@ -0,0 +1,194 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="Geometry3D" inherits="Object" version="4.0">
|
||||
<brief_description>
|
||||
Helper node to calculate generic geometry operations in 3D space.
|
||||
</brief_description>
|
||||
<description>
|
||||
Geometry3D provides users with a set of helper functions to create geometric shapes, compute intersections between shapes, and process various other geometric operations.
|
||||
</description>
|
||||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="build_box_planes">
|
||||
<return type="Array">
|
||||
</return>
|
||||
<argument index="0" name="extents" type="Vector3">
|
||||
</argument>
|
||||
<description>
|
||||
Returns an array with 6 [Plane]s that describe the sides of a box centered at the origin. The box size is defined by [code]extents[/code], which represents one (positive) corner of the box (i.e. half its actual size).
|
||||
</description>
|
||||
</method>
|
||||
<method name="build_capsule_planes">
|
||||
<return type="Array">
|
||||
</return>
|
||||
<argument index="0" name="radius" type="float">
|
||||
</argument>
|
||||
<argument index="1" name="height" type="float">
|
||||
</argument>
|
||||
<argument index="2" name="sides" type="int">
|
||||
</argument>
|
||||
<argument index="3" name="lats" type="int">
|
||||
</argument>
|
||||
<argument index="4" name="axis" type="int" enum="Vector3.Axis" default="2">
|
||||
</argument>
|
||||
<description>
|
||||
Returns an array of [Plane]s closely bounding a faceted capsule centered at the origin with radius [code]radius[/code] and height [code]height[/code]. The parameter [code]sides[/code] defines how many planes will be generated for the side part of the capsule, whereas [code]lats[/code] gives the number of latitudinal steps at the bottom and top of the capsule. The parameter [code]axis[/code] describes the axis along which the capsule is oriented (0 for X, 1 for Y, 2 for Z).
|
||||
</description>
|
||||
</method>
|
||||
<method name="build_cylinder_planes">
|
||||
<return type="Array">
|
||||
</return>
|
||||
<argument index="0" name="radius" type="float">
|
||||
</argument>
|
||||
<argument index="1" name="height" type="float">
|
||||
</argument>
|
||||
<argument index="2" name="sides" type="int">
|
||||
</argument>
|
||||
<argument index="3" name="axis" type="int" enum="Vector3.Axis" default="2">
|
||||
</argument>
|
||||
<description>
|
||||
Returns an array of [Plane]s closely bounding a faceted cylinder centered at the origin with radius [code]radius[/code] and height [code]height[/code]. The parameter [code]sides[/code] defines how many planes will be generated for the round part of the cylinder. The parameter [code]axis[/code] describes the axis along which the cylinder is oriented (0 for X, 1 for Y, 2 for Z).
|
||||
</description>
|
||||
</method>
|
||||
<method name="clip_polygon">
|
||||
<return type="PackedVector3Array">
|
||||
</return>
|
||||
<argument index="0" name="points" type="PackedVector3Array">
|
||||
</argument>
|
||||
<argument index="1" name="plane" type="Plane">
|
||||
</argument>
|
||||
<description>
|
||||
Clips the polygon defined by the points in [code]points[/code] against the [code]plane[/code] and returns the points of the clipped polygon.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_closest_point_to_segment">
|
||||
<return type="Vector3">
|
||||
</return>
|
||||
<argument index="0" name="point" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="s1" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="s2" type="Vector3">
|
||||
</argument>
|
||||
<description>
|
||||
Returns the 3D point on the 3D segment ([code]s1[/code], [code]s2[/code]) that is closest to [code]point[/code]. The returned point will always be inside the specified segment.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_closest_point_to_segment_uncapped">
|
||||
<return type="Vector3">
|
||||
</return>
|
||||
<argument index="0" name="point" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="s1" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="s2" type="Vector3">
|
||||
</argument>
|
||||
<description>
|
||||
Returns the 3D point on the 3D line defined by ([code]s1[/code], [code]s2[/code]) that is closest to [code]point[/code]. The returned point can be inside the segment ([code]s1[/code], [code]s2[/code]) or outside of it, i.e. somewhere on the line extending from the segment.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_closest_points_between_segments">
|
||||
<return type="PackedVector3Array">
|
||||
</return>
|
||||
<argument index="0" name="p1" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="p2" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="q1" type="Vector3">
|
||||
</argument>
|
||||
<argument index="3" name="q2" type="Vector3">
|
||||
</argument>
|
||||
<description>
|
||||
Given the two 3D segments ([code]p1[/code], [code]p2[/code]) and ([code]q1[/code], [code]q2[/code]), finds those two points on the two segments that are closest to each other. Returns a [PackedVector3Array] that contains this point on ([code]p1[/code], [code]p2[/code]) as well the accompanying point on ([code]q1[/code], [code]q2[/code]).
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_uv84_normal_bit">
|
||||
<return type="int">
|
||||
</return>
|
||||
<argument index="0" name="normal" type="Vector3">
|
||||
</argument>
|
||||
<description>
|
||||
Used internally by the engine.
|
||||
</description>
|
||||
</method>
|
||||
<method name="ray_intersects_triangle">
|
||||
<return type="Variant">
|
||||
</return>
|
||||
<argument index="0" name="from" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="dir" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="a" type="Vector3">
|
||||
</argument>
|
||||
<argument index="3" name="b" type="Vector3">
|
||||
</argument>
|
||||
<argument index="4" name="c" type="Vector3">
|
||||
</argument>
|
||||
<description>
|
||||
Tests if the 3D ray starting at [code]from[/code] with the direction of [code]dir[/code] intersects the triangle specified by [code]a[/code], [code]b[/code] and [code]c[/code]. If yes, returns the point of intersection as [Vector3]. If no intersection takes place, an empty [Variant] is returned.
|
||||
</description>
|
||||
</method>
|
||||
<method name="segment_intersects_convex">
|
||||
<return type="PackedVector3Array">
|
||||
</return>
|
||||
<argument index="0" name="from" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="to" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="planes" type="Array">
|
||||
</argument>
|
||||
<description>
|
||||
Given a convex hull defined though the [Plane]s in the array [code]planes[/code], tests if the segment ([code]from[/code], [code]to[/code]) intersects with that hull. If an intersection is found, returns a [PackedVector3Array] containing the point the intersection and the hull's normal. If no intersecion is found, an the returned array is empty.
|
||||
</description>
|
||||
</method>
|
||||
<method name="segment_intersects_cylinder">
|
||||
<return type="PackedVector3Array">
|
||||
</return>
|
||||
<argument index="0" name="from" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="to" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="height" type="float">
|
||||
</argument>
|
||||
<argument index="3" name="radius" type="float">
|
||||
</argument>
|
||||
<description>
|
||||
Checks if the segment ([code]from[/code], [code]to[/code]) intersects the cylinder with height [code]height[/code] that is centered at the origin and has radius [code]radius[/code]. If no, returns an empty [PackedVector3Array]. If an intersection takes place, the returned array contains the point of intersection and the cylinder's normal at the point of intersection.
|
||||
</description>
|
||||
</method>
|
||||
<method name="segment_intersects_sphere">
|
||||
<return type="PackedVector3Array">
|
||||
</return>
|
||||
<argument index="0" name="from" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="to" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="sphere_position" type="Vector3">
|
||||
</argument>
|
||||
<argument index="3" name="sphere_radius" type="float">
|
||||
</argument>
|
||||
<description>
|
||||
Checks if the segment ([code]from[/code], [code]to[/code]) intersects the sphere that is located at [code]sphere_position[/code] and has radius [code]sphere_radius[/code]. If no, returns an empty [PackedVector3Array]. If yes, returns a [PackedVector3Array] containing the point of intersection and the sphere's normal at the point of intersection.
|
||||
</description>
|
||||
</method>
|
||||
<method name="segment_intersects_triangle">
|
||||
<return type="Variant">
|
||||
</return>
|
||||
<argument index="0" name="from" type="Vector3">
|
||||
</argument>
|
||||
<argument index="1" name="to" type="Vector3">
|
||||
</argument>
|
||||
<argument index="2" name="a" type="Vector3">
|
||||
</argument>
|
||||
<argument index="3" name="b" type="Vector3">
|
||||
</argument>
|
||||
<argument index="4" name="c" type="Vector3">
|
||||
</argument>
|
||||
<description>
|
||||
Tests if the segment ([code]from[/code], [code]to[/code]) intersects the triangle [code]a[/code], [code]b[/code], [code]c[/code]. If yes, returns the point of intersection as [Vector3]. If no intersection takes place, an empty [Variant] is returned.
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
<constants>
|
||||
</constants>
|
||||
</class>
|
||||
Loading…
Reference in New Issue