cmtkSearchTrace.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkSearchTrace_h_included_
00034 #define __cmtkSearchTrace_h_included_
00035 
00036 #include <stdlib.h>
00037 
00038 namespace
00039 cmtk
00040 {
00041 
00044 
00054 template<class R = short>
00055 class SearchTrace 
00056 {
00057 private:
00058   typedef struct _TraceListEntry 
00059   {
00061     R *RelativePosition;
00062 
00064     double FunctionValue;
00065 
00067     struct _TraceListEntry *Next;
00068   } TraceListEntry;
00069 
00072   int DOF;
00073 
00076   TraceListEntry *List;
00077 
00089   int IsHit ( const TraceListEntry* entry, const int dir, const R step ) const 
00090   {
00091     for ( int idx=0; idx<DOF; ++idx )
00092       if ( entry->RelativePosition[idx] && ( (dir != idx) || (entry->RelativePosition[idx] != step) ) )
00093         return 0;
00094     
00095     return 1;
00096   }
00097   
00098 public:
00102   SearchTrace ( const int _DOF ) {
00103     DOF = _DOF;
00104     List = NULL;
00105   }
00106 
00110   ~SearchTrace () { Clear(); }
00111 
00121   void Add ( const double value, const int dir = 0, const R step = 0 ) 
00122   {
00123     TraceListEntry *add = new TraceListEntry;
00124     add->RelativePosition = Memory::AllocateArray<R>( DOF );
00125     memset( add->RelativePosition, 0, sizeof(R) );
00126     add->RelativePosition[dir] += step;
00127     add->FunctionValue = value;
00128     add->Next = List;
00129     List = add;
00130   }
00131 
00141   int Get ( double& value, const int dir = 0, const R step = 0 ) const 
00142   {
00143     TraceListEntry *cursor = List;
00144     while ( cursor ) 
00145       {
00146       if ( IsHit( cursor, dir, step ) ) 
00147         {
00148         value = cursor->FunctionValue;
00149         return 1;
00150         }
00151       cursor = cursor ->Next;
00152       }
00153     return 0;
00154   }
00155 
00162   void Move ( const int dir, const R step ) 
00163   {
00164     TraceListEntry *cursor = List;
00165     while ( cursor ) 
00166       {
00167       cursor->RelativePosition[dir] -= step;
00168       cursor = cursor ->Next;
00169       }    
00170   }
00171 
00176   void Clear () 
00177   {
00178     while ( List ) 
00179       {
00180       TraceListEntry *save = List->Next;
00181       Memory::DeleteArray( List->RelativePosition );
00182       delete List;
00183       List = save;
00184       }
00185   }
00186 };
00187 
00189 
00190 } // namespace cmtk
00191 
00192 #endif // #ifndef __cmtkSearchTrace_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines