Mesh: add unit tests for mesh export

This commit is contained in:
wmayer
2024-03-13 11:54:51 +01:00
committed by wwmayer
parent 99dc7826ae
commit 042d3a63e7
4 changed files with 156 additions and 9 deletions

View File

@@ -43,7 +43,7 @@ namespace Mesh
* If objects are meant to be combined into a single file, then the file should
* be saved from the derived class' destructor.
*/
class Exporter
class MeshExport Exporter
{
public:
Exporter();
@@ -76,7 +76,7 @@ protected:
};
/// Creates a single mesh, in a file, from one or more objects
class MergeExporter: public Exporter
class MeshExport MergeExporter: public Exporter
{
public:
MergeExporter(std::string fileName, MeshCore::MeshIO::Format fmt);
@@ -202,7 +202,7 @@ private:
* The constructor and destructor write the beginning and end of the 3MF,
* addObject() is used to add geometry
*/
class Exporter3MF: public Exporter
class MeshExport Exporter3MF: public Exporter
{
public:
Exporter3MF(std::string fileName, const std::vector<Extension3MFPtr>& = {});
@@ -235,7 +235,7 @@ private:
* The constructor and destructor write the beginning and end of the AMF,
* addObject() is used to add geometry
*/
class ExporterAMF: public Exporter
class MeshExport ExporterAMF: public Exporter
{
public:
/// Writes AMF header

View File

@@ -2,5 +2,6 @@ target_sources(
Mesh_tests_run
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Core/KDTree.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Exporter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Mesh.cpp
)

View File

@@ -0,0 +1,150 @@
#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 = dynamic_cast<Mesh::Cube*>(document->addObject("Mesh::Cube", "Cube1"));
cube1->Length.setValue(value);
cube1->Width.setValue(value);
cube1->Height.setValue(value);
cube2 = dynamic_cast<Mesh::Cube*>(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 = dynamic_cast<App::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

@@ -5,11 +5,7 @@
TEST(MeshTest, TestDefault)
{
MeshCore::MeshKernel kernel;
Base::Vector3f p1 {
0,
0,
0,
};
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));