C++ 几何计算库

news2024/9/23 9:37:27
代码

#include <iostream>
#include <list>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/AABB_segment_primitive.h>
#include <CGAL/Polygon_2.h>


typedef CGAL::Simple_cartesian<double> K;


// custom point type
struct My_point {
    double m_x;
    double m_y;
    double m_z;

    My_point(const double x,
        const double y,
        const double z)
        : m_x(x), m_y(y), m_z(z) {}
};

// custom triangle type with
// three pointers to points
struct My_triangle {
    My_point* m_pa;
    My_point* m_pb;
    My_point* m_pc;

    My_triangle(My_point* pa,
        My_point* pb,
        My_point* pc)
        : m_pa(pa), m_pb(pb), m_pc(pc) {}
};

// the custom triangles are stored into a vector
typedef std::vector<My_triangle>::const_iterator Iterator;

// The following primitive provides the conversion facilities between
// the custom triangle and point types and the CGAL ones
struct My_triangle_primitive {
public:

    // this is the type of data that the queries returns. For this example
    // we imagine that, for some reasons, we do not want to store the iterators
    // of the vector, but raw pointers. This is to show that the Id type
    // does not have to be the same as the one of the input parameter of the
    // constructor.
    typedef const My_triangle* Id;

    // CGAL types returned
    typedef K::Point_3    Point; // CGAL 3D point type
    typedef K::Triangle_3 Datum; // CGAL 3D triangle type

private:
    Id m_pt; // this is what the AABB tree stores internally

public:
    My_triangle_primitive() {} // default constructor needed

    // the following constructor is the one that receives the iterators from the
    // iterator range given as input to the AABB_tree
    My_triangle_primitive(Iterator it)
        : m_pt(&(*it)) {}

    const Id& id() const { return m_pt; }

    // utility function to convert a custom
    // point type to CGAL point type.
    Point convert(const My_point* p) const
    {
        return Point(p->m_x, p->m_y, p->m_z);
    }

    // on the fly conversion from the internal data to the CGAL types
    Datum datum() const
    {
        return Datum(convert(m_pt->m_pa),
            convert(m_pt->m_pb),
            convert(m_pt->m_pc));
    }

    // returns a reference point which must be on the primitive
    Point reference_point() const
    {
        return convert(m_pt->m_pa);
    }
};

/*
自定义KDTree测试,点相交
*/
int testKDTree()
{
    typedef CGAL::AABB_traits<K, My_triangle_primitive> My_AABB_traits;
    typedef CGAL::AABB_tree<My_AABB_traits> MyTree;
    typedef K::FT FT;

    My_point a(1.0, 0.0, 0.0);
    My_point b(0.0, 1.0, 0.0);
    My_point c(0.0, 0.0, 1.0);
    My_point d(0.0, 0.0, 0.0);

    std::vector<My_triangle> triangles;
    triangles.push_back(My_triangle(&a, &b, &c));
    triangles.push_back(My_triangle(&a, &b, &d));
    triangles.push_back(My_triangle(&a, &d, &c));

    // constructs AABB tree
    MyTree tree(triangles.begin(), triangles.end());

    // counts #intersections
    K::Ray_3 ray_query(K::Point_3(1.0, 0.0, 0.0), K::Point_3(0.0, 1.0, 0.0));
    std::cout << tree.number_of_intersected_primitives(ray_query)
        << " intersections(s) with ray query" << std::endl;

    // computes closest point
    K::Point_3 point_query(2.0, 2.0, 2.0);
    K::Point_3 closest_point = tree.closest_point(point_query);
    std::cerr << "closest point is: " << closest_point << std::endl;

    FT sqd = tree.squared_distance(point_query);
    std::cout << "squared distance: " << sqd << std::endl;
    return EXIT_SUCCESS;

/* 输出
3 intersections(s) with ray query
closest point is: 0.333333 0.333333 0.333333
*/
}

/*
光线追踪,面相交
*/
void testGlyph() {
    typedef K::FT FT;
    typedef K::Segment_3 Segment;
    typedef K::Point_3 Point;

    typedef std::list<Segment> SegmentRange;
    typedef SegmentRange::const_iterator Iterator;
    typedef CGAL::AABB_segment_primitive<K, Iterator> Primitive;
    typedef CGAL::AABB_traits<K, Primitive> Traits;
    typedef CGAL::AABB_tree<Traits> Tree;
    typedef Tree::Point_and_primitive_id Point_and_primitive_id;

    Point a(0.0, 0.0, 1);
    Point b(2.0, 1.0, 1);
    Point c(3.0, 4.0, 1);
    Point d(1.0, 6.0, 1);
    Point e(-1.0, 3.0, 1);

    std::list<Segment> seg;
    seg.push_back(Segment(a, b));
    seg.push_back(Segment(b, c));
    seg.push_back(Segment(c, d));
    seg.push_back(Segment(d, e));
    seg.push_back(Segment(e, a));

    // constructs the AABB tree and the internal search tree for
    // efficient distance computations.
    Tree tree(seg.begin(), seg.end());
    tree.build();

    tree.accelerate_distance_queries();

    // counts #intersections with a segment query
    Segment segment_query(Point(1.0, 0.0, 1), Point(0.0, 7.0, 1));
    std::cout << tree.number_of_intersected_primitives(segment_query)
        << " intersections(s) with segment" << std::endl;

    // computes the closest point from a point query
    Point point_query(1.5, 3.0, 1);
    Point closest = tree.closest_point(point_query);
    std::cerr << "closest point is: " << closest << std::endl;

    Point_and_primitive_id id = tree.closest_point_and_primitive(point_query);
    std::cout << id.second->source() << " " << id.second->target() << std::endl;
}

/*
测试布尔运算
*/

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Polygon_set_2.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/Intersections_3/Line_3_Line_3.h>
//-----------------------------------------------------------------------------
// Pretty-print a CGAL polygon.
//
template<class Kernel, class Container>
void print_polygon(const CGAL::Polygon_2<Kernel, Container>& P)
{
    typename CGAL::Polygon_2<Kernel, Container>::Vertex_const_iterator  vit;

    std::cout << "[ " << P.size() << " vertices:";
    for (vit = P.vertices_begin(); vit != P.vertices_end(); ++vit)
        std::cout << " (" << *vit << ')';
    std::cout << " ]" << std::endl;

    return;
}

//-----------------------------------------------------------------------------
// Pretty-print a polygon with holes.
//
template<class Kernel, class Container>
void print_polygon_with_holes
(const CGAL::Polygon_with_holes_2<Kernel, Container>& pwh)
{
    if (!pwh.is_unbounded())
    {
        std::cout << "{ Outer boundary = ";
        print_polygon(pwh.outer_boundary());
    }
    else
        std::cout << "{ Unbounded polygon." << std::endl;

    typename CGAL::Polygon_with_holes_2<Kernel, Container>::
        Hole_const_iterator  hit;
    unsigned int                                                     k = 1;

    std::cout << "  " << pwh.number_of_holes() << " holes:" << std::endl;
    for (hit = pwh.holes_begin(); hit != pwh.holes_end(); ++hit, ++k)
    {
        std::cout << "    Hole #" << k << " = ";
        print_polygon(*hit);
    }
    std::cout << " }" << std::endl;

    return;
}

void testBoolOpeation() {
    typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
    typedef Kernel::Point_2                                   Point_2;
    typedef CGAL::Polygon_2<Kernel>                           Polygon_2;
    typedef CGAL::Polygon_with_holes_2<Kernel>                Polygon_with_holes_2;
    typedef CGAL::Polygon_set_2<Kernel>                       Polygon_set_2;
    typedef std::list<Polygon_with_holes_2>                   Pwh_list_2;
    typedef CGAL::Line_3<K>           Line;
    typedef CGAL::Point_3<K>          Point;

    // Construct the two initial polygons and the clipping rectangle.
    Polygon_2 P;
    P.push_back(Point_2(0, 1));
    P.push_back(Point_2(2, 0));
    P.push_back(Point_2(1, 1));
    P.push_back(Point_2(2, 2));

    Polygon_2 Q;
    Q.push_back(Point_2(3, 1));
    Q.push_back(Point_2(1, 2));
    Q.push_back(Point_2(2, 1));
    Q.push_back(Point_2(1, 0));

    Polygon_2 rect;
    rect.push_back(Point_2(0, 0));
    rect.push_back(Point_2(3, 0));
    rect.push_back(Point_2(3, 2));
    rect.push_back(Point_2(0, 2));

    // Perform a sequence of operations.
    Polygon_set_2 S;
    S.insert(P);
    S.join(Q);                   // Compute the union of P and Q.
    S.complement();               // Compute the complement.
    S.intersection(rect);        // Intersect with the clipping rectangle.

    // Print the result.
    std::list<Polygon_with_holes_2> res;
    std::list<Polygon_with_holes_2>::const_iterator it;

    std::cout << "The result contains " << S.number_of_polygons_with_holes()
        << " components:" << std::endl;

    S.polygons_with_holes(std::back_inserter(res));
    for (it = res.begin(); it != res.end(); ++it) {
        std::cout << "--> ";
        print_polygon_with_holes(*it);
    }

    // 交集
    if ((CGAL::do_intersect(P, Q)))
        std::cout << "The two polygons intersect in their interior." << std::endl;
    else
        std::cout << "The two polygons do not intersect." << std::endl;

    // union
      // Compute the union of P and Q.
    Polygon_with_holes_2 unionR;
    if (CGAL::join(P, Q, unionR)) {
        std::cout << "The union: ";
        print_polygon_with_holes(unionR);
    }
    else
        std::cout << "P and Q are disjoint and their union is trivial."
        << std::endl;

    // Compute the intersection of P and Q.
    CGAL::intersection(P, Q, std::back_inserter(res));

    // Compute the symmetric difference of P and Q.
    CGAL::symmetric_difference(P, Q, std::back_inserter(res));

    // 直线相交
    const Line l = Line(Point(0, 0, 0), Point(1, 2, 3));
    const Line l_1 = Line(Point(0, 0, 0), Point(-3, -2, -1));
    const CGAL::Object obj1 = CGAL::intersection(l, l_1);
}

/*
凸包检测
*/
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/Delaunay_triangulation_3.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/algorithm.h>
#include <CGAL/convex_hull_3_to_face_graph.h>
void testConvexHull() {
    typedef CGAL::Exact_predicates_inexact_constructions_kernel     K;
    typedef K::Point_3                                              Point_3;
    typedef CGAL::Delaunay_triangulation_3<K>                       Delaunay;
    typedef Delaunay::Vertex_handle                                 Vertex_handle;
    typedef CGAL::Surface_mesh<Point_3>                             Surface_mesh;
    CGAL::Random_points_in_sphere_3<Point_3> gen(100.0);
    std::list<Point_3> points;

    // generate 250 points randomly in a sphere of radius 100.0
    // and insert them into the triangulation
    std::copy_n(gen, 250, std::back_inserter(points));
    Delaunay T;
    T.insert(points.begin(), points.end());

    std::list<Vertex_handle>  vertices;
    T.incident_vertices(T.infinite_vertex(), std::back_inserter(vertices));
    std::cout << "This convex hull of the 250 points has "
        << vertices.size() << " points on it." << std::endl;

    // remove 25 of the input points
    std::list<Vertex_handle>::iterator v_set_it = vertices.begin();
    for (int i = 0; i < 25; i++)
    {
        T.remove(*v_set_it);
        v_set_it++;
    }

    //copy the convex hull of points into a polyhedron and use it
    //to get the number of points on the convex hull
    Surface_mesh chull;
    CGAL::convex_hull_3_to_face_graph(T, chull);

    std::cout << "After removal of 25 points, there are "
        << num_vertices(chull) << " points on the convex hull." << std::endl;
}

void test() {
    testKDTree();
    testGlyph();
    testBoolOpeation();
    testConvexHull();
}
输出
3 intersections(s) with ray query
closest point is: 0.333333 0.333333 0.333333
squared distance: 8.33333
2 intersections(s) with segment
closest point is: 2.55 2.65 1
2 1 1 3 4 1
The result contains 2 components:
--> { Outer boundary = [ 10 vertices: (2 2) (1 2) (0 2) (0 1) (0 0) (1 0) (2 0) (3 0) (3 1) (3 2) ]
  1 holes:
    Hole #1 = [ 12 vertices: (3 1) (1.66667 0.333333) (2 0) (1.5 0.25) (1 0) (1.33333 0.333333) (0 1) (1.33333 1.66667) (1 2) (1.5 1.75) (2 2) (1.66667 1.66667) ]
 }
--> { Outer boundary = [ 4 vertices: (1 1) (1.5 0.5) (2 1) (1.5 1.5) ]
  0 holes:
 }
The two polygons intersect in their interior.
The union: { Outer boundary = [ 12 vertices: (1.33333 0.333333) (1 0) (1.5 0.25) (2 0) (1.66667 0.333333) (3 1) (1.66667 1.66667) (2 2) (1.5 1.75) (1 2) (1.33333 1.66667) (0 1) ]
  1 holes:
    Hole #1 = [ 4 vertices: (1.5 1.5) (2 1) (1.5 0.5) (1 1) ]
 }
This convex hull of the 250 points has 88 points on it.
After removal of 25 points, there are 84 points on the convex hull.

GitHub - CGAL/cgal: The public CGAL repository, see the README below


创作不易,小小的支持一下吧!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1934097.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数学建模(1)

论文&#xff1a;做流程图 论文查重不能高于30% 论文 分模块备战 摘要不能超过一页的四分之三 数学建模的六个步骤: 【写作】---学术语言 团队练题

【hadoop大数据集群 2】

【hadoop大数据集群 2】 文章目录 【hadoop大数据集群 2】1. 虚拟机克隆2. 时间同步3. 环境变量配置、启动集群、关闭集群 1. 虚拟机克隆 克隆之后一定要重新生成新虚拟机唯一的MAC地址和UUID等&#xff0c;确保新虚拟机与源虚拟机在网络拓扑中不发生冲突。 注意1.生成新的MA…

新华三H3CNE网络工程师认证—VLAN使用场景与原理

通过华三的技术原理与VLAN配置来学习&#xff0c;首先介绍VLAN&#xff0c;然后介绍VLAN的基本原理&#xff0c;最后介绍VLAN的基本配置。 一、传统以太网问题 在传统网络中&#xff0c;交换机的数量足够多就会出现问题&#xff0c;广播域变得很大&#xff0c;分割广播域需要…

借力Jersey,铸就卓越RESTful API体验

目录 maven 创建 jersey 项目 运行 支持返回 json 数据对象 1. 引言 在当今数字化时代&#xff0c;API&#xff08;应用程序编程接口&#xff09;已成为连接不同软件系统和服务的桥梁。RESTful API以其简洁、轻量级和易于理解的特点&#xff0c;成为了API设计的首选标准。本…

甲骨文面试题【动态规划】力扣377.组合总和IV

给你一个由 不同 整数组成的数组 nums &#xff0c;和一个目标整数 target 。请你从 nums 中找出并返回总和为 target 的元素组合的个数。 题目数据保证答案符合 32 位整数范围。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3], target 4 输出&#xff1a;7 解释&#x…

C语言:键盘录入案例

主要使用了scanf&#xff1b; scanf的使用方法和注意事项&#xff1a; 1.作用&#xff1a; 用于接收键盘输入的数据并赋值给对应的变量 2.使用方式; scanf("占位符",&变量名); 3.注意事项; 占位符后面的的变量要对应 第一个参数中不写换行 案例1&#xf…

夏令营入门组day5

目录 一. 城市距离 二. 史莱姆 一. 城市距离 &#xff08;1&#xff09;思路 每次询问&#xff0c;对于每一个点都判断与下一个点是否为临近点会超时&#xff0c;因此预处理&#xff0c;预先判断每一个点的临近点&#xff0c;然后将花费存入前缀和数组&#xff0c;这样在每次询…

GraphRAG:一种新型的RAG技术

微软前几天发布的 GraphRAG 架构非常厉害&#xff0c;但是具体的原理和内容可能不太好理解。Neo4j 的 CTO 写了一篇详细的文章《GraphRAG 宣言&#xff1a;为 GenAI 增加知识》&#xff0c;通俗易懂的介绍了 GraphRAG 的原理、与传统 RAG 的区别、GraphRAG的优势、知识图谱的创…

lua 游戏架构 之 LoaderWallet 异步加载

定义了一个名为LoaderWallet class&#xff0c;用于管理资源加载器&#xff08;Loader&#xff09;。这个类封装了资源加载的功能&#xff0c;包括异步加载&#xff0c;以及资源的释放和状态查询。下面是对代码的详细解释&#xff1a; ### 类定义和初始化 这里定义了一个名为…

初学者对 WebGL 与 WebGPU 的看法(A Beginner’s Perspective of WebGL vs WebGPU)

初学者对 WebGL 与 WebGPU 的看法&#xff08;A Beginner’s Perspective of WebGL vs WebGPU&#xff09; WebGL 和 WebGPU 之间的主要区别&#xff1a;WebGL 是什么以及它适合哪些人使用&#xff1f;WebGPU 是什么&#xff1f;它适合谁使用&#xff1f;WebGL 和 WebGPU 的代码…

spring事件发布器ApplicationEventPublisher的使用

1、前言 spring中有一个事件发布器,使用了观察者模式,当有事件发布的时候,事件监听者会立刻收到发布的事件。今天我们来介绍下这个事件发布器怎么使用。 2、简单使用 2.1、创建事件实体类 事件实体类需要继承ApplicationEvent。我们模拟老师发布事件的诉求。 public class T…

【51项目】基于51单片机protues交通灯的设计(完整资料源码)

基于51单片机protues交通灯的设计 一、 项目背景 1.1背景 随着科技的不断发展&#xff0c;LED技术在交通领域的应用越来越广泛。LED模拟交通灯作为一种新型的交通信号控制设备&#xff0c;以其高效、节能、环保等优点&#xff0c;逐渐取代了传统的交通信号灯。近年来&#xff…

【人工智能】Transformers之Pipeline(三):文本转音频(text-to-audio/text-to-speech)

​​​​​​​ 一、引言 pipeline&#xff08;管道&#xff09;是huggingface transformers库中一种极简方式使用大模型推理的抽象&#xff0c;将所有大模型分为音频&#xff08;Audio&#xff09;、计算机视觉&#xff08;Computer vision&#xff09;、自然语言处理&#x…

【深度学习入门篇 ⑨】循环神经网络实战

【&#x1f34a;易编橙&#xff1a;一个帮助编程小伙伴少走弯路的终身成长社群&#x1f34a;】 大家好&#xff0c;我是小森( &#xfe61;ˆoˆ&#xfe61; ) &#xff01; 易编橙终身成长社群创始团队嘉宾&#xff0c;橙似锦计划领衔成员、阿里云专家博主、腾讯云内容共创官…

把当前img作为到爷爷的背景图

&#xff08;忽略图大小不一致&#xff0c;一般UI给的图会刚好适合页面大小&#xff0c;我这网上找的图&#xff0c;难调大小&#xff0c;我行内的就自己随便写的宽高&#xff09;&#xff0c;另外悄悄告诉你最后有简单方法&#xff5e;&#xff5e; 先来看看初始DOM结构代码 …

【接口自动化_12课_基于Flask搭建MockServer】

知识非核心点,面试题较少。框架搭建的过程中的细节才是面试要点 第三方接口,不方便进行测试, 自己要一个接口去进行模拟。去作为我们项目访问模拟接口。自己写一个接口,需要怎样写 一、flask:轻量级的web应用的框架 安装命令 pip install flask 1、flask-web应用 1)…

【防雷】浪涌保护器的选择与应用

浪涌保护器&#xff08;SPD&#xff09;是一种用于保护电气设备免受电力系统突发的电压浪涌或过电压等干扰的重要装置。供电系统由于外部受雷击、过电压影响&#xff0c;内部受大容量设备和变频设备的开、关、重启、短路故障等&#xff0c;都会产生瞬态过电压&#xff0c;带来日…

你下载的蓝光电影,为什么不那么清晰?

1080P 为什么糊 蓝光对应的就是 1080P分辨率为 1920 * 1080 随便抽取一帧画面&#xff0c;得到的就是一张有 1920 * 1080 个像素点的图片大多数电影是每秒播放 24 张图片&#xff0c;也就是一个 24 帧的电影 电影在电脑上的储存 压缩方案 不仅仅有如下两种&#xff0c;还有…

Vue3 + uni-app 微信小程序:仿知乎日报详情页设计及实现

引言 在移动互联网时代&#xff0c;信息的获取变得越来越便捷&#xff0c;而知乎日报作为一款高质量内容聚合平台&#xff0c;深受广大用户喜爱。本文将详细介绍如何利用Vue 3框架结合微信小程序的特性&#xff0c;设计并实现一个功能完备、界面美观的知乎日报详情页。我们将从…

Linux LVM扩容方法

问题描述 VMware Centos环境&#xff0c;根分区为LVM&#xff0c;大小50G&#xff0c;现在需要对根分区扩容。我添加了一块500G的虚拟硬盘(/dev/sdb)&#xff0c;如何把这500G扩容到根分区&#xff1f; LVM扩容方法 1. 对新磁盘分区 使用fdisk /dev/sdb命令&#xff0c;进…