[Insight-users] use of std::vector with ITK typedefs

Arvind Rao arvind.sbia at gmail.com
Thu Apr 5 09:53:48 EDT 2012


Hi Matt,

My apologizes. For brevity and debugging I removed the portion of code
which fills points. The full code for *ReadStreamFle* is below.

template< typename TPixelType>
void ReadStreamFile( std::string filename, std::vector<TPixelType> &points )
{
   TPixelType dir;
   int num;
   std::vector<float> vec;

   std::ifstream indata( filename.c_str() ); // opens the file
   if(!indata) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }

   indata >> num;
   while ( !indata.eof() )
   { // keep reading until end-of-file
      vec.push_back( num );
      indata >> num;
   }
   indata.close();
   std::cout << vec.size() << "\n";

   for(int j=0; j < int(vec.size()); j=+3)
   {
      dir[0] = vec[j];
      dir[1] = vec[j+1];
      dir[2] = vec[j+2];
      points.push_back( dir );
   }
}

Best,
Arvind

On Thu, Apr 5, 2012 at 12:59 AM, Matt McCormick
<matt.mccormick at kitware.com>wrote:

> Hi Arvind,
>
> That cannot be all of the function because "points" is not used inside.
>
> Matt
>
> On Thu, Apr 5, 2012 at 12:34 AM, Arvind Rao <arvind.sbia at gmail.com> wrote:
> > Hi Matt,
> >
> > Thanks for replying. ReadStreamFile(...) is defined below.
> The "bad_alloc>
> > caught: St9bad_alloc" error seems to originate from this function. For
> > clarity, TPixelType = itk::Vector<float, 3>
> >
> > template< typename TPixelType>
> > void ReadStreamFile( std::string filename, std::vector<TPixelType>
> &points )
> > {
> >    TPixelType dir;
> >    int num;
> >    std::vector<float> vec;
> >    std::ifstream indata( filename.c_str() ); // opens the file
> >
> >    indata >> num;
> >    while ( !indata.eof() )  { // keep reading until end-of-file
> >       vec.push_back( num );
> >       indata >> num;}
> >    indata.close();
> > }
> >
> > Best,
> > Arvind
> >
> > On Wed, Apr 4, 2012 at 10:53 PM, Matt McCormick <
> matt.mccormick at kitware.com>
> > wrote:
> >>
> >> Hi Arvind,
> >>
> >> It looks like the error is happening inside ReadStreamFile, so please
> >> post how that is declared and defined.
> >>
> >> Thanks,
> >> Matt
> >>
> >> On Wed, Apr 4, 2012 at 8:57 PM, Arvind Rao <arvind.sbia at gmail.com>
> wrote:
> >> > Hi all,
> >> >
> >> > In order to read-in a variable number of points from a file I want to
> >> > use a
> >> > dynamically sized container like STD::VECTOR. The points are stored
> like
> >> > so:
> >> > ( points is the vector I use )
> >> >
> >> >       typedef itk::Image< itk::Vector<ComponentType, Dimension>,
> >> > Dimension>
> >> >                                 PeakImageType;
> >> >       typedef PeakImageType::PixelType
> >> >                                                        DirectionType;
> >> >       std::vector<DirectionType>
> >> >                                                                points
> >> >
> >> > However, the following try-catch statement gives, at
> >> > run-time, "bad_alloc
> >> > caught: St9bad_alloc"
> >> >
> >> >       try
> >> >       {
> >> >                 std::vector<DirectionType> points;
> >> >                 ReadStreamFile<DirectionType>( streamArg.getValue(),
> >> > points
> >> > );
> >> >       }
> >> >      catch (std::bad_alloc& ba)
> >> >       {
> >> >                 std::cerr << "bad_alloc caught: " << ba.what() <<
> >> > std::endl;
> >> >                 return 0;
> >> >       }
> >> >
> >> > So, what is going on? Its seems like POINTS is not being allocated
> >> > properly,
> >> > but I'm sure how to fix this.
> >> >
> >> > Thanks for any help you can provide.
> >> >
> >> >
> >> > --
> >> > Arvind S. Rao, PhD
> >> > Section of Biomedical Image Analysis
> >> > Department of Radiology
> >> > University of Pennsylvania
> >> > 3600 Market Street, Suite 380
> >> > Philadelphia, PA 19104
> >> >
> >> >
> >> >
> >> > _____________________________________
> >> > Powered by www.kitware.com
> >> >
> >> > Visit other Kitware open-source projects at
> >> > http://www.kitware.com/opensource/opensource.html
> >> >
> >> > Kitware offers ITK Training Courses, for more information visit:
> >> > http://www.kitware.com/products/protraining.php
> >> >
> >> > Please keep messages on-topic and check the ITK FAQ at:
> >> > http://www.itk.org/Wiki/ITK_FAQ
> >> >
> >> > Follow this link to subscribe/unsubscribe:
> >> > http://www.itk.org/mailman/listinfo/insight-users
> >> >
> >
> >
> >
> >
> > --
> > Arvind S. Rao, PhD
> > Section of Biomedical Image Analysis
> > Department of Radiology
> > University of Pennsylvania
> > 3600 Market Street, Suite 380
> > Philadelphia, PA 19104
> > tel: (267) 283-8959
> >
>



-- 
*Arvind S. Rao, PhD*
Section of Biomedical Image Analysis
Department of Radiology
University of Pennsylvania
3600 Market Street, Suite 380
Philadelphia, PA 19104
tel: (267) 283-8959
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120405/50863eb7/attachment.htm>


More information about the Insight-users mailing list