diff --git a/docs/api/en/math/Triangle.html b/docs/api/en/math/Triangle.html index 862ae98528f35f..3d42ed390bb7a7 100644 --- a/docs/api/en/math/Triangle.html +++ b/docs/api/en/math/Triangle.html @@ -97,7 +97,7 @@

[page:Vector3 target] — the result will be copied into this Vector3.

Return a [link:https://en.wikipedia.org/wiki/Barycentric_coordinate_system barycentric coordinate] - from the given vector.

+ from the given vector. Returns null if the triangle is degenerate.

[link:http://commons.wikimedia.org/wiki/File:Barycentric_coordinates_1.png Picture of barycentric coordinates]

diff --git a/src/math/Triangle.js b/src/math/Triangle.js index b78602571919e3..42b39d02e1dbe7 100644 --- a/src/math/Triangle.js +++ b/src/math/Triangle.js @@ -60,9 +60,8 @@ class Triangle { // collinear or singular triangle if ( denom === 0 ) { - // arbitrary location outside of triangle? - // not sure if this is the best idea, maybe should be returning undefined - return target.set( - 2, - 1, - 1 ); + target.set( 0, 0, 0 ); + return null; } @@ -77,7 +76,12 @@ class Triangle { static containsPoint( point, a, b, c ) { - this.getBarycoord( point, a, b, c, _v3 ); + // if the triangle is degenerate then we can't contain a point + if ( this.getBarycoord( point, a, b, c, _v3 ) === null ) { + + return false; + + } return ( _v3.x >= 0 ) && ( _v3.y >= 0 ) && ( ( _v3.x + _v3.y ) <= 1 ); diff --git a/test/unit/src/math/Triangle.tests.js b/test/unit/src/math/Triangle.tests.js index 5c6374607df7e9..180f3fe6ae5cc2 100644 --- a/test/unit/src/math/Triangle.tests.js +++ b/test/unit/src/math/Triangle.tests.js @@ -211,16 +211,12 @@ export default QUnit.module( 'Maths', () => { let a = new Triangle(); - const bad = new Vector3( - 2, - 1, - 1 ); const barycoord = new Vector3(); const midpoint = new Vector3(); - a.getBarycoord( a.a, barycoord ); - assert.ok( barycoord.equals( bad ), 'Passed!' ); - a.getBarycoord( a.b, barycoord ); - assert.ok( barycoord.equals( bad ), 'Passed!' ); - a.getBarycoord( a.c, barycoord ); - assert.ok( barycoord.equals( bad ), 'Passed!' ); + assert.ok( a.getBarycoord( a.a, barycoord ) === null, 'Passed!' ); + assert.ok( a.getBarycoord( a.b, barycoord ) === null, 'Passed!' ); + assert.ok( a.getBarycoord( a.c, barycoord ) === null, 'Passed!' ); a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) ); a.getMidpoint( midpoint );