Clion-cmake配置CGAL(含openGL)
程序员文章站
2022-03-29 19:29:50
...
综述
最近在搞计算几何的相关项目。由于xcode对eigen的配置支持较差(不方便)
而在泊松表面重建中需要用到它。
所以索性cmake配置走起。
环境
macos
clion编译器
说明
首先确保您下载了cgal
sudo brew install cgal
代码
cmakelist.txt
cmake_minimum_required(VERSION 3.5)
project(myproject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(CGAL REQUIRED)
include(${CGAL_USE_FILE})
set(SOURCE_FILES main.cpp)
add_executable(myproject ${SOURCE_FILES})
target_link_libraries(myproject ${CGAL_LIBS})
main.cpp
#include <iostream>
#include <CGAL/Nef_polyhedron_3.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
进阶
很多人可能在cgal计算完毕后需要openGL“可视化”一下,所以我们顺便给出openGL+CGAL在cmake的配置方法:
下面给出一个泊松表面的代码
cmakelist.txt
cmake_minimum_required(VERSION 3.5)
project(OpenGL)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIR})
find_package(CGAL REQUIRED)
include(${CGAL_USE_FILE})
find_package(Eigen3 3.1.0) #(requires 3.1.0 or greater)
if (EIGEN3_FOUND)
include( ${EIGEN3_USE_FILE} )
endif()
set(CMAKE_CXX_FLAGS "-g -Wall")
set(SOURCE_FILES main.cpp)
add_executable(OpenGL ${SOURCE_FILES})
target_link_libraries(OpenGL ${GLUT_LIBRARY} ${OPENGL_LIBRARY} ${CGAL_LIBS})
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
#
#target_link_libraries(AGRAN)
main.cpp
#include <CGAL/trace.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Surface_mesh_default_triangulation_3.h>
#include <CGAL/make_surface_mesh.h>
#include <CGAL/Implicit_surface_3.h>
#include <CGAL/IO/output_surface_facets_to_polyhedron.h>
#include <CGAL/Poisson_reconstruction_function.h>
#include <CGAL/Point_with_normal_3.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/compute_average_spacing.h>
#include <CGAL/Polygon_mesh_processing/distance.h>
#include <vector>
#include <fstream>
// Types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::FT FT;
typedef Kernel::Point_3 Point;
typedef CGAL::Point_with_normal_3<Kernel> Point_with_normal;
typedef Kernel::Sphere_3 Sphere;
typedef std::vector<Point_with_normal> PointList;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Poisson_reconstruction_function<Kernel> Poisson_reconstruction_function;
typedef CGAL::Surface_mesh_default_triangulation_3 STr;
typedef CGAL::Surface_mesh_complex_2_in_triangulation_3<STr> C2t3;
typedef CGAL::Implicit_surface_3<Kernel, Poisson_reconstruction_function> Surface_3;
int main(void)
{
// Poisson options
FT sm_angle = 20.0; // Min triangle angle in degrees.
FT sm_radius = 30; // Max triangle size w.r.t. point set average spacing.
FT sm_distance = 0.375; // Surface Approximation error w.r.t. point set average spacing.
// Reads the point set file in points[].
// Note: read_xyz_points_and_normals() requires an iterator over points
// + property maps to access each point's position and normal.
// The position property map can be omitted here as we use iterators over Point_3 elements.
PointList points;
std::ifstream stream("/Users/frankdura/Desktop/test.xyz");
if (!stream ||
!CGAL::read_xyz_points_and_normals(
stream,
std::back_inserter(points),
CGAL::make_normal_of_point_with_normal_pmap(PointList::value_type())))
{
return EXIT_FAILURE;
}
Poisson_reconstruction_function function(points.begin(), points.end(),
CGAL::make_normal_of_point_with_normal_pmap(PointList::value_type()) );
if ( ! function.compute_implicit_function() )
return EXIT_FAILURE;
FT average_spacing = CGAL::compute_average_spacing<CGAL::Sequential_tag>(points.begin(),points.end(),6);
// Gets one point inside the implicit surface
// and computes implicit function bounding sphere radius.
Point inner_point = function.get_inner_point();
Sphere bsphere = function.bounding_sphere();
FT radius = std::sqrt(bsphere.squared_radius());
// Defines the implicit surface: requires defining a
// conservative bounding sphere centered at inner point.
FT sm_sphere_radius = 5.0 * radius;
FT sm_dichotomy_error = sm_distance*average_spacing/1000.0; // Dichotomy error must be << sm_distance
Surface_3 surface(function,
Sphere(inner_point,sm_sphere_radius*sm_sphere_radius),
sm_dichotomy_error/sm_sphere_radius);
// Defines surface mesh generation criteria
CGAL::Surface_mesh_default_criteria_3<STr> criteria(sm_angle, // Min triangle angle (degrees)
sm_radius*average_spacing, // Max triangle size
sm_distance*average_spacing); // Approximation error
// Generates surface mesh with manifold option
STr tr; // 3D Delaunay triangulation for surface mesh generation
C2t3 c2t3(tr); // 2D complex in 3D Delaunay triangulation
CGAL::make_surface_mesh(c2t3, // reconstructed mesh
surface, // implicit surface
criteria, // meshing criteria
CGAL::Manifold_with_boundary_tag()); // require manifold mesh
if(tr.number_of_vertices() == 0)
return EXIT_FAILURE;
// saves reconstructed surface mesh
std::ofstream out("/Users/frankdura/Desktop/test.off");
Polyhedron output_mesh;
CGAL::output_surface_facets_to_polyhedron(c2t3, output_mesh);
out << output_mesh;
// computes the approximation error of the reconstruction
double max_dist =
CGAL::Polygon_mesh_processing::approximate_max_distance_to_point_set(output_mesh,
points,
4000);
std::cout << "Max distance to point_set: " << max_dist << std::endl;
return EXIT_SUCCESS;
}
上一篇: Mac Tensorflow环境搭建
下一篇: ssh执行远程脚本遇到的坑