DGtal 2.2.0
Loading...
Searching...
No Matches
TangencyComputer.h
1
16
17#pragma once
18
30
31#if defined(TangencyComputer_RECURSES)
32#error Recursive header files inclusion detected in TangencyComputer.h
33#else // defined(TangencyComputer_RECURSES)
35#define TangencyComputer_RECURSES
36
37#if !defined TangencyComputer_h
39#define TangencyComputer_h
40
42// Inclusions
43#include <iostream>
44#include <vector>
45#include <string>
46#include <limits>
47#include <unordered_set>
48#include "DGtal/base/Common.h"
49#include "DGtal/base/Clone.h"
50#include "DGtal/kernel/domains/HyperRectDomain.h"
51#include "DGtal/topology/CCellularGridSpaceND.h"
52#include "DGtal/kernel/LatticeSetByIntervals.h"
53#include "DGtal/geometry/volumes/CellGeometry.h"
54#include "DGtal/geometry/volumes/DigitalConvexity.h"
56
57namespace DGtal
58{
59
61 // template class TangencyComputer
72 template < typename TKSpace >
74 {
76
77 public:
79 typedef TKSpace KSpace;
80 typedef typename KSpace::Space Space;
81 typedef typename KSpace::Point Point;
82 typedef typename KSpace::Vector Vector;
84 typedef std::size_t Index;
85 typedef std::size_t Size;
86 typedef std::vector< Index > Path;
89
90 // ------------------------- Shortest path services --------------------------------
91 public:
92
97 typedef std::tuple< Index, Index, double > Node;
98
103 struct Comparator {
107 bool operator() ( const Node& p1,
108 const Node& p2 ) const
109 {
110 return std::get<2>( p1 ) > std::get<2>( p2 );
111 }
112 };
113
116 : myTgcyComputer( nullptr ), mySecure( 0.0 )
117 {}
118
121 ShortestPaths( const ShortestPaths& other ) = default;
124 ShortestPaths( ShortestPaths&& other ) = default;
128 ShortestPaths& operator=( const ShortestPaths& other ) = default;
132 ShortestPaths& operator=( ShortestPaths&& other ) = default;
133
151 double secure = sqrt( KSpace::dimension ) )
152 : myTgcyComputer( &tgcy_computer ),
153 mySecure( std::max( secure, 0.0 ) )
154 {
155 clear();
156 }
157
160 {
161 return myTgcyComputer;
162 }
163
165 Index size() const
166 {
167 return myTgcyComputer->size();
168 }
169
172 void clear()
173 {
174 const auto nb = size();
175 myAncestor = std::vector< Index > ( nb, nb );
176 myDistance = std::vector< double >( nb, std::numeric_limits<double>::infinity() );
177 myVisited = std::vector< bool > ( nb, false );
178 myQ = std::priority_queue< Node, std::vector< Node >, Comparator >();
179 }
180
191 void clearVisited( const std::vector<std::size_t>& visited )
192 {
193 const auto nb = size();
194 for ( auto v : visited )
195 {
196 myAncestor[ v ] = nb;
197 myDistance[ v ] = std::numeric_limits<double>::infinity();
198 myVisited [ v ] = false;
199 }
200 myQ = std::priority_queue< Node, std::vector< Node >, Comparator >();
201 }
202
206 void init( Index i )
207 {
208 ASSERT( i < size() );
209 myQ.push( std::make_tuple( i, i, 0.0 ) );
210 myAncestor[ i ] = i;
211 myDistance[ i ] = 0.0;
212 myVisited [ i ] = true;
213 }
214
221 template < typename IndexFwdIterator >
222 void init( IndexFwdIterator it, IndexFwdIterator itE )
223 {
224 for ( ; it != itE; ++it )
225 {
226 const auto i = *it;
227 ASSERT( i < size() );
228 myQ.push( std::make_tuple( i, i, 0.0 ) );
229 }
230 const auto elem = myQ.top();
231 const auto i = std::get<0>( elem );
232 myAncestor[ i ] = i;
233 myDistance[ i ] = 0.0;
234 myVisited [ i ] = true;
235 }
236
239 bool finished() const
240 {
241 return myQ.empty();
242 }
243
250 const Node& current() const
251 {
252 ASSERT( ! finished() );
253 return myQ.top();
254 }
255
260 void expand();
261
262
264 bool isValid() const
265 {
266 return myTgcyComputer != nullptr;
267 }
268
271 const Point& point( Index i ) const
272 {
273 ASSERT( i < size() );
274 return myTgcyComputer->point( i );
275 }
276
284 {
285 ASSERT( i < size() );
286 return myAncestor[ i ];
287 }
288
295 double distance( Index i ) const
296 {
297 ASSERT( i < size() );
298 return myDistance[ i ];
299 }
300
306 bool isVisited( Index i ) const
307 {
308 return ancestor( i ) < size();
309 }
310
312 static double infinity()
313 {
314 return std::numeric_limits<double>::infinity();
315 }
316
323 {
324 Path P;
325 if ( ! isVisited( i ) ) return P;
326 P.push_back( i );
327 while ( ancestor( i ) != i )
328 {
329 i = ancestor( i );
330 P.push_back( i );
331 }
332 return P;
333 }
334
338 const std::vector< Index >& ancestors() const
339 { return myAncestor; }
340
343 const std::vector< double >& distances() const
344 { return myDistance; }
345
348 const std::vector< bool >& visitedPoints() const
349 { return myVisited; }
350
351 protected:
360 double mySecure;
363 std::vector< Index > myAncestor;
365 std::vector< double > myDistance;
367 std::vector< bool > myVisited;
369 std::priority_queue< Node, std::vector< Node >, Comparator > myQ;
370
371 protected:
372
378
385 std::vector< Index >
387
388 };
389
391 friend struct ShortestPaths;
392
393 // ------------------------- Standard services --------------------------------
394 public:
397
399 TangencyComputer() = default;
400
403 TangencyComputer( const Self& other ) = default;
404
407 TangencyComputer( Self&& other ) = default;
408
412 Self& operator=( const Self& other ) = default;
413
417 Self& operator=( Self&& other ) = default;
418
422
434 template < typename PointIterator >
435 void init( PointIterator itB, PointIterator itE,
436 bool use_lattice_cell_cover = false );
437
439
440 // ------------------------- Accessors services --------------------------------
441 public:
444
446 const KSpace& space() const
447 { return myK; }
448
450 Size size() const
451 { return myX.size(); }
452
454 const std::vector< Point >& points() const
455 { return myX; }
456
459 const Point& point( Index i ) const
460 { return myX[ i ]; }
461
465 Size index( const Point& a ) const
466 {
467 const auto p = myPt2Index.find( a );
468 return p == myPt2Index.cend() ? size() : p->second;
469 }
470
472 const CellCover& cellCover() const
473 { return myCellCover; }
474
479
482 double length( const Path& path ) const
483 {
484 auto eucl_d = [] ( const Point& p, const Point& q )
485 { return ( p - q ).norm(); };
486 double l = 0.0;
487 for ( size_t i = 1; i < path.size(); i++ )
488 l += eucl_d( point( path[ i-1 ] ), point( path[ i ] ) );
489 return l;
490 }
491
493
494 // ------------------------- Tangency services --------------------------------
495 public:
498
503 bool arePointsCotangent( const Point& a, const Point& b ) const;
504
510 bool arePointsCotangent( const Point& a, const Point& b, const Point& c ) const;
511
515 std::vector< Index >
516 getCotangentPoints( const Point& a ) const;
517
525 std::vector< Index >
527 const std::vector< bool > & to_avoid ) const;
528
533 std::vector< Index >
534 getCotangentPoints( const Point& a, double max_discrete_distance ) const;
535
537
538 // ------------------------- Shortest paths services --------------------------------
539 public:
542
559 ShortestPaths
560 makeShortestPaths( double secure = sqrt( KSpace::dimension ) ) const;
561
586 std::vector< Path >
587 shortestPaths( const std::vector< Index >& sources,
588 const std::vector< Index >& targets,
589 double secure = sqrt( KSpace::dimension ),
590 bool verbose = false ) const;
591
617 Path
618 shortestPath( Index source, Index target,
619 double secure = sqrt( KSpace::dimension ),
620 bool verbose = false ) const;
621
623
624 // ------------------------- Protected Data ------------------------------
625 protected:
626
632 std::vector< Vector > myN;
635 std::vector< double > myDN;
637 std::vector< Point > myX;
646
648 std::unordered_map< Point, Index > myPt2Index;
649
650 // ------------------------- Private Data --------------------------------
651 private:
652
653
654 // ------------------------- Internals ------------------------------------
655 private:
656
658 void setUp();
659
660 }; // end of class TangencyComputer
661
664
671 template <typename TKSpace>
672 std::ostream&
673 operator<< ( std::ostream & out,
674 const TangencyComputer<TKSpace> & object );
675
677
678} // namespace DGtal
679
680
682// Includes inline functions.
683#include "TangencyComputer.ih"
684
685// //
687
688#endif // !defined TangencyComputer_h
689
690#undef TangencyComputer_RECURSES
691#endif // else defined(TangencyComputer_RECURSES)
Aim: This class encapsulates its parameter class to indicate that the given parameter is required to ...
Definition Clone.h:266
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition ConstAlias.h:187
PointVector< dim, Integer > Point
PointVector< dim, Integer > Vector
static const constexpr Dimension dimension
Aim: A class that computes tangency to a given digital set. It provides services to compute all the c...
const LatticeCellCover & latticeCellCover() const
LatticeCellCover myLatticeCellCover
std::vector< Vector > myN
TangencyComputer(Self &&other)=default
DigitalConvexity< KSpace > myDConv
const KSpace & space() const
void setUp()
Precomputes some neighborhood tables at construction.
CellCover myCellCover
Size index(const Point &a) const
bool arePointsCotangent(const Point &a, const Point &b, const Point &c) const
std::unordered_map< Point, Index > myPt2Index
TangencyComputer< TKSpace > Self
const std::vector< Point > & points() const
HyperRectDomain< Space > Domain
std::vector< Index > Path
LatticeSetByIntervals< Space > LatticeCellCover
double length(const Path &path) const
std::vector< Index > getCotangentPoints(const Point &a) const
bool arePointsCotangent(const Point &a, const Point &b) const
std::vector< double > myDN
TangencyComputer(const Self &other)=default
TangencyComputer(Clone< KSpace > aK)
std::vector< Index > getCotangentPoints(const Point &a, double max_discrete_distance) const
Self & operator=(const Self &other)=default
std::vector< Point > myX
const Point & point(Index i) const
const CellCover & cellCover() const
ShortestPaths makeShortestPaths(double secure=sqrt(KSpace::dimension)) const
Path shortestPath(Index source, Index target, double secure=sqrt(KSpace::dimension), bool verbose=false) const
CellGeometry< KSpace > CellCover
std::vector< Path > shortestPaths(const std::vector< Index > &sources, const std::vector< Index > &targets, double secure=sqrt(KSpace::dimension), bool verbose=false) const
Self & operator=(Self &&other)=default
BOOST_CONCEPT_ASSERT((concepts::CCellularGridSpaceND< TKSpace >))
std::vector< Index > getCotangentPoints(const Point &a, const std::vector< bool > &to_avoid) const
TangencyComputer()=default
Constructor. The object is invalid.
void init(PointIterator itB, PointIterator itE, bool use_lattice_cell_cover=false)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
STL namespace.
bool operator()(const Node &p1, const Node &p2) const
std::priority_queue< Node, std::vector< Node >, Comparator > myQ
The queue of points being currently processed.
std::vector< Index > getCotangentPoints(Index i) const
ShortestPaths(ShortestPaths &&other)=default
std::vector< bool > myVisited
Remembers for each point if it is already visited.
const TangencyComputer * tangencyComputerPtr() const
const std::vector< bool > & visitedPoints() const
ShortestPaths(const ShortestPaths &other)=default
const std::vector< Index > & ancestors() const
ShortestPaths & operator=(const ShortestPaths &other)=default
ShortestPaths(ConstAlias< TangencyComputer > tgcy_computer, double secure=sqrt(KSpace::dimension))
ShortestPaths & operator=(ShortestPaths &&other)=default
ShortestPaths()
Default constructor. The object is not valid.
void init(IndexFwdIterator it, IndexFwdIterator itE)
std::vector< double > myDistance
Stores for each point its distance to the closest source.
const Point & point(Index i) const
void clearVisited(const std::vector< std::size_t > &visited)
const std::vector< double > & distances() const
const TangencyComputer * myTgcyComputer
A pointer toward the tangency computer.
std::tuple< Index, Index, double > Node
Type used for Dijkstra's algorithm queue (point, ancestor, distance).
Aim: This concept describes a cellular grid space in nD. In these spaces obtained by cartesian produc...
int max(int a, int b)