본문으로 바로가기

CGAL의 boolean operation

category Devlogs 2024. 6. 18. 20:56

CGAL을 이용한 메시 boolean 연산..

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/Seam_mesh.h>
#include <CGAL/Surface_mesh_parameterization/IO/File_off.h>
#include <CGAL/Surface_mesh_parameterization/parameterize.h>
#include <CGAL/Surface_mesh_parameterization/Two_vertices_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/LSCM_parameterizer_3.h>
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <CGAL/Polygon_mesh_processing/measure.h>
#include <CGAL/Polygon_mesh_processing/remesh.h>

#include <CGAL/boost/graph/selection.h>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

typedef CGAL::Simple_cartesian<double>      Kernel;
typedef Kernel::Point_2                     Point_2;
typedef Kernel::Point_3                     Point_3;
typedef CGAL::Surface_mesh<Kernel::Point_3> SurfaceMesh;

int main(int argc, char* argv[])
{
	if(argc != 5) {
		std::cout << "Usage: <off_file> <off_file> <operation> <out_off_file>";
		return EXIT_FAILURE;
	}

	const std::string filename1 = argv[1];
	const std::string filename2 = argv[2];
	const std::string operation = argv[3];
	const std::string out_filename = argv[4];

	SurfaceMesh surface_mesh_1;
	SurfaceMesh surface_mesh_2;

	std::ifstream in_mesh1(filename1);
	if(!in_mesh1){
		std::cerr << "problem loading the input data" << std::endl;
		return EXIT_FAILURE;
	} else {
		in_mesh1 >> surface_mesh_1;
	}
	
	std::ifstream in_mesh2(filename2);
	if(!in_mesh2){
		std::cerr << "problem loading the input data" << std::endl;
		return EXIT_FAILURE;
	} else {
		in_mesh2 >> surface_mesh_2;
	}

	bool res = false;
	auto surf_1 = surface_mesh_1;
	auto surf_2 = surface_mesh_2;
	namespace PMP = CGAL::Polygon_mesh_processing;
	SurfaceMesh output_surface_mesh;

	if(operation == "union") {
		res = PMP::corefine_and_compute_union(surf_1, surf_2, output_surface_mesh);

	} else if(operation == "intersection") {
		res = PMP::corefine_and_compute_intersection(surf_1, surf_2, output_surface_mesh);

	} else if(operation == "difference") {
		res = PMP::corefine_and_compute_difference(surf_1, surf_2, output_surface_mesh);

	} else if(operation == "difference2") {
		res = PMP::corefine_and_compute_difference(surf_2, surf_1, output_surface_mesh);

	} else if(operation == "corefinement") {
		 PMP::corefine(surf_1, surf_2); // both surfaces are corefined
		 output_surface_mesh = std::move(surf_1);

	} else {
		output_surface_mesh.clear();
	}

	CGAL::IO::write_polygon_mesh(out_filename, output_surface_mesh, CGAL::parameters::stream_precision(17));

	return EXIT_SUCCESS;
}

 

아래는 가시화해본 화면..


Korea Tcl/Tk Community
블로그 이미지 ihmin 님의 블로그
VISITOR 오늘 / 전체