MOAB: Mesh Oriented datABase  (version 5.2.1)
transform.cpp
Go to the documentation of this file.
00001 #include "MeshTransform.hpp"
00002 #include "MeshImpl.hpp"
00003 #include "CLArgs.hpp"
00004 #include "MsqError.hpp"
00005 
00006 #include <iostream>
00007 
00008 using namespace MBMesquite;
00009 
00010 const char ROTATE_FLAG    = 'r';
00011 const char SCALE_FLAG     = 's';
00012 const char TRANSLATE_FLAG = 't';
00013 
00014 class RotateArg : public CLArgs::DoubleListArgI
00015 {
00016   private:
00017     MeshTransform* mTransform;
00018 
00019   public:
00020     RotateArg( MeshTransform* xform ) : mTransform( xform ) {}
00021     bool value( const std::vector< double >& vals );
00022 };
00023 class ScaleArg : public CLArgs::DoubleListArgI
00024 {
00025   private:
00026     MeshTransform* mTransform;
00027 
00028   public:
00029     ScaleArg( MeshTransform* xform ) : mTransform( xform ) {}
00030     bool value( const std::vector< double >& vals );
00031 };
00032 class TranslateArg : public CLArgs::DoubleListArgI
00033 {
00034   private:
00035     MeshTransform* mTransform;
00036 
00037   public:
00038     TranslateArg( MeshTransform* xform ) : mTransform( xform ) {}
00039     bool value( const std::vector< double >& vals );
00040 };
00041 bool RotateArg::value( const std::vector< double >& vals )
00042 {
00043     mTransform->add_rotation( Vector3D( vals[1], vals[2], vals[3] ), vals[0] * M_PI / 180 );
00044     return true;
00045 }
00046 bool ScaleArg::value( const std::vector< double >& vals )
00047 {
00048     for( unsigned i = 0; i < vals.size(); ++i )
00049         if( vals[i] <= 0 ) return false;
00050     if( vals.size() == 1 )
00051         mTransform->add_scale( vals[0] );
00052     else
00053         mTransform->add_scale( Vector3D( vals[0], vals[1], vals[2] ) );
00054     return true;
00055 }
00056 bool TranslateArg::value( const std::vector< double >& vals )
00057 {
00058     mTransform->add_translation( Vector3D( vals[0], vals[1], vals[2] ) );
00059     return true;
00060 }
00061 
00062 int main( int argc, char* argv[] )
00063 {
00064     MeshTransform xform;
00065     RotateArg rotate_arg( &xform );
00066     ScaleArg scale_arg( &xform );
00067     TranslateArg translate_arg( &xform );
00068     CLArgs::ToggleArg freeonly, skin;
00069 
00070     CLArgs args( "vtkxform", "Transform a mesh",
00071                  "Apply one or more transformations to vertex coordinates "
00072                  "in a mesh read from a VTK file." );
00073 
00074     const char* ROTATE_VALS[] = { "a", "i", "j", "k" };
00075     args.double_list_flag( ROTATE_FLAG, "Specify a rotation as an angle in degrees counter-clockwise about a vector",
00076                            &rotate_arg );
00077     args.limit_list_flag( ROTATE_FLAG, 4, ROTATE_VALS );
00078 
00079     const char* SCALE_VALS[] = { "s", "sx", "sy", "sz" };
00080     args.double_list_flag( SCALE_FLAG, "Specify factor(s) by which to scale mesh about origin", &scale_arg );
00081     args.limit_list_flag( SCALE_FLAG, 1, SCALE_VALS );
00082     args.limit_list_flag( SCALE_FLAG, 3, SCALE_VALS + 1 );
00083 
00084     const char* TRANSLATE_VALS[] = { "dx", "dy", "dz" };
00085     args.double_list_flag( TRANSLATE_FLAG, "Specify translation of vertex coordinates.", &translate_arg );
00086     args.limit_list_flag( TRANSLATE_FLAG, 3, TRANSLATE_VALS );
00087 
00088     args.toggle_flag( 'f', "Do not move fixed vertices.", &freeonly );
00089     args.toggle_flag( 'k', "Mark boundary vertices as fixed", &skin );
00090 
00091     args.add_required_arg( "input_file" );
00092     args.add_required_arg( "output_file" );
00093 
00094     std::vector< std::string > files;
00095     if( !args.parse_options( argc, argv, files, std::cerr ) )
00096     {
00097         args.print_usage( std::cerr );
00098         exit( 1 );
00099     }
00100     std::string input_file  = files[0];
00101     std::string output_file = files[1];
00102 
00103     MeshImpl mesh;
00104     MsqError err;
00105     mesh.read_vtk( input_file.c_str(), err );
00106     if( err )
00107     {
00108         std::cerr << err << std::endl << "Failed to read file: " << input_file << std::endl;
00109         return 1;
00110     }
00111 
00112     if( skin.value() )
00113     {
00114         mesh.mark_skin_fixed( err, false );
00115         if( err )
00116         {
00117             std::cerr << err << std::endl << "Failed to skin mesh from file: " << input_file << std::endl;
00118             return 1;
00119         }
00120     }
00121 
00122     xform.skip_fixed_vertices( freeonly.value() );
00123     MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &mesh, 0 );
00124     xform.loop_over_mesh( &mesh_and_domain, 0, err );
00125     if( err )
00126     {
00127         std::cerr << err << std::endl;
00128         return 2;
00129     }
00130 
00131     mesh.write_vtk( output_file.c_str(), err );
00132     if( err )
00133     {
00134         std::cerr << err << std::endl << "Failed to write file: " << output_file << std::endl;
00135         return 1;
00136     }
00137 
00138     return 0;
00139 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines