Using a proper polygon triangulation library instead of delaunator for lasso tool
This commit is contained in:
parent
a4ba969e11
commit
d07eaf4207
5 changed files with 879 additions and 580 deletions
|
|
@ -6,7 +6,27 @@
|
|||
#include "Helpers/SCollision.hpp"
|
||||
#include "../CoordSpaceHelper.hpp"
|
||||
#include <ranges>
|
||||
#include <delaunator.hpp>
|
||||
#include "../DrawCollision.hpp"
|
||||
#include <earcut.hpp>
|
||||
|
||||
namespace mapbox {
|
||||
namespace util {
|
||||
|
||||
template <>
|
||||
struct nth<0, Vector2f> {
|
||||
inline static auto get(const Vector2f &t) {
|
||||
return t.x();
|
||||
};
|
||||
};
|
||||
template <>
|
||||
struct nth<1, Vector2f> {
|
||||
inline static auto get(const Vector2f &t) {
|
||||
return t.y();
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
} // namespace mapbox
|
||||
|
||||
LassoSelectTool::LassoSelectTool(DrawingProgram& initDrawP):
|
||||
drawP(initDrawP)
|
||||
|
|
@ -19,6 +39,7 @@ void LassoSelectTool::gui_toolbox() {
|
|||
|
||||
void LassoSelectTool::reset_tool() {
|
||||
drawP.selection.deselect_all();
|
||||
controls.lassoPoints.clear();
|
||||
controls = LassoSelectControls();
|
||||
}
|
||||
|
||||
|
|
@ -42,27 +63,36 @@ void LassoSelectTool::tool_update() {
|
|||
case 1: {
|
||||
using namespace SCollision;
|
||||
Vector2f newLassoPoint = controls.coords.get_mouse_pos(drawP.world);
|
||||
if(vec_distance(controls.lassoPoints.back(), newLassoPoint)) {
|
||||
float lassoPointDist = vec_distance(controls.lassoPoints.back(), newLassoPoint);
|
||||
if(lassoPointDist > 4.0f)
|
||||
controls.lassoPoints.emplace_back(newLassoPoint);
|
||||
}
|
||||
|
||||
if(!drawP.controls.leftClickHeld) {
|
||||
std::vector<double> dCoords;
|
||||
for(Vector2f& lassoPoint : controls.lassoPoints) {
|
||||
dCoords.emplace_back(lassoPoint.x());
|
||||
dCoords.emplace_back(lassoPoint.y());
|
||||
if(controls.lassoPoints.size() > 3) {
|
||||
SCollision::ColliderCollection<float> cC;
|
||||
|
||||
std::vector<std::vector<Vector2f>> polygon;
|
||||
polygon.emplace_back(controls.lassoPoints);
|
||||
auto& poly = polygon[0];
|
||||
|
||||
std::vector<unsigned> indices = mapbox::earcut(polygon);
|
||||
|
||||
for(size_t i = 0; i < indices.size(); i += 3) {
|
||||
cC.triangle.emplace_back(
|
||||
poly[indices[i]],
|
||||
poly[indices[i + 1]],
|
||||
poly[indices[i + 2]]
|
||||
);
|
||||
}
|
||||
|
||||
cC.recalculate_bounds();
|
||||
|
||||
drawP.selection.add_from_cam_coord_collider_to_selection(cC);
|
||||
|
||||
controls.selectionMode = 0;
|
||||
}
|
||||
delaunator::Delaunator d(dCoords);
|
||||
ColliderCollection<float> cC;
|
||||
for(size_t i = 0; i < d.triangles.size(); i += 3) {
|
||||
SCollision::Triangle tri{
|
||||
Vector2f{dCoords[2 * d.triangles[i]], dCoords[2 * d.triangles[i] + 1]},
|
||||
Vector2f{dCoords[2 * d.triangles[i + 1]], dCoords[2 * d.triangles[i + 1] + 1]},
|
||||
Vector2f{dCoords[2 * d.triangles[i + 2]], dCoords[2 * d.triangles[i + 2] + 1]}
|
||||
};
|
||||
cC.triangle.emplace_back(tri);
|
||||
}
|
||||
drawP.selection.add_from_cam_coord_collider_to_selection(cC);
|
||||
controls.selectionMode = 0;
|
||||
else
|
||||
reset_tool();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ void RectSelectTool::tool_update() {
|
|||
ColliderCollection<float> cC;
|
||||
cC.triangle.emplace_back(controls.newT[0], controls.newT[1], controls.newT[2]);
|
||||
cC.triangle.emplace_back(controls.newT[2], controls.newT[3], controls.newT[0]);
|
||||
cC.recalculate_bounds();
|
||||
|
||||
drawP.selection.add_from_cam_coord_collider_to_selection(cC);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue