MOAB: Mesh Oriented datABase  (version 5.5.0)
kd_tree_time.cpp File Reference
#include "moab/Core.hpp"
#include "moab/AdaptiveKDTree.hpp"
#include "moab/Range.hpp"
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <ctime>
+ Include dependency graph for kd_tree_time.cpp:

Go to the source code of this file.

Functions

void usage (const char *argv0)
 
void print_file_stats (Interface &moab)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 36 of file kd_tree_time.cpp.

37 {
38  double* values;
39  unsigned long length;
40  FILE *file, *rfile = 0;
41  unsigned long count = 0;
42  clock_t t;
43 
44  const char* tree_file = 0;
45  const char* point_file = 0;
46  const char* result_file = 0;
47  bool query_triangles = false;
48 
49  if( argc < 3 || argc > 7 ) usage( argv[0] );
50 
51  for( int i = 1; i < argc; ++i )
52  {
53  if( !strcmp( "-t", argv[i] ) )
54  query_triangles = true;
55  else if( !strcmp( "-d", argv[i] ) )
56  {
57  ++i;
58  if( i == argc ) usage( argv[0] );
59  result_file = argv[i];
60  }
61  else if( !tree_file )
62  tree_file = argv[i];
63  else if( !point_file )
64  point_file = argv[i];
65  else
66  {
67  char* endptr;
68  count = strtol( argv[i], &endptr, 0 );
69  if( *endptr || count < 1 ) usage( argv[0] );
70  }
71  }
72 
73  file = fopen( point_file, "rb" );
74  if( !file )
75  {
76  perror( point_file );
77  return 2;
78  }
79  fseek( file, 0, SEEK_END );
80  length = ftell( file ) / ( 3 * sizeof( double ) );
81  fseek( file, 0, SEEK_SET );
82  values = new double[3 * length];
83  if( length != fread( values, 3 * sizeof( double ), length, file ) )
84  {
85  fprintf( stderr, "Error reading %lu points from file \"%s\"\n", length, argv[2] );
86  delete[] values;
87  return 2;
88  }
89  fclose( file );
90 
91  if( result_file )
92  {
93  rfile = fopen( result_file, "w" );
94  if( !rfile )
95  {
96  perror( result_file );
97  delete[] values;
98  return 2;
99  }
100  }
101 
102  if( !count ) count = length;
103 
104  printf( "Loading tree..." );
105  fflush( stdout );
106  t = clock();
107  Core moab;
108  ErrorCode rval = moab.load_mesh( tree_file, 0, 0 );
109  if( MB_SUCCESS != rval )
110  {
111  fprintf( stderr, "Failed to read file: %s\n", tree_file );
112  delete[] values;
113  return 2;
114  }
115  printf( "%0.2f seconds\n", ( clock() - t ) / (double)CLOCKS_PER_SEC );
116  fflush( stdout );
117 
118  Range range;
119  AdaptiveKDTree tool( &moab );
120  tool.find_all_trees( range );
121  if( range.size() != 1 )
122  {
123  fprintf( stderr, "%s : found %d kd-trees\n", argv[1], (int)range.size() );
124  delete[] values;
125  return 3;
126  }
127  EntityHandle root = range.front();
128 
130 
131  printf( "Running point queries..." );
132  fflush( stdout );
133  t = clock();
135  double pt[3];
136  for( unsigned long i = 0; i < count; ++i )
137  {
138  const double* coords = values + 3 * ( i % length );
139  if( query_triangles )
140  rval = tool.closest_triangle( root, coords, pt, leaf );
141  else
142  rval = tool.point_search( coords, leaf, 0.0, 0.0, NULL, &root );
143  if( MB_SUCCESS != rval )
144  {
145  fprintf( stderr, "Failure (ErrorCode == %d) for point %d (%f, %f, %f)\n", (int)rval,
146  (int)( count % length ), coords[0], coords[1], coords[2] );
147  }
148  else if( rfile )
149  {
150  if( query_triangles )
151  fprintf( rfile, "%f %f %f %f %f %f %lu\n", coords[0], coords[1], coords[2], pt[0], pt[1], pt[2],
152  (unsigned long)leaf );
153  else
154  fprintf( rfile, "%f %f %f %lu\n", coords[0], coords[1], coords[2], (unsigned long)leaf );
155  }
156  }
157  printf( "%0.2f seconds\n", ( clock() - t ) / (double)CLOCKS_PER_SEC );
158  fflush( stdout );
159  delete[] values;
160 
161  return 0;
162 }

References moab::AdaptiveKDTree::closest_triangle(), ErrorCode, moab::Tree::find_all_trees(), moab::Range::front(), leaf, length(), MB_SUCCESS, moab::AdaptiveKDTree::point_search(), print_file_stats(), moab::Range::size(), t, and usage().

◆ print_file_stats()

void print_file_stats ( Interface moab)

Definition at line 17 of file kd_tree_time.cpp.

18 {
19  ErrorCode rval;
20  int num_tri;
21  Range sets;
22  unsigned long long set_mem, set_am, tag_mem, tag_am;
23 
24  rval = moab.get_number_entities_by_type( 0, MBTRI, num_tri );
25  if( MB_SUCCESS != rval ) num_tri = -1;
26  rval = moab.get_entities_by_type( 0, MBENTITYSET, sets );
27  if( MB_SUCCESS != rval ) sets.clear();
28 
29  moab.estimated_memory_use( sets, 0, 0, &set_mem, &set_am, 0, 0, 0, 0, &tag_mem, &tag_am );
30  printf( "Triangles: %d\n", num_tri );
31  printf( "Sets: %lu\n", (unsigned long)sets.size() );
32  printf( "Set storage: %llu (%llu)\n", set_mem, set_am );
33  printf( "Tag storage: %llu (%llu)\n", tag_mem, tag_am );
34 }

References moab::Range::clear(), ErrorCode, MB_SUCCESS, MBENTITYSET, MBTRI, and moab::Range::size().

Referenced by main().

◆ usage()

void usage ( const char *  argv0)

Definition at line 11 of file kd_tree_time.cpp.

12 {
13  fprintf( stderr, "usage: %s [-t] [-d <result_file>] <tree_file> <point_file> [<count>]\n", argv0 );
14  exit( 1 );
15 }

References argv0.

Referenced by main().