FreeCAD WebAssembly development snapshot
Some checks failed
FreeCAD master CI / Prepare (push) Has been cancelled
FreeCAD master CI / Pixi (push) Has been cancelled
FreeCAD master CI / Ubuntu (push) Has been cancelled
FreeCAD master CI / Windows (push) Has been cancelled
FreeCAD master CI / Lint (push) Has been cancelled
FreeCAD master CI / WrapUp (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Stale Issues / stale (push) Has been cancelled

This commit is contained in:
2026-08-20 07:00:53 +08:00
commit b5a086fda5
12718 changed files with 7841410 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
add_executable(Mesh_tests_run
Core/KDTree.cpp
Exporter.cpp
Importer.cpp
Mesh.cpp
MeshFeature.cpp
)
target_compile_definitions(Mesh_tests_run PRIVATE DATADIR="${CMAKE_SOURCE_DIR}/data")

View File

@@ -0,0 +1,95 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <Mod/Mesh/App/Core/KDTree.h>
// NOLINTBEGIN(cppcoreguidelines-*,readability-*)
class KDTreeTest: public ::testing::Test
{
protected:
void SetUp() override
{
points.emplace_back(0.F, 0.F, 0.F);
points.emplace_back(0.F, 0.F, 1.F);
points.emplace_back(0.F, 1.F, 0.F);
points.emplace_back(0.F, 1.F, 1.F);
points.emplace_back(1.F, 0.F, 0.F);
points.emplace_back(1.F, 0.F, 1.F);
points.emplace_back(1.F, 1.F, 0.F);
points.emplace_back(1.F, 1.F, 1.F);
}
void TearDown() override
{}
const std::vector<Base::Vector3f>& GetPoints() const
{
return points;
}
private:
std::vector<Base::Vector3f> points;
};
TEST_F(KDTreeTest, TestKDTreeEmpty)
{
MeshCore::MeshKDTree tree;
EXPECT_EQ(tree.IsEmpty(), true);
}
TEST_F(KDTreeTest, TestKDTreeNearestEmpty)
{
MeshCore::MeshKDTree tree;
Base::Vector3f pnt;
Base::Vector3f nor;
float dist;
EXPECT_EQ(tree.FindNearest(pnt, nor, dist), MeshCore::POINT_INDEX_MAX);
}
TEST_F(KDTreeTest, TestKDTreeNearest)
{
MeshCore::MeshKDTree tree;
tree.AddPoints(GetPoints());
EXPECT_EQ(tree.IsEmpty(), false);
Base::Vector3f nor;
float dist;
EXPECT_EQ(tree.FindNearest(Base::Vector3f(0.9F, 0.1F, 0.1F), nor, dist), 4);
}
TEST_F(KDTreeTest, TestKDTreeNearestMaxDist)
{
MeshCore::MeshKDTree tree;
tree.AddPoints(GetPoints());
EXPECT_EQ(tree.IsEmpty(), false);
Base::Vector3f nor;
float dist;
EXPECT_EQ(
tree.FindNearest(Base::Vector3f(0.9F, 0.1F, 0.1F), 0.0F, nor, dist),
MeshCore::POINT_INDEX_MAX
);
}
TEST_F(KDTreeTest, TestKDTreeFindExact)
{
MeshCore::MeshKDTree tree;
tree.AddPoints(GetPoints());
EXPECT_EQ(tree.FindExact(Base::Vector3f(0.1F, 0, 0)), MeshCore::POINT_INDEX_MAX);
EXPECT_EQ(tree.FindExact(Base::Vector3f(0, 0, 0)), 0);
}
TEST_F(KDTreeTest, TestKDTreeFindRange)
{
MeshCore::MeshKDTree tree;
tree.AddPoints(GetPoints());
std::vector<MeshCore::PointIndex> index;
std::vector<MeshCore::PointIndex> result = {0, 4};
tree.FindInRange(Base::Vector3f(0.5F, 0, 0), 0.6F, index);
EXPECT_EQ(index, result);
}
// NOLINTEND(cppcoreguidelines-*,readability-*)

View File

@@ -0,0 +1,152 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <Base/FileInfo.h>
#include <Base/Interpreter.h>
#include <App/Document.h>
#include <App/Part.h>
#include <src/App/InitApplication.h>
#include <Mod/Mesh/App/Exporter.h>
#include <Mod/Mesh/App/FeatureMeshSolid.h>
#include <Mod/Mesh/App/Mesh.h>
class ExporterTest: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
void SetUp() override
{
document = App::GetApplication().newDocument("MeshExport");
fileInfo.setFile(Base::FileInfo::getTempFileName() + ".stl");
const double value = 10.0;
cube1 = document->addObject<Mesh::Cube>("Cube1");
cube1->Length.setValue(value);
cube1->Width.setValue(value);
cube1->Height.setValue(value);
cube2 = document->addObject<Mesh::Cube>("Cube2");
cube2->Length.setValue(value);
cube2->Width.setValue(value);
cube2->Height.setValue(value);
document->recompute();
}
void TearDown() override
{
App::GetApplication().closeDocument(document->getName());
fileInfo.deleteFile();
}
App::Document* getDocument() const
{
return document;
}
std::vector<App::DocumentObject*> getObjects() const
{
return {cube1, cube2};
}
App::DocumentObject* getMesh2() const
{
return cube2;
}
void setPlacementTo2ndCube(const Base::Placement& plm)
{
cube2->Placement.setValue(plm);
}
std::string getFileName() const
{
return fileInfo.filePath();
}
private:
Base::FileInfo fileInfo;
App::Document* document {};
Mesh::Cube* cube1 {};
Mesh::Cube* cube2 {};
};
// NOLINTBEGIN(cppcoreguidelines-*,readability-*)
TEST_F(ExporterTest, TestSingleMesh)
{
Base::Placement plm;
plm.setPosition(Base::Vector3d(10, 5, 2));
setPlacementTo2ndCube(plm);
// add extra scope because the file will be written when destroying the exporter
{
Mesh::MergeExporter exporter(getFileName(), MeshCore::MeshIO::Format::STL);
exporter.addObject(getMesh2(), 0.1F);
}
Mesh::MeshObject kernel;
EXPECT_TRUE(kernel.load(getFileName().c_str()));
auto bbox = kernel.getBoundBox();
EXPECT_DOUBLE_EQ(bbox.MinX, 5.0);
EXPECT_DOUBLE_EQ(bbox.MaxX, 15.0);
EXPECT_DOUBLE_EQ(bbox.MinY, 0.0);
EXPECT_DOUBLE_EQ(bbox.MaxY, 10.0);
EXPECT_DOUBLE_EQ(bbox.MinZ, -3.0);
EXPECT_DOUBLE_EQ(bbox.MaxZ, 7.0);
}
// Test for https://github.com/FreeCAD/FreeCAD/pull/11539
TEST_F(ExporterTest, TestMultipleMeshes)
{
Base::Placement plm;
plm.setPosition(Base::Vector3d(10, 5, 2));
setPlacementTo2ndCube(plm);
// add extra scope because the file will be written when destroying the exporter
{
Mesh::MergeExporter exporter(getFileName(), MeshCore::MeshIO::Format::STL);
for (auto obj : getObjects()) {
exporter.addObject(obj, 0.1F);
}
}
Mesh::MeshObject kernel;
EXPECT_TRUE(kernel.load(getFileName().c_str()));
auto bbox = kernel.getBoundBox();
EXPECT_DOUBLE_EQ(bbox.MinX, -5.0);
EXPECT_DOUBLE_EQ(bbox.MaxX, 15.0);
EXPECT_DOUBLE_EQ(bbox.MinY, -5.0);
EXPECT_DOUBLE_EQ(bbox.MaxY, 10.0);
EXPECT_DOUBLE_EQ(bbox.MinZ, -5.0);
EXPECT_DOUBLE_EQ(bbox.MaxZ, 7.0);
}
TEST_F(ExporterTest, TestMeshesInPart)
{
Base::Placement plm;
plm.setPosition(Base::Vector3d(10, 5, 2));
setPlacementTo2ndCube(plm);
// add extra scope because the file will be written when destroying the exporter
{
auto part = getDocument()->addObject<App::Part>("Part");
part->placement().setValue(plm);
part->addObjects(getObjects());
Mesh::MergeExporter exporter(getFileName(), MeshCore::MeshIO::Format::STL);
exporter.addObject(part, 0.1F);
}
Mesh::MeshObject kernel;
EXPECT_TRUE(kernel.load(getFileName().c_str()));
auto bbox = kernel.getBoundBox();
EXPECT_DOUBLE_EQ(bbox.MinX, 5.0);
EXPECT_DOUBLE_EQ(bbox.MaxX, 25.0);
EXPECT_DOUBLE_EQ(bbox.MinY, 0.0);
EXPECT_DOUBLE_EQ(bbox.MaxY, 15.0);
EXPECT_DOUBLE_EQ(bbox.MinZ, -3.0);
EXPECT_DOUBLE_EQ(bbox.MaxZ, 9.0);
}
// NOLINTEND(cppcoreguidelines-*,readability-*)

View File

@@ -0,0 +1,56 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <Base/FileInfo.h>
#include <Mod/Mesh/App/Core/IO/Reader3MF.h>
#include <Mod/Mesh/App/Core/IO/ReaderOBJ.h>
#include <xercesc/util/PlatformUtils.hpp>
#include <zipios++/fcoll.h>
class ImporterTest: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
XERCES_CPP_NAMESPACE::XMLPlatformUtils::Initialize();
}
};
// NOLINTBEGIN(cppcoreguidelines-*,readability-*)
TEST_F(ImporterTest, Test3MF)
{
std::string file(DATADIR);
file.append("/tests/mesh.3mf");
MeshCore::Reader3MF reader(file);
EXPECT_EQ(reader.Load(), true);
std::vector<int> ids = reader.GetMeshIds();
std::sort(ids.begin(), ids.end());
EXPECT_EQ(ids.size(), 2);
const MeshCore::MeshKernel& mesh1 = reader.GetMesh(ids[0]);
EXPECT_EQ(mesh1.CountPoints(), 8);
EXPECT_EQ(mesh1.CountEdges(), 18);
EXPECT_EQ(mesh1.CountFacets(), 12);
const MeshCore::MeshKernel& mesh2 = reader.GetMesh(ids[1]);
EXPECT_EQ(mesh2.CountPoints(), 652);
EXPECT_EQ(mesh2.CountEdges(), 1950);
EXPECT_EQ(mesh2.CountFacets(), 1300);
}
TEST_F(ImporterTest, TestOBJ)
{
std::string file(DATADIR);
file.append("/tests/mesh.obj");
MeshCore::MeshKernel kernel;
MeshCore::ReaderOBJ reader(kernel, nullptr);
EXPECT_EQ(reader.Load(file), true);
EXPECT_EQ(kernel.CountPoints(), 8);
EXPECT_EQ(kernel.CountFacets(), 12);
}
// NOLINTEND(cppcoreguidelines-*,readability-*)

View File

@@ -0,0 +1,111 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <Mod/Mesh/App/Mesh.h>
#include <Mod/Mesh/App/Core/Grid.h>
#include <src/App/InitApplication.h>
class MeshTest: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
};
// NOLINTBEGIN(cppcoreguidelines-*,readability-*)
TEST_F(MeshTest, TestDefault)
{
MeshCore::MeshKernel kernel;
Base::Vector3f p1 {0, 0, 0};
Base::Vector3f p2 {0, 0, 1};
Base::Vector3f p3 {0, 1, 0};
kernel.AddFacet(MeshCore::MeshGeomFacet(p1, p2, p3));
EXPECT_EQ(kernel.CountPoints(), 3);
EXPECT_EQ(kernel.CountEdges(), 3);
EXPECT_EQ(kernel.CountFacets(), 1);
}
TEST_F(MeshTest, TestGrid1OfPlanarMesh)
{
MeshCore::MeshKernel kernel;
Base::Vector3f p1 {0, 0, 0};
Base::Vector3f p2 {1, 0, 0};
Base::Vector3f p3 {0, 1, 0};
Base::Vector3f p4 {1, 1, 0};
kernel.AddFacet(MeshCore::MeshGeomFacet(p1, p2, p3));
kernel.AddFacet(MeshCore::MeshGeomFacet(p3, p2, p4));
MeshCore::MeshFacetGrid grid(kernel, 10);
unsigned long countX {};
unsigned long countY {};
unsigned long countZ {};
grid.GetCtGrids(countX, countY, countZ);
EXPECT_EQ(countX, 1);
EXPECT_EQ(countY, 1);
EXPECT_EQ(countZ, 1);
}
TEST_F(MeshTest, TestGrid2OfPlanarMesh)
{
MeshCore::MeshKernel kernel;
Base::Vector3f p1 {0, 0, 0};
Base::Vector3f p2 {1, 0, 0};
Base::Vector3f p3 {0, 1, 0};
Base::Vector3f p4 {1, 1, 0};
kernel.AddFacet(MeshCore::MeshGeomFacet(p1, p2, p3));
kernel.AddFacet(MeshCore::MeshGeomFacet(p3, p2, p4));
MeshCore::MeshFacetGrid grid(kernel);
unsigned long countX {};
unsigned long countY {};
unsigned long countZ {};
grid.GetCtGrids(countX, countY, countZ);
EXPECT_EQ(countX, 1);
EXPECT_EQ(countY, 1);
EXPECT_EQ(countZ, 1);
}
TEST_F(MeshTest, TestGrid1OfAlmostPlanarMesh)
{
MeshCore::MeshKernel kernel;
Base::Vector3f p1 {0, 0, 0};
Base::Vector3f p2 {1, 0, 0};
Base::Vector3f p3 {0, 1, 0};
Base::Vector3f p4 {1, 1, 1.0e-18F};
kernel.AddFacet(MeshCore::MeshGeomFacet(p1, p2, p3));
kernel.AddFacet(MeshCore::MeshGeomFacet(p3, p2, p4));
MeshCore::MeshFacetGrid grid(kernel, 10);
unsigned long countX {};
unsigned long countY {};
unsigned long countZ {};
grid.GetCtGrids(countX, countY, countZ);
EXPECT_EQ(countX, 1);
EXPECT_EQ(countY, 1);
EXPECT_EQ(countZ, 1);
}
TEST_F(MeshTest, TestGrid2OfAlmostPlanarMesh)
{
MeshCore::MeshKernel kernel;
Base::Vector3f p1 {0, 0, 0};
Base::Vector3f p2 {1, 0, 0};
Base::Vector3f p3 {0, 1, 0};
Base::Vector3f p4 {1, 1, 1.0e-18F};
kernel.AddFacet(MeshCore::MeshGeomFacet(p1, p2, p3));
kernel.AddFacet(MeshCore::MeshGeomFacet(p3, p2, p4));
MeshCore::MeshFacetGrid grid(kernel);
unsigned long countX {};
unsigned long countY {};
unsigned long countZ {};
grid.GetCtGrids(countX, countY, countZ);
EXPECT_EQ(countX, 1);
EXPECT_EQ(countY, 1);
EXPECT_EQ(countZ, 1);
}
// NOLINTEND(cppcoreguidelines-*,readability-*)

View File

@@ -0,0 +1,42 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "gtest/gtest.h"
#include <src/App/InitApplication.h>
#include <Mod/Mesh/App/MeshFeature.h>
class MeshFeatureTest: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
void SetUp() override
{}
void TearDown() override
{}
};
// NOLINTBEGIN(cppcoreguidelines-*,readability-*)
TEST_F(MeshFeatureTest, getElementTypes)
{
Mesh::Feature mf;
std::vector<const char*> types = mf.getElementTypes();
EXPECT_EQ(types.size(), 2);
EXPECT_STREQ(types[0], "Mesh");
EXPECT_STREQ(types[1], "Segment");
}
TEST_F(MeshFeatureTest, getComplexElementTypes)
{
Mesh::MeshObject mf;
std::vector<const char*> types = mf.getElementTypes();
EXPECT_EQ(types.size(), 2);
EXPECT_STREQ(types[0], "Mesh");
EXPECT_STREQ(types[1], "Segment");
}
// NOLINTEND(cppcoreguidelines-*,readability-*)

View File

@@ -0,0 +1,9 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
add_subdirectory(App)
target_link_libraries(Mesh_tests_run
gtest_main
${Google_Tests_LIBS}
Mesh
)