FreeCAD

Форк
0
/
MeshFlatteningBoostPython.cpp 
265 строк · 11.0 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2017 Lorenz Lechner                                     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <TopoDS.hxx>
26
#include <TopoDS_Edge.hxx>
27
#include <TopoDS_Face.hxx>
28
#include <map>
29
#include <memory>
30
#include <stdexcept>
31
#include <vector>
32
#endif
33

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>
37
#include <Eigen/Core>
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>
49

50
#include "MeshFlattening.h"
51
#include "MeshFlatteningLscmRelax.h"
52
#include "MeshFlatteningNurbs.h"
53

54

55
// clang-format off
56
namespace py = boost::python;
57

58
const TopoDS_Face& getTopoDSFace(const py::object& face)
59
{
60
    if (PyObject_TypeCheck(face.ptr(), &(Part::TopoShapeFacePy::Type)))
61
    {
62
        const Part::TopoShapeFacePy* fpy = static_cast<Part::TopoShapeFacePy*>(face.ptr());
63
        const TopoDS_Face& myFace = TopoDS::Face(fpy->getTopoShapePtr()->getShape());
64
        return myFace;
65
    }
66

67
    throw std::invalid_argument("must be a face");
68
}
69

70
const TopoDS_Edge& getTopoDSEdge(py::object* edge)
71
{
72
    if (PyObject_TypeCheck(edge->ptr(), &(Part::TopoShapeEdgePy::Type)))
73
    {
74
        const Part::TopoShapeEdgePy* epy = static_cast<Part::TopoShapeEdgePy*>(edge->ptr());
75
        const TopoDS_Edge& myEdge = TopoDS::Edge(epy->getTopoShapePtr()->getShape());
76
        return myEdge;
77
    }
78

79
    throw std::invalid_argument("must be an edge");
80
}
81

82
Py::Object makeEdge(const TopoDS_Edge& edge)
83
{
84
    return Py::asObject(new Part::TopoShapeEdgePy(new Part::TopoShape(edge)));
85
}
86

87
std::shared_ptr<FaceUnwrapper> FaceUnwrapper_face(const py::object& face)
88
{
89
    const TopoDS_Face& myFace = getTopoDSFace(face);
90
    return std::make_shared<FaceUnwrapper>(myFace);
91
}
92

93
std::shared_ptr<FaceUnwrapper> FaceUnwrapper_mesh(const py::object& points,
94
                                                    const py::object& facets)
95
{
96
    try {
97
        Py::Sequence l1(points.ptr());
98
        ColMat<double, 3> coords;
99
        coords.resize(l1.size(), 3);
100
        int row = 0;
101
        for (Py::Sequence::iterator it = l1.begin(); it != l1.end(); ++it, ++row) {
102
            Py::Sequence c(*it);
103
            int col = 0;
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;
107
            }
108
        }
109

110
        Py::Sequence l2(facets.ptr());
111
        ColMat<long, 3> triangles;
112
        triangles.resize(l2.size(), 3);
113
        row = 0;
114
        for (Py::Sequence::iterator it = l2.begin(); it != l2.end(); ++it, ++row) {
115
            Py::Sequence c(*it);
116
            int col = 0;
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;
120
            }
121
        }
122

123
        return std::shared_ptr<FaceUnwrapper>(new FaceUnwrapper(coords, triangles));
124
    }
125
    catch (const Py::Exception&) {
126
        Base::PyException e;
127
        throw std::invalid_argument(e.what());
128
    }
129
}
130

131
boost::python::list interpolateFlatFacePy(FaceUnwrapper& instance, const py::object& face)
132
{
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);
142
            vec.append(c);
143
        }
144
        plist.append(vec);
145
    }
146
    return plist;
147
}
148

149
boost::python::list getFlatBoundaryNodesPy(FaceUnwrapper& instance)
150
{
151
    std::vector<ColMat<double, 3>> mat_array = instance.getFlatBoundaryNodes();
152

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);
162
                vec.append(c);
163
            }
164
            plist.append(vec);
165
        }
166

167
        ary.append(plist);
168
    }
169
    return ary;
170
}
171

172
namespace fm {
173
// https://www.boost.org/doc/libs/1_52_0/libs/python/doc/v2/faq.html
174
template<typename eigen_type>
175
struct eigen_matrix
176
{
177
    static PyObject* convert(const eigen_type& mat)
178
    {
179
        // <class 'numpy.array'>
180
        py::list ary;
181
        for (int i = 0; i < mat.rows(); i++) {
182
            py::list row;
183
            for (int j = 0; j < mat.cols(); j++) {
184
                row.append(mat.coeff(i ,j));
185
            }
186
            ary.append(row);
187
        }
188
        return boost::python::incref(ary.ptr());
189
    }
190
    static void to_python_converter()
191
    {
192
        py::to_python_converter<eigen_type, eigen_matrix<eigen_type>>();
193
    }
194
};
195
} // namespace fm
196

197
BOOST_PYTHON_MODULE(flatmesh)
198
{
199
    //m.doc() = "functions to unwrapp faces/ meshes";
200

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);
213

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);
230

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);
244

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>()));
258

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();
264
}
265
// clang-format on
266

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.