stl_to_mesh.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <OGRE/OgreRoot.h>
00031 #include <OGRE/OgreLogManager.h>
00032 #include <OGRE/OgreMaterialManager.h>
00033 #include <OGRE/OgreSkeletonManager.h>
00034 #include <OGRE/OgreMeshSerializer.h>
00035 #include <OGRE/OgreMeshManager.h>
00036 #include <OGRE/OgreResourceGroupManager.h>
00037 #include <OGRE/OgreMath.h>
00038 #include <OGRE/OgreDefaultHardwareBufferManager.h>
00039 #include <OGRE/OgreManualObject.h>
00040 
00041 #include "ogre_tools/stl_loader.h"
00042 
00043 #include <boost/filesystem.hpp>
00044 
00045 #include <ros/console.h>
00046 
00061 using namespace Ogre;
00062 using namespace ogre_tools;
00063 namespace fs=boost::filesystem;
00064 
00065 
00066 int main( int argc, char** argv )
00067 {
00068   if ( argc < 3 )
00069   {
00070     ROS_INFO( "Usage: stl_to_mesh <stl files> <output directory>" );
00071     ROS_INFO( "or     stl_to_mesh <stl file> <output file>" );
00072 
00073     return 0;
00074   }
00075 
00076   typedef std::vector<std::string> V_string;
00077   V_string inputFiles;
00078   V_string outputFiles;
00079   std::string outputDirectory = argv[ argc - 1 ];
00080   if ( outputDirectory.rfind( ".mesh" ) != std::string::npos )
00081   {
00082     ROS_INFO( "Converting single mesh: %s to %s", argv[1], outputDirectory.c_str() );
00083     inputFiles.push_back( argv[ 1 ] );
00084     outputFiles.push_back( outputDirectory );
00085   }
00086   else
00087   {
00088     ROS_INFO( "Converting multiple meshes, into output directory %s...", outputDirectory.c_str() );
00089 
00090     for ( int i = 1; i < argc - 1; ++i )
00091     {
00092       std::string inputFile = argv[ i ];
00093       inputFiles.push_back( inputFile );
00094 
00095       fs::path p(inputFile);
00096       if (!(p.extension() == ".stl" || p.extension() == ".STL" || p.extension() == ".stlb" || p.extension() == ".STLB"))
00097       {
00098         ROS_ERROR( "Input file %s is not a .stl or .STL file!", inputFile.c_str() );
00099         exit(1);
00100       }
00101 
00102       p = p.replace_extension("mesh");
00103 
00104 #if BOOST_FILESYSTEM_VERSION==3
00105       std::string outputFile = outputDirectory + "/" + p.filename().string();
00106 #else
00107       std::string outputFile = outputDirectory + "/" + p.filename();
00108 #endif
00109 
00110       outputFiles.push_back( outputFile );
00111     }
00112   }
00113 
00114   // NB some of these are not directly used, but are required to
00115   //   instantiate the singletons used in the dlls
00116   LogManager* logMgr = 0;
00117 #if OGRE_VERSION_MAJOR >= 1 && OGRE_VERSION_MINOR >= 7
00118   LodStrategyManager* lodMgr = 0;
00119 #endif
00120   Math* mth = 0;
00121   MaterialManager* matMgr = 0;
00122   SkeletonManager* skelMgr = 0;
00123   MeshSerializer* meshSerializer = 0;
00124   DefaultHardwareBufferManager *bufferManager = 0;
00125   MeshManager* meshMgr = 0;
00126   ResourceGroupManager* rgm = 0;
00127 
00128   try
00129   {
00130     logMgr = new LogManager();
00131     logMgr->createLog( "STLToMesh_Ogre.log", false, false, true );
00132     rgm = new ResourceGroupManager();
00133     mth = new Math();
00134 #if OGRE_VERSION_MAJOR >= 1 && OGRE_VERSION_MINOR >= 7
00135     lodMgr = new LodStrategyManager();
00136 #endif
00137     meshMgr = new MeshManager();
00138     matMgr = new MaterialManager();
00139     matMgr->initialise();
00140     skelMgr = new SkeletonManager();
00141     meshSerializer = new MeshSerializer();
00142     bufferManager = new DefaultHardwareBufferManager(); // needed because we don't have a rendersystem
00143 
00144     for ( size_t i = 0; i < inputFiles.size(); ++i )
00145     {
00146       std::string inputFile = inputFiles[ i ];
00147       std::string outputFile = outputFiles[ i ];
00148 
00149       STLLoader loader;
00150       if (!loader.load(inputFile))
00151       {
00152         exit( 1 );
00153       }
00154 
00155       ROS_INFO( "Converting %s to %s...", inputFile.c_str(), outputFile.c_str() );
00156       ROS_INFO( "%d triangles", (uint32_t)loader.triangles_.size() );
00157       std::stringstream ss;
00158       ss << "converted" << i;
00159       Ogre::MeshPtr mesh = loader.toMesh(ss.str());
00160       meshSerializer->exportMesh( mesh.get(), outputFile, Serializer::ENDIAN_LITTLE );
00161     }
00162   }
00163   catch ( Exception& e )
00164   {
00165     ROS_ERROR( "%s", e.what() );
00166   }
00167 
00168   delete meshSerializer;
00169   delete skelMgr;
00170   delete matMgr;
00171   delete meshMgr;
00172   delete bufferManager;
00173   delete mth;
00174 #if OGRE_VERSION_MAJOR >= 1 && OGRE_VERSION_MINOR >= 7
00175   delete lodMgr;
00176 #endif
00177   delete rgm;
00178   delete logMgr;
00179 
00180   return 0;
00181 }


ogre_tools
Author(s): Josh Faust
autogenerated on Wed Apr 23 2014 10:34:49