//--------------------------------------------------------------------
clitk::AnatomicalFeatureDatabase::AnatomicalFeatureDatabase()
{
- SetFilename("noname.afdb");
+ SetFilename("default.afdb");
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
void clitk::AnatomicalFeatureDatabase::Load()
{
+ m_MapOfTag.clear();
// open file
std::ifstream is;
openFileForReading(is, GetFilename());
while (!is.fail()) {
std::string tag;
is >> tag;
- std::string value;
- std::getline(is,value,'\n');
- ltrim(value); // remove leading space
- m_MapOfTag[tag] = value;
+ if (tag != "") {
+ std::string value;
+ std::getline(is,value,'\n');
+ ltrim(value); // remove leading space
+ m_MapOfTag[tag] = value;
+ }
}
+ is.close();
}
//--------------------------------------------------------------------
}
//--------------------------------------------------------------------
+
//--------------------------------------------------------------------
void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p)
{
// parse the string into 3 doubles
for(int i=0; i<3; i++) {
- p[i] = atof(results[i].c_str());
+
+ if (!clitk::fromString<double>(p[i], results[i].c_str())) {
+ clitkExceptionMacro("Error while reading Point3D, could not convert '"
+ << results[i].c_str() << "' into double.");
+ }
+
+ // p[i] = atof(results[i].c_str());
}
/*
}
//--------------------------------------------------------------------
+
+
void SetImageFilename(TagType tag, std::string f);
template<class ImageType>
typename ImageType::Pointer GetImage(TagType tag);
+ template<class ImageType>
+ void SetImage(TagType tag,
+ std::string f,
+ typename ImageType::Pointer image,
+ bool write=false);
+ template<class ImageType>
+ void ReleaseImage(TagType tag);
// Set Get Double
void SetDouble(TagType tag, double d);
protected:
std::string m_Filename;
+ typedef itk::ImageBase<3> ImageBaseType;
typedef std::map<TagType, std::string> MapTagType;
+ typedef std::map<TagType, ImageBaseType*> MapTagImageType;
MapTagType m_MapOfTag;
+ MapTagImageType m_MapOfImage;
}; // end class
//--------------------------------------------------------------------
GetImage(std::string tag)
{
if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
- clitkExceptionMacro("Could not find the tag <" << tag << "> of type Image Filename in the DB");
+ clitkExceptionMacro("Could not find the tag <" << tag << "> of type 'Image' in the DB ('"
+ << GetFilename() << "')");
}
else {
- std::string s = m_MapOfTag[tag];
- // Read the file
- typename ImageType::Pointer image = readImage<ImageType>(s);
+ typename ImageType::Pointer image;
+ if (m_MapOfImage[tag]) {
+ image = static_cast<ImageType *>(m_MapOfImage[tag]);
+ }
+ else {
+ std::string s = m_MapOfTag[tag];
+ // Read the file
+ image = readImage<ImageType>(s);
+ // I add a reference count because the cache is not a smartpointer
+ image->SetReferenceCount(image->GetReferenceCount()+1);
+ // Insert into the cache
+ m_MapOfImage[tag] = &(*image); // pointer
+ }
return image;
}
}
//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+void AnatomicalFeatureDatabase::
+SetImage(TagType tag, std::string f, typename ImageType::Pointer image, bool write)
+{
+ SetImageFilename(tag, f);
+ m_MapOfImage[tag] = &(*image);
+ if (write) {
+ writeImage<ImageType>(image, f);
+ }
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+void AnatomicalFeatureDatabase::
+ReleaseImage(std::string tag)
+{
+ if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
+ clitkExceptionMacro("Could not find the tag <" << tag << "> of type Image Filename in the DB");
+ }
+ else {
+ DD("TODO");
+ exit(0);
+ if (m_MapOfImage[tag]) {
+ DD(m_MapOfImage[tag]->GetReferenceCount());
+ ImageType * image = static_cast<ImageType*>(m_MapOfImage[tag]);
+ image->SetReferenceCount(image->GetReferenceCount()-1);
+ m_MapOfImage.erase(tag);
+ /*
+ DD(image->GetReferenceCount());
+ image->Delete();
+ */
+ // DD(image->GetReferenceCount());
+ }
+ else {
+ // Do nothing in this case (image not loaded)
+ }
+ }
+}
+//--------------------------------------------------------------------