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)
return statsProcess.output().at(0);
}
vtkDataArray* medUtilities::getArray(dtkSmartPointer<medAbstractData> data,
vtkDataArray* medUtilities::getArray(medAbstractData* data,
QString arrayName)
{
vtkDataArray* result = nullptr;
......@@ -285,7 +285,51 @@ vtkDataArray* medUtilities::getArray(dtkSmartPointer<medAbstractData> data,
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,
int index)
{
......@@ -305,41 +349,44 @@ QList<double> medUtilities::peekArray(dtkSmartPointer<medAbstractData> data,
return result;
}
QList<double> medUtilities::arrayRange(dtkSmartPointer<medAbstractData> data,
QString arrayName,
int component)
bool medUtilities::arrayRange(medAbstractData* data,
QString arrayName,
double* minRange,
double* maxRange,
int component)
{
QList<double> result;
vtkDataArray* array = getArray(data, arrayName);
if (array && (component < array->GetNumberOfComponents()))
{
double* range = new double[2]();
array->GetRange(range, component);
result.push_back(range[0]);
result.push_back(range[1]);
*minRange = range[0];
*maxRange = range[1];
delete[] range;
return true;
}
return result;
return false;
}
QList<double> medUtilities::arrayStats(dtkSmartPointer<medAbstractData> data,
QString arrayName,
int component)
bool medUtilities::arrayStats(medAbstractData* data,
QString arrayName,
double* mean,
double* stdDev,
int component)
{
QList<double> result;
vtkDataArray* array = getArray(data, arrayName);
if (array && (component < array->GetNumberOfComponents()))
{
vtkIdType nbTuples = array->GetNumberOfTuples();
// compute mean and variance
double value, tmpMean, delta, mean, variance;
value = tmpMean = delta = mean = variance = 0.0;
double value, tmpMean, delta, finalMean, variance;
value = tmpMean = delta = finalMean = variance = 0.0;
for (vtkIdType i = 0; i < nbTuples; ++i)
{
value = array->GetComponent(i, component);
tmpMean = mean;
delta = value - mean;
mean += delta / (i + 1);
tmpMean = finalMean;
delta = value - finalMean;
finalMean += delta / (i + 1);
variance += delta * (value - tmpMean);
}
if (nbTuples > 1)
......@@ -351,8 +398,9 @@ QList<double> medUtilities::arrayStats(dtkSmartPointer<medAbstractData> data,
variance = 0.0;
}
result.push_back(mean);
result.push_back(std::sqrt(variance));
*mean = finalMean;
*stdDev = std::sqrt(variance);
return true;
}
return result;
return false;
}
......@@ -20,6 +20,13 @@ public:
MSR
};
enum DataArrayType
{
POINT_ARRAY,
CELL_ARRAY,
FIELD_ARRAY
};
static void setDerivedMetaData(medAbstractData* derived, medAbstractData* original,
QString derivationDescription, bool queryForDescription = false);
......@@ -59,13 +66,24 @@ public:
* @brief Retrieves an array from input data. This functions first looks in
* 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
* @return specified array if it exits, nullptr otherwise
*/
static vtkDataArray* getArray(dtkSmartPointer<medAbstractData> data,
static vtkDataArray* getArray(medAbstractData* data,
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.
*
......@@ -74,7 +92,7 @@ public:
* @param[in] index tuple index
* @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,
int index);
/**
......@@ -82,14 +100,17 @@ public:
*
* @param[in] data input data, must be a mesh or map
* @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.
* Pass -1 to compute the L2 norm over all components.
* @return list with 2 values: minimum and maximum. Returns an empty
* list on failure
* @return true if all went well, false otherwise
*/
static QList<double> arrayRange(dtkSmartPointer<medAbstractData> data,
QString arrayName,
int component = 0);
static bool arrayRange(medAbstractData* data,
QString arrayName,
double* minRange,
double* maxRange,
int component = 0);
/**
* @brief Computes mean and standard deviation of an real-valued array.
......@@ -101,7 +122,9 @@ public:
* @return list with 2 values: mean and standard deviation (in that order).
* Returns an empty list on failure.
*/
static QList<double> arrayStats(dtkSmartPointer<medAbstractData> data,
QString arrayName,
int component = 0);
static bool arrayStats(medAbstractData* data,
QString arrayName,
double* mean,
double* stdDev,
int component = 0);
};
......@@ -17,7 +17,6 @@
#include <medVtkInriaExport.h>
#include <string>
#include <vector>
#include <unordered_map>
#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