[Insight-users] how to write a single program working for both 2D and 3D cases?

Luis Ibanez luis . ibanez at kitware . com
Sun, 23 Nov 2003 17:41:38 -0500


Hi Bing,

Unless your program is a small demo or a toy example,
it is quite a bad idea to try to make it work for
several dimensions. In real medical image applications
you want to reduce ambiguity to a minimum, and defining
the image dimension is one of the characteristics that
you must resolve early on the design phase of your
application.

If you insist is mixing dimensions, what you may want to
do is to encapsulate all your program in a class that is
templated over dimension. Then in the main.cxx of your app,
you instantiate this class for 2D and 3D inside a switch
statement.

You may look at the Volview plugins for inspiration,
(although they are templated over pixel type instead of
dimension). This code is available in InsightApplications/
VolviewPlugins.

You will also find useful to look the code of the multi-
resolution registration application in

         InsightApplications/MultResMIRegistration

where the application is encapsulated in a top level class.



But, again, take some time and some coffee before you
go down this path. This is a risky idea for medical
applications.


Regards,


   Luis



-------------------
Bing Jian wrote:
> Hello, everyone,
> 
>    I'm trying to write a single program that can handle
> both 2D and 3D cases. First, the program accepts command
> line and gets the filename. If it's meta header file,
> then we can get the dimension info and pixel type info. 
> Then in my program,  I tried to write something like
> 
>   
>   if (dim==2)
>   {
>      const unsigned int dimension = 2;
>   }
>   else if (dim==3) 
>   {
>      const unsigned int dimension = 3;
>   }
> 
>   if (pixeltype == FLOAT)
>   {
>       typedef float PixelType; 
>   }
>   else if (pixeltype = UCHAR)
>   {
>      tyepdef unsigned char PixelType;
>   } 
> 
>   It won't work for several reasons:
>  
> (1) Being put inside if{} block, dimension and PixelType 
>    become invisible to other parts of program.
>  
> (2) template of class itk::image requires dimension to be const,
>   so I cannot define it outside and then change it later. 
>  
>   I hope you guys can understand my problems, any suggestions
> would be appreciated.
> 
> 
>    Have a nice weekend!
> 
> --
> Best wishes
> Bing Jian
> bjian at cise . ufl . edu
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk . org
> http://www . itk . org/mailman/listinfo/insight-users
>