MOAB: Mesh Oriented datABase  (version 5.5.0)
mbground_test.cpp
Go to the documentation of this file.
1 /**
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  */
8 /**
9  * \file mbground_test.cpp
10  *
11  * \brief test mbground, a test for the FBEngine class, which is providing iGeom like methods to a
12  * MOAB db
13  *
14  */
15 #include "moab/Core.hpp"
16 #include <iostream>
17 #include <fstream>
18 #include <set>
19 #include <algorithm>
20 #include <vector>
21 #include <iterator>
22 #include <algorithm>
23 #include <iomanip>
24 #include <cassert>
25 #include <cstring>
26 #include <cmath>
27 #include "moab/FBEngine.hpp"
28 #include "moab/GeomTopoTool.hpp"
29 #include "TestUtil.hpp"
30 
31 std::string filename;
32 std::string filename_out;
33 std::string polyline_file_name;
34 double min_dot = 0.8;
38 
39 using namespace moab;
40 
43 
44 void handle_error_code( ErrorCode rv, int& number_failed, int& number_successful )
45 {
46  if( rv == MB_SUCCESS )
47  {
48  std::cout << "Success";
49  number_successful++;
50  }
51  else
52  {
53  std::cout << "Failure";
54  number_failed++;
55  }
56 }
57 
58 int main( int argc, char* argv[] )
59 {
60  filename = TestDir + "unittest/PB.h5m";
61  polyline_file_name = TestDir + "unittest/polyline.txt";
62  filename_out = "PB_ground.h5m";
63  min_dot = 0.8;
64 
65  keep_output = false;
66 
67  if( argc == 1 )
68  {
69  std::cout << "Using default input " << filename << " " << polyline_file_name << " " << min_dot << " "
70  << filename_out << std::endl;
71  std::cout << " default output file: " << filename_out << " will be deleted \n";
72  }
73  else if( argc == 5 )
74  {
75  filename = argv[1];
76  polyline_file_name = argv[2];
77  min_dot = atof( argv[3] );
78  filename_out = argv[4];
79  keep_output = true;
80  }
81  else
82  {
83  std::cerr << "Usage: " << argv[0] << " [geom_filename] [polygon_file] [min_dot] [output_file]" << std::endl;
84  return 1;
85  }
86 
87  Core mbcore;
88  Interface* mb = &mbcore;
89 
90  ErrorCode rval = mb->load_file( filename.c_str() );MB_CHK_SET_ERR( rval, "failed to load input file" );
91 
92  FBEngine* pFacet = new FBEngine( mb, NULL, true ); // smooth facetting, no OBB tree passed
93 
94  if( !pFacet ) return 1; // error
95 
96  // should the init be part of constructor or not?
97  // this is where the obb tree is constructed, and smooth faceting initialized, too.
98  rval = pFacet->Init();MB_CHK_SET_ERR( rval, "failed to initialize smoothing" );
99 
100  delete pFacet;
101  pFacet = NULL;
102 
103  // split_test_across
104  std::cout << " split across test: ";
105  rval = split_test_across();
107  std::cout << "\n";
108 
109  std::cout << " verify split ";
110  rval = verify_split();
112  std::cout << "\n";
113  // when we are done, remove modified file if we want to
114  if( !keep_output )
115  {
116  remove( filename_out.c_str() );
117  }
118  return number_tests_failed;
119 }
120 
121 // this test will test a split like the one for grounding line
122 // the first and last point of the polyline should be close to the
123 // initial boundary of the face to be split
125 {
126  Core mbcore;
127  Interface* mb = &mbcore;
128 
129  ErrorCode rval = mb->load_file( filename.c_str() );MB_CHK_SET_ERR( rval, "failed to load already modified file" );
130 
131  FBEngine* pFacet = new FBEngine( mb, NULL, true );
132 
133  rval = pFacet->Init();MB_CHK_SET_ERR( rval, "failed to initialize smoothing" );
134 
136  rval = pFacet->getRootSet( &root_set );MB_CHK_SET_ERR( rval, "ERROR : getRootSet failed!" );
137  int top = 2; // iBase_FACE;
138 
139  Range faces;
140  rval = pFacet->getEntities( root_set, top, faces );MB_CHK_SET_ERR( rval, "Failed to get faces in split_test." );
141 
142  if( faces.size() != 1 )
143  {
144  std::cout << "num faces model:" << faces.size() << "\n";
145  return MB_FAILURE; //
146  }
147  // check only the second face
148 
149  EntityHandle second_face = faces[0];
150  // use the polyPB.txt file to get the trimming polygon
151  ;
152  // read the file with the polygon user data
153 
154  std::ifstream datafile( polyline_file_name.c_str(), std::ifstream::in );
155  if( !datafile )
156  {
157  std::cout << "can't read file\n";
158  return MB_FAILURE;
159  }
160  //
161  char temp[100];
162  double direction[3]; // normalized
163  double gridSize;
164  datafile.getline( temp, 100 ); // first line
165 
166  // get direction and mesh size along polygon segments, from file
167  sscanf( temp, " %lf %lf %lf %lf ", direction, direction + 1, direction + 2, &gridSize );
168  // NORMALIZE(direction);// just to be sure
169 
170  std::vector< double > xyz;
171  while( !datafile.eof() )
172  {
173  datafile.getline( temp, 100 );
174  // int id = 0;
175  double x, y, z;
176  int nr = sscanf( temp, "%lf %lf %lf", &x, &y, &z );
177  if( nr == 3 )
178  {
179  xyz.push_back( x );
180  xyz.push_back( y );
181  xyz.push_back( z );
182  }
183  }
184  int sizePolygon = (int)xyz.size() / 3;
185  if( sizePolygon < 2 )
186  {
187  std::cerr << " Not enough points in the polygon" << std::endl;
188  return MB_FAILURE;
189  }
190 
191  EntityHandle newFace; // this test is with a "grounding" line
192  // the second face should be the one that we want for test
193  rval = pFacet->split_surface_with_direction( second_face, xyz, direction, /*closed*/ 0,
194  /*min_dot */ 0.8, newFace );MB_CHK_ERR( rval );
195 
196  // save a new database, with 3 faces, eventually
197  pFacet->delete_smooth_tags();
198  delete pFacet;
199  pFacet = NULL; // try not to write the obb tree
200 
201  rval = mb->write_file( filename_out.c_str() );MB_CHK_SET_ERR( rval, "Writing mesh file failed\n" );
202 
203  return rval;
204 }
205 
207 {
208  Interface* mb = new Core();
209 
210  ErrorCode rval = mb->load_file( filename_out.c_str() );MB_CHK_SET_ERR( rval, "Loading mesh file failed\n" );
211 
212  moab::GeomTopoTool gTopoTool( mb, true, 0, true, false );
213 
214  if( !gTopoTool.check_model() ) return MB_FAILURE;
215 
216  delete mb;
217  return MB_SUCCESS;
218 }