MOAB: Mesh Oriented datABase  (version 5.4.1)
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 + "unittest/PB.h5m";
00061     polyline_file_name = TestDir + "unittest/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 )
00115     {
00116         remove( filename_out.c_str() );
00117     }
00118     return number_tests_failed;
00119 }
00120 
00121 // this test will test a split like the one for grounding line
00122 // the first and last point of the polyline should be close to the
00123 // initial boundary of the face to be split
00124 ErrorCode split_test_across()
00125 {
00126     Core mbcore;
00127     Interface* mb = &mbcore;
00128 
00129     ErrorCode rval = mb->load_file( filename.c_str() );MB_CHK_SET_ERR( rval, "failed to load already modified file" );
00130 
00131     FBEngine* pFacet = new FBEngine( mb, NULL, true );
00132 
00133     rval = pFacet->Init();MB_CHK_SET_ERR( rval, "failed to initialize smoothing" );
00134 
00135     EntityHandle root_set;
00136     rval = pFacet->getRootSet( &root_set );MB_CHK_SET_ERR( rval, "ERROR : getRootSet failed!" );
00137     int top = 2;  //  iBase_FACE;
00138 
00139     Range faces;
00140     rval = pFacet->getEntities( root_set, top, faces );MB_CHK_SET_ERR( rval, "Failed to get faces in split_test." );
00141 
00142     if( faces.size() != 1 )
00143     {
00144         std::cout << "num faces model:" << faces.size() << "\n";
00145         return MB_FAILURE;  //
00146     }
00147     // check only the second face
00148 
00149     EntityHandle second_face = faces[0];
00150     // use the polyPB.txt file to get the trimming polygon
00151     ;
00152     // read the file with the polygon user data
00153 
00154     std::ifstream datafile( polyline_file_name.c_str(), std::ifstream::in );
00155     if( !datafile )
00156     {
00157         std::cout << "can't read file\n";
00158         return MB_FAILURE;
00159     }
00160     //
00161     char temp[100];
00162     double direction[3];  // normalized
00163     double gridSize;
00164     datafile.getline( temp, 100 );  // first line
00165 
00166     // get direction and mesh size along polygon segments, from file
00167     sscanf( temp, " %lf %lf %lf %lf ", direction, direction + 1, direction + 2, &gridSize );
00168     // NORMALIZE(direction);// just to be sure
00169 
00170     std::vector< double > xyz;
00171     while( !datafile.eof() )
00172     {
00173         datafile.getline( temp, 100 );
00174         // int id = 0;
00175         double x, y, z;
00176         int nr = sscanf( temp, "%lf %lf %lf", &x, &y, &z );
00177         if( nr == 3 )
00178         {
00179             xyz.push_back( x );
00180             xyz.push_back( y );
00181             xyz.push_back( z );
00182         }
00183     }
00184     int sizePolygon = (int)xyz.size() / 3;
00185     if( sizePolygon < 2 )
00186     {
00187         std::cerr << " Not enough points in the polygon" << std::endl;
00188         return MB_FAILURE;
00189     }
00190 
00191     EntityHandle newFace;  // this test is with a "grounding" line
00192     // the second face should be the one that we want for test
00193     rval = pFacet->split_surface_with_direction( second_face, xyz, direction, /*closed*/ 0,
00194                                                  /*min_dot */ 0.8, newFace );MB_CHK_ERR( rval );
00195 
00196     // save a new database, with 3 faces, eventually
00197     pFacet->delete_smooth_tags();
00198     delete pFacet;
00199     pFacet = NULL;  // try not to write the obb tree
00200 
00201     rval = mb->write_file( filename_out.c_str() );MB_CHK_SET_ERR( rval, "Writing mesh file failed\n" );
00202 
00203     return rval;
00204 }
00205 
00206 ErrorCode verify_split()
00207 {
00208     Interface* mb = new Core();
00209 
00210     ErrorCode rval = mb->load_file( filename_out.c_str() );MB_CHK_SET_ERR( rval, "Loading mesh file failed\n" );
00211 
00212     moab::GeomTopoTool gTopoTool( mb, true, 0, true, false );
00213 
00214     if( !gTopoTool.check_model() ) return MB_FAILURE;
00215 
00216     delete mb;
00217     return MB_SUCCESS;
00218 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines