/*! * \brief Build the tree. */ void Octree::buildTree() { d_root_node->node_set = d_mesh_set; getEntSetBox( d_root_node->node_set, d_root_node->bounding_box ); buildTreeNode( d_root_node ); }
/*! * \brief Build a tree node. */ void Octree::buildTreeNode( RCP_Node node ) { int error = 0; // Create the children. for ( int i = 0; i < 8; ++i ) { node->children[i] = Teuchos::rcp( new OctreeNode ); iMesh_createEntSet( d_mesh, 1, &(node->children[i]->node_set), &error ); assert( iBase_SUCCESS == error ); } // Create the new bounding boxes and assign them to the children. sliceBox( node ); // Add the elements in the parent set to the child sets. std::vector<iBase_EntityHandle> root_list; int node_elements_allocated = 0; int node_elements_size = 0; iBase_EntityHandle *node_elements; iMesh_getEntities( d_mesh, node->node_set, d_entity_type, d_entity_topology, &node_elements, &node_elements_allocated, &node_elements_size, &error ); assert( iBase_SUCCESS == error ); bool element_found = false; for (int n = 0; n < node_elements_size; ++n ) { element_found = false; for ( int m = 0; m < 8; ++m ) { if ( !element_found ) { if ( isEntInBox( node->children[m]->bounding_box, node_elements[n] ) ) { iMesh_addEntToSet( d_mesh, node_elements[n], node->children[m]->node_set, &error ); assert( iBase_SUCCESS == error ); if ( node != d_root_node ) { iMesh_rmvEntFromSet( d_mesh, node_elements[n], node->node_set, &error ); assert( iBase_SUCCESS == error ); } element_found = true; } } } if ( !element_found ) { root_list.push_back( node_elements[n] ); } } free( node_elements ); // Special case for the root node. if ( node == d_root_node ) { iBase_EntitySetHandle new_root_set; iMesh_createEntSet( d_mesh, 1, &new_root_set, &error ); assert( iBase_SUCCESS == error ); iMesh_addEntArrToSet( d_mesh, &root_list[0], (int) root_list.size(), new_root_set, &error ); assert( iBase_SUCCESS == error ); node->node_set = new_root_set; } // See if we have any child entities. Teuchos::Tuple<int,8> num_child_ents; int total_child_ents = 0; for (int i = 0; i < 8; ++i ) { iMesh_getNumOfTopo( d_mesh, node->children[i]->node_set, d_entity_topology, &num_child_ents[i], &error ); assert( iBase_SUCCESS == error ); total_child_ents += num_child_ents[i]; } // Recurse. if ( total_child_ents == 0 ) { node->is_leaf = true; } else { for (int i = 0; i < 8; ++i) { if ( num_child_ents[i] == 0 ) { node->children[i]->is_leaf = true; } else { buildTreeNode( node->children[i] ); } } } }
TreeNode *loadParseTree(char **tree, int *par_num, int *ID, char *tempstring) { char *root_symbol, *leaf; int par_num_child=0; TreeNode *root; if(**tree!='(') { leaf=*tree; while(**tree!=')' && (*tree-leaf) < strlen(leaf))(*tree)++; if(**tree!=')'){ printf("\n\n>>>>>ERROR: malformed tree: %s\n\n",tempstring); if(strcmp(*tree,"")!=0)printf(">>>>>The problem occurred at:\n%s\n\n",*tree); else printf(">>>>>The problem occurred at the end of the tree (closing parenthesis?)\n"); fflush(stdout); exit(-1); } (**tree)=0; // remove right parenthesis do { (*tree)++; (*par_num)--; // decrease the number of parent. } // remove all of the last parenthesis while(**tree==')'); return buildTreeNode(leaf,(*ID)++); } else (*par_num)++; // count open parenthesis if((strstr(*tree," ")!=NULL) && strstr(*tree,")") - *tree > strstr(*tree," ") - *tree) root_symbol=strtok(*tree," "); // get the root symbol else { root_symbol=strtok(*tree,")"); // get the root symbol (*par_num)--; } (*tree)+=strlen(root_symbol)+1; while(**tree==')'){(*tree)++;(*par_num)--;} // remove all of the last parenthesis root_symbol++; // remove left parenthesis root=buildTreeNode(root_symbol,*ID); //printf("\nroot symbol %s\n",root_symbol); fflush(stdout); //printf("\nbefore while tree %s, %d\n",*tree,*par_num); fflush(stdout); while((*par_num)>0){ root->pChild[root->iNoOfChildren]=loadParseTree(tree,&par_num_child,ID,tempstring); root->iNoOfChildren++; (*par_num)+=par_num_child; } root->nodeID=(*ID)++; if(*par_num > 0){ printf("\n\n>>>>>ERROR: malformed tree: %s\n\n",tempstring); if(strcmp(*tree,"")!=0)printf(">>>>>The problem occurred at:\n%s\n\n",*tree); else printf(">>>>>The problem occurred at the end of the tree (closing parenthesis?)\n"); fflush(stdout); exit(-1); } return root; }