MOAB: Mesh Oriented datABase  (version 5.5.0)
stl_test.cpp
Go to the documentation of this file.
1 #include "TestUtil.hpp"
2 #include "moab/Core.hpp"
3 #include "moab/Range.hpp"
4 #include <cmath>
5 #include <algorithm>
6 
7 using namespace moab;
8 
9 /* Input test file: test/sample.stl
10  *
11  * Example ASCII STL file from:
12  * http://people.sc.fsu.edu/~burkardt/data/stla/stla.html
13  */
14 #ifdef MESHDIR
15 static const char sample[] = STRINGIFY( MESHDIR ) "/io/sample.stl";
16 #else
17 static const char sample[] = "sample.stl";
18 #endif
19 
20 // Use static keyword so that tmp_file can only be accessed within this file
21 static const char* tmp_file = "test.stl";
22 
23 void test_read_ascii();
24 void test_write_ascii();
25 void test_type_option();
26 void test_detect_type();
27 void test_endian_option();
28 void test_big_endian();
29 void test_little_endian();
31 
32 void read_file( Interface& moab, const char* input_file, const char* options = "" );
33 void convert_file( const char* source_file, const char* dest_file, const char* options = "" );
34 // check that the mesh constains the simple tetrahedron defined
35 // in test/sample.stl
37 
38 int main()
39 {
40  int result = 0;
41 
42  result += RUN_TEST( test_read_ascii );
43  result += RUN_TEST( test_write_ascii );
44  result += RUN_TEST( test_type_option );
45  result += RUN_TEST( test_detect_type );
46  result += RUN_TEST( test_endian_option );
47  result += RUN_TEST( test_big_endian );
48  result += RUN_TEST( test_little_endian );
49  result += RUN_TEST( test_detect_byte_order );
50 
51  remove( tmp_file );
52  return result;
53 }
54 
55 ErrorCode read_file_( Interface& moab, const char* input_file, const char* options = "" )
56 {
57  ErrorCode rval = moab.load_file( input_file, 0, options );
58  return rval;
59 }
60 
61 void read_file( Interface& moab, const char* input_file, const char* options )
62 {
63  ErrorCode rval = read_file_( moab, input_file, options );CHECK_ERR( rval );
64 }
65 
66 void convert_file( const char* input_file, const char* output_file, const char* options )
67 {
68  ErrorCode rval;
69  Core moab;
70 
71  rval = moab.load_file( input_file );CHECK_ERR( rval );
72 
73  rval = moab.write_file( output_file, "STL", options );CHECK_ERR( rval );
74 }
75 
77 {
78  Core moab;
79  read_file( moab, sample, "ASCII" );
81 }
82 
84 {
85  convert_file( sample, tmp_file, "ASCII" );
86  Core moab;
87  read_file( moab, tmp_file, "ASCII" );
88  remove( tmp_file );
90 }
91 
93 {
94  ErrorCode rval;
95  Core moab;
96 
97  rval = read_file_( moab, sample, "BINARY" );
98  CHECK( MB_SUCCESS != rval );
99 
100  convert_file( sample, tmp_file, "BINARY" );
101  rval = read_file_( moab, tmp_file, "ASCII" );
102  CHECK( MB_SUCCESS != rval );
103 
104  remove( tmp_file );
105 }
106 
108 {
109  Core moab;
110 
111  read_file( moab, sample );
112 
113  convert_file( sample, tmp_file, "BINARY" );
115 
116  remove( tmp_file );
117 }
118 
120 {
121  ErrorCode rval;
122  Core moab;
123 
124  convert_file( sample, tmp_file, "BINARY;BIG_ENDIAN" );
125  rval = read_file_( moab, tmp_file, "BINARY;LITTLE_ENDIAN" );
126  CHECK( MB_SUCCESS != rval );
127 
128  convert_file( sample, tmp_file, "BINARY;LITTLE_ENDIAN" );
129  rval = read_file_( moab, tmp_file, "BINARY;BIG_ENDIAN" );
130  CHECK( MB_SUCCESS != rval );
131 
132  remove( tmp_file );
133 }
134 
136 {
137  Core moab;
138  convert_file( sample, tmp_file, "BINARY;BIG_ENDIAN" );
139  read_file( moab, tmp_file, "BINARY;BIG_ENDIAN" );
141  remove( tmp_file );
142 }
143 
145 {
146  Core moab;
147  convert_file( sample, tmp_file, "BINARY;LITTLE_ENDIAN" );
148  read_file( moab, tmp_file, "BINARY;LITTLE_ENDIAN" );
150  remove( tmp_file );
151 }
152 
154 {
155  Core moab;
156 
157  convert_file( sample, tmp_file, "BINARY;LITTLE_ENDIAN" );
158  read_file( moab, tmp_file, "BINARY" );
159 
160  convert_file( sample, tmp_file, "BINARY;BIG_ENDIAN" );
161  read_file( moab, tmp_file, "BINARY" );
162 
163  remove( tmp_file );
164 }
165 
167 {
168  ErrorCode rval;
169  Range verts, tris, other;
170  rval = moab.get_entities_by_type( 0, MBVERTEX, verts );CHECK_ERR( rval );
171  rval = moab.get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval );
172  rval = moab.get_entities_by_handle( 0, other );CHECK_ERR( rval );
173 
174  CHECK_EQUAL( 4, (int)verts.size() );
175  CHECK_EQUAL( 4, (int)tris.size() );
176  other = subtract( other, verts );
177  other = subtract( other, tris );
178  CHECK( other.all_of_type( MBENTITYSET ) );
179 
180  const double expt_coords[4][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
181  EntityHandle vert_handles[4] = { 0, 0, 0, 0 };
182  for( Range::iterator i = verts.begin(); i != verts.end(); ++i )
183  {
184  double coords[3];
185  rval = moab.get_coords( &*i, 1, coords );CHECK_ERR( rval );
186 
187  bool found = false;
188  for( int j = 0; j < 4; ++j )
189  {
190  double ds = 0;
191  for( int d = 0; d < 3; ++d )
192  {
193  double dl = expt_coords[j][d] - coords[d];
194  ds += dl * dl;
195  }
196 
197  if( ds < 1e-6 )
198  {
199  CHECK_EQUAL( (EntityHandle)0, vert_handles[j] );
200  vert_handles[j] = *i;
201  found = true;
202  break;
203  }
204  }
205  CHECK( found );
206  }
207 
208  const int expt_conn[4][3] = { { 0, 1, 3 }, { 0, 2, 1 }, { 0, 3, 2 }, { 1, 2, 3 } };
209  EntityHandle tri_handles[4] = { 0, 0, 0, 0 };
210  for( Range::iterator i = tris.begin(); i != tris.end(); ++i )
211  {
212  const EntityHandle* conn = 0;
213  int len = 0;
214  rval = moab.get_connectivity( *i, conn, len );CHECK_ERR( rval );
215  CHECK_EQUAL( 3, len );
216 
217  int conn_idx[3] = { static_cast< int >( std::find( vert_handles, vert_handles + 4, conn[0] ) - vert_handles ),
218  static_cast< int >( std::find( vert_handles, vert_handles + 4, conn[1] ) - vert_handles ),
219  static_cast< int >( std::find( vert_handles, vert_handles + 4, conn[2] ) - vert_handles ) };
220  CHECK( conn_idx[0] != 4 );
221  CHECK( conn_idx[1] != 4 );
222  CHECK( conn_idx[2] != 4 );
223 
224  bool found = false;
225  for( int j = 0; j < 4; ++j )
226  {
227  int k = std::find( expt_conn[j], expt_conn[j] + 3, conn_idx[0] ) - expt_conn[j];
228  if( k == 3 ) continue;
229 
230  if( expt_conn[j][( k + 1 ) % 3] == conn_idx[1] && expt_conn[j][( k + 2 ) % 3] == conn_idx[2] )
231  {
232  CHECK_EQUAL( (EntityHandle)0, tri_handles[j] );
233  tri_handles[j] = *i;
234  found = true;
235  break;
236  }
237  }
238  CHECK( found );
239  }
240 }