55
66import { create , all } from 'mathjs' ;
77
8+ import { mathToJSFunction } from "./mathutils" ;
9+
810const config = { } ;
911const math = create ( all , config ) ;
1012
@@ -16,58 +18,25 @@ const math = create(all, config);
1618 */
1719export function updateParams ( gc , params ) {
1820 const { a, b, c, d, e, f, k } = params ;
19- let paramErrors = {
20- g : false ,
21- k : false
22- } ;
23-
24- return new Promise ( ( resolve , reject ) => {
25- setTimeout ( ( ) => {
26- const func = ( x , y , z ) => {
27- try {
28- return gc . evaluate ( { x, y, z} ) ;
29- } catch ( err ) {
30- paramErrors . g = true ;
31- return reject ( paramErrors ) ;
32- }
33- }
34-
35- // Test compiled g eval with some basic xyz params, for
36- // validation purposes.
37- try {
38- gc . evaluate ( { x : - 2 , y : - 2 , z : - 2 } ) ;
39- } catch ( err ) {
40- paramErrors . g = true ;
41- return reject ( paramErrors ) ;
42- }
43-
44- let kE = null ;
45- try {
46- kE = math . evaluate ( String ( k ) ) ;
47- } catch ( err ) {
48- paramErrors . k = true ;
49- return reject ( paramErrors ) ;
50- }
5121
52- const { normals, vertices, traceSegments } =
53- marchingCubesWithTraces ( {
54- f : func ,
55- level : kE ,
56- xMin : math . evaluate ( String ( a ) ) ,
57- xMax : math . evaluate ( String ( b ) ) ,
58- yMin : math . evaluate ( String ( c ) ) ,
59- yMax : math . evaluate ( String ( d ) ) ,
60- zMin : math . evaluate ( String ( e ) ) ,
61- zMax : math . evaluate ( String ( f ) ) ,
62- N : 30 ,
63- } ) ;
64-
65- resolve ( {
66- normals, vertices, xpts : [ ] , ypts : [ ] , zpts : traceSegments
67- } ) ;
68- } , 0 ) ;
22+ const func = mathToJSFunction ( gc , [ 'x' , 'y' , 'z' ] ) ;
23+
24+ const { normals, vertices, traceSegments } = marchingCubesWithTraces ( {
25+ f : func ,
26+ level : math . evaluate ( String ( k ) ) ,
27+ xMin : math . evaluate ( String ( a ) ) ,
28+ xMax : math . evaluate ( String ( b ) ) ,
29+ yMin : math . evaluate ( String ( c ) ) ,
30+ yMax : math . evaluate ( String ( d ) ) ,
31+ zMin : math . evaluate ( String ( e ) ) ,
32+ zMax : math . evaluate ( String ( f ) ) ,
33+ N : 60 ,
6934 } ) ;
70- }
35+ return {
36+ normals, vertices, xpts : [ ] , ypts : [ ] , zpts : traceSegments
37+ } ;
38+ } ;
39+
7140
7241const edgeTable = new Uint16Array ( [
7342 0x0 , 0x109 , 0x203 , 0x30a , 0x406 , 0x50f , 0x605 , 0x70c , 0x80c , 0x905 ,
@@ -472,10 +441,10 @@ function marchingCubesWithTraces({
472441 zMax = 1 ,
473442 N = 30 ,
474443 xN = 10 ,
475- /* eslint-disable */
444+ /* eslint-disable */
476445 yN = 10 ,
477446 zN = 10 ,
478- /* eslint-enable */
447+ /* eslint-enable */
479448} = { } ) {
480449 // const geometry = new THREE.BufferGeometry();
481450
@@ -554,8 +523,8 @@ function marchingCubesWithTraces({
554523 const pt = pts [ index ] ;
555524
556525 const u = x + pt [ 0 ] * dx ,
557- v = y + pt [ 1 ] * dy ,
558- w = z + pt [ 2 ] * dz ;
526+ v = y + pt [ 1 ] * dy ,
527+ w = z + pt [ 2 ] * dz ;
559528
560529 h = Math . max ( u * eps , ( 2 * eps ) ** 2 ) ;
561530 const fx = ( f ( u + h / 2 , v , w ) - f ( u - h / 2 , v , w ) ) / h ;
@@ -564,7 +533,7 @@ function marchingCubesWithTraces({
564533 h = Math . max ( w * eps , ( 2 * eps ) ** 2 ) ;
565534 const fz = ( f ( u , v , w + h / 2 ) - f ( u , v , w - h / 2 ) ) / h ;
566535 const inverseSquare =
567- 1 / math . sqrt ( fx * fx + fy * fy + fz * fz ) ;
536+ 1 / math . sqrt ( fx * fx + fy * fy + fz * fz ) ;
568537
569538 vertices . push ( u , v , w ) ;
570539 normals . push (
0 commit comments