Categories
2D Delaunay Triangulation Examples in C++

Progress Bar Mechanism – Example 10

Fade2D is fast, and most of the time, you will not need progress updates at all. However, for very large triangulations, you may still want to connect your own progress bar to Fade2D’s update mechanism.

Creating Your Custom Progress Receiver Class

To receive progress information, you must create a custom class in your software that derives from MsgBase. The below code demonstrates that by defining a class MyProgressBar, which is a simple terminal-progress-indicator.

// Create a simple command line progress bar that 
// derives from MsgBase so that it can receive messages
class MyProgressBar:public MsgBase
{
public: 
    // Fade will later call the update method with d={0.0,...,1.0} 
    void update(MsgType ,const char* s,double d)
    {
        cout<<s<<" [";
        for(size_t i=0;i<10;++i)
        {
           if(i/10.0<d) cout<<"=";
              else cout<<" ";
        }
        cout<<"] "<<d*100.0<<" %    \r"<<flush;
        if(d==1.0) cout<<endl<<endl;
    }
};

Connect Your Progress Bar Object with Fade2D

The code snippet below instantiates an object from the MyProgressBar class and connects it to Fade2D.

“Make sure that the progress bar object is not deleted before the Fade2D object. Otherwise, Fade2D could attempt to notify a deleted object”

// * 1 *   Create a Fade object dt
Fade_2D dt;
 
// * 2 *   Progress bar object: Subscribe to MSG_PROGRESS messages 
MyProgressBar myPBar;
dt.subscribe(MSG_PROGRESS,&myPBar);
 
// * 3 *   Insert some random segments. Observe that
//         myPBar receives progress updates
std::vector<Segment2> vInputSegments;
generateRandomSegments(10000,0,100,10,vInputSegments,0);
dt.createConstraint(vInputSegments, CIS_CONSTRAINED_DELAUNAY);
...

Leave a Reply

Your email address will not be published. Required fields are marked *