diff --git a/src/Mod/Part/App/TopoShape.h b/src/Mod/Part/App/TopoShape.h index 9b1e1a73bd..4cc128c696 100644 --- a/src/Mod/Part/App/TopoShape.h +++ b/src/Mod/Part/App/TopoShape.h @@ -214,6 +214,18 @@ enum class AsAngle yes }; +enum class CheckScale +{ + noScaleCheck, + checkScale +}; + +enum class Copy +{ + noCopy, + copy +}; + /** The representation for a CAD Shape */ // NOLINTNEXTLINE cppcoreguidelines-special-member-functions @@ -802,7 +814,7 @@ public: /** Make a hollowed solid by removing some faces from a given solid * - * @param source: input shape + * @param shape: input shape * @param faces: list of faces to remove, must be sub shape of the input shape * @param offset: thickness of the walls * @param tol: tolerance criterion for coincidence in generated shapes @@ -818,13 +830,12 @@ public: * a self reference so that multiple operations can be carried out * for the same shape in the same line of code. */ - TopoShape &makeElementThickSolid(const TopoShape &source, const std::vector &faces, + TopoShape &makeElementThickSolid(const TopoShape &shape, const std::vector &faces, double offset, double tol, bool intersection = false, bool selfInter = false, short offsetMode = 0, JoinType join = JoinType::arc, const char *op=nullptr); /** Make a hollowed solid by removing some faces from a given solid * - * @param source: input shape * @param faces: list of faces to remove, must be sub shape of the input shape * @param offset: thickness of the walls * @param tol: tolerance criterion for coincidence in generated shapes @@ -1240,6 +1251,43 @@ public: return TopoShape(0, Hasher).makeElementWires(*this, op, tol, policy, output); } + /** Make a new shape with transformation that may contain non-uniform scaling + * + * @param source: input shape + * @param mat: transformation matrix + * @param op: optional string to be encoded into topo naming for indicating + * the operation + * @param copy: whether to perform deep copy of the shape. If false, the + * shape will still be copied if there is scaling. + * + * @return The original content of this TopoShape is discarded and replaced + * with the new transformed shape. The function returns the + * TopoShape itself as a self reference so that multiple operations + * can be carried out for the same shape in the same line of code. + */ + TopoShape& makeElementGTransform(const TopoShape& source, + const Base::Matrix4D& mat, + const char* op = nullptr, + Copy copy = Copy::noCopy); + + /** Make a new shape with transformation that may contain non-uniform scaling + * + * @param source: input shape + * @param mat: transformation matrix + * @param op: optional string to be encoded into topo naming for indicating + * the operation + * @param copy: whether to perform deep copy of the shape. If false, the + * shape will still be copied if there is scaling. + * + * @return Return a new shape with transformation. The shape itself is not + * modified + */ + TopoShape makeElementGTransform(const Base::Matrix4D& mat, + const char* op = nullptr, + Copy copy = Copy::noCopy) const + { + return TopoShape(Tag, Hasher).makeElementGTransform(*this, mat, op, copy); + } /** Make a deep copy of the shape * @@ -1411,6 +1459,114 @@ public: .makeElementChamfer(*this, edges, radius1, radius2, op, flipDirection, asAngle); } + /** Make a new shape with transformation + * + * @param source: input shape + * @param mat: transformation matrix + * @param op: optional string to be encoded into topo naming for indicating + * the operation + * @param checkScale: whether to check if the transformation matrix + * contains scaling factor. + * @param copy: whether to perform deep copy of the shape. If noCopy, and + * checkScale, then the shape will be copied if there + * is scaling. + * + * @return Returns true if scaling is performed. + * + * The original content of this TopoShape is discarded and replaced with + * the new transformed shape. + */ + bool _makeElementTransform(const TopoShape& source, + const Base::Matrix4D& mat, + const char* op = nullptr, + CheckScale checkScale = CheckScale::noScaleCheck, + Copy copy = Copy::noCopy); + + /** Make a new shape with transformation + * + * @param source: input shape + * @param mat: transformation matrix + * @param op: optional string to be encoded into topo naming for indicating + * the operation + * @param checkScale: whether to check if the transformation matrix + * contains scaling factor. + * @param copy: whether to perform deep copy of the shape. If noCopy, and + * checkScale, then the shape will be copied if there + * is scaling. + * + * @return The original content of this TopoShape is discarded and replaced + * with the new transformed shape. The function returns the + * TopoShape itself as a self reference so that multiple operations + * can be carried out for the same shape in the same line of code. + */ + TopoShape& makeElementTransform(const TopoShape& source, + const Base::Matrix4D& mat, + const char* op = nullptr, + CheckScale checkScale = CheckScale::noScaleCheck, + Copy copy = Copy::noCopy) + { + _makeElementTransform(source, mat, op, checkScale, copy); + return *this; + } + + /** Make a new shape with transformation + * + * @param source: input shape + * @param mat: transformation matrix + * @param op: optional string to be encoded into topo naming for indicating + * the operation + * @param checkScale: whether to check if the transformation matrix + * contains scaling factor. + * @param copy: whether to perform deep copy of the shape. If noCopy, and + * checkScale, then the shape will be copied if there + * is scaling. + * + * @return Return a new shape with transformation. The shape itself is not + * modified + */ + TopoShape makeElementTransform(const Base::Matrix4D& mat, + const char* op = nullptr, + CheckScale checkScale = CheckScale::noScaleCheck, + Copy copy = Copy::noCopy) const + { + return TopoShape(Tag, Hasher).makeElementTransform(*this, mat, op, checkScale, copy); + } + + /** Make a new shape with transformation + * + * @param source: input shape + * @param trsf: OCCT transformation matrix + * @param op: optional string to be encoded into topo naming for indicating + * the operation + * @param copy: whether to perform deep copy of the shape. + * + * @return The original content of this TopoShape is discarded and replaced + * with the new transformed shape. The function returns the + * TopoShape itself as a self reference so that multiple operations + * can be carried out for the same shape in the same line of code. + */ + TopoShape& makeElementTransform(const TopoShape& shape, + const gp_Trsf& trsf, + const char* op = nullptr, + Copy copy = Copy::noCopy); + + /** Make a new shape with transformation + * + * @param source: input shape + * @param trsf: OCCT transformation matrix + * @param op: optional string to be encoded into topo naming for indicating + * the operation + * @param copy: whether to perform deep copy of the shape. + * + * @return Return a new shape with transformation. The shape itself is not + * modified + */ + TopoShape + makeElementTransform(const gp_Trsf& trsf, const char* op = nullptr, Copy copy = Copy::noCopy) + { + return TopoShape(Tag, Hasher).makeElementTransform(*this, trsf, op, copy); + } + /* Make draft shape * * @param source: the source shape @@ -1649,7 +1805,7 @@ public: * makeShapeWithElementMap directly. For example: * makeElementShape(sewer, sources) * makeShapeWithElementMap(sewer.SewedShape(), MapperSewing(sewer), sources, OpCodes::Sewing); - * Note that if op exists in the method, it should be checked for null and overriden with + * Note that if op exists in the method, it should be checked for null and overridden with * the appropriate operation if so. */ diff --git a/src/Mod/Part/App/TopoShapeExpansion.cpp b/src/Mod/Part/App/TopoShapeExpansion.cpp index 1fc8a6639c..08daf6132a 100644 --- a/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -48,9 +48,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -83,8 +85,6 @@ #include "Geometry.h" #include -#include -#include "Geometry.h" FC_LOG_LEVEL_INIT("TopoShape", true, true) // NOLINT @@ -2506,6 +2506,110 @@ TopoShape& TopoShape::makeElementOrderedWires(const std::vector& shap return makeElementCompound(wires, nullptr, SingleShapeCompoundCreationPolicy::returnShape); } +bool TopoShape::_makeElementTransform(const TopoShape& shape, + const Base::Matrix4D& mat, + const char* op, + CheckScale checkScale, + Copy copy) +{ + if (checkScale == CheckScale::checkScale) { + auto scaleType = mat.hasScale(); + if (scaleType != Base::ScaleType::NoScaling && scaleType != Base::ScaleType::Uniform) { + makeElementGTransform(shape, mat, op, copy); + return true; + } + } + makeElementTransform(shape, convert(mat), op, copy); + return false; +} + +TopoShape& TopoShape::makeElementTransform(const TopoShape& shape, + const gp_Trsf& trsf, + const char* op, + Copy copy) +{ + if (copy == Copy::noCopy) { + // OCCT checks the ScaleFactor against gp::Resolution() which is DBL_MIN!!! + // No scaling is 1 as in 1:1 + const bool scaling = Abs(Abs(trsf.ScaleFactor()) - 1) > Precision::Confusion(); + const bool negative_scaling = + trsf.ScaleFactor() * trsf.HVectorialPart().Determinant() < 0.0; + copy = negative_scaling || scaling ? Copy::copy : Copy::noCopy; + } + TopoShape tmp(shape); + if (copy == Copy::copy) { + if (shape.isNull()) { + FC_THROWM(NullShapeException, "Null input shape"); + } + + BRepBuilderAPI_Transform mkTrf(shape.getShape(), trsf, Standard_True); + // TODO: calling Moved() is to make sure the shape has some Location, + // which is necessary for STEP export to work. However, if we reach + // here, it probably means BRepBuilderAPI_Transform has modified + // underlying shapes (because of scaling), it will break compound child + // parent relationship anyway. In short, STEP import/export will most + // likely break badly if there is any scaling involved + tmp.setShape(mkTrf.Shape().Moved(gp_Trsf()), false); + } + else { + tmp.move(trsf); + } + + if (op || (shape.Tag && shape.Tag != Tag)) { + setShape(tmp._Shape); + initCache(); + if (!Hasher) { + Hasher = tmp.Hasher; + } + copyElementMap(tmp, op); + } + else { + *this = tmp; + } + return *this; +} + +TopoShape& TopoShape::makeElementGTransform(const TopoShape& shape, + const Base::Matrix4D& mat, + const char* op, + Copy copy) +{ + if (shape.isNull()) { + FC_THROWM(NullShapeException, "Null input shape"); + } + + // if(!op) op = Part::OpCodes::Gtransform; + gp_GTrsf matrix; + matrix.SetValue(1, 1, mat[0][0]); + matrix.SetValue(2, 1, mat[1][0]); + matrix.SetValue(3, 1, mat[2][0]); + matrix.SetValue(1, 2, mat[0][1]); + matrix.SetValue(2, 2, mat[1][1]); + matrix.SetValue(3, 2, mat[2][1]); + matrix.SetValue(1, 3, mat[0][2]); + matrix.SetValue(2, 3, mat[1][2]); + matrix.SetValue(3, 3, mat[2][2]); + matrix.SetValue(1, 4, mat[0][3]); + matrix.SetValue(2, 4, mat[1][3]); + matrix.SetValue(3, 4, mat[2][3]); + + // geometric transformation + TopoShape tmp(shape); + BRepBuilderAPI_GTransform mkTrf(shape.getShape(), matrix, copy == Copy::copy); + tmp.setShape(mkTrf.Shape(), false); + if (op || (shape.Tag && shape.Tag != Tag)) { + setShape(tmp._Shape); + initCache(); + if (!Hasher) { + Hasher = tmp.Hasher; + } + copyElementMap(tmp, op); + } + else { + *this = tmp; + } + return *this; +} TopoShape& TopoShape::makeElementCopy(const TopoShape& shape, const char* op, bool copyGeom, bool copyMesh) diff --git a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp index 16f9578827..c122481cdd 100644 --- a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -1523,7 +1523,6 @@ TEST_F(TopoShapeExpansionTest, makeElementThickSolid) EXPECT_EQ(elements[IndexedName("Edge", 1)], MappedName("Edge11;THK;:H1:4,E")); } - TEST_F(TopoShapeExpansionTest, makeElementGeneralFuse) { // Arrange @@ -1845,4 +1844,99 @@ TEST_F(TopoShapeExpansionTest, makeElementFillet) })); } +TEST_F(TopoShapeExpansionTest, makeElementTransformWithoutMap) +{ + // Arrange + auto [cube1, cube2] = CreateTwoCubes(); + auto tr {gp_Trsf()}; + tr.SetTranslation(gp_Vec(gp_XYZ(-0.5, -0.5, 0))); + TopoShape topoShape1 {cube1, 1L}; + // Act + TopoShape& result = topoShape1.makeElementTransform(topoShape1, tr); + auto elements = elementMap(result); + Base::BoundBox3d bb = result.getBoundBox(); + // Assert shape is correct + EXPECT_TRUE(PartTestHelpers::boxesMatch(bb, Base::BoundBox3d(-0.5, -0.5, 0.0, 0.5, 0.5, 1.0))); + EXPECT_FLOAT_EQ(getVolume(result.getShape()), 1); + // Assert elementMap is correct + EXPECT_EQ(elements.size(), 0); +} + +TEST_F(TopoShapeExpansionTest, makeElementTransformWithMap) +{ + // Arrange + auto [cube1, cube2] = CreateTwoCubes(); + auto tr {gp_Trsf()}; + tr.SetTranslation(gp_Vec(gp_XYZ(-0.5, -0.5, 0))); + cube2.Move(TopLoc_Location(tr)); + TopoShape topoShape1 {cube1, 1L}; + TopoShape topoShape2 {cube2, 2L}; + // Act + TopoShape& result = topoShape1.makeElementFuse({topoShape1, topoShape2}); // op, tolerance + topoShape1.makeElementTransform(result, tr); + auto elements = elementMap(result); + Base::BoundBox3d bb = result.getBoundBox(); + // Assert shape is correct + EXPECT_TRUE(PartTestHelpers::boxesMatch(bb, Base::BoundBox3d(-0.5, -1.0, 0.0, 1.0, 0.5, 1.0))); + EXPECT_FLOAT_EQ(getVolume(result.getShape()), 1.75); + // Assert elementMap is correct + EXPECT_EQ(elements.size(), 66); + EXPECT_EQ(elements.count(IndexedName("Face", 1)), 1); + EXPECT_EQ( + elements[IndexedName("Face", 1)], + MappedName( + "Face3;:M;FUS;:H1:7,F;:U;FUS;:H1:7,E;:L(Face5;:M;FUS;:H1:7,F;:U2;FUS;:H1:8,E|Face5;:M;" + "FUS;:H1:7,F;:U2;FUS;:H1:8,E;:U;FUS;:H1:7,V;:L(Face6;:M;FUS;:H1:7,F;:U2;FUS;:H1:8,E;:U;" + "FUS;:H1:7,V);FUS;:H1:3c,E|Face6;:M;FUS;:H1:7,F;:U2;FUS;:H1:8,E);FUS;:H1:cb,F")); +} + +TEST_F(TopoShapeExpansionTest, makeElementGTransformWithoutMap) +{ + // Arrange + auto [cube1, cube2] = CreateTwoCubes(); + auto tr {gp_Trsf()}; + tr.SetTranslation(gp_Vec(gp_XYZ(-0.5, -0.5, 0))); + TopoShape topoShape1 {cube1, 1L}; + // Act + TopoShape& result = topoShape1.makeElementGTransform(topoShape1, TopoShape::convert(tr)); + auto elements = elementMap(result); + Base::BoundBox3d bb = result.getBoundBox(); + // Assert shape is correct + EXPECT_TRUE(PartTestHelpers::boxesMatch(bb, Base::BoundBox3d(-0.5, -0.5, 0.0, 0.5, 0.5, 1.0))); + EXPECT_FLOAT_EQ(getVolume(result.getShape()), 1); + // Assert elementMap is correct + EXPECT_EQ(elements.size(), 0); +} + +TEST_F(TopoShapeExpansionTest, makeElementGTransformWithMap) +{ + // Arrange + auto [cube1, cube2] = CreateTwoCubes(); + auto tr {gp_Trsf()}; + tr.SetTranslation(gp_Vec(gp_XYZ(-0.5, -0.5, 0))); + cube2.Move(TopLoc_Location(tr)); + TopoShape topoShape1 {cube1, 1L}; + TopoShape topoShape2 {cube2, 2L}; + // Act + TopoShape& result = topoShape1.makeElementFuse({topoShape1, topoShape2}); // op, tolerance + topoShape1.makeElementGTransform(result, TopoShape::convert(tr)); + auto elements = elementMap(result); + Base::BoundBox3d bb = result.getBoundBox(); + // Assert shape is correct + EXPECT_TRUE(PartTestHelpers::boxesMatch(bb, Base::BoundBox3d(-0.5, -1.0, 0.0, 1.0, 0.5, 1.0))); + EXPECT_FLOAT_EQ(getVolume(result.getShape()), 1.75); + // Assert elementMap is correct + EXPECT_EQ(elements.size(), 66); + EXPECT_EQ(elements.count(IndexedName("Face", 1)), 1); + EXPECT_EQ( + elements[IndexedName("Face", 1)], + MappedName( + "Face3;:M;FUS;:H1:7,F;:U;FUS;:H1:7,E;:L(Face5;:M;FUS;:H1:7,F;:U2;FUS;:H1:8,E|Face5;:M;" + "FUS;:H1:7,F;:U2;FUS;:H1:8,E;:U;FUS;:H1:7,V;:L(Face6;:M;FUS;:H1:7,F;:U2;FUS;:H1:8,E;:U;" + "FUS;:H1:7,V);FUS;:H1:3c,E|Face6;:M;FUS;:H1:7,F;:U2;FUS;:H1:8,E);FUS;:H1:cb,F")); +} + +// Not testing _makeElementTransform as it is a thin wrapper that calls the same places as the four +// preceding tests. + // NOLINTEND(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) diff --git a/tests/src/Mod/Part/App/TopoShapeMakeElementRefine.cpp b/tests/src/Mod/Part/App/TopoShapeMakeElementRefine.cpp index e823ace748..84afef491b 100644 --- a/tests/src/Mod/Part/App/TopoShapeMakeElementRefine.cpp +++ b/tests/src/Mod/Part/App/TopoShapeMakeElementRefine.cpp @@ -48,6 +48,6 @@ TEST_F(FeaturePartMakeElementRefineTest, makeElementRefineBoxes) EXPECT_EQ(refined.countSubElements("Face"), 6); // After refining it is one box EXPECT_EQ(refined.countSubElements("Edge"), 12); // 12 edges in a box // TODO: Make sure we have an elementMap for the refine. - // Refine doesn't work on compounds, so we're going to need a binary operation or the + // TODO: Refine doesn't work on compounds, so we're going to need a binary operation or the // like, and those don't exist yet. Once they do, this test can be expanded }