DGtal 1.3.0
Loading...
Searching...
No Matches
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
41template <typename TDomain, typename TAdjacency>
42inline
43DGtal::DomainAdjacency<TDomain,TAdjacency>
44::~DomainAdjacency()
45{}
46//------------------------------------------------------------------------------
47template <typename TDomain, typename TAdjacency>
48inline
49DGtal::DomainAdjacency<TDomain,TAdjacency>
50::DomainAdjacency( ConstAlias<Domain> aDomain,
51 ConstAlias<Adjacency> adjacency )
52 : myPred( aDomain ), myAdjacency( adjacency )
53{
54}
55//------------------------------------------------------------------------------
56template <typename TDomain, typename TAdjacency>
57inline
58DGtal::DomainAdjacency<TDomain,TAdjacency>
59::DomainAdjacency( const DomainAdjacency & other )
60 : myPred( other.myPred ), myAdjacency( other.myAdjacency )
61{
62}
63//------------------------------------------------------------------------------
64template <typename TDomain, typename TAdjacency>
65inline
66const TDomain &
67DGtal::DomainAdjacency<TDomain,TAdjacency>
68::domain() const
69{
70 return myPred.domain();
71}
72//------------------------------------------------------------------------------
73template <typename TDomain, typename TAdjacency>
74inline
75const typename DGtal::DomainAdjacency<TDomain,TAdjacency>::Predicate &
76DGtal::DomainAdjacency<TDomain,TAdjacency>
77::predicate() const
78{
79 return myPred;
80}
81//------------------------------------------------------------------------------
82template <typename TDomain, typename TAdjacency>
83inline
84bool
85DGtal::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//------------------------------------------------------------------------------
93template <typename TDomain, typename TAdjacency>
94inline
95bool
96DGtal::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//------------------------------------------------------------------------------
104template <typename TDomain, typename TAdjacency>
105inline
106void
107DGtal::DomainAdjacency<TDomain,TAdjacency>
108::selfDisplay ( std::ostream & out ) const
109{
110 out << "[DomainAdjacency]";
111}
112//------------------------------------------------------------------------------
113template <typename TDomain, typename TAdjacency>
114inline
115bool
116DGtal::DomainAdjacency<TDomain,TAdjacency>
117::isValid() const
118{
119 return true;
120}
121//------------------------------------------------------------------------------
122template <typename TDomain, typename TAdjacency>
123inline
124std::ostream&
125DGtal::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 */
138template <typename TDomain, typename TAdjacency>
139inline
140typename DGtal::DomainAdjacency<TDomain, TAdjacency>::Size
141DGtal::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 */
151template <typename TDomain, typename TAdjacency>
152inline
153typename DGtal::DomainAdjacency<TDomain, TAdjacency>::Size
154DGtal::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 */
175template <typename TDomain, typename TAdjacency>
176template <typename OutputIterator>
177inline
178void
179DGtal::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 */
204template <typename TDomain, typename TAdjacency>
205template <typename OutputIterator, typename VertexPredicate>
206void
207DGtal::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