CGAL文件格式的转换
程序员文章站
2024-03-24 18:37:52
...
1、.stl转.off文件
// C++ includes
#include <string.h>
#include <fstream>
#include <iostream>
// VTK includes
#include<vtkSTLReader.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkSmartPointer.h>
int main() {
vtkSmartPointer<vtkSTLReader> reader =
vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName("../data/input.stl");
reader->Update();
vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
polyData = reader->GetOutput();
double x[3];
ofstream outfile;//创建一个ofstream对象
outfile.open("../save/output.off");
outfile.precision(17);//有效位
outfile << "OFF" << "\n";
outfile << polyData->GetNumberOfPoints() << " "
<< polyData->GetNumberOfCells() << " 0\n";
for (int ww = 0; ww < polyData->GetNumberOfPoints() ; ww++) {
polyData->GetPoint(ww, x);
outfile << x[0] << " " << x[1] << " " << x[2] << "\n";
}
for (int ww = 0; ww < polyData->GetNumberOfCells() ; ww++) {
outfile << "3 ";
for (int i = 0; i < polyData->GetCell(ww)->GetNumberOfPoints(); i++) {
outfile << polyData->GetCell(ww)->GetPointId(i) << " ";
}
outfile << "\n";
}
outfile .close();
return 0;
}
2、.off转.stl文件
//VTK includes
#include <vtkOBJReader.h>
#include <vtkSTLWriter.h>
#include <vtkSmartPointer.h>
// C++ includes
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;
//CGAL includes
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point_3;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;//多面体
typedef Polyhedron::Facet_iterator Facet_iterator;//面(facets)
typedef Polyhedron::Point_iterator Point_iterator;//点
typedef Polyhedron::Halfedge_around_facet_circulator Halfedge_facet_circulator;
int main(int argc, char *argv[]) {
const char *filename = (argc > 1) ? argv[1] : "../data/elephant.off";
std::ifstream input(filename);
Polyhedron P;
if (!input || !(input >> P) || P.empty()) {
std::cerr << "Not a valid off file." << std::endl;
return 1;
}
ofstream outfile;//创建一个ofstream对象
outfile.open("../save/elephant.obj");
outfile.precision(17);//有效位
outfile << "obj" << " " << "try" << "\n";
//输出所有的点信息
for (Point_iterator k = P.points_begin(); k != P.points_end(); ++k) {
outfile << "v " << *k << endl;
}
//输出所有的mian信息
for (Facet_iterator i = P.facets_begin(); i != P.facets_end(); ++i) {
Halfedge_facet_circulator j = i->facet_begin();
outfile << "f ";
do {
outfile << 1 + distance(P.vertices_begin(), j->vertex()) << " ";
} while (++j != i->facet_begin());
outfile << endl;
}
outfile .close();
//VTK读取obj,写入STL
vtkSmartPointer<vtkOBJReader> reader =
vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName("../save/elephant.obj");
reader->Update();
vtkSmartPointer<vtkSTLWriter> stlWriter =
vtkSmartPointer<vtkSTLWriter>::New();
stlWriter->SetFileName("../save/elephant.stl");
stlWriter->SetInputConnection(reader->GetOutputPort());
stlWriter->Write();
return 0;
}
3、.off转.obj文件
// C++ includes
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;
//CGAL includes
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point_3;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;//多面体
typedef Polyhedron::Facet_iterator Facet_iterator;//面(facets)
typedef Polyhedron::Point_iterator Point_iterator;//点
typedef Polyhedron::Halfedge_around_facet_circulator Halfedge_facet_circulator;
int main(int argc, char *argv[]) {
const char *filename = (argc > 1) ? argv[1] : "../data/input.off";
std::ifstream input(filename);
Polyhedron P;
if (!input || !(input >> P) || P.empty()) {
std::cerr << "Not a valid off file." << std::endl;
return 1;
}
ofstream outfile;//创建一个ofstream对象
outfile.open("../save/output.obj");
outfile.precision(17);//有效位
outfile << "obj" << " " << "try" << "\n";
//输出所有的点信息
for (Point_iterator k = P.points_begin(); k != P.points_end(); ++k) {
outfile << "v " << *k << endl;
}
//输出所有的mian信息
for (Facet_iterator i = P.facets_begin(); i != P.facets_end(); ++i) {
Halfedge_facet_circulator j = i->facet_begin();
outfile << "f ";
do {
outfile << 1 + distance(P.vertices_begin(), j->vertex()) << " ";
} while (++j != i->facet_begin());
outfile << endl;
}
outfile .close();
return 0;
}
4、.mesh转.vtu文件
//CGAL includes
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Mesh_triangulation_3.h>
#include <CGAL/Mesh_complex_3_in_triangulation_3.h>
#include <CGAL/Mesh_criteria_3.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/boost/graph/helpers.h>
#include <CGAL/Polyhedral_mesh_domain_3.h>
#include <CGAL/make_mesh_3.h>
#include <CGAL/refine_mesh_3.h>
#include <CGAL/IO/Complex_3_in_triangulation_3_to_vtk.h>
// VTK includes
#include <vtkSmartPointer.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
// Domain
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Polyhedron_3<K> Polyhedron;
typedef CGAL::Polyhedral_mesh_domain_3<Polyhedron, K> Mesh_domain;
#ifdef CGAL_CONCURRENT_MESH_3
typedef CGAL::Parallel_tag Concurrency_tag;
#else
typedef CGAL::Sequential_tag Concurrency_tag;
#endif
// Triangulation
typedef CGAL::Mesh_triangulation_3<Mesh_domain, CGAL::Default, Concurrency_tag>::type Tr;
typedef CGAL::Mesh_complex_3_in_triangulation_3<Tr> C3t3;
// Criteria
typedef CGAL::Mesh_criteria_3<Tr> Mesh_criteria;
// To avoid verbose function and named parameters call
using namespace CGAL::parameters;
int main(int argc, char *argv[]) {
const char *fname = (argc > 1) ? argv[1] : "../data/elephant.off";//mech-holes-shark.elephant.off
// Create input polyhedron
Polyhedron polyhedron;
std::ifstream input(fname);
input >> polyhedron;
if (input.fail()) {
std::cerr << "Error: Cannot read file " << fname << std::endl;
return EXIT_FAILURE;
}
input.close();
if (!CGAL::is_triangle_mesh(polyhedron)) {
std::cerr << "Input geometry is not triangulated." << std::endl;
return EXIT_FAILURE;
}
// Create domain
Mesh_domain domain(polyhedron);
// Mesh criteria (no cell_size set)
Mesh_criteria criteria(facet_angle = 25, facet_size = 0.15, facet_distance = 0.008,
cell_radius_edge_ratio = 3);//25,0.15,0.008,3
// Mesh generation
C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain, criteria, no_perturb(), no_exude());
// Output
std::ofstream medit_file("../save/elephant_out1.mesh");
c3t3.output_to_medit(medit_file);
medit_file.close();
// Set tetrahedron size (keep cell_radius_edge_ratio), ignore facets
Mesh_criteria new_criteria(cell_radius_edge_ratio = 3, cell_size = 0.05);//cell_size0.01
// Mesh refinement (and make the output manifold)
CGAL::refine_mesh_3(c3t3, domain, new_criteria, manifold());
// Output
medit_file.open("../save/elephant-out2.mesh");
c3t3.output_to_medit(medit_file);
//Try convert CGAL object to a vtkUnstructuredGrid
vtkUnstructuredGrid *output;
output = CGAL::output_c3t3_to_vtk_unstructured_grid(c3t3);
//Write
vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
writer->SetFileName("../save/elephant2_output.vtu");
writer->SetInputData(output);
writer->Write();
//Render
vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
reader->SetFileName("../save/elephant2_output.vtu");
reader->Update();
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(.3, .6, .3);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
上一篇: Mycat分库分表详细操作
下一篇: kafka原理及其使用
推荐阅读
-
CGAL文件格式的转换
-
Java boolean类型 Getter Setter 的使用和细节.md 博客分类: fastjson fastjsonjson取不到值转换为boolean为nullLinux
-
byte[]和InputStream的相互转换
-
文本的编码转换ANSI、UNICODE、UTF8
-
python 中utf8,u_str的转换
-
ANSI,UNICODE,UTF8之间的相互转换
-
go语言 进行文件编码格式转换中(GBK 转UTF8),可能出错部分文件出现乱码情况的解决。
-
Pandas 将gbk和utf-8 的文件格式转换出错时的解决方法(windows)
-
文件在线阅读的几种方案对比 博客分类: 文档转换与阅读 在线阅读 百度文库 邮箱附件预览 文档查看 在线文档
-
文件在线阅读的几种方案对比 博客分类: 文档转换与阅读 在线阅读 百度文库 邮箱附件预览 文档查看 在线文档