### Using mshr::CSGDifference within a loop

81
views
0
3 months ago by
Hello,

I am trying to create polygonal domain by subtracting small rectengles from a big rectengle in C++. I have done this in python as following:
    # define the initial rectengle
domain =   Rectangle(dolfin.Point(b0, 0), dolfin.Point(b1, R))

for i in range(n):
domain = domain - Rectangle(dolfin.Point(x[i]-t, R), dolfin.Point(x[i] + t[i], r))    ​

Now I want to write this part in C++ but I do not know how to use mshr::CSGDifference iteratively.

auto rectangle = std::make_shared<mshr::Rectangle>(dolfin::Point(b0, 0, 0.0), dolfin::Point(b1, R, 0.0));
for (unsigned int t = 0; t <  elects.size(); t++)
{
auto elec_domain = std::make_shared<mshr::Rectangle>(dolfin::Point(elects[t].pos()-elects[t].t()/2, elects[t].r(), 0.0), dolfin::Point(elects[t].pos()+elects[t].t()/2, R, 0.0));
auto difference = (std::make_shared<mshr::CSGDifference>(rectangle, elec_domain));
// rectangle cannot be updated by difference as CSGDifference class.
//rectangle = difference;
}
​

Does anybody know how to update initial rectengle with newly computed difference?

Best wishes,
Nick
Community: FEniCS Project

0
3 months ago by

Not tested:

auto rectangle = std::make_shared<mshr::Rectangle>(dolfin::Point(b0, 0, 0.0), dolfin::Point(b1, R, 0.0));
std::shared_ptr<CSGGeometry> domain = rectangle;  // or maybe with std::static_pointer_case

for (unsigned int t = 0; t < elects.size(); t++)
{
auto elec_domain = std::make_shared<mshr::Rectangle>(
dolfin::Point(elects[t].pos()-elects[t].t()/2, elects[t].r(), 0.0),
dolfin::Point(elects[t].pos()+elects[t].t()/2, R, 0.0));
domain = std::make_shared<mshr::CSGDifference>(domain, elec_domain);    // or maybe with std::static_pointer_case
}

The approach is to update a common parent class of CSGDifferenceand Rectanglewhich is CSGGeometry. (After all in your Python snippet rectangle object is not updated. The variable name rectangle is just assigned to an object newly created by taking a difference on every iteration.)

0
3 months ago by
Many thanks Jan. The one with static casting did the trick!

Yours,
Nick