v2.0.0
Loading...
Searching...
No Matches
interpolation.cpp
Go to the documentation of this file.
1//=============================================================================================================
12
13//=============================================================================================================
14// INCLUDES
15//=============================================================================================================
16
17#include "interpolation.h"
18
19//=============================================================================================================
20// QT INCLUDES
21//=============================================================================================================
22
23#include <QSet>
24#include <QDebug>
25
26//=============================================================================================================
27// STL INCLUDES
28//=============================================================================================================
29
30#include <unordered_set>
31
32//=============================================================================================================
33// USED NAMESPACES
34//=============================================================================================================
35
36using namespace DISP3DLIB;
37using namespace Eigen;
38
39//=============================================================================================================
40// DEFINE MEMBER METHODS
41//=============================================================================================================
42
43QSharedPointer<SparseMatrix<float> > Interpolation::createInterpolationMat(const VectorXi &vecProjectedSensors,
44 const QSharedPointer<MatrixXd> matDistanceTable,
45 double (*interpolationFunction) (double),
46 const double dCancelDist,
47 const VectorXi &vecExcludeIndex)
48{
49 if(matDistanceTable->rows() == 0 && matDistanceTable->cols() == 0) {
50 qDebug() << "[WARNING] Interpolation::createInterpolationMat - received an empty distance table.";
51 return QSharedPointer<SparseMatrix<float> >::create();
52 }
53
54 QSharedPointer<Eigen::SparseMatrix<float> > matInterpolationMatrix = QSharedPointer<SparseMatrix<float> >::create(matDistanceTable->rows(), static_cast<int>(vecProjectedSensors.size()));
55
56 QVector<Triplet<float> > vecNonZeroEntries;
57 const qint32 iRows = matInterpolationMatrix->rows();
58 const qint32 iCols = matInterpolationMatrix->cols();
59
60 // Build exclude set for O(1) lookup
61 std::unordered_set<int> excludeSet;
62 for(Eigen::Index i = 0; i < vecExcludeIndex.size(); ++i) {
63 excludeSet.insert(vecExcludeIndex[i]);
64 }
65
66 QSet<qint32> sensorLookup;
67 for(Eigen::Index idx = 0; idx < vecProjectedSensors.size(); ++idx){
68 if(excludeSet.count(static_cast<int>(idx)) == 0){
69 sensorLookup.insert(vecProjectedSensors[idx]);
70 }
71 }
72
73 for (qint32 r = 0; r < iRows; ++r) {
74 if (sensorLookup.contains(r) == false) {
75 QVector<QPair<qint32, float> > vecBelowThresh;
76 float dWeightsSum = 0.0;
77 const RowVectorXd& rowVec = matDistanceTable->row(r);
78
79 for (qint32 c = 0; c < iCols; ++c) {
80 const float dDist = rowVec[c];
81
82 if (dDist < dCancelDist) {
83 const float dValueWeight = std::fabs(1.0 / interpolationFunction(dDist));
84 dWeightsSum += dValueWeight;
85 vecBelowThresh.push_back(qMakePair(c, dValueWeight));
86 }
87 }
88
89 for (const QPair<qint32, float> &qp : vecBelowThresh) {
90 vecNonZeroEntries.push_back(Eigen::Triplet<float> (r, qp.first, qp.second / dWeightsSum));
91 }
92 } else {
93 // Find index of r in vecProjectedSensors
94 int iIndexInSubset = 0;
95 for(Eigen::Index k = 0; k < vecProjectedSensors.size(); ++k) {
96 if(vecProjectedSensors[k] == r) {
97 iIndexInSubset = static_cast<int>(k);
98 break;
99 }
100 }
101 vecNonZeroEntries.push_back(Eigen::Triplet<float> (r, iIndexInSubset, 1));
102 }
103 }
104
105 matInterpolationMatrix->setFromTriplets(vecNonZeroEntries.begin(), vecNonZeroEntries.end());
106
107 return matInterpolationMatrix;
108}
109
110//=============================================================================================================
111
112VectorXf Interpolation::interpolateSignal(const QSharedPointer<SparseMatrix<float> > matInterpolationMatrix,
113 const QSharedPointer<VectorXf> &vecMeasurementData)
114{
115 if (matInterpolationMatrix->cols() != vecMeasurementData->rows()) {
116 qDebug() << "[WARNING] Interpolation::interpolateSignal - Dimension mismatch. Return null pointer...";
117 return VectorXf();
118 }
119
120 VectorXf pOutVec = *matInterpolationMatrix * (*vecMeasurementData);
121 return pOutVec;
122}
123
124//=============================================================================================================
125
126VectorXf Interpolation::interpolateSignal(const SparseMatrix<float> &matInterpolationMatrix,
127 const VectorXf &vecMeasurementData)
128{
129 if (matInterpolationMatrix.cols() != vecMeasurementData.rows()) {
130 qDebug() << "[WARNING] Interpolation::interpolateSignal - Dimension mismatch. Return null pointer...";
131 return VectorXf();
132 }
133
134 VectorXf pOutVec = matInterpolationMatrix * vecMeasurementData;
135 return pOutVec;
136}
137
138//=============================================================================================================
139
140double Interpolation::linear(const double dIn)
141{
142 return dIn;
143}
144
145//=============================================================================================================
146
147double Interpolation::gaussian(const double dIn)
148{
149 return exp(-((dIn * dIn) / 2.0));
150}
151
152//=============================================================================================================
153
154double Interpolation::square(const double dIn)
155{
156 return std::max((-(1.0f / 9.0f) * (dIn * dIn) + 1), 0.0);
157}
158
159//=============================================================================================================
160
161double Interpolation::cubic(const double dIn)
162{
163 return dIn * dIn * dIn;
164}
Distance-based sparse interpolation weights and per-frame signal smoothing on triangulated meshes.
3-D brain visualisation using the Qt RHI rendering backend.
static double gaussian(const double dIn)
gaussian Gaussian interpolation function (sigma=1).
static Eigen::VectorXf interpolateSignal(const QSharedPointer< Eigen::SparseMatrix< float > > matInterpolationMatrix, const QSharedPointer< Eigen::VectorXf > &vecMeasurementData)
interpolateSignal Interpolates sensor data using the weight matrix (shared pointer version).
static double linear(const double dIn)
linear Identity interpolation function.
static double cubic(const double dIn)
cubic Cubic hyperbola interpolation function.
static QSharedPointer< Eigen::SparseMatrix< float > > createInterpolationMat(const Eigen::VectorXi &vecProjectedSensors, const QSharedPointer< Eigen::MatrixXd > matDistanceTable, double(*interpolationFunction)(double), const double dCancelDist=FLOAT_INFINITY, const Eigen::VectorXi &vecExcludeIndex=Eigen::VectorXi())
createInterpolationMat Calculates the weight matrix for interpolation.
static double square(const double dIn)
square Negative parabola interpolation function with y-offset of 1.