C++ 几何计算库

代码

#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 returnedtypedef K::Point_3    Point; // CGAL 3D point typetypedef K::Triangle_3 Datum; // CGAL 3D triangle typeprivate:Id m_pt; // this is what the AABB tree stores internallypublic: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_treeMy_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 typesDatum 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 primitivePoint 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 treeMyTree tree(triangles.begin(), triangles.end());// counts #intersectionsK::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 pointK::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 querySegment 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 queryPoint 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());}elsestd::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;elsestd::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);}elsestd::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 triangulationstd::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 pointsstd::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 hullSurface_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.mzph.cn/pingmian/47480.shtml

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

相关文章

线性代数|机器学习-P24加速梯度下降(动量法)

文章目录 1. 概述2. 引入3. 动量法梯度下降 1. 概述 我们之前学的最速梯度下降[线搜索方法] 公式如下&#xff1a; x k 1 x k − s k ∇ f ( x k ) \begin{equation} x_{k1}x_k-s_k\nabla f(x_k) \end{equation} xk1​xk​−sk​∇f(xk​)​​ 但对于这种方法来说&#xff…

助力数据记录:Connext的Historian功能启动指南

​ 在工业自动化领域&#xff0c;Connext以其全新的OPCUA数据采集解决方案为核心&#xff0c;不仅拥有超越传统的扩展功能&#xff0c;而且在多个行业中都得到了广泛的认证。Connext能够快速找出解决方案并迅速部署&#xff0c;整个过程不会影响到原有的生产。它的强大之处在于…

信创学习笔记(三),信创之操作系统OS思维导图

创作不易 只因热爱!! 热衷分享&#xff0c;一起成长! “你的鼓励就是我努力付出的动力” 一. 回顾信创CPU芯片 1. x86应用生态最丰富, 海光(3,5,7)授权较新,无桌面授权,多用于服务器 兆芯(ZX, KX, KH)授权较早期. 2. ARMv8移动端应用生态丰富, 华为鲲鹏(9) ,制裁中&#xff0c;…

基于python的三次样条插值原理及代码

1 三次样条插值 1.1 三次样条插值的基本概念 三次样条插值是通过求解三弯矩方程组&#xff08;即三次样条方程组的特殊形式&#xff09;来得出曲线函数组的过程。在实际计算中&#xff0c;还需要引入边界条件来完成计算。样条插值的名称来源于早期工程师制图时使用的细长木条&…

探索Node.js中的node-xlsx:将Excel文件解析为JSON

在Node.js开发中&#xff0c;处理Excel文件是一个常见需求&#xff0c;特别是在需要导入大量数据或生成报表的场景中。node-xlsx 是一个强大的库&#xff0c;它提供了Excel文件的解析和生成功能。本文将深入探讨 node-xlsx 的使用&#xff0c;并通过一个案例演示如何将Excel文件…

算法——双指针(day2)

目录 202.快乐数 题目解析&#xff1a; 算法解析&#xff1a; 代码&#xff1a; 11.盛最多水的容器 题目解析&#xff1a; 算法解析&#xff1a; 代码&#xff1a; 202.快乐数 力扣链接&#xff1a;202.快乐数 题目解析&#xff1a; 本文中最重要的一句话就是重复平方和…

AI自动生成PPT哪个软件好?高效制作PPT优选这4个

7.15初伏的到来&#xff0c;也宣告三伏天的酷热正式拉开序幕~在这个传统的节气里&#xff0c;人们以各种方式避暑纳凉&#xff0c;享受夏日的悠闲时光。 而除了传统的避暑活动&#xff0c;我们还可以用一种新颖的方式记录和分享这份夏日的清凉——那就是通过PPT的方式将这一传…

班迪录屏Bandicam使用详解

Bandicam是一款功能强大的视频录制工具&#xff0c;录制出来的视频体积较小且内容清晰度较高&#xff0c;平时录屏、录游戏都非常合适。可以全屏幕录制&#xff0c;也可以自定义录制区域&#xff0c;还可以在录制时添加自定义的logo&#xff0c;并且有个绘制模式&#xff0c;适…

学习008-01-02 Define the Data Model and Set the Initial Data(定义数据模型并设置初始数据 )

Define the Data Model and Set the Initial Data&#xff08;定义数据模型并设置初始数据 &#xff09; This topic explains how to implement entity classes for your application. It also describes the basics of automatic user interface construction based on a da…

基于AT89C51单片机的多功能自行车测速计程器(含文档、源码与proteus仿真,以及系统详细介绍)

本篇文章论述的是基于AT89C51单片机的多功能自行车测速计程器的详情介绍&#xff0c;如果对您有帮助的话&#xff0c;还请关注一下哦&#xff0c;如果有资源方面的需要可以联系我。 目录 选题背景 原理图 PCB图 仿真图 代码 系统论文 资源下载 选题背景 美丽的夜晚&…

JavaScript:移除元素

这是原题&#xff1a;给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。 假设 nums 中不等于 val 的元素数量为 k&#xff0c;要通过此题&#xff0c;您需要执行以下操…

【PyTorch][chapter 26][李宏毅深度学习][attention-2]

前言&#xff1a; Multi-Head Attention 主要作用&#xff1a;将Q,K,V向量分成多个头&#xff0c;形成多个子语义空间&#xff0c;可以让模型去关注不同维度语义空间的信息 目录&#xff1a; attention 机制 Multi-Head Attention 一 attention 注意力 Self-Attention&#x…

什么是im即时通讯?WorkPlus im即时通讯私有化部署安全可控

IM即时通讯是Instant Messaging的缩写&#xff0c;指的是一种实时的、即时的电子信息交流方式&#xff0c;也被称为即时通讯。它通过互联网和移动通信网络&#xff0c;使用户能够及时交换文本消息、语音通话、视频通话、文件共享等信息。而WorkPlus im即时通讯私有化部署则提供…

防火墙--双机热备

目录 双击热备作用 防火墙和路由器备份不同之处 如何连线 双机 热备 冷备 VRRP VGMP&#xff08;华为私有协议&#xff09; 场景解释 VGMP作用过程 主备的形成场景 接口故障的切换场景 整机故障 原主设备故障恢复的场景 如果没有开启抢占 如果开启了抢占 负载分…

对红酒品质进行数据分析(python)

http://t.csdnimg.cn/UWg2S 数据来源于这篇博客&#xff0c;直接下载好csv文件。 这篇内容均在VScode的jupyter notebook上完成&#xff0c;操作可以看我的另一篇博客&#xff1a;http://t.csdnimg.cn/69sDJ 一、准备工作 1. 导入数据库 #功能是可以内嵌绘图&#xff0c;并…

纯硬件一键开关机电路的工作原理

这是一个一键开关机电路: 当按一下按键然后松开&#xff0c;MOS管导通&#xff0c;VOUT等于电源电压; 当再次按一下按键然后松开&#xff0c;MOS管关闭&#xff0c;VOUT等于0; 下面来分析一下这个电路的工作原理。上电后&#xff0c;输入电压通过R1和R2给电容充电&#xff0c;最…

继承和多态常见的面试问题

文章目录 概念问答 概念 下面哪种面向对象的方法可以让你变得富有( A) A: 继承 B: 封装 C: 多态 D: 抽象 (D )是面向对象程序设计语言中的一种机制。这种机制实现了方法的定义与具体的对象无关&#xff0c; 而对方法的调用则可以关联于具体的对象。 A: 继承 B: 模板 C: 对象的…

如何让公众号文章排版变的高大上?

有的时候&#xff0c;你可能会疑惑&#xff0c;为什么你写的文章没人看&#xff1f;明明投入很多精力在标题和文章内容上&#xff0c;但收效甚微。 有一个关键性的因素可能被你忽略了&#xff0c;那就是排版&#xff0c;文章没有排版&#xff0c;无论你的内容再怎么精彩&#x…

力扣622.设计循环队列

力扣622.设计循环队列 通过数组索引构建一个虚拟的首尾相连的环当front rear时 队列为空当front rear 1时 队列为满 (最后一位不存) class MyCircularQueue {int front;int rear;int capacity;vector<int> elements;public:MyCircularQueue(int k) {//最后一位不存…