/vol/vipdata/irtk/packages/registration/include/irtkUtil.h

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkUtil.h 2 2008-12-23 12:40:14Z dr $
00005   Copyright : Imperial College, Department of Computing
00006               Visual Information Processing (VIP), 2008 onwards
00007   Date      : $Date: 2008-12-23 12:40:14 +0000 (Tue, 23 Dec 2008) $
00008   Version   : $Revision: 2 $
00009   Changes   : $Author: dr $
00010 
00011 =========================================================================*/
00012 
00013 #ifndef _IRTKUTIL_H
00014 
00015 #define _IRTKUTIL_H
00016 
00018 typedef struct _irtkHistoryEntry {
00019 
00020 public:
00021 
00023   double *_parameter;
00024 
00026   double _similarity;
00027 
00029   struct _irtkHistoryEntry *_succ;
00030 
00031 } irtkHistoryEntry;
00032 
00034 class irtkHistory
00035 {
00036 
00038   irtkHistoryEntry *_entries;
00039 
00041   int _no;
00042 
00044   int _success;
00045 
00047   int _failed;
00048 
00050   int IsEqual(const irtkHistoryEntry *, const irtkTransformation *);
00051 
00052 public:
00053 
00055   irtkHistory();
00056 
00058   ~irtkHistory();
00059 
00061   void Add(const irtkTransformation *, double);
00062 
00066   int Find(const irtkTransformation *, double &);
00067 
00069   void Print();
00070 
00072   void Clear();
00073 };
00074 
00075 inline irtkHistory::irtkHistory()
00076 {
00077   _entries = NULL;
00078   _no      = 0;
00079   _success = 0;
00080   _failed  = 0;
00081 }
00082 
00083 inline void irtkHistory::Clear()
00084 {
00085   irtkHistoryEntry *entry;
00086 
00087   entry = _entries;
00088   while (entry != NULL) {
00089     _entries = entry->_succ;
00090     delete [] entry->_parameter;
00091     delete entry;
00092     entry = _entries;
00093   }
00094   _entries = NULL;
00095   _no      = 0;
00096   _success = 0;
00097   _failed  = 0;
00098 }
00099 
00100 inline irtkHistory::~irtkHistory()
00101 {
00102   this->Clear();
00103 }
00104 
00105 inline void irtkHistory::Add(const irtkTransformation *transformation,
00106                              double similarity)
00107 {
00108   // Allocate a new entry
00109   irtkHistoryEntry *entry = new irtkHistoryEntry;
00110 
00111   // Create a new entry
00112   entry->_similarity = similarity;
00113   entry->_parameter  = new double[transformation->NumberOfDOFs()];
00114   for (int i = 0; i < transformation->NumberOfDOFs(); i++) {
00115     entry->_parameter[i] = transformation->Get(i);
00116   }
00117   entry->_succ = _entries;
00118 
00119   // Insert the new entry at the beginning of the list
00120   _entries = entry;
00121   _no++;
00122 }
00123 
00124 inline int  irtkHistory::IsEqual(const irtkHistoryEntry *entry,
00125                                  const irtkTransformation *transformation)
00126 {
00127   for (int i = 0; i < transformation->NumberOfDOFs(); i++) {
00128     if (entry->_parameter[i] != transformation->Get(i)) return False;
00129   }
00130   return True;
00131 }
00132 
00133 inline int  irtkHistory::Find(const irtkTransformation *transformation,
00134                               double &similarity)
00135 {
00136   for (irtkHistoryEntry *entry = _entries; entry != NULL; entry = entry->_succ) {
00137     if (IsEqual(entry, transformation) == True) {
00138       similarity = entry->_similarity;
00139       _success++;
00140       return True;
00141     }
00142   }
00143   _failed++;
00144   return False;
00145 }
00146 
00147 inline void irtkHistory::Print()
00148 {
00149   if (_no > 0) {
00150     cout << "Cache has " << _no << " entries" << endl;
00151     cout << "Cache statistics: " << 100 * _success / (_success + _failed)
00152          << " % hits (" << _success << " out of " << (_success + _failed)
00153          << ")" << endl;
00154   }
00155 }
00156 
00157 extern void irtkPadding(irtkGreyImage &, irtkGreyPixel);
00158 extern void irtkPadding(irtkGreyImage &, irtkGreyPixel, irtkFreeFormTransformation3D *ffd);
00159 extern int  irtkCalculateNumberOfBins(irtkGreyImage *, int, int, int);
00160 extern int  irtkCalculateNumberOfBins(irtkGreyImage **, int, int, int, int);
00161 extern double GuessResolution(double, double);
00162 extern double GuessResolution(double, double, double);
00163 extern int GuessPadding(irtkGreyImage &);
00164 extern int read_line(istream &, char *, char *&);
00165 
00166 
00167 #ifdef HAS_VTK
00168 
00169 #include <vtkPolyData.h>
00170 
00171 void MarkBoundary(vtkPolyData *polydata);
00172 
00173 #endif
00174 
00175 #endif