Skip to content

Commit 8fea5d2

Browse files
authored
Capsule: Move and rename line-line-closest-points function to Octree util (mrdoob#27044)
1 parent 5d04c8e commit 8fea5d2

File tree

2 files changed

+70
-58
lines changed

2 files changed

+70
-58
lines changed

examples/jsm/math/Capsule.js

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ import {
22
Vector3
33
} from 'three';
44

5-
const _v1 = new Vector3();
6-
const _v2 = new Vector3();
7-
const _v3 = new Vector3();
8-
9-
const EPS = 1e-10;
10-
115
class Capsule {
126

137
constructor( start = new Vector3( 0, 0, 0 ), end = new Vector3( 0, 1, 0 ), radius = 1 ) {
@@ -83,55 +77,6 @@ class Capsule {
8377

8478
}
8579

86-
lineLineMinimumPoints( line1, line2 ) {
87-
88-
const r = _v1.copy( line1.end ).sub( line1.start );
89-
const s = _v2.copy( line2.end ).sub( line2.start );
90-
const w = _v3.copy( line2.start ).sub( line1.start );
91-
92-
const a = r.dot( s ),
93-
b = r.dot( r ),
94-
c = s.dot( s ),
95-
d = s.dot( w ),
96-
e = r.dot( w );
97-
98-
let t1, t2;
99-
const divisor = b * c - a * a;
100-
101-
if ( Math.abs( divisor ) < EPS ) {
102-
103-
const d1 = - d / c;
104-
const d2 = ( a - d ) / c;
105-
106-
if ( Math.abs( d1 - 0.5 ) < Math.abs( d2 - 0.5 ) ) {
107-
108-
t1 = 0;
109-
t2 = d1;
110-
111-
} else {
112-
113-
t1 = 1;
114-
t2 = d2;
115-
116-
}
117-
118-
} else {
119-
120-
t1 = ( d * a + e * c ) / divisor;
121-
t2 = ( t1 * a - d ) / c;
122-
123-
}
124-
125-
t2 = Math.max( 0, Math.min( 1, t2 ) );
126-
t1 = Math.max( 0, Math.min( 1, t1 ) );
127-
128-
const point1 = r.multiplyScalar( t1 ).add( line1.start );
129-
const point2 = s.multiplyScalar( t2 ).add( line2.start );
130-
131-
return [ point1, point2 ];
132-
133-
}
134-
13580
}
13681

13782
export { Capsule };

examples/jsm/math/Octree.js

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,75 @@ import { Capsule } from '../math/Capsule.js';
1111

1212
const _v1 = new Vector3();
1313
const _v2 = new Vector3();
14+
const _point1 = new Vector3();
15+
const _point2 = new Vector3();
1416
const _plane = new Plane();
1517
const _line1 = new Line3();
1618
const _line2 = new Line3();
1719
const _sphere = new Sphere();
1820
const _capsule = new Capsule();
1921

22+
const _temp1 = new Vector3();
23+
const _temp2 = new Vector3();
24+
const _temp3 = new Vector3();
25+
const EPS = 1e-10;
26+
27+
function lineToLineClosestPoints( line1, line2, target1 = null, target2 = null ) {
28+
29+
const r = _temp1.copy( line1.end ).sub( line1.start );
30+
const s = _temp2.copy( line2.end ).sub( line2.start );
31+
const w = _temp3.copy( line2.start ).sub( line1.start );
32+
33+
const a = r.dot( s ),
34+
b = r.dot( r ),
35+
c = s.dot( s ),
36+
d = s.dot( w ),
37+
e = r.dot( w );
38+
39+
let t1, t2;
40+
const divisor = b * c - a * a;
41+
42+
if ( Math.abs( divisor ) < EPS ) {
43+
44+
const d1 = - d / c;
45+
const d2 = ( a - d ) / c;
46+
47+
if ( Math.abs( d1 - 0.5 ) < Math.abs( d2 - 0.5 ) ) {
48+
49+
t1 = 0;
50+
t2 = d1;
51+
52+
} else {
53+
54+
t1 = 1;
55+
t2 = d2;
56+
57+
}
58+
59+
} else {
60+
61+
t1 = ( d * a + e * c ) / divisor;
62+
t2 = ( t1 * a - d ) / c;
63+
64+
}
65+
66+
t2 = Math.max( 0, Math.min( 1, t2 ) );
67+
t1 = Math.max( 0, Math.min( 1, t1 ) );
68+
69+
if ( target1 ) {
70+
71+
target1.copy( r ).multiplyScalar( t1 ).add( line1.start );
72+
73+
}
74+
75+
if ( target2 ) {
76+
77+
target2.copy( s ).multiplyScalar( t2 ).add( line2.start );
78+
79+
}
80+
81+
}
82+
2083
class Octree {
2184

2285

@@ -195,11 +258,15 @@ class Octree {
195258

196259
const line2 = _line2.set( lines[ i ][ 0 ], lines[ i ][ 1 ] );
197260

198-
const [ point1, point2 ] = capsule.lineLineMinimumPoints( line1, line2 );
261+
lineToLineClosestPoints( line1, line2, _point1, _point2 );
199262

200-
if ( point1.distanceToSquared( point2 ) < r2 ) {
263+
if ( _point1.distanceToSquared( _point2 ) < r2 ) {
201264

202-
return { normal: point1.clone().sub( point2 ).normalize(), point: point2.clone(), depth: capsule.radius - point1.distanceTo( point2 ) };
265+
return {
266+
normal: _point1.clone().sub( _point2 ).normalize(),
267+
point: _point2.clone(),
268+
depth: capsule.radius - _point1.distanceTo( _point2 )
269+
};
203270

204271
}
205272

0 commit comments

Comments
 (0)