1
/***************************************************************************
2
* Copyright (c) 2017 Lorenz Lechner *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
#include <TopoDS_Edge.hxx>
27
#include <TopoDS_Face.hxx>
34
// boost is purposely not in the precompiled headers, see
35
// https://github.com/FreeCAD/FreeCAD/pull/7979#issuecomment-1358123252
36
#include <Base/Interpreter.h>
38
#include <Eigen/Geometry>
39
#include <Eigen/Sparse>
40
#include <Mod/Part/App/TopoShapeEdgePy.h>
41
#include <Mod/Part/App/TopoShapeFacePy.h>
42
#include <boost/python.hpp>
43
#include <boost/python/call.hpp>
44
#include <boost/python/class.hpp>
45
#include <boost/python/copy_const_reference.hpp>
46
#include <boost/python/module.hpp>
47
#include <boost/python/return_value_policy.hpp>
48
#include <boost/python/wrapper.hpp>
50
#include "MeshFlattening.h"
51
#include "MeshFlatteningLscmRelax.h"
52
#include "MeshFlatteningNurbs.h"
56
namespace py = boost::python;
58
const TopoDS_Face& getTopoDSFace(const py::object& face)
60
if (PyObject_TypeCheck(face.ptr(), &(Part::TopoShapeFacePy::Type)))
62
const Part::TopoShapeFacePy* fpy = static_cast<Part::TopoShapeFacePy*>(face.ptr());
63
const TopoDS_Face& myFace = TopoDS::Face(fpy->getTopoShapePtr()->getShape());
67
throw std::invalid_argument("must be a face");
70
const TopoDS_Edge& getTopoDSEdge(py::object* edge)
72
if (PyObject_TypeCheck(edge->ptr(), &(Part::TopoShapeEdgePy::Type)))
74
const Part::TopoShapeEdgePy* epy = static_cast<Part::TopoShapeEdgePy*>(edge->ptr());
75
const TopoDS_Edge& myEdge = TopoDS::Edge(epy->getTopoShapePtr()->getShape());
79
throw std::invalid_argument("must be an edge");
82
Py::Object makeEdge(const TopoDS_Edge& edge)
84
return Py::asObject(new Part::TopoShapeEdgePy(new Part::TopoShape(edge)));
87
std::shared_ptr<FaceUnwrapper> FaceUnwrapper_face(const py::object& face)
89
const TopoDS_Face& myFace = getTopoDSFace(face);
90
return std::make_shared<FaceUnwrapper>(myFace);
93
std::shared_ptr<FaceUnwrapper> FaceUnwrapper_mesh(const py::object& points,
94
const py::object& facets)
97
Py::Sequence l1(points.ptr());
98
ColMat<double, 3> coords;
99
coords.resize(l1.size(), 3);
101
for (Py::Sequence::iterator it = l1.begin(); it != l1.end(); ++it, ++row) {
104
for (Py::Sequence::iterator jt = c.begin(); jt != c.end(); ++jt, ++col) {
105
double v = static_cast<double>(Py::Float(*jt));
106
coords(row, col) = v;
110
Py::Sequence l2(facets.ptr());
111
ColMat<long, 3> triangles;
112
triangles.resize(l2.size(), 3);
114
for (Py::Sequence::iterator it = l2.begin(); it != l2.end(); ++it, ++row) {
117
for (Py::Sequence::iterator jt = c.begin(); jt != c.end(); ++jt, ++col) {
118
long v = static_cast<long>(Py::Long(*jt));
119
triangles(row, col) = v;
123
return std::shared_ptr<FaceUnwrapper>(new FaceUnwrapper(coords, triangles));
125
catch (const Py::Exception&) {
127
throw std::invalid_argument(e.what());
131
boost::python::list interpolateFlatFacePy(FaceUnwrapper& instance, const py::object& face)
133
const TopoDS_Face& myFace = getTopoDSFace(face);
134
ColMat<double, 3> mat = instance.interpolateFlatFace(myFace);
135
boost::python::list plist;
136
auto cols = mat.cols();
137
auto rows = mat.rows();
138
for (int i=0; i<rows; i++) {
139
boost::python::list vec;
140
for (int j=0; j<cols; j++) {
141
double c = mat.coeff(i, j);
149
boost::python::list getFlatBoundaryNodesPy(FaceUnwrapper& instance)
151
std::vector<ColMat<double, 3>> mat_array = instance.getFlatBoundaryNodes();
153
boost::python::list ary;
154
for (auto& mat : mat_array) {
155
boost::python::list plist;
156
auto cols = mat.cols();
157
auto rows = mat.rows();
158
for (int i=0; i<rows; i++) {
159
boost::python::list vec;
160
for (int j=0; j<cols; j++) {
161
double c = mat.coeff(i, j);
173
// https://www.boost.org/doc/libs/1_52_0/libs/python/doc/v2/faq.html
174
template<typename eigen_type>
177
static PyObject* convert(const eigen_type& mat)
179
// <class 'numpy.array'>
181
for (int i = 0; i < mat.rows(); i++) {
183
for (int j = 0; j < mat.cols(); j++) {
184
row.append(mat.coeff(i ,j));
188
return boost::python::incref(ary.ptr());
190
static void to_python_converter()
192
py::to_python_converter<eigen_type, eigen_matrix<eigen_type>>();
197
BOOST_PYTHON_MODULE(flatmesh)
199
//m.doc() = "functions to unwrapp faces/ meshes";
201
py::class_<lscmrelax::LscmRelax>("LscmRelax")
202
.def(py::init<ColMat<double, 3>, ColMat<long, 3>, std::vector<long>>())
203
.def("lscm", &lscmrelax::LscmRelax::lscm)
204
.def("relax", &lscmrelax::LscmRelax::relax)
205
.def("rotate_by_min_bound_area", &lscmrelax::LscmRelax::rotate_by_min_bound_area)
206
.def("transform", &lscmrelax::LscmRelax::transform)
207
.def_readonly("rhs", &lscmrelax::LscmRelax::rhs)
208
.def_readonly("MATRIX", &lscmrelax::LscmRelax::MATRIX)
209
.def_readonly("area", &lscmrelax::LscmRelax::get_area)
210
.def_readonly("flat_area", &lscmrelax::LscmRelax::get_flat_area)
211
// .def_readonly("flat_vertices", [](lscmrelax::LscmRelax& L){return L.flat_vertices.transpose();}, py::return_value_policy<py::copy_const_reference>())
212
.def_readonly("flat_vertices_3D", &lscmrelax::LscmRelax::get_flat_vertices_3D);
214
py::class_<nurbs::NurbsBase2D>("NurbsBase2D")
215
.def(py::init<Eigen::VectorXd, Eigen::VectorXd, Eigen::VectorXd, int, int>())
216
.def_readonly("u_knots", &nurbs::NurbsBase2D::u_knots)
217
.def_readonly("weights", &nurbs::NurbsBase2D::weights)
218
.def_readonly("degree_u", &nurbs::NurbsBase2D::degree_u)
219
// .def_readonly("v_knots", &nurbs::NurbsBase2D::u_knots)
220
.def_readonly("degree_v", &nurbs::NurbsBase2D::degree_u)
221
.def("getUVMesh", &nurbs::NurbsBase2D::getUVMesh)
222
.def("computeFirstDerivatives", &nurbs::NurbsBase2D::computeFirstDerivatives)
223
.def("getInfluenceVector", &nurbs::NurbsBase2D::getInfluenceVector)
224
.def("getInfluenceMatrix", &nurbs::NurbsBase2D::getInfluenceMatrix)
225
.def("getDuVector", &nurbs::NurbsBase2D::getDuVector)
226
.def("getDuMatrix", &nurbs::NurbsBase2D::getDuMatrix)
227
.def("getDvVector", &nurbs::NurbsBase2D::getDvVector)
228
.def("getDvMatrix", &nurbs::NurbsBase2D::getDvMatrix)
229
.def("interpolateUBS", &nurbs::NurbsBase2D::interpolateUBS);
231
py::class_<nurbs::NurbsBase1D>("NurbsBase1D")
232
.def(py::init<Eigen::VectorXd, Eigen::VectorXd, int>())
233
.def_readonly("u_knots", &nurbs::NurbsBase1D::u_knots)
234
.def_readonly("weights", &nurbs::NurbsBase1D::weights)
235
.def_readonly("degree_u", &nurbs::NurbsBase1D::degree_u)
236
.def("getUMesh", &nurbs::NurbsBase1D::getUMesh)
237
.def("computeFirstDerivatives", &nurbs::NurbsBase1D::computeFirstDerivatives)
238
.def("getInfluenceVector", &nurbs::NurbsBase1D::getInfluenceVector)
239
.def("getInfluenceMatrix", &nurbs::NurbsBase1D::getInfluenceMatrix)
240
.def("getDuVector", &nurbs::NurbsBase1D::getDuVector)
241
.def("getDuMatrix", &nurbs::NurbsBase1D::getDuMatrix)
242
.add_static_property("getKnotSequence", &nurbs::NurbsBase1D::getKnotSequence)
243
.add_static_property("getWeightList", &nurbs::NurbsBase1D::getWeightList);
245
py::class_<FaceUnwrapper>("FaceUnwrapper")
246
.def("__init__", py::make_constructor(&FaceUnwrapper_face))
247
.def("__init__", py::make_constructor(&FaceUnwrapper_mesh))
248
.def(py::init<ColMat<double, 3>, ColMat<long, 3>>())
249
.def("findFlatNodes", &FaceUnwrapper::findFlatNodes)
250
.def("interpolateFlatFace", &interpolateFlatFacePy)
251
.def("getFlatBoundaryNodes", &getFlatBoundaryNodesPy)
252
.add_property("tris", py::make_getter(&FaceUnwrapper::tris, py::return_value_policy<py::return_by_value>()))
253
.add_property("nodes", py::make_getter(&FaceUnwrapper::xyz_nodes, py::return_value_policy<py::return_by_value>()))
254
.add_property("uv_nodes", py::make_getter(&FaceUnwrapper::uv_nodes, py::return_value_policy<py::return_by_value>()))
255
.add_property("ze_nodes", py::make_getter(&FaceUnwrapper::ze_nodes, py::return_value_policy<py::return_by_value>()))
256
.add_property("ze_poles", py::make_getter(&FaceUnwrapper::ze_poles, py::return_value_policy<py::return_by_value>()))
257
.add_property("A", py::make_getter(&FaceUnwrapper::A, py::return_value_policy<py::return_by_value>()));
259
fm::eigen_matrix<spMat>::to_python_converter();
260
fm::eigen_matrix<ColMat<double, 2>>::to_python_converter();
261
fm::eigen_matrix<ColMat<double, 3>>::to_python_converter();
262
fm::eigen_matrix<ColMat<long, 1>>::to_python_converter();
263
fm::eigen_matrix<ColMat<long, 3>>::to_python_converter();