DGtal  1.5.beta
HyperRectDomain.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 HyperRectDomain.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * @author Guillaume Damiand
21  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
22  *
23  * @date 2010/05/25
24  *
25  * Implementation of inline methods defined in HyperRectDomain.h
26  *
27  * This file is part of the DGtal library.
28  */
29 
30 ///////////////////////////////////////////////////////////////////////////////
31 // IMPLEMENTATION of inline methods.
32 ///////////////////////////////////////////////////////////////////////////////
33 
34 //////////////////////////////////////////////////////////////////////////////
35 #include <cstdlib>
36 #include "DGtal/io/Color.h"
37 
38 //-----------------------------------------------------------------------------
39 template<typename TSpace>
40 inline
41 DGtal::HyperRectDomain<TSpace>::HyperRectDomain()
42  : myLowerBound(Point::zero),
43  myUpperBound(Point::zero - Point::diagonal(1)), // So that it gives an empty domain.
44  myPredicate( myLowerBound, myUpperBound ),
45  myIteratorBegin(myLowerBound,
46  myLowerBound,
47  myUpperBound),
48  myIteratorEnd(myUpperBound,
49  myLowerBound,
50  myUpperBound)
51 {
52  ++myIteratorEnd;
53 }
54 
55 //-----------------------------------------------------------------------------
56 template<typename TSpace>
57 inline
58 DGtal::HyperRectDomain<TSpace>::HyperRectDomain ( const Point &lowerPoint,
59  const Point &upperPoint ) :
60  myLowerBound(lowerPoint),
61  myUpperBound(upperPoint),
62  myPredicate( myLowerBound, myUpperBound ),
63  myIteratorBegin(myLowerBound,
64  myLowerBound,
65  myUpperBound),
66  myIteratorEnd(myUpperBound,
67  myLowerBound,
68  myUpperBound)
69 {
70  ASSERT_MSG( // For an empty domain, lower = upper + diag(1) so that begin() == end().
71  myLowerBound.isLower(myUpperBound) || myLowerBound == myUpperBound + Point::diagonal(1),
72  "The lower bound must be lower than the upper bound or, for an empty domain, be equal to the upper bound + diagonal(1)."
73  );
74  ASSERT_MSG( // Check that begin() == end() for empty domains.
75  ConstIterator(myUpperBound+Point::diagonal(1), myUpperBound+Point::diagonal(1), myUpperBound) == ++ConstIterator(myUpperBound, myUpperBound+Point::diagonal(1), myUpperBound),
76  "ConstIterator is not compatible with the definition of an empty domain."
77  );
78 
79  ++myIteratorEnd;
80 }
81 
82 //-----------------------------------------------------------------------------
83 template<typename TSpace>
84 inline
85 DGtal::HyperRectDomain<TSpace>::HyperRectDomain (
86  const typename Space::RealPoint &lowerPoint,
87  const typename Space::RealPoint &upperPoint ) :
88  HyperRectDomain( Point(lowerPoint, functors::Floor<>()),
89  Point(upperPoint, functors::Ceil<>()) )
90 {
91 }
92 
93 //-----------------------------------------------------------------------------
94 template<typename TSpace>
95 inline
96 DGtal::HyperRectDomain<TSpace>
97 ::HyperRectDomain( const typename DGtal::HyperRectDomain<TSpace> &aDomain) :
98  myLowerBound(aDomain.lowerBound()),
99  myUpperBound(aDomain.upperBound()),
100  myPredicate( myLowerBound, myUpperBound ),
101  myIteratorBegin(myLowerBound,
102  myLowerBound,
103  myUpperBound),
104  myIteratorEnd(myUpperBound,
105  myLowerBound,
106  myUpperBound)
107 {
108  ++myIteratorEnd;
109 }
110 
111 //-----------------------------------------------------------------------------
112 template<typename TSpace>
113 inline
114 DGtal::HyperRectDomain<TSpace>::~HyperRectDomain()
115 {}
116 
117 //-----------------------------------------------------------------------------
118 template<typename TSpace>
119 inline
120 DGtal::HyperRectDomain<TSpace> &
121 DGtal::HyperRectDomain<TSpace>
122 ::operator= ( const typename DGtal::HyperRectDomain<TSpace> & other )
123 {
124  if ( this != &other )
125  {
126  myLowerBound = other.myLowerBound;
127  myUpperBound = other.myUpperBound;
128  myPredicate = other.myPredicate;
129  new(&myIteratorBegin) ConstIterator(myLowerBound,myLowerBound,myUpperBound);
130  new(&myIteratorEnd) ConstIterator(myUpperBound,myLowerBound,myUpperBound);
131  ++myIteratorEnd;
132  }
133  return *this;
134 }
135 
136 //-----------------------------------------------------------------------------
137 
138 
139 //-----------------------------------------------------------------------------
140 template<typename TSpace>
141 inline
142 const typename DGtal::HyperRectDomain<TSpace>::Point &
143 DGtal::HyperRectDomain<TSpace>::lowerBound() const
144 {
145  return myLowerBound;
146 }
147 
148 //-----------------------------------------------------------------------------
149 template<typename TSpace>
150 inline
151 bool
152 DGtal::HyperRectDomain<TSpace>::isInside( const Point & p ) const
153 {
154  return myPredicate( p );
155 }
156 
157 //-----------------------------------------------------------------------------
158 template<typename TSpace>
159 inline
160 bool
161 DGtal::HyperRectDomain<TSpace>::isEmpty() const
162 {
163  return ! myLowerBound.isLower(myUpperBound);
164 }
165 
166 //-----------------------------------------------------------------------------
167 template<typename TSpace>
168 inline
169 const typename DGtal::HyperRectDomain<TSpace>::Predicate &
170 DGtal::HyperRectDomain<TSpace>::predicate() const
171 {
172  return myPredicate;
173 }
174 
175 //-----------------------------------------------------------------------------
176 template<typename TSpace>
177 inline
178 const typename DGtal::HyperRectDomain<TSpace>::Point &
179 DGtal::HyperRectDomain<TSpace>::upperBound() const
180 {
181  return myUpperBound;
182 }
183 
184 //-----------------------------------------------------------------------------
185 template<typename TSpace>
186 inline
187 void
188 DGtal::HyperRectDomain<TSpace>::selfDisplay ( std::ostream & out ) const
189 {
190  out << "[HyperRectDomain] = [" << myLowerBound << "]x["
191  << myUpperBound << "]";
192 }
193 
194 //-----------------------------------------------------------------------------
195 template<typename TSpace>
196 inline
197 bool
198 DGtal::HyperRectDomain<TSpace>::isValid() const
199 {
200  return true;
201 }
202 
203 //-----------------------------------------------------------------------------
204 template<typename TSpace>
205 inline
206 std::string
207 DGtal::HyperRectDomain<TSpace>::className() const
208 {
209  return "HyperRectDomain";
210 }
211 
212 //-----------------------------------------------------------------------------
213 template<typename TSpace>
214 inline
215 std::ostream&
216 DGtal::operator<< ( std::ostream & out,
217  const HyperRectDomain<TSpace> & object )
218 {
219  object.selfDisplay ( out );
220  return out;
221 }
222 
223 ///////////////////////////////////////////////////////////////////////////////
224 
225