c++ - CGAL. Concave hull 3d (Alpha_Shapes_3) -
there set of points (3d space). , construct 3d conсave hull according given set of points. result want obtain subset of points, calculated conсave hull consists of. looking forward help. in advance.
i did job. #include <cgal/exact_predicates_inexact_constructions_kernel.h> #include <cgal/delaunay_triangulation_3.h> #include <cgal/triangulation_hierarchy_3.h> #include <cgal/alpha_shape_3.h> #include <cgal/io/io.h> #include <iostream> #include <sstream> #include <string> #include <fstream> #include <cgal/io/writer_off.h> #include <cgal/simple_cartesian.h> using namespace std; typedef cgal::exact_predicates_inexact_constructions_kernel k; typedef cgal::alpha_shape_vertex_base_3<k> vb; typedef cgal::triangulation_hierarchy_vertex_base_3<vb> vbh; typedef cgal::alpha_shape_cell_base_3<k> fb; typedef cgal::triangulation_data_structure_3<vbh, fb> tds; typedef cgal::delaunay_triangulation_3<k, tds> delaunay; typedef cgal::triangulation_hierarchy_3<delaunay> delaunay_hierarchy; typedef cgal::alpha_shape_3<delaunay_hierarchy> alpha_shape_3; typedef alpha_shape_3::facet facet; typedef alpha_shape_3::edge edges; typedef alpha_shape_3::vertex vertex; typedef alpha_shape_3::vertex_handle vertex_handle; typedef alpha_shape_3::alpha_iterator alpha_iterator; typedef alpha_shape_3::nt nt; typedef alpha_shape_3::vertex_iterator vertex_iterator; typedef cgal::simple_cartesian<double> kernel; typedef k::point_3 point_3; int main(int argc, const char* const argv[]) { if (argc != 3) { std::cerr << "\n there have 3 parameters (name of program, file coordinates, coefficient )" << std::endl; exit(1); } int mode = static_cast<int>(atoi(argv[1])); std::fstream is(argv[1], std::ios::in); if (!is) { std::cerr << "\n not open file " << argv[1] << " reading, exiting \n"; exit(1); } double coefficient = atof(argv[2]); std::vector<std::vector<point_3>> points; std::vector<point_3> coord; char sztempstring[1024]; std::string str = "----------end of agglomerate----------"; double dposx, dposy, dposz; while (is.getline(sztempstring, 1024)) { if (sztempstring == str) { points.push_back(coord); coord.clear(); } else { sscanf_s(sztempstring, "%lf %lf %lf", &dposx, &dposy, &dposz); coord.push_back(point_3(dposx, dposy, dposz)); } } is.close(); (size_t = 0; < points.size(); i++) { alpha_shape_3 as(points[i].begin(), points[i].end(), 0.001, alpha_shape_3::general); alpha_iterator opt = as.find_optimal_alpha(1); std::cout << "optimal alpha value 1 connected component " << std::fixed << std::setprecision(12) << *opt * coefficient << std::endl; as.set_alpha(*opt * coefficient); assert(as.number_of_solid_components() == 1); // collect regular facets std::vector<facet> facets; as.get_alpha_shape_facets(std::back_inserter(facets), alpha_shape_3::regular); std::stringstream pts; std::stringstream ind; (size_t = 0; < facets.size(); i++) { facet f = facets[i]; //to have consistent orientation of facet, consider exterior cell if (as.classify(facets[i].first) != alpha_shape_3::exterior) { facets[i] = as.mirror_facet(facets[i]); } cgal_assertion(as.classify(facets[i].first) == alpha_shape_3::exterior); int indices[3] = { (facets[i].second + 1) % 4, (facets[i].second + 2) % 4, (facets[i].second + 3) % 4 }; /// according encoding of vertex indices, needed consistent orientation if (facets[i].second % 2 == 0) { std::swap(indices[0], indices[1]); } pts << facets[i].first->vertex(indices[0])->point() << "\n" << facets[i].first->vertex(indices[1])->point() << "\n" << facets[i].first->vertex(indices[2])->point() << "\n"; ind << "3 " << 3 * << " " << 3 * + 1 << " " << 3 * + 2 << "\n"; } std::ofstream off_file; std::stringstream ss; ss << "agglomerate[" << << "].off"; off_file.open(ss.str(), std::ios::out); off_file << "off " << "\n" << 3 * facets.size() << " " << facets.size() << " 0\n"; off_file << pts.str(); off_file << ind.str(); } return 0; }
Comments
Post a Comment