core/vnl/vnl_vector_fixed_ref.txx
Go to the documentation of this file.
00001 // This is core/vnl/vnl_vector_fixed_ref.txx
00002 #ifndef vnl_vector_fixed_ref_txx_
00003 #define vnl_vector_fixed_ref_txx_
00004 // Author: Paul P. Smyth, Vicon Motion Systems Ltd.
00005 // Created: 02 May 2001
00006 //
00007 #include "vnl_vector_fixed_ref.h"
00008 #include <vcl_cassert.h>
00009 #include <vcl_algorithm.h> // for vcl_swap
00010 #include <vcl_iostream.h>  // for vcl_cerr
00011 #include <vcl_cstdlib.h>   // for vcl_abort
00012 #include <vnl/vnl_math.h>  // for vnl_math_isfinite
00013 
00014 
00015 //------------------------------------------------------------
00016 
00017 template<class T, unsigned int n>
00018 vnl_vector_fixed<T,n>
00019 vnl_vector_fixed_ref_const<T,n>::apply( T (*f)(T) ) const
00020 {
00021   vnl_vector_fixed<T,n> ret;
00022   for ( size_type i = 0; i < n; ++i )
00023     ret[i] = f( data_block()[i] );
00024   return ret;
00025 }
00026 
00027 template<class T, unsigned int n>
00028 vnl_vector_fixed<T,n>
00029 vnl_vector_fixed_ref_const<T,n>::apply( T (*f)(const T&) ) const
00030 {
00031   vnl_vector_fixed<T,n> ret;
00032   for ( size_type i = 0; i < n; ++i )
00033     ret[i] = f( data_block()[i] );
00034   return ret;
00035 }
00036 
00037 
00038 template<class T, unsigned int n>
00039 vnl_vector<T>
00040 vnl_vector_fixed_ref_const<T,n>::extract( unsigned int len, unsigned int start ) const
00041 {
00042   assert( start < n && start + len <= n );
00043   return vnl_vector<T>( data_block() + start, len );
00044 }
00045 
00046 template<class T, unsigned int n>
00047 vnl_vector_fixed_ref<T,n> const&
00048 vnl_vector_fixed_ref<T,n>::update( const vnl_vector<T>& v, unsigned int start ) const
00049 {
00050   size_type stop = start + v.size();
00051   assert( stop <= n );
00052   for (size_type i = start; i < stop; i++)
00053     this->data_block()[i] = v[i-start];
00054   return *this;
00055 }
00056 
00057 template <class T, unsigned int n>
00058 vnl_vector_fixed_ref<T,n> const&
00059 vnl_vector_fixed_ref<T,n>::flip() const
00060 {
00061   for ( unsigned int i=0; 2*i+1 < n; ++i )
00062     vcl_swap( data_block()[i], data_block()[n-1-i] );
00063   return *this;
00064 }
00065 
00066 template <class T, unsigned int n>
00067 bool
00068 vnl_vector_fixed_ref_const<T,n>::is_finite() const
00069 {
00070   for ( size_type i = 0; i < this->size(); ++i )
00071     if ( !vnl_math_isfinite( (*this)[i] ) )
00072       return false;
00073 
00074   return true;
00075 }
00076 
00077 
00078 template <class T, unsigned int n>
00079 bool
00080 vnl_vector_fixed_ref_const<T,n>::is_zero() const
00081 {
00082   T const zero(0);
00083   for ( size_type i = 0; i < this->size(); ++i )
00084     if ( !( (*this)[i] == zero) )
00085       return false;
00086 
00087   return true;
00088 }
00089 
00090 
00091 template <class T, unsigned int n>
00092 bool
00093 vnl_vector_fixed_ref<T,n>::read_ascii(vcl_istream& s) const
00094 {
00095   for (unsigned i = 0; i < this->size(); ++i)
00096     s >> (*this)(i);
00097 
00098   return s.good() || s.eof();
00099 }
00100 
00101 template <class T, unsigned int n>
00102 void
00103 vnl_vector_fixed_ref_const<T,n>::assert_finite_internal() const
00104 {
00105   if (this->is_finite())
00106     return;
00107 
00108   vcl_cerr << __FILE__ ": *** NAN FEVER **\n" << *this;
00109   vcl_abort();
00110 }
00111 
00112 template <class T, unsigned int n>
00113 void
00114 vnl_vector_fixed_ref_const<T,n>::print( vcl_ostream& s ) const
00115 {
00116   if ( this->size() > 0 )
00117     s << (*this)[0];
00118   for ( size_type i = 1; i < this->size(); ++i )
00119     s << ' ' << (*this)[i];
00120 }
00121 
00122 // instantiation macros for vnl_vector_fixed_ref<T,unsigned> :
00123 
00124 #define VNL_VECTOR_FIXED_REF_INSTANTIATE(T,n) \
00125 template class vnl_vector_fixed_ref<T, n >; \
00126 template class vnl_vector_fixed_ref_const<T, n >
00127 
00128 #endif // vnl_vector_fixed_ref_txx_