ParseObj.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 83 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-02-27 17:31:45 +0100 (Fr, 27. Feb 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS Core - IMPLEMENTATION
00049 //
00050 //=============================================================================
00051 
00052 
00053 //== INCLUDES =================================================================
00054 
00055 // -------------------- mview
00056 #include "Core.hh"
00057 // -------------------- ACG
00058 #include <ACG/Scenegraph/DrawModes.hh>
00059 
00060 #include <OpenFlipper/common/GlobalOptions.hh>
00061 #include <OpenFlipper/common/RecentFiles.hh>
00062 
00063 #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
00064 
00065 #include <QFile>
00066 
00067 #include <OpenFlipper/ACGHelper/DrawModeConverter.hh>
00068 
00069 //== IMPLEMENTATION ==========================================================
00070 
00071 
00072 void Core::openObjFile(QString _filename)
00073 {
00074   std::string fname = _filename.toStdString();
00075   std::fstream in(fname.c_str(), std::ios_base::in);
00076 
00077   if (!in)
00078   {
00079     emit log(LOGERR,tr("Cannot open to obj file") + _filename);
00080     return;
00081   }
00082 
00083   if ( OpenFlipper::Options::gui() )
00084   {
00085     coreWidget_->statusMessage( tr("Loading obj File ") + _filename + " ...");
00086     coreWidget_->setStatus(ApplicationStatus::BLOCKED );
00087   }
00088 
00089   OpenFlipper::Options::loadingSettings(true);
00090 
00091 
00092   while(!in.eof())
00093   {
00094     std::string keyword;
00095     in >> keyword;
00096 
00097     // Parse for external files to open
00098     if (keyword.compare("call") == 0)
00099     {
00100       // open external file
00101       std::string file;
00102       in >> file;
00103 
00104       QString path(file.c_str());
00105 
00106       //check if path is relative
00107       if (path.startsWith( "." + OpenFlipper::Options::dirSeparator() ))
00108       {
00109         //check if _filename contains a path
00110         if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != "")
00111         {
00112           path.remove(0,1); // remove .
00113           path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
00114         }
00115       }
00116 
00117       // preprocess file
00118       preprocessObjFile(path);
00119     }
00120 //     else
00121 //       std::cout << "ObjParser: unknown keyword " << keyword << std::endl;
00122 
00123   }
00124 
00125 
00126   OpenFlipper::Options::loadingSettings(false);
00127 
00128   // Reset scenegraph and recompute scene center containing all new objects
00129   resetScenegraph( true );
00130 
00131   if ( OpenFlipper::Options::gui() )
00132   {
00133     coreWidget_->statusMessage( tr("Loading obj File ") + _filename + tr(" ... done"), 4000);
00134     coreWidget_->setStatus(ApplicationStatus::READY );
00135   }
00136 
00137   in.close();
00138 
00139 }
00140 
00141 
00142 //-----------------------------------------------------------------------------
00143 
00144 
00145 void Core::preprocessObjFile(QString _filename)
00146 {
00147   bool newActiveObject = false;
00148 
00149   std::cout << "[preprocessObjFile] filename: " << _filename.toStdString() << std::endl;
00150 
00151   QString path = _filename;
00152 
00153   //check if path is relative
00154   if (path.startsWith( "." + OpenFlipper::Options::dirSeparator() ))
00155   {
00156     //check if _filename contains a path
00157     if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != "")
00158     {
00159       path.remove(0,1); // remove .
00160       path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
00161     }
00162   }
00163 
00164 
00165   // find out data type
00166   DataType dataType = DATA_UNKNOWN;
00167 
00168   std::string fname = _filename.toStdString();
00169   std::fstream in(fname.c_str(), std::ios_base::in);
00170   if (!in)
00171   {
00172     emit log(LOGERR,tr("Cannot open to obj file") + _filename);
00173     return;
00174   }
00175 
00176 
00177   std::string token;
00178   std::string tmp;
00179 
00180   bool typeFound = false;
00181   bool meshFound = false;
00182 
00183   while ( (!typeFound) && (!in.eof()) )
00184   {
00185     // read next token
00186     in >> token;
00187 
00188     if (token == "v") // ignore vertices, since they occur in many objects
00189       continue;
00190 
00191     else if (token == "f")
00192     {
00193       // we have found a mesh. now, find out if its a poly or a triangle mesh
00194       bool nextLine = false;
00195       int numVerticesPerFace = 0;
00196 
00197       meshFound = true;
00198 
00199       while((!nextLine) && (!in.eof()))
00200       {
00201         in >> tmp;
00202         if (tmp == "f")  nextLine = true;
00203         else ++numVerticesPerFace;
00204       }
00205 
00206       if (numVerticesPerFace > 3)
00207       {
00208         dataType = DATA_POLY_MESH;
00209         typeFound = true;
00210       }
00211     }
00212 
00213     // all faces processed and no poly mesh found
00214 //     else if ((meshFound) && (typeFound == false))
00215 //     {
00216 //       dataType = DATA_TRIANGLE_MESH;
00217 //       typeFound = true;
00218 //     }
00219 
00220     // curve/surface type
00221     else if (token == "cstype")
00222     {
00223       bool nextLine = false;
00224       in >> tmp;
00225       if (tmp == "bspline")
00226       {
00227         // find out if it is a curve or a surface
00228         while((!nextLine) && (!in.eof()))
00229         {
00230           in >> tmp;
00231           if (tmp == "curv")
00232           {
00233             nextLine = true;
00234             dataType = typeId("BSplineCurve");
00235             typeFound = true;
00236           }
00237           else if (tmp == "surf")
00238           {
00239             nextLine = true;
00240             dataType = typeId("BSplineSurface");
00241             typeFound = true;
00242           }
00243         }
00244 
00245       }
00246     }
00247 
00248   }
00249 
00250   if (!typeFound)
00251   {
00252     dataType = DATA_TRIANGLE_MESH;
00253     emit log(LOGWARN, tr("Unable to get DataType for object ") +  _filename + tr(" - assuming Triangle Mesh") );
00254   }
00255 
00256 
00257   if (dataType == DATA_POLY_MESH)
00258     std::cout << _filename.toStdString() << " is of type DATA_POLY_MESH" << std::endl;
00259   else if (dataType == DATA_TRIANGLE_MESH)
00260     std::cout << _filename.toStdString() << " is of type DATA_TRIANGLE_MESH" << std::endl;
00261   else if  (dataType == typeId("BSplineCurve"))
00262     std::cout << _filename.toStdString() << " is of type DATA_BSPLINE_CURVE" << std::endl;
00263   else if  (dataType == typeId("BSplineSurface"))
00264     std::cout << _filename.toStdString() << " is of type DATA_BSPLINE_SURFACE" << std::endl;
00265   else
00266     std::cout << "no data type found " << std::endl;
00267 
00268 
00269   int newObjectId = loadObject(dataType, path);
00270 
00271   BaseObject* object = objectRoot_->childExists( newObjectId );
00272   if ( object == 0 )
00273     emit log(LOGERR,tr("Unable to open Object ") + path);
00274 
00275   if ( newActiveObject )
00276     emit objectSelectionChanged(-1);
00277 }
00278 
00279 //-----------------------------------------------------------------------------
00280 
00281 
00282 void Core::writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly)
00283 {
00284   // open file
00285   std::string fname = _filename.toStdString();
00286   std::fstream out(fname.c_str(), std::ios_base::out);
00287   if (!out)
00288   {
00289     emit log(LOGERR,tr("Cannot open to obj file") + _filename);
00290     return;
00291   }
00292 
00293 
00294   if ( OpenFlipper::Options::gui() )
00295   {
00296     coreWidget_->statusMessage( tr("Saving obj File ") + _filename + " ...");
00297     coreWidget_->setStatus(ApplicationStatus::BLOCKED );
00298   }
00299 
00300   PluginFunctions::IteratorRestriction restriction;
00301   if ( _targetOnly )
00302     restriction = PluginFunctions::TARGET_OBJECTS;
00303   else
00304     restriction = PluginFunctions::ALL_OBJECTS;
00305 
00306   // write all objects to a separate obj file and save external references in the global obj file
00307   for ( PluginFunctions::ObjectIterator o_it (restriction) ;
00308                                         o_it != PluginFunctions::objectsEnd(); ++o_it)
00309   {
00310     QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
00311 
00312     if (QFile(file).exists())
00313     {
00314       // Modify filename if relativePaths are wanted
00315       if (_relativePaths)
00316       {
00317         int prefixLen = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2).length();
00318         file.remove(0, prefixLen);
00319         file = "." + file;
00320       }
00321 
00322       // save to global inifile
00323       out << "call " << file.toStdString() << "\n";
00324     }
00325   }
00326 
00327   out.close();
00328 
00329   if ( OpenFlipper::Options::gui() )
00330   {
00331     coreWidget_->statusMessage( tr("Saving obj File ") + _filename + tr(" ... done"), 4000);
00332     coreWidget_->setStatus(ApplicationStatus::READY );
00333   }
00334 
00335 }
00336 
00337 
00338 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .