File: region_growing_with_custom_classes.cpp

package info (click to toggle)
cgal 6.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 141,840 kB
  • sloc: cpp: 797,081; ansic: 203,398; sh: 490; python: 411; makefile: 286; javascript: 174
file content (159 lines) | stat: -rw-r--r-- 4,494 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// STL includes.
#include <unordered_map>
#include <list>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
#include <cassert>

// CGAL includes.
#include "include/utils.h"
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Shape_detection/Region_growing/Region_growing.h>

// Custom Neighbor_query and Region_type classes for region growing.
namespace Custom {

  // An object that stores indices of all its neighbors.
  struct Object {
    std::vector<std::vector<Object>::const_iterator> neighbors;
    bool operator==(const Object& obj) const {
      return neighbors == obj.neighbors;
    }
  };

  // A range of objects.
  using Objects = std::vector<Object>; // std::list<Object> works as well

  // The Neighbor_query functor that accesses neighbors stored in
  // the object struct above.
  class Neighbor_query {
    const Objects& m_objects;

    using Item = typename Objects::const_iterator;
    using Region = std::vector<Item>;

  public:
    Neighbor_query(const Objects& objects) :
    m_objects(objects)
    { }

    void operator()(
      const Item &query,
      std::vector<Item>& neighbors) const {

      for (auto it = m_objects.begin(); it != m_objects.end(); it++) {
        if (it == query) {
          neighbors = query->neighbors;
          return;
        }
      }
    }
  };

  // The Region_type class, where the function is_part_of_region() verifies
  // a very specific condition that the first and second objects in the
  // range are in fact neighbors; is_valid_region() function always
  // returns true after the first call to the update() function.
  // These are the only functions that have to be defined.
  class Region_type {
    bool m_is_valid = false;
    const std::vector<Object>& m_input;

  public:
    Region_type(const std::vector<Object> &input) : m_input(input) { }

    using Primitive = std::size_t;

    using Item = std::vector<Object>::const_iterator;
    using Region = std::vector<Item>;

    struct hash_item {
      std::size_t operator()(Item i) const {
        using boost::hash_value;
        return boost::hash_value(i.operator->());
      }
    };

    using Region_unordered_map = std::unordered_map<Item, std::size_t, hash_item >;
    using Region_index_map = boost::associative_property_map<Region_unordered_map>;

    Region_index_map region_index_map() {
      Region_index_map index_map(m_region_map);
      return index_map;
    }

    bool is_part_of_region(
      const Item query,
      const Region& region) const {

      if (region.size() == 0) return false;

      auto it = m_input.begin();

      if (query == it || query == (it + 1))
        return true;

      return false;
    }

    inline bool is_valid_region(const Region&) const {
      return m_is_valid;
    }

    Primitive primitive() {
      return Primitive();
    }

    bool update(const Region&) {
      m_is_valid = true;
      return m_is_valid;
    }

  private:
    Region_unordered_map m_region_map;
  };

} // namespace Custom

// Typedefs.
using Object         = Custom::Object;
using Objects        = Custom::Objects;
using Neighbor_query = Custom::Neighbor_query;
using Region_type    = Custom::Region_type;
using Region_growing = CGAL::Shape_detection::Region_growing<Neighbor_query, Region_type>;

int main() {

  // Define a range of objects, where the first two objects form
  // the first region, while the third object forms the second region.
  // Note that Objects is a random access container here, however the
  // same algorithm/example can work with other containers, e.g. std::list.
  Objects objects(3);
  auto it = objects.begin();

  // Region 1.
  objects[0].neighbors.push_back(it+1);
  objects[1].neighbors.push_back(it);

  // The single third object constitutes the second region.

  std::cout << "* number of input objects: " << objects.size() << std::endl;
  assert(objects.size() == 3);

  // Create instances of the classes Neighbor_query and Region_type.
  Neighbor_query neighbor_query = Neighbor_query(objects);
  Region_type    region_type    = Region_type(objects);

  // Create an instance of the region growing class.
  Region_growing region_growing(
    objects, neighbor_query, region_type);

  // Run the algorithm.
  std::vector<typename Region_growing::Primitive_and_region> regions;
  region_growing.detect(std::back_inserter(regions));
  std::cout << "* number of found regions: " << regions.size() << std::endl;
  assert(regions.size() == 2);
  return EXIT_SUCCESS;
}