MOAB: Mesh Oriented datABase  (version 5.3.0)
mbground_test.cpp
Go to the documentation of this file.
00001 /**
00002  * This library is free software; you can redistribute it and/or
00003  * modify it under the terms of the GNU Lesser General Public
00004  * License as published by the Free Software Foundation; either
00005  * version 2.1 of the License, or (at your option) any later version.
00006  *
00007  */
00008 /**
00009  * \file mbground_test.cpp
00010  *
00011  * \brief test mbground, a test for the FBEngine class, which is providing iGeom like methods to a
00012  * MOAB db
00013  *
00014  */
00015 #include "moab/Core.hpp"
00016 #include <iostream>
00017 #include <fstream>
00018 #include <set>
00019 #include <algorithm>
00020 #include <vector>
00021 #include <iterator>
00022 #include <algorithm>
00023 #include <iomanip>
00024 #include <cassert>
00025 #include <cstring>
00026 #include <cmath>
00027 #include "moab/FBEngine.hpp"
00028 #include "moab/GeomTopoTool.hpp"
00029 #include "TestUtil.hpp"
00030 
00031 std::string filename;
00032 std::string filename_out;
00033 std::string polyline_file_name;
00034 double min_dot = 0.8;
00035 bool keep_output;
00036 int number_tests_successful = 0;
00037 int number_tests_failed     = 0;
00038 
00039 using namespace moab;
00040 
00041 ErrorCode split_test_across();
00042 ErrorCode verify_split();
00043 
00044 void handle_error_code( ErrorCode rv, int& number_failed, int& number_successful )
00045 {
00046     if( rv == MB_SUCCESS )
00047     {
00048         std::cout << "Success";
00049         number_successful++;
00050     }
00051     else
00052     {
00053         std::cout << "Failure";
00054         number_failed++;
00055     }
00056 }
00057 
00058 int main( int argc, char* argv[] )
00059 {
00060     filename           = TestDir + "/PB.h5m";
00061     polyline_file_name = TestDir + "/polyline.txt";
00062     filename_out       = "PB_ground.h5m";
00063     min_dot            = 0.8;
00064 
00065     keep_output = false;
00066 
00067     if( argc == 1 )
00068     {
00069         std::cout << "Using default input " << filename << " " << polyline_file_name << " " << min_dot << " "
00070                   << filename_out << std::endl;
00071         std::cout << "    default output file: " << filename_out << " will be deleted \n";
00072     }
00073     else if( argc == 5 )
00074     {
00075         filename           = argv[1];
00076         polyline_file_name = argv[2];
00077         min_dot            = atof( argv[3] );
00078         filename_out       = argv[4];
00079         keep_output        = true;
00080     }
00081     else
00082     {
00083         std::cerr << "Usage: " << argv[0] << " [geom_filename] [polygon_file] [min_dot] [output_file]" << std::endl;
00084         return 1;
00085     }
00086 
00087     Core mbcore;
00088     Interface* mb = &mbcore;
00089 
00090     ErrorCode rval = mb->load_file( filename.c_str() );MB_CHK_SET_ERR( rval, "failed to load input file" );
00091 
00092     FBEngine* pFacet = new FBEngine( mb, NULL, true );  // smooth facetting, no OBB tree passed
00093 
00094     if( !pFacet ) return 1;  // error
00095 
00096     // should the init be part of constructor or not?
00097     // this is where the obb tree is constructed, and smooth faceting initialized, too.
00098     rval = pFacet->Init();MB_CHK_SET_ERR( rval, "failed to initialize smoothing" );
00099 
00100     delete pFacet;
00101     pFacet = NULL;
00102 
00103     // split_test_across
00104     std::cout << " split across test: ";
00105     rval = split_test_across();
00106     handle_error_code( rval, number_tests_failed, number_tests_successful );
00107     std::cout << "\n";
00108 
00109     std::cout << " verify split ";
00110     rval = verify_split();
00111     handle_error_code( rval, number_tests_failed, number_tests_successful );
00112     std::cout << "\n";
00113     // when we are done, remove modified file if we want to
00114     if( !keep_output ) { remove( filename_out.c_str() ); }
00115     return number_tests_failed;
00116 }
00117 
00118 // this test will test a split like the one for grounding line
00119 // the first and last point of the polyline should be close to the
00120 // initial boundary of the face to be split
00121 ErrorCode split_test_across()
00122 {
00123     Core mbcore;
00124     Interface* mb = &mbcore;
00125 
00126     ErrorCode rval = mb->load_file( filename.c_str() );MB_CHK_SET_ERR( rval, "failed to load already modified file" );
00127 
00128     FBEngine* pFacet = new FBEngine( mb, NULL, true );
00129 
00130     rval = pFacet->Init();MB_CHK_SET_ERR( rval, "failed to initialize smoothing" );
00131 
00132     EntityHandle root_set;
00133     rval = pFacet->getRootSet( &root_set );MB_CHK_SET_ERR( rval, "ERROR : getRootSet failed!" );
00134     int top = 2;  //  iBase_FACE;
00135 
00136     Range faces;
00137     rval = pFacet->getEntities( root_set, top, faces );MB_CHK_SET_ERR( rval, "Failed to get faces in split_test." );
00138 
00139     if( faces.size() != 1 )
00140     {
00141         std::cout << "num faces model:" << faces.size() << "\n";
00142         return MB_FAILURE;  //
00143     }
00144     // check only the second face
00145 
00146     EntityHandle second_face = faces[0];
00147     // use the polyPB.txt file to get the trimming polygon
00148     ;
00149     // read the file with the polygon user data
00150 
00151     std::ifstream datafile( polyline_file_name.c_str(), std::ifstream::in );
00152     if( !datafile )
00153     {
00154         std::cout << "can't read file\n";
00155         return MB_FAILURE;
00156     }
00157     //
00158     char temp[100];
00159     double direction[3];  // normalized
00160     double gridSize;
00161     datafile.getline( temp, 100 );  // first line
00162 
00163     // get direction and mesh size along polygon segments, from file
00164     sscanf( temp, " %lf %lf %lf %lf ", direction, direction + 1, direction + 2, &gridSize );
00165     // NORMALIZE(direction);// just to be sure
00166 
00167     std::vector< double > xyz;
00168     while( !datafile.eof() )
00169     {
00170         datafile.getline( temp, 100 );
00171         // int id = 0;
00172         double x, y, z;
00173         int nr = sscanf( temp, "%lf %lf %lf", &x, &y, &z );
00174         if( nr == 3 )
00175         {
00176             xyz.push_back( x );
00177             xyz.push_back( y );
00178             xyz.push_back( z );
00179         }
00180     }
00181     int sizePolygon = (int)xyz.size() / 3;
00182     if( sizePolygon < 2 )
00183     {
00184         std::cerr << " Not enough points in the polygon" << std::endl;
00185         return MB_FAILURE;
00186     }
00187 
00188     EntityHandle newFace;  // this test is with a "grounding" line
00189     // the second face should be the one that we want for test
00190     rval = pFacet->split_surface_with_direction( second_face, xyz, direction, /*closed*/ 0,
00191                                                  /*min_dot */ 0.8, newFace );MB_CHK_ERR( rval );
00192 
00193     // save a new database, with 3 faces, eventually
00194     pFacet->delete_smooth_tags();
00195     delete pFacet;
00196     pFacet = NULL;  // try not to write the obb tree
00197 
00198     rval = mb->write_file( filename_out.c_str() );MB_CHK_SET_ERR( rval, "Writing mesh file failed\n" );
00199 
00200     return rval;
00201 }
00202 
00203 ErrorCode verify_split()
00204 {
00205     Interface* mb = new Core();
00206 
00207     ErrorCode rval = mb->load_file( filename_out.c_str() );MB_CHK_SET_ERR( rval, "Loading mesh file failed\n" );
00208 
00209     moab::GeomTopoTool gTopoTool( mb, true, 0, true, false );
00210 
00211     if( !gTopoTool.check_model() ) return MB_FAILURE;
00212 
00213     delete mb;
00214     return MB_SUCCESS;
00215 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines