Categories
Fade2D Examples

Saving and Loading a Triangulation

Saving and loading a triangulation is not trivial. This is especially true when there are millions of elements and it must be fast. For this reason, there are functions in Fade to support you in saving and loading a triangulation, it’s zones and constraint edges. The contained source file “examples_2D/ex11_save_and_load.cpp” provides simple demo code for this purpose.

int ex11_save_and_load_main()
{
    // * 1 *   Create 3 test zones
    Fade_2D dt;
    Zone2* pEarthZone(NULL);
    Zone2* pMarsZone(NULL);
    Zone2* pDiffZone(NULL);
    createTestZones(dt,pEarthZone,pMarsZone,pDiffZone);
    std::vector<Zone2*> vSaveZones;
    vSaveZones.push_back(pEarthZone);
    vSaveZones.push_back(pMarsZone);
    vSaveZones.push_back(pDiffZone);

    // * 2 *   Visualize
    pDiffZone->show("ex11_original_earth_minus_mars.ps",true,true);
    for(size_t i=0;i<vSaveZones.size();++i)
    {
        std::cout<<"Zone "<<i<<", numT="<<vSaveZones[i]->getNumberOfTriangles()<<std::endl;
    }

    // * 3 *   Save *all* triangles and zones
    std::cout<<"Saving using saveTriangulation()..."<<std::endl;
    dt.saveTriangulation("ex11_saveTriangulation.fade",vSaveZones);

    // * 4 *   Save only the 3 zones
    std::cout<<"Saving using saveZones()..."<<std::endl;
    dt.saveZones("ex11_saveZones.fade",vSaveZones);

    // * 5 *   Save only one zone
    std::cout<<"Saving using Zone2::save()..."<<std::endl;
    pDiffZone->save("ex11_marsZone.fade");

    // * 6 *   Choose one of the 3 files to reload and show
    std::cout<<std::endl<<"Reloading ex11_save..."<<std::endl;
    Fade_2D loadDt;
    std::vector<Zone2*> vLoadZones;
    bool bOK=loadDt.load("ex11_saveTriangulation.fade",vLoadZones);
    //bool bOK=loadDt.load("ex11_saveZones.fade",vLoadZones);
    //bool bOK=loadDt.load("ex11_marsZone.fade",vLoadZones);
    if(!bOK)
    {
        std::cerr<<"File not loaded, exit"<<std::endl;
        exit(1);
    }

    for(size_t i=0;i<vLoadZones.size();++i)
    {
        Zone2* pZone(vLoadZones[i]);
        std::cout<<"Zone "<<i<<": numT="<<pZone->getNumberOfTriangles()<<std::endl;
        std::string name("loadZone"+toString(i)+".ps");
        pZone->show(name.c_str(),true,true);
    }
    return 0;
}
  1. The first step is a call to createTestZones() where we create the three zones pEarthZone, pMarsZone and pDiffZone for this demo. The code in this function is trivial if you have studied the previous example Polygon Clipping and Boolean Operations.
  2. The second step prints the number of triangles in each zone and next there are three use-cases:
  3. The first use case saves all triangles and zones using Fade_2D::saveTriangulation(filename,vZones)
  4. In contrast the second use case does not save the whole triangulation but only specified zones. Is uses Fade_2D::saveZones(filename,vZones) for this purpose.
  5. The third use case is similar but we want to save only a single zone. Although we could push the zone into a vector<Zone2*> and use Fade_2D::saveZones() there is a shorter command, namely Zone2::save(filename).
  6. In the final step we use Fade_2D::load(filename,vZones) to reload the triangulation data from one of the files.
Zone 3 "pEarthZone" reloaded from file "ex11_saveTriangulation.fade"
Reloaded Zone0 “pEarthZone”
Zone 1 "pMarsZone" reloaded from file "ex11_saveTriangulation.fade"
Reloaded Zone1 “pMarsZone”
Zone 2 "pDiffZone" reloaded from file "ex11_saveTriangulation.fade"
Reloaded Zone2 “pDiffZone”

The above three images show the whole triangulation and highlight the three zones as reloaded from the file “ex11_saveTriangulation.fade”: All triangles are contained because we used saveTriangulation() to create the file.

In contrast the three images below show a reduced triangulation from the file “ex11_saveZones.fade” that we created with saveZones(): The file contains only the highlighted zone triangles plus a few additional fill-triangles, see below.

Zone0 "pEarthZone" reloaded from file "ex11_saveZones.fade"
Reloaded Zone0 “pEarthZone”

Zone1 "pMarsZone" reloaded from file "ex11_saveZones.fade"
Reloaded Zone1 “pMarsZone”
Zone2 "pDiffZone" reloaded from file "ex11_saveZones.fade"
Reloaded Zone2 “pDiffZone”

Saved triangulations are always convex

A Delaunay triangulation is always convex. This is not only a geometric property, but also a technical requirement in Fade. In contrast, zones that you save can have concave locations. Therefore, the saveZones() command may create additional fill-triangles to complete the convex hull of the area to be saved, see the image below. These fill-triangles then belong to the triangulation, but not to any of the saved zones.

Fill-triangles created to complete the convex hull of the stored zones. This is a technical requirement.
This image shows 6 fill-triangles created to complete the convex hull of the stored zones. This is a technical requirement.

Leave a Reply

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