MOAB: Mesh Oriented datABase  (version 5.5.0)
crop_vol_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  * crop_vol_test.cpp
10  * \brief Driver to create a volume from 2 surfaces (top and bottom), a user defined polyline
11  * (contour) and a direction
12  *
13  */
14 
15 #include "moab/Core.hpp"
16 #include <iostream>
17 #include <fstream>
18 #include <cstring>
19 #include "moab/FBEngine.hpp"
20 #include "moab/GeomTopoTool.hpp"
21 #include "TestUtil.hpp"
22 
23 std::string filename_top;
24 std::string filename_bot;
25 std::string polygon_file_name;
26 double min_dot = 0.8;
27 std::string vol_file;
28 
31 
32 using namespace moab;
33 
34 ErrorCode volume_test( FBEngine* pFacet );
35 
36 void handle_error_code( ErrorCode rv, int& number_failed, int& number_successful )
37 {
38  if( rv == MB_SUCCESS )
39  {
40  std::cout << "Success";
41  number_successful++;
42  }
43  else
44  {
45  std::cout << "Failure";
46  number_failed++;
47  }
48 }
49 
50 int main( int argc, char* argv[] )
51 {
52  filename_bot = TestDir + "unittest/BedCrop2.h5m";
53  filename_top = TestDir + "unittest/SECrop2.h5m"; //"/polyPB.txt";
54  polygon_file_name = TestDir + "unittest/poly14.txt";
55  vol_file = "volIce.h5m"; // output
56 
58  bool remove_output = true;
59 
60  if( argc == 1 )
61  {
62  std::cout << "Using default input files: " << filename_bot << " " << filename_top << " " << polygon_file_name
63  << " " << min_dot << " " << vol_file << std::endl;
64  std::cout << " default output file: " << vol_file << " will be deleted \n";
65  }
66  else if( argc == 6 )
67  {
68  remove_output = false;
69  filename_bot = argv[1];
70  filename_top = argv[2];
71  polygon_file_name = argv[3];
72  min_dot = atof( argv[4] );
73  vol_file = argv[5];
74  }
75  else
76  {
77  std::cout << " wrong number of arguments\n";
78  return 1; // fail
79  }
80  Core mbcore;
81  Interface* mb = &mbcore;
82 
83  ErrorCode rval = mb->load_file( filename_bot.c_str() );MB_CHK_SET_ERR( rval, "failed to load bed file" );
84 
85  rval = mb->load_file( filename_top.c_str() ); // load the second face (we know we have one face in each file)
86  MB_CHK_SET_ERR( rval, "failed to load top file" );
87 
88  FBEngine* pFacet = new FBEngine( mb, NULL, true ); // smooth facetting, no OBB tree passed
89 
90  if( !pFacet ) return 1; // error
91 
92  // should the init be part of constructor or not?
93  // this is where the obb tree is constructed, and smooth faceting initialized, too.
94  rval = pFacet->Init();MB_CHK_SET_ERR( rval, "failed to initialize smoothing" );
95 
96  std::cout << "volume creation test: ";
97  rval = volume_test( pFacet );
99  std::cout << "\n";
100 
101  if( remove_output )
102  {
103  remove( vol_file.c_str() );
104  }
105  return number_tests_failed;
106 }
107 
109 {
110  // there should be 2 faces in the model
111  std::ifstream datafile( polygon_file_name.c_str(), std::ifstream::in );
112  if( !datafile )
113  {
114  std::cout << "can't read file\n";
115  return MB_FAILURE;
116  }
117  //
118  char temp[100];
119  double direction[3]; // normalized
120  double gridSize;
121  datafile.getline( temp, 100 ); // first line
122 
123  // get direction and mesh size along polygon segments, from file
124  sscanf( temp, " %lf %lf %lf %lf ", direction, direction + 1, direction + 2, &gridSize );
125  // NORMALIZE(direction);// just to be sure
126 
127  std::vector< double > xyz;
128  while( !datafile.eof() )
129  {
130  datafile.getline( temp, 100 );
131  // int id = 0;
132  double x, y, z;
133  int nr = sscanf( temp, "%lf %lf %lf", &x, &y, &z );
134  if( nr == 3 )
135  {
136  xyz.push_back( x );
137  xyz.push_back( y );
138  xyz.push_back( z );
139  }
140  }
141  int sizePolygon = (int)xyz.size() / 3;
142  if( sizePolygon < 3 )
143  {
144  std::cerr << " Not enough points in the polygon" << std::endl;
145  return MB_FAILURE;
146  }
148  ErrorCode rval = pFacet->getRootSet( &root_set );MB_CHK_SET_ERR( rval, "ERROR : getRootSet failed!" );
149 
150  int top = 2; // iBase_FACE;
151 
152  Range faces;
153  rval = pFacet->getEntities( root_set, top, faces );MB_CHK_SET_ERR( rval, "Failed to get faces in volume_test." );
154 
155  if( 2 != faces.size() )
156  {
157  std::cerr << "expected 2 faces, top and bottom\n";
158  return MB_FAILURE;
159  }
160  EntityHandle newFace1; // first test is with closed surface
161  rval = pFacet->split_surface_with_direction( faces[0], xyz, direction, /*closed*/ 1, min_dot, newFace1 );MB_CHK_SET_ERR( rval, "Failed to crop first face." );
162 
163  EntityHandle newFace2; // first test is with closed surface
164  rval = pFacet->split_surface_with_direction( faces[1], xyz, direction, /*closed*/ 1, min_dot, newFace2 );MB_CHK_SET_ERR( rval, "Failed to crop second face." );
165 
166  // here we make the assumption that the edges, vertices are matching fine...
167  EntityHandle volume;
168  rval = pFacet->create_volume_with_direction( newFace1, newFace2, direction, volume );MB_CHK_SET_ERR( rval, "Failed to create volume." );
169 
170  Interface* mb = pFacet->moab_instance();
171  pFacet->delete_smooth_tags();
172  // duplicate -- extract a new geom topo tool
173  GeomTopoTool* gtt = pFacet->get_gtt();
174  GeomTopoTool* duplicate = NULL;
175  std::vector< EntityHandle > gents;
176  gents.push_back( volume );
177  rval = gtt->duplicate_model( duplicate, &gents );MB_CHK_SET_ERR( rval, "Failed to extract volume." );
178  EntityHandle newRootSet = duplicate->get_root_model_set();
179  delete pFacet;
180  pFacet = NULL; // try not to write the obb tree
181  delete duplicate;
182  rval = mb->write_file( vol_file.c_str(), NULL, NULL, &newRootSet, 1 );
183 
184  return rval;
185 }