Fission FSN
Loading...
Searching...
No Matches
fsCGraph.h
Go to the documentation of this file.
1/********************************************************************
2*
3* Created: 8/3/2010 9:03
4*
5* File: fsCGraph.h
6*
7* Author: Martin King
8*
9* Purpose: Calculate variation of a couple of values
10*
11*********************************************************************/
12
13#ifndef fsCGraphhdr
14#define fsCGraphhdr
15
16#include <vector>
17
19#include <fsCore/fsBaseTypes.h>
20
21
22template <typename TValue>
24{
25public:
26
27 void clear() { mValues.clear(); }
28 void dataAdd(fsS32 pDuration, const TValue& pValue);
29
31 TValue valueGet(fsS32 pX) const;
32
34 {
35 }
36
37 fsCGraph(const TValue& pInitialValue)
38 {
39 mValues.push_back(std::pair<fsS32, TValue>(0, pInitialValue));
40 }
41
43 {
44 }
45
46protected:
47
48private:
49
50 std::vector<std::pair<fsS32, TValue>> mValues;
51};
52
53
54template <typename TValue>
55void fsCGraph<TValue>::dataAdd(fsS32 pDuration, const TValue& pValue)
56{
57 if (mValues.empty())
58 {
59 fsAssert(pDuration == 0, "First value must have a duration of 0.\n");
60 mValues.push_back(std::pair<fsS32, TValue>(0, pValue));
61 }
62 else
63 {
64 mValues.push_back(std::pair<fsS32, TValue>(mValues.back().first + pDuration, pValue));
65 }
66}
67
68template <typename TValue>
70{
71 return mValues.back().first;
72}
73
74template <typename TValue>
76{
77 typename std::vector<std::pair<fsS32, TValue>>::const_iterator currVal = mValues.begin();
78
79 if (pX < mValues.back().first)
80 {
81 while (currVal != mValues.end())
82 {
83 if (pX <= (*currVal).first)
84 {
85 if (pX == (*currVal).first)
86 {
87 return (*currVal).second;
88 }
89
90 if (mValues.begin() == currVal)
91 {
92 return ((*currVal).second);
93 }
94 std::pair<fsS32, TValue> a(*(currVal - 1));
95 std::pair<fsS32, TValue> b(*currVal);
96 TValue ret = a.second +
97 ((b.second - a.second) / (b.first - a.first)) * (pX - a.first);
98
99 return ret;
100 }
101
102 ++currVal;
103 }
104 }
105 return mValues.back().second;
106}
107
108
109#endif
TValue valueGet(fsS32 pX) const
Definition fsCGraph.h:75
void dataAdd(fsS32 pDuration, const TValue &pValue)
Definition fsCGraph.h:55
~fsCGraph()
Definition fsCGraph.h:42
fsCGraph(const TValue &pInitialValue)
Definition fsCGraph.h:37
fsS32 periodGet() const
Definition fsCGraph.h:69
void clear()
Definition fsCGraph.h:27
fsCGraph()
Definition fsCGraph.h:33
#define fsAssert(pStatement, pFmtString,...)
Definition fsAssert.h:23
int fsS32
Definition fsBaseTypes.h:20