@@ -47,35 +47,13 @@ class MBDirectAccess {
4747
4848 // ! \brief Determine the index of an element in the managed data
4949 inline size_t element_index (EntityHandle element) {
50- size_t element_index = 0 ;
51- auto fe = element_data_.first_elements .begin ();
52- while (true ) {
53- if (element - fe->first < fe->second ) { break ; }
54- element_index += fe->second ;
55- fe++;
56- if (fe == element_data_.first_elements .end ()) {
57- throw std::runtime_error (" Element not found in MBDirectAccess::element_index" );
58- }
59- }
60- element_index += element - fe->first ;
61- return element_index;
50+ return element_data_.entity_range .index (element);
6251 }
6352
6453 // ! \brief Determine the element handle of an element based on its index in
6554 // ! the managed data
6655 inline EntityHandle element_handle (size_t index) {
67- size_t running_index = 0 ;
68- auto fe = element_data_.first_elements .begin ();
69- while (true ) {
70- if (index < running_index + fe->second ) {
71- return fe->first + (index - running_index);
72- }
73- running_index += fe->second ;
74- fe++;
75- if (fe == element_data_.first_elements .end ()) {
76- throw std::runtime_error (" Index out of range in MBDirectAccess::element_handle" );
77- }
78- }
56+ return element_data_.entity_range [index];
7957 }
8058
8159 // ! \brief Get the coordinates of a triangle as XDG Vertices
@@ -102,33 +80,11 @@ class MBDirectAccess {
10280 }
10381
10482 int vertex_index (EntityHandle vertex) {
105- int vertex_index = 0 ;
106- auto fe = vertex_data_.first_vertices .begin ();
107- while (true ) {
108- if (vertex - fe->first < fe->second ) { break ; }
109- vertex_index += fe->second ;
110- fe++;
111- if (fe == vertex_data_.first_vertices .end ()) {
112- throw std::runtime_error (" Vertex not found in MBDirectAccess::vertex_index" );
113- }
114- }
115- vertex_index += vertex - fe->first ;
116- return vertex_index;
83+ return vertex_data_.vertex_range .index (vertex);
11784 }
11885
11986 EntityHandle vertex_handle (int vertex_index) {
120- int running_index = 0 ;
121- auto fe = vertex_data_.first_vertices .begin ();
122- while (true ) {
123- if (vertex_index < running_index + fe->second ) {
124- return fe->first + (vertex_index - running_index);
125- }
126- running_index += fe->second ;
127- fe++;
128- if (fe == vertex_data_.first_vertices .end ()) {
129- throw std::runtime_error (" Index out of range in MBDirectAccess::vertex_handle" );
130- }
131- }
87+ return vertex_data_.vertex_range [vertex_index];
13288 }
13389
13490 // ! \brief Get the adjacent element
@@ -233,33 +189,33 @@ class MBDirectAccess {
233189 int element_stride {-1 }; // !< Number of vertices used by each element
234190 std::vector<std::pair<EntityHandle, size_t >> first_elements; // !< Pairs of first element and length pairs for contiguous blocks of memory
235191 std::vector<const EntityHandle*> vconn; // !< Storage array(s) for the connectivity array
192+ moab::Range entity_range; // !< Range of entities managed
236193
237194 void setup (Interface * mbi) {
238195 ErrorCode rval;
239196
240197 // setup face connectivity data
241- Range faces;
242- rval = mbi->get_entities_by_type (0 , entity_type, faces, true );
243- MB_CHK_SET_ERR_CONT (rval, " Failed to get all elements of dimension 2 (faces)" );
244- num_entities = faces.size ();
198+ rval = mbi->get_entities_by_type (0 , entity_type, entity_range, true );
199+ MB_CHK_SET_ERR_CONT (rval, " Failed to get all elements for the given entity type" );
200+ num_entities = entity_range.size ();
245201
246202 // only supporting triangle elements for now
247- if (!faces .all_of_type (entity_type)) { throw std::runtime_error (" Not all 2D elements are triangles" ); }
203+ if (!entity_range .all_of_type (entity_type)) { throw std::runtime_error (" Not all 2D elements are triangles" ); }
248204
249- moab::Range::iterator faces_it = faces .begin ();
250- while (faces_it != faces .end ()) {
205+ moab::Range::iterator entity_it = entity_range .begin ();
206+ while (entity_it != entity_range .end ()) {
251207 // set connectivity pointer, element stride and the number of elements
252208 EntityHandle* conntmp;
253209 int n_elements;
254- rval = mbi->connect_iterate (faces_it, faces .end (), conntmp, element_stride, n_elements);
210+ rval = mbi->connect_iterate (entity_it, entity_range .end (), conntmp, element_stride, n_elements);
255211 MB_CHK_SET_ERR_CONT (rval, " Failed to get direct access to triangle elements" );
256212
257213 // set const pointers for the connectivity array and add first element/length pair to the set of first elements
258214 vconn.push_back (conntmp);
259- first_elements.push_back ({*faces_it , n_elements});
215+ first_elements.push_back ({*entity_it , n_elements});
260216
261217 // move iterator forward by the number of triangles in this contiguous memory block
262- faces_it += n_elements;
218+ entity_it += n_elements;
263219 }
264220 }
265221
@@ -295,19 +251,18 @@ class MBDirectAccess {
295251 void setup (Interface* mbi) {
296252 ErrorCode rval;
297253 // setup vertices
298- Range verts;
299- rval = mbi->get_entities_by_dimension (0 , 0 , verts, true );
254+ rval = mbi->get_entities_by_dimension (0 , 0 , vertex_range, true );
300255 MB_CHK_SET_ERR_CONT (rval, " Failed to get all elements of dimension 0 (vertices)" );
301- num_vertices = verts .size ();
256+ num_vertices = vertex_range .size ();
302257
303- moab::Range::iterator verts_it = verts .begin ();
304- while (verts_it != verts .end ()) {
258+ moab::Range::iterator verts_it = vertex_range .begin ();
259+ while (verts_it != vertex_range .end ()) {
305260 // set vertex coordinate pointers
306261 double * xtmp;
307262 double * ytmp;
308263 double * ztmp;
309264 int n_vertices;
310- rval = mbi->coords_iterate (verts_it, verts .end (), xtmp, ytmp, ztmp, n_vertices);
265+ rval = mbi->coords_iterate (verts_it, vertex_range .end (), xtmp, ytmp, ztmp, n_vertices);
311266 MB_CHK_SET_ERR_CONT (rval, " Failed to get direct access to vertex elements" );
312267
313268 // add the vertex coordinate arrays to their corresponding vector of array pointers
@@ -351,6 +306,7 @@ class MBDirectAccess {
351306 std::vector<const double *> ty; // !< Storage array(s) for vertex y coordinates
352307 std::vector<const double *> tz; // !< Storage array(s) for vertex z coordinates
353308 std::vector<std::pair<EntityHandle, size_t >> first_vertices; // !< Pairs of first vertex and length pairs for contiguous blocks of memory
309+ moab::Range vertex_range; // !< Range of entities managed
354310 };
355311
356312 ConnectivityData face_data_;
0 commit comments