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/>.
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 Histogram.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
41 //-----------------------------------------------------------------------------
42 template <typename TQuantity, typename TBinner>
44 DGtal::Histogram<TQuantity, TBinner>::~Histogram()
46 if ( myBinner != 0 ) delete myBinner;
48 //-----------------------------------------------------------------------------
49 template <typename TQuantity, typename TBinner>
51 DGtal::Histogram<TQuantity, TBinner>::Histogram()
55 //-----------------------------------------------------------------------------
56 template <typename TQuantity, typename TBinner>
59 DGtal::Histogram<TQuantity, TBinner>::clear()
67 myCumulativeHistogram.clear();
70 //-----------------------------------------------------------------------------
71 template <typename TQuantity, typename TBinner>
74 DGtal::Histogram<TQuantity, TBinner>::init( Clone<Binner> binner )
77 myBinner = new Binner( binner );
78 prepare( myBinner->size() );
81 //-----------------------------------------------------------------------------
82 template <typename TQuantity, typename TBinner>
84 DGtal::Histogram<TQuantity, TBinner>::init( Formula formula, const Statistic<Quantity> & stat )
88 case SquareRoot: /**< Rule is k=sqrt(n) */
89 myBinner = new Binner( stat.min(), stat.max(),
90 static_cast<Bin>( ceil( sqrt( stat.samples() ) ) ) );
92 case Sturges: /**< Rule is k=ceil(log_2(n)+1) */
93 myBinner = new Binner( stat.min(), stat.max(),
94 static_cast<Bin>( ceil( log2( stat.samples() ) + 1 ) ) );
96 case Rice: /**< Rule is k=ceil(n^(1/3)) */
97 myBinner = new Binner( stat.min(), stat.max(),
98 static_cast<Bin>( ceil( pow( stat.samples(), 1.0/3.0 ) ) ) );
100 case Scott: /**< Rule is h=3.5s/(n^(1/3)) */
101 double h = 3.5 * stat.unbiasedVariance() / ceil( pow( stat.samples(), 1.0/3.0 ) );
102 myBinner = new Binner( stat.min(), stat.max(),
103 static_cast<Bin>( ceil( ( stat.max() - stat.min() ) / h ) ) );
106 if ( myBinner != 0 ) prepare( myBinner->size() );
108 //-----------------------------------------------------------------------------
109 template <typename TQuantity, typename TBinner>
111 DGtal::Histogram<TQuantity, TBinner>::init( Bin nbBins, const Statistic<Quantity> & stat )
114 myBinner = new Binner( stat.min(), stat.max(), nbBins );
115 prepare( myBinner->size() );
117 //-----------------------------------------------------------------------------
118 template <typename TQuantity, typename TBinner>
120 typename DGtal::Histogram<TQuantity, TBinner>::Bin
121 DGtal::Histogram<TQuantity, TBinner>::bin( Quantity q ) const
124 return (*myBinner)( q );
126 //-----------------------------------------------------------------------------
127 template <typename TQuantity, typename TBinner>
130 DGtal::Histogram<TQuantity, TBinner>::addValue( Quantity q )
132 ++myHistogram[ bin( q ) ];
134 //-----------------------------------------------------------------------------
135 template <typename TQuantity, typename TBinner>
136 template <typename TInputIterator>
139 DGtal::Histogram<TQuantity, TBinner>::addValues( TInputIterator it, TInputIterator itE )
141 BOOST_CONCEPT_ASSERT(( boost::InputIterator< ConstIterator > ));
142 for ( ; it != itE; ++it )
145 //-----------------------------------------------------------------------------
146 template <typename TQuantity, typename TBinner>
148 typename DGtal::Histogram<TQuantity, TBinner>::Bin
149 DGtal::Histogram<TQuantity, TBinner>::size() const
151 return static_cast<Bin>( myHistogram.size() );
153 //-----------------------------------------------------------------------------
154 template <typename TQuantity, typename TBinner>
156 typename DGtal::Histogram<TQuantity, TBinner>::Size
157 DGtal::Histogram<TQuantity, TBinner>::area() const
159 return myCumulativeHistogram.back();
161 //-----------------------------------------------------------------------------
162 template <typename TQuantity, typename TBinner>
164 typename DGtal::Histogram<TQuantity, TBinner>::Size
165 DGtal::Histogram<TQuantity, TBinner>::nb( Bin b ) const
167 ASSERT( b < size() );
168 return myHistogram[ b ];
170 //-----------------------------------------------------------------------------
171 template <typename TQuantity, typename TBinner>
173 typename DGtal::Histogram<TQuantity, TBinner>::Size
174 DGtal::Histogram<TQuantity, TBinner>::accumulation( Bin b ) const
176 ASSERT( b < size() );
177 return myCumulativeHistogram[ b ];
179 //-----------------------------------------------------------------------------
180 template <typename TQuantity, typename TBinner>
183 DGtal::Histogram<TQuantity, TBinner>::pdf( Bin b ) const
185 return NumberTraits<Bin>::castToDouble( nb( b ) )
186 / NumberTraits<Size>::castToDouble( area() );
188 //-----------------------------------------------------------------------------
189 template <typename TQuantity, typename TBinner>
192 DGtal::Histogram<TQuantity, TBinner>::cdf( Bin b ) const
194 return NumberTraits<Bin>::castToDouble( accumulation( b ) )
195 / NumberTraits<Size>::castToDouble( area() );
197 //-----------------------------------------------------------------------------
198 template <typename TQuantity, typename TBinner>
201 DGtal::Histogram<TQuantity, TBinner>::terminate()
203 ConstIterator srcIt = myHistogram.begin();
204 ConstIterator srcItE = myHistogram.end();
206 myCumulativeHistogram[ b ] = *srcIt++;
207 for ( ; srcIt != srcItE; ++srcIt, ++b )
209 myCumulativeHistogram[ b+1 ] = myCumulativeHistogram[ b ] + *srcIt;
214 ///////////////////////////////////////////////////////////////////////////////
215 // Interface - public :
218 * Writes/Displays the object on an output stream.
219 * @param out the output stream where the object is written.
221 template <typename TQuantity, typename TBinner>
224 DGtal::Histogram<TQuantity, TBinner>::selfDisplay ( std::ostream & out ) const
226 out << "[Histogram size=" << size() << " area=" << area() << "]";
230 * Checks the validity/consistency of the object.
231 * @return 'true' if the object is valid, 'false' otherwise.
233 template <typename TQuantity, typename TBinner>
236 DGtal::Histogram<TQuantity, TBinner>::isValid() const
238 return myBinner != 0;
241 //-----------------------------------------------------------------------------
242 template <typename TQuantity, typename TBinner>
245 DGtal::Histogram<TQuantity, TBinner>::prepare( Bin aSize )
247 boost::ignore_unused_variable_warning( aSize );
248 myHistogram.resize( myBinner->size(), 0 );
249 myCumulativeHistogram.resize( myBinner->size(), 0 );
253 ///////////////////////////////////////////////////////////////////////////////
254 // Implementation of inline functions //
256 template <typename TQuantity, typename TBinner>
259 DGtal::operator<< ( std::ostream & out,
260 const Histogram<TQuantity, TBinner> & object )
262 object.selfDisplay( out );
267 ///////////////////////////////////////////////////////////////////////////////