memory cleanup

This commit is contained in:
zzzzrrr 2010-04-25 12:40:54 -04:00
parent bab616f9ea
commit d6dd5da27c
10 changed files with 120 additions and 109 deletions

View File

@ -40,7 +40,6 @@
namespace p2t {
struct Node;
struct Edge;
struct Point {

View File

@ -102,9 +102,6 @@ Node* AdvancingFront::LocatePoint(const Point* point)
AdvancingFront::~AdvancingFront()
{
delete head_;
delete search_node_;
delete tail_;
}
}

View File

@ -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);
@ -221,6 +220,7 @@ void Sweep::Fill(SweepContext& tcx, Node& node)
*/
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
@ -529,22 +525,18 @@ void Sweep::FillBasinReq(SweepContext& tcx, Node* node)
}
Fill(tcx, *node);
Node *temp = 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);
}
@ -816,5 +807,14 @@ void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle&
}
}
Sweep::~Sweep() {
// Clean up memory
for(int i = 0; i < nodes_.size(); i++) {
delete nodes_[i];
}
}
}

View File

@ -53,6 +53,7 @@ class Sweep {
public:
void Triangulate(SweepContext& tcx);
~Sweep();
private:

View File

@ -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()
{
// 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];
}
}
}

View File

@ -92,6 +92,7 @@ void MeshClean(Triangle& triangle);
std::vector<Triangle*> GetTriangles();
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);

View File

@ -199,6 +199,7 @@ int main(int argc, char* argv[])
MainLoop(zoom);
delete cdt;
ShutDown(0);
return 0;
}

0
waf vendored Normal file → Executable file
View File

View File

@ -34,8 +34,8 @@ def configure(conf):
print(' calling the configuration')
conf.check_tool('g++')
#conf.env.CXXFLAGS = ['-O0', '-pg', '-g']
#conf.env.CXXFLAGS = ['-O0', '-g']
conf.env.CXXFLAGS = ['-O3', '-ffast-math']
conf.env.CXXFLAGS = ['-O0', '-g']
#conf.env.CXXFLAGS = ['-O3', '-ffast-math']
def build(bld):