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.
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.
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/>.
18 * @file BoundedLatticePolytopeCounter.ih
19 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20 * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
24 * Implementation of inline methods defined in BoundedLatticePolytopeCounter.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
32//////////////////////////////////////////////////////////////////////////////
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
41//-----------------------------------------------------------------------------
42template <typename TSpace>
43DGtal::BoundedLatticePolytopeCounter<TSpace>::
44BoundedLatticePolytopeCounter
50//-----------------------------------------------------------------------------
51template <typename TSpace>
53DGtal::BoundedLatticePolytopeCounter<TSpace>::
55( const Polytope* ptrP )
58 if ( ptrP == nullptr ) return;
59 myLower = ptrP->getDomain().lowerBound();
60 myUpper = ptrP->getDomain().upperBound();
64//-----------------------------------------------------------------------------
65template <typename TSpace>
66typename DGtal::BoundedLatticePolytopeCounter<TSpace>::Interval
67DGtal::BoundedLatticePolytopeCounter<TSpace>::
68intersectionIntervalAlongAxis( Point p, Dimension a ) const
70 ASSERT( myPolytope != nullptr );
71 const Polytope& P = *myPolytope;
72 const InequalityMatrix& A = P.getA();
73 const InequalityVector& B = P.getB();
74 const std::vector<bool>& I = P.getI();
75 Integer x_min = myLower[ a ];
76 Integer x_max = myUpper[ a ]+1;
78 const Integer x_a = x_min;
81 for ( Dimension k = 2*dimension; k < A.size(); k++ )
83 const Integer c = A[ k ].dot( p );
84 const Integer n = A[ k ][ a ];
85 const Integer b = B[ k ];
87 { // constraint is // to the specified axis.
88 empty = ! ( I[ k ] ? ( c <= b ) : c < b );
93 if ( d < 0 ) empty = true;
96 x = I[ k ] ? ( d / n + 1 ) : ( (d+n-1) / n ) ;
97 x_max = std::min( x_max, x_a + x );
105 x = I[ k ] ? ( (d-n-1) / -n ) : ( d / -n + 1 );
106 x_min = std::max( x_min, x_a + x );
108 // otherwise the constraint is true
110 if ( empty || ( x_max <= x_min ) ) return Interval( 0, 0 );
112 return Interval( x_min, x_max );
115//-----------------------------------------------------------------------------
116template <typename TSpace>
117typename DGtal::BoundedLatticePolytopeCounter<TSpace>::Interval
118DGtal::BoundedLatticePolytopeCounter<TSpace>::
119interiorIntersectionIntervalAlongAxis( Point p, Dimension a ) const
121 ASSERT( myPolytope != nullptr );
122 const Polytope& P = *myPolytope;
123 const InequalityMatrix& A = P.getA();
124 const InequalityVector& B = P.getB();
125 Integer x_min = myLower[ a ];
126 Integer x_max = myUpper[ a ]+1;
128 const Integer x_a = x_min;
131 // We must take into account also bounding box constraints for interior points.
132 for ( Dimension k = 0; k < A.size(); k++ )
134 const Integer c = A[ k ].dot( p );
135 const Integer n = A[ k ][ a ];
136 const Integer b = B[ k ];
138 { // constraint is // to the specified axis.
144 if ( d < 0 ) empty = true;
148 x_max = std::min( x_max, x_a + x );
157 x_min = std::max( x_min, x_a + x );
159 // otherwise the constraint is true
161 // std::cout << " (" << empty << ":" << x_min << "," << x_max << ")";
162 if ( empty || ( x_max <= x_min ) ) return Interval( 0, 0 );
164 return Interval( x_min, x_max );
167//-----------------------------------------------------------------------------
168template <typename TSpace>
169typename DGtal::BoundedLatticePolytopeCounter<TSpace>::Integer
170DGtal::BoundedLatticePolytopeCounter<TSpace>::
171countAlongAxis( Dimension a ) const
173 ASSERT( myPolytope != nullptr );
181 auto I = intersectionIntervalAlongAxis( p, a );
182 nb += I.second - I.first;
187//-----------------------------------------------------------------------------
188template <typename TSpace>
189typename DGtal::BoundedLatticePolytopeCounter<TSpace>::Integer
190DGtal::BoundedLatticePolytopeCounter<TSpace>::
191countInteriorAlongAxis( Dimension a ) const
193 ASSERT( myPolytope != nullptr );
201 auto I = interiorIntersectionIntervalAlongAxis( p, a );
202 nb += I.second - I.first;
207//-----------------------------------------------------------------------------
208template <typename TSpace>
210DGtal::BoundedLatticePolytopeCounter<TSpace>::
211getPointsAlongAxis( PointRange& pts, Dimension a ) const
213 ASSERT( myPolytope != nullptr );
220 auto I = intersectionIntervalAlongAxis( p, a );
222 for ( Integer x = I.first; x != I.second; x++ )
230//-----------------------------------------------------------------------------
231template <typename TSpace>
233DGtal::BoundedLatticePolytopeCounter<TSpace>::
234getInteriorPointsAlongAxis( PointRange& pts, Dimension a ) const
236 ASSERT( myPolytope != nullptr );
241 //Integer nb = 0; not used
244 auto I = interiorIntersectionIntervalAlongAxis( p, a );
246 for ( Integer x = I.first; x != I.second; x++ )
255//-----------------------------------------------------------------------------
256template <typename TSpace>
257typename DGtal::BoundedLatticePolytopeCounter<TSpace>::LatticeSetByInterval
258DGtal::BoundedLatticePolytopeCounter<TSpace>::
259getLatticeSet( Dimension a ) const
261 ASSERT( myPolytope != nullptr );
267 LatticeSetByInterval L;
270 auto I = intersectionIntervalAlongAxis( p, a );
275//-----------------------------------------------------------------------------
276template <typename TSpace>
277typename DGtal::BoundedLatticePolytopeCounter<TSpace>::LatticeSetByInterval
278DGtal::BoundedLatticePolytopeCounter<TSpace>::
279getLatticeCells( Dimension a ) const
281 ASSERT( myPolytope != nullptr );
287 LatticeSetByInterval L; //< stores the intersected cells
288 const Point One = Point::diagonal( 1 );
292 q = 2*p - One; q[ a ] = 0;
293 const auto I = intersectionIntervalAlongAxis( p, a );
294 const auto n = I.second - I.first;
297 // Now the second bound is included
298 L[ q ] = Interval( 2 * I.first - 1, 2 * I.second - 3 );
301 // It remains to compute all the k-cells, 0 <= k < d, intersected by Cvxh( Z )
302 for ( Dimension k = 0; k < dimension; k++ )
304 if ( k == a ) continue;
305 std::vector< Point > q_computed;
306 std::vector< Interval > I_computed;
307 for ( const auto& value : L )
309 Point p = value.first;
310 Interval I = value.second;
311 Point r = p; r[ k ] += 2;
312 const auto it = L.find( r );
313 if ( it == L.end() ) continue; // neighbor is empty
314 // Otherwise compute common part.
315 Interval J = it->second;
316 auto f = std::max( I.first, J.first );
317 auto s = std::min( I.second, J.second );
320 Point qq = p; qq[ k ] += 1;
321 q_computed.push_back( qq );
322 I_computed.push_back( Interval( f, s ) );
325 // Add new columns to map Point -> column
326 for ( typename Point::Index i = 0; i < q_computed.size(); ++i )
328 L[ q_computed[ i ] ] = I_computed[ i ];
334//-----------------------------------------------------------------------------
335template <typename TSpace>
337DGtal::BoundedLatticePolytopeCounter<TSpace>::
340 ASSERT( myPolytope != nullptr );
342 auto b_size = myUpper[ 0 ] - myLower[ 0 ];
343 for ( Dimension a = 1; a < dimension; a++ )
345 const auto a_size = myUpper[ a ] - myLower[ a ];
346 if ( b_size < a_size ) { b = a; b_size = a_size; }
353///////////////////////////////////////////////////////////////////////////////