revert most doubles back to float and truncate at 6 decimals

This commit is contained in:
Mark Vejvoda
2013-10-03 00:17:51 +00:00
parent 0eeb526e27
commit 9e60723296
22 changed files with 202 additions and 191 deletions

View File

@@ -233,7 +233,7 @@ public:
}
}
double area() {
float area() {
Vec2i v0= p[3]-p[0];
Vec2i v1= p[1]-p[2];
@@ -312,54 +312,6 @@ inline T radToDeg(T rad){
//#endif
//}
template<typename T>
inline T truncateDecimal(const T &value, int precision=6) {
/*
int iSigned = value >= 0 ? 1: -1;
#ifdef USE_STREFLOP
unsigned int uiTemp = (unsigned int)(value * streflop::pow((streflop::Simple)10, (streflop::Simple)precision)) * iSigned; //Note I'm using unsigned int so that I can increase the precision of the truncate
T result = (((T)uiTemp) / streflop::pow((streflop::Simple)10,(streflop::Simple)precision) * iSigned);
#else
unsigned int uiTemp = (value * pow((T)10, precision)) * iSigned; //Note I'm using unsigned int so that I can increase the precision of the truncate
T result = (((double)uiTemp) / pow((T)10,precision) * iSigned);
#endif
return result;
*/
T precNum = 0;
if(precision == 0) {
precNum = 1;
}
else if(precision == 1) {
precNum = 10;
}
else if(precision == 2) {
precNum = 100;
}
else if(precision == 3) {
precNum = 1000;
}
else if(precision == 4) {
precNum = 10000;
}
else if(precision == 5) {
precNum = 100000;
}
else if(precision == 6) {
precNum = 1000000;
}
else {
precNum = std::pow((T)10,(T)precision);
}
int64 resultInt = (T)value * (T)precNum;
T result = (long double)resultInt / precNum;
return result;
}
}}//end namespace

View File

@@ -24,10 +24,61 @@
//#include <tr1/unordered_map>
//using namespace std::tr1;
#include "data_types.h"
#include "leak_dumper.h"
using namespace Shared::Platform;
namespace Shared{ namespace Graphics{
template<typename T>
inline T truncateDecimal(const T &value, int precision=6) {
/*
int iSigned = value >= 0 ? 1: -1;
#ifdef USE_STREFLOP
unsigned int uiTemp = (unsigned int)(value * streflop::pow((streflop::Simple)10, (streflop::Simple)precision)) * iSigned; //Note I'm using unsigned int so that I can increase the precision of the truncate
T result = (((T)uiTemp) / streflop::pow((streflop::Simple)10,(streflop::Simple)precision) * iSigned);
#else
unsigned int uiTemp = (value * pow((T)10, precision)) * iSigned; //Note I'm using unsigned int so that I can increase the precision of the truncate
T result = (((double)uiTemp) / pow((T)10,precision) * iSigned);
#endif
return result;
*/
T precNum = 0;
if(precision == 0) {
precNum = 1;
}
else if(precision == 1) {
precNum = 10;
}
else if(precision == 2) {
precNum = 100;
}
else if(precision == 3) {
precNum = 1000;
}
else if(precision == 4) {
precNum = 10000;
}
else if(precision == 5) {
precNum = 100000;
}
else if(precision == 6) {
precNum = 1000000;
}
else {
precNum = std::pow((T)10,(T)precision);
}
int64 resultInt = (T)value * (T)precNum;
T result = (long double)resultInt / precNum;
return result;
}
inline std::vector<std::string> TokenizeString(const std::string str,const std::string delimiters) {
std::vector<std::string> tokens;
// Assume textLine contains the line of text to parse.
@@ -206,8 +257,10 @@ public:
return x*v.x+y*v.y;
}
inline double dist(const Vec2<T> &v) const{
return Vec2<T>(v-*this).length();
inline float dist(const Vec2<T> &v) const{
float distance = Vec2<T>(v-*this).length();
distance = truncateDecimal<float>(distance,6);
return distance;
}
// strict week ordering, so Vec2<T> can be used as key for set<> or map<>
@@ -215,7 +268,7 @@ public:
return x < v.x || (x == v.x && y < v.y);
}
inline double length() const {
inline float length() const {
//#ifdef USE_STREFLOP
// if(requireAccuracy == true) {
// return static_cast<float>(streflop::sqrt(static_cast<streflop::Simple>(x*x + y*y)));
@@ -224,7 +277,9 @@ public:
//#else
// return static_cast<float>(sqrt(static_cast<float>(x*x + y*y)));
//#endif
return static_cast<double>(std::sqrt(static_cast<double>(x*x + y*y)));
float len = static_cast<float>(std::sqrt(static_cast<float>(x*x + y*y)));
len = truncateDecimal<float>(len,6);
return len;
}
inline void normalize(){
@@ -233,7 +288,7 @@ public:
y/= m;
}
inline Vec2<T> rotate(double rad) {
inline Vec2<T> rotate(float rad) {
// const float
//#ifdef USE_STREFLOP
// c = streflop::cosf(rad),
@@ -242,13 +297,13 @@ public:
// c = cosf(rad),
// s = sinf(rad);
//#endif
double c = std::cos(rad),
float c = std::cos(rad),
s = std::sin(rad);
return Vec2<T>(x*c-y*s,x*s+y*c);
}
inline Vec2<T> rotateAround(double rad,const Vec2<T>& pt){
inline Vec2<T> rotateAround(float rad,const Vec2<T>& pt) {
return pt+(*this-pt).rotate(rad);
}
@@ -452,11 +507,13 @@ public:
return x*v.x + y*v.y + z*v.z;
}
inline double dist(const Vec3<T> &v) const{
return Vec3<T>(v-*this).length();
inline float dist(const Vec3<T> &v) const {
float distance = Vec3<T>(v-*this).length();
distance = truncateDecimal<float>(distance,6);
return distance;
}
inline double length() const {
inline float length() const {
//#ifdef USE_STREFLOP
// if(requireAccuracy == true) {
// return static_cast<float>(streflop::sqrt(static_cast<streflop::Simple>(x*x + y*y + z*z)));
@@ -465,7 +522,9 @@ public:
//#else
// return static_cast<float>(sqrt(x*x + y*y + z*z));
//#endif
return static_cast<double>(std::sqrt(x*x + y*y + z*z));
float len = static_cast<float>(std::sqrt(x*x + y*y + z*z));
len = truncateDecimal<float>(len,6);
return len;
}
inline void normalize() {

View File

@@ -43,7 +43,7 @@ public:
void init(int seed);
int randRange(int min, int max,std::string lastCaller="");
double randRange(double min, double max,std::string lastCaller="");
float randRange(float min, float max,std::string lastCaller="");
int getLastNumber() const { return lastNumber; }
void setLastNumber(int value) { lastNumber = value; }

View File

@@ -231,10 +231,10 @@ void copyStringToBuffer(char *buffer, int bufferSize, const string& s);
//numeric fcs
int clamp(int value, int min, int max);
double clamp(double value, double min, double max);
float clamp(float value, float min, float max);
int64 clamp(int64 value, int64 min, int64 max);
double saturate(double value);
int round(double f);
float saturate(float value);
int round(float f);
//misc
bool checkVersionComptability(string clientVersionString, string serverVersionString);

View File

@@ -109,7 +109,7 @@ void ParticleRendererGl::renderSystem(ParticleSystem *ps){
for(int i=0; i<ps->getAliveParticleCount(); ++i){
const Particle *particle= ps->getParticle(i);
float size= particle->getSize()/2.0f;
Vec3f pos= Vec3f(particle->getPos());
Vec3f pos= particle->getPos();
Vec4f color= particle->getColor();
vertexBuffer[bufferIndex] = pos - (rightVector - upVector) * size;
@@ -159,8 +159,8 @@ void ParticleRendererGl::renderSystemLine(ParticleSystem *ps){
particle= ps->getParticle(i);
Vec4f color= particle->getColor();
vertexBuffer[bufferIndex] = Vec3f(particle->getPos());
vertexBuffer[bufferIndex+1] = Vec3f(particle->getLastPos());
vertexBuffer[bufferIndex] = particle->getPos();
vertexBuffer[bufferIndex+1] = particle->getLastPos();
colorBuffer[bufferIndex]= color;
colorBuffer[bufferIndex+1]= color;
@@ -203,8 +203,8 @@ void ParticleRendererGl::renderSystemLineAlpha(ParticleSystem *ps){
particle= ps->getParticle(i);
Vec4f color= particle->getColor();
vertexBuffer[bufferIndex] = Vec3f(particle->getPos());
vertexBuffer[bufferIndex+1] = Vec3f(particle->getLastPos());
vertexBuffer[bufferIndex] = particle->getPos();
vertexBuffer[bufferIndex+1] = particle->getLastPos();
colorBuffer[bufferIndex]= color;
colorBuffer[bufferIndex+1]= color;
@@ -236,11 +236,11 @@ void ParticleRendererGl::renderModel(GameParticleSystem *ps, ModelRenderer *mr){
glPushMatrix();
//translate
Vec3f pos= Vec3f(ps->getPos());
Vec3f pos= ps->getPos();
glTranslatef(pos.x, pos.y, pos.z);
//rotate
Vec3f direction= Vec3f(ps->getDirection());
Vec3f direction= ps->getDirection();
Vec3f flatDirection= Vec3f(direction.x, 0.f, direction.z);
Vec3f rotVector= Vec3f(0.f, 1.f, 0.f).cross(flatDirection);

View File

@@ -80,8 +80,8 @@ int RandomGen::randRange(int min, int max,string lastCaller) {
}
int diff= max-min;
double numerator = static_cast<double>(diff + 1) * static_cast<double>(RandomGen::rand(lastCaller));
int res= min + static_cast<int>(truncateDecimal<double>(numerator / static_cast<double>(m),6));
float numerator = static_cast<float>(diff + 1) * static_cast<float>(RandomGen::rand(lastCaller));
int res= min + static_cast<int>(truncateDecimal<float>(numerator / static_cast<float>(m),6));
if(res < min || res > max) {
char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] res < min || res > max, min = %d, max = %d, res = %d",__FILE__,__FUNCTION__,__LINE__,min,max,res);
@@ -93,16 +93,16 @@ int RandomGen::randRange(int min, int max,string lastCaller) {
return res;
}
double RandomGen::randRange(double min, double max,string lastCaller) {
float RandomGen::randRange(float min, float max,string lastCaller) {
if(min > max) {
char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] min > max, min = %f, max = %f",__FILE__,__FUNCTION__,__LINE__,min,max);
throw megaglest_runtime_error(szBuf);
}
double rand01 = static_cast<double>(RandomGen::rand(lastCaller)) / (m-1);
double res= min + (max - min) * rand01;
res = truncateDecimal<double>(res,6);
float rand01 = static_cast<float>(RandomGen::rand(lastCaller)) / (m-1);
float res= min + (max - min) * rand01;
res = truncateDecimal<float>(res,6);
if(res < min || res > max) {
char szBuf[8096]="";

View File

@@ -710,7 +710,7 @@ void copyStringToBuffer(char *buffer, int bufferSize, const string& s){
// ==================== numeric fcs ====================
double saturate(double value) {
float saturate(float value) {
if (value < 0.f){
return 0.f;
}
@@ -740,7 +740,7 @@ int64 clamp(int64 value, int64 min, int64 max){
return value;
}
double clamp(double value, double min, double max) {
float clamp(float value, float min, float max) {
if (value < min) {
return min;
}
@@ -750,7 +750,7 @@ double clamp(double value, double min, double max) {
return value;
}
int round(double f){
int round(float f){
return (int) f;
}