@@ -26,13 +26,15 @@ function IsosurfaceTrace(scene, mesh, uid) {
2626
2727var proto = IsosurfaceTrace . prototype ;
2828
29- function findNearestOnAxis ( w , arr ) {
30- for ( var q = arr . length - 1 ; q > 0 ; q -- ) {
31- var min = Math . min ( arr [ q ] , arr [ q - 1 ] ) ;
32- var max = Math . max ( arr [ q ] , arr [ q - 1 ] ) ;
29+ function findNearestOnAxis ( w , arr , nPoints , nDim , step ) {
30+ var id = nDim ;
31+ for ( var q = nPoints - 1 ; q > 0 && id > 0 ; q -= step ) {
32+ id -- ;
33+ var min = Math . min ( arr [ q ] , arr [ q - step ] ) ;
34+ var max = Math . max ( arr [ q ] , arr [ q - step ] ) ;
3335 if ( max > min && min < w && w <= max ) {
3436 return {
35- id : q ,
37+ id : id ,
3638 distRatio : ( max - w ) / ( max - min )
3739 } ;
3840 }
@@ -52,14 +54,16 @@ proto.handlePick = function(selection) {
5254 var y = this . data . y [ rawId ] ;
5355 var z = this . data . z [ rawId ] ;
5456
55- var i = findNearestOnAxis ( x , this . data . _Xs ) . id ;
56- var j = findNearestOnAxis ( y , this . data . _Ys ) . id ;
57- var k = findNearestOnAxis ( z , this . data . _Zs ) . id ;
57+ var width = this . data . width ;
58+ var height = this . data . height ;
59+ var depth = this . data . depth ;
60+ var nPoints = width * height * depth ;
5861
59- var width = this . data . _Xs . length ;
60- var height = this . data . _Ys . length ;
62+ var i = findNearestOnAxis ( x , this . data . x , nPoints , width , 1 + depth * height ) . id ;
63+ var j = findNearestOnAxis ( y , this . data . y , nPoints , height , 1 + depth ) . id ;
64+ var k = findNearestOnAxis ( z , this . data . z , nPoints , depth , 1 ) . id ;
6165
62- var selectIndex = selection . index = i + width * j + width * height * k ;
66+ var selectIndex = selection . index = k + depth * j + depth * height * i ;
6367
6468 selection . traceCoordinate = [
6569 this . data . x [ selectIndex ] ,
@@ -150,26 +154,14 @@ function generateIsosurfaceMesh(data) {
150154 var numVertices ;
151155 var beginVertextLength ;
152156
153- var width = data . x . length ;
154- var height = data . y . length ;
155- var depth = data . z . length ;
157+ var width = data . width ;
158+ var height = data . height ;
159+ var depth = data . depth ;
156160
157161 function getIndex ( i , j , k ) {
158- return i + width * j + width * height * k ;
162+ return k + depth * j + depth * height * i ;
159163 }
160164
161- function copyElements ( src ) {
162- var dst = [ ] ;
163- for ( var i = 0 ; i < src . length ; i ++ ) {
164- dst [ i ] = src [ i ] ;
165- }
166- return dst ;
167- }
168-
169- var Xs = copyElements ( data . x ) ;
170- var Ys = copyElements ( data . y ) ;
171- var Zs = copyElements ( data . z ) ;
172-
173165 var minValues = data . _minValues ;
174166 var maxValues = data . _maxValues ;
175167
@@ -384,16 +376,11 @@ function generateIsosurfaceMesh(data) {
384376 var xyzv = [ ] ;
385377 for ( var q = 0 ; q < 4 ; q ++ ) {
386378 var index = indecies [ q ] ;
387-
388- var k = Math . floor ( index / ( width * height ) ) ;
389- var j = Math . floor ( ( index - k * width * height ) / width ) ;
390- var i = Math . floor ( index - k * width * height - j * width ) ;
391-
392379 xyzv . push (
393380 [
394- Xs [ i ] ,
395- Ys [ j ] ,
396- Zs [ k ] ,
381+ data . x [ index ] ,
382+ data . y [ index ] ,
383+ data . z [ index ] ,
397384 data . value [ index ]
398385 ]
399386 ) ;
@@ -816,10 +803,16 @@ function generateIsosurfaceMesh(data) {
816803 }
817804
818805 function insertGridPoints ( ) {
819- for ( var k = 0 ; k < depth ; k ++ ) {
806+ for ( var i = 0 ; i < width ; i ++ ) {
820807 for ( var j = 0 ; j < height ; j ++ ) {
821- for ( var i = 0 ; i < width ; i ++ ) {
822- addVertex ( Xs [ i ] , Ys [ j ] , Zs [ k ] , data . value [ getIndex ( i , j , k ) ] ) ;
808+ for ( var k = 0 ; k < depth ; k ++ ) {
809+ var index = getIndex ( i , j , k ) ;
810+ addVertex (
811+ data . x [ index ] ,
812+ data . y [ index ] ,
813+ data . z [ index ] ,
814+ data . value [ index ]
815+ ) ;
823816 }
824817 }
825818 }
@@ -883,13 +876,19 @@ function generateIsosurfaceMesh(data) {
883876 var ceilIndices = [ ] ;
884877 var distRatios = [ ] ;
885878 if ( slice . locations . length ) {
879+
886880 for ( var q = 0 ; q < slice . locations . length ; q ++ ) {
887881
888- var near = findNearestOnAxis (
889- slice . locations [ q ] ,
890- ( e === 'x' ) ? Xs :
891- ( e === 'y' ) ? Ys : Zs
892- ) ;
882+ var location = slice . locations [ q ] ;
883+
884+ var near ;
885+ if ( e === 'x' ) {
886+ near = findNearestOnAxis ( location , data . x , width * height * depth , width , 1 + depth * height ) ;
887+ } else if ( e === 'y' ) {
888+ near = findNearestOnAxis ( location , data . y , width * height * depth , height , 1 + depth ) ;
889+ } else {
890+ near = findNearestOnAxis ( location , data . z , width * height * depth , depth , 1 ) ;
891+ }
893892
894893 if ( near . distRatio === 0 ) {
895894 exactIndices . push ( near . id ) ;
@@ -955,10 +954,6 @@ function generateIsosurfaceMesh(data) {
955954 emptyVertices ( ) ;
956955 }
957956
958- data . _Xs = Xs ;
959- data . _Ys = Ys ;
960- data . _Zs = Zs ;
961-
962957 data . x = allXs ;
963958 data . y = allYs ;
964959 data . z = allZs ;
0 commit comments