Best way to visualize a 2D triangulation?
Hello!
I want to visualize a large 2D triangulation consisting of several million triangles. It works already
but the memory consumption is way too high:
Code:
{
public:
TriangulationScene();
void scribble();
...
}
void TriangulationScene::scribble()
{
setItemIndexMethod(NoIndex);
for(vector<Triangle2*>::iterator it(vTriangles.begin());it!=vTriangles.end();++it)
{
Triangle2* pT(*it);
Point2* p0(pT->getCorner(0));
Point2* p1(pT->getCorner(1));
Point2* p2(pT->getCorner(2));
polygon <<
QPointF(p0
->x
(),p0
->y
())<<QPointF
(p1
->x
(),p1
->y
())<<QPointF
(p2
->x
(),p2
->y
());
this->addPolygon(polygon);
}
}
int main(int argc, char *argv[])
{
TriangulationScene s;
v.resize(1024,768);
v.scale(100,100);
v.show();
return a.exec();
}
This way I need 1 GB for 2 million triangles. I see two possible workarounds:
1) Creating a picture of the triangulation. The drawback is that the picture will certainly
look ugly when zoomed in.
2) I could do it like shown above if I add only the small amount of triangles that must currently
be shown. However, this would require quite some work to implement updates when zooming
or scrolling is done.
Any better ideas? Is there a better class for my purpose than QGraphicsScene?
Thanks, best regards
Re: Best way to visualize a 2D triangulation?
You can drop the scene and use simple widget.
Just draw on the widget using paint event.
It will require less memory and may be even faster if done right, but it will require more work to implement.
In my test 1m polys in scene requires around 600mb of memory, when doing the same on a widget requires just ~90mb.
Qwt uses that technique and it works well.
Depends on what you need - you could try Qwt.
Re: Best way to visualize a 2D triangulation?
Quote:
Originally Posted by
Spitfire
You can drop the scene and use simple widget.
Just draw on the widget using paint event.
It will require less memory and may be even faster if done right, but it will require more work to implement.
In my test 1m polys in scene requires around 600mb of memory, when doing the same on a widget requires just ~90mb.
Qwt uses that technique and it works well.
Depends on what you need - you could try Qwt.
Thank you!
I'm new to Qwt, which classes do you have used for your test?
Re: Best way to visualize a 2D triangulation?
I haven't tested how well Qwt will perform in this case.
If you want to try Qwt here's simple example how to use it:
Code:
#include "mainwindow.h"
#include <QTime>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
MainWindow
::MainWindow(QWidget *parent
){
plot->setAutoReplot(false);
qsrand
(QTime::currentTime().
msec());
int count = 100;
for(int i = 0; i < count; ++i)
{
QPointF start
(qrand
()%300, qrand
()%300
);
poly << start;
poly <<
QPointF(qrand
()%300, qrand
()%300
);
poly <<
QPointF(qrand
()%300, qrand
()%300
);
poly << start;
c->setData(poly);
c->attach(plot);
}
this->setCentralWidget(plot);
}
But you may need to do some optimization for 1M triangles to get acceptable performance.
Here's how I've done the paint event test:
Code:
//testcanvas.h
#ifndef TESTCANVAS_H
#define TESTCANVAS_H
#include <QWidget>
{
Q_OBJECT
public:
explicit testcanvas
(QWidget *parent
= 0);
protected:
private:
QList<QPolygon> poly_list;
};
#endif // TESTCANVAS_H
Code:
//testcanvas.cpp
#include "testcanvas.h"
#include <QTime>
#include <QDebug>
#include <QPainter>
testcanvas
::testcanvas(QWidget *parent
) : poly_list( QList<QPolygon>() )
{
this->setMinimumSize(500, 500);
int count = 1000000;
qsrand
(QTime::currentTime().
msec());
t.start();
for(int i = 0; i < count; ++i)
{
poly.
append(QPoint(qrand
()%500, qrand
()%500
));
poly.
append(QPoint(qrand
()%500, qrand
()%500
));
poly.
append(QPoint(qrand
()%500, qrand
()%500
));
poly_list.append(poly);
}
qDebug() << "Generating " << count << " polys took " << t.elapsed() << "ms.";
}
{
p.fillRect(this->rect(), Qt::green);
t.start();
int count = poly_list.size();
for(int i = 0; i < count; ++i)
{
p.drawPolygon(poly_list[i]);
}
qDebug() << "Drawing " << count << " polys took " << t.elapsed() << "ms.";
}
Code:
//mainwindow.cpp
#include "mainwindow.h"
#include "testcanvas.h"
MainWindow
::MainWindow(QWidget *parent
){
this->setCentralWidget(new testcanvas());
}
Code:
Generating 1000000 polys took 842 ms.
Drawing 1000000 polys took 26380 ms.
Memory footprint ~87MB
Re: Best way to visualize a 2D triangulation?
Thank you for your help! I will try that.
Best regards