DGtal  1.2.0
DomainAdjacency.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file DomainAdjacency.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
21  *
22  * @date 2010/07/10
23  *
24  * Implementation of inline methods defined in DomainAdjacency.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 #include "DGtal/topology/MetricAdjacency.h"
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 template <typename TDomain, typename TAdjacency>
42 inline
43 DGtal::DomainAdjacency<TDomain,TAdjacency>
44 ::~DomainAdjacency()
45 {}
46 //------------------------------------------------------------------------------
47 template <typename TDomain, typename TAdjacency>
48 inline
49 DGtal::DomainAdjacency<TDomain,TAdjacency>
50 ::DomainAdjacency( ConstAlias<Domain> aDomain,
51  ConstAlias<Adjacency> adjacency )
52  : myPred( aDomain ), myAdjacency( adjacency )
53 {
54 }
55 //------------------------------------------------------------------------------
56 template <typename TDomain, typename TAdjacency>
57 inline
58 DGtal::DomainAdjacency<TDomain,TAdjacency>
59 ::DomainAdjacency( const DomainAdjacency & other )
60  : myPred( other.myPred ), myAdjacency( other.myAdjacency )
61 {
62 }
63 //------------------------------------------------------------------------------
64 template <typename TDomain, typename TAdjacency>
65 inline
66 const TDomain &
67 DGtal::DomainAdjacency<TDomain,TAdjacency>
68 ::domain() const
69 {
70  return myPred.domain();
71 }
72 //------------------------------------------------------------------------------
73 template <typename TDomain, typename TAdjacency>
74 inline
75 const typename DGtal::DomainAdjacency<TDomain,TAdjacency>::Predicate &
76 DGtal::DomainAdjacency<TDomain,TAdjacency>
77 ::predicate() const
78 {
79  return myPred;
80 }
81 //------------------------------------------------------------------------------
82 template <typename TDomain, typename TAdjacency>
83 inline
84 bool
85 DGtal::DomainAdjacency<TDomain,TAdjacency>
86 ::isAdjacentTo( const Point & p1, const Point & p2 ) const
87 {
88  ASSERT( myPred( p1 ) );
89  ASSERT( myPred( p2 ) );
90  return myAdjacency.isAdjacentTo( p1, p2 );
91 }
92 //------------------------------------------------------------------------------
93 template <typename TDomain, typename TAdjacency>
94 inline
95 bool
96 DGtal::DomainAdjacency<TDomain,TAdjacency>
97 ::isProperlyAdjacentTo( const Point & p1, const Point & p2 ) const
98 {
99  ASSERT( myPred( p1 ) );
100  ASSERT( myPred( p2 ) );
101  return myAdjacency.isProperlyAdjacentTo( p1, p2 );
102 }
103 //------------------------------------------------------------------------------
104 template <typename TDomain, typename TAdjacency>
105 inline
106 void
107 DGtal::DomainAdjacency<TDomain,TAdjacency>
108 ::selfDisplay ( std::ostream & out ) const
109 {
110  out << "[DomainAdjacency]";
111 }
112 //------------------------------------------------------------------------------
113 template <typename TDomain, typename TAdjacency>
114 inline
115 bool
116 DGtal::DomainAdjacency<TDomain,TAdjacency>
117 ::isValid() const
118 {
119  return true;
120 }
121 //------------------------------------------------------------------------------
122 template <typename TDomain, typename TAdjacency>
123 inline
124 std::ostream&
125 DGtal::operator<< ( std::ostream & out,
126  const DomainAdjacency<TDomain,TAdjacency> & object )
127 {
128  object.selfDisplay( out );
129  return out;
130 }
131 
132 ///////////////////////////////////////////////////////////////////////////////
133 // ----------------------- Local graph services -----------------------------
134 
135 /**
136  * @return maximum number of neighbors for this adjacency
137  */
138 template <typename TDomain, typename TAdjacency>
139 inline
140 typename DGtal::DomainAdjacency<TDomain, TAdjacency>::Size
141 DGtal::DomainAdjacency<TDomain, TAdjacency>::bestCapacity() const
142 {
143  return myAdjacency.bestCapacity();
144 }
145 
146 /**
147  * @param v any vertex
148  *
149  * @return the number of neighbors of this vertex
150  */
151 template <typename TDomain, typename TAdjacency>
152 inline
153 typename DGtal::DomainAdjacency<TDomain, TAdjacency>::Size
154 DGtal::DomainAdjacency<TDomain, TAdjacency>::degree
155 ( const Vertex & v ) const
156 {
157  std::vector<Vertex> vect;
158  std::back_insert_iterator< std::vector<Vertex> > out_it(vect);
159  myAdjacency.writeNeighbors( out_it, v, myPred );
160  return static_cast<Size>( vect.size() );
161  // return myAdjacency.degree( v );
162 }
163 
164 /**
165  * Writes the neighbors of a vertex using an output iterator
166  *
167  *
168  * @tparam OutputIterator the type of an output iterator writing
169  * in a container of vertices.
170  *
171  * @param it the output iterator
172  *
173  * @param v the vertex whose neighbors will be written
174  */
175 template <typename TDomain, typename TAdjacency>
176 template <typename OutputIterator>
177 inline
178 void
179 DGtal::DomainAdjacency<TDomain, TAdjacency>::writeNeighbors
180 ( OutputIterator &it, const Vertex & v ) const
181 {
182  //myAdjacency.writeProperNeighborhood( v, it, myPred );//writeNeighbors<OutputIterator>( v, it );
183  // myAdjacency.writeNeighbors( it, v );
184  myAdjacency.writeNeighbors( it, v, myPred );
185 
186 }
187 
188 /**
189  * Writes the neighbors of a vertex which satisfy a predicate using an
190  * output iterator
191  *
192  *
193  * @tparam OutputIterator the type of an output iterator writing
194  * in a container of vertices.
195  *
196  * @tparam VertexPredicate the type of the predicate
197  *
198  * @param it the output iterator
199  *
200  * @param v the vertex whose neighbors will be written
201  *
202  * @param pred the predicate that must be satisfied
203  */
204 template <typename TDomain, typename TAdjacency>
205 template <typename OutputIterator, typename VertexPredicate>
206 void
207 DGtal::DomainAdjacency<TDomain, TAdjacency>::writeNeighbors
208 ( OutputIterator &it, const Vertex & v, const VertexPredicate & pred) const
209 {
210  std::vector<Vertex> vect;
211  std::back_insert_iterator< std::vector<Vertex> > out_it(vect);
212  //myAdjacency.writeProperNeighborhood( vect, out_it, myPred );
213  myAdjacency.writeNeighbors( out_it, v, myPred );
214  for( typename std::vector<Vertex>::const_iterator cit = vect.begin(); cit != vect.end(); cit ++ )
215  {
216  if( pred(*cit) )
217  {
218  *it++ = *cit;
219  }
220  }
221  //myAdjacency.writeNeighbors<OutputIterator, VertexPredicate>( v, it, pred );
222 }
223 
224 // //
225 ///////////////////////////////////////////////////////////////////////////////
226 
227