DGtal  1.5.beta
DomainMetricAdjacency.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 DomainMetricAdjacency.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 DomainMetricAdjacency.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 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 /**
42  * Constructor from a domain.
43  * @param aDomain any domain.
44  */
45 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
46 inline
47 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>
48 ::DomainMetricAdjacency( ConstAlias<Domain> aDomain )
49  : myDomain( aDomain )
50 {
51 }
52 
53 /**
54  * Copy Constructor.
55  * @param other the object to clone.
56  */
57 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
58 inline
59 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>
60 ::DomainMetricAdjacency( const DomainMetricAdjacency & other )
61  : myDomain( other.myDomain )
62 {
63 }
64 
65 /**
66  * Destructor.
67  */
68 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
69 inline
70 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>
71 ::~DomainMetricAdjacency()
72 {
73 }
74 
75 
76 ///////////////////////////////////////////////////////////////////////////////
77 // ----------------------- Adjacency services -----------------------------
78 
79 /**
80  * @param p1 any point in this space.
81  * @param p2 any point in this space.
82  *
83  * @return 'true' iff p1 is adjacent to p2 according to this
84  * adjacency relation.
85  */
86 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
87 inline
88 bool
89 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>::isAdjacentTo
90 ( const Point & p1, const Point & p2 ) const
91 {
92  ASSERT( myDomain.isInside( p1 ) && myDomain.isInside( p2 ) );
93  Vector v( p2 - p1 );
94  return ( v.normInfinity() <= 1 ) && ( v.norm1() <= maxNorm1 );
95 }
96 
97 /**
98  * @param p1 any point in this space.
99  * @param p2 any point in this space.
100  *
101  * @return 'true' iff p1 is adjacent to p2 according to this
102  * adjacency relation and p1 != p2.
103  */
104 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
105 inline
106 bool
107 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>::isProperlyAdjacentTo
108 ( const Point & p1, const Point & p2 ) const
109 {
110  ASSERT( myDomain.isInside( p1 ) && myDomain.isInside( p2 ) );
111  Vector v( p2 - p1 );
112  if ( v.normInfinity() <= 1 )
113  {
114  typename Vector::UnsignedComponent n1 = v.norm1();
115  return ( n1 <= maxNorm1 ) && ( n1 != 0 );
116  }
117  return false;
118 }
119 
120 /**
121  * Outputs the whole neighborhood of point [p] as a sequence of
122  * *out_it++ = ...
123  *
124  * @tparam OutputIterator any output iterator (like
125  * std::back_insert_iterator< std::vector<int> >).
126  *
127  * @param p any point of this space.
128  * @param out_it any output iterator.
129  */
130 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
131 template <typename OutputIterator>
132 void
133 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>::writeNeighborhood
134 ( const Point & p, OutputIterator & out_it ) const
135 {
136  Point p1( p );
137  for ( typename Point::Iterator it = p1.begin(); it != p1.end(); ++it )
138  --(*it);
139  Point p2( p );
140  for ( typename Point::Iterator it = p2.begin(); it != p2.end(); ++it )
141  ++(*it);
142  typedef HyperRectDomain<Space> LocalDomain;
143  LocalDomain domain( p1, p2 );
144  for ( typename LocalDomain::ConstIterator it = domain.begin();
145  it != domain.end();
146  ++it )
147  {
148  if ( myDomain.isInside( *it ) )
149  {
150  Vector v( p - *it );
151  typename Vector::UnsignedComponent n1 = v.norm1();
152  if ( n1 <= maxNorm1 )
153  *out_it++ = *it;
154  }
155  }
156 }
157 
158 /**
159  * Outputs the whole proper neighborhood of point [p] (thus
160  * without [p] itself) as a sequence of *out_it++ = ...
161  *
162  * @tparam OutputIterator any output iterator (like
163  * std::back_insert_iterator< std::vector<int> >).
164  *
165  * @param p any point of this space.
166  * @param out_it any output iterator.
167  */
168 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
169 template <typename OutputIterator>
170 void
171 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>::writeProperNeighborhood
172 ( const Point & p, OutputIterator & out_it ) const
173 {
174  Point p1( p );
175  for ( typename Point::Iterator it = p1.begin(); it != p1.end(); ++it )
176  --(*it);
177  Point p2( p );
178  for ( typename Point::Iterator it = p2.begin(); it != p2.end(); ++it )
179  ++(*it);
180  typedef HyperRectDomain<Space> LocalDomain;
181  LocalDomain domain( p1, p2 );
182  for ( typename LocalDomain::ConstIterator it = domain.begin();
183  it != domain.end();
184  ++it )
185  {
186  if ( myDomain.isInside( *it ) )
187  {
188  Vector v( p - *it );
189  typename Vector::UnsignedComponent n1 = v.norm1();
190  if ( ( n1 <= maxNorm1 ) && ( n1 != 0 ) )
191  *out_it++ = *it;
192  }
193  }
194 }
195 
196 
197 
198 ///////////////////////////////////////////////////////////////////////////////
199 // Interface - public :
200 
201 /**
202  * Writes/Displays the object on an output stream.
203  * @param out the output stream where the object is written.
204  */
205 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
206 inline
207 void
208 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>::selfDisplay ( std::ostream & out ) const
209 {
210  out << "[DomainMetricAdjacency domain="
211  << myDomain << "]";
212 }
213 
214 /**
215  * Checks the validity/consistency of the object.
216  * @return 'true' if the object is valid, 'false' otherwise.
217  */
218 template <typename Domain, DGtal::Dimension maxNorm1, DGtal::Dimension dimension>
219 inline
220 bool
221 DGtal::deprecated::DomainMetricAdjacency<Domain,maxNorm1,dimension>::isValid() const
222 {
223  return true;
224 }
225 
226 
227 
228 ///////////////////////////////////////////////////////////////////////////////
229 // Implementation of inline functions //
230 
231 template <typename Domain, DGtal::Dimension maxNorm1>
232 inline
233 std::ostream&
234 DGtal::deprecated::operator<< ( std::ostream & out,
235  const DomainMetricAdjacency<Domain,maxNorm1,
236  Domain::Space::dimension> & object )
237 {
238  object.selfDisplay( out );
239  return out;
240 }
241 
242 // //
243 ///////////////////////////////////////////////////////////////////////////////
244 
245