From 8a50918d2a58a84111567d73949fe67b9424c660 Mon Sep 17 00:00:00 2001
From: Woazboat <f.kargl@posteo.de>
Date: Tue, 28 Apr 2015 02:51:21 +0200
Subject: [PATCH] Tracer::signum function now returns int

convert c style casts to c++ static casts

Changed fabs() to std::abs()
---
 src/SetChunkData.cpp |  2 +-
 src/Tracer.cpp       | 49 +++++++++++++++++++++++---------------------
 src/Tracer.h         |  3 ++-
 3 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/src/SetChunkData.cpp b/src/SetChunkData.cpp
index e5afd96ed..7549b0dbf 100644
--- a/src/SetChunkData.cpp
+++ b/src/SetChunkData.cpp
@@ -103,7 +103,7 @@ void cSetChunkData::CalculateHeightMap(void)
 				int index = cChunkDef::MakeIndexNoCheck(x, y, z);
 				if (m_BlockTypes[index] != E_BLOCK_AIR)
 				{
-					m_HeightMap[x + z * cChunkDef::Width] = (HEIGHTTYPE)y;
+					m_HeightMap[x + z * cChunkDef::Width] = static_cast<HEIGHTTYPE>(y);
 					break;
 				}
 			}  // for y
diff --git a/src/Tracer.cpp b/src/Tracer.cpp
index 38968bc61..b6b0fd634 100644
--- a/src/Tracer.cpp
+++ b/src/Tracer.cpp
@@ -12,6 +12,8 @@
 
 
 
+const float FLOAT_EPSILON = 0.0001f;  // TODO: Stash this in some header where it can be reused
+
 
 const std::array<const Vector3f, 6>& cTracer::m_NormalTable(void)
 {
@@ -50,7 +52,7 @@ cTracer::~cTracer()
 
 
 
-float cTracer::SigNum(float a_Num)
+int cTracer::SigNum(float a_Num)
 {
 	if (a_Num < 0.f)
 	{
@@ -76,9 +78,10 @@ void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction)
 	dir = a_Direction;
 
 	// decide which direction to start walking in
-	step.x = (int) SigNum(dir.x);
-	step.y = (int) SigNum(dir.y);
-	step.z = (int) SigNum(dir.z);
+	step.x = SigNum(dir.x);
+	step.y = SigNum(dir.y);
+	step.z = SigNum(dir.z);
+
 
 	// normalize the direction vector
 	dir.Normalize();
@@ -89,7 +92,7 @@ void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction)
 	// same but y-direction
 	if (dir.x != 0.f)
 	{
-		tDelta.x = 1 / fabs(dir.x);
+		tDelta.x = 1 / std::abs(dir.x);
 	}
 	else
 	{
@@ -97,7 +100,7 @@ void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction)
 	}
 	if (dir.y != 0.f)
 	{
-		tDelta.y = 1 / fabs(dir.y);
+		tDelta.y = 1 / std::abs(dir.y);
 	}
 	else
 	{
@@ -105,44 +108,45 @@ void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction)
 	}
 	if (dir.z != 0.f)
 	{
-		tDelta.z = 1 / fabs(dir.z);
+		tDelta.z = 1 / std::abs(dir.z);
 	}
 	else
 	{
 		tDelta.z = 0;
 	}
 
+
 	// start voxel coordinates
-	pos.x = (int)floorf(a_Start.x);
-	pos.y = (int)floorf(a_Start.y);
-	pos.z = (int)floorf(a_Start.z);
+	pos.x = static_cast<int>(floorf(a_Start.x));
+	pos.y = static_cast<int>(floorf(a_Start.y));
+	pos.z = static_cast<int>(floorf(a_Start.z));
 
 	// calculate distance to first intersection in the voxel we start from
 	if (dir.x < 0)
 	{
-		tMax.x = ((float)pos.x - a_Start.x) / dir.x;
+		tMax.x = (static_cast<float>(pos.x) - a_Start.x) / dir.x;
 	}
 	else
 	{
-		tMax.x = (((float)pos.x + 1) - a_Start.x) / dir.x;
+		tMax.x = (static_cast<float>(pos.x + 1) - a_Start.x) / dir.x;  // TODO: Possible division by zero
 	}
 
 	if (dir.y < 0)
 	{
-		tMax.y = ((float)pos.y - a_Start.y) / dir.y;
+		tMax.y = (static_cast<float>(pos.y) - a_Start.y) / dir.y;
 	}
 	else
 	{
-		tMax.y = (((float)pos.y + 1) - a_Start.y) / dir.y;
+		tMax.y = (static_cast<float>(pos.y + 1) - a_Start.y) / dir.y;  // TODO: Possible division by zero
 	}
 
 	if (dir.z < 0)
 	{
-		tMax.z = ((float)pos.z - a_Start.z) / dir.z;
+		tMax.z = (static_cast<float>(pos.z) - a_Start.z) / dir.z;
 	}
 	else
 	{
-		tMax.z = (((float)pos.z + 1) - a_Start.z) / dir.z;
+		tMax.z = (static_cast<float>(pos.z + 1) - a_Start.z) / dir.z;  // TODO: Possible division by zero
 	}
 }
 
@@ -165,18 +169,18 @@ bool cTracer::Trace(const Vector3f & a_Start, const Vector3f & a_Direction, int
 	
 	SetValues(a_Start, a_Direction);
 
-	Vector3f End = a_Start + (dir * (float)a_Distance);
+	Vector3f End = a_Start + (dir * static_cast<float>(a_Distance));
 	
 	if (End.y < 0)
 	{
-		float dist = -a_Start.y / dir.y;
+		float dist = -a_Start.y / dir.y;  // No division by 0 possible
 		End = a_Start + (dir * dist);
 	}
 
 	// end voxel coordinates
-	end1.x = (int)floorf(End.x);
-	end1.y = (int)floorf(End.y);
-	end1.z = (int)floorf(End.z);
+	end1.x = static_cast<int>(floorf(End.x));
+	end1.y = static_cast<int>(floorf(End.y));
+	end1.z = static_cast<int>(floorf(End.z));
 	
 	// check if first is occupied
 	if (pos.Equals(end1))
@@ -314,8 +318,7 @@ int cTracer::intersect3D_SegmentPlane(const Vector3f & a_Origin, const Vector3f
 	float     D = a_PlaneNormal.Dot(u);      // dot(Pn.n, u);
 	float     N = -(a_PlaneNormal.Dot(w));  // -dot(a_Plane.n, w);
 
-	const float EPSILON = 0.0001f;
-	if (fabs(D) < EPSILON)
+	if (std::abs(D) < FLOAT_EPSILON)
 	{
 		// segment is parallel to plane
 		if (N == 0.0)
diff --git a/src/Tracer.h b/src/Tracer.h
index cf54fa66c..31531719f 100644
--- a/src/Tracer.h
+++ b/src/Tracer.h
@@ -63,7 +63,8 @@ private:
 	/// Return 1 through 6 for the following block faces, repectively: -x, -z, x, z, y, -y
 	int GetHitNormal( const Vector3f & start, const Vector3f & end, const Vector3i &  a_BlockPos);
 
-	float SigNum( float a_Num);
+	/// Signum function
+	int SigNum( float a_Num);
 	cWorld* m_World;
 
 	static const std::array<const Vector3f, 6> & m_NormalTable(void);