MOAB: Mesh Oriented datABase
(version 5.4.1)
|
00001 #include "TestUtil.hpp" 00002 #include "moab/Core.hpp" 00003 #include "moab/Range.hpp" 00004 #include <cmath> 00005 #include <algorithm> 00006 00007 using namespace moab; 00008 00009 /* Input test file: test/sample.stl 00010 * 00011 * Example ASCII STL file from: 00012 * http://people.sc.fsu.edu/~burkardt/data/stla/stla.html 00013 */ 00014 #ifdef MESHDIR 00015 static const char sample[] = STRINGIFY( MESHDIR ) "/io/sample.stl"; 00016 #else 00017 static const char sample[] = "sample.stl"; 00018 #endif 00019 00020 // Use static keyword so that tmp_file can only be accessed within this file 00021 static const char* tmp_file = "test.stl"; 00022 00023 void test_read_ascii(); 00024 void test_write_ascii(); 00025 void test_type_option(); 00026 void test_detect_type(); 00027 void test_endian_option(); 00028 void test_big_endian(); 00029 void test_little_endian(); 00030 void test_detect_byte_order(); 00031 00032 void read_file( Interface& moab, const char* input_file, const char* options = "" ); 00033 void convert_file( const char* source_file, const char* dest_file, const char* options = "" ); 00034 // check that the mesh constains the simple tetrahedron defined 00035 // in test/sample.stl 00036 void check_mesh_is_tet( Interface& moab ); 00037 00038 int main() 00039 { 00040 int result = 0; 00041 00042 result += RUN_TEST( test_read_ascii ); 00043 result += RUN_TEST( test_write_ascii ); 00044 result += RUN_TEST( test_type_option ); 00045 result += RUN_TEST( test_detect_type ); 00046 result += RUN_TEST( test_endian_option ); 00047 result += RUN_TEST( test_big_endian ); 00048 result += RUN_TEST( test_little_endian ); 00049 result += RUN_TEST( test_detect_byte_order ); 00050 00051 remove( tmp_file ); 00052 return result; 00053 } 00054 00055 ErrorCode read_file_( Interface& moab, const char* input_file, const char* options = "" ) 00056 { 00057 ErrorCode rval = moab.load_file( input_file, 0, options ); 00058 return rval; 00059 } 00060 00061 void read_file( Interface& moab, const char* input_file, const char* options ) 00062 { 00063 ErrorCode rval = read_file_( moab, input_file, options );CHECK_ERR( rval ); 00064 } 00065 00066 void convert_file( const char* input_file, const char* output_file, const char* options ) 00067 { 00068 ErrorCode rval; 00069 Core moab; 00070 00071 rval = moab.load_file( input_file );CHECK_ERR( rval ); 00072 00073 rval = moab.write_file( output_file, "STL", options );CHECK_ERR( rval ); 00074 } 00075 00076 void test_read_ascii() 00077 { 00078 Core moab; 00079 read_file( moab, sample, "ASCII" ); 00080 check_mesh_is_tet( moab ); 00081 } 00082 00083 void test_write_ascii() 00084 { 00085 convert_file( sample, tmp_file, "ASCII" ); 00086 Core moab; 00087 read_file( moab, tmp_file, "ASCII" ); 00088 remove( tmp_file ); 00089 check_mesh_is_tet( moab ); 00090 } 00091 00092 void test_type_option() 00093 { 00094 ErrorCode rval; 00095 Core moab; 00096 00097 rval = read_file_( moab, sample, "BINARY" ); 00098 CHECK( MB_SUCCESS != rval ); 00099 00100 convert_file( sample, tmp_file, "BINARY" ); 00101 rval = read_file_( moab, tmp_file, "ASCII" ); 00102 CHECK( MB_SUCCESS != rval ); 00103 00104 remove( tmp_file ); 00105 } 00106 00107 void test_detect_type() 00108 { 00109 Core moab; 00110 00111 read_file( moab, sample ); 00112 00113 convert_file( sample, tmp_file, "BINARY" ); 00114 read_file_( moab, tmp_file ); 00115 00116 remove( tmp_file ); 00117 } 00118 00119 void test_endian_option() 00120 { 00121 ErrorCode rval; 00122 Core moab; 00123 00124 convert_file( sample, tmp_file, "BINARY;BIG_ENDIAN" ); 00125 rval = read_file_( moab, tmp_file, "BINARY;LITTLE_ENDIAN" ); 00126 CHECK( MB_SUCCESS != rval ); 00127 00128 convert_file( sample, tmp_file, "BINARY;LITTLE_ENDIAN" ); 00129 rval = read_file_( moab, tmp_file, "BINARY;BIG_ENDIAN" ); 00130 CHECK( MB_SUCCESS != rval ); 00131 00132 remove( tmp_file ); 00133 } 00134 00135 void test_big_endian() 00136 { 00137 Core moab; 00138 convert_file( sample, tmp_file, "BINARY;BIG_ENDIAN" ); 00139 read_file( moab, tmp_file, "BINARY;BIG_ENDIAN" ); 00140 check_mesh_is_tet( moab ); 00141 remove( tmp_file ); 00142 } 00143 00144 void test_little_endian() 00145 { 00146 Core moab; 00147 convert_file( sample, tmp_file, "BINARY;LITTLE_ENDIAN" ); 00148 read_file( moab, tmp_file, "BINARY;LITTLE_ENDIAN" ); 00149 check_mesh_is_tet( moab ); 00150 remove( tmp_file ); 00151 } 00152 00153 void test_detect_byte_order() 00154 { 00155 Core moab; 00156 00157 convert_file( sample, tmp_file, "BINARY;LITTLE_ENDIAN" ); 00158 read_file( moab, tmp_file, "BINARY" ); 00159 00160 convert_file( sample, tmp_file, "BINARY;BIG_ENDIAN" ); 00161 read_file( moab, tmp_file, "BINARY" ); 00162 00163 remove( tmp_file ); 00164 } 00165 00166 void check_mesh_is_tet( Interface& moab ) 00167 { 00168 ErrorCode rval; 00169 Range verts, tris, other; 00170 rval = moab.get_entities_by_type( 0, MBVERTEX, verts );CHECK_ERR( rval ); 00171 rval = moab.get_entities_by_type( 0, MBTRI, tris );CHECK_ERR( rval ); 00172 rval = moab.get_entities_by_handle( 0, other );CHECK_ERR( rval ); 00173 00174 CHECK_EQUAL( 4, (int)verts.size() ); 00175 CHECK_EQUAL( 4, (int)tris.size() ); 00176 other = subtract( other, verts ); 00177 other = subtract( other, tris ); 00178 CHECK( other.all_of_type( MBENTITYSET ) ); 00179 00180 const double expt_coords[4][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; 00181 EntityHandle vert_handles[4] = { 0, 0, 0, 0 }; 00182 for( Range::iterator i = verts.begin(); i != verts.end(); ++i ) 00183 { 00184 double coords[3]; 00185 rval = moab.get_coords( &*i, 1, coords );CHECK_ERR( rval ); 00186 00187 bool found = false; 00188 for( int j = 0; j < 4; ++j ) 00189 { 00190 double ds = 0; 00191 for( int d = 0; d < 3; ++d ) 00192 { 00193 double dl = expt_coords[j][d] - coords[d]; 00194 ds += dl * dl; 00195 } 00196 00197 if( ds < 1e-6 ) 00198 { 00199 CHECK_EQUAL( (EntityHandle)0, vert_handles[j] ); 00200 vert_handles[j] = *i; 00201 found = true; 00202 break; 00203 } 00204 } 00205 CHECK( found ); 00206 } 00207 00208 const int expt_conn[4][3] = { { 0, 1, 3 }, { 0, 2, 1 }, { 0, 3, 2 }, { 1, 2, 3 } }; 00209 EntityHandle tri_handles[4] = { 0, 0, 0, 0 }; 00210 for( Range::iterator i = tris.begin(); i != tris.end(); ++i ) 00211 { 00212 const EntityHandle* conn = 0; 00213 int len = 0; 00214 rval = moab.get_connectivity( *i, conn, len );CHECK_ERR( rval ); 00215 CHECK_EQUAL( 3, len ); 00216 00217 int conn_idx[3] = { static_cast< int >( std::find( vert_handles, vert_handles + 4, conn[0] ) - vert_handles ), 00218 static_cast< int >( std::find( vert_handles, vert_handles + 4, conn[1] ) - vert_handles ), 00219 static_cast< int >( std::find( vert_handles, vert_handles + 4, conn[2] ) - vert_handles ) }; 00220 CHECK( conn_idx[0] != 4 ); 00221 CHECK( conn_idx[1] != 4 ); 00222 CHECK( conn_idx[2] != 4 ); 00223 00224 bool found = false; 00225 for( int j = 0; j < 4; ++j ) 00226 { 00227 int k = std::find( expt_conn[j], expt_conn[j] + 3, conn_idx[0] ) - expt_conn[j]; 00228 if( k == 3 ) continue; 00229 00230 if( expt_conn[j][( k + 1 ) % 3] == conn_idx[1] && expt_conn[j][( k + 2 ) % 3] == conn_idx[2] ) 00231 { 00232 CHECK_EQUAL( (EntityHandle)0, tri_handles[j] ); 00233 tri_handles[j] = *i; 00234 found = true; 00235 break; 00236 } 00237 } 00238 CHECK( found ); 00239 } 00240 }