diff --git a/Code/Common/otbGenericInterpolateImageFunction.h b/Code/Common/otbGenericInterpolateImageFunction.h index 0490c931fb79087a877d5bd19e5653f31cf2e3e5..a91d9480274d7ecc199c6e989845cb7314bc26b4 100644 --- a/Code/Common/otbGenericInterpolateImageFunction.h +++ b/Code/Common/otbGenericInterpolateImageFunction.h @@ -98,17 +98,20 @@ public itk::InterpolateImageFunction<TInputImage,TCoordRep> } /** Delete tables.*/ - void ResetOffsetTable(); + void ResetOffsetTable() const; /** Initialize used tables*/ - void InitializeTables(); + void InitializeTables() const; /** Fill the weight offset table*/ - void FillWeightOffsetTable(); + void FillWeightOffsetTable() const; protected: GenericInterpolateImageFunction(); ~GenericInterpolateImageFunction(); void PrintSelf(std::ostream& os, itk::Indent indent) const; - + + /** Call the superclass implementation and set the TablesHaveBeenGenerated + * flag to false */ + virtual void Modified(void); private: GenericInterpolateImageFunction(const Self&); //purposely not implemented @@ -118,17 +121,23 @@ public itk::InterpolateImageFunction<TInputImage,TCoordRep> // Constant to store twice the radius unsigned int m_WindowSize; - /** The offset array, used to keep a list of relevant - * offsets in the neihborhoodIterator */ - unsigned int *m_OffsetTable; /** Used function */ FunctionType m_Function; - /** Size of the offset table */ - unsigned int m_OffsetTableSize; /** Store the image dimension.*/ unsigned int m_ImageDimension; + + /** These members are declared mutable so that they can be + regenerated seamlessly inside the EvaluateAtContinuousIndex method if + they need to */ + /** Size of the offset table */ + mutable unsigned int m_OffsetTableSize; + /** The offset array, used to keep a list of relevant + * offsets in the neihborhoodIterator */ + mutable unsigned int *m_OffsetTable; /** Index into the weights array for each offset */ - unsigned int **m_WeightOffsetTable; + mutable unsigned int **m_WeightOffsetTable; + /** True if internal statistics have been generated */ + mutable bool m_TablesHaveBeenGenerated; }; } // end namespace itk diff --git a/Code/Common/otbGenericInterpolateImageFunction.txx b/Code/Common/otbGenericInterpolateImageFunction.txx index 0d1528102be1145fd57303b249db4c2be36843d5..06d9e8972810410fcfc47ef28a020e91fcd91109 100644 --- a/Code/Common/otbGenericInterpolateImageFunction.txx +++ b/Code/Common/otbGenericInterpolateImageFunction.txx @@ -28,6 +28,9 @@ GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoo { //m_Radius = 1; m_WindowSize = 1; + m_OffsetTable = NULL; + m_WeightOffsetTable = NULL; + m_TablesHaveBeenGenerated=false; } /** Destructor */ @@ -42,17 +45,25 @@ GenericInterpolateImageFunction<TInputImage, TFunction,TBoundaryCondition, TCoor template<class TInputImage, class TFunction, class TBoundaryCondition, class TCoordRep> void GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoordRep> -::ResetOffsetTable() +::ResetOffsetTable() const { // Clear the offset table - delete [] m_OffsetTable; + if(m_OffsetTable!=NULL) + { + delete [] m_OffsetTable; + m_OffsetTable=NULL; + } - // Clear the weights tables - for(unsigned int i=0; i < m_OffsetTableSize; i++) + // Clear the weights tales + if(m_WeightOffsetTable!=NULL) { - delete [] m_WeightOffsetTable[i]; + for(unsigned int i=0; i < m_OffsetTableSize; i++) + { + delete [] m_WeightOffsetTable[i]; + } + delete[] m_WeightOffsetTable; + m_WeightOffsetTable = NULL; } - delete[] m_WeightOffsetTable; } @@ -78,20 +89,24 @@ GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoo //m_Radius = rad; this->GetFunction().SetRadius(rad); m_WindowSize = rad << 1; - // Delete existing tables - this->ResetOffsetTable(); - // Tables initialization - this->InitializeTables(); - // fill the weigth table - this->FillWeightOffsetTable(); this->Modified(); } +template<class TInputImage, class TFunction, class TBoundaryCondition, class TCoordRep> +void +GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoordRep> +::Modified() +{ + Superclass::Modified(); + m_TablesHaveBeenGenerated=false; + +} + /** Initialize used tables*/ template<class TInputImage, class TFunction, class TBoundaryCondition, class TCoordRep> void GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoordRep> -::InitializeTables() +::InitializeTables() const { // Compute the offset table size m_OffsetTableSize = 1; @@ -115,7 +130,7 @@ GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoo template<class TInputImage, class TFunction, class TBoundaryCondition, class TCoordRep> void GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoordRep> -::FillWeightOffsetTable() +::FillWeightOffsetTable() const { // Initialize the neighborhood SizeType radius; @@ -171,6 +186,17 @@ typename GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondit GenericInterpolateImageFunction<TInputImage, TFunction, TBoundaryCondition, TCoordRep> ::EvaluateAtContinuousIndex(const ContinuousIndexType& index) const { + if(!m_TablesHaveBeenGenerated) + { + // Delete existing tables + this->ResetOffsetTable(); + // Tables initialization + this->InitializeTables(); + // fill the weigth table + this->FillWeightOffsetTable(); + m_TablesHaveBeenGenerated=true; + } + unsigned int dim; IndexType baseIndex; double distance[ImageDimension];