summaryrefslogtreecommitdiffhomepage
path: root/src/raymath.h
diff options
context:
space:
mode:
authorRay <[email protected]>2017-02-07 09:40:06 +0100
committerGitHub <[email protected]>2017-02-07 09:40:06 +0100
commit94929011330b27f3d027975b52cf0d43ba85c38b (patch)
tree9ddff0e6ddba65004557c3628a3d56dfaf47ea4b /src/raymath.h
parent17f09cb03484a408cdd50a3d2e4d6604bb1f4c70 (diff)
parent1f6eb1fc61d2cc0cca54a79c1516432f09c86313 (diff)
downloadraylib-94929011330b27f3d027975b52cf0d43ba85c38b.tar.gz
raylib-94929011330b27f3d027975b52cf0d43ba85c38b.zip
Merge pull request #223 from raysan5/develop
Integrate develop branch into master
Diffstat (limited to 'src/raymath.h')
-rw-r--r--src/raymath.h43
1 files changed, 35 insertions, 8 deletions
diff --git a/src/raymath.h b/src/raymath.h
index 10eabb6b..c073b72d 100644
--- a/src/raymath.h
+++ b/src/raymath.h
@@ -130,6 +130,7 @@ RMDEF void VectorTransform(Vector3 *v, Matrix mat); // Transforms a Ve
RMDEF Vector3 VectorZero(void); // Return a Vector3 init to zero
RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2); // Return min value for each pair of components
RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2); // Return max value for each pair of components
+RMDEF Vector3 Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c); // Barycenter coords for p in triangle abc
//------------------------------------------------------------------------------------
// Functions Declaration to work with Matrix
@@ -222,16 +223,16 @@ RMDEF Vector3 VectorPerpendicular(Vector3 v)
{
Vector3 result;
- float min = fabs(v.x);
+ float min = fabsf(v.x);
Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f};
- if (fabs(v.y) < min)
+ if (fabsf(v.y) < min)
{
- min = fabs(v.y);
+ min = fabsf(v.y);
cardinalAxis = (Vector3){0.0f, 1.0f, 0.0f};
}
- if(fabs(v.z) < min)
+ if(fabsf(v.z) < min)
{
cardinalAxis = (Vector3){0.0f, 0.0f, 1.0f};
}
@@ -256,7 +257,7 @@ RMDEF float VectorLength(const Vector3 v)
{
float length;
- length = sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
+ length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
return length;
}
@@ -284,7 +285,7 @@ RMDEF void VectorNormalize(Vector3 *v)
length = VectorLength(*v);
- if (length == 0) length = 1.0f;
+ if (length == 0.0f) length = 1.0f;
ilength = 1.0f/length;
@@ -302,7 +303,7 @@ RMDEF float VectorDistance(Vector3 v1, Vector3 v2)
float dy = v2.y - v1.y;
float dz = v2.z - v1.z;
- result = sqrt(dx*dx + dy*dy + dz*dz);
+ result = sqrtf(dx*dx + dy*dy + dz*dz);
return result;
}
@@ -382,6 +383,32 @@ RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2)
return result;
}
+// Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
+// NOTE: Assumes P is on the plane of the triangle
+RMDEF Vector3 Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c)
+{
+ //Vector v0 = b - a, v1 = c - a, v2 = p - a;
+
+ Vector3 v0 = VectorSubtract(b, a);
+ Vector3 v1 = VectorSubtract(c, a);
+ Vector3 v2 = VectorSubtract(p, a);
+ float d00 = VectorDotProduct(v0, v0);
+ float d01 = VectorDotProduct(v0, v1);
+ float d11 = VectorDotProduct(v1, v1);
+ float d20 = VectorDotProduct(v2, v0);
+ float d21 = VectorDotProduct(v2, v1);
+
+ float denom = d00*d11 - d01*d01;
+
+ Vector3 result;
+
+ result.y = (d11*d20 - d01*d21)/denom;
+ result.z = (d00*d21 - d01*d20)/denom;
+ result.x = 1.0f - (result.z + result.y);
+
+ return result;
+}
+
//----------------------------------------------------------------------------------
// Module Functions Definition - Matrix math
//----------------------------------------------------------------------------------
@@ -590,7 +617,7 @@ RMDEF Matrix MatrixRotate(Vector3 axis, float angle)
float x = axis.x, y = axis.y, z = axis.z;
- float length = sqrt(x*x + y*y + z*z);
+ float length = sqrtf(x*x + y*y + z*z);
if ((length != 1.0f) && (length != 0.0f))
{