[Insight-users] Design question when using ITK classes

Anja Ende anja.ende at googlemail.com
Wed Feb 20 09:01:26 EST 2008


Hi Bill,
Thanks for your reply.

However, with this scheme you are unable to have <i>class member
variables</i> that are of templated type. Say for example, I need to read
data from a file inside a class method. So, I find the underlying data type
and dimensions and read the data. However, if I want to access this data
later or basically store it in the class, how can I do that?

For example,

If I have a class

MyReader
{
      void SomeReaderMethod()
      {
           // find the type and initialize
           MyTemplatedMethod<datatype, dimensions> mtm;
      }
}

This variable now only has local scope inside the function. How can I make a
class member point to it since the templated arguments are only defined at
run-time?

I hope I am successful in specifying my problem here... have a feeling that
I am not being clear enough as to what the hurdle is...

Thanks,
Anja

On 20/02/2008, Bill Lorensen <bill.lorensen at gmail.com> wrote:
>
> Anja,
>
> Yes, at compile time you will need to instantiate each type you expect. At
> runtime you can determine which type to use with something like this:
>
> namespace itk
> {
>   // Description:
>   // Get the PixelType and ComponentType from fileName
>   void GetImageType (std::string fileName,
>                      ImageIOBase::IOPixelType &pixelType,
>                      ImageIOBase::IOComponentType &componentType)
>     {
>       typedef itk::Image<unsigned char, 3> ImageType;
>       itk::ImageFileReader<ImageType>::Pointer imageReader =
>         itk::ImageFileReader<ImageType>::New();
>       imageReader->SetFileName(fileName.c_str());
>       imageReader->UpdateOutputInformation();
>       pixelType = imageReader->GetImageIO()->GetPixelType();
>       componentType = imageReader->GetImageIO()->GetComponentType();
>     }
> }
>
> Then in your application do something like this:
>
>   itk::ImageIOBase::IOPixelType pixelType;
>   itk::ImageIOBase::IOComponentType componentType;
>   try
>     {
>     itk::GetImageType (inputVolume, pixelType, componentType);
>     // This filter handles all types
>
>     switch (componentType)
>       {
>       case itk::ImageIOBase::UCHAR:
>         return DoIt( static_cast<unsigned char>(0));
>         break;
>       case itk::ImageIOBase::CHAR:
>         return DoIt( static_cast<char>(0));
>         break;
>       case itk::ImageIOBase::USHORT:
>         return DoIt( static_cast<unsigned short>(0));
>         break;
>       case itk::ImageIOBase::SHORT:
>         return DoIt( static_cast<short>(0));
>         break;
>       case itk::ImageIOBase::UINT:
>         return DoIt( static_cast<unsigned int>(0));
>         break;
>       case itk::ImageIOBase::INT:
>         return DoIt( static_cast<int>(0));
>         break;
>       case itk::ImageIOBase::ULONG:
>         return DoIt( static_cast<unsigned long>(0));
>         break;
>       case itk::ImageIOBase::LONG:
>         return DoIt( static_cast<long>(0));
>         break;
>       case itk::ImageIOBase::FLOAT:
>         return DoIt( static_cast<float>(0));
>         break;
>       case itk::ImageIOBase::DOUBLE:
>         return DoIt( static_cast<double>(0));
>         break;
>       case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
>       default:
>         std::cout << "unknown component type" << std::endl;
>         break;
>       }
>     }
>   catch( itk::ExceptionObject &excep)
>     {
>     std::cerr << argv[0] << ": exception caught !" << std::endl;
>     std::cerr << excep << std::endl;
>     return EXIT_FAILURE;
>     }
>   return EXIT_SUCCESS;
> }
> where Doit is your method:
> template<class T> int DoIt(T )
> {
> ...
> }
>
> CAUTION: your compiler may not be able to instantiate all type without
> having stack overflow or other problems, You can sometimes combine some of
> the types (like unsigned int and int) to reduce the number needed.
>
> Bill
>
>
>
> On Wed, Feb 20, 2008 at 7:04 AM, Anja Ende <anja.ende at googlemail.com>
> wrote:
>
> > Hi everyone,
> >
> > A quick question about using ITK classes. I am using C++ and am having a
> > bit of trouble understanding how I can use the templated classes without
> > instantiating for every possible type combination.
> >
> > A simple example:
> >
> > I have a custom file reader where the data type and dimensions are
> > something that is only available at run time..
> >
> > So, now if I want to have a class member that uses a reader, I have to
> > somehow define these template parameters at runtime... What I wanted was to
> > have one reader which has one instance of the ImageImportFilter instance.
> > However, to declare this class I need to define the template parameters for
> > the ImportImageFilter at design time. So, now if a file is inputted with a
> > different input data type and a dimension, I am stuck!
> >
> > Any ideas??
> >
> > Cheers,
> >
> > Anja
> > _______________________________________________
> > Insight-users mailing list
> > Insight-users at itk.org
> > http://www.itk.org/mailman/listinfo/insight-users
> >
> >
>


-- 
Cheers,

Anja
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20080220/1d2d0e09/attachment.htm


More information about the Insight-users mailing list