Skip to content

Commit

Permalink
more 64-bit fixes (don't rely on std.math)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingrater committed Oct 14, 2013
1 parent 6c14bb3 commit 88b2b8d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
58 changes: 30 additions & 28 deletions src/thBase/math3d/all.d
Expand Up @@ -11,6 +11,8 @@ public import thBase.math3d.quaternion;
public import thBase.math3d.triangle;
public import thBase.math3d.sphere;

import core.stdc.math;

/**
* Computes the normal for a given triangle
* Params:
Expand Down Expand Up @@ -46,12 +48,12 @@ const(mat4) RotationMatrixXYZ(float x, float y, float z){
mat4 result;
float A,B,C,D,E,F,AD,BD;

A = cos(x/(-180.0f) * PI);
B = sin(x/(-180.0f) * PI);
C = cos(y/(-180.0f) * PI);
D = sin(y/(-180.0f) * PI);
E = cos(z/(-180.0f) * PI);
F = sin(z/(-180.0f) * PI);
A = cosf(x/(-180.0f) * PI);
B = sinf(x/(-180.0f) * PI);
C = cosf(y/(-180.0f) * PI);
D = sinf(y/(-180.0f) * PI);
E = cosf(z/(-180.0f) * PI);
F = sinf(z/(-180.0f) * PI);
AD = A * D;
BD = B * D;
result.f[0] = C * E;
Expand Down Expand Up @@ -79,12 +81,12 @@ const(mat4) RotationMatrixXYZ(ref const(vec3) v3Rotation)
mat4 result;
float A,B,C,D,E,F,AD,BD;

A = cos(v3Rotation.x/(-180.0f) * PI);
B = sin(v3Rotation.x/(-180.0f) * PI);
C = cos(v3Rotation.y/(-180.0f) * PI);
D = sin(v3Rotation.y/(-180.0f) * PI);
E = cos(v3Rotation.z/(-180.0f) * PI);
F = sin(v3Rotation.z/(-180.0f) * PI);
A = cosf(v3Rotation.x/(-180.0f) * PI);
B = sinf(v3Rotation.x/(-180.0f) * PI);
C = cosf(v3Rotation.y/(-180.0f) * PI);
D = sinf(v3Rotation.y/(-180.0f) * PI);
E = cosf(v3Rotation.z/(-180.0f) * PI);
F = sinf(v3Rotation.z/(-180.0f) * PI);
AD = A * D;
BD = B * D;
result.f[0] = C * E;
Expand Down Expand Up @@ -210,43 +212,43 @@ const(vec4) UpVektor(float X,float Y,float Z,float Radians){
float RotY,RotZ,Distance;
vec4 Up;
//Z Rotation Berechnen
Distance = sqrt(X * X + Y * Y);
Distance = sqrtf(X * X + Y * Y);
if(Distance != 0){
if(Y >= 0)
RotZ = acos( X / Distance);
RotZ = acosf( X / Distance);
else
RotZ = 2 * PI - acos( X / Distance);
RotZ = 2 * PI - acosf( X / Distance);
X = Distance;
Y = 0;
}
else {
RotZ=0;
}
//Y Rotation Berechnen
Distance = sqrt( X * X + Z * Z);
Distance = sqrtf( X * X + Z * Z);
if(Distance != 0)
RotY = acos( X / Distance);
RotY = acosf( X / Distance);
else
RotY = 0;
//Up Point berechnen
X = 0;
Y = sin(Radians);
Z = cos(Radians);
Y = sinf(Radians);
Z = cosf(Radians);
//Um Y Achse drehen
if(RotY != 0){
X = sin(RotY) * Z;
Z = cos(RotY) * Z;
X = sinf(RotY) * Z;
Z = cosf(RotY) * Z;
}
//Um Z Achse drehen
if(RotZ != 0){
Distance = sqrt(X * X + Y * Y);
Distance = sqrtf(X * X + Y * Y);
if(Distance != 0){
if(Y >= 0)
RotZ += acos(X / Distance);
RotZ += acosf(X / Distance);
else
RotZ += 2 * PI - acos(X / Distance);
X = cos(RotZ) * Distance;
Y = sin(RotZ) * Distance;
RotZ += 2 * PI - acosf(X / Distance);
X = cosf(RotZ) * Distance;
Y = sinf(RotZ) * Distance;
}
}
Up.x = X;
Expand All @@ -272,7 +274,7 @@ const(vec4) Tangent(ref const(vec4) v1, ref const(vec4) v2, ref const(vec4) v3,
float div;
//T
div = (t2.x - t1.x) * (t3.y - t1.y) - (t3.x - t1.x) * (t2.y - t1.y);
div = fabs(div);
div = fabsf(div);
t = ((t3.y - t1.y) * (v2 - v1) - (t2.y - t1.y) * (v3 - v1)) / div;
t.w = 1.0f;
return t;
Expand All @@ -292,7 +294,7 @@ const(vec4) Binormal(ref const(vec4) v1, ref const(vec4) v2, ref const(vec4) v3,
vec4 b;
float div;
div = (t2.x - t1.x) * (t3.y - t1.y) - (t3.x - t1.x) * (t2.y - t1.y);
div = fabs(div);
div = fabsf(div);
b = ((t2.x - t1.x)* (v3 - v1) - (t3.x - t1.x) * (v2 - v1)) / div;
b.w = 1.0f;
return b;
Expand Down
21 changes: 11 additions & 10 deletions src/thBase/math3d/quaternion.d
Expand Up @@ -3,6 +3,7 @@ module thBase.math3d.quaternion;
import thBase.math3d.vecs;
import thBase.math3d.mats;
import std.math;
import core.stdc.math;
import thBase.math : FloatEpsilon;
import core.stdc.stdio;
import rtti;
Expand All @@ -26,12 +27,12 @@ struct Quaternion {
this(vec3 axis, float angle){
angle = angle / 180.0f * PI;
angle /= 2;
float temp = sin(angle);
float temp = sinf(angle);

this.x = axis.x * temp;
this.y = axis.y * temp;
this.z = axis.z * temp;
this.angle = cos(angle);
this.angle = cosf(angle);
}

//ditto
Expand All @@ -46,7 +47,7 @@ struct Quaternion {
float trace = 1.0f + rot.f[0] + rot.f[4] + rot.f[8];
if(trace > 0.00000001f)
{
float S = sqrt(trace) * 2.0f;
float S = sqrtf(trace) * 2.0f;
this.x = ( rot.f[7] - rot.f[5] ) / S;
this.y = ( rot.f[2] - rot.f[6] ) / S;
this.z = ( rot.f[3] - rot.f[1] ) / S;
Expand All @@ -56,23 +57,23 @@ struct Quaternion {
{
if( rot.f[0] > rot.f[4] && rot.f[0] > rot.f[8] ) //Column 0:
{
float S = sqrt( 1.0f + rot.f[0] - rot.f[4] - rot.f[8] ) * 2;
float S = sqrtf( 1.0f + rot.f[0] - rot.f[4] - rot.f[8] ) * 2;
this.x = 0.25f * S;
this.y = ( rot.f[3] + rot.f[1] ) / S;
this.z = ( rot.f[2] + rot.f[6] ) / S;
this.angle = ( rot.f[7] - rot.f[5] ) / S;
}
else if( rot.f[4] > rot.f[8] ) // Column 1:
{
float S = sqrt( 1.0f + rot.f[4] - rot.f[0] - rot.f[8] ) * 2.0f;
float S = sqrtf( 1.0f + rot.f[4] - rot.f[0] - rot.f[8] ) * 2.0f;
this.x = ( rot.f[3] + rot.f[1] ) / S;
this.y = 0.25f * S;
this.z = ( rot.f[7] + rot.f[5] ) / S;
this.angle = ( rot.f[2] - rot.f[6] ) / S;
}
else
{
float S = sqrt( 1.0f + rot.f[8] - rot.f[0] - rot.f[4] ) * 2.0f;
float S = sqrtf( 1.0f + rot.f[8] - rot.f[0] - rot.f[4] ) * 2.0f;
this.x = ( rot.f[2] + rot.f[6] ) / S;
this.y = ( rot.f[7] + rot.f[5] ) / S;
this.z = 0.25f * S;
Expand All @@ -87,7 +88,7 @@ struct Quaternion {
*/
Quaternion normalize() const pure {
Quaternion res;
float length = sqrt(x * x + y * y + z * z + angle * angle);
float length = sqrtf(x * x + y * y + z * z + angle * angle);
if(length != 0){
res.x = x / length;
res.y = y / length;
Expand Down Expand Up @@ -227,10 +228,10 @@ struct Quaternion {
}
else
{
float velocityLength = sqrt(squaredVelocityLength);
float velocityLength = sqrtf(squaredVelocityLength);

deltaQ.angle = cos(velocityLength);
s = sin(velocityLength) / velocityLength;
deltaQ.angle = cosf(velocityLength);
s = sinf(velocityLength) / velocityLength;
}

deltaQ.x = scaledAngularVelocity.x * s;
Expand Down

0 comments on commit 88b2b8d

Please sign in to comment.