00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00115
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();
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 }