Refactorization: Use proper <cmath> functions, such as std::hypot() and std::log2() where appropriate. Added missing std:: prefix to some more <cmath> functions.
git-svn-id: https://svn.code.sf.net/p/extremetuxracer/code/trunk@603 0420edf4-82e4-42fc-9478-35b55e6d67a3master
parent
78729153f1
commit
ca62e16ae8
|
@ -24,10 +24,6 @@ GNU General Public License for more details.
|
|||
|
||||
#define clamp(minimum, x, maximum) (std::max(std::min(x, maximum), minimum))
|
||||
|
||||
#ifndef ROUND_TO_NEAREST
|
||||
# define ROUND_TO_NEAREST(x) ((int) ((x)+0.5))
|
||||
#endif
|
||||
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.1415926535
|
||||
#endif
|
||||
|
|
|
@ -419,12 +419,12 @@ static int GetObject(const unsigned char* pixel) {
|
|||
int b = pixel[2];
|
||||
|
||||
if (r<150 && b>200) return 0;
|
||||
if (abs(r-194)<10 && abs(g-40)<10 && abs(b-40)<10) return 1;
|
||||
if (abs(r-128)<10 && abs(g-128)<10 && b<10) return 2;
|
||||
if (std::abs(r-194)<10 && std::abs(g-40)<10 && std::abs(b-40)<10) return 1;
|
||||
if (std::abs(r-128)<10 && std::abs(g-128)<10 && b<10) return 2;
|
||||
if (r>220 && g>220 && b<20) return 3;
|
||||
if (r>220 && abs(g-128)<10 && b>220) return 4;
|
||||
if (r>220 && std::abs(g-128)<10 && b>220) return 4;
|
||||
if (r>220 && g>220 && b>220) return 5;
|
||||
if (r>220 && abs(g-96)<10 && b<40) return 6;
|
||||
if (r>220 && std::abs(g-96)<10 && b<40) return 6;
|
||||
if (r<40 && g >220 && b<80) return 7;
|
||||
return -1;
|
||||
}
|
||||
|
@ -573,9 +573,9 @@ bool CCourse::LoadObjectTypes() {
|
|||
|
||||
int CCourse::GetTerrain(const unsigned char* pixel) const {
|
||||
for (std::size_t i=0; i<TerrList.size(); i++) {
|
||||
if (abs(pixel[0]-TerrList[i].col.r) < 30
|
||||
&& abs(pixel[1]-TerrList[i].col.g) < 30
|
||||
&& abs(pixel[2]-TerrList[i].col.b) < 30) {
|
||||
if (std::abs(pixel[0]-TerrList[i].col.r) < 30
|
||||
&& std::abs(pixel[1]-TerrList[i].col.g) < 30
|
||||
&& std::abs(pixel[2]-TerrList[i].col.b) < 30) {
|
||||
return (int)i;
|
||||
}
|
||||
}
|
||||
|
@ -881,10 +881,10 @@ void CCourse::GetIndicesForPoint(double x, double z, unsigned int* x0, unsigned
|
|||
if (yidx < 0) yidx = 0;
|
||||
else if (yidx > ny-1) yidx = ny-1;
|
||||
|
||||
*x0 = (unsigned int)(xidx); // floor(xidx)
|
||||
*x1 = (unsigned int)(xidx + 0.9999); // ceil(xidx)
|
||||
*y0 = (unsigned int)(yidx); // floor(yidx)
|
||||
*y1 = (unsigned int)(yidx + 0.9999); // ceil(yidx)
|
||||
*x0 = (unsigned int)std::floor(xidx);
|
||||
*x1 = (unsigned int)std::ceil(xidx);
|
||||
*y0 = (unsigned int)std::floor(yidx);
|
||||
*y1 = (unsigned int)std::ceil(yidx);
|
||||
|
||||
if (*x0 == *x1) {
|
||||
if (*x1 < nx - 1)(*x1)++;
|
||||
|
|
|
@ -276,7 +276,7 @@ void CKeyframe::Update(float timestep) {
|
|||
TVector3d pos;
|
||||
CCharShape *shape = g_game.character->shape;
|
||||
|
||||
if (fabs(frames[keyidx].val[0]) < 0.0001) frac = 1.0;
|
||||
if (std::fabs(frames[keyidx].val[0]) < 0.0001) frac = 1.0;
|
||||
else frac = (frames[keyidx].val[0] - keytime) / frames[keyidx].val[0];
|
||||
|
||||
pos.x = interp(frac, frames[keyidx].val[1], frames[keyidx+1].val[1]) + refpos.x;
|
||||
|
@ -311,7 +311,7 @@ void CKeyframe::UpdateTest(float timestep, CCharShape *shape) {
|
|||
double frac;
|
||||
TVector3d pos;
|
||||
|
||||
if (fabs(frames[keyidx].val[0]) < 0.0001) frac = 1.0;
|
||||
if (std::fabs(frames[keyidx].val[0]) < 0.0001) frac = 1.0;
|
||||
else frac = (frames[keyidx].val[0] - keytime) / frames[keyidx].val[0];
|
||||
|
||||
pos.x = interp(frac, frames[keyidx].val[1], frames[keyidx+1].val[1]) + refpos.x;
|
||||
|
|
|
@ -105,7 +105,7 @@ TMatrix<4, 4> RotateAboutVectorMatrix(const TVector3d& u, double angle) {
|
|||
double b = u.y;
|
||||
double c = u.z;
|
||||
|
||||
double d = std::sqrt(b*b + c*c);
|
||||
double d = std::hypot(b, c);
|
||||
|
||||
if (d < EPS) {
|
||||
if (a < 0)
|
||||
|
@ -314,11 +314,11 @@ bool order(double *matrix, int n, int pivot) {
|
|||
int rmax = pivot;
|
||||
|
||||
for (int row=pivot+1; row<n; row++) {
|
||||
if (fabs(*(matrix+row*(n+1)+pivot)) > fabs(*(matrix+rmax*(n+1)+pivot)))
|
||||
if (std::fabs(*(matrix+row*(n+1)+pivot)) > std::fabs(*(matrix+rmax*(n+1)+pivot)))
|
||||
rmax = row;
|
||||
}
|
||||
|
||||
if (fabs(*(matrix+rmax*(n+1)+pivot)) < EPS)
|
||||
if (std::fabs(*(matrix+rmax*(n+1)+pivot)) < EPS)
|
||||
error = true;
|
||||
else if (rmax != pivot) {
|
||||
for (int k=0; k<(n+1); k++) {
|
||||
|
@ -365,12 +365,12 @@ bool IntersectPolygon(const TPolygon& p, std::vector<TVector3d>& v) {
|
|||
ray.vec = nml;
|
||||
|
||||
nuDotProd = DotProduct(nml, ray.vec);
|
||||
if (fabs(nuDotProd) < EPS)
|
||||
if (std::fabs(nuDotProd) < EPS)
|
||||
return false;
|
||||
|
||||
d = - DotProduct(nml, v[p.vertices[0]]);
|
||||
|
||||
if (fabs(d) > 1) return false;
|
||||
if (std::fabs(d) > 1) return false;
|
||||
|
||||
for (std::size_t i=0; i < p.vertices.size(); i++) {
|
||||
TVector3d *v0, *v1;
|
||||
|
@ -483,7 +483,7 @@ double ode23_EstimateError(TOdeData *data) {
|
|||
|
||||
for (int i=0; i<4; i++)
|
||||
err += ode23_error_mat[i] * data->k[i];
|
||||
return fabs(err);
|
||||
return std::fabs(err);
|
||||
}
|
||||
|
||||
double ode23_TimestepExponent() {
|
||||
|
|
|
@ -406,14 +406,14 @@ void generate_particles(const CControl *ctrl, double dtime, const TVector3d& pos
|
|||
* std::min(speed / PARTICLE_SPEED_FACTOR, 1.0);
|
||||
|
||||
double left_particles = turn_particles *
|
||||
fabs(std::min(ctrl->turn_fact, 0.)) +
|
||||
std::fabs(std::min(ctrl->turn_fact, 0.)) +
|
||||
brake_particles +
|
||||
roll_particles * fabs(std::min(ctrl->turn_animation, 0.));
|
||||
roll_particles * std::fabs(std::min(ctrl->turn_animation, 0.));
|
||||
|
||||
double right_particles = turn_particles *
|
||||
fabs(std::max(ctrl->turn_fact, 0.)) +
|
||||
std::fabs(std::max(ctrl->turn_fact, 0.)) +
|
||||
brake_particles +
|
||||
roll_particles * fabs(std::max(ctrl->turn_animation, 0.));
|
||||
roll_particles * std::fabs(std::max(ctrl->turn_animation, 0.));
|
||||
|
||||
left_particles = adjust_particle_count(left_particles);
|
||||
right_particles = adjust_particle_count(right_particles);
|
||||
|
|
|
@ -275,7 +275,7 @@ TVector3d CControl::CalcAirForce() {
|
|||
double windspeed = windvec.Length();
|
||||
double re = 34600 * windspeed;
|
||||
int tablesize = sizeof(airdrag) / sizeof(airdrag[0]);
|
||||
double interpol = LinearInterp(airlog, airdrag, log10(re), tablesize);
|
||||
double interpol = LinearInterp(airlog, airdrag, std::log10(re), tablesize);
|
||||
double dragcoeff = std::pow(10.0, interpol);
|
||||
double airfact = 0.104 * dragcoeff * windspeed;
|
||||
return airfact * windvec;
|
||||
|
@ -326,9 +326,9 @@ TVector3d CControl::CalcFrictionForce(double speed, const TVector3d& nmlforce) {
|
|||
|
||||
double steer_angle = turn_fact * MAX_TURN_ANGLE;
|
||||
|
||||
if (fabs(fric_f_mag * std::sin(steer_angle * M_PI / 180)) > MAX_TURN_PERP) {
|
||||
if (std::fabs(fric_f_mag * std::sin(steer_angle * M_PI / 180)) > MAX_TURN_PERP) {
|
||||
steer_angle = RADIANS_TO_ANGLES(std::asin(MAX_TURN_PERP / fric_f_mag)) *
|
||||
turn_fact / fabs(turn_fact);
|
||||
turn_fact / std::fabs(turn_fact);
|
||||
}
|
||||
TMatrix<4, 4> fric_rot_mat = RotateAboutVectorMatrix(ff.surfnml, steer_angle);
|
||||
frictforce = TransformVector(fric_rot_mat, frictforce);
|
||||
|
|
|
@ -86,8 +86,8 @@ quadsquare::quadsquare(quadcornerdata* pcd) {
|
|||
|
||||
for (int i = 0; i < 2; i++) Error[i] = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Error[i+2] = fabs((Vertex[0].Y + pcd->Verts[i].Y)
|
||||
- (Vertex[i+1].Y + Vertex[((i+1)&3) + 1].Y)) * 0.25f;
|
||||
Error[i+2] = std::fabs((Vertex[0].Y + pcd->Verts[i].Y)
|
||||
- (Vertex[i+1].Y + Vertex[((i+1)&3) + 1].Y)) * 0.25f;
|
||||
}
|
||||
|
||||
MinY = MaxY = pcd->Verts[0].Y;
|
||||
|
@ -127,8 +127,8 @@ float quadsquare::GetHeight(const quadcornerdata &cd, float x, float z) {
|
|||
float lx = (x - cd.xorg) / float(half);
|
||||
float lz = (z - cd.zorg) / float(half);
|
||||
|
||||
int ix = (int) floor(lx);
|
||||
int iz = (int) floor(lz);
|
||||
int ix = (int)std::floor(lx);
|
||||
int iz = (int)std::floor(lz);
|
||||
|
||||
if (ix < 0) ix = 0;
|
||||
if (ix > 1) ix = 1;
|
||||
|
@ -208,9 +208,9 @@ float quadsquare::RecomputeError(const quadcornerdata& cd) {
|
|||
|
||||
std::size_t numTerr = Course.TerrList.size();
|
||||
if (cd.ChildIndex & 1) {
|
||||
e = fabs(Vertex[0].Y - (cd.Verts[1].Y + cd.Verts[3].Y) * 0.5f);
|
||||
e = std::fabs(Vertex[0].Y - (cd.Verts[1].Y + cd.Verts[3].Y) * 0.5f);
|
||||
} else {
|
||||
e = fabs(Vertex[0].Y - (cd.Verts[0].Y + cd.Verts[2].Y) * 0.5f);
|
||||
e = std::fabs(Vertex[0].Y - (cd.Verts[0].Y + cd.Verts[2].Y) * 0.5f);
|
||||
}
|
||||
if (e > maxerror) maxerror = e;
|
||||
|
||||
|
@ -224,11 +224,11 @@ float quadsquare::RecomputeError(const quadcornerdata& cd) {
|
|||
}
|
||||
|
||||
|
||||
e = fabs(Vertex[1].Y - (cd.Verts[0].Y + cd.Verts[3].Y) * 0.5f);
|
||||
e = std::fabs(Vertex[1].Y - (cd.Verts[0].Y + cd.Verts[3].Y) * 0.5f);
|
||||
if (e > maxerror) maxerror = e;
|
||||
Error[0] = e;
|
||||
|
||||
e = fabs(Vertex[4].Y - (cd.Verts[2].Y + cd.Verts[3].Y) * 0.5f);
|
||||
e = std::fabs(Vertex[4].Y - (cd.Verts[2].Y + cd.Verts[3].Y) * 0.5f);
|
||||
if (e > maxerror) maxerror = e;
|
||||
Error[1] = e;
|
||||
|
||||
|
@ -321,8 +321,8 @@ float quadsquare::RecomputeError(const quadcornerdata& cd) {
|
|||
if (Child[i]->MinY < MinY) MinY = Child[i]->MinY;
|
||||
if (Child[i]->MaxY > MaxY) MaxY = Child[i]->MaxY;
|
||||
} else {
|
||||
Error[i+2] = fabs((Vertex[0].Y + cd.Verts[i].Y)
|
||||
- (Vertex[i+1].Y + Vertex[((i+1)&3) + 1].Y)) * 0.25f;
|
||||
Error[i+2] = std::fabs((Vertex[0].Y + cd.Verts[i].Y)
|
||||
- (Vertex[i+1].Y + Vertex[((i+1)&3) + 1].Y)) * 0.25f;
|
||||
}
|
||||
if (Error[i+2] > maxerror) maxerror = Error[i+2];
|
||||
}
|
||||
|
@ -451,8 +451,8 @@ void quadsquare::StaticCullAux(const quadcornerdata& cd, float ThresholdDetail,
|
|||
if (StaticChildren == false && cd.Parent != nullptr) {
|
||||
bool NecessaryEdges = false;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
float diff = fabs(Vertex[i+1].Y - (cd.Verts[i].Y
|
||||
+ cd.Verts[(i+3)&3].Y) * 0.5f);
|
||||
float diff = std::fabs(Vertex[i+1].Y - (cd.Verts[i].Y
|
||||
+ cd.Verts[(i+3)&3].Y) * 0.5f);
|
||||
if (diff > 0.00001f) {
|
||||
NecessaryEdges = true;
|
||||
}
|
||||
|
@ -568,9 +568,9 @@ static float DetailThreshold = 100;
|
|||
|
||||
bool quadsquare::VertexTest(int x, float y, int z, float error,
|
||||
const float Viewer[3], int level, vertex_loc_t vertex_loc) const {
|
||||
float dx = fabs(x - Viewer[0]) * fabs(ScaleX);
|
||||
float dy = fabs(y - Viewer[1]);
|
||||
float dz = fabs(z - Viewer[2]) * fabs(ScaleZ);
|
||||
float dx = std::fabs(x - Viewer[0]) * std::fabs(ScaleX);
|
||||
float dy = std::fabs(y - Viewer[1]);
|
||||
float dz = std::fabs(z - Viewer[2]) * std::fabs(ScaleZ);
|
||||
float d = std::max(dx, std::max(dy, dz));
|
||||
|
||||
if (vertex_loc == South && ForceSouthVert && d < VERTEX_FORCE_THRESHOLD) {
|
||||
|
@ -588,9 +588,9 @@ bool quadsquare::VertexTest(int x, float y, int z, float error,
|
|||
|
||||
bool quadsquare::BoxTest(int x, int z, float size, float miny, float maxy, float error, const float Viewer[3]) {
|
||||
float half = size * 0.5f;
|
||||
float dx = (fabs(x + half - Viewer[0]) - half) * fabs(ScaleX);
|
||||
float dy = fabs((miny + maxy) * 0.5f - Viewer[1]) - (maxy - miny) * 0.5f;
|
||||
float dz = (fabs(z + half - Viewer[2]) - half) * fabs(ScaleZ);
|
||||
float dx = (std::fabs(x + half - Viewer[0]) - half) * std::fabs(ScaleX);
|
||||
float dy = std::fabs((miny + maxy) * 0.5f - Viewer[1]) - (maxy - miny) * 0.5f;
|
||||
float dz = (std::fabs(z + half - Viewer[2]) - half) * std::fabs(ScaleZ);
|
||||
float d = std::max(dx, std::max(dy , dz));
|
||||
|
||||
if (d < ERROR_MAGNIFICATION_THRESHOLD) {
|
||||
|
@ -1081,10 +1081,7 @@ void ResetQuadtree() {
|
|||
}
|
||||
|
||||
static int get_root_level(int nx, int nz) {
|
||||
int xlev = (int)(std::log(static_cast<double>(nx)) / std::log(2.0));
|
||||
int zlev = (int)(std::log(static_cast<double>(nz)) / std::log(2.0));
|
||||
|
||||
return std::max(xlev, zlev);
|
||||
return (int)std::log2(static_cast<double>(std::max(nx, nz)));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -306,7 +306,7 @@ static void CalcFinishControls(CControl *ctrl, float timestep, bool airborne) {
|
|||
double speed = ctrl->cvel.Length();
|
||||
double dir_angle = RADIANS_TO_ANGLES(std::atan(ctrl->cvel.x / ctrl->cvel.z));
|
||||
|
||||
if (fabs(dir_angle) > 5 && speed > 5) {
|
||||
if (std::fabs(dir_angle) > 5 && speed > 5) {
|
||||
ctrl->turn_fact = dir_angle / 20;
|
||||
if (ctrl->turn_fact < -1) ctrl->turn_fact = -1;
|
||||
if (ctrl->turn_fact > 1) ctrl->turn_fact = 1;
|
||||
|
|
|
@ -231,7 +231,7 @@ void add_track_mark(const CControl *ctrl, int *id) {
|
|||
double left_y = Course.FindYCoord(left_wing.x, left_wing.z);
|
||||
double right_y = Course.FindYCoord(right_wing.x, right_wing.z);
|
||||
|
||||
if (fabs(left_y-right_y) > MAX_TRACK_DEPTH) {
|
||||
if (std::fabs(left_y-right_y) > MAX_TRACK_DEPTH) {
|
||||
break_track_marks();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -248,7 +248,7 @@ bool CCharShape::VisibleNode(std::size_t node_name, float level) {
|
|||
|
||||
if (node->visible) {
|
||||
node->divisions =
|
||||
clamp(MIN_SPHERE_DIV, ROUND_TO_NEAREST(param.tux_sphere_divisions * level / 10), MAX_SPHERE_DIV);
|
||||
clamp(MIN_SPHERE_DIV, (int)std::lround(param.tux_sphere_divisions * level / 10), MAX_SPHERE_DIV);
|
||||
node->radius = 1.0;
|
||||
}
|
||||
if (newActions && useActions) AddAction(node_name, 5, NullVec3, level);
|
||||
|
|
|
@ -28,7 +28,7 @@ struct TVector2 {
|
|||
: x(_x), y(_y)
|
||||
{}
|
||||
double Length() const {
|
||||
return std::sqrt(static_cast<double>(x*x + y*y));
|
||||
return std::hypot(x, y);
|
||||
}
|
||||
double Norm();
|
||||
TVector2<T>& operator*=(T f) {
|
||||
|
|
Loading…
Reference in New Issue