Field3D
main.cpp
Go to the documentation of this file.
00001 //----------------------------------------------------------------------------//
00002 
00003 /*
00004  * Copyright (c) 2009 Sony Pictures Imageworks Inc.
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * Redistributions of source code must retain the above copyright
00013  * notice, this list of conditions and the following disclaimer.
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the
00017  * distribution.  Neither the name of Sony Pictures Imageworks nor the
00018  * names of its contributors may be used to endorse or promote
00019  * products derived from this software without specific prior written
00020  * permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
00026  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00028  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00031  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00033  * OF THE POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 
00036 //----------------------------------------------------------------------------//
00037 
00038 #include <iostream>
00039 #include <vector>
00040 #include <map>
00041 #include <string>
00042 
00043 #include <fnmatch.h>
00044 
00045 #include <boost/program_options.hpp>
00046 #include <boost/foreach.hpp>
00047 
00048 #include <Field3D/DenseField.h>
00049 #include <Field3D/MACField.h>
00050 #include <Field3D/SparseField.h>
00051 #include <Field3D/InitIO.h>
00052 #include <Field3D/Field3DFile.h>
00053 
00054 //----------------------------------------------------------------------------//
00055 
00056 using namespace std;
00057 using namespace Field3D;
00058 
00059 //----------------------------------------------------------------------------//
00060 // Options struct
00061 //----------------------------------------------------------------------------//
00062 
00063 struct Options {
00064   vector<string> inputFiles;
00065   vector<string> names;
00066   vector<string> attributes;
00067 };
00068 
00069 //----------------------------------------------------------------------------//
00070 // Function prototypes
00071 //----------------------------------------------------------------------------//
00072 
00074 Options parseOptions(int argc, char **argv);
00075 
00077 void printFileInfo(const std::string &filename, const Options &options);
00078 
00080 template <typename Data_T>
00081 void printFieldInfo(typename Field<Data_T>::Ptr field, const Options &options);
00082 
00084 template <typename T>
00085 void printMap(const map<string, T> m, const string &indent);
00086 
00088 void printMapping(FieldMapping::Ptr mapping);
00089 
00091 bool matchString(const std::string &str, const vector<string> &patterns);
00092 
00093 //----------------------------------------------------------------------------//
00094 // Function implementations
00095 //----------------------------------------------------------------------------//
00096 
00097 int main(int argc, char **argv)
00098 {
00099   Field3D::initIO();
00100 
00101   Options options = parseOptions(argc, argv);
00102 
00103   BOOST_FOREACH (const string &file, options.inputFiles) {
00104     printFileInfo(file, options);
00105   }
00106 }
00107 
00108 //----------------------------------------------------------------------------//
00109 
00110 Options parseOptions(int argc, char **argv)
00111 {
00112   namespace po = boost::program_options;
00113 
00114   Options options;
00115 
00116   po::options_description desc("Available options");
00117 
00118   desc.add_options()
00119     ("help", "Display help")
00120     ("input-file", po::value<vector<string> >(), "Input files")
00121     ("name,n", po::value<vector<string> >(), "Load field(s) by name")
00122     ("attribute,a", po::value<vector<string> >(), "Load field(s) by attribute")
00123     ;
00124   
00125   po::variables_map vm;
00126   po::store(po::parse_command_line(argc, argv, desc), vm);
00127   po::notify(vm);    
00128   
00129   po::positional_options_description p;
00130   p.add("input-file", -1);
00131   
00132   po::store(po::command_line_parser(argc, argv).
00133             options(desc).positional(p).run(), vm);
00134   po::notify(vm);
00135   
00136   if (vm.count("help")) {
00137     cout << desc << endl;
00138     exit(0);
00139   }
00140 
00141   if (vm.count("input-file"))
00142   {
00143     options.inputFiles = vm["input-file"].as<std::vector<std::string> >();
00144   }
00145   if (vm.count("name"))
00146   {
00147     options.names = vm["name"].as<std::vector<std::string> >();
00148   }
00149   if (vm.count("attribute"))
00150   {
00151     options.attributes = vm["attribute"].as<std::vector<std::string> >();
00152   }
00153 
00154   return options;
00155 }
00156 
00157 //----------------------------------------------------------------------------//
00158 
00159 template <typename T>
00160 void printMap(const map<string, T> m, const string &indent)
00161 {
00162   typedef pair<string, T> KeyValuePair;
00163 
00164   if (m.size() == 0) {
00165     cout << indent << "None" <<  endl;
00166   }
00167 
00168   BOOST_FOREACH(const KeyValuePair &i, m) {
00169     cout << indent << i.first << " : " << i.second << endl;
00170   }
00171 }
00172 
00173 //----------------------------------------------------------------------------//
00174 
00175 void printMapping(FieldMapping::Ptr mapping)
00176 {
00177   cout << "    Mapping:" << endl;
00178   cout << "      Type: " << mapping->className() << endl;
00179 
00180   // In the case of a MatrixFieldMapping, we print the local to world matrix.
00181 
00182   MatrixFieldMapping::Ptr matrixMapping = 
00183     boost::dynamic_pointer_cast<MatrixFieldMapping>(mapping);
00184 
00185   if (matrixMapping) {
00186     M44d m = matrixMapping->localToWorld();
00187     cout << "      Local to world transform:" << endl;
00188     for (int j = 0; j < 4; ++j) {
00189       cout << "        ";
00190       for (int i = 0; i < 4; ++i) {
00191         cout << m[i][j] << " ";
00192       }
00193       cout << endl;
00194     }
00195   }
00196 }
00197 
00198 //----------------------------------------------------------------------------//
00199 
00200 template <typename Data_T>
00201 void printFieldInfo(typename Field<Data_T>::Ptr field, const Options &options)
00202 {
00203   Box3i dataWindow = field->dataWindow();
00204   Box3i extents = field->extents();
00205 
00206   cout << "  Field: " << endl
00207        << "    Name:        " << field->name << endl
00208        << "    Attribute:   " << field->attribute << endl
00209        << "    Field type:  " << field->className() << endl
00210        << "    Data type:   " << field->dataTypeString() << endl
00211        << "    Extents:     " << extents.min << " " << extents.max << endl
00212        << "    Data window: " << dataWindow.min << " " << dataWindow.max << endl;
00213 
00214   printMapping(field->mapping());
00215 
00216   cout << "    Int metadata:" << endl;
00217   printMap(field->metadata().intMetadata(), "      ");
00218   cout << "    Float metadata:" << endl;
00219   printMap(field->metadata().floatMetadata(), "      ");
00220   cout << "    V3i metadata:" << endl;
00221   printMap(field->metadata().vecIntMetadata(), "      ");
00222   cout << "    V3f metadata:" << endl;
00223   printMap(field->metadata().vecFloatMetadata(), "      ");
00224   cout << "    String metadata:" << endl;
00225   printMap(field->metadata().strMetadata(), "      ");
00226 }
00227 
00228 //----------------------------------------------------------------------------//
00229 
00230 bool matchString(const std::string &str, const vector<string> &patterns)
00231 {
00232   // If patterns is empty all strings match
00233   if (patterns.size() == 0) {
00234     return true;
00235   }
00236   // Check all patterns
00237   BOOST_FOREACH (const string &pattern, patterns) {
00238     if (fnmatch(pattern.c_str(), str.c_str(), 0) != FNM_NOMATCH) {
00239       return true;
00240     }
00241   }
00242   // If no pattern matched return false
00243   return false;
00244 }
00245 
00246 //----------------------------------------------------------------------------//
00247 
00248 void printFileInfo(const std::string &filename, const Options &options)
00249 {
00250   typedef Field3D::half half;
00251 
00252   Field3DInputFile in;
00253 
00254   if (!in.open(filename)) {
00255     cout << "Error: Couldn't open f3d file: " << filename << endl;
00256     exit(1);
00257   }
00258 
00259   cout << "Field3D file: " << filename << endl;
00260 
00261   vector<string> partitions;
00262   in.getPartitionNames(partitions);
00263 
00264   BOOST_FOREACH (const string &partition, partitions) {
00265 
00266     if (!matchString(partition, options.names)) {
00267       continue;
00268     }
00269 
00270     vector<string> scalarLayers, vectorLayers;
00271     in.getScalarLayerNames(scalarLayers, partition);
00272     in.getVectorLayerNames(vectorLayers, partition);
00273 
00274     BOOST_FOREACH (const string &scalarLayer, scalarLayers) {
00275 
00276       if (!matchString(scalarLayer, options.attributes)) {
00277         continue;
00278       }  
00279 
00280       Field<half>::Vec hScalarFields = 
00281         in.readScalarLayers<half>(partition, scalarLayer);
00282       BOOST_FOREACH (Field<half>::Ptr field, hScalarFields) {
00283         printFieldInfo<half>(field, options);
00284       }
00285 
00286       Field<float>::Vec fScalarFields = 
00287         in.readScalarLayers<float>(partition, scalarLayer);
00288       BOOST_FOREACH (Field<float>::Ptr field, fScalarFields) {
00289         printFieldInfo<float>(field, options);
00290       }
00291 
00292       Field<double>::Vec dScalarFields = 
00293         in.readScalarLayers<double>(partition, scalarLayer);
00294       BOOST_FOREACH (Field<double>::Ptr field, dScalarFields) {
00295         printFieldInfo<double>(field, options);
00296       }
00297 
00298     }
00299 
00300     BOOST_FOREACH (const string &vectorLayer, vectorLayers) {
00301       
00302       if (!matchString(vectorLayer, options.attributes)) {
00303         continue;
00304       }  
00305 
00306       Field<V3h>::Vec hVectorFields = 
00307         in.readVectorLayers<half>(partition, vectorLayer);
00308       BOOST_FOREACH (Field<V3h>::Ptr field, hVectorFields) {
00309         printFieldInfo<V3h>(field, options);
00310       }
00311 
00312       Field<V3f>::Vec fVectorFields = 
00313         in.readVectorLayers<float>(partition, vectorLayer);
00314       BOOST_FOREACH (Field<V3f>::Ptr field, fVectorFields) {
00315         printFieldInfo<V3f>(field, options);
00316       }
00317 
00318       Field<V3d>::Vec dVectorFields = 
00319         in.readVectorLayers<double>(partition, vectorLayer);
00320       BOOST_FOREACH (Field<V3d>::Ptr field, dVectorFields) {
00321         printFieldInfo<V3d>(field, options);
00322       }
00323 
00324     }
00325   }
00326 
00327   cout << "  Global metadata" << endl;
00328 
00329   cout << "    Int metadata:" << endl;
00330   printMap(in.metadata().intMetadata(), "      ");
00331   cout << "    Float metadata:" << endl;
00332   printMap(in.metadata().floatMetadata(), "      ");
00333   cout << "    V3i metadata:" << endl;
00334   printMap(in.metadata().vecIntMetadata(), "      ");
00335   cout << "    V3f metadata:" << endl;
00336   printMap(in.metadata().vecFloatMetadata(), "      ");
00337   cout << "    String metadata:" << endl;
00338   printMap(in.metadata().strMetadata(), "      ");
00339 
00340 }
00341 
00342 //----------------------------------------------------------------------------//
00343