基本思想:在工程中使用Clion编译器去调用boost的函数实现图形的并集处理,所以需要预先编译 一下boost库,以便可以在Clion中调用 <前提已经安装了mingw32编译器>
一、首先下载boost的源代码
首先下载最新版板的boost版本
https://www.boost.org/users/history/version_1_77_0.html
二、然后进行编译
F:\sxj\20210911>cd boost_1_77_0
F:\sxj\20210911\boost_1_77_0>bootstrap gcc
'bootstrap' is not recognized as an internal or external command,
operable program or batch file.
F:\sxj\20210911\boost_1_77_0>ls
'ls' is not recognized as an internal or external command,
operable program or batch file.
F:\sxj\20210911\boost_1_77_0>dir
Volume in drive F is 新加卷
Volume Serial Number is 502C-22B3
Directory of F:\sxj\20210911\boost_1_77_0
09/11/2021 09:19 AM <DIR> .
09/11/2021 09:19 AM <DIR> ..
09/11/2021 09:19 AM <DIR> boost
09/11/2021 09:20 AM <DIR> libs
0 File(s) 0 bytes
4 Dir(s) 23,627,280,384 bytes free
F:\sxj\20210911\boost_1_77_0>bootstrap gcc
Building Boost.Build engine
LOCALAPPDATA=C:\Users\PHILIPS\AppData\Local
Found with vswhere D:\VS2019
Found with vswhere D:\VS2019
Call_If_Exists "D:\VS2019\Common7\Tools\..\..\VC\Auxiliary\Build\vcvarsall.bat" AMD64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.6.5
** Copyright (c) 2020 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
###
### Using 'vc142' toolset.
###
F:\sxj\20210911\boost_1_77_0\tools\build\src\engine>"cl" /nologo /MP /MT /TP /Feb2 /wd4996 /O2 /GL /EHsc -DNDEBUG builtins.cpp class.cpp command.cpp compile.cpp constants.cpp cwd.cpp debug.cpp debugger.cpp execcmd.cpp execnt.cpp execunix.cpp filent.cpp filesys.cpp fileunix.cpp frames.cpp function.cpp glob.cpp hash.cpp hcache.cpp hdrmacro.cpp headers.cpp jam.cpp jamgram.cpp lists.cpp make.cpp make1.cpp md5.cpp mem.cpp modules.cpp native.cpp object.cpp option.cpp output.cpp parse.cpp pathnt.cpp pathsys.cpp pathunix.cpp regexp.cpp rules.cpp scan.cpp search.cpp jam_strings.cpp startup.cpp subst.cpp sysinfo.cpp timestamp.cpp variable.cpp w32_getreg.cpp modules/order.cpp modules/path.cpp modules/property-set.cpp modules/regex.cpp modules/sequence.cpp modules/set.cpp /link kernel32.lib advapi32.lib user32.lib
command.cpp
class.cpp
compile.cpp
builtins.cpp
constants.cpp
cwd.cpp
debug.cpp
debugger.cpp
execcmd.cpp
execnt.cpp
execunix.cpp
filent.cpp
filesys.cpp
fileunix.cpp
frames.cpp
function.cpp
glob.cpp
hash.cpp
hcache.cpp
hdrmacro.cpp
headers.cpp
jam.cpp
jamgram.cpp
lists.cpp
make.cpp
make1.cpp
md5.cpp
mem.cpp
modules.cpp
native.cpp
object.cpp
option.cpp
output.cpp
parse.cpp
pathnt.cpp
pathsys.cpp
pathunix.cpp
regexp.cpp
rules.cpp
scan.cpp
search.cpp
jam_strings.cpp
startup.cpp
subst.cpp
sysinfo.cpp
timestamp.cpp
variable.cpp
w32_getreg.cpp
order.cpp
path.cpp
property-set.cpp
regex.cpp
sequence.cpp
set.cpp
正在生成代码
已完成代码的生成
F:\sxj\20210911\boost_1_77_0\tools\build\src\engine>dir *.exe
Volume in drive F is 新加卷
Volume Serial Number is 502C-22B3
Directory of F:\sxj\20210911\boost_1_77_0\tools\build\src\engine
09/11/2021 09:22 AM 526,848 b2.exe
1 File(s) 526,848 bytes
0 Dir(s) 23,111,815,168 bytes free
F:\sxj\20210911\boost_1_77_0\tools\build\src\engine>copy /b .\b2.exe .\bjam.exe
1 file(s) copied.
Generating Boost.Build configuration in project-config.jam for gcc...
Bootstrapping is done. To build, run:
.\b2
To adjust configuration, edit 'project-config.jam'.
Further information:
- Command line help:
.\b2 --help
- Getting started guide:
http://boost.org/more/getting_started/windows.html
- Boost.Build documentation:
http://www.boost.org/build/
F:\sxj\20210911\boost_1_77_0>b2 -j8 --build-dir=.\build --build-type=complete threading=multi link=shared,static address-model=64 toolset=gcc stage
......
ty-hidden\libboost_log_setup-mgw8-mt-s-x64-1_77.a
1 file(s) copied.
common.copy F:\sxj\20210911\boost_1_77_0\stage\lib\cmake\boost_log_setup-1.77.0\libboost_log_setup-variant-mgw8-mt-s-x64-1_77-static.cmake
build\boost\bin.v2\libs\log\build\gcc-8\release\link-static\runtime-link-static\threadapi-win32\threading-multi\visibility-hidden\libboost_log_setup-variant-mgw8-mt-s-x64-1_77-static.cmake
1 file(s) copied.
...updated 3791 targets...
F:\sxj\20210911\boost_1_77_0>
生成的动静态库文件 ;
我使用的命令是只生成静态包就行
F:\sxj\20210911\boost_1_77_0>b2 -j8 --build-dir=.\build --build-type=complete threading=multi link=static address-model=64 toolset=gcc stage
生成静态库
三、因为生成的静态库太多了,我合并了一下
python脚本
# 将多个静态包打包成一个静态包的脚本
import os
import shutil
staticDir=r"F:\sxj\20210911\boost_1_77_0\stage\lib"
for root,dirs,files in os.walk(staticDir):
for file in files:
(filepath, tempfilename) = os.path.split(os.path.join(root,file))
(filename, extension) = os.path.splitext(tempfilename)
if tempfilename.endswith(".a"):
command= "".join(["ar"," ","x"," ",os.path.join(root,file)])
print(command)
os.chdir(filepath)
os.system(command)
#-----------------解压o----------------------
os.chdir(staticDir)
command="".join(["ar"," ","crsv"," ","../boost.a"," ",os.path.join(staticDir,"*.o")])
os.chdir(staticDir)
os.system(command)
合并的文件为 F:\sxj\20210911\boost_1_77_0\stage\boost.a
四、然后测试一下
Clion工程目录(其中boost文件来自源码根目录的boost文件夹)
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(untitled2)
include_directories(${CMAKE_SOURCE_DIR})
set(CMAKE_CXX_STANDARD 14)
add_library(libboost STATIC IMPORTED)
set_target_properties(libboost PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/boost.a)
add_executable(untitled2 main.cpp)
target_link_libraries(untitled2 libboost )
代码
//boost多边形交集、并集
//交集:http://www.boost.org/doc/libs/1_56_0/libs/geometry/doc/html/geometry/reference/algorithms/intersection.html
//
//并集:http://www.boost.org/doc/libs/1_56_0/libs/geometry/doc/html/geometry/reference/algorithms/union_.html
#include <iostream>
#include <vector>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/foreach.hpp>
int main()
{
typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;
polygon green, blue;
boost::geometry::read_wkt(
"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);
boost::geometry::read_wkt(
"POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);
std::vector<polygon> output;
boost::geometry::union_(green, blue, output);
int i = 0;
std::cout << "green || blue:" << std::endl;
BOOST_FOREACH(polygon const& p, output)
{
std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
}
return 0;
}
测试一下
F:\untitled2\cmake-build-debug\untitled2.exe
green || blue:
0: 5.64795
Process finished with exit code 0
四、测试代码
#include <fstream>
#include <iostream>
#include <boost/geometry.hpp> // read_wkt
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/algorithms/union.hpp>
using PointType = boost::geometry::model::d2::point_xy<double>;
using PolygonType = boost::geometry::model::polygon<PointType>;
using MultiPolygonType = boost::geometry::model::multi_polygon<PolygonType>;
template <typename TPolygon>
void WritePolygonsToSVG(const std::vector<TPolygon>& polygons, const std::string& filename)
{
std::ofstream svg(filename);
boost::geometry::svg_mapper<PointType> mapper(svg, 400, 400);
for(unsigned int i = 0; i < polygons.size(); ++i) {
mapper.add(polygons[i]);
mapper.map(polygons[i], "fill:rgb(255,128,0);stroke:rgb(0,0,100);stroke-width:1");
}
}
int main(int, char**)
{
/// Create two polygons
PolygonType singlePolygon1;
PolygonType singlePolygon2;
boost::geometry::read_wkt("POLYGON((-52.8018 -26.744,-57.5465 -27.9916,-62.2844 -29.2642,-63.19 -26.066,-57.564 -24.5243,-53.7273 -23.3394,-52.8018 -26.744))", singlePolygon1);
boost::geometry::read_wkt("POLYGON((-56.9695 -33.6407,-58.009 -33.0132,-58.5119 -32.8011,-59.0182 -32.6562,-59.5392 -32.5779,-60.0858 -32.5658,"
"-61.3005 -32.7384,-62.2844 -29.2642,-59.997 -28.7264,-58.0701 -28.125,-56.7078 -27.7124,-55.3109 -27.4556,-55.0955 -27.4599,"
"-54.8762 -27.503,-54.6545 -27.5806,-54.4318 -27.6887,-53.9893 -27.98,-53.5601 -28.344,-52.7879 -29.1592,-52.207 -29.8722,-56.9695 -33.6407))", singlePolygon2);
boost::geometry::correct(singlePolygon1);
boost::geometry::correct(singlePolygon2);
/// Run union and check validity (should fail check)
MultiPolygonType unionResult;
boost::geometry::union_(singlePolygon1, singlePolygon2, unionResult);
boost::geometry::validity_failure_type failure_type;
if(!boost::geometry::is_valid(unionResult, failure_type)) {
std::cout << "Result of union operation is not valid! " << failure_type << std::endl; // failure_type is 21 == failure_self_intersections
}
// Put these into the same type so that they can be passed to WritePolygonsToSVG
MultiPolygonType polygon1;
polygon1.push_back(singlePolygon1);
MultiPolygonType polygon2;
polygon2.push_back(singlePolygon2);
std::vector<MultiPolygonType> polygons = {polygon1, polygon2, unionResult};
WritePolygonsToSVG(polygons, "allPolygons.svg");
return EXIT_SUCCESS;
}
结果是svg格式
五、测试代码将交集图形的坐标打印出来
#include <iostream>
#include <vector>
#include <fstream>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
#include <boost/foreach.hpp>
#include <boost/foreach.hpp>
#include <typeinfo>
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
using namespace std;
typedef boost::geometry::model::polygon<boost::tuple<int, int> > Polygon;
int main(){
stringstream outstream;
Polygon green, blue;
boost::geometry::read_wkt("POLYGON((0 0,0 9,9 9,9 0,0 0))", green);
boost::geometry::read_wkt("POLYGON((2 2,2 9,9 9,9 2,2 2))", blue);
boost::geometry::correct(green);
boost::geometry::correct(blue);
std::deque<Polygon> output;
boost::geometry::union_(green, blue, output);
BOOST_FOREACH(Polygon const& p, output)
{
outstream<<boost::geometry::dsv(p,","," ","","","","");
std::cout<<boost::geometry::dsv(p,","," ","","","","") << std::endl;
std::cout <<boost::geometry::area(p) << std::endl;
}
string str;
while(outstream>>str){
std::vector<std::string> results;
boost::split_regex(results, str, boost::regex(","));
cout<<"x="<<results[0]<<" y="<<results[1]<<endl;
}
return 0;
}
测试结果
G:\processSVG\cmake-build-debug\processSVG.exe
9,9 9,2 9,0 0,0 0,9 9,9
81
x=9 y=9
x=9 y=2
x=9 y=0
x=0 y=0
x=0 y=9
x=9 y=9
Process finished with exit code 0
补充linux打包方式
axel -n 100 https://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/boost_1_77_0.tar.bz2
tar -jxvf boost_1_77_0.tar.bz2
cd boost_1_54_0
./bootstrap.sh --prefix=./build
./b2 install --with=all
python代码
import os
import shutil
staticDir=r"/media/teamhd/HBSpace/sxj731533730/boost_1_77_0/build/lib"
for root,dirs,files in os.walk(staticDir):
for file in files:
(filepath, tempfilename) = os.path.split(os.path.join(root,file))
(filename, extension) = os.path.splitext(tempfilename)
if tempfilename.endswith(".a"):
command= "".join(["ar"," ","x"," ",os.path.join(root,file)])
print(command)
os.chdir(filepath)
os.chdir(staticDir)
command="".join(["ar"," ","crsv"," ","../boost.a"," ",os.path.join(staticDir,"*.0")])
os.chdir(staticDir)
os.system(command)