Boost Geometry编译与测试

Boost Geometry编译与测试,第1张

Boost Geometry编译测试 编译

最近在做可行驶区域后处理时,需要用到多边形面积计算与去重,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   
#include   
#include   
#include   
      
int main()  
{  
    using namespace boost::lambda;  
    typedef std::istream_iterator 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   
   #include   
     
   using namespace boost::filesystem;  
     
   int main(int argc, char *argv[])  
   {  
       if (argc < 2) {  
           std::cout << "Usage: tut1 pathn";  
           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 

#include 
#include 
#include 

namespace bg = boost::geometry;

int main()
{
    // Calculate the area of a cartesian polygon
    bg::model::polygon > 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 > > 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。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5098875.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-16
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存