栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Boost Geometry编译与测试

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号