2016-12-09 20:34:22 +05:00
|
|
|
|
|
|
|
#ifdef NG_PYTHON
|
|
|
|
|
|
|
|
#include <../general/ngpython.hpp>
|
2019-07-11 19:23:22 +05:00
|
|
|
#include <core/python_ngcore.hpp>
|
2016-12-09 20:34:22 +05:00
|
|
|
#include <stlgeom.hpp>
|
2019-07-26 02:25:58 +05:00
|
|
|
#include "../meshing/python_mesh.hpp"
|
2016-12-09 20:34:22 +05:00
|
|
|
|
|
|
|
using namespace netgen;
|
|
|
|
namespace netgen
|
|
|
|
{
|
2022-08-09 20:40:21 +05:00
|
|
|
extern shared_ptr<Mesh> mesh;
|
2016-12-09 20:34:22 +05:00
|
|
|
extern shared_ptr<NetgenGeometry> ng_geometry;
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:22:53 +05:00
|
|
|
static string stlparameter_description = R"delimiter(
|
|
|
|
STL Specific Meshing Parameters
|
|
|
|
-------------------------------
|
|
|
|
|
2019-08-27 13:10:17 +05:00
|
|
|
yangle: float = 30.
|
2019-08-02 19:22:53 +05:00
|
|
|
Angle for edge detection
|
|
|
|
|
2019-08-27 13:10:17 +05:00
|
|
|
contyangle: float = 20.
|
2019-08-02 19:22:53 +05:00
|
|
|
Edges continue if angle > contyangle
|
|
|
|
|
2019-08-27 13:10:17 +05:00
|
|
|
edgecornerangle: float = 60.
|
2019-08-02 19:22:53 +05:00
|
|
|
Angle of geometry edge at which the mesher should set a point.
|
|
|
|
|
2019-08-27 13:10:17 +05:00
|
|
|
closeedgefac: Optional[float] = 1.
|
|
|
|
Factor for meshing close edges, if None it is disabled.
|
|
|
|
|
|
|
|
minedgelen: Optional[float] = 0.001
|
|
|
|
Minimum edge length to be used for dividing edges to mesh points. If
|
|
|
|
None this is disabled.
|
2019-08-02 19:22:53 +05:00
|
|
|
)delimiter";
|
|
|
|
|
2019-08-06 20:38:44 +05:00
|
|
|
void CreateSTLParametersFromKwargs(STLParameters& stlparam, py::dict kwargs)
|
2019-08-02 19:22:53 +05:00
|
|
|
{
|
|
|
|
if(kwargs.contains("yangle"))
|
|
|
|
stlparam.yangle = py::cast<double>(kwargs.attr("pop")("yangle"));
|
|
|
|
if(kwargs.contains("contyangle"))
|
|
|
|
stlparam.contyangle = py::cast<double>(kwargs.attr("pop")("contyangle"));
|
|
|
|
if(kwargs.contains("edgecornerangle"))
|
|
|
|
stlparam.edgecornerangle = py::cast<double>(kwargs.attr("pop")("edgecornerangle"));
|
|
|
|
if(kwargs.contains("chartangle"))
|
|
|
|
stlparam.chartangle = py::cast<double>(kwargs.attr("pop")("chartangle"));
|
|
|
|
if(kwargs.contains("outerchartangle"))
|
|
|
|
stlparam.outerchartangle = py::cast<double>(kwargs.attr("pop")("outerchartangle"));
|
|
|
|
if(kwargs.contains("usesearchtree"))
|
|
|
|
stlparam.usesearchtree = py::cast<int>(kwargs.attr("pop")("usesearchtree"));
|
2019-10-07 15:56:12 +05:00
|
|
|
if(kwargs.contains("atlasfac"))
|
2019-08-02 19:22:53 +05:00
|
|
|
{
|
|
|
|
auto val = kwargs.attr("pop")("resthatlasfac");
|
|
|
|
if(val.is_none())
|
|
|
|
stlparam.resthatlasenable = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stlparam.resthatlasenable = true;
|
|
|
|
stlparam.resthatlasfac = py::cast<double>(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(kwargs.contains("atlasminh"))
|
|
|
|
stlparam.atlasminh = py::cast<double>(kwargs.attr("pop")("atlasminh"));
|
2019-10-07 15:56:12 +05:00
|
|
|
if(kwargs.contains("surfcurvfac"))
|
2019-08-02 19:22:53 +05:00
|
|
|
{
|
2019-10-07 15:56:12 +05:00
|
|
|
auto val = kwargs.attr("pop")("surfcurvfac");
|
2019-08-02 19:22:53 +05:00
|
|
|
if(val.is_none())
|
|
|
|
stlparam.resthsurfcurvenable = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stlparam.resthsurfcurvenable = true;
|
|
|
|
stlparam.resthsurfcurvfac = py::cast<double>(val);
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 15:56:12 +05:00
|
|
|
if(kwargs.contains("chartdistfac"))
|
2019-08-02 19:22:53 +05:00
|
|
|
{
|
2019-10-07 15:56:12 +05:00
|
|
|
auto val = kwargs.attr("pop")("chartdistfac");
|
2019-08-02 19:22:53 +05:00
|
|
|
if(val.is_none())
|
|
|
|
stlparam.resthchartdistenable = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stlparam.resthchartdistenable = true;
|
|
|
|
stlparam.resthchartdistfac = py::cast<double>(val);
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 15:56:12 +05:00
|
|
|
if(kwargs.contains("edgeanglefac"))
|
2019-08-02 19:22:53 +05:00
|
|
|
{
|
2019-10-07 15:56:12 +05:00
|
|
|
auto val = kwargs.attr("pop")("edgeanglefac");
|
2019-08-02 19:22:53 +05:00
|
|
|
if(val.is_none())
|
|
|
|
stlparam.resthedgeangleenable = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stlparam.resthedgeangleenable = true;
|
|
|
|
stlparam.resthedgeanglefac = py::cast<double>(val);
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 15:56:12 +05:00
|
|
|
if(kwargs.contains("surfmeshcurvfac"))
|
2019-08-02 19:22:53 +05:00
|
|
|
{
|
2019-10-07 15:56:12 +05:00
|
|
|
auto val = kwargs.attr("pop")("surfmeshcurvfac");
|
2019-08-02 19:22:53 +05:00
|
|
|
if(val.is_none())
|
|
|
|
stlparam.resthsurfmeshcurvenable = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stlparam.resthsurfmeshcurvenable = true;
|
|
|
|
stlparam.resthsurfmeshcurvfac = py::cast<double>(val);
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 15:56:12 +05:00
|
|
|
if(kwargs.contains("linelengthfac"))
|
2019-08-02 19:22:53 +05:00
|
|
|
{
|
2019-10-07 15:56:12 +05:00
|
|
|
auto val = kwargs.attr("pop")("linelengthfac");
|
2019-08-02 19:22:53 +05:00
|
|
|
if(val.is_none())
|
|
|
|
stlparam.resthlinelengthenable = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stlparam.resthlinelengthenable = true;
|
|
|
|
stlparam.resthlinelengthfac = py::cast<double>(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(kwargs.contains("recalc_h_opt"))
|
|
|
|
stlparam.recalc_h_opt = py::cast<bool>(kwargs.attr("pop")("recalc_h_opt"));
|
|
|
|
}
|
|
|
|
|
2016-12-09 20:34:22 +05:00
|
|
|
|
2021-02-18 15:37:05 +05:00
|
|
|
NGCORE_API_EXPORT void ExportSTL(py::module & m)
|
2016-12-09 20:34:22 +05:00
|
|
|
{
|
2018-04-17 22:37:29 +05:00
|
|
|
py::class_<STLGeometry,shared_ptr<STLGeometry>, NetgenGeometry> (m,"STLGeometry")
|
2016-12-09 20:34:22 +05:00
|
|
|
.def(py::init<>())
|
2022-08-09 20:40:21 +05:00
|
|
|
.def(py::init<>([](const string& filename, bool surface)
|
2019-07-26 02:25:58 +05:00
|
|
|
{
|
|
|
|
ifstream ist(filename);
|
2022-08-09 20:40:21 +05:00
|
|
|
return shared_ptr<STLGeometry>(STLGeometry::Load(ist,
|
|
|
|
surface));
|
|
|
|
}), py::arg("filename"), py::arg("surface")=false,
|
2019-07-26 02:25:58 +05:00
|
|
|
py::call_guard<py::gil_scoped_release>())
|
2018-12-20 21:01:27 +05:00
|
|
|
.def(NGSPickle<STLGeometry>())
|
2018-07-12 19:35:52 +05:00
|
|
|
.def("_visualizationData", [](shared_ptr<STLGeometry> stl_geo)
|
|
|
|
{
|
|
|
|
std::vector<float> vertices;
|
|
|
|
std::vector<int> trigs;
|
|
|
|
std::vector<float> normals;
|
|
|
|
std::vector<float> min = {std::numeric_limits<float>::max(),
|
|
|
|
std::numeric_limits<float>::max(),
|
|
|
|
std::numeric_limits<float>::max()};
|
|
|
|
std::vector<float> max = {std::numeric_limits<float>::lowest(),
|
|
|
|
std::numeric_limits<float>::lowest(),
|
|
|
|
std::numeric_limits<float>::lowest()};
|
|
|
|
std::vector<string> surfnames;
|
|
|
|
|
|
|
|
surfnames.push_back("stl");
|
|
|
|
vertices.reserve(stl_geo->GetNT()*3*3);
|
|
|
|
trigs.reserve(stl_geo->GetNT()*4);
|
|
|
|
normals.reserve(stl_geo->GetNT()*3*3);
|
|
|
|
size_t ii = 0;
|
|
|
|
for(int i = 0; i < stl_geo->GetNT(); i++)
|
|
|
|
{
|
|
|
|
auto& trig = stl_geo->GetTriangle(i+1);
|
|
|
|
for(int k = 0; k < 3; k++)
|
|
|
|
{
|
|
|
|
trigs.push_back(ii++);
|
|
|
|
auto& pnt = stl_geo->GetPoint(trig[k]);
|
|
|
|
for (int l = 0; l < 3; l++)
|
|
|
|
{
|
|
|
|
float val = pnt[l];
|
|
|
|
vertices.push_back(val);
|
|
|
|
min[l] = min2(min[l], val);
|
|
|
|
max[l] = max2(max[l], val);
|
|
|
|
normals.push_back(trig.Normal()[l]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trigs.push_back(0);
|
|
|
|
}
|
|
|
|
py::gil_scoped_acquire ac;
|
|
|
|
py::dict res;
|
|
|
|
py::list snames;
|
|
|
|
for(auto name : surfnames)
|
|
|
|
snames.append(py::cast(name));
|
|
|
|
res["vertices"] = MoveToNumpy(vertices);
|
|
|
|
res["triangles"] = MoveToNumpy(trigs);
|
|
|
|
res["normals"] = MoveToNumpy(normals);
|
|
|
|
res["surfnames"] = snames;
|
|
|
|
res["min"] = MoveToNumpy(min);
|
|
|
|
res["max"] = MoveToNumpy(max);
|
|
|
|
return res;
|
|
|
|
}, py::call_guard<py::gil_scoped_release>())
|
2019-07-29 10:11:57 +05:00
|
|
|
.def("GenerateMesh", [] (shared_ptr<STLGeometry> geo,
|
2022-09-01 13:43:16 +05:00
|
|
|
MeshingParameters* pars,
|
|
|
|
shared_ptr<Mesh> mesh, py::kwargs kwargs)
|
2019-07-26 02:25:58 +05:00
|
|
|
{
|
|
|
|
MeshingParameters mp;
|
2019-08-06 23:03:17 +05:00
|
|
|
STLParameters stlparam;
|
2019-08-06 13:42:53 +05:00
|
|
|
{ py::gil_scoped_acquire aq;
|
2019-08-02 19:22:53 +05:00
|
|
|
if(pars)
|
|
|
|
{
|
2019-08-06 23:13:31 +05:00
|
|
|
auto mp_flags = pars->geometrySpecificParameters;
|
|
|
|
auto mp_kwargs = CreateDictFromFlags(mp_flags);
|
|
|
|
CreateSTLParametersFromKwargs(stlparam, mp_kwargs);
|
2019-08-02 19:22:53 +05:00
|
|
|
mp = *pars;
|
|
|
|
}
|
|
|
|
CreateSTLParametersFromKwargs(stlparam, kwargs);
|
2019-08-06 13:42:53 +05:00
|
|
|
CreateMPfromKwargs(mp, kwargs); // this will throw if any kwargs are not passed
|
2019-07-26 02:25:58 +05:00
|
|
|
}
|
2022-09-01 13:43:16 +05:00
|
|
|
if(!mesh)
|
|
|
|
{
|
|
|
|
mesh = make_shared<Mesh>();
|
|
|
|
}
|
2019-07-26 02:25:58 +05:00
|
|
|
mesh->SetGeometry(geo);
|
|
|
|
ng_geometry = geo;
|
2019-08-06 13:42:53 +05:00
|
|
|
SetGlobalMesh(mesh);
|
2019-11-26 21:08:21 +05:00
|
|
|
auto result = STLMeshingDummy(geo.get(), mesh, mp, stlparam);
|
|
|
|
if(result != 0)
|
2022-08-09 20:40:21 +05:00
|
|
|
{
|
|
|
|
netgen::mesh = mesh;
|
|
|
|
throw Exception("Meshing failed!");
|
|
|
|
}
|
2019-11-26 21:08:21 +05:00
|
|
|
|
2019-07-26 02:25:58 +05:00
|
|
|
return mesh;
|
2022-09-01 13:43:16 +05:00
|
|
|
}, py::arg("mp") = nullptr, py::arg("mesh") = nullptr,
|
2019-07-26 02:25:58 +05:00
|
|
|
py::call_guard<py::gil_scoped_release>(),
|
2019-08-02 19:22:53 +05:00
|
|
|
(meshingparameter_description + stlparameter_description).c_str())
|
2022-04-28 13:46:49 +05:00
|
|
|
.def("Draw", FunctionPointer
|
|
|
|
([] (shared_ptr<STLGeometry> self)
|
|
|
|
{
|
|
|
|
ng_geometry = self;
|
|
|
|
})
|
|
|
|
)
|
2023-11-23 00:36:52 +05:00
|
|
|
.def("GetVicinity", [] (shared_ptr<STLGeometry> self, int node, int size, string type) {
|
|
|
|
NgArray<int> vic;
|
|
|
|
|
|
|
|
int trig;
|
|
|
|
if(type == "trig")
|
|
|
|
trig = node;
|
|
|
|
|
|
|
|
if(type == "point")
|
|
|
|
trig = self->TrigPerPoint(node, 1);
|
|
|
|
|
|
|
|
self->GetVicinity(trig, size, vic);
|
|
|
|
auto geo = make_shared<STLGeometry>();
|
|
|
|
NgArray<STLReadTriangle> trigs;
|
|
|
|
|
|
|
|
for(auto i : Range(vic.Size())) {
|
|
|
|
int trigi = vic[i];
|
|
|
|
STLReadTriangle t;
|
|
|
|
Vec<3> normal = self->GetTriangle(trigi).Normal();
|
|
|
|
Point<3> pts[3];
|
|
|
|
auto trig = self->GetTriangle(trigi);
|
|
|
|
for(auto pi : Range(3))
|
|
|
|
pts[pi] = self->GetPoint(trig[pi]);
|
|
|
|
trigs.Append(STLReadTriangle(pts, normal));
|
|
|
|
}
|
|
|
|
|
|
|
|
geo->SetSurfaceSTL(true);
|
|
|
|
geo->InitSTLGeometry(trigs);
|
|
|
|
return geo;
|
|
|
|
}, py::arg("node"), py::arg("size"), py::arg("node_type") = "trig")
|
|
|
|
.def("SmoothDirtyTrigs", [] (shared_ptr<STLGeometry> self, py::kwargs kwargs) {
|
|
|
|
STLParameters stlparam;
|
|
|
|
CreateSTLParametersFromKwargs(stlparam, kwargs);
|
|
|
|
self->SmoothDirtyTrigs(stlparam);
|
|
|
|
})
|
|
|
|
.def("GetDirtyTrigs", [] (shared_ptr<STLGeometry> self, py::kwargs kwargs) {
|
|
|
|
STLParameters stlparam;
|
|
|
|
CreateSTLParametersFromKwargs(stlparam, kwargs);
|
|
|
|
self->MarkDirtyTrigs(stlparam);
|
|
|
|
py::list dirty;
|
|
|
|
for(auto i : Range(self->GetNT()))
|
|
|
|
if(self->IsMarkedTrig(i+1))
|
|
|
|
dirty.append(i);
|
|
|
|
})
|
|
|
|
.def("MovePointToMiddle", [] (shared_ptr<STLGeometry> self, int node, int count) {
|
|
|
|
auto trignr = self->TrigPerPoint(node, 1);
|
|
|
|
auto trig = self->GetTriangle(trignr);
|
|
|
|
int point_in_trig = -1;
|
|
|
|
for(auto i : Range(3))
|
|
|
|
if(trig[i] == node)
|
|
|
|
point_in_trig = i;
|
|
|
|
|
|
|
|
if(point_in_trig == -1)
|
|
|
|
throw Exception("Point not found in triangle");
|
|
|
|
self->SetSelectTrig(trignr);
|
|
|
|
self->SetNodeOfSelTrig(point_in_trig);
|
|
|
|
for(auto i : Range(count))
|
|
|
|
self->MoveSelectedPointToMiddle();
|
|
|
|
})
|
|
|
|
.def("Save", &STLGeometry::Save)
|
2016-12-09 20:34:22 +05:00
|
|
|
;
|
2019-07-26 02:25:58 +05:00
|
|
|
m.def("LoadSTLGeometry", [] (const string & filename)
|
|
|
|
{
|
|
|
|
cout << "WARNING: LoadSTLGeometry is deprecated, use the STLGeometry(filename) constructor instead!" << endl;
|
|
|
|
ifstream ist(filename);
|
|
|
|
return shared_ptr<STLGeometry>(STLGeometry::Load(ist));
|
|
|
|
},py::call_guard<py::gil_scoped_release>());
|
2016-12-09 20:34:22 +05:00
|
|
|
}
|
|
|
|
|
2017-09-01 12:33:57 +05:00
|
|
|
PYBIND11_MODULE(libstl, m) {
|
2016-12-09 20:34:22 +05:00
|
|
|
ExportSTL(m);
|
|
|
|
}
|
2016-12-12 17:14:47 +05:00
|
|
|
|
2016-12-09 20:34:22 +05:00
|
|
|
#endif
|