This example demonstrates adaptive kD-tree for point location in hexahedral meshes. It shows how to load a hexahedral mesh, build an adaptive kD-tree for efficient spatial queries, find hexahedra containing specified points, and use geometric utilities for point-in-hex tests. The adaptive kD-tree provides efficient spatial partitioning for point location queries in large meshes.
#include <iostream>
#include <string>
{
std::string message;
std::string code;
code =
mb.get_error_string( err );
std::cerr << "Error: " << code << std::endl;
if( !message.empty() ) std::cerr << " " << message << std::endl;
}
#define CHKERR( err ) \
do \
{ \
if( moab::MB_SUCCESS != ( err ) ) \
{ \
print_error( mb, ( err ) ); \
std::cerr << "Unexpected failure at: " << __FILE__ << ":" << __LINE__ << std::endl; \
return 2; \
} \
} while( false )
{
std::string filename;
std::cout << "Hex mesh file name: ";
std::cin >> filename;
{
std::cerr << filename << ": file containd no hexahedra" << std::endl;
return 1;
}
MB_CHK_SET_ERR( tool.build_tree( elems, tree_root ),
"Failed to build kD-tree" );
for( ;; )
{
double point[3];
std::cout << "Point coordinates: ";
if( !( std::cin >> point[0] >> point[1] >> point[2] ) ) break;
MB_CHK_SET_ERR( tool.leaf_containing_point( tree_root, point, leaf ),
"Failed to find leaf containing point" );
if( 0 == hex )
std::cout << "Point is not contained in any hexahedron." << std::endl;
else
}
return 0;
}
{
int conn_len;
std::vector< moab::EntityHandle > hexes;
std::vector< moab::EntityHandle >::const_iterator i;
for( i = hexes.begin(); i != hexes.end(); ++i )
{
MB_CHK_SET_ERR(
mb.get_connectivity( *i, conn, conn_len ),
"Failed to get connectivity" );
MB_CHK_SET_ERR(
mb.get_coords( conn, 8, &coords[0][0] ),
"Failed to get coordinates" );
}
return 0;
}
{
int id =
mb.id_from_handle( hex );
int conn_len;
mb.get_connectivity( hex, conn, conn_len );
double coords[3 * 8];
mb.get_coords( conn, 8, coords );
std::cout << " Point is in hex " << id << " with corners: " << std::endl;
for( int i = 0; i < 8; ++i )
{
std::cout << " (" << coords[3 * i] << ", " << coords[3 * i + 1] << ", " << coords[3 * i + 2] << ")"
<< std::endl;
}
}