欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
}

CGAL文件格式的转换

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;
}

CGAL文件格式的转换

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;
}

CGAL文件格式的转换

4、.mesh转.vtu文件

CGAL文件格式的转换

//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;
}
相关标签: 博客快捷键