diff --git a/Common/gmsh.cpp b/Common/gmsh.cpp index bf8f4e14461f62bc431f54f5764a223c0b5e76df..474cb55a088db0951755d97739853aae1fe4763f 100644 --- a/Common/gmsh.cpp +++ b/Common/gmsh.cpp @@ -3,11 +3,22 @@ // See the LICENSE.txt file for license information. Please report all // bugs and problems to the public mailing list <gmsh@onelab.info>. -#include "gmsh.h" #include "GmshGlobal.h" #include "GModel.h" #include "GModelIO_GEO.h" #include "GModelIO_OCC.h" +#include "GVertex.h" +#include "GEdge.h" +#include "GFace.h" +#include "GRegion.h" +#include "MPoint.h" +#include "MLine.h" +#include "MTriangle.h" +#include "MQuadrangle.h" +#include "MTetrahedron.h" +#include "MHexahedron.h" +#include "MPrism.h" +#include "MPyramid.h" // gmsh @@ -128,24 +139,24 @@ int gmshModelDestroy() return 1; } -int gmshModelGetElementaryTags(int dim, std::vector<int> &tags) +int gmshModelGetEntities(std::vector<std::pair<int, int> > &dimTags) { std::vector<GEntity*> entities; - GModel::current()->getEntities(entities, dim); - tags.clear(); + GModel::current()->getEntities(entities); for(unsigned int i = 0; i < entities.size(); i++) - tags.push_back(entities[i]->tag()); + dimTags.push_back(std::pair<int, int>(entities[i]->dim(), entities[i]->tag())); return 0; } -int gmshModelGetPhysicalTags(int dim, std::vector<int> &tags) +int gmshModelGetPhysicalGroups(std::vector<std::pair<int, int> > &dimTags) { - std::map<int, std::vector<GEntity*> > groups; - GModel::current()->getPhysicalGroups(dim, groups); - tags.clear(); - for(std::map<int, std::vector<GEntity*> >::iterator it = groups.begin(); - it != groups.end(); it++) - tags.push_back(it->first); + std::map<int, std::vector<GEntity*> > groups[4]; + GModel::current()->getPhysicalGroups(groups); + for(int dim = 0; dim < 4; dim++){ + for(std::map<int, std::vector<GEntity*> >::iterator it = groups[dim].begin(); + it != groups[dim].end(); it++) + dimTags.push_back(std::pair<int, int>(dim, it->first)); + } return 0; } @@ -160,13 +171,12 @@ int gmshModelAddPhysicalGroup(int dim, int tag, const std::vector<int> &tags) return 1; } -int gmshModelGetElementaryTagsForPhysicalGroup(int dim, int tag, - std::vector<int> &tags) +int gmshModelGetEntitiesForPhysicalGroup(int dim, int tag, + std::vector<int> &tags) { std::map<int, std::vector<GEntity*> > groups; GModel::current()->getPhysicalGroups(dim, groups); std::map<int, std::vector<GEntity*> >::iterator it = groups.find(tag); - tags.clear(); if(it != groups.end()){ for(unsigned j = 0; j < it->second.size(); j++) tags.push_back(it->second[j]->tag()); @@ -198,9 +208,9 @@ int gmshModelGetVertexCoordinates(int tag, double &x, double &y, double &z) return 1; } -int gmshModelGetBoundaryTags(const std::vector<std::pair<int, int> > &inDimTags, - std::vector<std::pair<int, int> > &outDimTags, - bool combined, bool oriented, bool recursive) +int gmshModelGetBoundary(const std::vector<std::pair<int, int> > &inDimTags, + std::vector<std::pair<int, int> > &outDimTags, + bool combined, bool oriented, bool recursive) { bool r = GModel::current()->getBoundaryTags(inDimTags, outDimTags, combined, oriented, recursive); @@ -208,15 +218,14 @@ int gmshModelGetBoundaryTags(const std::vector<std::pair<int, int> > &inDimTags, return 1; } -int gmshModelGetElementaryTagsInBoundingBox(int dim, - double x1, double y1, double z1, - double x2, double y2, double z2, - std::vector<int> &tags) +int gmshModelGetEntitiesInBoundingBox(int dim, + double x1, double y1, double z1, + double x2, double y2, double z2, + std::vector<int> &tags) { SBoundingBox3d box(x1, y1, z1, x2, y2, z2); std::vector<GEntity*> entities; GModel::current()->getEntitiesInBox(entities, box, dim); - tags.clear(); for(unsigned int i = 0; i < entities.size(); i++) tags.push_back(entities[i]->tag()); return 0; @@ -256,27 +265,78 @@ int gmshModelMesh(int dim) } int gmshModelGetMeshVertices(int dim, int tag, std::vector<int> &vertexTags, - std::vector<double> &coords, - std::vector<double> ¶metricCoords) + std::vector<double> &coords) { + GEntity *ge = GModel::current()->getEntityByTag(dim, tag); + if(!ge) return 1; + for(unsigned int i = 0; i < ge->mesh_vertices.size(); i++){ + MVertex *v = ge->mesh_vertices[i]; + vertexTags.push_back(v->getNum()); + coords.push_back(v->x()); + coords.push_back(v->y()); + coords.push_back(v->z()); + } return 0; } +template<class T> +static void addElementInfo(std::vector<T*> &ele, + std::vector<int> &elementType, + std::vector<std::vector<int> > &elementTags, + std::vector<std::vector<int> > &vertexTags) +{ + if(ele.empty()) return; + elementType.push_back(ele.front()->getTypeForMSH()); + elementTags.push_back(std::vector<int>()); + vertexTags.push_back(std::vector<int>()); + for(unsigned int i = 0; i < ele.size(); i++){ + elementTags.back().push_back(ele[i]->getNum()); + for(unsigned int j = 0; j < ele[i]->getNumVertices(); j++){ + vertexTags.back().push_back(ele[i]->getVertex(j)->getNum()); + } + } +} + int gmshModelGetMeshElements(int dim, int tag, std::vector<int> &types, std::vector<std::vector<int> > &elementTags, std::vector<std::vector<int> > &vertexTags) { + GEntity *ge = GModel::current()->getEntityByTag(dim, tag); + if(!ge) return 1; + switch(dim){ + case 0: { + GVertex *v = static_cast<GVertex*>(ge); + addElementInfo(v->points, types, elementTags, vertexTags); + break; } + case 1: { + GEdge *e = static_cast<GEdge*>(ge); + addElementInfo(e->lines, types, elementTags, vertexTags); + break; } + case 2: { + GFace *f = static_cast<GFace*>(ge); + addElementInfo(f->triangles, types, elementTags, vertexTags); + addElementInfo(f->quadrangles, types, elementTags, vertexTags); + break; } + case 3: { + GRegion *r = static_cast<GRegion*>(ge); + addElementInfo(r->tetrahedra, types, elementTags, vertexTags); + addElementInfo(r->hexahedra, types, elementTags, vertexTags); + addElementInfo(r->prisms, types, elementTags, vertexTags); + addElementInfo(r->pyramids, types, elementTags, vertexTags); + break; } + } return 0; } int gmshModelSetMeshSize(int dim, int tag, double size) { - return 0; -} - -int gmshModelSetCompound(int dim, const std::vector<int> &tags) -{ - return 0; + if(dim) return 2; + GVertex *gv = GModel::current()->getVertexByTag(tag); + if(gv){ + gv->setPrescribedMeshSizeAtVertex(size); + return 0; + } + return 1; } int gmshModelSetTransfiniteLine(int tag, int nPoints, int type, double coef) diff --git a/Common/gmsh.h b/Common/gmsh.h index b0c660d8421eedb8f87ff12c937f239f6d515a6c..976651a9e8cd0891511000fed0df7cae29452c01 100644 --- a/Common/gmsh.h +++ b/Common/gmsh.h @@ -29,7 +29,7 @@ #endif // gmsh -GMSH_API gmshInitialize(int argc, char **argv); +GMSH_API gmshInitialize(int argc=0, char **argv=0); GMSH_API gmshFinalize(); GMSH_API gmshOpen(const std::string &fileName); GMSH_API gmshMerge(const std::string &fileName); @@ -46,34 +46,31 @@ GMSH_API gmshOptionGetString(const std::string &name, std::string &value); GMSH_API gmshModelCreate(const std::string &name); GMSH_API gmshModelSetCurrent(const std::string &name); GMSH_API gmshModelDestroy(); -GMSH_API gmshModelGetElementaryTags(int dim, std::vector<int> &tags); -GMSH_API gmshModelGetPhysicalTags(int dim, std::vector<int> &tags); +GMSH_API gmshModelGetEntities(std::vector<std::pair<int, int> > &dimTags); +GMSH_API gmshModelGetPhysicalGroups(std::vector<std::pair<int, int> > &dimTags); GMSH_API gmshModelAddPhysicalGroup(int dim, int tag, const std::vector<int> &tags); -GMSH_API gmshModelGetElementaryTagsForPhysicalGroup(int dim, int tag, - std::vector<int> &tags); +GMSH_API gmshModelGetEntitiesForPhysicalGroup(int dim, int tag, std::vector<int> &tags); GMSH_API gmshModelSetPhysicalName(int dim, int tag, const std::string &name); GMSH_API gmshModelGetPhysicalName(int dim, int tag, std::string &name); GMSH_API gmshModelGetVertexCoordinates(int tag, std::vector<double> &coord); -GMSH_API gmshModelGetBoundaryTags(const std::vector<std::pair<int, int> > &inDimTags, - std::vector<std::pair<int, int> > &outDimTags, - bool combined, bool oriented, bool recursive); -GMSH_API gmshModelGetElementaryTagsInBoundingBox(int dim, - double x1, double y1, double z1, - double x2, double y2, double z2, - std::vector<int> &tags); -GMSH_API gmshModelGetBoundingBox(int dim, int tag, double &x1, double &y1, double &z1, - double &x2, double &y2, double &z2); +GMSH_API gmshModelGetBoundary(const std::vector<std::pair<int, int> > &inDimTags, + std::vector<std::pair<int, int> > &outDimTags, + bool combined=true, bool oriented=true, + bool recursive=false); +GMSH_API gmshModelGetEntitiesInBoundingBox(int dim, double x1, double y1, double z1, + double x2, double y2, double z2, + std::vector<int> &tags); +GMSH_API gmshModelGetBoundingBox(int dim, int tag, double &x1, double &y1, + double &z1, double &x2, double &y2, double &z2); GMSH_API gmshModelRemove(const std::vector<std::pair<int, int> > &dimTags, bool recursive=false); GMSH_API gmshModelMesh(int dim); GMSH_API gmshModelGetMeshVertices(int dim, int tag, std::vector<int> &vertexTags, - std::vector<double> &coords, - std::vector<double> ¶metricCoords); + std::vector<double> &coords); GMSH_API gmshModelGetMeshElements(int dim, int tag, std::vector<int> &types, std::vector<std::vector<int> > &elementTags, std::vector<std::vector<int> > &vertexTags); GMSH_API gmshModelSetMeshSize(int dim, int tag, double size); -GMSH_API gmshModelSetCompound(int dim, const std::vector<int> &tags); GMSH_API gmshModelSetTransfiniteLine(int tag, int nPoints, int type, double coef); GMSH_API gmshModelSetTransfiniteSurface(int tag, int arrangement, const std::vector<int> &cornerTags); diff --git a/utils/api_demos/t0.cpp b/utils/api_demos/t0.cpp index 9be5666a2738898222c8500b32ca0e0d09c39327..33c99384a96c3198d310584810887ba11bf6a64f 100644 --- a/utils/api_demos/t0.cpp +++ b/utils/api_demos/t0.cpp @@ -1,13 +1,39 @@ +#include <iostream> #include <gmsh.h> int main(int argc, char **argv) { - gmshInitialize(argc, argv); + if(argc < 2){ + std::cout << "Usage: " << argv[0] << " file.geo [options]\n"; + return 1; + } + gmshInitialize(); gmshOptionSetNumber("General.Terminal", 1); gmshOptionSetNumber("Mesh.Algorithm", 5); - gmshOpen("test.geo"); + gmshOpen(argv[1]); gmshModelMesh(3); + + std::vector<std::pair<int, int> > entities; + gmshModelGetEntities(entities); + for(unsigned int i = 0; i < entities.size(); i++){ + std::vector<int> vertexTags; + std::vector<double> vertexCoords; + int dim = entities[i].first, tag = entities[i].second; + gmshModelGetMeshVertices(dim, tag, vertexTags, vertexCoords); + std::vector<int> elemTypes; + std::vector<std::vector<int> > elemTags, elemVertexTags; + gmshModelGetMeshElements(dim, tag, elemTypes, elemTags, elemVertexTags); + + int numElem = 0; + for(unsigned int i = 0; i < elemTags.size(); i++) + numElem += elemTags[i].size(); + std::cout << vertexTags.size() << " mesh vertices and " + << numElem << " mesh elements on entity (" + << dim << "," << tag << ")\n"; + } + gmshExport("test.msh"); gmshExport("test.unv"); gmshFinalize(); + return 0; }