<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><DIV>Dear All:</DIV>
<DIV> </DIV>
<DIV>I trying to use some function template in my code. The below code is reading an image and do isotropic resampling. Now I put main function and function template in signle .cxx file. The code can be run.</DIV>
<DIV> </DIV>
<DIV>1: I want to seperate my function template with main function. If I put the whole implementation of templation function in .h file, I can make the exctable file.</DIV>
<DIV>2: If I want have individual implementation file .txx or .cxx( I tried all) and plus the header file, the code cannot be compiled and showed undefined reference to the template function. I did a google search, seems G++ requires to put all the implementation in the header.</DIV>
<DIV>3: I double the above requirement greatly. Since many of ITK template class has .txx and .h file. Can somebody teach me how can I compile the code when the .txx and .h and main code are seperate files.</DIV>
<DIV> </DIV>
<DIV>Thanks </DIV>
<DIV>Baoyun</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>#if defined(_MSC_VER)<BR>#pragma warning ( disable : 4786 )<BR>#endif</DIV>
<DIV>#ifdef __BORLANDC__<BR>#define ITK_LEAN_AND_MEAN<BR>#endif</DIV>
<DIV><BR>#include "itkImage.h"<BR>#include "itkImageFileReader.h"<BR>#include "itkImageFileWriter.h"</DIV>
<DIV>// Software Guide : BeginCodeSnippet<BR>#include "itkResampleImageFilter.h"<BR>#include "itkRecursiveGaussianImageFilter.h"<BR>// Software Guide : EndCodeSnippet</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><BR>// Software Guide : BeginCodeSnippet<BR>#include "itkIdentityTransform.h"<BR>#include "itkLinearInterpolateImageFunction.h"<BR>#include "itkAffineTransform.h"<BR>// Software Guide : EndCodeSnippet</DIV>
<DIV>// Software Guide : BeginCodeSnippet<BR>#include "itkIntensityWindowingImageFilter.h"<BR>#include "itkCastImageFilter.h"<BR>// Software Guide : EndCodeSnippet</DIV>
<DIV>template <class TInputFilter, class TInternalFilter, class TScale></DIV>
<DIV>void ConfidenceConnected( TInputFilter* InputFilter, TInternalFilter* InternalFilter, TScale scale)<BR>{<BR> </DIV>
<DIV> //typedef typename TInputFilter::OutputImageType InputImageType;<BR> typedef typename TInputFilter::OutputImageType OutputImageType;<BR> typedef typename TInternalFilter::OutputImageType InternalImageType;<BR> typedef typename TInternalFilter::InputImageType InputImageType;<BR>}</DIV>
<DIV><BR>template <class TInputFilter, class TInternalFilter, class TScale></DIV>
<DIV>void IsotropicResample( TInputFilter* InputFilter, TInternalFilter* InternalFilter, TScale scale)<BR>{<BR> </DIV>
<DIV> //typedef typename TInputFilter::OutputImageType InputImageType;<BR> typedef typename TInputFilter::OutputImageType OutputImageType;<BR> typedef typename TInternalFilter::OutputImageType InternalImageType;<BR> typedef typename TInternalFilter::InputImageType InputImageType;<BR> //typedef TInputFilter InputImageType;</DIV>
<DIV> typedef itk::ImageFileWriter< InternalImageType > WriterType1;<BR> typename WriterType1::Pointer writer1=WriterType1::New();<BR> typedef itk::CastImageFilter< InputImageType,InternalImageType> CastingFilterType1;<BR> typename CastingFilterType1::Pointer caster11 = CastingFilterType1::New();</DIV>
<DIV> typedef itk::CastImageFilter< InternalImageType,OutputImageType> CastingFilterType2;<BR> typename CastingFilterType1::Pointer caster21 = CastingFilterType1::New();</DIV>
<DIV> typedef itk::RecursiveGaussianImageFilter< <BR> InternalImageType,<BR> InternalImageType > GaussianFilterType;<BR> <BR> <BR> <BR> InputFilter->Update();<BR> //InternalFilter->SetInput(InputFilter->GetOutput());<BR> //InternalFilter->Update();</DIV>
<DIV> typename InputImageType::ConstPointer inputImage = InputFilter->GetOutput();</DIV>
<DIV> typename InputImageType::SpacingType inputSpacing= inputImage->GetSpacing();<BR> <BR> const double isoSpacing = sqrt( inputSpacing[2] * inputSpacing[0] )*scale;</DIV>
<DIV> caster11->SetInput(InputFilter->GetOutput());</DIV>
<DIV> typedef itk::ResampleImageFilter<<BR> InternalImageType, OutputImageType > ResampleFilterType;</DIV>
<DIV> typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();</DIV>
<DIV> <BR> typename GaussianFilterType::Pointer smootherX = GaussianFilterType::New();<BR> typename GaussianFilterType::Pointer smootherY = GaussianFilterType::New();<BR> typename GaussianFilterType::Pointer smootherZ = GaussianFilterType::New();</DIV>
<DIV> if(inputSpacing[0]<isoSpacing&&inputSpacing[3]<isoSpacing)<BR> {</DIV>
<DIV> smootherX->SetInput( caster11->GetOutput() );<BR> smootherY->SetInput( smootherX->GetOutput() );<BR> smootherZ->SetInput( smootherY->GetOutput());<BR> resampler->SetInput(smootherZ->GetOutput());<BR> <BR> smootherX->SetDirection( 0 );<BR> smootherY->SetDirection( 1 );<BR> smootherZ->SetDirection( 2 );</DIV>
<DIV> smootherX->SetSigma( isoSpacing );<BR> smootherY->SetSigma( isoSpacing );<BR> smootherZ->SetSigma( isoSpacing );<BR> </DIV>
<DIV> smootherX->SetNormalizeAcrossScale( false );<BR> smootherY->SetNormalizeAcrossScale( false );<BR> smootherZ->SetNormalizeAcrossScale( false );<BR> </DIV>
<DIV> }</DIV>
<DIV><BR>if(inputSpacing[0]<isoSpacing&&inputSpacing[3]>isoSpacing)<BR> {<BR> typename GaussianFilterType::Pointer smootherX = GaussianFilterType::New();<BR> typename GaussianFilterType::Pointer smootherY = GaussianFilterType::New();</DIV>
<DIV> smootherX->SetInput( caster11->GetOutput() );<BR> smootherY->SetInput( smootherX->GetOutput() );<BR> resampler->SetInput(smootherY->GetOutput());</DIV>
<DIV> smootherX->SetDirection( 0 );<BR> smootherY->SetDirection( 1 );</DIV>
<DIV> smootherX->SetSigma( isoSpacing );<BR> smootherY->SetSigma( isoSpacing );</DIV>
<DIV> </DIV>
<DIV> smootherX->SetNormalizeAcrossScale( false );<BR> smootherY->SetNormalizeAcrossScale( false );<BR> <BR> </DIV>
<DIV> }</DIV>
<DIV><BR>if(inputSpacing[0]>=isoSpacing&&inputSpacing[3]>=isoSpacing)<BR> {<BR> <BR> resampler->SetInput(caster11->GetOutput());<BR> }<BR> <BR> const unsigned int Dimension=3;<BR> typedef itk::AffineTransform<double, Dimension> TransformType;</DIV>
<DIV> typename TransformType::Pointer transform = TransformType::New();<BR> resampler->SetTransform(transform);</DIV>
<DIV> typedef itk::LinearInterpolateImageFunction< <BR> InternalImageType, double > InterpolatorType;<BR> typename InterpolatorType::Pointer interpolator = InterpolatorType::New();<BR> resampler->SetInterpolator( interpolator );</DIV>
<DIV> resampler->SetDefaultPixelValue( -1024 );</DIV>
<DIV> typename OutputImageType::SpacingType spacing;<BR>// <BR> spacing[0] = isoSpacing;<BR> spacing[1] = isoSpacing;<BR> spacing[2] = isoSpacing;<BR>// // <BR> resampler->SetOutputSpacing( spacing );<BR> resampler->SetOutputOrigin( inputImage->GetOrigin() );<BR> resampler->SetOutputDirection( inputImage->GetDirection() );<BR>// <BR>// <BR> typename InputImageType::SizeType inputSize = <BR> inputImage->GetLargestPossibleRegion().GetSize();<BR>// <BR> typedef typename InputImageType::SizeType::SizeValueType SizeValueType;<BR>// <BR> const double dx = inputSize[0] * inputSpacing[0] / isoSpacing;<BR> const double dy =
inputSize[1] * inputSpacing[1] / isoSpacing;<BR> const double dz = (inputSize[2] - 1 ) * inputSpacing[2] / isoSpacing;</DIV>
<DIV> typename InputImageType::SizeType size;<BR>// <BR> size[0] = static_cast<SizeValueType>( dx );<BR> size[1] = static_cast<SizeValueType>( dy );<BR> size[2] = static_cast<SizeValueType>( dz );<BR>// <BR> resampler->SetSize( size );<BR> InternalFilter->SetInput(resampler->GetOutput());<BR> writer1->SetFileName("../data/out_test.hdr");<BR> writer1->SetInput(InternalFilter->GetOutput());</DIV>
<DIV> <BR> // inputSpacing = inputImage->GetSpacing();<BR> // std::cout << inputImage->GetSpacing()<< std::endl;</DIV>
<DIV> </DIV>
<DIV> <BR>// <BR>// // InternalFilter->SetInput(InputFilter->GetOutput());<BR>// <BR> try<BR> {<BR> writer1->Update();<BR> }<BR> catch( itk::ExceptionObject & excep )<BR> {<BR> std::cerr << "Exception caught !" << std::endl;<BR> std::cerr << excep << std::endl;<BR> }<BR> <BR>// </DIV>
<DIV><BR>};</DIV>
<DIV><BR>int main( int argc, char * argv[] )<BR>{<BR> if( argc < 3 )<BR> {<BR> std::cerr << "Usage: " << std::endl;<BR> std::cerr << argv[0] << " inputImageFile outputImageFile lower upper " << std::endl; <BR> return EXIT_FAILURE;<BR> }</DIV>
<DIV><BR> const unsigned int Dimension = 3;</DIV>
<DIV> typedef short InputPixelType;<BR> typedef float InternalPixelType;</DIV>
<DIV> typedef itk::Image< InputPixelType, Dimension > InputImageType;<BR> typedef itk::Image< InternalPixelType, Dimension > InternalImageType;<BR> typedef short OutputPixelType;<BR> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<BR>// Software Guide : EndCodeSnippet</DIV>
<DIV> </DIV>
<DIV> typedef itk::ImageFileReader< InputImageType > ReaderType;</DIV>
<DIV> ReaderType::Pointer reader = ReaderType::New();</DIV>
<DIV> reader->SetFileName( argv[1] );</DIV>
<DIV> typedef itk::ImageFileWriter< InternalImageType > WriterType1;<BR> typedef itk::ImageFileWriter< OutputImageType > WriterType;</DIV>
<DIV> </DIV>
<DIV> InternalImageType::Pointer isotropic1=InternalImageType::New();</DIV>
<DIV><BR> try <BR> {<BR> reader->Update();<BR> }<BR> catch( itk::ExceptionObject & excep )<BR> {<BR> std::cerr << "Exception caught!" << std::endl;<BR> std::cerr << excep << std::endl;<BR> }</DIV>
<DIV><BR>typedef itk::CastImageFilter< InputImageType,InternalImageType> CastingFilterType1;<BR> CastingFilterType1::Pointer caster11 = CastingFilterType1::New();</DIV>
<DIV> <BR> typedef itk::CastImageFilter< InternalImageType,OutputImageType> CastingFilterType2;<BR> CastingFilterType1::Pointer caster21 = CastingFilterType1::New();<BR> <BR> double scale=1.0;</DIV>
<DIV> IsotropicResample <ReaderType,CastingFilterType1,double> (reader,caster11,scale);<BR> <BR> WriterType1::Pointer writer1 = WriterType1::New();</DIV>
<DIV> writer1->SetFileName( argv[2] );<BR> writer1->SetInput(caster11->GetOutput());<BR> <BR> try <BR> {<BR> writer1->Update();<BR> }<BR> catch( itk::ExceptionObject & excep )<BR> {<BR> std::cerr << "Exception caught !" << std::endl;<BR> std::cerr << excep << std::endl;<BR> }</DIV>
<DIV><BR> return EXIT_SUCCESS;<BR>}</DIV>
<DIV> </DIV></div><br>
</body></html>