Skip to content

Commit

Permalink
CatmullRomCurve3: Support .closed property, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zz85 committed Nov 26, 2015
1 parent f058021 commit 96de7b7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/extras/curves/CatmullRomCurve3.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,41 @@ THREE.CatmullRomCurve3 = ( function() {

if ( l < 2 ) console.log( 'duh, you need at least 2 points' );

point = ( l - 1 ) * t;
point = ( l - ( this.closed ? 0 : 1 ) ) * t;
intPoint = Math.floor( point );
weight = point - intPoint;

if ( weight === 0 && intPoint === l - 1 ) {

if ( this.closed ) {

intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / points.length ) + 1 ) * points.length;

} else if ( weight === 0 && intPoint === l - 1 ) {

intPoint = l - 2;
weight = 1;

}

var p0, p1, p2, p3;
var p0, p1, p2, p3; // 4 points

if ( intPoint === 0 ) {
if ( this.closed || intPoint > 0 ) {

// extrapolate first point
tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
p0 = tmp;
p0 = points[ ( intPoint - 1 ) % l ];

} else {

p0 = points[ intPoint - 1 ];
// extrapolate first point
tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
p0 = tmp;

}

p1 = points[ intPoint ];
p2 = points[ intPoint + 1 ];
p1 = points[ intPoint % l ];
p2 = points[ ( intPoint + 1 ) % l ];

if ( intPoint + 2 < l ) {
if ( this.closed || intPoint + 2 < l ) {

p3 = points[ intPoint + 2 ]
p3 = points[ ( intPoint + 2 ) % l ]

} else {

Expand Down
28 changes: 28 additions & 0 deletions test/unit/extras/curves/CatmullRomCurve3.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,32 @@ test( "centripetal basic check", function() {
var desc = error ? ' ' + error : '';
ok( !error, 'Lists of Vectors3 should be equal.' + desc );

});

test( "closed catmullrom basic check", function() {

var curve = new THREE.CatmullRomCurve3( positions );
curve.type = 'catmullrom';
curve.closed = true;

var closedSplinePoints = [
new THREE.Vector3(-60,-100,60),
new THREE.Vector3(-67.5,-46.25,67.5),
new THREE.Vector3(-60,20,60),
new THREE.Vector3(-67.5,83.75,67.5),
new THREE.Vector3(-60,120,60),
new THREE.Vector3(0,83.75,0),
new THREE.Vector3(60,20,-60),
new THREE.Vector3(75,-46.25,-75),
new THREE.Vector3(60,-100,-60),
new THREE.Vector3(0,-115,0),
new THREE.Vector3(-60,-100,60),
];

var getPoints = curve.getPoints(10);
var error = vectorsAreEqual( getPoints , closedSplinePoints );
ok( getPoints.length == 11, 'getPoints should be equal.');
var desc = error ? ' ' + error : '';
ok( !error, 'Lists of Vectors3 should be equal.' + desc );

});

0 comments on commit 96de7b7

Please sign in to comment.