頭大啊,自己寫三維算法太累了,還是引入開源庫吧
CGAL是計算幾何算法庫,是一個大型C++庫的幾何數據結構和算法,如Delaunay三角網、網格生成、布爾運算的多邊形以及各種幾何處理算法。
CGAL是用來在各個領域:計算機圖形學、科學可視化、計算機輔助設計與建模、地理信息系統、分子生物學、醫學影像學、機器人學和運動規划和數值方法
太復雜了,頭大啊編譯這個鬼東西,到處都是坑
一.CGAL安裝
CGAL必須依賴Boost庫 gmp庫 mpfx庫
boost_system-vc142-mt-gd-x64-1_74.lib
boost_system-vc142-mt-x64-1_74.lib
boost_thread-vc142-mt-gd-x64-1_74.lib
boost_thread-vc142-mt-x64-1_74.lib
libgmp-10.lib
libmpfr-4.lib
CGAL也會依賴 eigen庫 openmesh庫 opencv庫 zlib庫等
CGAL也依賴QT5庫 (常用 QtWidgets QtGui QtOpenGL QtCore Qt)
注意:QT5的安裝在VS中必須安裝QT VS TOOLS功能插件,來支持QT中的UI界面,不然在VS中會識別不出來
#include "ui_ImageInterface.h" 這個在QT對應 ImageInterface.ui 要么用VS右鍵編譯生成頭文件,要么在QT的bin中找 uic.exe 進行cmd命令生成
注意:如果出現無法識別 CGAL::QGLViewer::staticMetaObject 這個東西跟QObject相關聯,而它的識別需要QT的bin中找 moc.exe 進行cmd命令生成一個.cpp 最后鏈接到代碼上
CGAL必須事先用cmake編譯出 CGAL_Core-vc141
CGAL_ImageIO-vc141
CGAL_Qt5-vc141
CGAL-vc141
二.CGAL使用
1.創建點 線 面
//表示幾何圖元
typedef CGAL::Simple_cartesian<double> Kernel;
//表示點
typedef Kernel::Point_2 Point_2;
//表示線
typedef Kernel::Segment_2 Segment_2;
Point_2 p(1, 1), q(10, 10);
Segment_2 s(p, q);
2.計算點到線段的距離
Point_2 p(1,1), q(10, 10);
//兩點距離
CGAL::squared_distance(p, q)
CGAL::orientation(p, q, m)
CGAL::midpoint(p, q)
三.CGAL解析
四.CGAL Examples
1.AABB_tree
2.Advancing_font_surface_reconstruction
3.Algebraic_foundations
4.Algebraic_kernel_d
5.Alpha_shapes_2
6.Alpha_shapes_3
7.Apollonius_graph_2
8.Approximate_min_ellipsoid_d
9.Arrangement_on_surface_2
10.Barycentric_coordinates_2
11.BGL_arrangement_2
12.BGL_graphcut
13.BGL_LCC
14.BGL_OpenMesh
15.BGL_polyhedron_3
16.BGL_surface_mesh
17.BGL_triangulation_2
18.Boolean_set_operations_2
19.Box_intersection_d
20.CGAL_ipelets
21.CGALimageIO
22.Circular_kernel_2
23.Circular_kernel_3
24.Circulator
25.Classification
26.Combinatorial_map
27.Cone_spanners_2
28.Convex_decomposition_3
29.Convex_decomposition_3
30.Convex_hull_2
31.Convex_hull_3
32.Core
33.Envelope_2
34.Envelope_3
35.Filtered_kernel
36.Generator
37.HalfedgeDS
38.Heat_method_3
39.Hyperbolic_triangulation_2
40.Inscribed_areas
41.Interpolation
42.Interval_skip_list
43.Jet_fitting_3
44.Kernel_23
45.Linear_cell_complex
46.Matrix_search
47.Mesh_2
48.Mesh_3
49.Min_annulus_d
50.Min_circle_2
51.Min_ellipse_2
52.Min_quadrilateral_2
53.Min_sphere_d
54.Min_sphere_of_spheres_d
55.Minkowski_sum_2
56.Minkowski_sum_3
57.Modular_arithmetric
58.Nef_2
59.Nef_3
60.Nef_32
61.Optimal_bounding_box
62.Optimal_transportation_reconstruction_2
63.Orthtree
64.Partition_2
65.Periodic_2_triangulation_2
66.Periodic_3_mesh_3
67.Periodic_3_triangulation_3
68.Periodic_4_hyperbolic_triangulation_2
69.Point_set_2
70.Point_set_3
71.Point_set_processing_3
72.Poisson_surface_reconstruction_3
73.Polygon
//繪制多邊形
//draw_polygon.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#define CGAL_USE_BASIC_VIEWER
#ifdef CGAL_USE_BASIC_VIEWER
#include <CGAL/draw_triangulation_2.h>
#include <CGAL/draw_polygon_2.h>
#endif
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Point_2<K> Point;
int main()
{
//create a polygon and put some points in it
Polygon_2 p;
p.push_back(Point(0, 0));
p.push_back(Point(4, 0));
p.push_back(Point(4, 4));
p.push_back(Point(2, 2));
p.push_back(Point(0, 4));
CGAL::draw(p);
return EXIT_SUCCESS;
}
//帶洞的多邊形
//draw_polygon_with_holes.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_with_holes_2.h>
#define CGAL_USE_BASIC_VIEWER
#ifdef CGAL_USE_BASIC_VIEWER
#include <CGAL/draw_polygon_with_holes_2.h>
#endif
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Point_2<K> Point;
int main()
{
//Create a polygon with three holes
Polygon_2 outer_polygon;
outer_polygon.push_back(Point(0, 0)); outer_polygon.push_back(Point(9, 0));
outer_polygon.push_back(Point(6, 8)); outer_polygon.push_back(Point(5, 3));
outer_polygon.push_back(Point(2, 8)); outer_polygon.push_back(Point(0, 8));
std::vector<Polygon_2> holes(3);
holes[0].push_back(Point(6, 2)); holes[0].push_back(Point(7, 1));
holes[0].push_back(Point(7, 3)); holes[0].push_back(Point(6, 3));
holes[0].push_back(Point(5, 2));
holes[1].push_back(Point(2, 1)); holes[1].push_back(Point(3, 1));
holes[1].push_back(Point(3, 3)); holes[1].push_back(Point(2, 2));
holes[1].push_back(Point(1, 2));
holes[2].push_back(Point(1, 4)); holes[2].push_back(Point(2, 4));
holes[2].push_back(Point(2, 5)); holes[2].push_back(Point(3, 5));
holes[2].push_back(Point(3, 6)); holes[2].push_back(Point(1, 6));
Polygon_with_holes_2 p(outer_polygon, holes.begin(), holes.end());
//And draw it
CGAL::draw(p);
return EXIT_SUCCESS;
}
//Example.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <list>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Point_2<K> Point;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef Polygon_2::Vertex_iterator VertexIterator;
typedef Polygon_2::Edge_const_iterator EdgeIterator;
int main()
{
//create a polygon and put some points in it
Polygon_2 p;
p.push_back(Point(0, 0));
p.push_back(Point(4, 0));
p.push_back(Point(4, 4));
p.push_back(Point(2, 2));
p.push_back(Point(0, 4));
CGAL::set_pretty_mode(std::cout);
//CGAL::IO::set_pretty_mode(std::cout);
std::cout << "created the polygon p" << std::endl;
std::cout << p << std::endl;
//determine some properties of the polygon
bool IsSimple = p.is_simple();
bool IsConvex = p.is_convex();
bool IsClockwise = (p.orientation() == CGAL::CLOCKWISE);
double Area = p.area();
std::cout << "polygon p is";
if (!IsSimple) std::cout << "not";
std::cout << "simple." << std::endl;
std::cout << "polygon p is";
if (!IsConvex) std::cout << "not";
std::cout << "convex." << std::endl;
std::cout << "polygon p is";
if (!IsClockwise) std::cout << "not";
std::cout << "clockwise oriented" << std::endl;
std::cout << "the area of polygon p is" << Area << std::endl;
std::cout << std::endl;
// apply some algorithms
Point q(1, 1);
std::cout << "created point q = " << q << std::endl;
std::cout << std::endl;
bool IsInside = (p.bounded_side(q) == CGAL::ON_BOUNDED_SIDE);
std::cout << "point q is";
if (!IsInside) std::cout << "not";
std::cout << "inside polygon p" << std::endl;
std::cout << std::endl;
//traverse the vertices and the edges
int n = 0;
for (VertexIterator vi = p.vertices_begin(); vi != p.vertices_end(); ++vi)
std::cout << "vertex" << n++ << "=" << *vi << std::endl;
std::cout << std::endl;
n = 0;
for (EdgeIterator ei = p.edges_begin(); ei != p.edges_end(); ++ei)
std::cout << "edge" << n++ << "=" << *ei << std::endl;
return 0;
}
//Polygon.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <iostream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point;
typedef CGAL::Polygon_2<K> Polygon_2;
using std::cout;
using std::endl;
int main()
{
Point points[] = { Point(0, 0), Point(5.1, 0), Point(1, 1), Point(0.5, 6) };
Polygon_2 pgn(points, points + 4);
//check if the polygon is simple
cout << "The polygon is " << (pgn.is_simple() ? "" : "not") << "simple" << endl;
//check if the polygon is convex
cout << "The polygon is " << (pgn.is_convex() ? "" : "not") << "convex" << endl;
return 0;
}
//提供了判斷點是否在多邊形內部或者外部的算法
//polygon_algorithms.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2_algorithms.h>
#include <iostream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point;
using std::cout;
using std::endl;
void check_inside(Point pt, Point* pgn_begin, Point* pgn_end, K traits)
{
cout << "The point" << pt;
switch (CGAL::bounded_side_2(pgn_begin, pgn_end, pt, traits)) {
case CGAL::ON_BOUNDED_SIDE :
cout << "is inside the polygon\n";
break;
case CGAL::ON_BOUNDARY:
cout << "is on the polygon boudary\n";
break;
case CGAL::ON_UNBOUNDED_SIDE:
cout << "is outside the polygon\n";
break;
}
}
int main()
{
Point points[] = { Point(0, 0), Point(5.1, 0), Point(1, 1), Point(0.5, 6) };
//check if the polygon is simple
cout << "The polygon is" << (CGAL::is_simple_2(points, points + 4, K()) ? "" : "not") << "simple" << endl;
check_inside(Point(0.5, 0.5), points, points + 4, K());
check_inside(Point(1.5, 2.5), points, points + 4, K());
check_inside(Point(2.5, 0), points, points + 4, K());
return 0;
}
//projected_polygon.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Projection_traits_yz_3.h>
#include <CGAL/Polygon_2_algorithms.h>
#include <iostream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point_3;
int main()
{
Point_3 points[4] = { Point_3(0, 1, 1), Point_3(0, 2, 1), Point_3(0, 2, 2), Point_3(0, 1, 2) };
bool b = CGAL::is_simple_2(points, points + 4, CGAL::Projection_traits_yz_3<K>());
if (!b) {
std::cerr << "Error polygon is not simple" << std::endl;
return 1;
}
return 0;
}
74.Polygon_mesh_processing
75.Polygonal_surface_reconstruction
76.Polyhedron
77.Polyhedron_IO
78.Polyline_simplification_2
79.Polynomial
80.Polytope_distance_d
81.Principal_component_analysis
82.Profiling_tools
83.Property_map
84.QP_solver
85.RangeSegmentTrees
86.Rectangular_p_center_2
87.Ridges_3
88.Scale_space_reconstruction_3
89.Segment_Delaunay_graph_2
90.Segment_Delaunay_graph_Linf_2
91.Set_movable_separability_2
92.Shape_detection
93.Skin_surface_3
94.Snap_rounding_2
95.Solver_interface
96.Spatial_searching
97.Spatial_sorting
98.STL_Extension
99.Straight_skeleton_2
100.Stream_lines_2
101.Stream_support
102.Subdivision_method_3
103.Surface_mesh
104.Surface_mesh_approximation
105.Surface_mesh_deformation
106.Surface_mesh_parameterization
107.Surface_mesh_segmentation
108.Surface_mesh_shortest_path
109.Surface_mesh_simplification
110.Surface_mesh_skeletonization
111.Surface_mesh_topology
112.Surface_mesher
113.Surface_sweep_2
114.TDS_3
115.Tetrahedral_remeshing
116.Triangulation
117.Triangulation_2
118.Triangulation_3
119.Visibility_2
120.Voronoi_diagram_2
五.CGAL Demo
1.AABB_tree
2.Alpha_shapes_2
3.Alpha_shapes_3
4.Apollonius_graph_2
5.Arrangement_on_surface_2
6.Bounding_volumes
7.CGAL_ipelets
8.Circular_kernel_2
9.Circulal_kernel_3
10.Convex_hull_3
11.Generator
12.Geomview
13.GraphicsView
14.Hyperbolic_triangulation_2
15.icons
16.Interpolation
17.L1_Voronoi_diagram_2
18.Largest_empty_rect_2
19.Linear_cell_complex
20.Mesh_2
21.Optimal_transportation_reconstruction_2
22.Periodic_2_triangulation_2
23.Periodic_3_triangulation_3
24.Periodic_4_hyperbolic_triangulation_2
25.Periodic_LIoyd_3
26.Polygon
27.Polyhedron
28.Polyhedron_IO
29.Polyline_simplication_2
30.Principal_component_analysis
31.resources
32.Segment_Delaunay_graph_2
33.Segment_Delaunay_graph_Linf_2
34.Snap_rounding_2
35.Spatial_searching_2
36.Stream_lines_2
37.Surface_mesh_deformation
38.Surface_mesher
39.Triangulation_2
40.Triangulation_3
41.Triangulation_3_Geomview_demos