00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00021
00022
00023
00024
00025 #include<ImLib3D/SparseImage3D.hpp>
00026 #include<ImLib3D/XMLTools.hpp>
00027
00028
00029
00030 vector<const SparseStructure3D *> SparseStructure3D::allreadyExisting;
00031 void
00032 SparseStructure3D::Read( ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node)
00033 {
00034 node=CreateReadNode("SparseStructure3D",file,parentNode,node);
00035 mask.Read(file,node);
00036 }
00037 void
00038 SparseStructure3D::Write(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node) const
00039 {
00040 node=CreateWriteNode("SparseStructure3D",file,parentNode,node);
00041 mask.Write(file,node);
00042 }
00043
00044
00045 bool
00046 SparseStructure3D::Dereference(SparseImage3DBase *image) const
00047 {
00048 if(find(referencingImages.begin(),referencingImages.end(),image)==referencingImages.end())
00049 {
00050 ThrowError("SparseStructure3D::Dereference atempting to dereference unreferenced image");
00051 }
00052 referencingImages.erase(find(referencingImages.begin(),referencingImages.end(),image));
00053 if(autoDestroy && referencingImages.size()==0)
00054 {
00055 printf("SparseStructure3D::Dereference refCount reached zero, deleting %x.\n",(uint)this);
00056 suicideInProgress=true;
00057 delete this;return true;
00058 }
00059 return false;
00060 }
00061
00062 const SparseStructure3D *
00063 SparseStructure3D::Find(string sname)
00064 {
00065 for(uint i=0;i<allreadyExisting.size();i++)
00066 {
00067 if(allreadyExisting[i]->name==sname)
00068 {
00069 printf("SparseStructure3D::Find found \"%s\" in allreadyExisting (%x)\n",
00070 sname.c_str(),(uint)allreadyExisting[i]);
00071 return allreadyExisting[i];
00072 }
00073 }
00074 printf("SparseStructure3D::CreateShared did not find \"%s\" in allreadyExisting\n",sname.c_str());
00075 return NULL;
00076 }
00077
00078
00079
00080 SparseStructure3D:: ~SparseStructure3D()
00081 {
00082 if(autoDestroy && !suicideInProgress)
00083 {
00084 ThrowError("SparseStructure3D::~SparseStructure3D: trying to kill a SparseStructure3D that manages its own destruction");
00085 }
00086 allreadyExisting.erase(find(allreadyExisting.begin(),allreadyExisting.end(),this));
00087 if(referencingImages.size()>0)
00088 {
00089 printf("SparseStructure3D::~SparseStructure3D deleting %x with refCount==%d. I will invalidate all referencingImages\n",(uint)this,referencingImages.size());
00090 for(uint i=0;i<referencingImages.size();i++)
00091 {
00092 referencingImages[i]->Invalidate();
00093 }
00094 }
00095 }
00096
00097 SparseStructure3D *SparseStructure3D::Create(const string &name)
00098 {
00099 SparseStructure3D *res=new SparseStructure3D();
00100 res->ReadFromFile(name);
00101 res->ComputeValSize();
00102 return res;
00103 }
00104 SparseStructure3D *SparseStructure3D::Create(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node)
00105 {
00106 SparseStructure3D *res=new SparseStructure3D();
00107 res->Read(file,parentNode,node);
00108 res->ComputeValSize();
00109 return res;
00110 }
00111 SparseStructure3D *SparseStructure3D::Create(const BitMask3D &_mask,bool _autoDestroy)
00112 {
00113 return new SparseStructure3D(_mask,_autoDestroy);
00114 }
00115
00116 SparseStructure3D::SparseStructure3D():
00117 size(-1),
00118 autoDestroy(false),
00119 suicideInProgress(false),
00120 valNbToPosOk(false),posToValNbOk(false)
00121 {
00122 allreadyExisting.push_back(this);
00123 }
00124 SparseStructure3D::SparseStructure3D(BitMask3D _mask,bool _autoDestroy,int _size) :
00125 size(_size),mask(_mask),
00126 autoDestroy(_autoDestroy),
00127 suicideInProgress(false),
00128 valNbToPosOk(false),posToValNbOk(false)
00129 {
00130 allreadyExisting.push_back(this);
00131
00132 if(size<0){ComputeValSize();}
00133 }