PLearn 0.1
|
00001 #include "Molecule.h" 00002 #include <plearn/db/getDataSet.h> 00003 #include <plearn/vmat/VMat.h> 00004 00005 using namespace std ; 00006 namespace PLearn { 00007 00008 00009 Molecule::Molecule(){} 00010 Molecule::Molecule(Mat _chem, Mat _geom, string vrml_file) { 00011 chem.resize(_chem.length(), _chem.width() ) ; 00012 chem << _chem ; 00013 00014 geom.resize(_geom.length(), _geom.width() ) ; 00015 geom << _geom ; 00016 this->vrml_file = vrml_file ; 00017 00018 } 00019 00020 PLEARN_IMPLEMENT_OBJECT(Molecule, 00021 "A Molecule", 00022 "" 00023 ); 00024 00025 void Molecule::declareOptions(OptionList& ol) 00026 { 00027 declareOption(ol, "chem", &Molecule::chem, OptionBase::buildoption, 00028 "Chemical Properties"); 00029 00030 declareOption(ol, "geom", &Molecule::geom, 00031 OptionBase::buildoption, 00032 "Geom Properties"); 00033 00034 declareOption(ol, "vrml_file", &Molecule::vrml_file, 00035 OptionBase::buildoption, 00036 "The vrml filename"); 00037 00038 // Now call the parent class' declareOptions 00039 inherited::declareOptions(ol); 00040 } 00041 00042 void Molecule::build_() 00043 {} 00044 00045 void Molecule::build() 00046 { 00047 inherited::build(); 00048 build_(); 00049 } 00050 00051 void Molecule::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00052 { 00053 inherited::makeDeepCopyFromShallowCopy(copies); 00054 00055 deepCopyField(chem, copies); 00056 deepCopyField(geom, copies); 00057 00058 } 00059 00060 string Molecule::getVrmlType(const string& name) { 00061 string name2 = name + ".vrml"; 00062 ifstream os(name2.c_str()); 00063 string temp,s; 00064 s=""; 00065 while(os>>temp){ 00066 s=s+temp; 00067 } 00068 string::size_type a = s.find("Line"); 00069 if (a!= string::npos) return string("line"); 00070 a = s.find("Face"); 00071 if (a!=string::npos) return string("face"); 00072 PLERROR("getType : Invalid VRML file"); 00073 return ""; 00074 } 00075 00076 void Molecule::readVrml(const string& name,SurfMesh& sm){ 00077 string type = getVrmlType(name); 00078 if(type=="line"){ 00079 sm->readVRMLIndexedLineSet(name+".vrml"); 00080 }else if(type=="face"){ 00081 sm->readVRMLIndexedFaceSet(name+".vrml"); 00082 } 00083 } 00084 void Molecule::writeVrml(const string& name,const string& to,SurfMesh& sm){ 00085 string type = getVrmlType(name); 00086 if(type=="line"){ 00087 sm->writeVRMLIndexedLineSet(to+".vrml"); 00088 }else if(type=="face"){ 00089 sm->writeVRMLIndexedFaceSet(to+".vrml"); 00090 } 00091 00092 } 00093 void Molecule::getVrmlVertexCoords(const string& name,Mat& xmat){ 00094 SurfMesh xmesh = new SurfaceMesh(); 00095 readVrml(name,xmesh); 00096 xmat = xmesh->getVertexCoords(); 00097 } 00098 00099 //append to storage the molecules from file 1 00100 PMolecule Molecule::readMolecule(const string & file){ 00101 00102 00103 Vec column_indices(5) ; 00104 for(int i=0 ; i<5 ; ++i) column_indices[i] = i ; 00105 00106 00107 Mat chem, geom ; 00108 VMat t = getDataSet(file + ".amat") ; 00109 Mat full_chem = t.toMat() ; 00110 chem.resize(full_chem.length() ,5 ) ; 00111 selectColumns(full_chem , column_indices , chem) ; 00112 normalize(chem) ; 00113 Molecule::getVrmlVertexCoords(file,geom); 00114 // Molecule m(chem,geom) ; 00115 00116 PMolecule pm = new Molecule(chem , geom ,file+".vrml" ) ; 00117 return pm ; 00118 00119 } 00120 void Molecule::readMolecules(const string & fileName, vector<PMolecule> & storage ) { 00121 00122 ifstream f(fileName.c_str()); 00123 string file; 00124 00125 00126 while(f>>file){ 00127 // load(file+"SurfacePrpi.mat",chem); 00128 storage.push_back(readMolecule(file)) ; 00129 } 00130 00131 } 00132 00133 } 00134