Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cValueCurve.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cValueCurve_H
7 #define _INC_cValueCurve_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
14 
15 #ifdef GRAY_DLL // force implementation/instantiate for DLL/SO.
16 namespace Gray
17 {
18  template class GRAYLIB_LINK cArrayVal<float>;
19 }
20 #endif
21 
22 namespace GrayLib
23 {
25  class cVariant;
26 
27  template < typename TYPE = float >
29  {
34  public:
36  typedef TYPE VALUE_t;
37 
38  protected:
40 
41  public:
42  cValueCurveT() noexcept
43  {
44  }
46  {
47  m_aValues.SetCopy(src.m_aValues);
48  }
49  THIS_t& operator = (const THIS_t& src)
50  {
51  m_aValues.SetCopy(src.m_aValues);
52  return(*this);
53  }
54 
55  bool isEmpty() const noexcept
56  {
57  return m_aValues.IsEmpty();
58  }
59  void Init()
60  {
61  m_aValues.RemoveAll();
62  }
63 
65  {
66  return m_aValues.GetSize();
67  }
69  {
70  return m_aValues[i];
71  }
73  {
74  if (!m_aValues.IsValidIndex(i))
75  return 0;
76  return m_aValues[i];
77  }
78 
79  static VALUE_t GRAYCALL GetLinearA(const VALUE_t* pnArray, ITERATE_t iQty, ITERATE_t iSelect, ITERATE_t iSelectRange)
80  {
82  ASSERT(iSelectRange > 1);
83  ITERATE_t iIndex; // interpolate between this index and the one after it.
84  ITERATE_t iSelectElement = 0; // single element range.
85  switch (iQty)
86  {
87  case 0:
88  return(0); // no values defined !
89  case 1:
90  // iSelect value is irrelevant.
91  return(pnArray[0]);
92  case 2:
93  iIndex = 0;
94  break;
95  default:
96  iIndex = Calc::MulDiv<ITERATE_t>(iSelect, iQty, iSelectRange);
97  iQty--;
98  if (iIndex < 0)
99  iIndex = 0;
100  if (iIndex >= iQty)
101  iIndex = iQty - 1;
102  iSelectElement = iSelectRange / iQty;
103  iSelect -= (iIndex * iSelectElement);
104  break;
105  }
106 
107  // linear interpolate the 2 nearest values.
108  VALUE_t nLoVal = pnArray[iIndex + 0];
109  VALUE_t nHiVal = pnArray[iIndex + 1];
110  VALUE_t nVal = nLoVal + Calc::MulDiv<VALUE_t>(nHiVal - nLoVal, (VALUE_t)iSelect, (VALUE_t)iSelectElement);
111 
112  // NOTE: nVal can be extrapolated above or below the range.
113  // i.e. nVal can be negative.
114  return(nVal);
115  }
116 
117  VALUE_t GetLinearN(ITERATE_t iSelect, ITERATE_t iSelectRange) const
118  {
121  if (iSelectRange <= 0)
122  {
123  iSelect = 0;
124  iSelectRange = 1;
125  }
126  return(GetLinearA(m_aValues.GetData(), m_aValues.GetSize(), iSelect, iSelectRange));
127  }
128 
129  VALUE_t GetLinear1(float fOne) const
130  {
133  return(GetLinearA(m_aValues.GetData(), m_aValues.GetSize(), (ITERATE_t)(fOne * SHRT_MAX), SHRT_MAX));
134  }
135 
136  void SetValues(ITERATE_t iQty, const VALUE_t* pnVals)
137  {
139  ASSERT(pnVals != nullptr);
140  m_aValues.SetSize(iQty);
141  for (ITERATE_t i = 0; i < iQty; i++)
142  {
143  m_aValues.SetAt(i, pnVals[i]);
144  }
145  }
147  {
149  m_aValues.SetSize(2);
150  m_aValues.SetAt(0, nLo);
151  m_aValues.SetAt(1, nHi);
152  }
153  };
154 
156  {
161  public:
162  cValueCurvef() noexcept
163  {
164  }
166  : cValueCurveT<float>(src)
167  {
168  }
169 
170  bool v_SetCurve(const cVariant& vVal);
171  void v_GetCurve(cVariant& vVal) const;
172 
173  VALUE_t GetLinearF(float fAge, float fLife) const;
174  VALUE_t GetLinear1000(int iPercent1000) const;
175  VALUE_t GetChance1000(int iPercent1000) const;
176 
178  };
179 }
180 
181 #endif // _INC_cValueCurve_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define TYPE
Definition: StrT.cpp:38
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define UNITTEST2_PREDEF(x)
Definition: cUnitTestDecl.h:19
Definition: cValueCurve.h:29
static VALUE_t GRAYCALL GetLinearA(const VALUE_t *pnArray, ITERATE_t iQty, ITERATE_t iSelect, ITERATE_t iSelectRange)
Definition: cValueCurve.h:79
bool isEmpty() const noexcept
Definition: cValueCurve.h:55
void Init()
Definition: cValueCurve.h:59
cValueCurveT() noexcept
Definition: cValueCurve.h:42
VALUE_t GetLinear1(float fOne) const
Definition: cValueCurve.h:129
void SetValuesRange(VALUE_t nLo, VALUE_t nHi)
Definition: cValueCurve.h:146
cArrayVal< VALUE_t > m_aValues
array of values for defining the curve. values spaced evenly.
Definition: cValueCurve.h:39
VALUE_t EnumValueCheck(ITERATE_t i) const
Definition: cValueCurve.h:72
VALUE_t GetLinearN(ITERATE_t iSelect, ITERATE_t iSelectRange) const
Definition: cValueCurve.h:117
cValueCurveT< TYPE > THIS_t
Definition: cValueCurve.h:35
cValueCurveT(const THIS_t &src)
Definition: cValueCurve.h:45
ITERATE_t get_EnumQty() const
Definition: cValueCurve.h:64
void SetValues(ITERATE_t iQty, const VALUE_t *pnVals)
Definition: cValueCurve.h:136
VALUE_t EnumValue(ITERATE_t i) const
Definition: cValueCurve.h:68
TYPE VALUE_t
value/dimension type. like DVALUE_t
Definition: cValueCurve.h:36
Definition: cValueCurve.h:156
UNITTEST_FRIEND(cValueCurvef)
cValueCurvef(const cValueCurvef &src)
Definition: cValueCurve.h:165
cValueCurvef() noexcept
Definition: cValueCurve.h:162
Definition: cVariant.h:26
void RemoveAll()
Clean up.
Definition: cArray.h:230
bool IsEmpty() const noexcept
Definition: cArray.h:145
void SetAt(ITERATE_t nIndex, ARG_TYPE newElement)
Definition: cArray.h:173
ITERATE_t GetSize() const noexcept
Definition: cArray.h:137
const TYPE * GetData() const
Definition: cArray.h:181
void SetSize(ITERATE_t nNewSize)
Definition: cArray.h:248
void SetCopy(const cArrayTyped< TYPE, ARG_TYPE > &aValues)
Definition: cArray.h:598
bool IsValidIndex(ITERATE_t i) const noexcept
Definition: cArray.h:495
Definition: cMesh.h:22
class __DECL_IMPORT cVariant
Definition: cJSONWriter.h:19
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int ITERATE_t
like size_t but signed
Definition: Index.h:28
Definition: cDebugAssert.h:29