implementing better triangulation

This commit is contained in:
2017-10-11 10:16:54 +01:00
parent 163937b4a6
commit 32ede1be90
8 changed files with 185 additions and 54 deletions

View File

@@ -6,7 +6,6 @@
#include "shader.h"
#include "node_canvas.h"
#include "app.h"
#include <poly2tri.h>
NodeCanvas* CanvasMode::node;
ui::Canvas* CanvasMode::canvas;
@@ -375,6 +374,145 @@ char get_line_intersection(float p0_x, float p0_y, float p1_x, float p1_y,
return 0; // No collision
}
int SegmentCount = 0;
std::vector<ui::Shape::vertex_t> CanvasModeFill::triangulate(const std::vector<std::shared_ptr<p2t::Point>>& points)
{
struct Segment
{
std::shared_ptr<p2t::Point> a = nullptr;
std::shared_ptr<p2t::Point> b = nullptr;
Segment* prev = nullptr;
std::shared_ptr<Segment> next = nullptr;
bool end = false;
Segment()
{
SegmentCount++;
}
~Segment()
{
SegmentCount--;
}
};
std::shared_ptr<Segment> root = std::make_shared<Segment>();
std::shared_ptr<Segment> node = root;
for (int i = 0; i < points.size(); i++)
{
node->a = points[i];
if (i == points.size() - 1)
{
node->b = points[0];
node->next = root;
node->end = true;
root->prev = node.get();
}
else
{
node->b = points[i + 1];
node->next = std::make_shared<Segment>();
node->next->prev = node.get();
}
node = node->next;
}
node = root;
std::stack<std::shared_ptr<Segment>> todo;
std::vector<std::shared_ptr<Segment>> polys;
todo.push(root);
while (!todo.empty())
{
node = todo.top();
todo.pop();
polys.push_back(node);
while (node)
{
std::shared_ptr<Segment> other = node->next;
while (other)
{
if (*node->a == *other->a || *node->a == *other->b ||
*node->b == *other->a || *node->b == *other->b)
{
other = other->end ? nullptr : other->next;
continue;
}
glm::vec2 s0a(node->a->x, node->a->y);
glm::vec2 s0b(node->b->x, node->b->y);
glm::vec2 s1a(other->a->x, other->a->y);
glm::vec2 s1b(other->b->x, other->b->y);
glm::vec2 is;
if (segments_intersect(s0a, s0b, s1a, s1b, is))
{
auto p = std::make_shared<p2t::Point>(is.x, is.y);
auto poly_root = std::make_shared<Segment>();
poly_root->a = p;
poly_root->b = node->b;
poly_root->next = node->next;
todo.push(poly_root);
other->a = p;
node->b = p;
auto poly_end = std::make_shared<Segment>();
poly_end->a = other->prev->b;
poly_end->b = p;
poly_end->end = true;
poly_end->prev = other->prev;
other->prev->next = poly_end;
other->prev = node.get();
node->next = other;
break;
}
other = other->end ? nullptr : other->next;
}
node = node->end ? nullptr : node->next;
}
}
std::vector<ui::Shape::vertex_t> vertices;
for (auto poly : polys)
{
std::vector<p2t::Point*> outline;
node = poly;
while (node)
{
outline.push_back(new p2t::Point(*node->a));
auto current = node;
node = node->end ? nullptr : node->next;
current->next = nullptr;
}
LOG("poly %ld", outline.size());
if (outline.size() > 2)
{
p2t::CDT* cdt = new p2t::CDT(outline);
cdt->Triangulate();
auto tr = cdt->GetTriangles();
for (auto t : tr)
{
ui::Shape::vertex_t vertex;
for (int i = 0; i < 3; i++)
{
auto p = t->GetPoint(i);
glm::vec3 ro, rd, hit_o, hit_d;
glm::vec2 hit_fb;
int plane_id;
if (canvas->point_trace({p->x, p->y}, ro, rd, hit_o, hit_fb, hit_d, plane_id))
{
m_dirty_planes[plane_id]++;
vertex.pos = glm::vec4(hit_o, 1);
vertices.push_back(vertex);
}
}
}
delete cdt;
}
for (auto p : outline)
delete p;
}
return vertices;
}
void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
static glm::vec2 oldpos;
@@ -432,32 +570,14 @@ void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
m_dragging = false;
if (m_points2d.size() > 3)
{
std::vector<p2t::Point*> points;
std::vector<std::shared_ptr<p2t::Point>> points;
for (int i = 0; i < (int)m_points2d.size() - 1; i++)
points.push_back(new p2t::Point(m_points2d[i].x, m_points2d[i].y));
p2t::CDT cdt(points);
cdt.Triangulate();
auto triangles = cdt.GetTriangles();
std::vector<ui::Shape::vertex_t> v;
for (auto t : triangles)
{
ui::Shape::vertex_t vertex;
for (int i = 0; i < 3; i++)
{
auto p = t->GetPoint(i);
glm::vec3 ro, rd, hit_o, hit_d;
glm::vec2 hit_fb;
int plane_id;
if (canvas->point_trace({p->x, p->y}, ro, rd, hit_o, hit_fb, hit_d, plane_id))
{
m_dirty_planes[plane_id]++;
vertex.pos = glm::vec4(hit_o, 1);
v.push_back(vertex);
}
}
}
m_shape.update_vertices(v.data(), v.size());
points.emplace_back(std::make_shared<p2t::Point>(m_points2d[i].x, m_points2d[i].y));
auto v = triangulate(points);
LOG("leaked segments: %d", SegmentCount);
LOG("%d points", (int)v.size());
m_shape.update_vertices(v.data(), (int)v.size());
if (!m_points.empty())
{
@@ -513,7 +633,7 @@ void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
m_points.push_back(vert);
m_points.push_back(vert);
}
m_shape.update_vertices(m_points.data(), m_points.size());
m_shape.update_vertices(m_points.data(), (int)m_points.size());
}
}
break;
@@ -551,14 +671,14 @@ if (canvas->point_trace_plane(loc, ro, rd, hit_o, hit_d, hit_fb, 0))
if (m_dragging)
{
m_points.pop_back();
m_shape.update_vertices(m_points.data(), m_points.size());
m_shape.update_vertices(m_points.data(), (int)m_points.size());
}
m_dragging = false;
node->mouse_release();
if (m_points.size() < 4)
{
m_points.clear();
m_shape.update_vertices(m_points.data(), m_points.size());
m_shape.update_vertices(m_points.data(), (int)m_points.size());
}
break;
default: