(六) geometry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <ros/ros.h>
#include <boost/assign.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/algorithms/transform.hpp>
#include <list>
using namespace std;
namespace bg = boost::geometry;
namespace bgi = bg::index;

using Point = bg::model::point<double, 2, bg::cs::cartesian>;
using Box = bg::model::box<Point>;
using Line = bg::model::linestring<Point>;
// 线段
using Segment = bg::model::segment<Point>;


int main(int argc, char** argv)
{
ros::init(argc, argv, "test_boost");
ros::NodeHandle nh;
Point pt0(100, 100);
Point pt1(200, 200);
// 点的间距
cout << "distance from pt0 to pt1: "<< bg::distance(pt0, pt1) << endl;

Segment s1(Point(0, 100), Point(100, 0));
Segment s2(Point(0, 0), Point(120, 120));
// 点到线段的距离,垂足不一定在线段上
cout << "distance from pt0 to s1: " << bg::distance(pt0, s1) << endl;
// 两线段是否相交
cout << "Is segment1 and segment2 intersect: " << bg::intersects(s1, s2) << endl;

std::list<Point> points;
// 两线段的交点
bg::intersection(s1, s2, points);
for(auto p : points)
cout << bg::dsv(p) << " ";
cout << endl;

// 判断box是否相交
Box rc(Point(0, 0), Point(200, 200));
Box rc0(Point(250, 250), Point(450, 450));
Box rc1(Point(100, 100), Point(300, 300));

cout << "rc and rc0 intersect: " << bg::intersects(rc, rc0) <<endl;
cout << "rc and rc1 intersect: " << bg::intersects(rc, rc1) <<endl;

// 求外接矩形Bounding Box
Line line = { {1,1},{2,0},{3,2} };
Box box;
bg::envelope(line, box);
std::cout << bg::dsv(line) << "'s AABB is " << bg::dsv(box) << std::endl;

return 0;
}

注意直线的初始化和线段是不一样的

参考:
SLAM本质剖析-Boost
boost::geometry简介