Centroid area calculation incorrect
Migrated from https://bugs.launchpad.net/lib2geom/+bug/1564588
Not sure if it's still valid, should be reviewd.
The Geom::centroid function can be used to calculate the area of a Piecewise. The following code creates a bounded region with a hole in it. In this case the area returned is the sum of the areas of the hole and the bounding region. I would expect that the area returned was the area of the bounding region less the area of any holes.
In this particular case I've used circles to demonstrate as their area is easily calculated at pi*r^2. Note, I've used the auto keyword from C++11 so compile with -std=c++11
#include <iostream>
#include <2geom/path.h>
#include <2geom/circle.h>
#include <2geom/piecewise.h>
#include <2geom/d2.h>
#include <2geom/sbasis.h>
#include <2geom/pathvector.h>
#include <2geom/sbasis-to-bezier.h>
#include <2geom/intersection-graph.h>
int main() {
// Circle with radius 10
Geom::Circle c(0, 0, 10);
Geom::D2<Geom::SBasis> d2 = c.toSBasis();
Geom::Piecewise<Geom::D2<Geom::SBasis> > pw;
pw.push_seg(d2);
double area;
Geom::Point p;
Geom::centroid(pw, p, area);
// area should be around 3.14159 * 10 * 10
std::cout << "area: " << area << std::endl;
Geom::PathVector container = Geom::path_from_piecewise(pw, Geom::EPSILON);
// Circle with radius 5
Geom::Circle c2(0, 0, 5);
Geom::Piecewise<Geom::D2<Geom::SBasis> > cd;
cd.push_seg(c2.toSBasis());
Geom::PathVector contained = Geom::path_from_piecewise(cd, Geom::EPSILON);
Geom::PathIntersectionGraph pig(container, contained);
Geom::PathVector exclusion = pig.getAminusB();
auto ex = paths_to_pw(exclusion);
Geom::centroid(ex, p, area);
// area should be around (3.14159 * 10 * 10) - (3.14159 * 5 * 5)
std::cout << "area: " << area << std::endl;
return 0;
}