GemaCoreLib
The GeMA Core library
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 
34 class GmLogCategory;
35 
36 class Unit;
37 class UnitConverter;
38 
39 class GmUserFunction;
41 
42 class QVariant;
43 
44 class GmMesh;
45 
60 {
61 public:
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) { Q_UNUSED(firstGhostIndex); return (index < size()) ? index : -1; }
127 
155  virtual const double* valueAt(int index, const GmVector* coord) const = 0;
156 
158  const double* value(int index) const { return valueAt(index, NULL); }
159 
161  virtual double scalarValueAt(int index, const GmVector* coord) const = 0;
162 
164  double scalarValue(int index) const { return scalarValueAt(index, NULL); }
165 
169  void matrixValueAt(int index, const GmVector* coord, GmCRMatrix& mat) const
170  {
171  mat.setMemory(valueAt(index, coord), info()->nlin(), info()->ncol());
172  }
173 
175  void matrixValue(int index, GmCRMatrix& mat) const { matrixValueAt(index, NULL, mat); }
176 
180  void vectorValueAt(int index, const GmVector* coord, GmCRVector& vec) const
181  {
182  vec.setMemory(valueAt(index, coord), info()->size());
183  }
184 
186  void vectorValue(int index, GmCRVector& vec) const { vectorValueAt(index, NULL, vec); }
187 
200  virtual bool setValue(int index, const double* value) = 0;
201 
203  virtual bool setScalarValue(int index, double value) = 0;
204 
210  virtual bool setScalarValueAtDim(int index, int dim, double val)
211  {
212  int n = valueSize();
213  assert(dim >= 0 && dim < n);
214  QVarLengthArray<double, 128> data(n); // If size < 128, allocated on the stack
215  memcpy(data.data(), value(index), n * sizeof(double));
216  data[dim] = val;
217  return setValue(index, data.data());
218  }
219 
221  virtual bool setValueAsDef(int index) = 0;
222 
224  bool setMatrixValue(int index, const GmMatrix& mat)
225  {
226  assert(info()->dimType() == GM_MATRIX_VALUE && info()->nlin() == (int)mat.n_rows && info()->ncol() == (int)mat.n_cols);
227  return setValue(index, mat.memptr());
228  }
229 
231  bool setVectorValue(int index, const GmVector& vec)
232  {
233  assert(info()->dimType() != GM_MATRIX_VALUE && info()->nlin() == (int)vec.n_rows);
234  return setValue(index, vec.memptr());
235  }
236 
238  bool addScalarToValue(int index, double value)
239  {
240  return setScalarValue(index, scalarValue(index) + value);
241  }
242 
247  bool addToValue(int index, const double* val)
248  {
249  int n = valueSize();
250  QVarLengthArray<double, 128> data(n); // If size < 128, allocated on the stack
251  const double* v = value(index);
252  for(int i = 0; i<n; i++)
253  data[i] = v[i] + val[i];
254  return setValue(index, data.data());
255  }
256 
263  virtual bool setFunctionValue(int index, GmUserFunction* functionDef) = 0;
264 
273  virtual bool setFunctionValue(int index, GmUserFunctionEvaluator* functionEval) = 0;
274 
288  virtual QString valueStr(int index, bool evalFunctions, bool printDefAsNil,
289  int fieldWidth = 0, char format = 'g', int precision = -1,
290  const GmVector* coord = NULL) const = 0;
291 
297  virtual bool setFunctionFromName(int index, QString userFunctionId, QString& err) = 0;
298 
316  virtual bool setValueFromVariant(int index, const QVariant& v, bool acceptMissingDimension, QString& err) = 0;
317 
321  virtual bool isFunction(int index) const = 0;
322 
326  virtual QString functionId(int index) const = 0;
327 
332  virtual void setEvalContext(GmValueSetEvalContext* context, bool ownsContext) = 0;
333 
337  virtual GmValueSetEvalContext* evalContext() const = 0;
338 
339 protected:
340 
344  virtual double convertToAccessorUnit(double val) const = 0;
345 
350  virtual const double* convertToAccessorUnit(const double* p) const = 0;
351 
353  virtual void convertFromAccessorUnit(double val, double* dst) const = 0;
354 
356  virtual void convertFromAccessorUnit(const double* val, double* dst) const = 0;
357 
358  friend class GmAccessorProxy; // GmAccessorProxy & GmGhostNodeAccessor needs access to unit
359  friend class GmGhostNodeAccessor; // conversion functions and those methods should not be general public ones...
360 };
361 
362 
365 {
366 public:
367  GmValueAccessorBase(GmValueInfo* info, const GmLogCategory& logger, UnitConverter* conv, QString desiredUnit);
368 
369  virtual ~GmValueAccessorBase();
370 
371  // See comments on the base class
372  virtual GmValueInfo* info() const { return _info; }
373 
374  // See comments on the base class
375  virtual Unit unit() const { return Unit(_unit); }
376 
377  // See comments on the base class
378  virtual bool hasUnitConversion() const { return _conv; }
379 
380  virtual QString valueStr(int index, bool evalFunctions, bool printDefAsNil,
381  int fieldWidth = 0, char format = 'g', int precision = -1,
382  const GmVector* coord = NULL) const;
383 
384  virtual bool setFunctionFromName(int index, QString userFunctionId, QString& err);
385  virtual bool setValueFromVariant(int index, const QVariant& v, bool acceptMissingDimension, QString& err);
386 
387  virtual void setEvalContext(GmValueSetEvalContext* context, bool ownsContext);
388  virtual GmValueSetEvalContext* evalContext() const { return _evalContext; }
389 
390  static bool fillMatrixFromLuaTable(double* m, int nlin, int ncol, LuaTable& t, const GmValueInfo* info, const double* defVal, QString& err);
391 
392 protected:
393  friend class GmDiscontinuityAccessor; // Discontinuity Accessor needs access to tha AccessorBase logger
394 
395  const double* evalValue(GmUserFunctionEvaluator* functionEval, int index, const GmVector* coord) const;
396  const double* evalValue(GmUserFunction* functionDef, int index, const GmVector* coord) const;
397  const double* evalDefValue(int index, const GmVector* coord) const;
398 
399  virtual double convertToAccessorUnit(double val) const;
400  virtual const double* convertToAccessorUnit(const double* p) const;
401  virtual void convertFromAccessorUnit(double val, double* dst) const;
402  virtual void convertFromAccessorUnit(const double* val, double* dst) const;
403 
404  double* convData() const { assert(_convData); return _convData->localBuffer(); }
405 
408 
410 
414 
415 private:
416  Q_DISABLE_COPY(GmValueAccessorBase)
417 
418  GmValueSetEvalContext* _evalContext;
419  bool _ownsEvalContext;
420 };
421 
422 #endif
423 
424 
Auxiliar class used to store the definition of a value. It can be used to store informations about st...
Definition: gmValueInfo.h:126
virtual bool hasUnitConversion() const
Returns true if this accessor perfroms unit conversions, false if not.
Definition: gmValueAccessor.h:378
virtual GmValueInfo * info() const
Returns the information object describing the values returned by this accessor.
Definition: gmValueAccessor.h:372
void vectorValue(int index, GmCRVector &vec) const
Similar to vectorValueAt(), passing NULL as coordinates.
Definition: gmValueAccessor.h:186
bool isScalar() const
Returns true if the accessor returns scalar values (can be false even if valueSize() == 1 – think abo...
Definition: gmValueAccessor.h:76
bool addToValue(int index, const double *val)
Convenience function logically equivalent to calling setValue(index, value(index) + val) operating ov...
Definition: gmValueAccessor.h:247
void matrixValue(int index, GmCRMatrix &mat) const
Similar to matrixValueAt(), passing NULL as coordinates.
Definition: gmValueAccessor.h:175
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 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
double scalarValue(int index) const
Similar to scalarValueAt(), passing NULL as coordinates.
Definition: gmValueAccessor.h:164
virtual Unit unit() const
Returns the unit in which data returned/received by the accessor is expressed.
Definition: gmValueAccessor.h:375
Class responsible for evaluating a UserFunction over a node / cell.
Definition: gmUserFunction.h:148
virtual void setEvalContext(GmValueSetEvalContext *context, bool ownsContext)=0
Sets the EvalContext object that will be used to translate function names / objects into function eva...
Declaration of usefull configuration definitions for the Core library.
Declaration of the GmValueInfo class.
const double * value(int index) const
Similar to valueAt(), passing NULL as coordinates.
Definition: gmValueAccessor.h:158
Declaration of the GmTLBuffer class.
virtual GmValueSetEvalContext * evalContext() const
Returns the internal evaluation context. Needed by GmCellAccessorProxy, GmBcAccessorProxy & state dum...
Definition: gmValueAccessor.h:388
Declaration of the GmMatrix class.
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:100
Interface class for accessing and setting values from an "indexable" collection of values.
Definition: gmValueAccessor.h:59
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 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.
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:210
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:180
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:413
virtual bool setFunctionFromName(int index, QString userFunctionId, QString &err)=0
Sets a function value from the function name.
virtual ~GmValueAccessor()
Virtual destructor.
Definition: gmValueAccessor.h:63
GmValueInfo * _info
The metadata describing the values returned by this accessor.
Definition: gmValueAccessor.h:406
Declaration of the GmVector class.
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,...
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
An auxiliary vector WRAPPER that binds the vector to a CONST memory area with data already initialize...
Definition: gmVector.h:69
UnitConverter * _conv
Unit converter.
Definition: gmValueAccessor.h:411
Auxiliar class used to store the needed information to translate an user function id / user function ...
Definition: gmValueSetEvalContext.h:45
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,...
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
Means that the value is a 2D matrix of numbers.
Definition: gmValueInfo.h:70
UnitConverter * _rconv
Unit converter in the reverse direction: from desired unit to data unit.
Definition: gmValueAccessor.h:412
A proxy class that receives as parameters two accessors for the same data, one for the common node da...
Definition: gmGhostNodeAccessor.h:75
The GmDiscontinuityAccessor class is a proxy object to a value accesor implementing a more convenient...
Definition: gmDiscontinuityAccessor.h:47
bool setVectorValue(int index, const GmVector &vec)
Similar to setValue() but receiving the data as a vector.
Definition: gmValueAccessor.h:231
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:169
virtual bool setValueFromVariant(int index, const QVariant &v, bool acceptMissingDimension, QString &err)=0
Sets a value with data read from a QVariant.
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition: gmVector.h:34
bool addScalarToValue(int index, double value)
Convenience function equivalent to calling setScalarValue(index, scalarValue(index) + value)
Definition: gmValueAccessor.h:238
bool setMatrixValue(int index, const GmMatrix &mat)
Similar to setValue() but receiving the data as a matrix.
Definition: gmValueAccessor.h:224
Base interface class for Mesh type plugins.
Definition: gmMesh.h:44
An auxiliary matrix WRAPPER that binds the matrix to a CONST memory area with data already inicialize...
Definition: gmMatrix.h:100
An implementation of basic attributes and functions that should be common for most value accessor imp...
Definition: gmValueAccessor.h:364
arma::mat GmMatrix
The basic type for a GeMA matrix object. Currently based on an Armadillo matrix.
Definition: gmMatrix.h:38
Class used to store the definition of a user function and its parameters.
Definition: gmUserFunction.h:78
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:131
Helper class with common code for several classes that wrap a value accessor, provinding a slitly dif...
Definition: gmAccessorProxy.h:38
GmTLBuffer< double, true > * _convData
Per thread local buffer used to store converted data. Null if _conv == NULL.
Definition: gmValueAccessor.h:409
const GmLogCategory & _logger
The logger object used to report errors while accessing or setting data.
Definition: gmValueAccessor.h:407
virtual int adjustLinearIndex(int index, int firstGhostIndex)
Simmilar to adjustLinearIndex(int, const GmMesh*), this function overload gets as second parameter th...
Definition: gmValueAccessor.h:126