NIREP

NIREPDataManager.cxx

Go to the documentation of this file.
00001 #include "NIREPDataManager.h"
00002 #include "NIREPResourceDatabase.h"
00003 #include "NIREPDefinitions.h"
00004 #include <algorithm>
00005 #include <cstdlib>
00006 #include "Interface.h"
00007 #include "gecSICLETransformation.h"
00008 #include "gecAIRTransformation.h"
00009 
00010 /* Constructor */
00011 NIREPDataManager::NIREPDataManager(void)
00012 {
00013   // Set data memory usage to zero
00014   //this->memoryUsage = 0;
00015 }
00016 
00017 
00018 /* Destructor */
00019 NIREPDataManager::~NIREPDataManager(void)
00020 {
00021   for(LoadedSpatialDataList::iterator entry=this->loadedSpatialDataList.begin();
00022         entry!=this->loadedSpatialDataList.end(); entry++)
00023     delete (entry->second).data;
00024   this->loadedSpatialDataList.clear();
00025   this->spatialDataResourceListTree.dataset.clear();
00026   this->transformationResourceList.clear();
00027 }
00028 
00029 
00040 void NIREPDataManager::SetLoadedSpatialDataListEntry(const std::string& dmid,
00041                                                      gec::SpatialData* data)
00042 {
00043   // Add new entry if entry doesn't already exist.
00044   // Else, update existing data with new data
00045   LoadedSpatialData entry;
00046   entry.data = data; entry.refCount = 0;
00047   this->loadedSpatialDataList[dmid] = entry;
00048   mainPage->addDatabaseItem(dmid, data->GetVTKData()->GetActualMemorySize(), this->GetMemoryUsage());
00049 }
00050 
00051 
00061 gec::SpatialData* NIREPDataManager::GetLoadedSpatialDataListEntry(const std::string& dmid, DisplayDescription * displayDescription)
00062 {
00063   LoadedSpatialDataList::iterator entry = this->loadedSpatialDataList.find(dmid);
00064   if(entry != this->loadedSpatialDataList.end()) {
00065     // Increment reference counter
00066     (entry->second).refCount++;
00067     return (entry->second).data;
00068   }
00069   // Did not find the requested data loaded in memory.  Now see if it is on disk.
00070   ResourceDescription * rdescrip;
00071   bool LoadOkay;
00072 
00073   // Check if data manager id is in the data description list.  If so then load.
00074   rdescrip = displayDescription->GetRDL()->GetDataDescrip(dmid);
00075   if (rdescrip != NULL){
00076     // Load data into loadedSpatialDataList
00077     LoadOkay = LoadSpatialData(dmid,displayDescription);
00078     if (LoadOkay){
00079       // Get spatial data from loadedSpatialDataList
00080       entry = this->loadedSpatialDataList.find(dmid);
00081       if(entry != this->loadedSpatialDataList.end()) {
00082         // Increment reference counter
00083         (entry->second).refCount++;
00084         return (entry->second).data;
00085       } else {
00086         wxString temp = wxT("ERROR - Unable to retrieve resource with given ID: ");
00087         temp.Append(wxString::Format (wxT("%s"), dmid.c_str()));
00088         wxLogError(temp);
00089         return NULL; // Error could not find data even though it was just inserted
00090       }
00091     } else {
00092       wxString temp = wxT("ERROR - Unable to load resource with given ID: ");
00093       temp.Append(wxString::Format (wxT("%s"), dmid.c_str()));
00094       wxLogError(temp);
00095 
00096       return NULL; // Error: Could not find loaded data
00097     }
00098   }
00099 
00100   // Check if data manager id is in the transformation description list.  If so then load.
00101   rdescrip = displayDescription->GetRDL()->GetTransformDescrip(dmid);
00102   if (rdescrip != NULL){
00103     // Load data into loadedSpatialDataList
00104     LoadOkay = LoadTransformation(dmid,displayDescription);
00105     if (LoadOkay){
00106       // Get spatial data from loadedSpatialDataList
00107       entry = this->loadedSpatialDataList.find(dmid);
00108       if(entry != this->loadedSpatialDataList.end()) {
00109         // Increment reference counter
00110         (entry->second).refCount++;
00111         return (entry->second).data;
00112       } else {
00113         wxString temp = wxT("ERROR - Unable to retrieve resource with given ID: ");
00114         temp.Append(wxString::Format (wxT("%s"), dmid.c_str()));
00115         wxLogError(temp);
00116         return NULL; // Error could not find data even though it was just inserted
00117       }
00118     } else {
00119       wxString temp = wxT("ERROR - Unable to load resource with given ID: ");
00120       temp.Append(wxString::Format (wxT("%s"), dmid.c_str()));
00121       wxLogError(temp);
00122 
00123       return NULL; // Error: Could not find loaded data
00124     }
00125   }
00126 
00127  
00128   // Return NULL so that the Evaluator Process can respond appropriately
00129   return NULL;
00130 
00131   /* Worry about memory management later
00132   // Check if entry is in flushMe queue and remove if present
00133   for(std::vector<std::string>::iterator iter = flushMe.begin();
00134         iter!=flushMe.end(); ++iter) {
00135     if(*iter == dataID) {
00136       flushMe.erase(iter);
00137     }
00138   }
00139   */
00140 
00141 }
00142 
00147 bool NIREPDataManager::LoadSpatialData(const std::string& datasetID,
00148                                        const std::string& dataID,
00149                                        DisplayDescription * displayDescription){
00150 
00151   const std::string dmid = EvalCmd["SpatialData"] + "(" + datasetID + "," + dataID + ")";
00152   return LoadSpatialData(dmid,displayDescription);
00153 }
00154 
00159 bool NIREPDataManager::LoadSpatialData(const std::string& dmid,
00160                                         DisplayDescription * displayDescription){
00161 
00162   ResourceDescription * rdescrip = displayDescription->GetRDL()->GetDataDescrip(dmid);
00163 
00164   if (rdescrip != NULL){
00165     gec::SpatialData *data = NULL;
00166     if (rdescrip->GetDataType().compare("image") == 0){
00167       data = new gec::Image(rdescrip->GetFilename("0"));
00168     } 
00169     else if (rdescrip->GetDataType().compare("objectmap") == 0){
00170       data = new gec::ObjectMap(rdescrip->GetFilename("0"));
00171     } 
00172     else if (rdescrip->GetDataType().compare("transformation") == 0){
00173       LoadTransformation(dmid,displayDescription);
00174     } 
00175     else {
00176       // Other SpatialData type support later
00177       wxLogError( _("Unsupported SpatialData type!") );
00178       return false;
00179     }
00180     this->SetLoadedSpatialDataListEntry(dmid, data);
00181     return true;
00182   } else {
00183     wxString temp = wxT("ERROR - The spatial data resource did not contain any entry with the given ID: ");
00184     temp.Append(wxString::Format (wxT("%s"), dmid.c_str()));
00185     wxLogError(temp);
00186     return false;
00187   }
00188 
00189 }
00190 
00195 bool NIREPDataManager::LoadTransformation(const std::string& source,
00196                                           const std::string& target,
00197                                           const std::string& transformation,
00198                                           DisplayDescription * displayDescription){
00199 
00200   const std::string dmid =
00201      EvalCmd["Transformation"] + "(" + source + "," + target + "," + transformation + ")";
00202   return LoadTransformation(dmid,displayDescription);
00203 }
00204 
00209 bool NIREPDataManager::LoadTransformation(const std::string& dmid,
00210                                           DisplayDescription * displayDescription){
00211 
00212   ResourceDescription * rdescrip = displayDescription->GetRDL()->GetTransformDescrip(dmid);
00213 
00214   if (rdescrip != NULL){
00215     gec::SpatialData* trans = NULL;
00216     if (rdescrip->GetFormat().compare("displacement") == 0){
00217       trans = new gec::Transformation(rdescrip->GetFilename("0"));
00218     } 
00219     else if (rdescrip->GetFormat().compare("displacement3") == 0){
00220       const std::string files[3] = {rdescrip->GetFilename("0"),
00221                                     rdescrip->GetFilename("1"),
00222                                     rdescrip->GetFilename("2")};
00223       trans = new gec::Transformation(&(files[0]));
00224     } 
00225     else if (rdescrip->GetFormat().compare("sicle_coeff") == 0){
00226       trans = new gec::SICLETransformation(rdescrip->GetFilename("0"));
00227     }
00228     else if (rdescrip->GetFormat().compare("air5") == 0){
00229       trans = new gec::AIRTransformation(rdescrip->GetFilename("0"));
00230     }
00231     else {
00232       // Other SpatialData type support later
00233       wxLogError( _("Unsupported Transformation type!") );
00234       return false;
00235     }
00236     // Scale all transformations to image space before saving them to the Loaded Spatial Data List
00237     if (rdescrip->GetTransformUnits().compare("unit_cube") == 0){
00238       dynamic_cast<gec::Transformation *>(trans)->Rescale(trans->GetDimension());
00239     }
00240     if (rdescrip->GetTransformUnits().compare("physical_space") == 0){
00241       double* spacing = trans->GetSpacing();
00242       double ispacing[] = {1.0/spacing[0], 1.0/spacing[1], 1.0/spacing[2]};
00243       dynamic_cast<gec::Transformation *>(trans)->Rescale(ispacing);
00244     }
00245 
00246     this->SetLoadedSpatialDataListEntry(dmid, trans);
00247     return true;
00248   } else {
00249     wxString temp = wxT("ERROR - Resource Description List did not contain entry with the given ID: ");
00250     temp.Append(wxString::Format (wxT("%s"), dmid.c_str()));
00251     wxLogError(temp);
00252     return false;
00253   }
00254 
00255 }
00256 
00257 
00265 bool NIREPDataManager::ReleaseLoadedSpatialDataListEntry(
00266                                                const std::string& dmid)
00267 {
00268   LoadedSpatialDataList::iterator entry =
00269                                  this->loadedSpatialDataList.find(dmid);
00270   if(entry == this->loadedSpatialDataList.end()) {
00271     wxString temp = wxT("ERROR locating data entry with given ID: ");
00272     temp.Append(wxString::Format (wxT("%s"), dmid.c_str()));
00273     wxLogError(temp);
00274     return false;
00275   }
00276 
00277   // Decrement reference counter
00278   //if((entry->second).refCount > 0) {
00279   //  (entry->second).refCount--;
00280   //}
00281 
00282   
00283 
00284 
00285 
00286   /* Worry about memory management later
00287   // If refCount is zero, ready to be flushed out
00288   if((entry->second).refCount == 0) {
00289     this->flushMe.push((entry->first));
00290   }
00291   */
00292 
00293   delete entry->second.data;
00294   this->loadedSpatialDataList.erase(entry);
00295 
00296   mainPage->SetTotalMemoryUsage(this->GetMemoryUsage());
00297 
00298   return true;
00299 }
00300 
00301 unsigned long NIREPDataManager::GetMemoryUsage()
00302 {
00303   LoadedSpatialDataList::iterator tempIter = this->loadedSpatialDataList.begin();
00304   unsigned long memoryUsage = 0;
00305   while(tempIter!=this->loadedSpatialDataList.end())
00306   { 
00307     //for(unsigned int i=0; i< tempIter->second.data->GetNumberOfData(); i++)
00308     //{
00309       memoryUsage = memoryUsage + tempIter->second.data->GetVTKData()->GetActualMemorySize();
00310     //}
00311     tempIter++;
00312   }
00313   return memoryUsage;
00314 }
00315 
00316 
All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines