gpt4 book ai didi

c++ - Boost::Geometry - 在 3d 空间中查找 2d 多边形的面积?

转载 作者:行者123 更新时间:2023-11-30 02:44:24 26 4
gpt4 key购买 nike

我正在尝试获取 3d 空间中 2d 多边形的面积。有没有办法通过 Boost::Geometry 做到这一点?这是我的实现,但它始终返回 0:

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

namespace bg = boost::geometry;

typedef bg::model::point<double, 3, bg::cs::cartesian> point3d;

int main()
{
bg::model::multi_point<point3d> square;
bg::read_wkt("MULTIPOINT((0 0 0), (0 2 0), (0 2 2), (0 0 2), (0 0 0))", square);
double area = bg::area(square);
std::cout << "Area: " << area << std::endl;

return 0;
}

UPD:实际上,我对简单的 2d 多点正方形有同样的问题:

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

namespace bg = boost::geometry;

typedef bg::model::point<double, 2, bg::cs::cartesian> point2d;

int main()
{
bg::model::multi_point<point2d> square;
bg::read_wkt("MULTIPOINT((0 0), (2 0), (2 2), (0 2))", square);
double area = bg::area(square);
std::cout << "Area: " << area << std::endl;

return 0;
}

结果如下:

$ ./test_area
Area: 0

UPD:看起来像 boost::geometry 中的面积计算仅适用于二维多边形。

最佳答案

我不希望点的集合有一个区域。你需要相当于一个 model::polygon<poind3d>但目前似乎不受支持。

如果保证点共面并且线段彼此不相交,您可以将多边形分解为一系列三角形,并根据以下公式用一点线性代数计算面积对于三角形的面积:
enter image description here

在非凸多边形的情况下,需要调整面积之和以减去多边形以外的区域。实现这一点的最简单方法是使用三角形的有符号区域,包括右手三角形的正贡献和左手三角形的负贡献:
enter image description here

请注意,似乎有一些计划包括 cross_product Boost 中的实现,但从 1.56 版开始它似乎并未包含在内。以下替换应该可以解决您的用例问题:

point3d cross_product(const point3d& p1, const point3d& p2)
{
double x = bg::get<0>(p1);
double y = bg::get<1>(p1);
double z = bg::get<2>(p1);
double u = bg::get<0>(p2);
double v = bg::get<1>(p2);
double w = bg::get<2>(p2);
return point3d(y*w-z*v, z*u-x*w, x*v-y*u);
}
point3d cross_product(const bg::model::segment<point3d>& p1
, const bg::model::segment<point3d>& p2)
{
point3d v1(p1.second);
point3d v2(p2.second);
bg::subtract_point(v1, p1.first);
bg::subtract_point(v2, p2.first);

return cross_product(v1, v2);
}

然后可以用以下方法计算面积:

// compute the are of a collection of 3D points interpreted as a 3D polygon
// Note that there are no checks as to whether or not the points are
// indeed co-planar.
double area(bg::model::multi_point<point3d>& polygon)
{
if (polygon.size()<3) return 0;

bg::model::segment<point3d> v1(polygon[1], polygon[0]);
bg::model::segment<point3d> v2(polygon[2], polygon[0]);
// Compute the cross product for the first pair of points, to handle
// shapes that are not convex.
point3d n1 = cross_product(v1, v2);
double normSquared = bg::dot_product(n1, n1);
if (normSquared > 0)
{
bg::multiply_value(n1, 1.0/sqrt(normSquared));
}
// sum signed areas of triangles
double result = 0.0;
for (size_t i=1; i<polygon.size(); ++i)
{
bg::model::segment<point3d> v1(polygon[0], polygon[i-1]);
bg::model::segment<point3d> v2(polygon[0], polygon[i]);

result += bg::dot_product(cross_product(v1, v2), n1);
}
result *= 0.5;
return abs(result);
}

关于c++ - Boost::Geometry - 在 3d 空间中查找 2d 多边形的面积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com