File size: 23,699 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <algorithm>
#include <map>
#include <memory>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObjectPy.h>
#include <Base/GeometryPyCXX.h>
#include <Base/Interpreter.h>
#include <Base/PlacementPy.h>
#include <Base/PyWrapParseTupleAndKeywords.h>
#include <Base/VectorPy.h>
#include "Core/Approximation.h"
#include "Core/Evaluation.h"
#include "Core/Iterator.h"
#include "Core/MeshIO.h"
#include "Core/MeshKernel.h"
#include "WildMagic4/Wm4ContBox3.h"
#include "Exporter.h"
#include "Importer.h"
#include "Mesh.h"
#include "MeshPy.h"
using namespace Mesh;
using namespace MeshCore;
namespace Mesh
{
class Module: public Py::ExtensionModule<Module>
{
public:
Module()
: Py::ExtensionModule<Module>("Mesh")
{
add_varargs_method("read", &Module::read, "Read a mesh from a file and returns a Mesh object.");
add_varargs_method(
"open",
&Module::open,
"open(string)\n"
"Create a new document and a Mesh feature to load the file into\n"
"the document."
);
add_varargs_method(
"insert",
&Module::importer,
"insert(string|mesh,[string])\n"
"Load or insert a mesh into the given or active document."
);
add_keyword_method(
"export",
&Module::exporter,
"export(objects, filename, [tolerance=0.1, exportAmfCompressed=True])\n"
"Export a list of objects into a single file identified by filename.\n"
"tolerance is in mm and specifies the maximum acceptable deviation\n"
"between the specified objects and the exported mesh.\n"
"exportAmfCompressed specifies whether exported AMF files should be\n"
"compressed.\n"
);
add_varargs_method(
"show",
&Module::show,
"show(shape,[string]) -- Add the mesh to the active document or create "
"one if no document exists. Returns document object."
);
add_varargs_method("createBox", &Module::createBox, "Create a solid mesh box");
add_varargs_method("createPlane", &Module::createPlane, "Create a mesh XY plane normal +Z");
add_varargs_method("createSphere", &Module::createSphere, "Create a tessellated sphere");
add_varargs_method("createEllipsoid", &Module::createEllipsoid, "Create a tessellated ellipsoid");
add_varargs_method("createCylinder", &Module::createCylinder, "Create a tessellated cylinder");
add_varargs_method("createCone", &Module::createCone, "Create a tessellated cone");
add_varargs_method("createTorus", &Module::createTorus, "Create a tessellated torus");
add_varargs_method(
"calculateEigenTransform",
&Module::calculateEigenTransform,
"calculateEigenTransform(seq(Base.Vector))\n"
"Calculates the eigen Transformation from a list of points.\n"
"calculate the point's local coordinate system with the center\n"
"of gravity as origin. The local coordinate system is computed\n"
"this way that u has minimum and w has maximum expansion.\n"
"The local coordinate system is right-handed.\n"
);
add_varargs_method(
"polynomialFit",
&Module::polynomialFit,
"polynomialFit(seq(Base.Vector)) -- Calculates a polynomial fit."
);
add_varargs_method(
"minimumVolumeOrientedBox",
&Module::minimumVolumeOrientedBox,
"minimumVolumeOrientedBox(seq(Base.Vector)) -- Calculates the minimum\n"
"volume oriented box containing all points. The return value is a\n"
"tuple of seven items:\n"
" center, u, v, w directions and the lengths of the three vectors.\n"
);
initialize(
"The functions in this module allow working with mesh objects.\n"
"A set of functions are provided for reading in registered mesh\n"
"file formats to either a new or existing document.\n"
"\n"
"open(string) -- Create a new document and a Mesh feature\n"
" to load the file into the document.\n"
"insert(string, string) -- Create a Mesh feature to load\n"
" the file into the given document.\n"
"Mesh() -- Create an empty mesh object.\n"
"\n"
);
}
private:
Py::Object invoke_method_varargs(void* method_def, const Py::Tuple& args) override
{
try {
return Py::ExtensionModule<Module>::invoke_method_varargs(method_def, args);
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
catch (const std::exception& e) {
throw Py::RuntimeError(e.what());
}
}
Py::Object read(const Py::Tuple& args)
{
char* Name {};
if (!PyArg_ParseTuple(args.ptr(), "et", "utf-8", &Name)) {
throw Py::Exception();
}
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
std::unique_ptr<MeshObject> mesh(new MeshObject);
mesh->load(EncodedName.c_str());
return Py::asObject(new MeshPy(mesh.release()));
}
Py::Object open(const Py::Tuple& args)
{
char* Name {};
if (!PyArg_ParseTuple(args.ptr(), "et", "utf-8", &Name)) {
throw Py::Exception();
}
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
// create new document and add Import feature
App::Document* pcDoc = App::GetApplication().newDocument();
Mesh::Importer import(pcDoc);
import.load(EncodedName);
return Py::None();
}
Py::Object importer(const Py::Tuple& args)
{
char* Name {};
char* DocName = nullptr;
if (!PyArg_ParseTuple(args.ptr(), "et|s", "utf-8", &Name, &DocName)) {
throw Py::Exception();
}
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
App::Document* pcDoc = nullptr;
if (DocName) {
pcDoc = App::GetApplication().getDocument(DocName);
}
else {
pcDoc = App::GetApplication().getActiveDocument();
}
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument(DocName);
}
Mesh::Importer import(pcDoc);
import.load(EncodedName);
return Py::None();
}
Py::Object exporter(const Py::Tuple& args, const Py::Dict& keywds)
{
PyObject* objects {};
char* fileNamePy {};
// If tolerance is specified via python interface, use that.
// If not, use the preference, if that exists, else default to 0.1mm.
auto hGrp(
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Mesh")
);
auto fTolerance(hGrp->GetFloat("MaxDeviationExport", 0.1F));
int exportAmfCompressed(hGrp->GetBool("ExportAmfCompressed", true));
bool export3mfModel(hGrp->GetBool("Export3mfModel", true));
static const std::array<const char*, 5>
kwList {"objectList", "filename", "tolerance", "exportAmfCompressed", nullptr};
if (!Base::Wrapped_ParseTupleAndKeywords(
args.ptr(),
keywds.ptr(),
"Oet|dp",
kwList,
&objects,
"utf-8",
&fileNamePy,
&fTolerance,
&exportAmfCompressed
)) {
throw Py::Exception();
}
std::string outputFileName(fileNamePy);
PyMem_Free(fileNamePy);
// Construct list of objects to export before making the Exporter, so
// we don't get empty exports if the list can't be constructed.
Py::Sequence list(objects);
if (list.length() == 0) {
return Py::None();
}
// collect all object types that can be exported as mesh
std::vector<App::DocumentObject*> objectList;
for (const auto& it : list) {
PyObject* item = it.ptr();
if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) {
auto obj(static_cast<App::DocumentObjectPy*>(item)->getDocumentObjectPtr());
objectList.push_back(obj);
}
}
if (objectList.empty()) {
throw Py::TypeError("None of the objects can be exported to a mesh file");
}
auto exportFormat(MeshOutput::GetFormat(outputFileName.c_str()));
std::unique_ptr<Exporter> exporter;
if (exportFormat == MeshIO::AMF) {
std::map<std::string, std::string> meta;
meta["cad"] = App::Application::Config()["ExeName"] + " "
+ App::Application::Config()["ExeVersion"];
meta[App::Application::Config()["ExeName"] + "-buildRevisionHash"]
= App::Application::Config()["BuildRevisionHash"];
exporter = std::make_unique<ExporterAMF>(outputFileName, meta, exportAmfCompressed);
}
else if (exportFormat == MeshIO::ThreeMF) {
Extension3MFFactory::initialize();
exporter = std::make_unique<Exporter3MF>(
outputFileName,
Extension3MFFactory::createExtensions()
);
dynamic_cast<Exporter3MF*>(exporter.get())->setForceModel(export3mfModel);
}
else if (exportFormat != MeshIO::Undefined) {
exporter = std::make_unique<MergeExporter>(outputFileName, exportFormat);
}
else {
std::string exStr(
"Cannot determine the mesh format from the file name.\nSpecify mesh "
"format file extension: '"
);
exStr += outputFileName + "'";
throw Py::ValueError(exStr.c_str());
}
for (auto it : objectList) {
exporter->addObject(it, fTolerance);
}
exporter.reset(); // deletes Exporter, mesh file is written by destructor
return Py::None();
}
Py::Object show(const Py::Tuple& args)
{
PyObject* pcObj {};
const char* name = "Mesh";
if (!PyArg_ParseTuple(args.ptr(), "O!|s", &(MeshPy::Type), &pcObj, &name)) {
throw Py::Exception();
}
App::Document* pcDoc = App::GetApplication().getActiveDocument();
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument();
}
MeshPy* pMesh = static_cast<MeshPy*>(pcObj);
Mesh::Feature* pcFeature = pcDoc->addObject<Mesh::Feature>(name);
Mesh::MeshObject* mo = pMesh->getMeshObjectPtr();
if (!mo) {
throw Py::Exception(PyExc_ReferenceError, "object does not reference a valid mesh");
}
// copy the data
pcFeature->Mesh.setValue(*mo);
return Py::asObject(pcFeature->getPyObject());
}
Py::Object createBox(const Py::Tuple& args)
{
MeshObject* mesh = nullptr;
do {
float length = 10.0F;
float width = 10.0F;
float height = 10.0F;
float edgelen = -1.0F;
if (PyArg_ParseTuple(args.ptr(), "|ffff", &length, &width, &height, &edgelen)) {
if (edgelen < 0.0F) {
mesh = MeshObject::createCube(length, width, height);
}
else {
mesh = MeshObject::createCube(length, width, height, edgelen);
}
break;
}
PyErr_Clear();
PyObject* box {};
if (PyArg_ParseTuple(args.ptr(), "O!", &Base::BoundBoxPy::Type, &box)) {
Py::BoundingBox bbox(box, false);
mesh = MeshObject::createCube(bbox.getValue());
break;
}
throw Py::TypeError("Must be real numbers or BoundBox");
} while (false);
if (!mesh) {
throw Py::RuntimeError("Creation of box failed");
}
return Py::asObject(new MeshPy(mesh));
}
Py::Object createPlane(const Py::Tuple& args)
{
float x = 1, y = 0, z = 0;
if (!PyArg_ParseTuple(args.ptr(), "|fff", &x, &y, &z)) {
throw Py::Exception();
}
if (y == 0) {
y = x;
}
float hx = x / 2.0F;
float hy = y / 2.0F;
std::vector<MeshCore::MeshGeomFacet> TriaList;
TriaList.emplace_back(
Base::Vector3f(-hx, -hy, 0.0),
Base::Vector3f(hx, hy, 0.0),
Base::Vector3f(-hx, hy, 0.0)
);
TriaList.emplace_back(
Base::Vector3f(-hx, -hy, 0.0),
Base::Vector3f(hx, -hy, 0.0),
Base::Vector3f(hx, hy, 0.0)
);
std::unique_ptr<MeshObject> mesh(new MeshObject);
mesh->addFacets(TriaList);
return Py::asObject(new MeshPy(mesh.release()));
}
Py::Object createSphere(const Py::Tuple& args)
{
float radius = 5.0F;
int sampling = 50;
if (!PyArg_ParseTuple(args.ptr(), "|fi", &radius, &sampling)) {
throw Py::Exception();
}
MeshObject* mesh = MeshObject::createSphere(radius, sampling);
if (!mesh) {
throw Py::RuntimeError("Creation of sphere failed");
}
return Py::asObject(new MeshPy(mesh));
}
Py::Object createEllipsoid(const Py::Tuple& args)
{
float radius1 = 2.0F;
float radius2 = 4.0F;
int sampling = 50;
if (!PyArg_ParseTuple(args.ptr(), "|ffi", &radius1, &radius2, &sampling)) {
throw Py::Exception();
}
MeshObject* mesh = MeshObject::createEllipsoid(radius1, radius2, sampling);
if (!mesh) {
throw Py::RuntimeError("Creation of ellipsoid failed");
}
return Py::asObject(new MeshPy(mesh));
}
Py::Object createCylinder(const Py::Tuple& args)
{
float radius = 2.0F;
float length = 10.0F;
int closed = 1;
float edgelen = 1.0F;
int sampling = 50;
if (!PyArg_ParseTuple(args.ptr(), "|ffifi", &radius, &length, &closed, &edgelen, &sampling)) {
throw Py::Exception();
}
MeshObject* mesh = MeshObject::createCylinder(radius, length, closed, edgelen, sampling);
if (!mesh) {
throw Py::RuntimeError("Creation of cylinder failed");
}
return Py::asObject(new MeshPy(mesh));
}
Py::Object createCone(const Py::Tuple& args)
{
float radius1 = 2.0F;
float radius2 = 4.0F;
float len = 10.0F;
int closed = 1;
float edgelen = 1.0F;
int sampling = 50;
if (!PyArg_ParseTuple(args.ptr(), "|fffifi", &radius1, &radius2, &len, &closed, &edgelen, &sampling)) {
throw Py::Exception();
}
MeshObject* mesh = MeshObject::createCone(radius1, radius2, len, closed, edgelen, sampling);
if (!mesh) {
throw Py::RuntimeError("Creation of cone failed");
}
return Py::asObject(new MeshPy(mesh));
}
Py::Object createTorus(const Py::Tuple& args)
{
float radius1 = 10.0F;
float radius2 = 2.0F;
int sampling = 50;
if (!PyArg_ParseTuple(args.ptr(), "|ffi", &radius1, &radius2, &sampling)) {
throw Py::Exception();
}
MeshObject* mesh = MeshObject::createTorus(radius1, radius2, sampling);
if (!mesh) {
throw Py::RuntimeError("Creation of torus failed");
}
return Py::asObject(new MeshPy(mesh));
}
Py::Object calculateEigenTransform(const Py::Tuple& args)
{
PyObject* input {};
if (!PyArg_ParseTuple(args.ptr(), "O", &input)) {
throw Py::Exception();
}
if (!PySequence_Check(input)) {
throw Py::TypeError("Input has to be a sequence of Base.Vector()");
}
MeshCore::MeshKernel aMesh;
MeshCore::MeshPointArray vertices;
vertices.clear();
MeshCore::MeshFacetArray faces;
faces.clear();
MeshCore::MeshPoint current_node;
Py::Sequence list(input);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
PyObject* value = (*it).ptr();
if (PyObject_TypeCheck(value, &(Base::VectorPy::Type))) {
Base::VectorPy* pcObject = static_cast<Base::VectorPy*>(value);
Base::Vector3d* val = pcObject->getVectorPtr();
current_node.Set(float(val->x), float(val->y), float(val->z));
vertices.push_back(current_node);
}
}
MeshCore::MeshFacet aFacet;
aFacet._aulPoints[0] = 0;
aFacet._aulPoints[1] = 1;
aFacet._aulPoints[2] = 2;
faces.push_back(aFacet);
// Fill the Kernel with the temp mesh structure and delete the current containers
aMesh.Adopt(vertices, faces);
MeshCore::MeshEigensystem pca(aMesh);
pca.Evaluate();
Base::Matrix4D Trafo = pca.Transform();
return Py::asObject(new Base::PlacementPy(new Base::Placement(Trafo)));
}
Py::Object polynomialFit(const Py::Tuple& args)
{
PyObject* input {};
if (!PyArg_ParseTuple(args.ptr(), "O", &input)) {
throw Py::Exception();
}
if (!PySequence_Check(input)) {
throw Py::TypeError("Input has to be a sequence of Base.Vector()");
}
MeshCore::SurfaceFit polyFit;
Base::Vector3f point;
Py::Sequence list(input);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
PyObject* value = (*it).ptr();
if (PyObject_TypeCheck(value, &(Base::VectorPy::Type))) {
Base::VectorPy* pcObject = static_cast<Base::VectorPy*>(value);
Base::Vector3d* val = pcObject->getVectorPtr();
point.Set(float(val->x), float(val->y), float(val->z));
polyFit.AddPoint(point);
}
}
// fit quality
float fit = polyFit.Fit();
Py::Dict dict;
dict.setItem(Py::String("Sigma"), Py::Float(fit));
// coefficients
double a {}, b {}, c {}, d {}, e {}, f {};
polyFit.GetCoefficients(a, b, c, d, e, f);
Py::Tuple p(6);
p.setItem(0, Py::Float(a));
p.setItem(1, Py::Float(b));
p.setItem(2, Py::Float(c));
p.setItem(3, Py::Float(d));
p.setItem(4, Py::Float(e));
p.setItem(5, Py::Float(f));
dict.setItem(Py::String("Coefficients"), p);
// residuals
std::vector<Base::Vector3f> local = polyFit.GetLocalPoints();
Py::Tuple r(local.size());
for (auto it = local.begin(); it != local.end(); ++it) {
double z = polyFit.Value(it->x, it->y);
double d = it->z - z;
r.setItem(it - local.begin(), Py::Float(d));
}
dict.setItem(Py::String("Residuals"), r);
return dict; // NOLINT
}
Py::Object minimumVolumeOrientedBox(const Py::Tuple& args)
{
PyObject* input {};
if (!PyArg_ParseTuple(args.ptr(), "O", &input)) {
throw Py::Exception();
}
if (!PySequence_Check(input)) {
throw Py::TypeError("Input has to be a sequence of Base.Vector()");
}
Py::Sequence list(input);
std::vector<Wm4::Vector3d> points;
points.reserve(list.size());
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
PyObject* value = (*it).ptr();
if (PyObject_TypeCheck(value, &(Base::VectorPy::Type))) {
Base::VectorPy* pcObject = static_cast<Base::VectorPy*>(value);
Base::Vector3d* val = pcObject->getVectorPtr();
Wm4::Vector3d pt;
pt[0] = val->x;
pt[1] = val->y;
pt[2] = val->z;
points.push_back(pt);
}
}
if (points.size() < 4) {
throw Py::RuntimeError("Too few points");
}
Wm4::Box3d mobox = Wm4::ContMinBox(points.size(), points.data(), 0.001, Wm4::Query::QT_REAL);
Py::Tuple result(7);
Base::Vector3d v;
v.x = mobox.Center[0];
v.y = mobox.Center[1];
v.z = mobox.Center[2];
result.setItem(0, Py::Vector(v));
v.x = mobox.Axis[0][0];
v.y = mobox.Axis[0][1];
v.z = mobox.Axis[0][2];
result.setItem(1, Py::Vector(v));
v.x = mobox.Axis[1][0];
v.y = mobox.Axis[1][1];
v.z = mobox.Axis[1][2];
result.setItem(2, Py::Vector(v));
v.x = mobox.Axis[2][0];
v.y = mobox.Axis[2][1];
v.z = mobox.Axis[2][2];
result.setItem(3, Py::Vector(v));
result.setItem(4, Py::Float(mobox.Extent[0]));
result.setItem(5, Py::Float(mobox.Extent[1]));
result.setItem(6, Py::Float(mobox.Extent[2]));
return result; // NOLINT
}
};
PyObject* initModule()
{
return Base::Interpreter().addModule(new Module);
}
} // namespace Mesh
|