MOAB: Mesh Oriented datABase
(version 5.4.1)
|
00001 #include "moab/Core.hpp" 00002 #include "moab/AdaptiveKDTree.hpp" 00003 #include "moab/Range.hpp" 00004 #include <cerrno> 00005 #include <cstdio> 00006 #include <cstdlib> 00007 #include <ctime> 00008 00009 using namespace moab; 00010 00011 void usage( const char* argv0 ) 00012 { 00013 fprintf( stderr, "usage: %s [-t] [-d <result_file>] <tree_file> <point_file> [<count>]\n", argv0 ); 00014 exit( 1 ); 00015 } 00016 00017 void print_file_stats( Interface& moab ) 00018 { 00019 ErrorCode rval; 00020 int num_tri; 00021 Range sets; 00022 unsigned long long set_mem, set_am, tag_mem, tag_am; 00023 00024 rval = moab.get_number_entities_by_type( 0, MBTRI, num_tri ); 00025 if( MB_SUCCESS != rval ) num_tri = -1; 00026 rval = moab.get_entities_by_type( 0, MBENTITYSET, sets ); 00027 if( MB_SUCCESS != rval ) sets.clear(); 00028 00029 moab.estimated_memory_use( sets, 0, 0, &set_mem, &set_am, 0, 0, 0, 0, &tag_mem, &tag_am ); 00030 printf( "Triangles: %d\n", num_tri ); 00031 printf( "Sets: %lu\n", (unsigned long)sets.size() ); 00032 printf( "Set storage: %llu (%llu)\n", set_mem, set_am ); 00033 printf( "Tag storage: %llu (%llu)\n", tag_mem, tag_am ); 00034 } 00035 00036 int main( int argc, char* argv[] ) 00037 { 00038 double* values; 00039 unsigned long length; 00040 FILE *file, *rfile = 0; 00041 unsigned long count = 0; 00042 clock_t t; 00043 00044 const char* tree_file = 0; 00045 const char* point_file = 0; 00046 const char* result_file = 0; 00047 bool query_triangles = false; 00048 00049 if( argc < 3 || argc > 7 ) usage( argv[0] ); 00050 00051 for( int i = 1; i < argc; ++i ) 00052 { 00053 if( !strcmp( "-t", argv[i] ) ) 00054 query_triangles = true; 00055 else if( !strcmp( "-d", argv[i] ) ) 00056 { 00057 ++i; 00058 if( i == argc ) usage( argv[0] ); 00059 result_file = argv[i]; 00060 } 00061 else if( !tree_file ) 00062 tree_file = argv[i]; 00063 else if( !point_file ) 00064 point_file = argv[i]; 00065 else 00066 { 00067 char* endptr; 00068 count = strtol( argv[i], &endptr, 0 ); 00069 if( *endptr || count < 1 ) usage( argv[0] ); 00070 } 00071 } 00072 00073 file = fopen( point_file, "rb" ); 00074 if( !file ) 00075 { 00076 perror( point_file ); 00077 return 2; 00078 } 00079 fseek( file, 0, SEEK_END ); 00080 length = ftell( file ) / ( 3 * sizeof( double ) ); 00081 fseek( file, 0, SEEK_SET ); 00082 values = new double[3 * length]; 00083 if( length != fread( values, 3 * sizeof( double ), length, file ) ) 00084 { 00085 fprintf( stderr, "Error reading %lu points from file \"%s\"\n", length, argv[2] ); 00086 delete[] values; 00087 return 2; 00088 } 00089 fclose( file ); 00090 00091 if( result_file ) 00092 { 00093 rfile = fopen( result_file, "w" ); 00094 if( !rfile ) 00095 { 00096 perror( result_file ); 00097 delete[] values; 00098 return 2; 00099 } 00100 } 00101 00102 if( !count ) count = length; 00103 00104 printf( "Loading tree..." ); 00105 fflush( stdout ); 00106 t = clock(); 00107 Core moab; 00108 ErrorCode rval = moab.load_mesh( tree_file, 0, 0 ); 00109 if( MB_SUCCESS != rval ) 00110 { 00111 fprintf( stderr, "Failed to read file: %s\n", tree_file ); 00112 delete[] values; 00113 return 2; 00114 } 00115 printf( "%0.2f seconds\n", ( clock() - t ) / (double)CLOCKS_PER_SEC ); 00116 fflush( stdout ); 00117 00118 Range range; 00119 AdaptiveKDTree tool( &moab ); 00120 tool.find_all_trees( range ); 00121 if( range.size() != 1 ) 00122 { 00123 fprintf( stderr, "%s : found %d kd-trees\n", argv[1], (int)range.size() ); 00124 delete[] values; 00125 return 3; 00126 } 00127 EntityHandle root = range.front(); 00128 00129 print_file_stats( moab ); 00130 00131 printf( "Running point queries..." ); 00132 fflush( stdout ); 00133 t = clock(); 00134 EntityHandle leaf; 00135 double pt[3]; 00136 for( unsigned long i = 0; i < count; ++i ) 00137 { 00138 const double* coords = values + 3 * ( i % length ); 00139 if( query_triangles ) 00140 rval = tool.closest_triangle( root, coords, pt, leaf ); 00141 else 00142 rval = tool.point_search( coords, leaf, 0.0, 0.0, NULL, &root ); 00143 if( MB_SUCCESS != rval ) 00144 { 00145 fprintf( stderr, "Failure (ErrorCode == %d) for point %d (%f, %f, %f)\n", (int)rval, 00146 (int)( count % length ), coords[0], coords[1], coords[2] ); 00147 } 00148 else if( rfile ) 00149 { 00150 if( query_triangles ) 00151 fprintf( rfile, "%f %f %f %f %f %f %lu\n", coords[0], coords[1], coords[2], pt[0], pt[1], pt[2], 00152 (unsigned long)leaf ); 00153 else 00154 fprintf( rfile, "%f %f %f %lu\n", coords[0], coords[1], coords[2], (unsigned long)leaf ); 00155 } 00156 } 00157 printf( "%0.2f seconds\n", ( clock() - t ) / (double)CLOCKS_PER_SEC ); 00158 fflush( stdout ); 00159 delete[] values; 00160 00161 return 0; 00162 }