Darryl Pogue
10 years ago
6 changed files with 0 additions and 692 deletions
@ -1,156 +0,0 @@ |
|||||||
/*==LICENSE==*
|
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
|
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
|
||||||
it under the terms of the GNU General Public License as published by |
|
||||||
the Free Software Foundation, either version 3 of the License, or |
|
||||||
(at your option) any later version. |
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||||
GNU General Public License for more details. |
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
Additional permissions under GNU GPL version 3 section 7 |
|
||||||
|
|
||||||
If you modify this Program, or any covered work, by linking or |
|
||||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
|
||||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
|
||||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
|
||||||
(or a modified version of those libraries), |
|
||||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
|
||||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
|
||||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
|
||||||
licensors of this Program grant you additional |
|
||||||
permission to convey the resulting work. Corresponding Source for a |
|
||||||
non-source form of such a combination shall include the source code for |
|
||||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
|
||||||
work. |
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
||||||
or by snail mail at: |
|
||||||
Cyan Worlds, Inc. |
|
||||||
14617 N Newport Hwy |
|
||||||
Mead, WA 99021 |
|
||||||
|
|
||||||
*==LICENSE==*/ |
|
||||||
#include "hsNoiseFunc.h" |
|
||||||
#include "HeadSpin.h" |
|
||||||
|
|
||||||
#include "hsGeometry3.h" |
|
||||||
|
|
||||||
hsNoiseFunc::hsNoiseFunc()
|
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
hsNoiseFunc::~hsNoiseFunc()
|
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
void hsNoiseFunc::Seed(uint32_t s) |
|
||||||
{ |
|
||||||
srand(s); |
|
||||||
} |
|
||||||
|
|
||||||
hsTableNoise::hsTableNoise() |
|
||||||
: fTable(nil), fTableLen(0) |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
hsTableNoise::~hsTableNoise() |
|
||||||
{ |
|
||||||
delete [] fTable; |
|
||||||
} |
|
||||||
|
|
||||||
void hsTableNoise::SetTable(int len, float* arr) |
|
||||||
{ |
|
||||||
fTableLen = len; |
|
||||||
|
|
||||||
delete [] fTable; |
|
||||||
if( !len ) |
|
||||||
{ |
|
||||||
fTable = nil; |
|
||||||
return; |
|
||||||
} |
|
||||||
|
|
||||||
fTable = new float[len+2]; |
|
||||||
|
|
||||||
int i; |
|
||||||
for( i = 0; i < len; i++ ) |
|
||||||
fTable[i] = arr[i]; |
|
||||||
fTable[i++] = fTable[i-1]; |
|
||||||
fTable[i++] = fTable[i-1]; |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
float hsTableNoise::Noise(float lo, float hi, float t) |
|
||||||
{ |
|
||||||
hsAssert(fTableLen, "Badly initialized table noise function"); |
|
||||||
|
|
||||||
float r = float(rand()) / float(RAND_MAX); |
|
||||||
r = lo + (hi - lo) * r; |
|
||||||
|
|
||||||
if( t < 0 ) |
|
||||||
t = 0; |
|
||||||
else if( t > 1.f ) |
|
||||||
t = 1.f; |
|
||||||
|
|
||||||
float tIdx = t * fTableLen; |
|
||||||
uint32_t idx = uint32_t(tIdx); |
|
||||||
float frac = tIdx - float(idx); |
|
||||||
hsAssert((idx >= 0)&&(idx <= fTableLen), "Noise parm t out of range [0..1]"); |
|
||||||
|
|
||||||
float scale = fTable[idx] + (fTable[idx+1] - fTable[idx]) * frac; |
|
||||||
|
|
||||||
r *= scale; |
|
||||||
|
|
||||||
return r; |
|
||||||
} |
|
||||||
|
|
||||||
float hsTableNoise::NoisePoint(const hsPoint3& p, float lo, float hi, float t) |
|
||||||
{ |
|
||||||
hsAssert(fTableLen, "Badly initialized table noise function"); |
|
||||||
|
|
||||||
uint32_t sX = *((uint32_t*)&p.fX); |
|
||||||
uint32_t sY = *((uint32_t*)&p.fY); |
|
||||||
uint32_t sZ = *((uint32_t*)&p.fZ); |
|
||||||
|
|
||||||
uint32_t sAll = ((((sX & 0x07800000) >> 16) | ((sX & 0x007fffff) >> 17)) << 20) |
|
||||||
| ((((sY & 0x07800000) >> 16) | ((sY & 0x007fffff) >> 17)) << 10) |
|
||||||
| ((((sZ & 0x07800000) >> 16) | ((sZ & 0x007fffff) >> 17)) ); |
|
||||||
|
|
||||||
const uint32_t kExp = 0x3f800000; |
|
||||||
const uint32_t kMsk = 0x007fffff; |
|
||||||
|
|
||||||
const uint32_t kA = 1665636L; |
|
||||||
const uint32_t kC = 1013904223L; |
|
||||||
|
|
||||||
uint32_t iR = kA * sAll + kC; |
|
||||||
iR &= kMsk; |
|
||||||
iR |= kExp; |
|
||||||
|
|
||||||
float r = (*(float*)&iR) - 1.f; |
|
||||||
|
|
||||||
r = lo + (hi - lo) * r; |
|
||||||
|
|
||||||
if( t < 0 ) |
|
||||||
t = 0; |
|
||||||
else if( t > 1.f ) |
|
||||||
t = 1.f; |
|
||||||
|
|
||||||
float tIdx = t * fTableLen; |
|
||||||
uint32_t idx = uint32_t(tIdx); |
|
||||||
float frac = tIdx - float(idx); |
|
||||||
hsAssert((idx >= 0)&&(idx <= fTableLen), "Noise parm t out of range [0..1]"); |
|
||||||
|
|
||||||
float scale = fTable[idx] + (fTable[idx+1] - fTable[idx]) * frac; |
|
||||||
|
|
||||||
r *= scale; |
|
||||||
|
|
||||||
return r; |
|
||||||
} |
|
@ -1,82 +0,0 @@ |
|||||||
/*==LICENSE==*
|
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
|
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
|
||||||
it under the terms of the GNU General Public License as published by |
|
||||||
the Free Software Foundation, either version 3 of the License, or |
|
||||||
(at your option) any later version. |
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||||
GNU General Public License for more details. |
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
Additional permissions under GNU GPL version 3 section 7 |
|
||||||
|
|
||||||
If you modify this Program, or any covered work, by linking or |
|
||||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
|
||||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
|
||||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
|
||||||
(or a modified version of those libraries), |
|
||||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
|
||||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
|
||||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
|
||||||
licensors of this Program grant you additional |
|
||||||
permission to convey the resulting work. Corresponding Source for a |
|
||||||
non-source form of such a combination shall include the source code for |
|
||||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
|
||||||
work. |
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
||||||
or by snail mail at: |
|
||||||
Cyan Worlds, Inc. |
|
||||||
14617 N Newport Hwy |
|
||||||
Mead, WA 99021 |
|
||||||
|
|
||||||
*==LICENSE==*/ |
|
||||||
|
|
||||||
#ifndef hsNoiseFunc_inc |
|
||||||
#define hsNoiseFunc_inc |
|
||||||
|
|
||||||
#include "HeadSpin.h" |
|
||||||
#include "hsRefCnt.h" |
|
||||||
|
|
||||||
struct hsPoint3; |
|
||||||
|
|
||||||
class hsNoiseFunc : public hsRefCnt // should inherit from keyed object
|
|
||||||
{ |
|
||||||
public: |
|
||||||
hsNoiseFunc(); |
|
||||||
virtual ~hsNoiseFunc(); |
|
||||||
|
|
||||||
virtual void Seed(uint32_t s); |
|
||||||
virtual float Noise(float lo=0, float hi=1.f, float t=0) = 0; // t = [0..1] - returns random num [lo..hi] scaled by fTable[t]
|
|
||||||
|
|
||||||
virtual float NoisePoint(const hsPoint3& p, float lo=0, float hi=1.f, float t=0) = 0; // t = [0..1] - returns random num [lo..hi] scaled by fTable[t]
|
|
||||||
}; |
|
||||||
|
|
||||||
class hsTableNoise : public hsNoiseFunc // should inherit from keyed object
|
|
||||||
{ |
|
||||||
protected: |
|
||||||
float* fTable; |
|
||||||
uint32_t fTableLen; |
|
||||||
|
|
||||||
|
|
||||||
public: |
|
||||||
hsTableNoise(); |
|
||||||
virtual ~hsTableNoise(); |
|
||||||
|
|
||||||
void SetTable(int len, float* arr); // copies. arr should be hsScalars in range [0..1]
|
|
||||||
float* GetTable(int& len) { len = fTableLen; return fTable; } // should be debug only, access through noise func
|
|
||||||
|
|
||||||
virtual float Noise(float lo=0, float hi=1.f, float t=0); // t = [0..1] - returns random num [lo..hi] scaled by fTable[t]
|
|
||||||
|
|
||||||
virtual float NoisePoint(const hsPoint3& p, float lo=0, float hi=1.f, float t=0); // t = [0..1] - returns random num [lo..hi] scaled by fTable[t]
|
|
||||||
}; |
|
||||||
|
|
||||||
#endif // hsNoiseFunc_inc
|
|
@ -1,181 +0,0 @@ |
|||||||
/*==LICENSE==*
|
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
|
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
|
||||||
it under the terms of the GNU General Public License as published by |
|
||||||
the Free Software Foundation, either version 3 of the License, or |
|
||||||
(at your option) any later version. |
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||||
GNU General Public License for more details. |
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
Additional permissions under GNU GPL version 3 section 7 |
|
||||||
|
|
||||||
If you modify this Program, or any covered work, by linking or |
|
||||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
|
||||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
|
||||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
|
||||||
(or a modified version of those libraries), |
|
||||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
|
||||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
|
||||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
|
||||||
licensors of this Program grant you additional |
|
||||||
permission to convey the resulting work. Corresponding Source for a |
|
||||||
non-source form of such a combination shall include the source code for |
|
||||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
|
||||||
work. |
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
||||||
or by snail mail at: |
|
||||||
Cyan Worlds, Inc. |
|
||||||
14617 N Newport Hwy |
|
||||||
Mead, WA 99021 |
|
||||||
|
|
||||||
*==LICENSE==*/ |
|
||||||
|
|
||||||
#ifndef hsSearchVersion_inc |
|
||||||
#define hsSearchVersion_inc |
|
||||||
|
|
||||||
#include "HeadSpin.h" |
|
||||||
|
|
||||||
/*
|
|
||||||
do a template of lists to search for a matching entry. basic idea is that |
|
||||||
you start off with an array of buckets and you know that you will get at |
|
||||||
least one, possibly several, in each bucket. when you go to search, you already |
|
||||||
know which bucket it will be in if it's there. even as the array is being filled, |
|
||||||
each filled entry in each bucket has a valid forever index, as well as it's key |
|
||||||
value. so array is fixed length, index into array has no bearing on forever index, |
|
||||||
elements of array can grow, and at all times the used forever indices form a contiguous |
|
||||||
set from 0 to max forever index. |
|
||||||
*/ |
|
||||||
|
|
||||||
template <class T> class hsVersionNode { |
|
||||||
protected: |
|
||||||
T fData; |
|
||||||
int32_t fIndex; |
|
||||||
hsVersionNode<T>* fNext; |
|
||||||
public: |
|
||||||
hsVersionNode(const uint32_t idx, const T &data) : fIndex(idx), fNext(nil) { fData = data; } |
|
||||||
~hsVersionNode() { delete fNext; } |
|
||||||
|
|
||||||
hsVersionNode<T>* Next() const { return fNext; } |
|
||||||
|
|
||||||
int32_t Index() const { return fIndex; } |
|
||||||
|
|
||||||
inline void Append(hsVersionNode<T>* next); |
|
||||||
inline int operator==(const T& o) const; |
|
||||||
|
|
||||||
int operator!=(const T& o) const { return !(this->operator==(o)); } |
|
||||||
|
|
||||||
T& GetData() { return fData; } |
|
||||||
}; |
|
||||||
|
|
||||||
template <class T> int hsVersionNode<T>::operator==(const T& data) const |
|
||||||
{ |
|
||||||
return fData == data; |
|
||||||
} |
|
||||||
|
|
||||||
template <class T> void hsVersionNode<T>::Append(hsVersionNode<T>* next) |
|
||||||
{ |
|
||||||
if( fNext ) |
|
||||||
fNext->Append(next); |
|
||||||
else |
|
||||||
fNext = next; |
|
||||||
} |
|
||||||
|
|
||||||
template <class T> class hsSearchVersion { |
|
||||||
protected: |
|
||||||
uint32_t fLength; |
|
||||||
hsVersionNode<T>** fArray; |
|
||||||
uint32_t fNextIndex; |
|
||||||
uint32_t fNumIndex; |
|
||||||
uint32_t fIncIndex; |
|
||||||
T** fBackArray; |
|
||||||
|
|
||||||
void ICheckBackArray(); |
|
||||||
public: |
|
||||||
hsSearchVersion(uint32_t len, uint32_t inc = 0); |
|
||||||
~hsSearchVersion(); |
|
||||||
|
|
||||||
T& operator[]( int32_t index ); |
|
||||||
|
|
||||||
int32_t Find(int where, const T& what, bool forceUnique=false); |
|
||||||
|
|
||||||
uint32_t GetCount() const { return fNextIndex; } |
|
||||||
}; |
|
||||||
|
|
||||||
template <class T> T& hsSearchVersion<T>::operator[]( int32_t index ) |
|
||||||
{ |
|
||||||
hsDebugCode(hsThrowIfBadParam((uint32_t)index >= (uint32_t)fNextIndex);) |
|
||||||
|
|
||||||
return *fBackArray[index]; |
|
||||||
} |
|
||||||
|
|
||||||
template <class T> hsSearchVersion<T>::hsSearchVersion(uint32_t len, uint32_t inc) |
|
||||||
: fNextIndex(0) |
|
||||||
{
|
|
||||||
fIncIndex = inc ? inc : len; |
|
||||||
fLength = len;
|
|
||||||
fArray = new hsVersionNode<T>*[fLength];
|
|
||||||
HSMemory::Clear(fArray, fLength*sizeof(*fArray));
|
|
||||||
fBackArray = new T*[fNumIndex = fLength]; |
|
||||||
} |
|
||||||
|
|
||||||
template <class T> hsSearchVersion<T>::~hsSearchVersion() |
|
||||||
{ |
|
||||||
int i; |
|
||||||
for( i = 0; i < fLength; i++ ) |
|
||||||
delete fArray[i]; |
|
||||||
delete [] fArray; |
|
||||||
delete [] fBackArray; |
|
||||||
} |
|
||||||
|
|
||||||
template <class T> void hsSearchVersion<T>::ICheckBackArray() |
|
||||||
{ |
|
||||||
if( fNextIndex >= fNumIndex ) |
|
||||||
{ |
|
||||||
T** newBackArray = new T*[fNumIndex + fIncIndex]; |
|
||||||
HSMemory::BlockMove(fBackArray, newBackArray, fNextIndex*sizeof(T*)); |
|
||||||
delete [] fBackArray; |
|
||||||
fBackArray = newBackArray; |
|
||||||
fNumIndex += fIncIndex; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
template <class T> int32_t hsSearchVersion<T>::Find(int where, const T&what, bool forceUnique) |
|
||||||
{ |
|
||||||
hsVersionNode<T>* curr = fArray[where]; |
|
||||||
|
|
||||||
ICheckBackArray(); |
|
||||||
|
|
||||||
if( !curr ) |
|
||||||
{ |
|
||||||
hsVersionNode<T>* next = new hsVersionNode<T>(fNextIndex, what); |
|
||||||
fArray[where] = next; |
|
||||||
fBackArray[fNextIndex] = &next->GetData(); |
|
||||||
return fNextIndex++; |
|
||||||
} |
|
||||||
if( *curr == what ) |
|
||||||
return curr->Index(); |
|
||||||
|
|
||||||
while( curr->Next()
|
|
||||||
&& (forceUnique || (*curr->Next() != what)) ) |
|
||||||
curr = curr->Next(); |
|
||||||
|
|
||||||
if( curr->Next() ) |
|
||||||
return curr->Next()->Index(); |
|
||||||
|
|
||||||
hsVersionNode<T>* next = new hsVersionNode<T>(fNextIndex, what); |
|
||||||
curr->Append(next); |
|
||||||
fBackArray[fNextIndex] = &next->GetData(); |
|
||||||
return fNextIndex++; |
|
||||||
} |
|
||||||
|
|
||||||
#endif // hsSearchVersion_inc
|
|
@ -1,160 +0,0 @@ |
|||||||
/*==LICENSE==*
|
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
|
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
|
||||||
it under the terms of the GNU General Public License as published by |
|
||||||
the Free Software Foundation, either version 3 of the License, or |
|
||||||
(at your option) any later version. |
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||||
GNU General Public License for more details. |
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
Additional permissions under GNU GPL version 3 section 7 |
|
||||||
|
|
||||||
If you modify this Program, or any covered work, by linking or |
|
||||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
|
||||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
|
||||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
|
||||||
(or a modified version of those libraries), |
|
||||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
|
||||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
|
||||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
|
||||||
licensors of this Program grant you additional |
|
||||||
permission to convey the resulting work. Corresponding Source for a |
|
||||||
non-source form of such a combination shall include the source code for |
|
||||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
|
||||||
work. |
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
||||||
or by snail mail at: |
|
||||||
Cyan Worlds, Inc. |
|
||||||
14617 N Newport Hwy |
|
||||||
Mead, WA 99021 |
|
||||||
|
|
||||||
*==LICENSE==*/ |
|
||||||
|
|
||||||
#include "HeadSpin.h" |
|
||||||
#include "plAvg.h" |
|
||||||
#include <cmath> |
|
||||||
#include <algorithm> |
|
||||||
|
|
||||||
template class TimeBasedAvgRing<float>; |
|
||||||
template class TimeBasedAvgRing<double>; |
|
||||||
|
|
||||||
#define PercisionRoundUp(x) (ceil(x / kPercision) * kPercision) |
|
||||||
|
|
||||||
template <class T> |
|
||||||
const float TimeBasedAvgRing<T>::kPercision = 0.001; |
|
||||||
|
|
||||||
template <class T> |
|
||||||
void TimeBasedAvgRing<T>::AddItem(T value, double time) |
|
||||||
{ |
|
||||||
std::lock_guard<std::mutex> lock(fLock); |
|
||||||
|
|
||||||
if ( fList.empty() ) |
|
||||||
{ |
|
||||||
// initialize with the first time and zero the first value
|
|
||||||
fList.insert(fList.end(),Item<T>(0.0,time)); |
|
||||||
fRingStart = fRingEnd = fList.begin(); |
|
||||||
fAvg = (float)value; |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
// if we're within the percision amount subtract the RingEnd value from total
|
|
||||||
// and update the RingEnd value by adding the current value to it
|
|
||||||
if (time - (*fRingEnd).GetTime() <= kPercision) |
|
||||||
{ |
|
||||||
fTotal -= PercisionRoundUp((*fRingEnd).GetValue()); |
|
||||||
(*fRingEnd).SetValue((*fRingEnd).GetValue() + value); |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
// clean up the begining of the ring
|
|
||||||
//// there can be some precision loss in the loop time calc
|
|
||||||
//// check to see if the difference is within 1 milli
|
|
||||||
while (time - (*fRingStart).GetTime() > fLen + kPercision |
|
||||||
&& fRingStart != fRingEnd) |
|
||||||
{ |
|
||||||
// remove RingStart from the avg part of the average calc
|
|
||||||
fTotal -= (*fRingStart).GetValue(); |
|
||||||
|
|
||||||
typename TimeList::iterator prev = fRingStart++; |
|
||||||
|
|
||||||
// loop the ring if needed
|
|
||||||
if (fRingStart == fList.end()) |
|
||||||
fRingStart = fList.begin(); |
|
||||||
|
|
||||||
|
|
||||||
// if the new ring start is in the range, interpolate
|
|
||||||
// and reuse prev
|
|
||||||
if (time - (*fRingStart).GetTime() < fLen) |
|
||||||
{ |
|
||||||
// remove RingStart from the avg part of the average calc
|
|
||||||
fTotal -= PercisionRoundUp((*fRingStart).GetValue()); |
|
||||||
|
|
||||||
// Set up the interp
|
|
||||||
double remainder = fLen - (time - (*fRingStart).GetTime()); |
|
||||||
double timedelta = (*fRingStart).GetTime() - (*prev).GetTime(); |
|
||||||
(*prev).SetTime((*fRingStart).GetTime() - remainder); |
|
||||||
(*prev).SetValue(0); |
|
||||||
// rounding loss occurs here if T is not floting point
|
|
||||||
double scale = remainder/timedelta; |
|
||||||
hsAssert(scale < 1.0 && scale > 0.0,"Interp Scale Out of Bounds"); |
|
||||||
(*fRingStart).SetValue((float)((*fRingStart).GetValue() * scale)); |
|
||||||
|
|
||||||
// add the new value in
|
|
||||||
fTotal += (*fRingStart).GetValue(); |
|
||||||
|
|
||||||
// put prev back as ring start
|
|
||||||
fRingStart = prev; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
// zero total & fAvg if we looped or neg
|
|
||||||
if (fRingStart == fRingEnd || fTotal < 0.0) |
|
||||||
{ |
|
||||||
fTotal = 0.0; |
|
||||||
fAvg = 0.0; |
|
||||||
} |
|
||||||
|
|
||||||
// put the new value in the ring by expanding the ring if needed
|
|
||||||
// or replacing an empty value
|
|
||||||
fRingEnd++; |
|
||||||
if (fRingEnd == fList.end()) |
|
||||||
fRingEnd = fList.begin(); |
|
||||||
// Do we have free space?
|
|
||||||
if (fRingEnd == fRingStart) |
|
||||||
{ |
|
||||||
// no free space
|
|
||||||
fList.insert(fRingEnd,Item<T>(value,time)); |
|
||||||
fRingEnd--; |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
// yes free space @ fRingEnd
|
|
||||||
(*fRingEnd) = Item<T>(value,time); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
//update the avg
|
|
||||||
fTotal += (*fRingEnd).GetValue(); |
|
||||||
double currentLen = (*fRingEnd).GetTime() - (*fRingStart).GetTime(); |
|
||||||
if (currentLen < 1.0) |
|
||||||
fAvg = (float)fTotal; |
|
||||||
else |
|
||||||
fAvg = (float)(fTotal / currentLen); |
|
||||||
} |
|
||||||
|
|
||||||
// update the max avg
|
|
||||||
fMaxAvg = std::max(fMaxAvg, fAvg); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
@ -1,108 +0,0 @@ |
|||||||
/*==LICENSE==*
|
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
|
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
|
||||||
it under the terms of the GNU General Public License as published by |
|
||||||
the Free Software Foundation, either version 3 of the License, or |
|
||||||
(at your option) any later version. |
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||||
GNU General Public License for more details. |
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
Additional permissions under GNU GPL version 3 section 7 |
|
||||||
|
|
||||||
If you modify this Program, or any covered work, by linking or |
|
||||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
|
||||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
|
||||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
|
||||||
(or a modified version of those libraries), |
|
||||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
|
||||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
|
||||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
|
||||||
licensors of this Program grant you additional |
|
||||||
permission to convey the resulting work. Corresponding Source for a |
|
||||||
non-source form of such a combination shall include the source code for |
|
||||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
|
||||||
work. |
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
||||||
or by snail mail at: |
|
||||||
Cyan Worlds, Inc. |
|
||||||
14617 N Newport Hwy |
|
||||||
Mead, WA 99021 |
|
||||||
|
|
||||||
*==LICENSE==*/ |
|
||||||
#ifndef PL_AVG_H |
|
||||||
#define PL_AVG_H |
|
||||||
|
|
||||||
#include "HeadSpin.h" |
|
||||||
#include <list> |
|
||||||
#include <mutex> |
|
||||||
|
|
||||||
// A Time based Value Averaging class
|
|
||||||
// implemented in a ring buffer
|
|
||||||
// Values are averaged over the RingLen
|
|
||||||
// independant of frame time
|
|
||||||
// The ring buffer will grow to accomadate
|
|
||||||
// as many samples as added during the Len
|
|
||||||
// Only Accurate to kPercision (0.001) of whatever units
|
|
||||||
// of time are used
|
|
||||||
|
|
||||||
template <class T> class TimeBasedAvgRing |
|
||||||
{ |
|
||||||
/* T must be a simple type
|
|
||||||
|
|
||||||
If T is not a floating point type then rounding |
|
||||||
loss may occur when samples are interpolated |
|
||||||
at window boundries |
|
||||||
*/ |
|
||||||
private: |
|
||||||
static const float kPercision; |
|
||||||
|
|
||||||
template <class S> class Item |
|
||||||
{ |
|
||||||
private: |
|
||||||
T fValue; |
|
||||||
double fTime; |
|
||||||
public: |
|
||||||
Item() { Reset(); } |
|
||||||
Item(const T val, const double time) : fValue(val), fTime(time) { } |
|
||||||
void Reset() { fValue = 0; fTime = 0; } |
|
||||||
void SetValue(const T val) { fValue = val; } |
|
||||||
void SetTime(const double time) { fTime = time; } |
|
||||||
T GetValue() const { return fValue; } |
|
||||||
double GetTime() const { return fTime; } |
|
||||||
}; |
|
||||||
typedef std::list< Item<T> > TimeList; |
|
||||||
typedef typename TimeList::iterator TimeListIterator; // .NET added typename to be C++ ISO compliant - JL
|
|
||||||
|
|
||||||
TimeList fList; |
|
||||||
float fLen; // in time
|
|
||||||
float fAvg; |
|
||||||
float fMaxAvg; |
|
||||||
double fTotal; |
|
||||||
TimeListIterator fRingStart, fRingEnd; |
|
||||||
std::mutex fLock; |
|
||||||
public: |
|
||||||
TimeBasedAvgRing():fLen(0.f),fAvg(0.f),fMaxAvg(0.f),fTotal(0.0) {} |
|
||||||
|
|
||||||
void SetRingLen(const float len) { fLen = len; } |
|
||||||
float GetRingLen() const { return fLen; } |
|
||||||
void AddItem(T value, double time); |
|
||||||
float GetAvg() const { return fAvg; } |
|
||||||
double GetTotal() const { return fTotal; } |
|
||||||
float GetMaxAvg() const { return fMaxAvg; } |
|
||||||
void ResetMaxAvg() { fMaxAvg=fAvg; } |
|
||||||
void Reset() { fRingStart=fRingEnd; ResetMaxAvg(); } |
|
||||||
}; |
|
||||||
|
|
||||||
|
|
||||||
#endif // PL_AVG_H
|
|
||||||
|
|
Loading…
Reference in new issue