|
public static float sign(Vector3 p1, Vector3 p2, Vector3 p3) { |
|
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); |
|
} |
|
|
|
public static boolean checkPointInTriangle(Vector3 pt, Vector3 v1, Vector3 v2, Vector3 v3) { |
|
boolean b1, b2, b3; |
|
|
|
b1 = sign(pt, v1, v2) < 0.0f; |
|
b2 = sign(pt, v2, v3) < 0.0f; |
|
b3 = sign(pt, v3, v1) < 0.0f; |
|
|
|
return ((b1 == b2) && (b2 == b3)); |
|
} |
|
|
|
// OR |
|
|
|
public static Vector3 checkPointInTriangle(Vector3 point, Vector3 p1, Vector3 p2, Vector3 p3) { |
|
// Compute vectors |
|
Vector3 v0 = Vector3.sub(p3, p1); |
|
Vector3 v1 = Vector3.sub(p2, p1); |
|
Vector3 v2 = Vector3.sub(point, p1); |
|
|
|
// Compute dot products |
|
float dot00 = v0.dot(v0); |
|
float dot01 = v0.dot(v1); |
|
float dot02 = v0.dot(v2); |
|
float dot11 = v1.dot(v1); |
|
float dot12 = v1.dot(v2); |
|
|
|
// Compute barycentric coordinates |
|
float denom = dot00 * dot11 - dot01 * dot01; |
|
float u = (dot11 * dot02 - dot01 * dot12) / denom; |
|
float v = (dot00 * dot12 - dot01 * dot02) / denom; |
|
|
|
return new Vector3(u, v, 1 - u - v); |
|
} |
|
|
|
public static Vector3 calculateCartesianCoordinate(Vector3 barycentric, Vector3 p1, Vector3 p2, Vector3 p3) { |
|
Vector3 result = Vector3.add(Vector3.add(Vector3.mul(p1, barycentric.x), Vector3.mul(p2, barycentric.y)), Vector3.mul(p3, barycentric.z)); |
|
|
|
float x = result.x; |
|
result.x = result.y; |
|
result.y = x; |
|
|
|
return result; |
|
} |