Commit 32d539f2 authored by Didier WECKMANN's avatar Didier WECKMANN
Browse files

feat(fwData): get/set a 4x4 Matrix from TranformationMatrix3D

- Add new functions to get/set a TransformationMatrix3D from/to a 4x4 Matrix (array of array). This ensure the row major order and avoid linear to 2d conversions before setting coefficients.

Merge branch '497-fwdata-get-set-a-4x4-matrix-from-tranformationmatrix3d' into 'dev'
Closes #497
See merge request sight/sight!420
parents cda024e9 2f2f8c05
/************************************************************************
*
* Copyright (C) 2009-2019 IRCAD France
* Copyright (C) 2012-2019 IHU Strasbourg
* Copyright (C) 2009-2020 IRCAD France
* Copyright (C) 2012-2020 IHU Strasbourg
*
* This file is part of Sight.
*
......@@ -40,12 +40,13 @@ class FWDATA_CLASS_API TransformationMatrix3D : public Object
{
public:
fwCoreClassMacro(TransformationMatrix3D, ::fwData::Object, ::fwData::factory::New< TransformationMatrix3D >);
fwCoreClassMacro(TransformationMatrix3D, ::fwData::Object, ::fwData::factory::New< TransformationMatrix3D >)
fwCampMakeFriendDataMacro((fwData)(TransformationMatrix3D));
typedef double TM3DType;
typedef std::array<TM3DType, 16> TMCoefArray;
typedef std::array< std::array< TM3DType, 4 >, 4 > MatrixType;
/**
* @brief Constructor
......@@ -92,6 +93,17 @@ public:
return s;
}
/**
* @brief Returns matrix coeficients as a 4x4 matrix (Row major).
* @return 4x4 matrix (std::array< std::array< double, 4> 4>).
*/
FWDATA_API MatrixType getMatrix4x4() const;
/**
* @brief Sets coeficients as a 4x4 matrix (Row major).
* @param _matrix : matrix coeficients as std::array< std::array< double, 4 > 4 >.
*/
FWDATA_API void setMatrix4x4(const MatrixType& _matrix);
protected:
//! Matrix coefficient number (4x4). m_vCoefficients[0] to m_vCoefficients[3] is the first row of the matrix
......@@ -135,6 +147,32 @@ inline void TransformationMatrix3D::setCoefficient(size_t l, size_t c, Transform
m_vCoefficients.at(pos) = val;
}
//------------------------------------------------------------------------------
inline TransformationMatrix3D::MatrixType TransformationMatrix3D::getMatrix4x4() const
{
// linear index to 2d.
const TransformationMatrix3D::MatrixType
matrix4x4 {{
{m_vCoefficients[0], m_vCoefficients[1], m_vCoefficients[2], m_vCoefficients[3]},
{m_vCoefficients[4], m_vCoefficients[5], m_vCoefficients[6], m_vCoefficients[7]},
{m_vCoefficients[8], m_vCoefficients[9], m_vCoefficients[10], m_vCoefficients[11]},
{m_vCoefficients[12], m_vCoefficients[13], m_vCoefficients[14], m_vCoefficients[15]}
}};
return matrix4x4;
}
//-----------------------------------------------------------------------------
inline void TransformationMatrix3D::setMatrix4x4(const TransformationMatrix3D::MatrixType& _matrix)
{
// 2d to linear index.
m_vCoefficients = { _matrix[0][0], _matrix[0][1], _matrix[0][2], _matrix[0][3],
_matrix[1][0], _matrix[1][1], _matrix[1][2], _matrix[1][3],
_matrix[2][0], _matrix[2][1], _matrix[2][2], _matrix[2][3],
_matrix[3][0], _matrix[3][1], _matrix[3][2], _matrix[3][3]};
}
//-----------------------------------------------------------------------------
} // namespace fwData
/************************************************************************
*
* Copyright (C) 2009-2015 IRCAD France
* Copyright (C) 2012-2015 IHU Strasbourg
* Copyright (C) 2009-2020 IRCAD France
* Copyright (C) 2012-2020 IHU Strasbourg
*
* This file is part of Sight.
*
......@@ -20,8 +20,7 @@
*
***********************************************************************/
#ifndef __FWDATA_UT_TRANSFORMATIONMATRIX3DTEST_HPP__
#define __FWDATA_UT_TRANSFORMATIONMATRIX3DTEST_HPP__
#pragma once
#include <cppunit/extensions/HelperMacros.h>
......@@ -36,6 +35,7 @@ private:
CPPUNIT_TEST_SUITE( TransformationMatrix3DTest );
CPPUNIT_TEST( getterSetterByCoef );
CPPUNIT_TEST( getterSetterByArray );
CPPUNIT_TEST( getterSetterByMatrix );
CPPUNIT_TEST_SUITE_END();
public:
......@@ -45,9 +45,8 @@ public:
void getterSetterByCoef();
void getterSetterByArray();
void getterSetterByMatrix();
};
} //namespace ut
} //namespace fwData
#endif //__FWDATA_UT_TRANSFORMATIONMATRIX3DTEST_HPP__
/************************************************************************
*
* Copyright (C) 2009-2015 IRCAD France
* Copyright (C) 2012-2015 IHU Strasbourg
* Copyright (C) 2009-2020 IRCAD France
* Copyright (C) 2012-2020 IHU Strasbourg
*
* This file is part of Sight.
*
......@@ -20,10 +20,10 @@
*
***********************************************************************/
#include <fwData/TransformationMatrix3D.hpp>
#include "TransformationMatrix3DTest.hpp"
#include <fwData/TransformationMatrix3D.hpp>
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( ::fwData::ut::TransformationMatrix3DTest );
......@@ -32,6 +32,8 @@ namespace fwData
namespace ut
{
//------------------------------------------------------------------------------
void TransformationMatrix3DTest::setUp()
{
// Set up context before running a test.
......@@ -131,6 +133,50 @@ void TransformationMatrix3DTest::getterSetterByArray()
}
}
//------------------------------------------------------------------------------
void TransformationMatrix3DTest::getterSetterByMatrix()
{
::fwData::TransformationMatrix3D::MatrixType matrix4x4 = { { {0., 0., 1., 100.},
{0., 1., 0., 200.},
{1., 0., 0., 3000.},
{0., 0., 0., 1.} } };
::fwData::TransformationMatrix3D::sptr mat = ::fwData::TransformationMatrix3D::New();
mat->setMatrix4x4(matrix4x4);
const auto mat2 = mat->getMatrix4x4();
for (size_t i = 0; i < 4; ++i)
{
for (size_t j = 0; j < 4; ++j)
{
CPPUNIT_ASSERT_DOUBLES_EQUAL(matrix4x4[i][j], mat2[i][j], 10e-8);
CPPUNIT_ASSERT_DOUBLES_EQUAL(matrix4x4[i][j], mat->getCoefficient(i, j), 10e-8);
}
}
::fwData::TransformationMatrix3D::TMCoefArray coefs = { 1, -2, .3, .4,
5.5, 6, 7.77, 8.,
0.09, 10., -11., 1.2,
0, 0, 0, 1.};
mat->setCoefficients(coefs);
const auto matFromCoefs = mat->getMatrix4x4();
for (size_t i = 0; i < 4; ++i)
{
for (size_t j = 0; j < 4; ++j)
{
CPPUNIT_ASSERT_DOUBLES_EQUAL(coefs[i * 4 + j], matFromCoefs[i][j], 10e-8);
}
}
}
//------------------------------------------------------------------------------
} //namespace ut
} //namespace fwData
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment