Skip to content

Commit

Permalink
Base: add overloaded method Matrix4D::isUnity()
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Oct 19, 2023
1 parent 9defdaf commit 2e13a6a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/Base/Matrix.cpp
Expand Up @@ -26,7 +26,6 @@
# include <cstring>
# include <sstream>
#endif
# include <array>

#include "Matrix.h"
#include "Converter.h"
Expand Down Expand Up @@ -90,16 +89,23 @@ void Matrix4D::setToUnity ()
}

bool Matrix4D::isUnity() const
{
return isUnity(0.0);
}

bool Matrix4D::isUnity(double tol) const
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) {
if (dMtrx4D[i][j] != 1.0)
if (fabs(dMtrx4D[i][j] - 1.0) > tol) {
return false;
}
}
else {
if (dMtrx4D[i][j] != 0.0)
if (fabs(dMtrx4D[i][j]) > tol) {
return false;
}
}
}
}
Expand Down Expand Up @@ -899,7 +905,8 @@ ScaleType Matrix4D::hasScale(double tol) const
return ScaleType::NoScaling;
}

std::array<Matrix4D, 4> Matrix4D::decompose() const {
std::array<Matrix4D, 4> Matrix4D::decompose() const
{
// decompose the matrix to shear, scale, rotation and move
// so that matrix = move * rotation * scale * shear
// return an array of matrices
Expand Down
2 changes: 2 additions & 0 deletions src/Base/Matrix.h
Expand Up @@ -149,6 +149,8 @@ class BaseExport Matrix4D //NOLINT(cppcoreguidelines-special-member-functions)
void setToUnity();
/// Checks if this is the unit matrix
bool isUnity() const;
/// Checks if this is the unit matrix
bool isUnity(double tol) const;
/// Makes a null matrix
void nullify();
/// Checks if this is the null matrix
Expand Down
4 changes: 2 additions & 2 deletions src/Base/MatrixPy.xml
Expand Up @@ -125,9 +125,9 @@ Check if this is the null matrix.</UserDocu>
Make this matrix to unity (4D identity matrix).</UserDocu>
</Documentation>
</Methode>
<Methode Name="isUnity" Const="true" NoArgs="true">
<Methode Name="isUnity" Const="true">
<Documentation>
<UserDocu>isUnity() -> bool
<UserDocu>isUnity([tol=0.0]) -> bool

Check if this is the unit matrix (4D identity matrix).</UserDocu>
</Documentation>
Expand Down
9 changes: 6 additions & 3 deletions src/Base/MatrixPyImp.cpp
Expand Up @@ -22,7 +22,6 @@


#include "PreCompiled.h"
//#include <array>

// inclusion of the generated files (generated out of MatrixPy.xml)
#include "RotationPy.h"
Expand Down Expand Up @@ -352,6 +351,7 @@ PyObject* MatrixPy::hasScale(PyObject * args)
Py::Module mod("FreeCAD");
return Py::new_reference_to(mod.callMemberFunction("ScaleType", Py::TupleN(Py::Int(static_cast<int>(type)))));
}

PyObject* MatrixPy::decompose(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
Expand Down Expand Up @@ -392,10 +392,13 @@ PyObject* MatrixPy::unity()
PY_CATCH;
}

PyObject* MatrixPy::isUnity()
PyObject* MatrixPy::isUnity(PyObject * args)
{
double tol = 0.0;
if (!PyArg_ParseTuple(args, "|d", &tol))
return nullptr;
PY_TRY {
bool ok = getMatrixPtr()->isUnity();
bool ok = getMatrixPtr()->isUnity(tol);
return Py::new_reference_to(Py::Boolean(ok));
}
PY_CATCH;
Expand Down

0 comments on commit 2e13a6a

Please sign in to comment.