#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
#include <CGAL/Shape_detection/Region_growing/Region_growing.h>
namespace Custom {
struct Object {
std::vector<std::size_t> neighbors;
};
using Objects = std::vector<Object>;
class Neighbor_query {
const Objects& m_objects;
public:
Neighbor_query(const Objects& objects) :
m_objects(objects)
{ }
void operator()(
const std::size_t query_index,
std::vector<std::size_t>& neighbors) const {
std::size_t i = 0;
for (const auto& object : m_objects) {
if (i == query_index) {
neighbors = object.neighbors;
return;
} ++i;
}
}
};
class Region_type {
bool m_is_valid = false;
public:
Region_type() { }
bool is_part_of_region(
const std::size_t,
const std::size_t query_index,
const std::vector<std::size_t>& region) const {
if (region.size() == 0)
return false;
const std::size_t index = region[0];
if (index == 0 && query_index == 1) return true;
if (query_index == 0 && index == 1) return true;
return false;
}
inline bool is_valid_region(const std::vector<std::size_t>&) const {
return m_is_valid;
}
void update(const std::vector<std::size_t>&) {
m_is_valid = true;
}
};
class Seed_map {
public:
using key_type = std::size_t;
using value_type = std::size_t;
using category = boost::lvalue_property_map_tag;
Seed_map(const std::map<std::size_t, std::size_t>& objects_map) :
m_objects_map(objects_map)
{ }
value_type operator[](const key_type key) const {
return m_objects_map.find(key)->second;
}
friend value_type get(
const Seed_map& seed_map,
const key_type key) {
return seed_map[key];
}
private:
const std::map<std::size_t, std::size_t>& m_objects_map;
};
}
using Object = Custom::Object;
using Objects = Custom::Objects;
using Neighbor_query = Custom::Neighbor_query;
using Region_type = Custom::Region_type;
using Seed_map = Custom::Seed_map;
using Region = std::vector<std::size_t>;
using Regions = std::vector<Region>;
using Region_growing =
int main() {
std::cout << std::endl <<
"region_growing_with_custom_classes example started"
<< std::endl << std::endl;
Objects objects;
Object object1;
object1.neighbors.resize(1, 1);
objects.push_back(object1);
Object object2;
object2.neighbors.resize(1, 0);
objects.push_back(object2);
Object object3;
objects.push_back(object3);
Object object4;
objects.push_back(object4);
Neighbor_query neighbor_query = Neighbor_query(objects);
Region_type region_type = Region_type();
std::map<std::size_t, std::size_t> objects_map;
objects_map[0] = 1;
objects_map[1] = 0;
objects_map[2] = 2;
objects_map[3] = std::size_t(-1);
const Seed_map seed_map(objects_map);
Region_growing region_growing(
objects, neighbor_query, region_type, seed_map);
Regions regions;
region_growing.detect(std::back_inserter(regions));
std::cout << "* " << regions.size() <<
" regions have been found among " << objects.size() << " objects"
<< std::endl;
std::cout << std::endl <<
"region_growing_with_custom_classes example finished"
<< std::endl << std::endl;
return EXIT_SUCCESS;
}