Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove optionalTarget - part 5 #13520

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions docs/api/math/Triangle.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ <h2>Methods</h2>
<h3>[method:Float area]()</h3>
<div>Return the area of the triangle.</div>

<h3>[method:Vector3 barycoordFromPoint]( [param:Vector3 point], [param:Vector3 optionalTarget] )</h3>
<h3>[method:Vector3 barycoordFromPoint]( [param:Vector3 point], [param:Vector3 target] )</h3>
<div>
[page:Vector3 point] - [page:Vector3] <br />
[page:Vector3 optionalTarget] - (optional) if specified, the result will be copied into this [page:Vector3], otherwise a new [page:Vector3] will be created.<br /><br />
[page:Vector3 target] — the result will be copied into this Vector3.<br /><br />

Return a [link:https://en.wikipedia.org/wiki/Barycentric_coordinate_system barycentric coordinate]
from the given vector. <br/><br/>
Expand All @@ -67,10 +67,10 @@ <h3>[method:Triangle clone]()</h3>
Returns a new triangle with the same [page:.a a], [page:.b b] and [page:.c c] properties as this one.
</div>

<h3>[method:Vector3 closestPointToPoint]( [param:Vector3 point], [param:Vector3 optionalTarget] )</h3>
<h3>[method:Vector3 closestPointToPoint]( [param:Vector3 point], [param:Vector3 target] )</h3>
<div>
point - [page:Vector3] <br />
[page:Vector3 optionalTarget] - (optional) if specified, the result will be copied into this [page:Vector3], otherwise a new [page:Vector3] will be created.<br /><br />
[page:Vector3 target] — the result will be copied into this Vector3.<br /><br />

Returns the closest point on the triangle to [page:Vector3 point].
</div>
Expand Down Expand Up @@ -100,23 +100,23 @@ <h3>[method:Boolean intersectsBox]( [param:Box3 box] )</h3>
Determines whether or not this triangle intersects [page:Box3 box].
</div>

<h3>[method:Vector3 midpoint]( [param:Vector3 optionalTarget] )</h3>
<h3>[method:Vector3 midpoint]( [param:Vector3 target] )</h3>
<div>
[page:Vector3 optionalTarget] - (optional) if specified, the result will be copied into this [page:Vector3], otherwise a new [page:Vector3] will be created.<br /><br />
[page:Vector3 target] — the result will be copied into this Vector3.<br /><br />

Calculate the midpoint of the triangle.
</div>

<h3>[method:Vector3 normal]( [param:Vector3 optionalTarget] )</h3>
<h3>[method:Vector3 normal]( [param:Vector3 target] )</h3>
<div>
[page:Vector3 optionalTarget] - (optional) if specified, the result will be copied into this [page:Vector3], otherwise a new [page:Vector3] will be created.<br /><br />
[page:Vector3 target] — the result will be copied into this Vector3.<br /><br />

Calculate the [link:https://en.wikipedia.org/wiki/Normal_(geometry) normal vector] of the triangle.
</div>

<h3>[method:Plane plane]( [param:Plane optionalTarget] )</h3>
<h3>[method:Plane plane]( [param:Plane target] )</h3>
<div>
[page:Plane optionalTarget] - (optional) if specified, the result will be copied into this [page:Plane], otherwise a new [page:Plane] will be created.<br /><br />
[page:Vector3 target] — the result will be copied into this Vector3.<br /><br />

Create a [page:Plane plane] based on the triangle. .
</div>
Expand Down
87 changes: 57 additions & 30 deletions src/math/Triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ Object.assign( Triangle, {

var v0 = new Vector3();

return function normal( a, b, c, optionalTarget ) {
return function normal( a, b, c, target ) {

var result = optionalTarget || new Vector3();
if ( target === undefined ) {

result.subVectors( c, b );
console.warn( 'THREE.Triangle: .normal() target is now required' );
target = new Vector3();

}

target.subVectors( c, b );
v0.subVectors( a, b );
result.cross( v0 );
target.cross( v0 );

var resultLengthSq = result.lengthSq();
if ( resultLengthSq > 0 ) {
var targetLengthSq = target.lengthSq();
if ( targetLengthSq > 0 ) {

return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) );

}

return result.set( 0, 0, 0 );
return target.set( 0, 0, 0 );

};

Expand All @@ -50,7 +55,7 @@ Object.assign( Triangle, {
var v1 = new Vector3();
var v2 = new Vector3();

return function barycoordFromPoint( point, a, b, c, optionalTarget ) {
return function barycoordFromPoint( point, a, b, c, target ) {

v0.subVectors( c, a );
v1.subVectors( b, a );
Expand All @@ -64,14 +69,19 @@ Object.assign( Triangle, {

var denom = ( dot00 * dot11 - dot01 * dot01 );

var result = optionalTarget || new Vector3();
if ( target === undefined ) {

console.warn( 'THREE.Triangle: .barycoordFromPoint() target is now required' );
target = new Vector3();

}

// 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 result.set( - 2, - 1, - 1 );
return target.set( - 2, - 1, - 1 );

}

Expand All @@ -80,7 +90,7 @@ Object.assign( Triangle, {
var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;

// barycentric coordinates must always sum to 1
return result.set( 1 - u - v, v, u );
return target.set( 1 - u - v, v, u );

};

Expand All @@ -92,9 +102,9 @@ Object.assign( Triangle, {

return function containsPoint( point, a, b, c ) {

var result = Triangle.barycoordFromPoint( point, a, b, c, v1 );
Triangle.barycoordFromPoint( point, a, b, c, v1 );

return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
return ( v1.x >= 0 ) && ( v1.y >= 0 ) && ( ( v1.x + v1.y ) <= 1 );

};

Expand Down Expand Up @@ -156,30 +166,41 @@ Object.assign( Triangle.prototype, {

}(),

midpoint: function ( optionalTarget ) {
midpoint: function ( target ) {

if ( target === undefined ) {

console.warn( 'THREE.Triangle: .midpoint() target is now required' );
target = new Vector3();

var result = optionalTarget || new Vector3();
return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
}

return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );

},

normal: function ( optionalTarget ) {
normal: function ( target ) {

return Triangle.normal( this.a, this.b, this.c, optionalTarget );
return Triangle.normal( this.a, this.b, this.c, target );

},

plane: function ( optionalTarget ) {
plane: function ( target ) {

if ( target === undefined ) {

var result = optionalTarget || new Plane();
console.warn( 'THREE.Triangle: .plane() target is now required' );
target = new Vector3();

}

return result.setFromCoplanarPoints( this.a, this.b, this.c );
return target.setFromCoplanarPoints( this.a, this.b, this.c );

},

barycoordFromPoint: function ( point, optionalTarget ) {
barycoordFromPoint: function ( point, target ) {

return Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
return Triangle.barycoordFromPoint( point, this.a, this.b, this.c, target );

},

Expand All @@ -202,9 +223,15 @@ Object.assign( Triangle.prototype, {
var projectedPoint = new Vector3();
var closestPoint = new Vector3();

return function closestPointToPoint( point, optionalTarget ) {
return function closestPointToPoint( point, target ) {

if ( target === undefined ) {

console.warn( 'THREE.Triangle: .closestPointToPoint() target is now required' );
target = new Vector3();

}

var result = optionalTarget || new Vector3();
var minDistance = Infinity;

// project the point onto the plane of the triangle
Expand All @@ -218,11 +245,11 @@ Object.assign( Triangle.prototype, {

// if so, this is the closest point

result.copy( projectedPoint );
target.copy( projectedPoint );

} else {

// if not, the point falls outside the triangle. the result is the closest point to the triangle's edges or vertices
// if not, the point falls outside the triangle. the target is the closest point to the triangle's edges or vertices

edgeList[ 0 ].set( this.a, this.b );
edgeList[ 1 ].set( this.b, this.c );
Expand All @@ -238,15 +265,15 @@ Object.assign( Triangle.prototype, {

minDistance = distance;

result.copy( closestPoint );
target.copy( closestPoint );

}

}

}

return result;
return target;

};

Expand Down