CGAL 6.2 - Shape Detection

Loading...
Searching...
No Matches
Shape_detection/region_growing_lines_on_segment_set.cpp
#include <CGAL/IO/PLY.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Shape_detection/Region_growing/Region_growing.h>
#include <CGAL/Shape_detection/Region_growing/Segment_set.h>
#include <CGAL/Shape_detection/Region_growing/Polygon_mesh.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/IO/polygon_mesh_io.h>
#include "include/utils.h"
// Typedefs.
using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point_3 = typename Kernel::Point_3;
using FT = typename Kernel::FT;
using Surface_mesh = CGAL::Surface_mesh<Point_3>;
using Face_range = typename Surface_mesh::Face_range;
using Edge_range = typename Surface_mesh::Edge_range;
using One_ring_query = CGAL::Shape_detection::Polygon_mesh::One_ring_neighbor_query<Surface_mesh>;
using Plane_region = CGAL::Shape_detection::Polygon_mesh::Plane_face_region<Kernel, Surface_mesh>;
using Face_area_Sorting = CGAL::Shape_detection::Polygon_mesh::Face_area_sorting<Kernel, Surface_mesh>;
using RG_planes = CGAL::Shape_detection::Region_growing<One_ring_query, Plane_region>;
using Polyline_graph = CGAL::Shape_detection::Polygon_mesh::Polyline_graph<Surface_mesh>;
using Segment_range = typename Polyline_graph::Segment_range;
using Segment_map = typename Polyline_graph::Segment_map;
using LS_Line_region = CGAL::Shape_detection::Segment_set::Least_squares_line_fit_region<Kernel, Surface_mesh::Edge_index, Segment_map>;
using LS_Line_sorting = CGAL::Shape_detection::Segment_set::Least_squares_line_fit_sorting<Kernel, Surface_mesh::Edge_index, Polyline_graph, Segment_map>;
using Segment_Line_region = CGAL::Shape_detection::Segment_set::Line_segment_region<Kernel, Surface_mesh::Edge_index, Segment_map>;
using Length_line_sorting = CGAL::Shape_detection::Segment_set::Segment_length_sorting<Kernel, Surface_mesh::Edge_index, Segment_map>;
template<class RegionType, class Sorting_type>
void detect(Polyline_graph& pgraph, const Segment_range& segment_range, Sorting_type &line_sorting, const std::string& out_filename) {
using Region_growing = CGAL::Shape_detection::Region_growing<Polyline_graph, RegionType>;
// Create instances of the classes Neighbor_query and Region_type.
RegionType line_region(
CGAL::parameters::segment_map(pgraph.segment_map()).
maximum_distance(0).
maximum_angle(90));
line_sorting.sort();
Region_growing rg_lines(
segment_range, line_sorting.ordered(), pgraph, line_region);
std::vector<typename Region_growing::Primitive_and_region> subregions;
rg_lines.detect(std::back_inserter(subregions));
std::cout << "* number of found linear regions: " << subregions.size() << std::endl;
utils::save_segment_regions_3<Kernel, std::vector<typename Region_growing::Primitive_and_region>, Segment_map>(
subregions, out_filename, pgraph.segment_map());
}
int main(int argc, char *argv[]) {
// Load data either from a local folder or a user-provided file.
const bool is_default_input = argc > 1 ? false : true;
const std::string filename = is_default_input ? CGAL::data_file_path("meshes/step.off") : argv[1];
Surface_mesh surface_mesh;
if (!CGAL::IO::read_polygon_mesh(filename, surface_mesh)) {
std::cerr << "ERROR: cannot read the input file!" << std::endl;
return EXIT_FAILURE;
}
const Face_range face_range = faces(surface_mesh);
const Edge_range edge_range = edges(surface_mesh);
std::cout << "* number of input faces: " << face_range.size() << std::endl;
std::cout << "* number of input edges: " << edge_range.size() << std::endl;
assert(!is_default_input || face_range.size() == 16);
assert(!is_default_input || edge_range.size() == 24);
const FT max_distance = FT(10);
const FT max_angle = FT(90);
const std::size_t min_region_size = 1;
// Find planar regions.
One_ring_query one_ring_query(surface_mesh);
Plane_region plane_region(surface_mesh,
CGAL::parameters::
maximum_distance(max_distance).
maximum_angle(max_angle).
minimum_region_size(min_region_size));
Face_area_Sorting sorting(surface_mesh, one_ring_query);
sorting.sort();
RG_planes rg_planes(face_range, sorting.ordered(), one_ring_query, plane_region);
std::vector<typename RG_planes::Primitive_and_region> regions;
rg_planes.detect(std::back_inserter(regions));
std::cout << "* number of found planar regions: " << regions.size() << std::endl;
assert(!is_default_input || regions.size() == 7);
std::string fullpath = (argc > 2 ? argv[2] : "regions_sm.ply");
utils::save_polygon_mesh_regions(surface_mesh, regions, fullpath);
// Find linear regions.
Polyline_graph pgraph(surface_mesh, rg_planes.region_map());
const auto& segment_range = pgraph.segment_range();
std::cout << "* number of extracted segments: " << segment_range.size() << std::endl;
LS_Line_sorting ls_line_sorting(
segment_range, pgraph, CGAL::parameters::segment_map(pgraph.segment_map()));
Length_line_sorting length_line_sorting(segment_range, CGAL::parameters::segment_map(pgraph.segment_map()));
detect<LS_Line_region>(pgraph, segment_range, ls_line_sorting, "out_least_squares_subregions.ply");
detect<Segment_Line_region>(pgraph, segment_range, length_line_sorting, "out_segment_line_subregions.ply");
return EXIT_SUCCESS;
}
Polygon_mesh.h
A convenience header that includes all classes related to the region growing algorithm on a polygon m...
Segment_set.h
A convenience header that includes all classes related to the region growing algorithm on a segment s...
CGAL::Exact_predicates_inexact_constructions_kernel
CGAL::Shape_detection::Polygon_mesh::Face_area_sorting
Sorting of polygon mesh faces with respect to their area.
Definition: Face_area_sorting.h:46
CGAL::Shape_detection::Polygon_mesh::One_ring_neighbor_query
Edge-adjacent faces connectivity in a polygon mesh.
Definition: One_ring_neighbor_query.h:43
CGAL::Shape_detection::Polygon_mesh::Plane_face_region
Region type based on the plane of the first face selected.
Definition: Plane_face_region.h:54
CGAL::Shape_detection::Polygon_mesh::Polyline_graph
Polygon mesh edges connected into a graph.
Definition: Polyline_graph.h:44
CGAL::Shape_detection::Region_growing
Main class/entry point for running the region growing algorithm.
Definition: Region_growing.h:72
CGAL::Shape_detection::Segment_set::Least_squares_line_fit_region
Region type based on the quality of the least squares line fit applied to a segment set.
Definition: Least_squares_line_fit_region.h:53
CGAL::Shape_detection::Segment_set::Least_squares_line_fit_sorting
Sorting of segments with respect to the local line fit quality.
Definition: Least_squares_line_fit_sorting.h:51
CGAL::Shape_detection::Segment_set::Line_segment_region
Region type based on the line of the first segment selected.
Definition: Line_segment_region.h:50
CGAL::Shape_detection::Segment_set::Segment_length_sorting
Sorting of segments with respect to their length.
Definition: Segment_length_sorting.h:47
CGAL::Surface_mesh
Kernel::Point_3
RegionType
A concept that describes the set of methods used by the CGAL::Shape_detection::Region_growing to main...
Definition: RegionType.h:20
CGAL::IO::read_polygon_mesh
bool read_polygon_mesh(const std::string &fname, Graph &g, const NamedParameters &np=parameters::default_values())
CGAL::parameters
Kernel