GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
gmValueAccessor.h
Go to the documentation of this file.
1/************************************************************************
2**
3** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4** All rights reserved.
5**
6** This file is part of the "GeMA" software. It's use should respect
7** the terms in the license agreement that can be found together
8** with this source code.
9** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11** A PARTICULAR PURPOSE.
12**
13************************************************************************/
14
23#ifndef _GEMA_VALUE_ACCESSOR_H_
24#define _GEMA_VALUE_ACCESSOR_H_
25
26#include "gmCoreConfig.h"
27#include "gmValueInfo.h"
28#include "gmMatrix.h"
29#include "gmVector.h"
30
31#include "gmThreadLocalBuffer.h"
32
34class GmLogCategory;
35
36class Unit;
37class UnitConverter;
38
39class GmUserFunction;
41
42class QVariant;
43
44class GmMesh;
45
60{
61public:
63 virtual ~GmValueAccessor() {}
64
68 virtual int size() const = 0;
69
71 virtual GmValueInfo* info() const = 0;
72
76 bool isScalar() const { return info()->isScalar(); }
77
81 virtual bool isDefValue(int index) const = 0;
82
87 virtual const double* defValue() const { return convertToAccessorUnit(info()->defValue()); }
88
94 virtual Unit unit() const = 0;
95
97 virtual bool hasUnitConversion() const = 0;
98
102 int valueSize() const { return info()->size(); }
103
121 virtual int adjustLinearIndex(int index, const GmMesh* m) { Q_UNUSED(m); return (index < size()) ? index : -1; }
122
126 virtual int adjustLinearIndex(int index, int firstGhostIndex) {
127 Q_UNUSED(firstGhostIndex); return (index < size()) ? index : -1;
128 }
129
157 virtual const double* valueAt(int index, const GmVector* coord) const = 0;
158
160 const double* value(int index) const { return valueAt(index, NULL); }
161
163 virtual double scalarValueAt(int index, const GmVector* coord) const = 0;
164
166 double scalarValue(int index) const { return scalarValueAt(index, NULL); }
167
171 void matrixValueAt(int index, const GmVector* coord, GmCRMatrix& mat) const
172 {
173 mat.setMemory(valueAt(index, coord), info()->nlin(), info()->ncol());
174 }
175
177 void matrixValue(int index, GmCRMatrix& mat) const { matrixValueAt(index, NULL, mat); }
178
182 void vectorValueAt(int index, const GmVector* coord, GmCRVector& vec) const
183 {
184 vec.setMemory(valueAt(index, coord), info()->size());
185 }
186
188 void vectorValue(int index, GmCRVector& vec) const { vectorValueAt(index, NULL, vec); }
189
202 virtual bool setValue(int index, const double* value) = 0;
203
205 virtual bool setScalarValue(int index, double value) = 0;
206
212 virtual bool setScalarValueAtDim(int index, int dim, double val)
213 {
214 int n = valueSize();
215 assert(dim >= 0 && dim < n);
216 QVarLengthArray<double, 128> data(n); // If size < 128, allocated on the stack
217 memcpy(data.data(), value(index), n * sizeof(double));
218 data[dim] = val;
219 return setValue(index, data.data());
220 }
221
223 virtual bool setValueAsDef(int index) = 0;
224
226 bool setMatrixValue(int index, const GmMatrix& mat)
227 {
228 assert(info()->dimType() == GM_MATRIX_VALUE && info()->nlin() == (int)mat.n_rows && info()->ncol() == (int)mat.n_cols);
229 return setValue(index, mat.memptr());
230 }
231
233 bool setVectorValue(int index, const GmVector& vec)
234 {
235 assert(info()->dimType() != GM_MATRIX_VALUE && info()->nlin() == (int)vec.n_rows);
236 return setValue(index, vec.memptr());
237 }
238
240 bool addScalarToValue(int index, double value)
241 {
242 return setScalarValue(index, scalarValue(index) + value);
243 }
244
249 bool addToValue(int index, const double* val)
250 {
251 int n = valueSize();
252 QVarLengthArray<double, 128> data(n); // If size < 128, allocated on the stack
253 const double* v = value(index);
254 for(int i = 0; i<n; i++)
255 data[i] = v[i] + val[i];
256 return setValue(index, data.data());
257 }
258
265 virtual bool setFunctionValue(int index, GmUserFunction* functionDef) = 0;
266
275 virtual bool setFunctionValue(int index, GmUserFunctionEvaluator* functionEval) = 0;
276
290 virtual QString valueStr(int index, bool evalFunctions, bool printDefAsNil,
291 int fieldWidth = 0, char format = 'g', int precision = -1,
292 const GmVector* coord = NULL) const = 0;
293
299 virtual bool setFunctionFromName(int index, QString userFunctionId, QString& err) = 0;
300
318 virtual bool setValueFromVariant(int index, const QVariant& v, bool acceptMissingDimension, QString& err) = 0;
319
323 virtual bool isFunction(int index) const = 0;
324
328 virtual QString functionId(int index) const = 0;
329
334 virtual void setEvalContext(GmValueSetEvalContext* context, bool ownsContext) = 0;
335
339 virtual GmValueSetEvalContext* evalContext() const = 0;
340
341protected:
342
346 virtual double convertToAccessorUnit(double val) const = 0;
347
352 virtual const double* convertToAccessorUnit(const double* p) const = 0;
353
355 virtual void convertFromAccessorUnit(double val, double* dst) const = 0;
356
358 virtual void convertFromAccessorUnit(const double* val, double* dst) const = 0;
359
360 friend class GmAccessorProxy; // GmAccessorProxy & GmGhostNodeAccessor needs access to unit
361 friend class GmGhostNodeAccessor; // conversion functions and those methods should not be general public ones...
362};
363
364
367{
368public:
369 GmValueAccessorBase(GmValueInfo* info, const GmLogCategory& logger, UnitConverter* conv, QString desiredUnit);
370
371 virtual ~GmValueAccessorBase();
372
373 // See comments on the base class
374 virtual GmValueInfo* info() const { return _info; }
375
376 // See comments on the base class
377 virtual Unit unit() const { return Unit(_unit); }
378
379 // See comments on the base class
380 virtual bool hasUnitConversion() const { return _conv; }
381
382 virtual QString valueStr(int index, bool evalFunctions, bool printDefAsNil,
383 int fieldWidth = 0, char format = 'g', int precision = -1,
384 const GmVector* coord = NULL) const;
385
386 virtual bool setFunctionFromName(int index, QString userFunctionId, QString& err);
387 virtual bool setValueFromVariant(int index, const QVariant& v, bool acceptMissingDimension, QString& err);
388
389 virtual void setEvalContext(GmValueSetEvalContext* context, bool ownsContext);
390 virtual GmValueSetEvalContext* evalContext() const { return _evalContext; }
391
392 static bool fillMatrixFromLuaTable(double* m, int nlin, int ncol, LuaTable& t, const GmValueInfo* info, const double* defVal, QString& err);
393
394protected:
395 friend class GmDiscontinuityAccessor; // Discontinuity Accessor needs access to tha AccessorBase logger
396
397 const double* evalValue(GmUserFunctionEvaluator* functionEval, int index, const GmVector* coord) const;
398 const double* evalValue(GmUserFunction* functionDef, int index, const GmVector* coord) const;
399 const double* evalDefValue(int index, const GmVector* coord) const;
400
401 virtual double convertToAccessorUnit(double val) const;
402 virtual const double* convertToAccessorUnit(const double* p) const;
403 virtual void convertFromAccessorUnit(double val, double* dst) const;
404 virtual void convertFromAccessorUnit(const double* val, double* dst) const;
405
406 double* convData() const { assert(_convData); return _convData->localBuffer(); }
407
410
412
416
417private:
418 Q_DISABLE_COPY(GmValueAccessorBase)
419
420 GmValueSetEvalContext* _evalContext;
421 bool _ownsEvalContext;
422};
423
424#endif
425
426
Helper class with common code for several classes that wrap a value accessor, provinding a slitly dif...
Definition gmAccessorProxy.h:39
An auxiliary matrix WRAPPER that binds the matrix to a CONST memory area with data already inicialize...
Definition gmMatrix.h:102
void setMemory(const double *data, int nlin, int ncol)
Exchanges the memory area used by the matrix. Same caveats explained in the class documentation apply...
Definition gmMatrix.h:132
An auxiliary vector WRAPPER that binds the vector to a CONST memory area with data already initialize...
Definition gmVector.h:72
void setMemory(const double *data, int nlin)
Exchanges the memory area used by the vector. Same caveats explained in the class documentation apply...
Definition gmVector.h:102
The GmDiscontinuityAccessor class is a proxy object to a value accesor implementing a more convenient...
Definition gmDiscontinuityAccessor.h:48
A proxy class that receives as parameters two accessors for the same data, one for the common node da...
Definition gmGhostNodeAccessor.h:76
Class representing a category with multiple logging levels.
Definition gmLog.h:58
Base interface class for Mesh type plugins.
Definition gmMesh.h:48
A class similar to GmTLS that creates a buffer for each possible thread in the GmThreadManager,...
Definition gmThreadLocalBuffer.h:106
Class responsible for evaluating a UserFunction over a node / cell.
Definition gmUserFunction.h:160
Class used to store the definition of a user function and its parameters.
Definition gmUserFunction.h:82
An implementation of basic attributes and functions that should be common for most value accessor imp...
Definition gmValueAccessor.h:367
UnitConverter * _rconv
Unit converter in the reverse direction: from desired unit to data unit.
Definition gmValueAccessor.h:414
virtual bool hasUnitConversion() const
Returns true if this accessor performs unit conversions, false if not.
Definition gmValueAccessor.h:380
UnitConverter * _conv
Unit converter.
Definition gmValueAccessor.h:413
virtual Unit unit() const
Returns the unit in which data returned/received by the accessor is expressed.
Definition gmValueAccessor.h:377
virtual GmValueSetEvalContext * evalContext() const
Returns the internal evaluation context. Needed by GmCellAccessorProxy, GmBcAccessorProxy & state dum...
Definition gmValueAccessor.h:390
GmTLBuffer< double, true > * _convData
Per thread local buffer used to store converted data. Null if _conv == NULL.
Definition gmValueAccessor.h:411
virtual GmValueInfo * info() const
Returns the information object describing the values returned by this accessor.
Definition gmValueAccessor.h:374
GmValueInfo * _info
The metadata describing the values returned by this accessor.
Definition gmValueAccessor.h:408
const GmLogCategory & _logger
The logger object used to report errors while accessing or setting data.
Definition gmValueAccessor.h:409
QString _unit
The unit in which data will be returned. Needed for the case when _conv = NULL and _info->unit() is e...
Definition gmValueAccessor.h:415
Interface class for accessing and setting values from an "indexable" collection of values.
Definition gmValueAccessor.h:60
int valueSize() const
Returns the size of the array returned by value(), i.e. 1 for scalar values and nlin * ncol for vecto...
Definition gmValueAccessor.h:102
virtual bool isDefValue(int index) const =0
Returns true if the value at the specified index is equal to the default value defined by info()->def...
virtual const double * convertToAccessorUnit(const double *p) const =0
An utility function that given a pointer to the stored data, converts the value to the accessor unit ...
virtual Unit unit() const =0
Returns the unit in which data returned/received by the accessor is expressed.
bool setVectorValue(int index, const GmVector &vec)
Similar to setValue() but receiving the data as a vector.
Definition gmValueAccessor.h:233
void matrixValue(int index, GmCRMatrix &mat) const
Similar to matrixValueAt(), passing NULL as coordinates.
Definition gmValueAccessor.h:177
virtual ~GmValueAccessor()
Virtual destructor.
Definition gmValueAccessor.h:63
const double * value(int index) const
Similar to valueAt(), passing NULL as coordinates.
Definition gmValueAccessor.h:160
bool isScalar() const
Returns true if the accessor returns scalar values (can be false even if valueSize() == 1 – think abo...
Definition gmValueAccessor.h:76
void vectorValue(int index, GmCRVector &vec) const
Similar to vectorValueAt(), passing NULL as coordinates.
Definition gmValueAccessor.h:188
virtual GmValueSetEvalContext * evalContext() const =0
Returns the internal evaluation context. Needed by GmCellAccessorProxy, GmBcAccessorProxy & state dum...
virtual bool setFunctionValue(int index, GmUserFunction *functionDef)=0
Overload of setValue() storing a user function definition.
virtual double convertToAccessorUnit(double val) const =0
An utility function that given a value, converts it to the accessor unit. If no conversion is needed,...
virtual QString valueStr(int index, bool evalFunctions, bool printDefAsNil, int fieldWidth=0, char format='g', int precision=-1, const GmVector *coord=NULL) const =0
Auxiliary function to return the value of an accessor converted to a string. Works for scalars,...
virtual void convertFromAccessorUnit(const double *val, double *dst) const =0
Copies the values given by val into the area pointed to by dst, converting units if needed.
virtual const double * valueAt(int index, const GmVector *coord) const =0
Returns the value associated with the "index" position of the data set, evaluated at the requested po...
virtual QString functionId(int index) const =0
Returns a function id name for a function reference at the index or an empty string for non function ...
virtual int adjustLinearIndex(int index, int firstGhostIndex)
Similar to adjustLinearIndex(int, const GmMesh*), this function overload gets as second parameter the...
Definition gmValueAccessor.h:126
virtual bool setValueFromVariant(int index, const QVariant &v, bool acceptMissingDimension, QString &err)=0
Sets a value with data read from a QVariant.
virtual int adjustLinearIndex(int index, const GmMesh *m)
Translates a linear index (from 0 to mesh->totalNumNodes()-1) into a valid index for this accessor or...
Definition gmValueAccessor.h:121
virtual bool setFunctionValue(int index, GmUserFunctionEvaluator *functionEval)=0
Overload of setValue() storing a user function evaluation context.
virtual bool setScalarValueAtDim(int index, int dim, double val)
Similar to setValue() but changing only the value at the given dimension. Can be used over scalar,...
Definition gmValueAccessor.h:212
virtual void setEvalContext(GmValueSetEvalContext *context, bool ownsContext)=0
Sets the EvalContext object that will be used to translate function names / objects into function eva...
virtual bool hasUnitConversion() const =0
Returns true if this accessor performs unit conversions, false if not.
virtual bool setScalarValue(int index, double value)=0
Similar to setValue() but passing a single double. Must be used for scalar data sets only.
virtual int size() const =0
Returns the number of values in the set referred to by the accessor, meaning that valid indices will ...
virtual const double * defValue() const
Returns the default value for the data converted to the accessor unit. The returned array contents is...
Definition gmValueAccessor.h:87
void matrixValueAt(int index, const GmVector *coord, GmCRMatrix &mat) const
Similar to valueAt(), but returning the result as a CONST matrix. Bear in mind that the resulting mat...
Definition gmValueAccessor.h:171
virtual void convertFromAccessorUnit(double val, double *dst) const =0
Copies the values given by val into the area pointed to by dst, converting units if needed.
void vectorValueAt(int index, const GmVector *coord, GmCRVector &vec) const
Similar to valueAt(), but returning the result as a CONST vector. Bear in mind that the resulting vec...
Definition gmValueAccessor.h:182
virtual bool setValue(int index, const double *value)=0
Allows for altering the value associated with entry "index" in the value set. This version of the fun...
virtual bool isFunction(int index) const =0
Returns true if the data in index is a reference to a function (in C or Lua). Used by valueStr() to m...
bool setMatrixValue(int index, const GmMatrix &mat)
Similar to setValue() but receiving the data as a matrix.
Definition gmValueAccessor.h:226
virtual GmValueInfo * info() const =0
Returns the information object describing the values returned by this accessor.
bool addScalarToValue(int index, double value)
Convenience function equivalent to calling setScalarValue(index, scalarValue(index) + value)
Definition gmValueAccessor.h:240
double scalarValue(int index) const
Similar to scalarValueAt(), passing NULL as coordinates.
Definition gmValueAccessor.h:166
virtual bool setFunctionFromName(int index, QString userFunctionId, QString &err)=0
Sets a function value from the function name.
virtual double scalarValueAt(int index, const GmVector *coord) const =0
Similar to valueAt() but returning a single double. Must be used for scalar data sets only.
bool addToValue(int index, const double *val)
Convenience function logically equivalent to calling setValue(index, value(index) + val) operating ov...
Definition gmValueAccessor.h:249
virtual bool setValueAsDef(int index)=0
Similar to setValue(), filling the index entry with the default value.
Auxiliar class used to store the definition of a value. It can be used to store informations about st...
Definition gmValueInfo.h:132
Auxiliar class used to store the needed information to translate an user function id / user function ...
Definition gmValueSetEvalContext.h:46
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Declaration of the GmMatrix class.
arma::mat GmMatrix
The basic type for a GeMA matrix object. Currently based on an Armadillo matrix.
Definition gmMatrix.h:38
Declaration of the GmTLBuffer class.
Declaration of the GmValueInfo class.
@ GM_MATRIX_VALUE
Means that the value is a 2D matrix of numbers.
Definition gmValueInfo.h:75
Declaration of the GmVector class.
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition gmVector.h:34