Skip to content

volume values are off a bit for large buildings #166

@hugoledoux

Description

@hugoledoux

double calculate_volume(const TriangleCollection& triangle_collection) {

This function assumes that the 4th point is the origin (0, 0, 0), but it's in France, and thus small rounding errors accumulate, and you do not get the "true" volume.

The solution is to define the 4th point as something close to the triangle (or its centroid) and thus something like this:

  double calculate_volume(const TriangleCollection& triangle_collection) {
    Vector centroid(0, 0, 0);
    size_t count = 0;
    for (const auto& t : triangle_collection) {
      for (int i = 0; i < 3; ++i) {
        centroid = centroid + Vector(t[i][0], t[i][1], t[i][2]);
        ++count;
      }
    }
    centroid /= count;

    double sum = 0;
    for (const auto& t : triangle_collection) {
      auto a = Vector(t[0][0], t[0][1], t[0][2]) - centroid;
      auto b = Vector(t[1][0], t[1][1], t[1][2]) - centroid;
      auto c = Vector(t[2][0], t[2][1], t[2][2]) - centroid;
      sum += CGAL::scalar_product(a, CGAL::cross_product(b, c));
    }
    return sum / 6;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions