poly2tri/poly2tri/sweep/sweep_context.cc

166 lines
3.6 KiB
C++
Raw Normal View History

2010-01-17 17:41:09 +01:00
#include "sweep_context.h"
#include <algorithm>
#include <GL/glfw.h>
#include "advancing_front.h"
2010-01-21 15:00:09 +01:00
SweepContext::SweepContext(Point** polyline, const int& point_count)
{
2010-01-17 17:41:09 +01:00
basin = Basin();
edge_event = EdgeEvent();
2010-01-21 15:00:09 +01:00
2010-01-20 18:10:58 +01:00
points_ = polyline;
point_count_ = point_count;
2010-01-21 15:00:09 +01:00
2010-01-20 18:10:58 +01:00
InitEdges(points_, point_count_);
2010-01-17 17:41:09 +01:00
InitTriangulation();
}
2010-01-21 15:00:09 +01:00
std::vector<Triangle*> SweepContext::GetTriangles()
{
2010-01-20 18:10:58 +01:00
return triangles_;
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
std::list<Triangle*> SweepContext::GetMap()
{
2010-01-20 18:25:10 +01:00
return map_;
}
2010-01-21 15:00:09 +01:00
void SweepContext::InitTriangulation()
{
2010-01-20 18:10:58 +01:00
double xmax(points_[0]->x), xmin(points_[0]->x);
double ymax(points_[0]->y), ymin(points_[0]->y);
2010-01-21 15:00:09 +01:00
// Calculate bounds.
for (int i = 0; i < point_count_; i++) {
2010-01-20 18:10:58 +01:00
Point p = *points_[i];
2010-01-21 15:00:09 +01:00
if (p.x > xmax)
xmax = p.x;
if (p.x < xmin)
xmin = p.x;
if (p.y > ymax)
ymax = p.y;
if (p.y < ymin)
ymin = p.y;
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
double dx = kAlpha * (xmax - xmin);
double dy = kAlpha * (ymax - ymin);
2010-01-17 17:41:09 +01:00
head_ = new Point(xmax + dx, ymin - dy);
tail_ = new Point(xmin - dx, ymin - dy);
2010-01-21 15:00:09 +01:00
2010-01-17 17:41:09 +01:00
// Sort points along y-axis
double init_time = glfwGetTime();
2010-01-20 18:10:58 +01:00
std::sort(points_, points_ + point_count_, cmp);
2010-01-17 17:41:09 +01:00
double dt = glfwGetTime() - init_time;
printf("Sort time (secs) = %f\n", dt);
2010-01-21 15:00:09 +01:00
2010-01-21 17:20:56 +01:00
/*
2010-01-20 18:10:58 +01:00
printf("*************************\n");
2010-01-21 15:00:09 +01:00
for (int i = 0; i < point_count_; i++) {
2010-01-20 21:36:05 +01:00
printf("%f,%f ", points_[i]->x, points_[i]->y);
printf("%p\n", points_[i]);
2010-01-20 18:10:58 +01:00
}
2010-01-17 17:41:09 +01:00
2010-01-21 15:00:09 +01:00
/*
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
for(int i = 0; i < edge_list.size(); i++) {
edge_list[i]->p->DebugPrint(); edge_list[i]->q->DebugPrint();
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
}
*/
}
2010-01-19 23:23:38 +01:00
2010-01-21 15:00:09 +01:00
void SweepContext::InitEdges(Point** polyline, const int& point_count)
{
for (int i = 0; i < point_count; i++) {
2010-01-20 18:10:58 +01:00
int j = i < point_count - 1 ? i + 1 : 0;
edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
2010-01-20 18:10:58 +01:00
/*
2010-01-21 15:00:09 +01:00
for(int i = 0; i < edge_list.size(); i++) {
edge_list[i]->p->DebugPrint(); edge_list[i]->q->DebugPrint();
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
}
*/
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
Point* SweepContext::GetPoint(const int& index)
{
return points_[index];
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
void SweepContext::AddToMap(Triangle* triangle)
{
2010-01-20 18:10:58 +01:00
map_.push_back(triangle);
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
Node& SweepContext::LocateNode(Point& point)
{
// TODO implement search tree
2010-01-22 15:57:49 +01:00
return *front_->LocateNode(point.x);
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
void SweepContext::CreateAdvancingFront()
{
2010-01-21 17:20:56 +01:00
Node *head, *middle, *tail;
2010-01-21 15:00:09 +01:00
// Initial triangle
Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
map_.push_back(triangle);
2010-01-21 17:20:56 +01:00
head = new Node(*triangle->GetPoint(1), *triangle);
middle = new Node(*triangle->GetPoint(0), *triangle);
tail = new Node(*triangle->GetPoint(2));
2010-01-21 15:00:09 +01:00
2010-01-21 17:20:56 +01:00
front_ = new AdvancingFront(*head, *tail);
2010-01-21 15:00:09 +01:00
// TODO: More intuitive if head is middles next and not previous?
// so swap head and tail
2010-01-21 17:20:56 +01:00
head->next = middle;
middle->next = tail;
middle->prev = head;
tail->prev = middle;
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
void SweepContext::RemoveNode(Node* node)
{
delete node;
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
void SweepContext::MapTriangleToNodes(Triangle& t)
{
for (int i = 0; i < 3; i++) {
2010-01-22 15:57:49 +01:00
if (!t.GetNeighbor(i)) {
2010-01-17 17:41:09 +01:00
Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i)));
2010-01-21 15:00:09 +01:00
if (n)
2010-01-17 17:41:09 +01:00
n->triangle = &t;
2010-01-21 15:00:09 +01:00
}
}
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
void SweepContext::RemoveFromMap(Triangle* triangle)
{
2010-01-20 18:10:58 +01:00
map_.remove(triangle);
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
void SweepContext::MeshClean(Triangle& triangle)
{
if (&triangle != NULL && !triangle.IsInterior()) {
2010-01-17 17:41:09 +01:00
triangle.IsInterior(true);
2010-01-20 18:10:58 +01:00
triangles_.push_back(&triangle);
2010-01-21 15:00:09 +01:00
for (int i = 0; i < 3; i++) {
if (!triangle.constrained_edge[i])
MeshClean(*triangle.GetNeighbor(i));
2010-01-20 18:10:58 +01:00
}
}
2010-01-17 17:41:09 +01:00
}
2010-01-21 15:00:09 +01:00
SweepContext::~SweepContext()
{
2010-01-17 17:41:09 +01:00
delete head_;
delete tail_;
delete front_;
}