Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance for checking if a point is inside a triangle (3D)
    primarykey
    data
    text
    <p>I am in pursuit of a lightning fast java method to check if a point is inside a triangle.</p> <p>I found the following c++ code in a paper from Kasper Fauerby:</p> <pre><code>typedef unsigned int uint32; #define in(a) ((uint32&amp;) a) bool checkPointInTriangle(const VECTOR&amp; point, const VECTOR&amp; pa,const VECTOR&amp; pb, const VECTOR&amp; pc) { VECTOR e10=pb-pa; VECTOR e20=pc-pa; float a = e10.dot(e10); float b = e10.dot(e20); float c = e20.dot(e20); float ac_bb=(a*c)-(b*b); VECTOR vp(point.x-pa.x, point.y-pa.y, point.z-pa.z); float d = vp.dot(e10); float e = vp.dot(e20); float x = (d*c)-(e*b); float y = (e*a)-(d*b); float z = x+y-ac_bb; return (( in(z)&amp; ~(in(x)|in(y)) ) &amp; 0x80000000); } </code></pre> <p>I was wondering if this code snippet could be converted to java, and if so, if it would outperform my Java code:</p> <pre><code>public class Util { public static boolean checkPointInTriangle(Vector p1, Vector p2, Vector p3, Vector point) { float angles = 0; Vector v1 = Vector.min(point, p1); v1.normalize(); Vector v2 = Vector.min(point, p2); v2.normalize(); Vector v3 = Vector.min(point, p3); v3.normalize(); angles += Math.acos(Vector.dot(v1, v2)); angles += Math.acos(Vector.dot(v2, v3)); angles += Math.acos(Vector.dot(v3, v1)); return (Math.abs(angles - 2*Math.PI) &lt;= 0.005); } public static void main(String [] args) { Vector p1 = new Vector(4.5f, 0, 0); Vector p2 = new Vector(0, -9f, 0); Vector p3 = new Vector(0, 0, 4.5f); Vector point = new Vector(2, -4, 0.5f); System.out.println(checkPointInTriangle(p1, p2, p3, point)); } } </code></pre> <p>and the Vector class:</p> <pre><code>public class Vector { public float x, y, z; public Vector(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } public float length() { return (float) Math.sqrt(x*x + y*y + z*z); } public void normalize() { float l = length(); x /= l; y /= l; z /= l; } public static float dot(Vector one, Vector two) { return one.x*two.x + one.y*two.y + one.z*two.z; } public static Vector min(Vector one, Vector two) { return new Vector(one.x-two.x, one.y-two.y, one.z-two.z); } } </code></pre> <p>or is there an even faster method for Java?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload