aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java
diff options
context:
space:
mode:
authorRami Santina <[email protected]>2011-06-14 11:10:27 +0300
committerRami Santina <[email protected]>2011-06-14 11:10:27 +0300
commit83e82c3f72901a062cd2e73f4fc53353addcb337 (patch)
treeb4206077796797be25fce6293218334e09df6c99 /src/jogl/classes/com/jogamp/graph/math/VectorUtil.java
parentd578d3c3f1d3d8df4912ec259a186bfe562a448c (diff)
Added handling for offcurve triangle overlaps.
subdivde overlaping triangles for the case of vertex inside a off-curve boundary triangle. added vertex in triangle test (using barycentric coordinates)
Diffstat (limited to 'src/jogl/classes/com/jogamp/graph/math/VectorUtil.java')
-rwxr-xr-xsrc/jogl/classes/com/jogamp/graph/math/VectorUtil.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java b/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java
index b91969745..2b4a7fd9d 100755
--- a/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java
+++ b/src/jogl/classes/com/jogamp/graph/math/VectorUtil.java
@@ -287,6 +287,36 @@ public class VectorUtil {
public static float triArea(Vertex a, Vertex b, Vertex c){
return (b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY())*(c.getX() - a.getX());
}
+
+ /** Check if a vertex is in triangle using
+ * barycentric coordinates computation.
+ * @param a first triangle vertex
+ * @param b second triangle vertex
+ * @param c third triangle vertex
+ * @param p the vertex in question
+ * @return true if p is in triangle (a, b, c), false otherwise.
+ */
+ public static boolean vertexInTriangle(float[] a, float[] b, float[] c, float[] p){
+ // Compute vectors
+ float[] ac = computeVector(a, c); //v0
+ float[] ab = computeVector(a, b); //v1
+ float[] ap = computeVector(a, p); //v2
+
+ // Compute dot products
+ float dot00 = dot(ac, ac);
+ float dot01 = dot(ac, ab);
+ float dot02 = dot(ac, ap);
+ float dot11 = dot(ab, ab);
+ float dot12 = dot(ab, ap);
+
+ // Compute barycentric coordinates
+ float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
+ float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
+ float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
+
+ // Check if point is in triangle
+ return (u > 0) && (v > 0) && (u + v < 1);
+ }
/** Check if points are in ccw order
* @param a first vertex