From e20536c865d7c51b79f299f899cd38b5b27c5cc8 Mon Sep 17 00:00:00 2001 From: aescalante Date: Wed, 26 May 2021 18:53:32 +0200 Subject: [PATCH 1/3] expose more cspice functions --- src/Spice.js | 119 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 28 deletions(-) diff --git a/src/Spice.js b/src/Spice.js index 62b2db3..1d12ef6 100644 --- a/src/Spice.js +++ b/src/Spice.js @@ -1,4 +1,4 @@ -import { ASM_SPICE_FULL, ASM_SPICE_LITE } from './constants.js'; +import createModule from './cspice.js'; const INT_SIZE = 4; const INT_TYPE = 'i32'; @@ -21,33 +21,7 @@ export class Spice { } // initialize the module - init(type = ASM_SPICE_LITE) { - if (this.module !== null) { - throw new Error('Spice: Class already initialized with an existing Module.'); - } - - let promise; - switch(type) { - case ASM_SPICE_LITE: - promise = import('./cspice/asm_lite.js'); - break; - case ASM_SPICE_FULL: - promise = import('./cspice/asm_full.js'); - break; - default: - throw new Error(`Spice: Unsupported SPICE module type enumeration ${type}`); - } - - return promise.then(m => { - return this.initFromFactory(m.default); - }); - } - - initFromFactory(createModule) { - if (this.module !== null) { - throw new Error('Spice: Class already initialized with an existing Module.'); - } - + init() { return createModule({ print: (...args) => this.onStdOut(...args), printErr: (...args) => this.onStdErr(...args), @@ -1276,4 +1250,93 @@ export class Spice { return { ptarg, lt }; } + spkezr(targ, et, ref, abcorr, obs) { + const Module = this.module; + // create output pointers + const starg_ptr = Module._malloc(DOUBLE_SIZE * 6); + const lt_ptr = Module._malloc(DOUBLE_SIZE); + + Module.ccall( + 'spkezr_c', + null, + /* ConstSpiceChar targ, SpiceDouble et, ConstSpiceChar ref, ConstSpiceChar abcorr, ConstSpiceChar obs, SpiceDouble ptarg, SpiceDouble lt */ + [ 'string', 'number', 'string', 'string', 'string', 'number', 'number' ], + [ targ, et, ref, abcorr, obs, starg_ptr, lt_ptr ], + ); + + // read and free output pointers + const starg = [ + Module.getValue( starg_ptr + DOUBLE_SIZE * 0, 'double' ), + Module.getValue( starg_ptr + DOUBLE_SIZE * 1, 'double' ), + Module.getValue( starg_ptr + DOUBLE_SIZE * 2, 'double' ), + Module.getValue( starg_ptr + DOUBLE_SIZE * 3, 'double' ), + Module.getValue( starg_ptr + DOUBLE_SIZE * 4, 'double' ), + Module.getValue( starg_ptr + DOUBLE_SIZE * 5, 'double' ), + ]; + Module._free( starg_ptr ); + + const lt = Module.getValue( lt_ptr, 'double' ); + Module._free( lt_ptr ); + + return { starg, lt }; + } + + pxform(from, to, et) { + const Module = this.module; + // create output pointers + + const rot_ptr = Module._malloc(DOUBLE_SIZE * 9); + + Module.ccall( + 'pxform_c', + null, + /* ConstSpiceChar from, ConstSpiceChar to, SpiceDouble et, SpiceDouble rot */ + [ 'string', 'string', 'number', 'number'], + [ from, to, et, rot_ptr ], + ); + + // read and free output pointers + const rot = [ + Module.getValue( rot_ptr + DOUBLE_SIZE * 0, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 1, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 2, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 3, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 4, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 5, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 6, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 7, 'double' ), + Module.getValue( rot_ptr + DOUBLE_SIZE * 8, 'double' ), + ]; + Module._free( rot_ptr ); + + + return rot ; + } + + m2q(R) { + const Module = this.module; + // create output pointers + const quat_ptr = Module._malloc(DOUBLE_SIZE * 4); + + Module.ccall( + 'm2q_c', + null, + /* ConstSpiceChar from, ConstSpiceChar to, SpiceDouble et, SpiceDouble rot */ + [ 'number', 'number'], + [ R, quat_ptr ], + ); + + // read and free output pointers + const quat = [ + Module.getValue( quat_ptr + DOUBLE_SIZE * 0, 'double' ), + Module.getValue( quat_ptr + DOUBLE_SIZE * 1, 'double' ), + Module.getValue( quat_ptr + DOUBLE_SIZE * 2, 'double' ), + Module.getValue( quat_ptr + DOUBLE_SIZE * 3, 'double' ), + ]; + Module._free( quat_ptr ); + + + return { quat }; + } + } From e8c54b0337deae963087c12567eeba01b066c14a Mon Sep 17 00:00:00 2001 From: aescalante Date: Wed, 26 May 2021 19:02:02 +0200 Subject: [PATCH 2/3] update repo --- src/Spice.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Spice.js b/src/Spice.js index 1d12ef6..6c7bd5a 100644 --- a/src/Spice.js +++ b/src/Spice.js @@ -1,4 +1,4 @@ -import createModule from './cspice.js'; +import { ASM_SPICE_FULL, ASM_SPICE_LITE } from './constants.js'; const INT_SIZE = 4; const INT_TYPE = 'i32'; @@ -21,7 +21,33 @@ export class Spice { } // initialize the module - init() { + init(type = ASM_SPICE_LITE) { + if (this.module !== null) { + throw new Error('Spice: Class already initialized with an existing Module.'); + } + + let promise; + switch(type) { + case ASM_SPICE_LITE: + promise = import('./cspice/asm_lite.js'); + break; + case ASM_SPICE_FULL: + promise = import('./cspice/asm_full.js'); + break; + default: + throw new Error(`Spice: Unsupported SPICE module type enumeration ${type}`); + } + + return promise.then(m => { + return this.initFromFactory(m.default); + }); + } + + initFromFactory(createModule) { + if (this.module !== null) { + throw new Error('Spice: Class already initialized with an existing Module.'); + } + return createModule({ print: (...args) => this.onStdOut(...args), printErr: (...args) => this.onStdErr(...args), From d5591c654157b39d6791344ba4b1e885b935e11e Mon Sep 17 00:00:00 2001 From: aescalante Date: Thu, 27 May 2021 10:19:43 +0200 Subject: [PATCH 3/3] memory allocation for input array for exposing m2q_c --- src/Spice.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Spice.js b/src/Spice.js index 6c7bd5a..af4d638 100644 --- a/src/Spice.js +++ b/src/Spice.js @@ -1342,6 +1342,10 @@ export class Spice { m2q(R) { const Module = this.module; // create output pointers + const r_ptr = Module._malloc(DOUBLE_SIZE * 9); + for (let i = 0; i < 9; i++) { + Module.setValue(r_ptr + DOUBLE_SIZE * i, R[i], DOUBLE_TYPE); + } const quat_ptr = Module._malloc(DOUBLE_SIZE * 4); Module.ccall( @@ -1349,7 +1353,7 @@ export class Spice { null, /* ConstSpiceChar from, ConstSpiceChar to, SpiceDouble et, SpiceDouble rot */ [ 'number', 'number'], - [ R, quat_ptr ], + [ r_ptr, quat_ptr ], ); // read and free output pointers @@ -1362,7 +1366,7 @@ export class Spice { Module._free( quat_ptr ); - return { quat }; + return quat ; } }