PLearn 0.1
SurfaceTemplate/Molecule.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Molecule.cc
00004 //
00005 // Copyright (C) 2006 Pascal Lamblin
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Pascal Lamblin
00036 
00040 #include "Molecule.h"
00041 #include <plearn/io/fileutils.h>
00042 #include <plearn/io/openFile.h>
00043 #include <plearn/db/getDataSet.h>
00044 #include <plearn/vmat/VMat.h>
00045 #include <plearn/vmat/AutoVMatrix.h>
00046 #include <plearn/vmat/MemoryVMatrix.h>
00047 
00048 namespace PLearn {
00049 using namespace std;
00050 
00051 PLEARN_IMPLEMENT_OBJECT(
00052     Molecule,
00053     "ONE LINE USER DESCRIPTION",
00054     "MULTI LINE\nHELP FOR USERS"
00055     );
00056 
00057 Molecule::Molecule()
00058 {
00059 }
00060 
00061 Molecule::Molecule( const PPath& filename )
00062 {
00063     readFromFile( filename );
00064     build();
00065 }
00066 
00067 void Molecule::readFromFile( const PPath& filename )
00068 {
00069     readFromVRMLFile( filename + ".vrml" );
00070     readFromAMATFile( filename + ".amat" );
00071 }
00072 
00073 void Molecule::writeToFile( const PPath& filename )
00074 {
00075     writeToVRMLFile( filename + ".vrml" );
00076     writeToAMATFile( filename + ".amat" );
00077 }
00078 
00079 
00080 void Molecule::build()
00081 {
00082     inherited::build();
00083     build_();
00084 }
00085 
00086 void Molecule::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00087 {
00088     inherited::makeDeepCopyFromShallowCopy(copies);
00089 
00090     // deepCopyField(trainvec, copies);
00091 
00092     deepCopyField( coordinates, copies );
00093     deepCopyField( features, copies );
00094     deepCopyField( feature_names, copies );
00095     deepCopyField( vrml_face_set, copies );
00096     deepCopyField( vrml_line_set, copies );
00097 }
00098 
00099 void Molecule::readFromVRMLFile( const PPath& filename )
00100 {
00101     // Read the geometrical informations, contained in a VRML file
00102     string vrml = loadFileAsString( filename );
00103 
00104     // Read the coordinates, and store them in coordinates
00105     size_t begin;
00106     size_t end;
00107     begin = vrml.find( "Coordinate3" );
00108     if( begin != string::npos )
00109     {
00110         begin = vrml.find( "[", begin );
00111         end = vrml.find( "]", begin );
00112 
00113         string coordinate3 = vrml.substr( begin, end - begin + 1 );
00114         PStream coords = openString( coordinate3, PStream::plearn_ascii );
00115         Vec coordinates_;
00116         coords >> coordinates_;
00117         coordinates = coordinates_->toMat( coordinates_->length() / 3, 3 );
00118     }
00119     else
00120         PLERROR("Molecule::readFromFile - File %s.vrml should contain a"
00121                 " 'Coordinate3' block.\n", filename.c_str());
00122 
00123     // Read the other geometrical informations (edges or faces)
00124     // Search for edges informations
00125     begin = vrml.find( "IndexedFaceSet" );
00126     if( begin != string::npos )
00127     {
00128         begin = vrml.find( "[", begin );
00129         end = vrml.find( "]", begin );
00130 
00131         // store them in vrml_face_set
00132         string indexedfaceset = vrml.substr( begin, end - begin + 1 );
00133         PStream face_indices = openString( indexedfaceset,
00134                                            PStream::plearn_ascii );
00135         face_indices >> vrml_face_set;
00136     }
00137 
00138     // Search for edges informations
00139     begin = vrml.find( "IndexedLineSet" );
00140     if( begin != string::npos )
00141     {
00142         begin = vrml.find( "[", begin );
00143         end = vrml.find( "]", begin );
00144 
00145         // store them in vrml_line_set
00146         string indexedlineset = vrml.substr( begin, end - begin + 1 );
00147         PStream line_indices = openString( indexedlineset,
00148                                            PStream::plearn_ascii );
00149         line_indices >> vrml_line_set;
00150     }
00151 }
00152 
00153 void Molecule::readFromAMATFile( const PPath& filename )
00154 {
00155     // Read the chemical informations, contained in an AMAT file
00156     VMat features_ = getDataSet( filename );
00157     features = features_->toMat();
00158     features.compact(); // for use as Var storage
00159 
00160     feature_names = features_->fieldNames();
00161 }
00162 
00163 void Molecule::writeToVRMLFile( const PPath& filename )
00164 {
00165     PStream vrml = openFile( filename, PStream::raw_ascii, "w" );
00166 
00167     // writes VRML header and beginning of file
00168     vrml<< "#VRML V1.0 ascii" << endl
00169         << endl
00170         << "Separator {" << endl
00171         << "    Material {" << endl
00172         << "        diffuseColor [ 1 1 1 ]" << endl
00173         << "    }" << endl
00174         << endl;
00175 
00176     // writes coordinates
00177     vrml<< "    Coordinate3 {" << endl
00178         << "        point [" << endl;
00179 
00180     for( int i=0 ; i<coordinates.length() ; i++ )
00181     {
00182         vrml<< "            ";
00183         for( int j=0 ; j<3 ; j++ )
00184             vrml<< coordinates(i,j) << " ";
00185 
00186         vrml<< "," << endl;
00187     }
00188 
00189     vrml<< "        ]" << endl
00190         << "    }" << endl
00191         << endl;
00192 
00193     // writes FaceSet (if any)
00194     int faceset_size = vrml_face_set.size();
00195     if( faceset_size > 0 )
00196     {
00197         vrml<< "    IndexedFaceSet {" << endl
00198             << "        coordIndex [" << endl
00199             << "            " ;
00200         for( int i=0 ; i<faceset_size ; i++ )
00201         {
00202             int index = vrml_face_set[i];
00203             if( index < 0 )
00204                 vrml<< "-1," << endl
00205                     << "            ";
00206             else
00207                 vrml<< index << ", ";
00208         }
00209         vrml<< "]" << endl
00210             << "    }" << endl
00211             << endl;
00212     }
00213 
00214     // writes LineSet (if any)
00215     int lineset_size = vrml_line_set.size();
00216     if( lineset_size > 0 )
00217     {
00218         vrml<< "    IndexedLineSet {" << endl
00219             << "        coordIndex [" << endl
00220             << "            " ;
00221         for( int i=0 ; i<lineset_size ; i++ )
00222         {
00223             int index = vrml_line_set[i];
00224             if( index < 0 )
00225                 vrml<< "-1," << endl
00226                     << "            ";
00227             else
00228                 vrml<< index << ", ";
00229         }
00230         vrml<< "]" << endl
00231             << "    }" << endl
00232             << endl;
00233     }
00234 
00235     // end of the file
00236     vrml<< "}" << endl;
00237 }
00238 
00239 void Molecule::writeToAMATFile( const PPath& filename )
00240 {
00241     VMat features_ = new MemoryVMatrix( features );
00242     features_->declareFieldNames( feature_names );
00243     features_->saveAMAT( filename, false );
00244 }
00245 
00246 void Molecule::declareOptions(OptionList& ol)
00247 {
00248     // ### ex:
00249     // declareOption(ol, "myoption", &Molecule::myoption,
00250     //               OptionBase::buildoption,
00251     //               "Help text describing this option");
00252     // ...
00253 
00254     declareOption(ol, "coordinates", &Molecule::coordinates,
00255                   OptionBase::buildoption,
00256                   "Mat containing the 3D coordinates of the surface points.");
00257 
00258     declareOption(ol, "features", &Molecule::features,
00259                   OptionBase::buildoption,
00260                   "Mat containing the values of the chemical features at"
00261                   " each point.");
00262 
00263     declareOption(ol, "feature_names", &Molecule::feature_names,
00264                   OptionBase::buildoption,
00265                   "Name of the chemical features stored in 'features'.");
00266 
00267     declareOption(ol, "vrml_face_set", &Molecule::vrml_face_set,
00268                   OptionBase::learntoption,
00269                   "List of point indices, used to define faces in VRML.");
00270 
00271     declareOption(ol, "vrml_line_set", &Molecule::vrml_line_set,
00272                   OptionBase::learntoption,
00273                   "List of point indices, used to define lines in VRML.");
00274 
00275     // Now call the parent class' declareOptions
00276     inherited::declareOptions(ol);
00277 }
00278 
00279 void Molecule::build_()
00280 {
00281     // ### This method should do the real building of the object,
00282     // ### according to set 'options', in *any* situation.
00283     // ### Typical situations include:
00284     // ###  - Initial building of an object from a few user-specified options
00285     // ###  - Building of a "reloaded" object: i.e. from the complete set of
00286     // ###    all serialised options.
00287     // ###  - Updating or "re-building" of an object after a few "tuning"
00288     // ###    options have been modified.
00289     // ### You should assume that the parent class' build_() has already been
00290     // ### called.
00291 
00292     // check consistency of the sizes
00293     int features_length = features.length();
00294 
00295     if( n_features() > 0 && features_length != n_points() )
00296         PLERROR("In Molecule::build_ - features.length() should be equal to\n"
00297                 "coordinates.length(), unless features is empty (%d != %d).\n",
00298                 features_length, n_points() );
00299 
00300 }
00301 
00302 
00303 } // end of namespace PLearn
00304 
00305 
00306 /*
00307   Local Variables:
00308   mode:c++
00309   c-basic-offset:4
00310   c-file-style:"stroustrup"
00311   c-file-offsets:((innamespace . 0)(inline-open . 0))
00312   indent-tabs-mode:nil
00313   fill-column:79
00314   End:
00315 */
00316 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines