Skip to content

Commit 2c00ae3

Browse files
committed
FILT: Adds a 'Matrix Calculator' filter and supporting files
This filter is meant to allow a user to chain matrix operations together on any number of matrices. Signed-off-by: Michael Jackson <mike.jackson@bluequartz.net>
1 parent 700306f commit 2c00ae3

9 files changed

+544
-1
lines changed

src/Plugins/SimplnxCore/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ set(FilterList
141141
WriteStlFileFilter
142142
WriteVtkRectilinearGridFilter
143143
WriteVtkStructuredPointsFilter
144+
MatrixCalculatorFilter
144145
)
145146

146147
set(ActionList
@@ -231,6 +232,7 @@ set(AlgorithmList
231232
WriteStlFile
232233
WriteVtkRectilinearGrid
233234
WriteVtkStructuredPoints
235+
MatrixCalculator
234236
)
235237

236238
create_simplnx_plugin(NAME ${PLUGIN_NAME}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Matrix Calculator
2+
3+
## Group (Subgroup)
4+
5+
6+
## Description
7+
8+
9+
% Auto generated parameter table will be inserted here
10+
11+
## License & Copyright
12+
13+
Please see the description file distributed with this **Plugin**
14+
15+
## DREAM3D-NX Help
16+
17+
If you need help, need to file a bug report or want to request a new feature, please head over to the [DREAM3DNX-Issues](https://github.com/BlueQuartzSoftware/DREAM3DNX-Issues/discussions) GitHub site where the community of DREAM3D-NX users can help answer your questions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "MatrixCalculator.hpp"
2+
3+
#include "simplnx/DataStructure/DataArray.hpp"
4+
#include "simplnx/DataStructure/Geometry/ImageGeom.hpp"
5+
#include "simplnx/Utilities/DataGroupUtilities.hpp"
6+
#include "simplnx/Utilities/FilterUtilities.hpp"
7+
#include "simplnx/Utilities/ImageRotationUtilities.hpp"
8+
9+
#include <Eigen/Dense>
10+
11+
using namespace nx::core;
12+
13+
namespace
14+
{
15+
template <typename T>
16+
Eigen::Matrix<T, 4, 4, Eigen::RowMajor> CreateEigenMatrix(const AbstractDataStore<T>& dataStore)
17+
{
18+
Eigen::Matrix<T, 4, 4, Eigen::RowMajor> matrix;
19+
matrix.fill(0);
20+
matrix << dataStore[0], dataStore[1], dataStore[2], dataStore[3], dataStore[4], dataStore[5], dataStore[6], dataStore[7], dataStore[8], dataStore[9], dataStore[10], dataStore[11], dataStore[12],
21+
dataStore[13], dataStore[14], dataStore[15];
22+
23+
return matrix;
24+
}
25+
26+
struct MatrixOperationFunctor
27+
{
28+
template <typename ScalarType>
29+
Result<> operator()(const IDataArray& array1, const IDataArray& array2, IDataArray& outputArray, ChoicesParameter::ValueType opIdx)
30+
{
31+
using MatrixType = Eigen::Matrix<ScalarType, 4, 4, Eigen::RowMajor>;
32+
using StoreType = AbstractDataStore<ScalarType>;
33+
const auto& array1StoreRef = array1.getIDataStoreRefAs<StoreType>();
34+
const auto& array2StoreRef = array2.getIDataStoreRefAs<StoreType>();
35+
36+
auto eigenMatrix1 = CreateEigenMatrix<ScalarType>(array1StoreRef);
37+
auto eigenMatrix2 = CreateEigenMatrix<ScalarType>(array2StoreRef);
38+
39+
MatrixType output;
40+
41+
if(opIdx == matrix_calculator::constants::k_MultiplicationIdx)
42+
{
43+
output = eigenMatrix1 * eigenMatrix2;
44+
}
45+
else if(opIdx == matrix_calculator::constants::k_AdditionIdx)
46+
{
47+
output = eigenMatrix1 + eigenMatrix2;
48+
}
49+
else if(opIdx == matrix_calculator::constants::k_SubtractionIdx)
50+
{
51+
output = eigenMatrix1 - eigenMatrix2;
52+
}
53+
54+
auto& dataStore = outputArray.getIDataStoreRefAs<StoreType>();
55+
56+
dataStore[0] = output(0, 0);
57+
dataStore[1] = output(0, 1);
58+
dataStore[2] = output(0, 2);
59+
dataStore[3] = output(0, 3);
60+
dataStore[4] = output(1, 0);
61+
dataStore[5] = output(1, 1);
62+
dataStore[6] = output(1, 2);
63+
dataStore[7] = output(1, 3);
64+
dataStore[8] = output(2, 0);
65+
dataStore[9] = output(2, 1);
66+
dataStore[10] = output(2, 2);
67+
dataStore[11] = output(2, 3);
68+
dataStore[12] = output(3, 0);
69+
dataStore[13] = output(3, 1);
70+
dataStore[14] = output(3, 2);
71+
dataStore[15] = output(3, 3);
72+
73+
return {};
74+
}
75+
};
76+
77+
} // namespace
78+
79+
// -----------------------------------------------------------------------------
80+
MatrixCalculator::MatrixCalculator(DataStructure& dataStructure, const IFilter::MessageHandler& mesgHandler, const std::atomic_bool& shouldCancel, MatrixCalculatorInputValues* inputValues)
81+
: m_DataStructure(dataStructure)
82+
, m_InputValues(inputValues)
83+
, m_ShouldCancel(shouldCancel)
84+
, m_MessageHandler(mesgHandler)
85+
{
86+
}
87+
88+
// -----------------------------------------------------------------------------
89+
MatrixCalculator::~MatrixCalculator() noexcept = default;
90+
91+
// -----------------------------------------------------------------------------
92+
const std::atomic_bool& MatrixCalculator::getCancel()
93+
{
94+
return m_ShouldCancel;
95+
}
96+
97+
// -----------------------------------------------------------------------------
98+
Result<> MatrixCalculator::operator()()
99+
{
100+
101+
auto& outputArray = m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->OutputPath);
102+
const auto& array1Ref = m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->SelectedPaths[0]);
103+
const auto& array2Ref = m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->SelectedPaths[1]);
104+
105+
if(array1Ref.getDataType() != array2Ref.getDataType())
106+
{
107+
return MakeErrorResult(-89750, "DataType mismatch");
108+
}
109+
110+
ExecuteDataFunction(MatrixOperationFunctor{}, array1Ref.getDataType(), array1Ref, array2Ref, outputArray, m_InputValues->Operation);
111+
112+
for(usize selectedArrayIdx = 2; selectedArrayIdx < m_InputValues->SelectedPaths.size(); selectedArrayIdx++)
113+
{
114+
115+
const auto& arrayRef = m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->SelectedPaths[selectedArrayIdx]);
116+
117+
ExecuteDataFunction(MatrixOperationFunctor{}, outputArray.getDataType(), outputArray, arrayRef, outputArray, m_InputValues->Operation);
118+
}
119+
120+
return {};
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma once
2+
3+
#include "SimplnxCore/SimplnxCore_export.hpp"
4+
5+
#include "simplnx/DataStructure/DataPath.hpp"
6+
#include "simplnx/DataStructure/DataStructure.hpp"
7+
#include "simplnx/Filter/IFilter.hpp"
8+
#include "simplnx/Parameters/ArrayCreationParameter.hpp"
9+
#include "simplnx/Parameters/ArraySelectionParameter.hpp"
10+
#include "simplnx/Parameters/ChoicesParameter.hpp"
11+
#include "simplnx/Parameters/MultiArraySelectionParameter.hpp"
12+
13+
namespace nx::core
14+
{
15+
16+
struct SIMPLNXCORE_EXPORT MatrixCalculatorInputValues
17+
{
18+
ChoicesParameter::ValueType Operation;
19+
MultiArraySelectionParameter::ValueType SelectedPaths;
20+
ArrayCreationParameter::ValueType OutputPath;
21+
};
22+
23+
namespace matrix_calculator::constants
24+
{
25+
26+
const std::string k_Addition("+ (Addition)");
27+
const std::string k_Subtraction("- (Subtraction)");
28+
const std::string k_Multiplication("* (Multiplication)");
29+
const nx::core::ChoicesParameter::Choices k_OperationChoices = {k_Addition, k_Subtraction, k_Multiplication};
30+
31+
const nx::core::ChoicesParameter::ValueType k_AdditionIdx = 0ULL;
32+
const nx::core::ChoicesParameter::ValueType k_SubtractionIdx = 1ULL;
33+
const nx::core::ChoicesParameter::ValueType k_MultiplicationIdx = 2ULL;
34+
} // namespace matrix_calculator::constants
35+
36+
/**
37+
* @class
38+
*/
39+
class SIMPLNXCORE_EXPORT MatrixCalculator
40+
{
41+
public:
42+
MatrixCalculator(DataStructure& dataStructure, const IFilter::MessageHandler& mesgHandler, const std::atomic_bool& shouldCancel, MatrixCalculatorInputValues* inputValues);
43+
~MatrixCalculator() noexcept;
44+
45+
MatrixCalculator(const MatrixCalculator&) = delete;
46+
MatrixCalculator(MatrixCalculator&&) noexcept = delete;
47+
MatrixCalculator& operator=(const MatrixCalculator&) = delete;
48+
MatrixCalculator& operator=(MatrixCalculator&&) noexcept = delete;
49+
50+
// Error Codes
51+
enum class ErrorCodes : int32
52+
{
53+
EmptyInputArrays = -2350,
54+
OneInputArray = -2351,
55+
NonPositiveTupleDimValue = -2352,
56+
TypeNameMismatch = -2353,
57+
ComponentShapeMismatch = -2354,
58+
InputArraysEqualAny = -2355,
59+
InputArraysUnsupported = -2356
60+
};
61+
62+
Result<> operator()();
63+
64+
const std::atomic_bool& getCancel();
65+
66+
private:
67+
DataStructure& m_DataStructure;
68+
const MatrixCalculatorInputValues* m_InputValues = nullptr;
69+
const std::atomic_bool& m_ShouldCancel;
70+
const IFilter::MessageHandler& m_MessageHandler;
71+
};
72+
73+
} // namespace nx::core

0 commit comments

Comments
 (0)