diff --git a/Plugin/CMakeLists.txt b/Plugin/CMakeLists.txt index 5c98130f5b0a54ca5dbe0116291415abdf42e6f0..32d1d68b92d8534691d77282fd735aa375617b52 100644 --- a/Plugin/CMakeLists.txt +++ b/Plugin/CMakeLists.txt @@ -36,6 +36,7 @@ set(SRC FaultZone.cpp MeshSubEntities.cpp CVTRemesh.cpp + ShowNeighborElements.cpp ) file(GLOB HDR RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h) diff --git a/Plugin/PluginManager.cpp b/Plugin/PluginManager.cpp index f997ed30855e8918b10e82e51691abf184d301f8..538dae0c121f821c0ebffee3d8a940f95f45f185 100644 --- a/Plugin/PluginManager.cpp +++ b/Plugin/PluginManager.cpp @@ -65,6 +65,7 @@ #include "FaultZone.h" #include "MeshSubEntities.h" #include "CVTRemesh.h" +#include "ShowNeighborElements.h" // for testing purposes only :-) #undef HAVE_DLOPEN @@ -261,6 +262,8 @@ void PluginManager::registerDefaultPlugins() ("DuplicateBoundaries", GMSH_RegisterDuplicateBoundariesPlugin())); allPlugins.insert(std::pair<std::string, GMSH_Plugin*> ("ThinLayerFixMesh", GMSH_RegisterThinLayerFixMeshPlugin())); + allPlugins.insert(std::pair<std::string, GMSH_Plugin*> + ("ShowNeighborElements", GMSH_RegisterShowNeighborElementsPlugin())); allPlugins.insert(std::pair<std::string, GMSH_Plugin*> ("MeshSubEntities", GMSH_RegisterMeshSubEntitiesPlugin())); #if defined(HAVE_MESH) diff --git a/Plugin/ShowNeighborElements.cpp b/Plugin/ShowNeighborElements.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3ddc8f65e824b35d56c945a1d20b196314908c2 --- /dev/null +++ b/Plugin/ShowNeighborElements.cpp @@ -0,0 +1,138 @@ +// Gmsh - Copyright (C) 1997-2016 C. Geuzaine, J.-F. Remacle +// +// See the LICENSE.txt file for license information. Please report all +// bugs and problems to the public mailing list <gmsh@onelab.info>. + +#include "ShowNeighborElements.h" +#include "GModel.h" +#include "MElement.h" +#include "MVertex.h" +#include "Options.h" +#include "Context.h" +#include "GmshDefines.h" +#if defined(HAVE_OPENGL) +#include "drawContext.h" +#endif + +StringXNumber ShowNeighborElementsOptions_Number[] = { + {GMSH_FULLRC, "NumLayers", NULL, 1}, + {GMSH_FULLRC, "Element1", NULL, 0}, + {GMSH_FULLRC, "Element2", NULL, 0}, + {GMSH_FULLRC, "Element3", NULL, 0}, + {GMSH_FULLRC, "Element4", NULL, 0}, + {GMSH_FULLRC, "Element5", NULL, 0} +}; +extern "C" +{ + GMSH_Plugin *GMSH_RegisterShowNeighborElementsPlugin() + { + return new GMSH_ShowNeighborElementsPlugin(); + } +} +int GMSH_ShowNeighborElementsPlugin::getNbOptions() const +{ + return sizeof(ShowNeighborElementsOptions_Number) / sizeof(StringXNumber); +} +StringXNumber *GMSH_ShowNeighborElementsPlugin::getOption(int iopt) +{ + return &ShowNeighborElementsOptions_Number[iopt]; +} +std::string GMSH_ShowNeighborElementsPlugin::getHelp() const +{ + return "Plugin(ShowNeighborElements) allows to set visible some given elements " + " and a layer of elements around them, the other being set invisible."; +} + +// Execution +PView* GMSH_ShowNeighborElementsPlugin::execute(PView *v) +{ + GModel *m = GModel::current(); + + _nLayers = static_cast<int>(ShowNeighborElementsOptions_Number[0].def); + _nel1 = static_cast<int>(ShowNeighborElementsOptions_Number[1].def); + _nel2 = static_cast<int>(ShowNeighborElementsOptions_Number[2].def); + _nel3 = static_cast<int>(ShowNeighborElementsOptions_Number[3].def); + _nel4 = static_cast<int>(ShowNeighborElementsOptions_Number[4].def); + _nel5 = static_cast<int>(ShowNeighborElementsOptions_Number[5].def); + + for (GModel::fiter it = m->firstFace(); it != m->lastFace(); it++) { + GFace *f = *it; + _init(f); + _showLayers(f, _nLayers); + } + + for (GModel::riter it = m->firstRegion(); it != m->lastRegion(); it++) { + GRegion *r = *it; + _init(r); + _showLayers(r, _nLayers); + } + +#if defined(HAVE_OPENGL) + CTX::instance()->mesh.changed = (ENT_ALL); + drawContext::global()->draw(); +#endif + + return NULL; +} + +void GMSH_ShowNeighborElementsPlugin::_init(GEntity *ent) +{ + _vert2elem.clear(); + for (unsigned i = 0; i < ent->getNumMeshElements(); ++i) { + MElement *el = ent->getMeshElement(i); + if (el->getNum() == _nel1 || + el->getNum() == _nel2 || + el->getNum() == _nel3 || + el->getNum() == _nel4 || + el->getNum() == _nel5) { + el->setVisibility(true); + for (int k = 0; k < el->getNumVertices(); ++k) { // TODO only corner vertices? + _vertices.insert(el->getVertex(k)); + } + } + else { + el->setVisibility(false); + for (int k = 0; k < el->getNumVertices(); ++k) { // TODO only corner vertices? + _vert2elem.insert(std::make_pair(el->getVertex(k), el)); + } + } + } +} + +void GMSH_ShowNeighborElementsPlugin::_showLayers(GEntity *ent, int nLayer) +{ +// std::pair<std::multimap<MVertex*, MElement*>::iterator, +// std::multimap<MVertex*, MElement*>::iterator> lowUpBound; + + if (_vertices.empty() || nLayer < 1) return; + + std::set<MVertex*> &vert = _vertices; +// _vertices.clear(); + std::map<MElement*, int> el2cnt; + + std::set<MVertex*>::iterator it; + for (it = vert.begin(); it != vert.end(); ++it) { + MVertex *v = *it; + std::multimap<MVertex*, MElement*>::iterator ite, itstop; + ite = _vert2elem.lower_bound(v); + itstop = _vert2elem.upper_bound(v); + for (; ite != itstop; ++ite) { + MElement *el = ite->second; + if (el2cnt.find(el) == el2cnt.end()) el2cnt[el] = 0; + ++el2cnt[el]; +// el->setVisibility(true); +// for (int k = 0; k < el->getNumVertices(); ++k) { // TODO only corner vertices? +// _vertices.insert(el->getVertex(k)); +// } + } + } + + std::map<MElement*, int>::iterator it2; + for (it2 = el2cnt.begin(); it2 != el2cnt.end(); ++it2) { + if (it2->second && it2->second > 3-nLayer) { + it2->first->setVisibility(true); + } + } + + //_showLayers(ent, nLayer-1); +} diff --git a/Plugin/ShowNeighborElements.h b/Plugin/ShowNeighborElements.h new file mode 100644 index 0000000000000000000000000000000000000000..99d70aec02c7d842407f1cf824215782de41cc7a --- /dev/null +++ b/Plugin/ShowNeighborElements.h @@ -0,0 +1,44 @@ +// Gmsh - Copyright (C) 1997-2016 C. Geuzaine, J.-F. Remacle +// +// See the LICENSE.txt file for license information. Please report all +// bugs and problems to the public mailing list <gmsh@onelab.info>. + +#ifndef _SHOWNEIGHBORELEMENTS_H_ +#define _SHOWNEIGHBORELEMENTS_H_ + +#include "Plugin.h" +#include <set> + +class MVertex; + +extern "C" +{ + GMSH_Plugin *GMSH_RegisterShowNeighborElementsPlugin(); +} + +class GMSH_ShowNeighborElementsPlugin : public GMSH_PostPlugin +{ +private : + int _nLayers; + int _nel1, _nel2, _nel3, _nel4, _nel5; + std::multimap<MVertex*, MElement*> _vert2elem; + std::set<MVertex*> _vertices; + +public : + GMSH_ShowNeighborElementsPlugin() {} + std::string getName() const { return "ShowNeighborElements"; } + std::string getShortHelp() const { + return "Choose which type of element to hide."; + } + std::string getHelp() const; + std::string getAuthor() const { return "Amaury Johnen"; } + int getNbOptions() const; + StringXNumber* getOption(int); + PView* execute(PView *); + +private : + void _init(GEntity *); + void _showLayers(GEntity *, int nLayer); +}; + +#endif