CGAL-线面、线线、点线求交、输出交点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/OOFFrankDura/article/details/82430434

综述

当然对于求交、碰撞检测而言,很多人可能选择AABB、OBB等等。但是很多时候,可能并不需要那么复杂的设计。这里针对简单的物体模型(点、线、面),直接使用cgal原始计算方法给出结果以及代码。

需要注意的是:不只是线线、线面对于其他物体相交只要是满足:CGAL::Exact_predicates_exact_constructions_kernel下的简单几何体皆可处理

此外数学原理部分时间原理不再给出,其实也比较基础。可以推导推导放松一下。

环境

mac OS
clion
release模式

代码

线线交点

//山东大学 计算机基地Frankdura
//2018.9.4
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/intersections.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef K::Point_3 Point_3;
typedef K::Segment_3 Segment_3;
typedef K::Plane_3 Plane_3;
typedef K::Intersect_3 Intersect_3;
int main()
{
    Segment_3 seg(Point_3(0,0,0), Point_3(2,2,0));
    Segment_3 lin(Point_3(1,2,0), Point_3(1,0,0));


    CGAL::cpp11::result_of<Intersect_3(Segment_3, Segment_3)>::type
            result = intersection(seg, lin);
        if (result) {
        if (const Segment_3* s = boost::get<Segment_3>(&*result)) {
            std::cout << *s << std::endl;
        } else {
            const Point_3* p = boost::get<Point_3 >(&*result);
            std::cout << (*p) << std::endl;
        }
    }
    return 0;
}

线面交点

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/intersections.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef K::Point_3 Point_3;
typedef K::Segment_3 Segment_3;
typedef K::Plane_3 Plane_3;
typedef K::Intersect_3 Intersect_3;
int main()
{
    Segment_3 seg(Point_3(0,0,0), Point_3(4,4,4));
    Plane_3 lin (Point_3(1,0,0),Point_3(0,1,0),Point_3(0,0,1));
    CGAL::cpp11::result_of<Intersect_3(Segment_3, Segment_3)>::type
            result = intersection(seg, lin);
        if (result) {
        if (const Segment_3* s = boost::get<Segment_3>(&*result)) {
            std::cout << *s << std::endl;
        } else {
            const Point_3* p = boost::get<Point_3 >(&*result);
            std::cout << (*p) << std::endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/OOFFrankDura/article/details/82430434