Results 1 to 5 of 5

Thread: Best way to visualize a 2D triangulation?

  1. #1
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1

    Default 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:
    Qt Code:
    1. class TriangulationScene : public QGraphicsScene
    2. {
    3. public:
    4. TriangulationScene();
    5. void scribble();
    6. ...
    7. }
    8.  
    9. void TriangulationScene::scribble()
    10. {
    11. setItemIndexMethod(NoIndex);
    12. for(vector<Triangle2*>::iterator it(vTriangles.begin());it!=vTriangles.end();++it)
    13. {
    14. Triangle2* pT(*it);
    15. Point2* p0(pT->getCorner(0));
    16. Point2* p1(pT->getCorner(1));
    17. Point2* p2(pT->getCorner(2));
    18. QPolygonF polygon;
    19. polygon << QPointF(p0->x(),p0->y())<<QPointF(p1->x(),p1->y())<<QPointF(p2->x(),p2->y());
    20. this->addPolygon(polygon);
    21. }
    22. }
    23.  
    24. int main(int argc, char *argv[])
    25. {
    26. QApplication a(argc, argv);
    27. TriangulationScene s;
    28. v.setDragMode(QGraphicsView::ScrollHandDrag);
    29. v.resize(1024,768);
    30. v.scale(100,100);
    31. v.show();
    32. return a.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1

    Default Re: Best way to visualize a 2D triangulation?

    Quote Originally Posted by Spitfire View Post
    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?

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QTime>
    4.  
    5. #include <qwt_plot.h>
    6. #include <qwt_plot_curve.h>
    7.  
    8. MainWindow::MainWindow(QWidget *parent)
    9. : QMainWindow(parent)
    10. {
    11. QwtPlot* plot = new QwtPlot(this);
    12. plot->setAutoReplot(false);
    13.  
    14. qsrand(QTime::currentTime().msec());
    15.  
    16. int count = 100;
    17.  
    18. for(int i = 0; i < count; ++i)
    19. {
    20. QPointF start(qrand()%300, qrand()%300);
    21.  
    22. QPolygonF poly;
    23. poly << start;
    24. poly << QPointF(qrand()%300, qrand()%300);
    25. poly << QPointF(qrand()%300, qrand()%300);
    26. poly << start;
    27.  
    28. c->setData(poly);
    29. c->attach(plot);
    30. }
    31.  
    32. this->setCentralWidget(plot);
    33. }
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. //testcanvas.h
    2. #ifndef TESTCANVAS_H
    3. #define TESTCANVAS_H
    4.  
    5. #include <QWidget>
    6.  
    7. class testcanvas : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit testcanvas(QWidget *parent = 0);
    12.  
    13. protected:
    14. void paintEvent(QPaintEvent *);
    15.  
    16. private:
    17. QList<QPolygon> poly_list;
    18. };
    19.  
    20. #endif // TESTCANVAS_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //testcanvas.cpp
    2. #include "testcanvas.h"
    3.  
    4. #include <QTime>
    5. #include <QDebug>
    6. #include <QPainter>
    7.  
    8. testcanvas::testcanvas(QWidget *parent) :
    9. QWidget(parent),
    10. poly_list( QList<QPolygon>() )
    11. {
    12. this->setMinimumSize(500, 500);
    13.  
    14. int count = 1000000;
    15. qsrand(QTime::currentTime().msec());
    16.  
    17. QTime t;
    18. t.start();
    19.  
    20. for(int i = 0; i < count; ++i)
    21. {
    22. QPolygon poly;
    23. poly.append(QPoint(qrand()%500, qrand()%500));
    24. poly.append(QPoint(qrand()%500, qrand()%500));
    25. poly.append(QPoint(qrand()%500, qrand()%500));
    26.  
    27. poly_list.append(poly);
    28. }
    29.  
    30. qDebug() << "Generating " << count << " polys took " << t.elapsed() << "ms.";
    31. }
    32.  
    33. void testcanvas::paintEvent(QPaintEvent *)
    34. {
    35. QPainter p(this);
    36.  
    37. p.fillRect(this->rect(), Qt::green);
    38.  
    39. QTime t;
    40. t.start();
    41.  
    42. int count = poly_list.size();
    43. for(int i = 0; i < count; ++i)
    44. {
    45. p.drawPolygon(poly_list[i]);
    46. }
    47. qDebug() << "Drawing " << count << " polys took " << t.elapsed() << "ms.";
    48. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //mainwindow.cpp
    2. #include "mainwindow.h"
    3.  
    4. #include "testcanvas.h"
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. this->setCentralWidget(new testcanvas());
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Generating 1000000 polys took 842 ms.
    2. Drawing 1000000 polys took 26380 ms.
    3. Memory footprint ~87MB
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Spitfire for this useful post:

    bts (2nd November 2011)

  6. #5
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1

    Default Re: Best way to visualize a 2D triangulation?

    Thank you for your help! I will try that.

    Best regards

Similar Threads

  1. Qt Creator Vertical split to visualize both header and implementation
    By akiross in forum Qt Tools
    Replies: 1
    Last Post: 16th July 2012, 14:05
  2. Drawing a 2D triangulation (solved)
    By bts in forum Newbie
    Replies: 0
    Last Post: 15th October 2011, 17:59
  3. visualize qtQuick project
    By amadanes in forum Newbie
    Replies: 1
    Last Post: 9th May 2011, 00:15
  4. Replies: 0
    Last Post: 6th May 2011, 17:37
  5. Visualize Signals & Slots
    By hyling in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2006, 02:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.