void Filler::treat_region(GRegion* gr){ int NumSmooth = CTX::instance()->mesh.smoothCrossField; std::cout << "NumSmooth = " << NumSmooth << std::endl ; if(NumSmooth && (gr->dim() == 3)){ double scale = gr->bounds().diag()*1e-2; Frame_field::initRegion(gr,NumSmooth); Frame_field::saveCrossField("cross0.pos",scale); Frame_field::smoothRegion(gr,NumSmooth); Frame_field::saveCrossField("cross1.pos",scale); } #if defined(HAVE_RTREE) unsigned int i; int j; int count; int limit; bool ok2; double x,y,z; SPoint3 point; Node *node,*individual,*parent; MVertex* vertex; MElement* element; MElementOctree* octree; deMeshGRegion deleter; Wrapper wrapper; GFace* gf; std::queue<Node*> fifo; std::vector<Node*> spawns; std::vector<Node*> garbage; std::vector<MVertex*> boundary_vertices; std::set<MVertex*> temp; std::list<GFace*> faces; std::map<MVertex*,int> limits; std::set<MVertex*>::iterator it; std::list<GFace*>::iterator it2; std::map<MVertex*,int>::iterator it3; RTree<Node*,double,3,double> rtree; Frame_field::init_region(gr); Size_field::init_region(gr); Size_field::solve(gr); octree = new MElementOctree(gr->model()); garbage.clear(); boundary_vertices.clear(); temp.clear(); new_vertices.clear(); faces.clear(); limits.clear(); faces = gr->faces(); for(it2=faces.begin();it2!=faces.end();it2++){ gf = *it2; limit = code(gf->tag()); for(i=0;i<gf->getNumMeshElements();i++){ element = gf->getMeshElement(i); for(j=0;j<element->getNumVertices();j++){ vertex = element->getVertex(j); temp.insert(vertex); limits.insert(std::pair<MVertex*,int>(vertex,limit)); } } } /*for(i=0;i<gr->getNumMeshElements();i++){ element = gr->getMeshElement(i); for(j=0;j<element->getNumVertices();j++){ vertex = element->getVertex(j); temp.insert(vertex); } }*/ for(it=temp.begin();it!=temp.end();it++){ if((*it)->onWhat()->dim()==0){ boundary_vertices.push_back(*it); } } for(it=temp.begin();it!=temp.end();it++){ if((*it)->onWhat()->dim()==1){ boundary_vertices.push_back(*it); } } for(it=temp.begin();it!=temp.end();it++){ if((*it)->onWhat()->dim()==2){ boundary_vertices.push_back(*it); } } /*for(it=temp.begin();it!=temp.end();it++){ if((*it)->onWhat()->dim()<3){ boundary_vertices.push_back(*it); } }*/ //std::ofstream file("nodes.pos"); //file << "View \"test\" {\n"; for(i=0;i<boundary_vertices.size();i++){ x = boundary_vertices[i]->x(); y = boundary_vertices[i]->y(); z = boundary_vertices[i]->z(); node = new Node(SPoint3(x,y,z)); compute_parameters(node,gr); node->set_layer(0); it3 = limits.find(boundary_vertices[i]); node->set_limit(it3->second); rtree.Insert(node->min,node->max,node); fifo.push(node); //print_node(node,file); } count = 1; while(!fifo.empty()){ parent = fifo.front(); fifo.pop(); garbage.push_back(parent); if(parent->get_limit()!=-1 && parent->get_layer()>=parent->get_limit()){ continue; } spawns.clear(); spawns.resize(6); for(i=0;i<6;i++){ spawns[i] = new Node(); } create_spawns(gr,octree,parent,spawns); for(i=0;i<6;i++){ ok2 = 0; individual = spawns[i]; point = individual->get_point(); x = point.x(); y = point.y(); z = point.z(); if(inside_domain(octree,x,y,z)){ compute_parameters(individual,gr); individual->set_layer(parent->get_layer()+1); individual->set_limit(parent->get_limit()); if(far_from_boundary(octree,individual)){ wrapper.set_ok(1); wrapper.set_individual(individual); wrapper.set_parent(parent); rtree.Search(individual->min,individual->max,rtree_callback,&wrapper); if(wrapper.get_ok()){ fifo.push(individual); rtree.Insert(individual->min,individual->max,individual); vertex = new MVertex(x,y,z,gr,0); new_vertices.push_back(vertex); ok2 = 1; //print_segment(individual->get_point(),parent->get_point(),file); } } } if(!ok2) delete individual; } if(count%100==0){ printf("%d\n",count); } count++; } //file << "};\n"; int option = CTX::instance()->mesh.algo3d; CTX::instance()->mesh.algo3d = ALGO_3D_DELAUNAY; deleter(gr); std::vector<GRegion*> regions; regions.push_back(gr); meshGRegion mesher(regions); //? mesher(gr); //? MeshDelaunayVolume(regions); CTX::instance()->mesh.algo3d = option; for(i=0;i<garbage.size();i++) delete garbage[i]; for(i=0;i<new_vertices.size();i++) delete new_vertices[i]; new_vertices.clear(); delete octree; rtree.RemoveAll(); Size_field::clear(); Frame_field::clear(); #endif }
int GModel::writeDIFF(const std::string &name, bool binary, bool saveAll, double scalingFactor) { if(binary){ Msg::Error("Binary DIFF output is not implemented"); return 0; } FILE *fp = Fopen(name.c_str(), binary ? "wb" : "w"); if(!fp){ Msg::Error("Unable to open file '%s'", name.c_str()); return 0; } if(noPhysicalGroups()) saveAll = true; // get the number of vertices and index the vertices in a continuous // sequence int numVertices = indexMeshVertices(saveAll); // tag the vertices according to which surface they belong to (Note // that we use a brute force approach here, so that we can deal with // models with incomplete topology. For example, when we merge 2 STL // triangulations we don't have the boundary information between the // faces, and the vertices would end up categorized on either one.) std::vector<std::list<int> > vertexTags(numVertices); std::list<int> boundaryIndicators; for(riter it = firstRegion(); it != lastRegion(); it++){ std::list<GFace*> faces = (*it)->faces(); for(std::list<GFace*>::iterator itf = faces.begin(); itf != faces.end(); itf++){ GFace *gf = *itf; boundaryIndicators.push_back(gf->tag()); for(unsigned int i = 0; i < gf->getNumMeshElements(); i++){ MElement *e = gf->getMeshElement(i); for(int j = 0; j < e->getNumVertices(); j++){ MVertex *v = e->getVertex(j); if(v->getIndex() > 0) vertexTags[v->getIndex() - 1].push_back(gf->tag()); } } } } boundaryIndicators.sort(); boundaryIndicators.unique(); for(int i = 0; i < numVertices; i++){ vertexTags[i].sort(); vertexTags[i].unique(); } // get all the entities in the model std::vector<GEntity*> entities; getEntities(entities); // find max dimension of mesh elements we need to save int dim = 0; for(unsigned int i = 0; i < entities.size(); i++) if(entities[i]->physicals.size() || saveAll) for(unsigned int j = 0; j < entities[i]->getNumMeshElements(); j++) dim = std::max(dim, entities[i]->getMeshElement(j)->getDim()); // loop over all elements we need to save int numElements = 0, maxNumNodesPerElement = 0; for(unsigned int i = 0; i < entities.size(); i++){ if(entities[i]->physicals.size() || saveAll){ for(unsigned int j = 0; j < entities[i]->getNumMeshElements(); j++){ MElement *e = entities[i]->getMeshElement(j); if(e->getStringForDIFF() && e->getDim() == dim){ numElements++; maxNumNodesPerElement = std::max(maxNumNodesPerElement, e->getNumVertices()); } } } } fprintf(fp, "\n\n"); fprintf(fp, " Finite element mesh (GridFE):\n\n"); fprintf(fp, " Number of space dim. = 3\n"); fprintf(fp, " Number of elements = %d\n", numElements); fprintf(fp, " Number of nodes = %d\n\n", numVertices); fprintf(fp, " All elements are of the same type : dpTRUE\n"); fprintf(fp, " Max number of nodes in an element: %d \n", maxNumNodesPerElement); fprintf(fp, " Only one subdomain : dpFALSE\n"); fprintf(fp, " Lattice data ? 0\n\n\n\n"); fprintf(fp, " %d Boundary indicators: ", (int)boundaryIndicators.size()); for(std::list<int>::iterator it = boundaryIndicators.begin(); it != boundaryIndicators.end(); it++) fprintf(fp, " %d", *it); fprintf(fp, "\n\n\n"); fprintf(fp," Nodal coordinates and nodal boundary indicators,\n"); fprintf(fp," the columns contain:\n"); fprintf(fp," - node number\n"); fprintf(fp," - coordinates\n"); fprintf(fp," - no of boundary indicators that are set (ON)\n"); fprintf(fp," - the boundary indicators that are set (ON) if any.\n"); fprintf(fp,"#\n"); // write mesh vertices for(unsigned int i = 0; i < entities.size(); i++){ for(unsigned int j = 0; j < entities[i]->mesh_vertices.size(); j++){ MVertex *v = entities[i]->mesh_vertices[j]; if(v->getIndex() > 0){ v->writeDIFF(fp, binary, scalingFactor); fprintf(fp, " [%d] ", (int)vertexTags[v->getIndex() - 1].size()); for(std::list<int>::iterator it = vertexTags[v->getIndex() - 1].begin(); it != vertexTags[v->getIndex() - 1].end(); it++) fprintf(fp," %d ", *it); fprintf(fp,"\n"); } } } fprintf(fp, "\n"); fprintf(fp, "\n"); fprintf(fp, " Element types and connectivity\n"); fprintf(fp, " the columns contain:\n"); fprintf(fp, " - element number\n"); fprintf(fp, " - element type\n"); fprintf(fp, " - subdomain number \n"); fprintf(fp, " - the global node numbers of the nodes in the element.\n"); fprintf(fp, "#\n"); // write mesh elements int num = 0; for(unsigned int i = 0; i < entities.size(); i++){ if(entities[i]->physicals.size() || saveAll){ for(unsigned int j = 0; j < entities[i]->getNumMeshElements(); j++){ MElement *e = entities[i]->getMeshElement(j); if(e->getStringForDIFF() && e->getDim() == dim) e->writeDIFF(fp, ++num, binary, entities[i]->tag()); } } } fprintf(fp, "\n"); fclose(fp); return 1; }
bool Filler3D::treat_region(GRegion *gr) { BGMManager::set_use_cross_field(true); bool use_vectorial_smoothness; bool use_fifo; string algo; // readValue("param.dat","SMOOTHNESSALGO",algo); algo.assign("SCALAR"); if (!algo.compare("SCALAR")){ use_vectorial_smoothness = false; use_fifo = false; } else if (!algo.compare("FIFO")){ use_vectorial_smoothness = false; use_fifo = true; } else{ cout << "unknown SMOOTHNESSALGO !" << endl; throw; } const bool debug=false; const bool export_stuff=true; double a; cout << "ENTERING POINTINSERTION3D" << endl; // acquire background mesh cout << "pointInsertion3D: recover BGM" << endl; a = Cpu(); frameFieldBackgroundMesh3D *bgm = dynamic_cast<frameFieldBackgroundMesh3D*>(BGMManager::get(gr)); time_smoothing += (Cpu() - a); if (!bgm){ cout << "pointInsertion3D:: BGM dynamic cast failed ! " << endl; throw; } // export BGM fields if(export_stuff){ cout << "pointInsertion3D: export size field " << endl; stringstream ss; ss << "bg3D_sizefield_" << gr->tag() << ".pos"; bgm->exportSizeField(ss.str()); cout << "pointInsertion3D : export crossfield " << endl; stringstream sscf; sscf << "bg3D_crossfield_" << gr->tag() << ".pos"; bgm->exportCrossField(sscf.str()); cout << "pointInsertion3D : export smoothness " << endl; stringstream sss; sss << "bg3D_smoothness_" << gr->tag() << ".pos"; bgm->exportSmoothness(sss.str()); if (use_vectorial_smoothness){ cout << "pointInsertion3D : export vectorial smoothness " << endl; stringstream ssvs; ssvs << "bg3D_vectorial_smoothness_" << gr->tag() << ".pos"; bgm->exportVectorialSmoothness(ssvs.str()); } } // ---------------- START FILLING NEW POINTS ---------------- cout << "pointInsertion3D : inserting points in region " << gr->tag() << endl; //ProfilerStart("/home/bernard/profile"); a = Cpu(); // ----- initialize fifo list ----- RTree<MVertex*,double,3,double> rtree; listOfPoints *fifo; if (use_fifo) fifo = new listOfPointsFifo(); else if (use_vectorial_smoothness) fifo = new listOfPointsVectorialSmoothness(); else fifo = new listOfPointsScalarSmoothness(); set<MVertex*> temp; vector<MVertex*> boundary_vertices; map<MVertex*,int> vert_priority; map<MVertex*,double> smoothness_forplot; MElement *element; MVertex *vertex; list<GFace*> faces = gr->faces(); for(list<GFace*>::iterator it=faces.begin();it!=faces.end();it++){// for all faces GFace *gf = *it; // int limit = code_kesskessai(gf->tag()); for(unsigned int i=0;i<gf->getNumMeshElements();i++){ element = gf->getMeshElement(i); for(int j=0;j<element->getNumVertices();j++){// for all vertices vertex = element->getVertex(j); temp.insert(vertex); // limits.insert(make_pair(vertex,limit)); } } } int geodim; for(set<MVertex*>::iterator it=temp.begin();it!=temp.end();it++){ geodim = (*it)->onWhat()->dim(); if ((geodim==0) || (geodim==1) || (geodim==2)) boundary_vertices.push_back(*it); } double min[3],max[3],x,y,z,h; for(unsigned int i=0;i<boundary_vertices.size();i++){ x = boundary_vertices[i]->x(); y = boundary_vertices[i]->y(); z = boundary_vertices[i]->z(); // "on boundary since working on boundary_vertices ... MVertex *closest = bgm->get_nearest_neighbor_on_boundary(boundary_vertices[i]); h = bgm->size(closest);// get approximate size, closest vertex, faster ?! fill_min_max(x,y,z,h,min,max); rtree.Insert(min,max,boundary_vertices[i]); if (!use_vectorial_smoothness){ smoothness_vertex_pair *svp = new smoothness_vertex_pair(); svp->v = boundary_vertices[i]; svp->rank = bgm->get_smoothness(x,y,z); svp->dir = 0; svp->layer = 0; svp->size = h; bgm->eval_approximate_crossfield(closest, svp->cf); fifo->insert(svp); if (debug){ smoothness_forplot[svp->v] = svp->rank; } } else{ STensor3 temp; bgm->eval_approximate_crossfield(closest, temp); for (int idir=0;idir<3;idir++){ smoothness_vertex_pair *svp = new smoothness_vertex_pair(); svp->v = boundary_vertices[i]; svp->rank = bgm->get_vectorial_smoothness(idir,x,y,z); svp->dir = idir; svp->layer = 0; svp->size = h; svp->cf = temp; for (int k=0;k<3;k++) svp->direction(k) = temp(k,idir); // cout << "fifo size=" << fifo->size() << " inserting " ; fifo->insert(svp); // cout << " -> fifo size=" << fifo->size() << endl; } } } // TODO: si fifo était list of *PTR -> pas de copies, gain temps ? Wrapper3D wrapper; wrapper.set_bgm(bgm); MVertex *parent,*individual; new_vertices.clear(); bool spawn_created; int priority_counter=0; STensor3 crossfield; int parent_layer; while(!fifo->empty()){ parent = fifo->get_first_vertex(); // parent_limit = fifo->get_first_limit(); parent_layer = fifo->get_first_layer(); // if(parent_limit!=-1 && parent_layer>=parent_limit()){ // continue; // } vector<MVertex*> spawns; if (!use_vectorial_smoothness){ spawns.resize(6); computeSixNeighbors(bgm,parent,spawns,fifo->get_first_crossfield(), fifo->get_first_size()); } else{ spawns.resize(2); computeTwoNeighbors(bgm,parent,spawns,fifo->get_first_direction(), fifo->get_first_size()); } fifo->erase_first(); // cout << "while, fifo->size()=" << fifo->size() << " parent=(" << // parent->x() << "," << parent->y() << "," << parent->z() << ")" << // endl; for(unsigned int i=0;i<spawns.size();i++){ spawn_created = false; individual = spawns[i]; x = individual->x(); y = individual->y(); z = individual->z(); // cout << " working on candidate " << "(" << individual->x() << "," // << individual->y() << "," << individual->z() << ")" << endl; if(bgm->inDomain(x,y,z)){ // cout << " spawn " << i << " in domain" << endl; MVertex *closest = bgm->get_nearest_neighbor(individual); h = bgm->size(closest);// get approximate size, closest vertex, faster ?! if(far_from_boundary_3D(bgm,individual,h)){ // cout << " spawn " << i << " far from bnd" << endl; bgm->eval_approximate_crossfield(closest, crossfield); wrapper.set_ok(true); wrapper.set_individual(individual); wrapper.set_parent(parent); wrapper.set_size(&h); wrapper.set_crossfield(&crossfield); fill_min_max(x,y,z,h,min,max); rtree.Search(min,max,rtree_callback_3D,&wrapper); if(wrapper.get_ok()){ // cout << " spawn " << i << " wrapper OK" << endl; if (!use_vectorial_smoothness){ smoothness_vertex_pair *svp = new smoothness_vertex_pair(); svp->v = individual; svp->rank=bgm->get_smoothness(individual->x(),individual->y(),individual->z()); svp->dir = 0; svp->layer = parent_layer+1; svp->size = h; svp->cf = crossfield; fifo->insert(svp); if (debug){ smoothness_forplot[svp->v] = svp->rank; vert_priority[individual] = priority_counter++; } } else{ if (debug) vert_priority[individual] = priority_counter++; for (int idir=0;idir<3;idir++){ smoothness_vertex_pair *svp = new smoothness_vertex_pair(); svp->v = individual; svp->rank = bgm->get_vectorial_smoothness(idir,x,y,z); svp->dir = idir; svp->layer = parent_layer+1; svp->size = h; for (int k=0;k<3;k++) svp->direction(k) = crossfield(k,idir); svp->cf = crossfield; fifo->insert(svp); } } rtree.Insert(min,max,individual); new_vertices.push_back(individual); spawn_created = true; } } } if(!spawn_created){ delete individual; } }// end loop on spawns } //ProfilerStop(); time_insert_points += (Cpu() - a); // --- output --- if (debug){ stringstream ss; ss << "priority_3D_" << gr->tag() << ".pos"; print_nodal_info(ss.str().c_str(),vert_priority); ss.clear(); stringstream sss; sss << "smoothness_3D_" << gr->tag() << ".pos"; print_nodal_info(sss.str().c_str(),smoothness_forplot); sss.clear(); } // ------- meshing using new points cout << "tets in gr before= " << gr->tetrahedra.size() << endl; cout << "nb new vertices= " << new_vertices.size() << endl; a=Cpu(); int option = CTX::instance()->mesh.algo3d; CTX::instance()->mesh.algo3d = ALGO_3D_DELAUNAY; deMeshGRegion deleter; deleter(gr); std::vector<GRegion*> regions; regions.push_back(gr); meshGRegion mesher(regions); //? mesher(gr); //? MeshDelaunayVolume(regions); time_meshing += (Cpu() - a); cout << "tets in gr after= " << gr->tetrahedra.size() << endl; cout << "gr tag=" << gr->tag() << endl; CTX::instance()->mesh.algo3d = option; delete fifo; for(unsigned int i=0;i<new_vertices.size();i++) delete new_vertices[i]; new_vertices.clear(); rtree.RemoveAll(); return true; }