Hi Matt,<div><br></div><div>After some investigation I've learned that my error was purely a result of how I handle the file stream, within the ReadStreamFile(...)<br>function. And now I'm working on properly parsing the input file. </div>
<div><br></div><div>Thanks so much for taking my question.</div><div><br></div><div>Best wishes, </div><div>Arvind</div><div><br></div><div><br><div class="gmail_quote">On Thu, Apr 5, 2012 at 9:53 AM, Arvind Rao <span dir="ltr"><<a href="mailto:arvind.sbia@gmail.com">arvind.sbia@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Hi Matt,</div><div><br></div>My apologizes. For brevity and debugging I removed the portion of code which fills points. The full code for <b>ReadStreamFle</b> is below.<div>
<br></div><div><div class="im"><div>template< typename TPixelType></div>
<div>void ReadStreamFile( std::string filename, std::vector<TPixelType> &points )</div><div>{</div><div> TPixelType dir;</div><div> int num;</div><div> std::vector<float> vec;</div><div><br></div><div>
std::ifstream indata( filename.c_str() ); // opens the file</div></div><div> if(!indata) { // file couldn't be opened</div><div> cerr << "Error: file could not be opened" << endl;</div>
<div> exit(1);</div><div class="im">
<div> }</div><div><br></div><div> indata >> num;</div><div> while ( !indata.eof() )</div><div> { // keep reading until end-of-file</div><div> vec.push_back( num );</div><div> indata >> num;</div>
<div> }</div><div> indata.close();</div></div><div> std::cout << vec.size() << "\n";</div><div><br></div><div> for(int j=0; j < int(vec.size()); j=+3) </div><div> {</div><div> dir[0] = vec[j];</div>
<div> dir[1] = vec[j+1];</div><div> dir[2] = vec[j+2];</div><div> points.push_back( dir );</div><div> }</div><div>}</div><div><br></div><div>Best, </div><span class="HOEnZb"><font color="#888888"><div>Arvind</div>
</font></span><div><div class="h5"><br><div class="gmail_quote">On Thu, Apr 5, 2012 at 12:59 AM, Matt McCormick <span dir="ltr"><<a href="mailto:matt.mccormick@kitware.com" target="_blank">matt.mccormick@kitware.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Arvind,<br>
<br>
That cannot be all of the function because "points" is not used inside.<br>
<span><font color="#888888"><br>
Matt<br>
</font></span><div><div><br>
On Thu, Apr 5, 2012 at 12:34 AM, Arvind Rao <<a href="mailto:arvind.sbia@gmail.com" target="_blank">arvind.sbia@gmail.com</a>> wrote:<br>
> Hi Matt,<br>
><br>
> Thanks for replying. ReadStreamFile(...) is defined below. The "bad_alloc><br>
> caught: St9bad_alloc" error seems to originate from this function. For<br>
> clarity, TPixelType = itk::Vector<float, 3><br>
><br>
> template< typename TPixelType><br>
> void ReadStreamFile( std::string filename, std::vector<TPixelType> &points )<br>
> {<br>
> TPixelType dir;<br>
> int num;<br>
> std::vector<float> vec;<br>
> std::ifstream indata( filename.c_str() ); // opens the file<br>
><br>
> indata >> num;<br>
> while ( !indata.eof() ) { // keep reading until end-of-file<br>
> vec.push_back( num );<br>
> indata >> num;}<br>
> indata.close();<br>
> }<br>
><br>
> Best,<br>
> Arvind<br>
><br>
> On Wed, Apr 4, 2012 at 10:53 PM, Matt McCormick <<a href="mailto:matt.mccormick@kitware.com" target="_blank">matt.mccormick@kitware.com</a>><br>
> wrote:<br>
>><br>
>> Hi Arvind,<br>
>><br>
>> It looks like the error is happening inside ReadStreamFile, so please<br>
>> post how that is declared and defined.<br>
>><br>
>> Thanks,<br>
>> Matt<br>
>><br>
>> On Wed, Apr 4, 2012 at 8:57 PM, Arvind Rao <<a href="mailto:arvind.sbia@gmail.com" target="_blank">arvind.sbia@gmail.com</a>> wrote:<br>
>> > Hi all,<br>
>> ><br>
>> > In order to read-in a variable number of points from a file I want to<br>
>> > use a<br>
>> > dynamically sized container like STD::VECTOR. The points are stored like<br>
>> > so:<br>
>> > ( points is the vector I use )<br>
>> ><br>
>> > typedef itk::Image< itk::Vector<ComponentType, Dimension>,<br>
>> > Dimension><br>
>> > PeakImageType;<br>
>> > typedef PeakImageType::PixelType<br>
>> > DirectionType;<br>
>> > std::vector<DirectionType><br>
>> > points<br>
>> ><br>
>> > However, the following try-catch statement gives, at<br>
>> > run-time, "bad_alloc<br>
>> > caught: St9bad_alloc"<br>
>> ><br>
>> > try<br>
>> > {<br>
>> > std::vector<DirectionType> points;<br>
>> > ReadStreamFile<DirectionType>( streamArg.getValue(),<br>
>> > points<br>
>> > );<br>
>> > }<br>
>> > catch (std::bad_alloc& ba)<br>
>> > {<br>
>> > std::cerr << "bad_alloc caught: " << ba.what() <<<br>
>> > std::endl;<br>
>> > return 0;<br>
>> > }<br>
>> ><br>
>> > So, what is going on? Its seems like POINTS is not being allocated<br>
>> > properly,<br>
>> > but I'm sure how to fix this.<br>
>> ><br>
>> > Thanks for any help you can provide.<br>
>> ><br>
>> ><br>
>> > --<br>
>> > Arvind S. Rao, PhD<br>
>> > Section of Biomedical Image Analysis<br>
>> > Department of Radiology<br>
>> > University of Pennsylvania<br>
>> > 3600 Market Street, Suite 380<br>
>> > Philadelphia, PA 19104<br>
>> ><br>
>> ><br>
>> ><br>
>> > _____________________________________<br>
>> > Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>> ><br>
>> > Visit other Kitware open-source projects at<br>
>> > <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
>> ><br>
>> > Kitware offers ITK Training Courses, for more information visit:<br>
>> > <a href="http://www.kitware.com/products/protraining.php" target="_blank">http://www.kitware.com/products/protraining.php</a><br>
>> ><br>
>> > Please keep messages on-topic and check the ITK FAQ at:<br>
>> > <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
>> ><br>
>> > Follow this link to subscribe/unsubscribe:<br>
>> > <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
>> ><br>
><br>
><br>
><br>
><br>
> --<br>
> Arvind S. Rao, PhD<br>
> Section of Biomedical Image Analysis<br>
> Department of Radiology<br>
> University of Pennsylvania<br>
> 3600 Market Street, Suite 380<br>
> Philadelphia, PA 19104<br>
> tel: (267) 283-8959<br>
><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><b>Arvind S. Rao, PhD</b><br>Section of Biomedical Image Analysis<br>Department of Radiology<br>University of Pennsylvania<br>3600 Market Street, Suite 380<br>
Philadelphia, PA 19104<div>tel: (267) 283-8959</div><br>
</div></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><b>Arvind S. Rao, PhD</b><br>Section of Biomedical Image Analysis<br>Department of Radiology<br>University of Pennsylvania<br>3600 Market Street, Suite 380<br>
Philadelphia, PA 19104<div>tel: (267) 283-8959</div><br>
</div>