This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Difference of geometries in C++

+2 votes

Dear all,

How can I generate a mesh resulting from the difference (or another boolean operation) of geometries in C++?

When trying the difference giving a polygon and a circle as parameters I cannot compile the program, since the class it's expecting a shared_ptr, so I've created shared pointers, but again I cannot compile:

In file included from /Users/sensei/Documents/Projects/fracture/fenicspp/fenicspp/main.cpp:24:
In file included from /Applications/FEniCS.app/Contents/Resources/include/dolfin.h:10:
In file included from /Applications/FEniCS.app/Contents/Resources/include/dolfin/common/dolfin_common.h:7:
In file included from /Applications/FEniCS.app/Contents/Resources/include/dolfin/common/defines.h:27:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2100:15: error: no matching constructor for initialization of 'mshr::Polygon'
              __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
              ^         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's my code:

    // Vector of points
    std::vector<Point> pts;
    pts.push_back(Point(  0.0,   0.0));
    pts.push_back(Point(  4.0,   0.0));
    pts.push_back(Point(  4.0,   8.0));
    pts.push_back(Point(  0.0,   8.0));
    pts.push_back(Point(  0.0,   8.0 / 2 + 0.5));
    pts.push_back(Point(  1.0,   8.0 / 2 + 0.5));
    pts.push_back(Point(  2.0,   8.0 / 4      ));
    pts.push_back(Point(  1.0,   8.0 / 2 - 0.5));
    pts.push_back(Point(  0.0,   8.0 / 2 - 0.5));

    // Polyline
    //mshr::Polygon polyline(pts);
    auto polyline = std::make_shared<mshr::Polygon>(new mshr::Polygon(pts));

    // Hole
    //mshr::Circle circle(Point(2.0, 7.0), 0.3);
    auto circle = std::make_shared<mshr::Circle>(new mshr::Circle(Point(2.0, 7.0), 0.3));

    // Difference
    mshr::CSGDifference difference(polyline, circle);

    std::cout << "> generating mesh " << std::endl;

    // Create a simple mesh
    Mesh simple;

    // Generator
    mshr::CSGCGALMeshGenerator2D generator;

    // Set patameters
    generator.parameters["mesh_resolution"] = -1.0;
    generator.parameters["cell_size"] = 1.0;

    // Mesh it!
    generator.generate(difference, simple);

How can I correct my errors? The documentation of mshr isn't complete (and in the difference says still "CSGDifference ... Create union of two geometries").

Thanks!

asked May 27, 2015 by senseiwa FEniCS User (2,620 points)

1 Answer

+2 votes
 
Best answer

So, please excuse this self-answer, but the Wiki didn't help... Instead, the source code did!

As simple as it can be, mshr works this way:

    // Polyline
    mshr::Polygon polyline(pts);

    // Hole
    mshr::Circle circle(Point(2.0, 7.0), 0.3);

    // Difference: IT IS A SHARED POINTER
    auto difference = polyline - circle;

    std::cout << "> generating mesh " << std::endl;

    // Create a simple mesh
    Mesh simple;

    // Generator
    mshr::CSGCGALMeshGenerator2D generator;

    // Set patameters
    generator.parameters["mesh_resolution"] = -1.0;
    generator.parameters["cell_size"] = 1.0;

    // Mesh it!
    generator.generate(*difference, simple);

Cheers!

answered May 27, 2015 by senseiwa FEniCS User (2,620 points)
selected May 27, 2015 by senseiwa
...