FreeCAD

Форк
0
/
FacetPyImp.cpp 
370 строк · 11.0 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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

25
#include <Base/GeometryPyCXX.h>
26
#include <Base/VectorPy.h>
27

28
#include "Facet.h"
29
#include "FacetPy.h"
30
#include "FacetPy.cpp"
31
#include "EdgePy.h"
32
#include "Mesh.h"
33

34

35
using namespace Mesh;
36

37
namespace
38
{
39
class Index
40
{
41
    FacetIndex index;
42

43
public:
44
    Index(FacetIndex index)
45
        : index {index}
46
    {}
47

48
    friend std::ostream& operator<<(std::ostream& os, Index idx)
49
    {
50
        if (idx.index < MeshCore::FACET_INDEX_MAX) {
51
            os << idx.index;
52
        }
53
        else {
54
            os << -1;
55
        }
56
        return os;
57
    }
58
};
59
}  // namespace
60

61
// returns a string which represent the object e.g. when printed in python
62
std::string FacetPy::representation() const
63
{
64
    // clang-format off
65
    FacetPy::PointerType ptr = getFacetPtr();
66
    std::stringstream str;
67
    str << "Facet (";
68
    if (ptr->isBound()) {
69
        str << "(" << ptr->_aclPoints[0].x << ", "
70
                   << ptr->_aclPoints[0].y << ", "
71
                   << ptr->_aclPoints[0].z << ", Idx="
72
                   << ptr->PIndex[0] << "), ";
73
        str << "(" << ptr->_aclPoints[1].x << ", "
74
                   << ptr->_aclPoints[1].y << ", "
75
                   << ptr->_aclPoints[1].z << ", Idx="
76
                   << ptr->PIndex[1] << "), ";
77
        str << "(" << ptr->_aclPoints[2].x << ", "
78
                   << ptr->_aclPoints[2].y << ", "
79
                   << ptr->_aclPoints[2].z << ", Idx="
80
                   << ptr->PIndex[2] << "), ";
81
        str << "Idx=" << Index(ptr->Index) << ", ("
82
                      << Index(ptr->NIndex[0]) << ", "
83
                      << Index(ptr->NIndex[1]) << ", "
84
                      << Index(ptr->NIndex[2]) << ")";
85
    }
86
    else {
87
        str << "(" << ptr->_aclPoints[0].x << ", "
88
                   << ptr->_aclPoints[0].y << ", "
89
                   << ptr->_aclPoints[0].z << "), ";
90
        str << "(" << ptr->_aclPoints[1].x << ", "
91
                   << ptr->_aclPoints[1].y << ", "
92
                   << ptr->_aclPoints[1].z << "), ";
93
        str << "(" << ptr->_aclPoints[2].x << ", "
94
                   << ptr->_aclPoints[2].y << ", "
95
                   << ptr->_aclPoints[2].z << ")";
96
    }
97
    str << ")";
98

99
    return str.str();
100
    // clang-format on
101
}
102

103
PyObject* FacetPy::PyMake(struct _typeobject*, PyObject*, PyObject*)  // Python wrapper
104
{
105
    // create a new instance of FacetPy and the Twin object
106
    return new FacetPy(new Facet);
107
}
108

109
// constructor method
110
int FacetPy::PyInit(PyObject* args, PyObject* /*kwds*/)
111
{
112
    if (!PyArg_ParseTuple(args, "")) {
113
        return -1;
114
    }
115
    return 0;
116
}
117

118
PyObject* FacetPy::unbound(PyObject* args)
119
{
120
    if (!PyArg_ParseTuple(args, "")) {
121
        return nullptr;
122
    }
123
    getFacetPtr()->Index = MeshCore::FACET_INDEX_MAX;
124
    getFacetPtr()->Mesh = nullptr;
125
    Py_Return;
126
}
127

128
PyObject* FacetPy::getEdge(PyObject* args)
129
{
130
    int index {};
131
    if (!PyArg_ParseTuple(args, "i", &index)) {
132
        return nullptr;
133
    }
134

135
    Edge edge = getFacetPtr()->getEdge(index);
136
    return new EdgePy(new Edge(edge));
137
}
138

139
Py::Long FacetPy::getIndex() const
140
{
141
    return Py::Long((long)getFacetPtr()->Index);
142
}
143

144
Py::Boolean FacetPy::getBound() const
145
{
146
    return {getFacetPtr()->isBound()};
147
}
148

149
Py::Object FacetPy::getNormal() const
150
{
151
    Base::VectorPy* normal = new Base::VectorPy(getFacetPtr()->GetNormal());
152
    normal->setConst();
153
    return Py::Object(normal, true);
154
}
155

156
PyObject* FacetPy::intersect(PyObject* args)
157
{
158
    PyObject* object {};
159
    if (!PyArg_ParseTuple(args, "O!", &FacetPy::Type, &object)) {
160
        return nullptr;
161
    }
162
    FacetPy* face = static_cast<FacetPy*>(object);
163
    FacetPy::PointerType face_ptr = face->getFacetPtr();
164
    FacetPy::PointerType this_ptr = this->getFacetPtr();
165
    Base::Vector3f p0, p1;
166
    int ret = this_ptr->IntersectWithFacet(*face_ptr, p0, p1);
167

168
    try {
169
        Py::List sct;
170

171
        if (ret > 0) {
172
            Py::Tuple pt(3);
173
            pt.setItem(0, Py::Float(p0.x));
174
            pt.setItem(1, Py::Float(p0.y));
175
            pt.setItem(2, Py::Float(p0.z));
176
            sct.append(pt);
177
        }
178
        if (ret > 1) {
179
            Py::Tuple pt(3);
180
            pt.setItem(0, Py::Float(p1.x));
181
            pt.setItem(1, Py::Float(p1.y));
182
            pt.setItem(2, Py::Float(p1.z));
183
            sct.append(pt);
184
        }
185

186
        return Py::new_reference_to(sct);
187
    }
188
    catch (const Py::Exception&) {
189
        return nullptr;
190
    }
191
}
192

193
PyObject* FacetPy::isDegenerated(PyObject* args)
194
{
195
    float fEpsilon = MeshCore::MeshDefinitions::_fMinPointDistanceP2;
196
    if (!PyArg_ParseTuple(args, "|f", &fEpsilon)) {
197
        return nullptr;
198
    }
199

200
    FacetPy::PointerType face = this->getFacetPtr();
201
    if (!face->isBound()) {
202
        throw Py::RuntimeError("Unbound facet");
203
    }
204

205
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
206
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
207
    return Py::new_reference_to(Py::Boolean(tria.IsDegenerated(fEpsilon)));
208
}
209

210
PyObject* FacetPy::isDeformed(PyObject* args)
211
{
212
    float fMinAngle {};
213
    float fMaxAngle {};
214
    if (!PyArg_ParseTuple(args, "ff", &fMinAngle, &fMaxAngle)) {
215
        return nullptr;
216
    }
217

218
    FacetPy::PointerType face = this->getFacetPtr();
219
    if (!face->isBound()) {
220
        throw Py::RuntimeError("Unbound facet");
221
    }
222

223
    float fCosOfMinAngle = cos(fMinAngle);
224
    float fCosOfMaxAngle = cos(fMaxAngle);
225
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
226
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
227
    return Py::new_reference_to(Py::Boolean(tria.IsDeformed(fCosOfMinAngle, fCosOfMaxAngle)));
228
}
229

230
Py::List FacetPy::getPoints() const
231
{
232
    FacetPy::PointerType face = this->getFacetPtr();
233

234
    Py::List pts;
235
    for (const auto& vec : face->_aclPoints) {
236
        Py::Tuple pt(3);
237
        pt.setItem(0, Py::Float(vec.x));
238
        pt.setItem(1, Py::Float(vec.y));
239
        pt.setItem(2, Py::Float(vec.z));
240
        pts.append(pt);
241
    }
242

243
    return pts;
244
}
245

246
Py::Tuple FacetPy::getPointIndices() const
247
{
248
    FacetPy::PointerType face = this->getFacetPtr();
249
    if (!face->isBound()) {
250
        return Py::Tuple();
251
    }
252

253
    Py::Tuple idxTuple(3);
254
    for (int i = 0; i < 3; i++) {
255
        idxTuple.setItem(i, Py::Long(face->PIndex[i]));
256
    }
257
    return idxTuple;
258
}
259

260
Py::Tuple FacetPy::getNeighbourIndices() const
261
{
262
    FacetPy::PointerType face = this->getFacetPtr();
263
    if (!face->isBound()) {
264
        return Py::Tuple();
265
    }
266

267
    Py::Tuple idxTuple(3);
268
    for (int i = 0; i < 3; i++) {
269
        auto index = face->NIndex[i];
270
        if (index < MeshCore::FACET_INDEX_MAX) {
271
            idxTuple.setItem(i, Py::Long(index));
272
        }
273
        else {
274
            idxTuple.setItem(i, Py::Long(-1L));
275
        }
276
    }
277
    return idxTuple;
278
}
279

280
Py::Float FacetPy::getArea() const
281
{
282
    FacetPy::PointerType face = this->getFacetPtr();
283
    if (!face->isBound()) {
284
        return Py::Float(0.0);
285
    }
286

287
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
288
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
289
    return Py::Float(tria.Area());
290
}
291

292
Py::Float FacetPy::getAspectRatio() const
293
{
294
    FacetPy::PointerType face = this->getFacetPtr();
295
    if (!face->isBound()) {
296
        return Py::Float(-1.0);
297
    }
298

299
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
300
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
301
    return Py::Float(tria.AspectRatio());
302
}
303

304
Py::Float FacetPy::getAspectRatio2() const
305
{
306
    FacetPy::PointerType face = this->getFacetPtr();
307
    if (!face->isBound()) {
308
        return Py::Float(-1.0);
309
    }
310

311
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
312
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
313
    return Py::Float(tria.AspectRatio2());
314
}
315

316
Py::Float FacetPy::getRoundness() const
317
{
318
    FacetPy::PointerType face = this->getFacetPtr();
319
    if (!face->isBound()) {
320
        return Py::Float(-1.0);
321
    }
322

323
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
324
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
325
    return Py::Float(tria.Roundness());
326
}
327

328
Py::Tuple FacetPy::getCircumCircle() const
329
{
330
    FacetPy::PointerType face = this->getFacetPtr();
331
    if (!face->isBound()) {
332
        return Py::None();
333
    }
334

335
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
336
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
337
    Base::Vector3f center;
338
    float radius = tria.CenterOfCircumCircle(center);
339
    Py::Tuple tuple(2);
340
    tuple.setItem(0, Py::Vector(center));
341
    tuple.setItem(1, Py::Float(radius));
342
    return tuple;
343
}
344

345
Py::Tuple FacetPy::getInCircle() const
346
{
347
    FacetPy::PointerType face = this->getFacetPtr();
348
    if (!face->isBound()) {
349
        return Py::None();
350
    }
351

352
    const MeshCore::MeshKernel& kernel = face->Mesh->getKernel();
353
    MeshCore::MeshGeomFacet tria = kernel.GetFacet(face->Index);
354
    Base::Vector3f center;
355
    float radius = tria.CenterOfInscribedCircle(center);
356
    Py::Tuple tuple(2);
357
    tuple.setItem(0, Py::Vector(center));
358
    tuple.setItem(1, Py::Float(radius));
359
    return tuple;
360
}
361

362
PyObject* FacetPy::getCustomAttributes(const char* /*attr*/) const
363
{
364
    return nullptr;
365
}
366

367
int FacetPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
368
{
369
    return 0;
370
}
371

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

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

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

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