mirror of
https://github.com/NGSolve/netgen.git
synced 2025-01-13 06:30:34 +05:00
Export ReadMedit/WriteMedit
This commit is contained in:
parent
cf0c8f1024
commit
5af59aba66
@ -7,6 +7,6 @@ target_sources(nglib PRIVATE writeuser.cpp
|
|||||||
)
|
)
|
||||||
|
|
||||||
install(FILES
|
install(FILES
|
||||||
writeuser.hpp
|
writeuser.hpp rw_medit.hpp
|
||||||
DESTINATION ${NG_INSTALL_DIR_INCLUDE}/interface COMPONENT netgen_devel
|
DESTINATION ${NG_INSTALL_DIR_INCLUDE}/interface COMPONENT netgen_devel
|
||||||
)
|
)
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
#include <meshing.hpp>
|
#include <meshing.hpp>
|
||||||
#include "writeuser.hpp"
|
#include "rw_medit.hpp"
|
||||||
|
|
||||||
#include <variant>
|
|
||||||
|
|
||||||
namespace netgen
|
namespace netgen
|
||||||
{
|
{
|
||||||
void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename)
|
void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename, map<int, tuple<int,int>> & index_map)
|
||||||
{
|
{
|
||||||
static Timer tall("ReadMeditMesh"); RegionTimer rtall(tall);
|
static Timer tall("ReadMeditMesh"); RegionTimer rtall(tall);
|
||||||
auto fin = ifstream(filename);
|
auto fin = ifstream(filename);
|
||||||
@ -14,17 +12,18 @@ void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename)
|
|||||||
mesh.ClearFaceDescriptors();
|
mesh.ClearFaceDescriptors();
|
||||||
|
|
||||||
int index_cnt[4] = {0,0,0,0};
|
int index_cnt[4] = {0,0,0,0};
|
||||||
std::map<int, int>index_map[4];
|
auto getIndex = [&](int eldim, int index) {
|
||||||
auto getIndex = [&](int dim, int index) {
|
if(index_map.count(index)==0) {
|
||||||
if(index_map[dim].count(index) == 0) {
|
auto n = ++index_cnt[eldim];
|
||||||
index_map[dim][index] = ++index_cnt[dim];
|
cout << "index " << eldim <<'\t' << index << '\t' << n << endl;
|
||||||
if(dim==2) {
|
index_map[index] = {eldim, n};
|
||||||
auto fd = FaceDescriptor(index_map[dim][index]-1,1,0,0);
|
if(eldim==2) {
|
||||||
fd.SetBCProperty(index_map[dim][index]);
|
auto fd = FaceDescriptor(n-1,1,0,0);
|
||||||
|
fd.SetBCProperty(n);
|
||||||
mesh.AddFaceDescriptor (fd);
|
mesh.AddFaceDescriptor (fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index_map[dim][index];
|
return get<1>(index_map[index]);
|
||||||
};
|
};
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
@ -43,12 +42,12 @@ void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename)
|
|||||||
if(token == "Vertices") {
|
if(token == "Vertices") {
|
||||||
int nvert;
|
int nvert;
|
||||||
fin >> nvert;
|
fin >> nvert;
|
||||||
Point<3> p;
|
Point<3> p{0.,0.,0.};
|
||||||
for(auto k : Range(nvert)) {
|
for(auto k : Range(nvert)) {
|
||||||
for(auto i : Range(dim))
|
for(auto i : Range(dim))
|
||||||
fin >> p[i];
|
fin >> p[i];
|
||||||
fin >> index;
|
fin >> index;
|
||||||
mesh.AddPoint(p);
|
mesh.AddPoint(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(token == "Edges") {
|
if(token == "Edges") {
|
||||||
@ -57,11 +56,11 @@ void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename)
|
|||||||
Segment seg;
|
Segment seg;
|
||||||
for(auto k : Range(nedge)) {
|
for(auto k : Range(nedge)) {
|
||||||
for(auto i : Range(2))
|
for(auto i : Range(2))
|
||||||
fin >> seg[i];
|
fin >> seg[i];
|
||||||
fin >> seg.edgenr;
|
fin >> seg.edgenr;
|
||||||
seg.edgenr = getIndex(1, seg.edgenr);
|
seg.edgenr = getIndex(1, seg.edgenr);
|
||||||
seg.si = seg.edgenr;
|
seg.si = seg.edgenr;
|
||||||
mesh.AddSegment(seg);
|
mesh.AddSegment(seg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(token == "Triangles") {
|
if(token == "Triangles") {
|
||||||
@ -70,10 +69,10 @@ void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename)
|
|||||||
Element2d sel;
|
Element2d sel;
|
||||||
for(auto k : Range(ntrig)) {
|
for(auto k : Range(ntrig)) {
|
||||||
for(auto i : Range(3))
|
for(auto i : Range(3))
|
||||||
fin >> sel[i];
|
fin >> sel[i];
|
||||||
fin >> index;
|
fin >> index;
|
||||||
sel.SetIndex(getIndex(2, index));
|
sel.SetIndex(getIndex(2, index));
|
||||||
mesh.AddSurfaceElement(sel);
|
mesh.AddSurfaceElement(sel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(token == "Tetrahedra") {
|
if(token == "Tetrahedra") {
|
||||||
@ -82,17 +81,24 @@ void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename)
|
|||||||
Element el(4);
|
Element el(4);
|
||||||
for(auto k : Range(ntet)) {
|
for(auto k : Range(ntet)) {
|
||||||
for(auto i : Range(4))
|
for(auto i : Range(4))
|
||||||
fin >> el[i];
|
fin >> el[i];
|
||||||
fin >> index;
|
fin >> index;
|
||||||
el.SetIndex(getIndex(3, index));
|
el.SetIndex(getIndex(3, index));
|
||||||
el.Invert();
|
el.Invert();
|
||||||
mesh.AddVolumeElement(el);
|
mesh.AddVolumeElement(el);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteMeditFormat (const Mesh & mesh, const filesystem::path & filename)
|
void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename)
|
||||||
|
{
|
||||||
|
map<int, tuple<int,int>> index_map;
|
||||||
|
ReadMeditFormat(mesh, filename, index_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void WriteMeditFormat (const Mesh & mesh, const filesystem::path & filename, map<tuple<int,int>, int> & index_map)
|
||||||
{
|
{
|
||||||
static Timer tall("WriteMeditFormat"); RegionTimer rtall(tall);
|
static Timer tall("WriteMeditFormat"); RegionTimer rtall(tall);
|
||||||
auto fout = ofstream(filename);
|
auto fout = ofstream(filename);
|
||||||
@ -101,9 +107,11 @@ void WriteMeditFormat (const Mesh & mesh, const filesystem::path & filename)
|
|||||||
fout << "Vertices\n" << mesh.GetNP() << endl;
|
fout << "Vertices\n" << mesh.GetNP() << endl;
|
||||||
int base_index = 0;
|
int base_index = 0;
|
||||||
int max_index = 0;
|
int max_index = 0;
|
||||||
auto getIndex = [&](int i) {
|
auto getIndex = [&](int i, int dim) {
|
||||||
max_index = max(max_index, i+base_index);
|
max_index = max(max_index, i+base_index);
|
||||||
return base_index+i;
|
auto index = base_index+i;
|
||||||
|
index_map[{dim,i}] = index;
|
||||||
|
return index;
|
||||||
};
|
};
|
||||||
fout << setprecision(16);
|
fout << setprecision(16);
|
||||||
|
|
||||||
@ -111,26 +119,34 @@ void WriteMeditFormat (const Mesh & mesh, const filesystem::path & filename)
|
|||||||
{
|
{
|
||||||
for(auto i : Range(mesh.GetDimension()))
|
for(auto i : Range(mesh.GetDimension()))
|
||||||
fout << setw(20) << p[i];
|
fout << setw(20) << p[i];
|
||||||
fout << setw(6) << getIndex(1) << endl;
|
fout << setw(6) << getIndex(1, 0) << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
base_index = max_index;
|
base_index = max_index;
|
||||||
fout << "Edges\n" << mesh.GetNSeg() << endl;
|
fout << "Edges\n" << mesh.GetNSeg() << endl;
|
||||||
for(const auto & seg : mesh.LineSegments())
|
for(const auto & seg : mesh.LineSegments())
|
||||||
fout << seg[0] << ' ' << seg[1] << ' ' << getIndex(seg.edgenr) << endl;
|
fout << seg[0] << ' ' << seg[1] << ' ' << getIndex(seg.edgenr, 1) << endl;
|
||||||
|
|
||||||
base_index = max_index;
|
base_index = max_index;
|
||||||
fout << "Triangles\n" << mesh.GetNSE() << endl;
|
fout << "Triangles\n" << mesh.GetNSE() << endl;
|
||||||
for(const auto & sel : mesh.SurfaceElements())
|
for(const auto & sel : mesh.SurfaceElements())
|
||||||
fout << sel[0] << ' ' << sel[1] << ' ' << sel[2] << ' ' << getIndex(sel.GetIndex()) << endl;
|
fout << sel[0] << ' ' << sel[1] << ' ' << sel[2] << ' ' << getIndex(sel.GetIndex(), 2) << endl;
|
||||||
|
|
||||||
base_index = max_index;
|
base_index = max_index;
|
||||||
fout << "Tetrahedra\n" << mesh.GetNE() << endl;
|
fout << "Tetrahedra\n" << mesh.GetNE() << endl;
|
||||||
for(const auto & el : mesh.VolumeElements())
|
for(const auto & el : mesh.VolumeElements())
|
||||||
fout << el[0] << ' ' << el[1] << ' ' << el[2] << ' ' << el[3] << '\t' << getIndex(el.GetIndex()) << endl;
|
fout << el[0] << ' ' << el[1] << ' ' << el[2] << ' ' << el[3] << '\t' << getIndex(el.GetIndex(), 3) << endl;
|
||||||
|
|
||||||
fout << "End" << endl;
|
fout << "End" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
static RegisterUserFormat reg_medit ("Medit Format", {".mesh"}, ReadMeditFormat, WriteMeditFormat);
|
void WriteMeditFormat (const Mesh & mesh, const filesystem::path & filename)
|
||||||
|
{
|
||||||
|
map<tuple<int,int>,int> index_map;
|
||||||
|
WriteMeditFormat(mesh, filename, index_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
static RegisterUserFormat reg_medit ("Medit Format", {".mesh"},
|
||||||
|
static_cast<void(*)(Mesh &, const filesystem::path&)>(ReadMeditFormat),
|
||||||
|
static_cast<void(*)(const Mesh &, const filesystem::path&)>(WriteMeditFormat));
|
||||||
} // namespace netgen
|
} // namespace netgen
|
||||||
|
11
libsrc/interface/rw_medit.hpp
Normal file
11
libsrc/interface/rw_medit.hpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include <meshing.hpp>
|
||||||
|
#include "writeuser.hpp"
|
||||||
|
|
||||||
|
namespace netgen
|
||||||
|
{
|
||||||
|
DLL_HEADER void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename, map<int, tuple<int,int>> & index_map);
|
||||||
|
DLL_HEADER void ReadMeditFormat (Mesh & mesh, const filesystem::path & filename);
|
||||||
|
|
||||||
|
DLL_HEADER void WriteMeditFormat (const Mesh & mesh, const filesystem::path & filename, map<tuple<int,int>, int> & index_map);
|
||||||
|
DLL_HEADER void WriteMeditFormat (const Mesh & mesh, const filesystem::path & filename);
|
||||||
|
} // namespace netgen
|
@ -10,6 +10,7 @@
|
|||||||
#include "meshing.hpp"
|
#include "meshing.hpp"
|
||||||
// #include <csg.hpp>
|
// #include <csg.hpp>
|
||||||
// #include <geometry2d.hpp>
|
// #include <geometry2d.hpp>
|
||||||
|
#include <../interface/rw_medit.hpp>
|
||||||
#include <../interface/writeuser.hpp>
|
#include <../interface/writeuser.hpp>
|
||||||
#include <../include/nginterface.h>
|
#include <../include/nginterface.h>
|
||||||
#include <../general/gzstream.h>
|
#include <../general/gzstream.h>
|
||||||
@ -1692,6 +1693,17 @@ project_boundaries : Optional[str] = None
|
|||||||
import_docu += s;
|
import_docu += s;
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
|
m.def("ReadMedit", [](const string& filename) {
|
||||||
|
map<int, tuple<int,int>> index_map;
|
||||||
|
auto mesh = make_shared<Mesh>();
|
||||||
|
ReadMeditFormat(*mesh, filename, index_map);
|
||||||
|
return py::make_tuple(mesh, index_map);
|
||||||
|
});
|
||||||
|
m.def("WriteMedit", [](const Mesh& mesh, const string& filename) {
|
||||||
|
map<tuple<int,int>, int> index_map;
|
||||||
|
WriteMeditFormat(mesh, filename, index_map);
|
||||||
|
return index_map;
|
||||||
|
});
|
||||||
m.def("ImportMesh", [](const string& filename, const string & format)
|
m.def("ImportMesh", [](const string& filename, const string & format)
|
||||||
{
|
{
|
||||||
auto mesh = make_shared<Mesh>();
|
auto mesh = make_shared<Mesh>();
|
||||||
|
Loading…
Reference in New Issue
Block a user