Commit 24ce0e83 authored by JUHOOR Mehdi's avatar JUHOOR Mehdi Committed by fcollot

[medUtilities] Add utility methods (#410)

* Add getArrayIndex function.

* Small change to doc

* Methods that had multiple return values now use output parameters. Remove unneeded include.
parent e40075a4
...@@ -260,7 +260,7 @@ double medUtilities::volume(dtkSmartPointer<medAbstractData> data) ...@@ -260,7 +260,7 @@ double medUtilities::volume(dtkSmartPointer<medAbstractData> data)
return statsProcess.output().at(0); return statsProcess.output().at(0);
} }
vtkDataArray* medUtilities::getArray(dtkSmartPointer<medAbstractData> data, vtkDataArray* medUtilities::getArray(medAbstractData* data,
QString arrayName) QString arrayName)
{ {
vtkDataArray* result = nullptr; vtkDataArray* result = nullptr;
...@@ -285,7 +285,51 @@ vtkDataArray* medUtilities::getArray(dtkSmartPointer<medAbstractData> data, ...@@ -285,7 +285,51 @@ vtkDataArray* medUtilities::getArray(dtkSmartPointer<medAbstractData> data,
return result; return result;
} }
QList<double> medUtilities::peekArray(dtkSmartPointer<medAbstractData> data, int medUtilities::getArrayIndex(medAbstractData* data,
QString arrayName,
DataArrayType* arrayType)
{
int arrayId = -1;
DataArrayType type;
if (data->identifier().contains("vtkDataMesh") ||
data->identifier().contains("EPMap"))
{
vtkMetaDataSet* metaData = static_cast<vtkMetaDataSet*>(data->data());
vtkDataSet* mesh = metaData->GetDataSet();
// try point data first
(void)mesh->GetPointData()->GetAbstractArray(qPrintable(arrayName), arrayId);
if (arrayId == -1)
{
(void)mesh->GetCellData()->GetAbstractArray(qPrintable(arrayName), arrayId);
if (arrayId == -1)
{
(void)mesh->GetFieldData()->GetAbstractArray(qPrintable(arrayName), arrayId);
if (arrayId != -1)
{
type = DataArrayType::FIELD_ARRAY;
}
}
else
{
type = DataArrayType::CELL_ARRAY;
}
}
else
{
type = DataArrayType::POINT_ARRAY;
}
}
if (arrayType)
{
*arrayType = type;
}
return arrayId;
}
QList<double> medUtilities::peekArray(medAbstractData* data,
QString arrayName, QString arrayName,
int index) int index)
{ {
...@@ -305,41 +349,44 @@ QList<double> medUtilities::peekArray(dtkSmartPointer<medAbstractData> data, ...@@ -305,41 +349,44 @@ QList<double> medUtilities::peekArray(dtkSmartPointer<medAbstractData> data,
return result; return result;
} }
QList<double> medUtilities::arrayRange(dtkSmartPointer<medAbstractData> data, bool medUtilities::arrayRange(medAbstractData* data,
QString arrayName, QString arrayName,
int component) double* minRange,
double* maxRange,
int component)
{ {
QList<double> result;
vtkDataArray* array = getArray(data, arrayName); vtkDataArray* array = getArray(data, arrayName);
if (array && (component < array->GetNumberOfComponents())) if (array && (component < array->GetNumberOfComponents()))
{ {
double* range = new double[2](); double* range = new double[2]();
array->GetRange(range, component); array->GetRange(range, component);
result.push_back(range[0]); *minRange = range[0];
result.push_back(range[1]); *maxRange = range[1];
delete[] range; delete[] range;
return true;
} }
return result; return false;
} }
QList<double> medUtilities::arrayStats(dtkSmartPointer<medAbstractData> data, bool medUtilities::arrayStats(medAbstractData* data,
QString arrayName, QString arrayName,
int component) double* mean,
double* stdDev,
int component)
{ {
QList<double> result;
vtkDataArray* array = getArray(data, arrayName); vtkDataArray* array = getArray(data, arrayName);
if (array && (component < array->GetNumberOfComponents())) if (array && (component < array->GetNumberOfComponents()))
{ {
vtkIdType nbTuples = array->GetNumberOfTuples(); vtkIdType nbTuples = array->GetNumberOfTuples();
// compute mean and variance // compute mean and variance
double value, tmpMean, delta, mean, variance; double value, tmpMean, delta, finalMean, variance;
value = tmpMean = delta = mean = variance = 0.0; value = tmpMean = delta = finalMean = variance = 0.0;
for (vtkIdType i = 0; i < nbTuples; ++i) for (vtkIdType i = 0; i < nbTuples; ++i)
{ {
value = array->GetComponent(i, component); value = array->GetComponent(i, component);
tmpMean = mean; tmpMean = finalMean;
delta = value - mean; delta = value - finalMean;
mean += delta / (i + 1); finalMean += delta / (i + 1);
variance += delta * (value - tmpMean); variance += delta * (value - tmpMean);
} }
if (nbTuples > 1) if (nbTuples > 1)
...@@ -351,8 +398,9 @@ QList<double> medUtilities::arrayStats(dtkSmartPointer<medAbstractData> data, ...@@ -351,8 +398,9 @@ QList<double> medUtilities::arrayStats(dtkSmartPointer<medAbstractData> data,
variance = 0.0; variance = 0.0;
} }
result.push_back(mean); *mean = finalMean;
result.push_back(std::sqrt(variance)); *stdDev = std::sqrt(variance);
return true;
} }
return result; return false;
} }
...@@ -20,6 +20,13 @@ public: ...@@ -20,6 +20,13 @@ public:
MSR MSR
}; };
enum DataArrayType
{
POINT_ARRAY,
CELL_ARRAY,
FIELD_ARRAY
};
static void setDerivedMetaData(medAbstractData* derived, medAbstractData* original, static void setDerivedMetaData(medAbstractData* derived, medAbstractData* original,
QString derivationDescription, bool queryForDescription = false); QString derivationDescription, bool queryForDescription = false);
...@@ -59,13 +66,24 @@ public: ...@@ -59,13 +66,24 @@ public:
* @brief Retrieves an array from input data. This functions first looks in * @brief Retrieves an array from input data. This functions first looks in
* point data, then cell data and finally in field data. * point data, then cell data and finally in field data.
* *
* @param[in] data input data, must be a mesh or map * @param[in] data input data, must be a mesh or map
* @param[in] arrayName array to retrieve * @param[in] arrayName array to retrieve
* @return specified array if it exits, nullptr otherwise * @return specified array if it exits, nullptr otherwise
*/ */
static vtkDataArray* getArray(dtkSmartPointer<medAbstractData> data, static vtkDataArray* getArray(medAbstractData* data,
QString arrayName); QString arrayName);
/**
* @brief Return array index and data type as optional argument
* @param[in] data input data, must be a mesh or map
* @param[in] arrayName array name
* @param[out] arrayType type of array: point, cell or field array
* @return data index of array
*/
static int getArrayIndex(medAbstractData* data,
QString arrayName,
DataArrayType* arrayType = nullptr);
/** /**
* @brief Retrieve single tuple from a real-valued array. * @brief Retrieve single tuple from a real-valued array.
* *
...@@ -74,7 +92,7 @@ public: ...@@ -74,7 +92,7 @@ public:
* @param[in] index tuple index * @param[in] index tuple index
* @return tuple as a list. Returns a empty list on failure * @return tuple as a list. Returns a empty list on failure
*/ */
static QList<double> peekArray(dtkSmartPointer<medAbstractData> data, static QList<double> peekArray(medAbstractData* data,
QString arrayName, QString arrayName,
int index); int index);
/** /**
...@@ -82,14 +100,17 @@ public: ...@@ -82,14 +100,17 @@ public:
* *
* @param[in] data input data, must be a mesh or map * @param[in] data input data, must be a mesh or map
* @param[in] arrayName input array name * @param[in] arrayName input array name
* @param[out] minRange minimum range
* @param[out] maxRange maximum range
* @param[in] component range will be computed on this component. * @param[in] component range will be computed on this component.
* Pass -1 to compute the L2 norm over all components. * Pass -1 to compute the L2 norm over all components.
* @return list with 2 values: minimum and maximum. Returns an empty * @return true if all went well, false otherwise
* list on failure
*/ */
static QList<double> arrayRange(dtkSmartPointer<medAbstractData> data, static bool arrayRange(medAbstractData* data,
QString arrayName, QString arrayName,
int component = 0); double* minRange,
double* maxRange,
int component = 0);
/** /**
* @brief Computes mean and standard deviation of an real-valued array. * @brief Computes mean and standard deviation of an real-valued array.
...@@ -101,7 +122,9 @@ public: ...@@ -101,7 +122,9 @@ public:
* @return list with 2 values: mean and standard deviation (in that order). * @return list with 2 values: mean and standard deviation (in that order).
* Returns an empty list on failure. * Returns an empty list on failure.
*/ */
static QList<double> arrayStats(dtkSmartPointer<medAbstractData> data, static bool arrayStats(medAbstractData* data,
QString arrayName, QString arrayName,
int component = 0); double* mean,
double* stdDev,
int component = 0);
}; };
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include <medVtkInriaExport.h> #include <medVtkInriaExport.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map>
#include <QString> #include <QString>
......
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