Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Properties.cpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00022 #include<ImLib3D/Image3D.hpp>
00023 #include<ImLib3D/Properties.hpp>
00024 
00025 
00026 void 
00027 Properties::AddMask(const Size3D &size)
00028 {
00029     if(!mask){mask=new Mask3D(size);mask->Fill(1);}
00030 }
00031 bool 
00032 ImageProperty::IsXML()
00033 {
00034     return dynamic_cast<PropertyXML *>(this)!=NULL;
00035 }
00036 
00037 
00039 void 
00040 Properties::ClearProperties()
00041 {
00042     if(mask)   {delete mask;mask=NULL;}
00043     if(iCenter){delete iCenter;iCenter=NULL;}
00044     for(map<string,Prop *>::iterator p=properties.begin();p!=properties.end();++p)
00045     {
00046         delete (*p).second;
00047     }
00048     properties.clear();
00049 }
00050 void 
00051 Properties::Remove(const string &name)
00052 {
00053     if(name=="Mask" && mask){delete mask;mask=NULL;}
00054     else
00055     if(name=="iCenter" && iCenter){delete iCenter;iCenter=NULL;}
00056     else
00057     {properties.erase(name);}
00058 }
00059 
00060 void 
00061 Properties::operator=(const Properties &other)
00062 {
00063     ClearProperties();
00064     if(other.mask   )   {mask   =new Mask3D(*other.mask);}
00065     if(other.iCenter)   {iCenter=new Vect3Di(*other.iCenter);}
00066     for(map<string,Prop *>::const_iterator p=other.properties.begin();p!=other.properties.end();++p)
00067     {
00068         properties[((*p).second)->name]=((*p).second)->Duplicate();
00069     }
00070 
00071 }
00072 
00073 bool
00074 Properties::operator==(const Properties &other) const
00075 {
00076     if( ((mask   ==NULL)   !=(other.mask   ==NULL))    ||  (mask     && !(*mask       == *other.mask    ))){return false;}
00077     if( ((iCenter==NULL)   !=(other.iCenter==NULL))    ||  (iCenter  && !(*iCenter    == *other.iCenter ))){return false;}
00078     if(properties.size()!=other.properties.size()){return false;}
00079     for(map<string,Prop *>::const_iterator p=other.properties.begin();p!=other.properties.end();++p)
00080     {
00081         Prop *oprop=(*p).second;
00082         if(properties.find(oprop->name)==properties.end()){return false;}
00083         if(!((*properties[oprop->name])==(*oprop))){return false;}
00084     }
00085     return true;
00086 }
00087 
00088 void 
00089 Properties::Read( ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node)
00090 {
00091     node=CreateReadNode("Properties",file,parentNode,node);
00092 
00093 //      {
00094 //          xmlpp::Element::NodeList nodeList=imageNode->get_children("Properties");
00095 //          int n=nodeList.size();
00096 //          if(!n){return;}// no properties
00097 //          if(n>1){ThrowError("Properties::Read found more than one Properties node:");}
00098 //          propNode=*nodeList.begin();
00099 //      }
00100 
00101     vector<xmlpp::Element *>  propNodes=ChildElements(node);
00102     for(vector<xmlpp::Element *>::iterator p=propNodes.begin();p!=propNodes.end();++p)
00103     {
00104         xmlpp::Element::NodeList nodeList;
00105         xmlpp::Element *snode=*p;
00106         string name=(*p)->get_name();
00107         if(name=="Mask3D")
00108         {
00109             mask=new Mask3D();
00110             if((nodeList=snode->get_children("ImLib3DImage")).size()<1)
00111             {ThrowError("Properties::Read: no ImLib3DImage node in Mask3D node");}
00112             mask->Read(file,snode);//*nodeList.begin());
00113         }
00114         else
00115         if(name=="iCenter")
00116         {
00117             iCenter=new Vect3Di(
00118                 LongInt(snode->get_attribute("x" )->get_value())  ,
00119                 LongInt(snode->get_attribute("y" )->get_value())  ,
00120                 LongInt(snode->get_attribute("z" )->get_value())   );
00121         }
00122         else
00123         {
00124 //              printf("reading into PropertyXML:%s\n",name.c_str());
00125             PropXML *propxml=new PropXML(name);
00126             propxml->Read(file,NULL,snode);
00127             properties[name]=propxml;
00128         }
00129     }
00130     // look for unparsed xml properties,  that can be automatically parsed
00131     vector<PropXML *> parseable;
00132     for(map<string,Prop *>::iterator p=properties.begin();p!=properties.end();++p)
00133     {
00134         if(!((*p).second)->IsXML()){continue;}
00135         PropXML *propxml=(PropXML *)((*p).second);
00136         if(propxml->RootElement()->get_attribute("PropertyType"))
00137         {
00138             if(propxml->RootElement()->get_attribute("PropertyType")->get_value()=="PropertyWrapPtr")
00139             {
00140                 parseable.push_back(propxml);
00141             }
00142         }
00143     }
00144     // now parse them
00145     for(uint i=0;i<parseable.size();i++)
00146     {
00147         string name=parseable[i]->name;
00148         string type=parseable[i]->RootElement()->get_attribute("type")->get_value();
00149         if(type=="Vect3Df"             ){DefaultParse<Vect3Df>(name);}
00150         if(type=="Vect3D<float, float>"){DefaultParse<Vect3Df>(name);}
00151         if(type=="Vect3Di"             ){DefaultParse<Vect3Di>(name);}
00152         if(type=="Vect3D<int, float>"  ){DefaultParse<Vect3Di>(name);}
00153         if(type=="RectZone3Di"         ){DefaultParse<RectZone3Di>(name);}
00154         if(type=="RectZone3Df"         ){DefaultParse<RectZone3Df>(name);}
00155         if(type=="int"    ){DefaultParse<int    >(name);}
00156         if(type=="float"  ){DefaultParse<float  >(name);}
00157         if(type=="double" ){DefaultParse<double >(name);}
00158         if(type=="byte"   ){DefaultParse<byte   >(name);}
00159         if(type=="string" ){DefaultParse<string >(name);}
00160     }
00161 }
00162 
00163 void 
00164 Properties::Write(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node) const
00165 {
00166     node=CreateWriteNode("Properties",file,parentNode,node);
00167 
00168     for(map<string,Prop *>::iterator p=properties.begin();p!=properties.end();++p)
00169     {
00170         Prop *prop=(*p).second;
00171         printf("Writing Property:\"%s\" (is xml:%d)\n",prop->name.c_str(),prop->IsXML());
00172         prop->Write(file,node);
00173     }
00174     if(mask   )  
00175     {
00176         xmlpp::Element *maskNode=node->add_child("Mask3D");
00177         mask->Write(file,maskNode);
00178     }
00179     if(iCenter)
00180     {
00181         xmlpp::Element *cnode=node->add_child("iCenter");
00182         cnode->set_attribute("x",SPrintf("%d",iCenter->x));
00183         cnode->set_attribute("y",SPrintf("%d",iCenter->y));
00184         cnode->set_attribute("z",SPrintf("%d",iCenter->z));
00185     }
00186 
00187 }
00188 
00189 
00190 

Generated on Fri Jun 17 13:36:06 2005 for ImLib3D by  doxygen 1.4.2