用成对向量提升几何区域?

Posted

技术标签:

【中文标题】用成对向量提升几何区域?【英文标题】:boost geometry area with a vector of pairs? 【发布时间】:2021-04-08 21:39:12 【问题描述】:

有什么方法可以让boost::geometry::area 使用成对向量?它在这里说 https://www.boost.org/doc/libs/1_75_0/libs/geometry/doc/html/geometry/reference/algorithms/area/area_1.html 它应该与 MultiPoint 一起使用。所以我打电话给BOOST_GEOMETRY_REGISTER_MULTI_POINT——它适用于convex_hull,但它不适用于area。或者我是否必须创建一个多边形并将点附加到它,如下所示: https://www.boost.org/doc/libs/1_75_0/libs/geometry/doc/html/geometry/reference/models/model_polygon.html

这是我的代码:

#include <iostream>
#include <cstdlib>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/multi/geometries/register/multi_point.hpp>

BOOST_GEOMETRY_REGISTER_POINT_2D(decltype(std::pair<double, double>), double, cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_MULTI_POINT(decltype(std::vector<std::pair<double, double>>))

using MultiPoint = std::vector<std::pair<double, double>>;
MultiPoint getHull(const MultiPoint& points)

  MultiPoint hull;
  boost::geometry::convex_hull(points, hull);
  return hull; // This returns a vector of pairs of points respresenting a hull

double getHullArea(const MultiPoint& points)

  return boost::geometry::area(points); // This always return 0


int main()

    // Unit square
    auto points = MultiPoint0, 0, 0, 1, 1, 1, 1, 0, 0,0;
    auto hull = getHull(points); // This works as expected
    auto area = getHullArea(hull); // This always return 0

【问题讨论】:

【参考方案1】:

是的,它支持多点。你会得到记录在案的behaviour:

所以,你得到的面积是 0,正如你所期望的那样。

很明显,你想适应一个面几何。假设您的配对是Points,形成Ring。

using Point = std::pair<double, double>;
using Ring  = std::vector<Point>;

BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_RING(Ring)

现在它可以正常工作了:

Live On Coliru

#include <iostream>
#include <cstdlib>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
namespace bg = boost::geometry;

using Point = std::pair<double, double>;
using Ring  = std::vector<Point>;

BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_RING(Ring)

Ring getHull(const Ring& points) 
    Ring hull;
    bg::convex_hull(points, hull);
    return hull;


double getArea(const Ring& points) 
    return bg::area(points);


int main() 
    for (Ring points : 
             Ring0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
             Ring0, 0, 0, 2, 3, 2, 3, 1, 1, 1, 1, 0, 0, 0,
         )
    
        std::cout << "-----\nInput:\t" << bg::wkt(points) << "\n";
        if (std::string reason; !bg::is_valid(points)) 
            std::cout << "Input:\t" << reason << "\n";
            bg::correct(points);
            std::cout << bg::wkt(points) << "\n";
        

        std::cout << "Hull:\t"      << bg::wkt(getHull(points)) << "\n";
        std::cout << "Area:\t"      << getArea(points)          << "\n";
        std::cout << "Hull Area:\t" << getArea(getHull(points)) << "\n";
    

打印

-----
Input:  POLYGON((0 0,0 1,1 1,1 0,0 0))
Hull:   POLYGON((0 0,0 1,1 1,1 0,0 0))
Area:   1
Hull Area:      1
-----
Input:  POLYGON((0 0,0 2,3 2,3 1,1 1,1 0,0 0))
Hull:   POLYGON((0 0,0 2,3 2,3 1,1 0,0 0))
Area:   4
Hull Area:      5

如你所见,我添加了一个更有趣的例子:

【讨论】:

非常感谢!这是有道理的。谢谢你的耐心,sehe :)

以上是关于用成对向量提升几何区域?的主要内容,如果未能解决你的问题,请参考以下文章

提升几何/空间查询形状

提升几何相交会产生奇怪的结果

提升几何:从多个点组成多边形

矩阵乘法的几何意义

如何使用 MPI 广播提升向量?

填充提升向量或矩阵