Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
KDTree.cpp File Reference
#include "moab/Core.hpp"
#include "moab/AdaptiveKDTree.hpp"
#include "moab/Range.hpp"
#include "moab/GeomUtil.hpp"
#include <iostream>
#include <string>
+ Include dependency graph for KDTree.cpp:

Go to the source code of this file.

Macros

#define CHKERR(err)
 

Functions

void print_error (moab::Interface &mb, moab::ErrorCode err)
 
moab::EntityHandle hex_containing_point (moab::Interface &mb, moab::EntityHandle set, const double point[3])
 
void print_hex (moab::Interface &mb, moab::EntityHandle hex)
 
int main ()
 

Variables

const double EPSILON = 1e-6
 

Macro Definition Documentation

◆ CHKERR

#define CHKERR (   err)
Value:
do \
{ \
if( moab::MB_SUCCESS != ( err ) ) \
{ \
print_error( mb, ( err ) ); \
std::cerr << "Unexpected failure at: " << __FILE__ << ":" << __LINE__ << std::endl; \
return 2; \
} \
} while( false )

Definition at line 31 of file KDTree.cpp.

Function Documentation

◆ hex_containing_point()

moab::EntityHandle hex_containing_point ( moab::Interface mb,
moab::EntityHandle  set,
const double  point[3] 
)
Examples
KDTree.cpp.

Definition at line 98 of file KDTree.cpp.

99 {
100  moab::ErrorCode rval;
101  moab::CartVect pt( point ); // input location
102  moab::CartVect coords[8]; // coordinates of corners of hexahedron
103  const moab::EntityHandle* conn; // hex connectivity
104  int conn_len;
105 
106  // Get hexes in leaf
107  std::vector< moab::EntityHandle > hexes;
108  MB_CHK_SET_ERR( mb.get_entities_by_type( set, moab::MBHEX, hexes ), "Failed to get hexahedra from set" );
109 
110  // Check which hex the point is in
111  std::vector< moab::EntityHandle >::const_iterator i;
112  for( i = hexes.begin(); i != hexes.end(); ++i )
113  {
114  MB_CHK_SET_ERR( mb.get_connectivity( *i, conn, conn_len ), "Failed to get connectivity" );
115  MB_CHK_SET_ERR( mb.get_coords( conn, 8, &coords[0][0] ), "Failed to get coordinates" );
116  if( moab::GeomUtil::point_in_trilinear_hex( coords, pt, EPSILON ) ) return *i;
117  }
118 
119  // Return 0 if no hex contains point.
120  return 0;
121 }

References EPSILON, ErrorCode, mb, MB_CHK_SET_ERR, MBHEX, and moab::GeomUtil::point_in_trilinear_hex().

Referenced by main().

◆ main()

int main ( )
Examples
KDTree.cpp.

Definition at line 50 of file KDTree.cpp.

51 {
52  // Ask user for file to read
53  std::string filename;
54  std::cout << "Hex mesh file name: ";
55  std::cin >> filename;
56 
57  // Read file into MOAB instance
58  moab::ErrorCode rval;
61  MB_CHK_SET_ERR( mb.load_file( filename.c_str() ), "File load failed" );
62 
63  // Get all hex elemeents
64  moab::Range elems;
65  MB_CHK_SET_ERR( mb.get_entities_by_type( 0, moab::MBHEX, elems ), "Failed to get hexahedra" );
66  if( elems.empty() )
67  {
68  std::cerr << filename << ": file containd no hexahedra" << std::endl;
69  return 1;
70  }
71 
72  // Build a kD-tree from hex elements
73  moab::EntityHandle tree_root;
74  moab::AdaptiveKDTree tool( &mb );
75  MB_CHK_SET_ERR( tool.build_tree( elems, tree_root ), "Failed to build kD-tree" );
76 
77  // Loop forever (or until EOF), asking user for a point
78  // to query and printing the hex element containing that
79  // point.
80  for( ;; )
81  {
82  double point[3];
83  std::cout << "Point coordinates: ";
84  if( !( std::cin >> point[0] >> point[1] >> point[2] ) ) break;
85 
86  moab::EntityHandle leaf;
87  MB_CHK_SET_ERR( tool.leaf_containing_point( tree_root, point, leaf ), "Failed to find leaf containing point" );
88  moab::EntityHandle hex = hex_containing_point( mb, leaf, point );
89  if( 0 == hex )
90  std::cout << "Point is not contained in any hexahedron." << std::endl;
91  else
92  print_hex( mb, hex );
93  }
94 
95  return 0;
96 }

References moab::AdaptiveKDTree::build_tree(), moab::Range::empty(), ErrorCode, hex_containing_point(), mb, MB_CHK_SET_ERR, MBHEX, and print_hex().

◆ print_error()

void print_error ( moab::Interface mb,
moab::ErrorCode  err 
)
Examples
KDTree.cpp.

Definition at line 20 of file KDTree.cpp.

21 {
22  std::string message;
23  std::string code;
24  if( moab::MB_SUCCESS != mb.get_last_error( message ) ) message.clear();
25  code = mb.get_error_string( err );
26  std::cerr << "Error: " << code << std::endl;
27  if( !message.empty() ) std::cerr << " " << message << std::endl;
28 }

References mb, and MB_SUCCESS.

Referenced by moab::FileTokenizer::match_token().

◆ print_hex()

void print_hex ( moab::Interface mb,
moab::EntityHandle  hex 
)
Examples
KDTree.cpp.

Definition at line 123 of file KDTree.cpp.

124 {
125  // Get MOAB's internal ID for hex element
126  int id = mb.id_from_handle( hex );
127 
128  // Get vertex handles for hex corners
129  const moab::EntityHandle* conn; // hex connectivity
130  int conn_len;
131  mb.get_connectivity( hex, conn, conn_len );
132 
133  // Get coordinates of vertices
134  double coords[3 * 8];
135  mb.get_coords( conn, 8, coords );
136 
137  // Print
138  std::cout << " Point is in hex " << id << " with corners: " << std::endl;
139  for( int i = 0; i < 8; ++i )
140  {
141  std::cout << " (" << coords[3 * i] << ", " << coords[3 * i + 1] << ", " << coords[3 * i + 2] << ")"
142  << std::endl;
143  }
144 }

References mb.

Referenced by main().

Variable Documentation

◆ EPSILON