Skip to content

Commit 96de7b7

Browse files
committed
CatmullRomCurve3: Support .closed property, update tests
1 parent f058021 commit 96de7b7

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/extras/curves/CatmullRomCurve3.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,41 @@ THREE.CatmullRomCurve3 = ( function() {
9696

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

99-
point = ( l - 1 ) * t;
99+
point = ( l - ( this.closed ? 0 : 1 ) ) * t;
100100
intPoint = Math.floor( point );
101101
weight = point - intPoint;
102-
103-
if ( weight === 0 && intPoint === l - 1 ) {
102+
103+
if ( this.closed ) {
104+
105+
intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / points.length ) + 1 ) * points.length;
106+
107+
} else if ( weight === 0 && intPoint === l - 1 ) {
104108

105109
intPoint = l - 2;
106110
weight = 1;
107111

108112
}
109113

110-
var p0, p1, p2, p3;
114+
var p0, p1, p2, p3; // 4 points
111115

112-
if ( intPoint === 0 ) {
116+
if ( this.closed || intPoint > 0 ) {
113117

114-
// extrapolate first point
115-
tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
116-
p0 = tmp;
118+
p0 = points[ ( intPoint - 1 ) % l ];
117119

118120
} else {
119121

120-
p0 = points[ intPoint - 1 ];
122+
// extrapolate first point
123+
tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
124+
p0 = tmp;
121125

122126
}
123127

124-
p1 = points[ intPoint ];
125-
p2 = points[ intPoint + 1 ];
128+
p1 = points[ intPoint % l ];
129+
p2 = points[ ( intPoint + 1 ) % l ];
126130

127-
if ( intPoint + 2 < l ) {
131+
if ( this.closed || intPoint + 2 < l ) {
128132

129-
p3 = points[ intPoint + 2 ]
133+
p3 = points[ ( intPoint + 2 ) % l ]
130134

131135
} else {
132136

test/unit/extras/curves/CatmullRomCurve3.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,32 @@ test( "centripetal basic check", function() {
9494
var desc = error ? ' ' + error : '';
9595
ok( !error, 'Lists of Vectors3 should be equal.' + desc );
9696

97+
});
98+
99+
test( "closed catmullrom basic check", function() {
100+
101+
var curve = new THREE.CatmullRomCurve3( positions );
102+
curve.type = 'catmullrom';
103+
curve.closed = true;
104+
105+
var closedSplinePoints = [
106+
new THREE.Vector3(-60,-100,60),
107+
new THREE.Vector3(-67.5,-46.25,67.5),
108+
new THREE.Vector3(-60,20,60),
109+
new THREE.Vector3(-67.5,83.75,67.5),
110+
new THREE.Vector3(-60,120,60),
111+
new THREE.Vector3(0,83.75,0),
112+
new THREE.Vector3(60,20,-60),
113+
new THREE.Vector3(75,-46.25,-75),
114+
new THREE.Vector3(60,-100,-60),
115+
new THREE.Vector3(0,-115,0),
116+
new THREE.Vector3(-60,-100,60),
117+
];
118+
119+
var getPoints = curve.getPoints(10);
120+
var error = vectorsAreEqual( getPoints , closedSplinePoints );
121+
ok( getPoints.length == 11, 'getPoints should be equal.');
122+
var desc = error ? ' ' + error : '';
123+
ok( !error, 'Lists of Vectors3 should be equal.' + desc );
124+
97125
});

0 commit comments

Comments
 (0)