| 1 | /**************************************************************************/ |
| 2 | /* geometry_2d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "geometry_2d.h" |
| 32 | |
| 33 | GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Walloc-zero") |
| 34 | #include "thirdparty/clipper2/include/clipper2/clipper.h" |
| 35 | GODOT_GCC_WARNING_POP |
| 36 | #include "thirdparty/misc/polypartition.h" |
| 37 | #define STB_RECT_PACK_IMPLEMENTATION |
| 38 | #include "thirdparty/misc/stb_rect_pack.h" |
| 39 | |
| 40 | const int clipper_precision = 5; // Based on CMP_EPSILON. |
| 41 | |
| 42 | void Geometry2D::merge_many_polygons(const Vector<Vector<Vector2>> &p_polygons, Vector<Vector<Vector2>> &r_out_polygons, Vector<Vector<Vector2>> &r_out_holes) { |
| 43 | using namespace Clipper2Lib; |
| 44 | |
| 45 | PathsD subjects; |
| 46 | for (const Vector<Vector2> &polygon : p_polygons) { |
| 47 | PathD path(polygon.size()); |
| 48 | for (int i = 0; i < polygon.size(); i++) { |
| 49 | const Vector2 &point = polygon[i]; |
| 50 | path[i] = PointD(point.x, point.y); |
| 51 | } |
| 52 | subjects.push_back(path); |
| 53 | } |
| 54 | |
| 55 | PathsD solution = Union(subjects, FillRule::NonZero); |
| 56 | solution = SimplifyPaths(solution, 0.01); |
| 57 | |
| 58 | r_out_polygons.clear(); |
| 59 | r_out_holes.clear(); |
| 60 | for (PathsD::size_type i = 0; i < solution.size(); ++i) { |
| 61 | PathD &path = solution[i]; |
| 62 | |
| 63 | Vector<Point2> output_polygon; |
| 64 | output_polygon.resize(path.size()); |
| 65 | for (PathsD::size_type j = 0; j < path.size(); ++j) { |
| 66 | output_polygon.set(j, Vector2(static_cast<real_t>(path[j].x), static_cast<real_t>(path[j].y))); |
| 67 | } |
| 68 | if (IsPositive(path)) { |
| 69 | r_out_polygons.push_back(output_polygon); |
| 70 | } else { |
| 71 | r_out_holes.push_back(output_polygon); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | Vector<Vector<Vector2>> Geometry2D::decompose_many_polygons_in_convex(const Vector<Vector<Point2>> &p_polygons, const Vector<Vector<Point2>> &p_holes) { |
| 77 | Vector<Vector<Vector2>> decomp; |
| 78 | List<TPPLPoly> in_poly, out_poly; |
| 79 | |
| 80 | for (const Vector<Vector2> &polygon : p_polygons) { |
| 81 | TPPLPoly inp; |
| 82 | inp.Init(polygon.size()); |
| 83 | for (int i = 0; i < polygon.size(); i++) { |
| 84 | inp.GetPoint(i) = polygon[i]; |
| 85 | } |
| 86 | inp.SetOrientation(TPPL_ORIENTATION_CCW); |
| 87 | in_poly.push_back(inp); |
| 88 | } |
| 89 | for (const Vector<Vector2> &polygon : p_holes) { |
| 90 | TPPLPoly inp; |
| 91 | inp.Init(polygon.size()); |
| 92 | for (int i = 0; i < polygon.size(); i++) { |
| 93 | inp.GetPoint(i) = polygon[i]; |
| 94 | } |
| 95 | inp.SetOrientation(TPPL_ORIENTATION_CW); |
| 96 | inp.SetHole(true); |
| 97 | in_poly.push_back(inp); |
| 98 | } |
| 99 | TPPLPartition tpart; |
| 100 | if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { // Failed. |
| 101 | ERR_PRINT("Convex decomposing failed!"); |
| 102 | return decomp; |
| 103 | } |
| 104 | |
| 105 | decomp.resize(out_poly.size()); |
| 106 | int idx = 0; |
| 107 | for (TPPLPoly &tp : out_poly) { |
| 108 | decomp.write[idx].resize(tp.GetNumPoints()); |
| 109 | |
| 110 | for (int64_t i = 0; i < tp.GetNumPoints(); i++) { |
| 111 | decomp.write[idx].write[i] = tp.GetPoint(i); |
| 112 | } |
| 113 | |
| 114 | idx++; |
| 115 | } |
| 116 | |
| 117 | return decomp; |
| 118 | } |
| 119 | |
| 120 | Vector<Vector<Vector2>> Geometry2D::decompose_polygon_in_convex(const Vector<Point2> &p_polygon) { |
| 121 | return Geometry2D::decompose_many_polygons_in_convex({ p_polygon }, {}); |
| 122 | } |
| 123 | |
| 124 | struct _AtlasWorkRect { |
| 125 | Size2i s; |
| 126 | Point2i p; |
| 127 | int idx = 0; |
| 128 | _FORCE_INLINE_ bool operator<(const _AtlasWorkRect &p_r) const { return s.width > p_r.s.width; } |
| 129 | }; |
| 130 | |
| 131 | struct _AtlasWorkRectResult { |
| 132 | Vector<_AtlasWorkRect> result; |
| 133 | int max_w = 0; |
| 134 | int max_h = 0; |
| 135 | }; |
| 136 | |
| 137 | void Geometry2D::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size) { |
| 138 | // Super simple, almost brute force scanline stacking fitter. |
| 139 | // It's pretty basic for now, but it tries to make sure that the aspect ratio of the |
| 140 | // resulting atlas is somehow square. This is necessary because video cards have limits |
| 141 | // on texture size (usually 2048 or 4096), so the squarer a texture, the more the chances |
| 142 | // that it will work in every hardware. |
| 143 | // For example, it will prioritize a 1024x1024 atlas (works everywhere) instead of a |
| 144 | // 256x8192 atlas (won't work anywhere). |
| 145 | |
| 146 | ERR_FAIL_COND(p_rects.is_empty()); |
| 147 | for (int i = 0; i < p_rects.size(); i++) { |
| 148 | ERR_FAIL_COND(p_rects[i].width <= 0); |
| 149 | ERR_FAIL_COND(p_rects[i].height <= 0); |
| 150 | } |
| 151 | |
| 152 | Vector<_AtlasWorkRect> wrects; |
| 153 | wrects.resize(p_rects.size()); |
| 154 | for (int i = 0; i < p_rects.size(); i++) { |
| 155 | wrects.write[i].s = p_rects[i]; |
| 156 | wrects.write[i].idx = i; |
| 157 | } |
| 158 | wrects.sort(); |
| 159 | int widest = wrects[0].s.width; |
| 160 | |
| 161 | Vector<_AtlasWorkRectResult> results; |
| 162 | |
| 163 | for (int i = 0; i <= 12; i++) { |
| 164 | int w = 1 << i; |
| 165 | int max_h = 0; |
| 166 | int max_w = 0; |
| 167 | if (w < widest) { |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | Vector<int> hmax; |
| 172 | hmax.resize(w); |
| 173 | for (int j = 0; j < w; j++) { |
| 174 | hmax.write[j] = 0; |
| 175 | } |
| 176 | |
| 177 | // Place them. |
| 178 | int ofs = 0; |
| 179 | int limit_h = 0; |
| 180 | for (int j = 0; j < wrects.size(); j++) { |
| 181 | if (ofs + wrects[j].s.width > w) { |
| 182 | ofs = 0; |
| 183 | } |
| 184 | |
| 185 | int from_y = 0; |
| 186 | for (int k = 0; k < wrects[j].s.width; k++) { |
| 187 | if (hmax[ofs + k] > from_y) { |
| 188 | from_y = hmax[ofs + k]; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | wrects.write[j].p.x = ofs; |
| 193 | wrects.write[j].p.y = from_y; |
| 194 | int end_h = from_y + wrects[j].s.height; |
| 195 | int end_w = ofs + wrects[j].s.width; |
| 196 | if (ofs == 0) { |
| 197 | limit_h = end_h; |
| 198 | } |
| 199 | |
| 200 | for (int k = 0; k < wrects[j].s.width; k++) { |
| 201 | hmax.write[ofs + k] = end_h; |
| 202 | } |
| 203 | |
| 204 | if (end_h > max_h) { |
| 205 | max_h = end_h; |
| 206 | } |
| 207 | |
| 208 | if (end_w > max_w) { |
| 209 | max_w = end_w; |
| 210 | } |
| 211 | |
| 212 | if (ofs == 0 || end_h > limit_h) { // While h limit not reached, keep stacking. |
| 213 | ofs += wrects[j].s.width; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | _AtlasWorkRectResult result; |
| 218 | result.result = wrects; |
| 219 | result.max_h = max_h; |
| 220 | result.max_w = max_w; |
| 221 | results.push_back(result); |
| 222 | } |
| 223 | |
| 224 | // Find the result with the best aspect ratio. |
| 225 | |
| 226 | int best = -1; |
| 227 | real_t best_aspect = 1e20; |
| 228 | |
| 229 | for (int i = 0; i < results.size(); i++) { |
| 230 | real_t h = next_power_of_2((uint32_t)results[i].max_h); |
| 231 | real_t w = next_power_of_2((uint32_t)results[i].max_w); |
| 232 | real_t aspect = h > w ? h / w : w / h; |
| 233 | if (aspect < best_aspect) { |
| 234 | best = i; |
| 235 | best_aspect = aspect; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | r_result.resize(p_rects.size()); |
| 240 | |
| 241 | for (int i = 0; i < p_rects.size(); i++) { |
| 242 | r_result.write[results[best].result[i].idx] = results[best].result[i].p; |
| 243 | } |
| 244 | |
| 245 | r_size = Size2(results[best].max_w, results[best].max_h); |
| 246 | } |
| 247 | |
| 248 | 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) { |
| 249 | using namespace Clipper2Lib; |
| 250 | |
| 251 | ClipType op = ClipType::Union; |
| 252 | |
| 253 | switch (p_op) { |
| 254 | case OPERATION_UNION: |
| 255 | op = ClipType::Union; |
| 256 | break; |
| 257 | case OPERATION_DIFFERENCE: |
| 258 | op = ClipType::Difference; |
| 259 | break; |
| 260 | case OPERATION_INTERSECTION: |
| 261 | op = ClipType::Intersection; |
| 262 | break; |
| 263 | case OPERATION_XOR: |
| 264 | op = ClipType::Xor; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | PathD path_a(p_polypath_a.size()); |
| 269 | for (int i = 0; i != p_polypath_a.size(); ++i) { |
| 270 | path_a[i] = PointD(p_polypath_a[i].x, p_polypath_a[i].y); |
| 271 | } |
| 272 | PathD path_b(p_polypath_b.size()); |
| 273 | for (int i = 0; i != p_polypath_b.size(); ++i) { |
| 274 | path_b[i] = PointD(p_polypath_b[i].x, p_polypath_b[i].y); |
| 275 | } |
| 276 | |
| 277 | ClipperD clp(clipper_precision); // Scale points up internally to attain the desired precision. |
| 278 | clp.PreserveCollinear(false); // Remove redundant vertices. |
| 279 | if (is_a_open) { |
| 280 | clp.AddOpenSubject({ path_a }); |
| 281 | } else { |
| 282 | clp.AddSubject({ path_a }); |
| 283 | } |
| 284 | clp.AddClip({ path_b }); |
| 285 | |
| 286 | PathsD paths; |
| 287 | |
| 288 | if (is_a_open) { |
| 289 | PolyTreeD tree; // Needed to populate polylines. |
| 290 | clp.Execute(op, FillRule::EvenOdd, tree, paths); |
| 291 | } else { |
| 292 | clp.Execute(op, FillRule::EvenOdd, paths); // Works on closed polygons only. |
| 293 | } |
| 294 | |
| 295 | Vector<Vector<Point2>> polypaths; |
| 296 | polypaths.resize(paths.size()); |
| 297 | for (PathsD::size_type i = 0; i < paths.size(); i++) { |
| 298 | const PathD &path = paths[i]; |
| 299 | |
| 300 | Vector<Vector2> polypath; |
| 301 | polypath.resize(path.size()); |
| 302 | for (PathsD::size_type j = 0; j < path.size(); ++j) { |
| 303 | polypath.set(j, Point2(static_cast<real_t>(path[j].x), static_cast<real_t>(path[j].y))); |
| 304 | } |
| 305 | polypaths.set(i, polypath); |
| 306 | } |
| 307 | return polypaths; |
| 308 | } |
| 309 | |
| 310 | Vector<Vector<Point2>> Geometry2D::_polypath_offset(const Vector<Point2> &p_polypath, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) { |
| 311 | using namespace Clipper2Lib; |
| 312 | |
| 313 | JoinType jt = JoinType::Square; |
| 314 | |
| 315 | switch (p_join_type) { |
| 316 | case JOIN_SQUARE: |
| 317 | jt = JoinType::Square; |
| 318 | break; |
| 319 | case JOIN_ROUND: |
| 320 | jt = JoinType::Round; |
| 321 | break; |
| 322 | case JOIN_MITER: |
| 323 | jt = JoinType::Miter; |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | EndType et = EndType::Polygon; |
| 328 | |
| 329 | switch (p_end_type) { |
| 330 | case END_POLYGON: |
| 331 | et = EndType::Polygon; |
| 332 | break; |
| 333 | case END_JOINED: |
| 334 | et = EndType::Joined; |
| 335 | break; |
| 336 | case END_BUTT: |
| 337 | et = EndType::Butt; |
| 338 | break; |
| 339 | case END_SQUARE: |
| 340 | et = EndType::Square; |
| 341 | break; |
| 342 | case END_ROUND: |
| 343 | et = EndType::Round; |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | PathD polypath(p_polypath.size()); |
| 348 | for (int i = 0; i != p_polypath.size(); ++i) { |
| 349 | polypath[i] = PointD(p_polypath[i].x, p_polypath[i].y); |
| 350 | } |
| 351 | |
| 352 | // Inflate/deflate. |
| 353 | PathsD paths = InflatePaths({ polypath }, p_delta, jt, et, 2.0, clipper_precision, 0.25); |
| 354 | |
| 355 | Vector<Vector<Point2>> polypaths; |
| 356 | polypaths.resize(paths.size()); |
| 357 | for (PathsD::size_type i = 0; i < paths.size(); ++i) { |
| 358 | const PathD &path = paths[i]; |
| 359 | |
| 360 | Vector<Vector2> polypath2; |
| 361 | polypath2.resize(path.size()); |
| 362 | for (PathsD::size_type j = 0; j < path.size(); ++j) { |
| 363 | polypath2.set(j, Point2(static_cast<real_t>(path[j].x), static_cast<real_t>(path[j].y))); |
| 364 | } |
| 365 | polypaths.set(i, polypath2); |
| 366 | } |
| 367 | return polypaths; |
| 368 | } |
| 369 | |
| 370 | Vector<Vector3i> Geometry2D::partial_pack_rects(const Vector<Vector2i> &p_sizes, const Size2i &p_atlas_size) { |
| 371 | Vector<stbrp_node> nodes; |
| 372 | nodes.resize(p_atlas_size.width); |
| 373 | memset(nodes.ptrw(), 0, sizeof(stbrp_node) * nodes.size()); |
| 374 | |
| 375 | stbrp_context context; |
| 376 | stbrp_init_target(&context, p_atlas_size.width, p_atlas_size.height, nodes.ptrw(), p_atlas_size.width); |
| 377 | |
| 378 | Vector<stbrp_rect> rects; |
| 379 | rects.resize(p_sizes.size()); |
| 380 | |
| 381 | for (int i = 0; i < p_sizes.size(); i++) { |
| 382 | rects.write[i].id = i; |
| 383 | rects.write[i].w = p_sizes[i].width; |
| 384 | rects.write[i].h = p_sizes[i].height; |
| 385 | rects.write[i].x = 0; |
| 386 | rects.write[i].y = 0; |
| 387 | rects.write[i].was_packed = 0; |
| 388 | } |
| 389 | |
| 390 | stbrp_pack_rects(&context, rects.ptrw(), rects.size()); |
| 391 | |
| 392 | Vector<Vector3i> ret; |
| 393 | ret.resize(p_sizes.size()); |
| 394 | |
| 395 | for (int i = 0; i < p_sizes.size(); i++) { |
| 396 | ret.write[rects[i].id] = Vector3i(rects[i].x, rects[i].y, rects[i].was_packed != 0 ? 1 : 0); |
| 397 | } |
| 398 | |
| 399 | return ret; |
| 400 | } |