Centroid area calculation incorrect

Bug #1564588 reported by Aidan Delaney on 2016-03-31
This bug affects 1 person
Affects Status Importance Assigned to Milestone

Bug Description

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;

  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;
  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;

Revision history for this message
Patrick Storz (ede123) wrote :
Changed in lib2geom:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.