DGtal  1.5.beta
testVoronoiMapComplete.cpp
Go to the documentation of this file.
1 
29 #include <limits>
30 #include <unordered_set>
31 #include "DGtal/base/Common.h"
32 #include "DGtal/helpers/StdDefs.h"
33 #include "DGtalCatch.h"
34 
35 #include "DGtal/geometry/volumes/distance/VoronoiMapComplete.h"
36 #include "DGtal/geometry/volumes/distance/VoronoiMap.h"
38 using namespace DGtal;
39 
41 typedef Z2i::Point Point;
43 
47  std::unordered_set<Z2i::Point>>
49 
51 {
52  TImageContainer * voronoi_map = new TImageContainer( set.domain() );
53  std::vector<Point> Sites;
54  // Getting all voronoi sites in one vector
55  for ( Point point : set.domain() )
56  {
57  if ( !set( point ) )
58  {
59  Sites.push_back( point );
60  }
61  }
62  std::cout << std::endl;
63 
64  for ( Point point : set.domain() )
65  {
66 
67  std::unordered_set<Point> voronoi_points;
68  double voronoi_distance =
69  std::numeric_limits<int>::max(); // maximum int value
70  for ( Point site : Sites )
71  {
72  if ( ( point - site ).norm() < voronoi_distance )
73  {
74  voronoi_points.clear();
75  voronoi_points.insert( site );
76  voronoi_distance = ( point - site ).norm();
77  }
78  else if ( ( point - site ).norm() == voronoi_distance )
79  {
80  voronoi_points.insert( site );
81  }
82  }
83  voronoi_map->setValue( point, voronoi_points );
84  }
85  return voronoi_map;
86 }
87 
89 // Functions for testing class VoronoiMapComplete
91 TEST_CASE( "Testing VoronoiMapComplete 2D" )
92 {
96  Point lowerBound( 0, 0 ), upperBound( 31, 31 );
97  Domain domain( lowerBound, upperBound );
98 
99  DigitalSet set( domain );
100  int point_setup_index = 0;
101  int x;
102  int y;
103 
104  while ( point_setup_index < 400 )
105  {
106  x = rand() % 32;
107  y = rand() % 32;
108  set.insert( Point( x, y ) );
109  point_setup_index++;
110  }
111 
115  CompleteVMap vmap( set.domain(), set, Z2i::l2Metric );
116 
120  TImageContainer * brut_force_vmap = brut_force_voronoi_map_complete( set );
121 
122  SECTION(
123  "Testing that VoronoiMapComplete class gives the right voronoi sites sets" )
124  {
125  for ( Point point : set )
126  {
127  std::unordered_set<Point> brut_force_set =
128  brut_force_vmap->operator()( point );
129  CompleteVMap::Value class_set = vmap( point );
130 
131  // Cheking that all voronoi sites from the brut force set are in the
132  // algorithm set
133  for ( Point voronoi_point : brut_force_set )
134  {
135  REQUIRE( std::find( class_set.begin(), class_set.end(),
136  voronoi_point ) != class_set.end() );
137  }
138 
139  // Cheking that all voronoi sites from the algorithm set are in the brut
140  // force set
141  for ( Point voronoi_point : class_set )
142  {
143  REQUIRE( std::find( brut_force_set.begin(), brut_force_set.end(),
144  voronoi_point ) != brut_force_set.end() );
145  }
146  }
147  }
148 
149  SECTION(
150  "Testing Complete Voronoi Map from Discrete Bisector Function paper" )
151  {
152  Point _lowerBound( 0, 0 ), _upperBound( 6, 7 );
153  Domain _domain( _lowerBound, _upperBound );
154  DigitalSet _set( _domain );
155  for ( Point point : _set.domain() )
156  {
157  if ( point != Point( 1, 0 ) && point != Point( 5, 0 ) &&
158  point != Point( 2, 2 ) && point != Point( 4, 4 ) &&
159  point != Point( 0, 6 ) && point != Point( 6, 6 ) &&
160  point != Point( 3, 7 ) )
161  _set.insert( point );
162  }
163 
164  TImageContainer * brutForceVoronoiMap =
166  CompleteVMap _vmap( _set.domain(), _set, Z2i::l2Metric );
167 
168  for ( Point point : _set )
169  {
170  std::unordered_set<Point> brut_force_set =
171  brutForceVoronoiMap->operator()( point );
172  CompleteVMap::Value class_set = _vmap( point );
173 
174  // Cheking that all voronoi sites from the brut force set are in the
175  // algorithm set
176  for ( Point voronoi_point : brut_force_set )
177  REQUIRE( std::find( class_set.begin(), class_set.end(),
178  voronoi_point ) != class_set.end() );
179 
180  // Cheking that all voronoi sites from the algorithm set are in the brut
181  // force set
182  for ( Point voronoi_point : class_set )
183  REQUIRE( std::find( brut_force_set.begin(), brut_force_set.end(),
184  voronoi_point ) != brut_force_set.end() );
185  }
186  }
187 }
188 
189 TEST_CASE( "No sites" )
190 {
194  Point lowerBound( 0, 0 ), upperBound( 255, 255 );
195  Domain domain( lowerBound, upperBound );
196  DigitalSet set( domain );
197 
198  trace.beginBlock( "Complete Map (no site)" );
199  CompleteVMap vmap( set.domain(), set, Z2i::l2Metric );
200  trace.endBlock();
201  trace.beginBlock( "Partial Map (no site)" );
202  VMap partialmap( set.domain(), set, Z2i::l2Metric );
203  trace.endBlock();
204 }
205 
206 TEST_CASE( "Testing Timings" )
207 {
211  Point lowerBound( 0, 0 ), upperBound( 255, 255 );
212  Domain domain( lowerBound, upperBound );
213 
214  DigitalSet set( domain );
215  int point_setup_index = 0;
216  int x;
217  int y;
218 
219  while ( point_setup_index < 5000 )
220  {
221  x = rand() % 256;
222  y = rand() % 256;
223  set.insert( Point( x, y ) );
224  point_setup_index++;
225  }
226  std::cout << std::endl;
227  trace.beginBlock( "Complete Map" );
228  CompleteVMap vmap( set.domain(), set, Z2i::l2Metric );
229  trace.endBlock();
230 
231  size_t maxsize = 0;
232  for ( auto & v : vmap.constRange() )
233  maxsize = std::max( maxsize, v.size() );
234  trace.info() << "Max number of co-cyclic points = " << maxsize << std::endl;
235 
236  trace.beginBlock( "Partial Map" );
237  VMap partialmap( set.domain(), set, Z2i::l2Metric );
238  trace.endBlock();
239 }
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
void setValue(const Point &aPoint, const Value &aValue)
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Aim: Implementation of the linear in time Voronoi map construction.
OutputImage::Value Value
Definition of the image value type.
ConstRange constRange() const
Aim: Implementation of the linear in time Voronoi map construction.
Definition: VoronoiMap.h:127
static const L2Metric l2Metric
Definition: StdDefs.h:123
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
int max(int a, int b)
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
SECTION("Testing constant forward iterators")
REQUIRE(domain.isInside(aPoint))
Z2i::Point Point
ImageContainerBySTLVector< HyperRectDomain< Z2i::Space >, std::unordered_set< Z2i::Point > > TImageContainer
TImageContainer * brut_force_voronoi_map_complete(DigitalSet &set)
TEST_CASE("Testing VoronoiMapComplete 2D")
Z2i::DigitalSet DigitalSet
VoronoiMapComplete< Z2i::Space, DigitalSet, Z2i::L2Metric > CompleteVMap
Z2i::Domain Domain
VoronoiMap< Z2i::Space, DigitalSet, Z2i::L2Metric > VMap