编译
最近在做可行驶区域后处理时,需要用到多边形面积计算与去重,c++比较适合的库,当属Boost了,其中有Geometry部分,专门用于解决几何问题。首先从github下载源码编译:
git clone https://github.com/boostorg/boost
我下载时的最新版本时1.77.0
下载之后进入boost目录,依次运行:
$ ./bootstrap.sh --prefix=path/to/installation/prefix
$ ./b2 install
prefix的值是你希望安装boost的路径, 不开启此参数的话默认安装在/usr/local/include/boost目录下。
其中boost的动态链接库还是安装在/usr/local/lib中。
测试
测试代码:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
因为这里只依赖头了文件的模块,我们就直接用命令行编译了:
$ g++ test.cpp -o test -I /usr/local/include/boost
执行 ./test 测试, 输入一个数, 返回这个数乘3的值。
再测试需要用到二进制库的功能模块:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << std::endl;
return 0;
}
这里我们用CmakeList.txt来编译:
cmake_minimum_required(VERSION 3.14)
project(test)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost)
find_package(Boost COMPONENTS system filesystem)
message(Boost COMPONENTS system filesystem)
include_directories(/usr/local/include/boost/)
add_executable(test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES})
测试结果输出:
Usage: tut1 path
最后我再测试了官网上的另一个示例:
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
namespace bg = boost::geometry;
int main()
{
// Calculate the area of a cartesian polygon
bg::model::polygon<bg::model::d2::point_xy<double> > poly;
bg::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly);
double area = bg::area(poly);
std::cout << "Area: " << area << std::endl;
// Calculate the area of a spherical equatorial polygon
bg::model::polygon<bg::model::point<float, 2, bg::cs::spherical_equatorial<bg::degree> > > sph_poly;
bg::read_wkt("POLYGON((0 0,0 45,45 0,0 0))", sph_poly);
area = bg::area(sph_poly);
std::cout << "Area: " << area << std::endl;
return 0;
}
得到结果:
Area: 16
Area: 0.339837
说明安装已完成。Get。