mirror of
https://github.com/jhasse/poly2tri.git
synced 2025-08-12 18:25:39 +02:00
memory cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
|
||||
* http://code.google.com/p/poly2tri/
|
||||
*
|
||||
@@ -40,7 +40,6 @@
|
||||
|
||||
namespace p2t {
|
||||
|
||||
struct Node;
|
||||
struct Edge;
|
||||
|
||||
struct Point {
|
||||
@@ -50,7 +49,7 @@ struct Point {
|
||||
/// Default constructor does nothing (for performance).
|
||||
Point()
|
||||
{
|
||||
x = 0.0;
|
||||
x = 0.0;
|
||||
y = 0.0;
|
||||
}
|
||||
|
||||
@@ -63,43 +62,43 @@ struct Point {
|
||||
/// Set this point to all zeros.
|
||||
void set_zero()
|
||||
{
|
||||
x = 0.0f;
|
||||
x = 0.0f;
|
||||
y = 0.0f;
|
||||
}
|
||||
|
||||
/// Set this point to some specified coordinates.
|
||||
void set(double x_, double y_)
|
||||
{
|
||||
x = x_;
|
||||
x = x_;
|
||||
y = y_;
|
||||
}
|
||||
|
||||
/// Negate this point.
|
||||
Point operator -() const
|
||||
{
|
||||
Point v;
|
||||
v.set(-x, -y);
|
||||
Point v;
|
||||
v.set(-x, -y);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// Add a point to this point.
|
||||
void operator +=(const Point& v)
|
||||
{
|
||||
x += v.x;
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
}
|
||||
|
||||
/// Subtract a point from this point.
|
||||
void operator -=(const Point& v)
|
||||
{
|
||||
x -= v.x;
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
}
|
||||
|
||||
/// Multiply this point by a scalar.
|
||||
void operator *=(double a)
|
||||
{
|
||||
x *= a;
|
||||
x *= a;
|
||||
y *= a;
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
|
||||
* http://code.google.com/p/poly2tri/
|
||||
*
|
||||
@@ -42,7 +42,7 @@ AdvancingFront::AdvancingFront(Node& head, Node& tail)
|
||||
Node* AdvancingFront::LocateNode(const double& x)
|
||||
{
|
||||
Node* node = search_node_;
|
||||
|
||||
|
||||
if (x < node->value) {
|
||||
while ((node = node->prev) != NULL) {
|
||||
if (x >= node->value) {
|
||||
@@ -72,7 +72,7 @@ Node* AdvancingFront::LocatePoint(const Point* point)
|
||||
const double px = point->x;
|
||||
Node* node = FindSearchNode(px);
|
||||
const double nx = node->point->x;
|
||||
|
||||
|
||||
if (px == nx) {
|
||||
if (point != node->point) {
|
||||
// We might have two nodes with same x value for a short time
|
||||
@@ -102,9 +102,6 @@ Node* AdvancingFront::LocatePoint(const Point* point)
|
||||
|
||||
AdvancingFront::~AdvancingFront()
|
||||
{
|
||||
delete head_;
|
||||
delete search_node_;
|
||||
delete tail_;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
|
||||
* http://code.google.com/p/poly2tri/
|
||||
*
|
||||
@@ -28,7 +28,7 @@
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef ADVANCED_FRONT_H
|
||||
#define ADVANCED_FRONT_H
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
|
||||
* http://code.google.com/p/poly2tri/
|
||||
*
|
||||
@@ -88,7 +88,6 @@ Node& Sweep::PointEvent(SweepContext& tcx, Point& point)
|
||||
// x value than node due to how we fetch nodes from the front
|
||||
if (point.x <= node.point->x + EPSILON) {
|
||||
Fill(tcx, node);
|
||||
delete &node;
|
||||
}
|
||||
|
||||
//tcx.AddNode(new_node);
|
||||
@@ -172,12 +171,12 @@ Node& Sweep::NewFrontTriangle(SweepContext& tcx, Point& point, Node& node)
|
||||
|
||||
Node* new_node = new Node(point);
|
||||
nodes_.push_back(new_node);
|
||||
|
||||
|
||||
new_node->next = node.next;
|
||||
new_node->prev = &node;
|
||||
node.next->prev = new_node;
|
||||
node.next = new_node;
|
||||
|
||||
node.next = new_node;
|
||||
|
||||
if (!Legalize(tcx, *triangle)) {
|
||||
tcx.MapTriangleToNodes(*triangle);
|
||||
}
|
||||
@@ -203,13 +202,13 @@ void Sweep::Fill(SweepContext& tcx, Node& node)
|
||||
|
||||
// Update the advancing front
|
||||
node.prev->next = node.next;
|
||||
node.next->prev = node.prev;
|
||||
node.next->prev = node.prev;
|
||||
|
||||
// If it was legalized the triangle has already been mapped
|
||||
if (!Legalize(tcx, *triangle)) {
|
||||
tcx.MapTriangleToNodes(*triangle);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,7 +219,8 @@ void Sweep::Fill(SweepContext& tcx, Node& node)
|
||||
* @param n
|
||||
*/
|
||||
void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
|
||||
{
|
||||
{
|
||||
|
||||
// Fill right holes
|
||||
Node* node = n.next;
|
||||
|
||||
@@ -228,9 +228,7 @@ void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
|
||||
double angle = HoleAngle(*node);
|
||||
if (angle > M_PI_2 || angle < -M_PI_2) break;
|
||||
Fill(tcx, *node);
|
||||
Node *temp = node;
|
||||
node = node->next;
|
||||
delete temp;
|
||||
}
|
||||
|
||||
// Fill left holes
|
||||
@@ -240,9 +238,7 @@ void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
|
||||
double angle = HoleAngle(*node);
|
||||
if (angle > M_PI_2 || angle < -M_PI_2) break;
|
||||
Fill(tcx, *node);
|
||||
Node *temp = node;
|
||||
node = node->prev;
|
||||
delete temp;
|
||||
}
|
||||
|
||||
// Fill right basins
|
||||
@@ -528,23 +524,19 @@ void Sweep::FillBasinReq(SweepContext& tcx, Node* node)
|
||||
return;
|
||||
}
|
||||
|
||||
Fill(tcx, *node);
|
||||
Node *temp = node;
|
||||
|
||||
Fill(tcx, *node);
|
||||
|
||||
if (node->prev == tcx.basin.left_node && node->next == tcx.basin.right_node) {
|
||||
delete node;
|
||||
return;
|
||||
} else if (node->prev == tcx.basin.left_node) {
|
||||
Orientation o = Orient2d(*node->point, *node->next->point, *node->next->next->point);
|
||||
if (o == CW) {
|
||||
delete node;
|
||||
return;
|
||||
}
|
||||
node = node->next;
|
||||
} else if (node->next == tcx.basin.right_node) {
|
||||
Orientation o = Orient2d(*node->point, *node->prev->point, *node->prev->prev->point);
|
||||
if (o == CCW) {
|
||||
delete node;
|
||||
return;
|
||||
}
|
||||
node = node->prev;
|
||||
@@ -557,7 +549,6 @@ void Sweep::FillBasinReq(SweepContext& tcx, Node* node)
|
||||
}
|
||||
}
|
||||
|
||||
delete temp;
|
||||
FillBasinReq(tcx, node);
|
||||
}
|
||||
|
||||
@@ -628,8 +619,8 @@ void Sweep::FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
|
||||
// Next is convex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Sweep::FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
|
||||
@@ -708,9 +699,9 @@ void Sweep::FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
|
||||
} else{
|
||||
// Next is convex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p)
|
||||
@@ -765,7 +756,7 @@ Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangl
|
||||
|
||||
// t is not crossing edge after flip
|
||||
int edge_index = t.EdgeIndex(&p, &op);
|
||||
|
||||
|
||||
t.delaunay_edge[edge_index] = true;
|
||||
Legalize(tcx, t);
|
||||
t.ClearDelunayEdges();
|
||||
@@ -814,6 +805,15 @@ void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle&
|
||||
Point& newP = NextFlipPoint(ep, eq, ot, op);
|
||||
FlipScanEdgeEvent(tcx, ep, eq, flip_triangle, ot, newP);
|
||||
}
|
||||
}
|
||||
|
||||
Sweep::~Sweep() {
|
||||
|
||||
// Clean up memory
|
||||
for(int i = 0; i < nodes_.size(); i++) {
|
||||
delete nodes_[i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
|
||||
* http://code.google.com/p/poly2tri/
|
||||
*
|
||||
@@ -35,7 +35,7 @@
|
||||
*
|
||||
* "FlipScan" Constrained Edge Algorithm invented by Thomas <20>hl<68>n, thahlen@gmail.com
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SWEEP_H
|
||||
#define SWEEP_H
|
||||
|
||||
@@ -53,6 +53,7 @@ class Sweep {
|
||||
public:
|
||||
|
||||
void Triangulate(SweepContext& tcx);
|
||||
~Sweep();
|
||||
|
||||
private:
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
|
||||
* http://code.google.com/p/poly2tri/
|
||||
*
|
||||
@@ -91,7 +91,7 @@ void SweepContext::InitTriangulation()
|
||||
|
||||
// Sort points along y-axis
|
||||
std::sort(points_.begin(), points_.end(), cmp);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void SweepContext::InitEdges(std::vector<Point*> polyline)
|
||||
@@ -121,28 +121,23 @@ Node& SweepContext::LocateNode(Point& point)
|
||||
|
||||
void SweepContext::CreateAdvancingFront(std::vector<Node*> nodes)
|
||||
{
|
||||
Node *head, *middle, *tail;
|
||||
|
||||
// Initial triangle
|
||||
Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
|
||||
|
||||
map_.push_back(triangle);
|
||||
|
||||
head = new Node(*triangle->GetPoint(1), *triangle);
|
||||
middle = new Node(*triangle->GetPoint(0), *triangle);
|
||||
tail = new Node(*triangle->GetPoint(2));
|
||||
front_ = new AdvancingFront(*head, *tail);
|
||||
|
||||
// Memory management :)
|
||||
nodes.push_back(head);
|
||||
nodes.push_back(middle);
|
||||
nodes.push_back(tail);
|
||||
|
||||
af_head_ = new Node(*triangle->GetPoint(1), *triangle);
|
||||
af_middle_ = new Node(*triangle->GetPoint(0), *triangle);
|
||||
af_tail_ = new Node(*triangle->GetPoint(2));
|
||||
front_ = new AdvancingFront(*af_head_, *af_tail_);
|
||||
|
||||
// TODO: More intuitive if head is middles next and not previous?
|
||||
// so swap head and tail
|
||||
head->next = middle;
|
||||
middle->next = tail;
|
||||
middle->prev = head;
|
||||
tail->prev = middle;
|
||||
af_head_->next = af_middle_;
|
||||
af_middle_->next = af_tail_;
|
||||
af_middle_->prev = af_head_;
|
||||
af_tail_->prev = af_middle_;
|
||||
}
|
||||
|
||||
void SweepContext::RemoveNode(Node* node)
|
||||
@@ -180,9 +175,27 @@ void SweepContext::MeshClean(Triangle& triangle)
|
||||
|
||||
SweepContext::~SweepContext()
|
||||
{
|
||||
delete head_;
|
||||
delete tail_;
|
||||
delete front_;
|
||||
|
||||
// Clean up memory
|
||||
|
||||
delete head_;
|
||||
delete tail_;
|
||||
delete front_;
|
||||
delete af_head_;
|
||||
delete af_middle_;
|
||||
delete af_tail_;
|
||||
|
||||
typedef std::list<Triangle*> type_list;
|
||||
|
||||
for(type_list::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
|
||||
Triangle* ptr = *iter;
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
for(int i = 0; i < edge_list.size(); i++) {
|
||||
delete edge_list[i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
|
||||
* http://code.google.com/p/poly2tri/
|
||||
*
|
||||
@@ -28,7 +28,7 @@
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SWEEP_CONTEXT_H
|
||||
#define SWEEP_CONTEXT_H
|
||||
|
||||
@@ -91,7 +91,8 @@ AdvancingFront* front();
|
||||
void MeshClean(Triangle& triangle);
|
||||
|
||||
std::vector<Triangle*> GetTriangles();
|
||||
std::list<Triangle*> GetMap();
|
||||
std::list<Triangle*> GetMap();
|
||||
|
||||
std::vector<Edge*> edge_list;
|
||||
|
||||
struct Basin {
|
||||
@@ -133,7 +134,6 @@ friend class Sweep;
|
||||
|
||||
std::vector<Triangle*> triangles_;
|
||||
std::list<Triangle*> map_;
|
||||
|
||||
std::vector<Point*> points_;
|
||||
|
||||
// Advancing front
|
||||
@@ -143,7 +143,7 @@ Point* head_;
|
||||
// tail point used with advancing front
|
||||
Point* tail_;
|
||||
|
||||
//EdgeEvent edgeEvent = new EdgeEvent();
|
||||
Node *af_head_, *af_middle_, *af_tail_;
|
||||
|
||||
void InitTriangulation();
|
||||
void InitEdges(std::vector<Point*> polyline);
|
||||
|
Reference in New Issue
Block a user