@selvajs/compute 1.5.2-beta.1 → 1.5.2-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-2D74PNQU.cjs → chunk-6VF4VVLW.cjs} +2 -2
- package/dist/{chunk-2D74PNQU.cjs.map → chunk-6VF4VVLW.cjs.map} +1 -1
- package/dist/{chunk-XIBQV4XW.js → chunk-RILJ3IA7.js} +2 -2
- package/dist/grasshopper.cjs +1 -1
- package/dist/grasshopper.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{visualization-XXHESVUI.js → visualization-7TK4UEZL.js} +1 -1
- package/dist/visualization-7TK4UEZL.js.map +1 -0
- package/dist/{visualization-3HFOMXUY.cjs → visualization-JYNKROSH.cjs} +1 -1
- package/dist/visualization-JYNKROSH.cjs.map +1 -0
- package/package.json +1 -1
- package/dist/visualization-3HFOMXUY.cjs.map +0 -1
- package/dist/visualization-XXHESVUI.js.map +0 -1
- /package/dist/{chunk-XIBQV4XW.js.map → chunk-RILJ3IA7.js.map} +0 -0
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkMZGKJZVPcjs = require('./chunk-MZGKJZVP.cjs');var _chunkVWOEUM7Ccjs = require('./chunk-VWOEUM7C.cjs');function L(r){let e=new WeakSet,t=n=>{if(n==null)return JSON.stringify(n);if(typeof n=="number")return Number.isFinite(n)?String(n):JSON.stringify(null);if(typeof n=="string"||typeof n=="boolean")return JSON.stringify(n);if(typeof n=="bigint")return JSON.stringify(n.toString());if(n instanceof Uint8Array){let s=n.length>64?Array.from(n.slice(0,32)).concat(Array.from(n.slice(-32))):Array.from(n);return JSON.stringify({__u8:!0,len:n.length,sample:s})}return Array.isArray(n)?`[${n.map(t).join(",")}]`:typeof n=="object"?e.has(n)?JSON.stringify("[Circular]"):(e.add(n),`{${Object.keys(n).sort().map(a=>`${JSON.stringify(a)}:${t(n[a])}`).join(",")}}`):JSON.stringify(null)};return t(r)}function _(r){let e=2166136261;for(let t=0;t<r.length;t++)e^=r.charCodeAt(t),e=e+((e<<1)+(e<<4)+(e<<7)+(e<<8)+(e<<24))>>>0;return e.toString(16).padStart(8,"0")}function R(r,e){let t=typeof r=="string"?r:L({__u8:!0,len:r.length});return _(`${t}|${L(e)}`)}var b=class{constructor(e,t,n={}){_chunkVWOEUM7Ccjs.b.call(void 0, this,"executor");_chunkVWOEUM7Ccjs.b.call(void 0, this,"baseConfig");_chunkVWOEUM7Ccjs.b.call(void 0, this,"mode");_chunkVWOEUM7Ccjs.b.call(void 0, this,"maxConcurrent");_chunkVWOEUM7Ccjs.b.call(void 0, this,"timeoutMs");_chunkVWOEUM7Ccjs.b.call(void 0, this,"retry");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cacheEnabled");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cacheMax");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cacheTtl");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cache",new Map);_chunkVWOEUM7Ccjs.b.call(void 0, this,"onStart");_chunkVWOEUM7Ccjs.b.call(void 0, this,"onSettle");_chunkVWOEUM7Ccjs.b.call(void 0, this,"onSuperseded");_chunkVWOEUM7Ccjs.b.call(void 0, this,"subscribers",new Set);_chunkVWOEUM7Ccjs.b.call(void 0, this,"inFlight",new Set);_chunkVWOEUM7Ccjs.b.call(void 0, this,"pendingForLatestWins",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"fifoQueue",[]);_chunkVWOEUM7Ccjs.b.call(void 0, this,"_lastResult",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"_lastError",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"_lastDurationMs",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"disposed",!1);this.executor=e,this.baseConfig=t,this.mode=_nullishCoalesce(n.mode, () => ("latest-wins")),this.maxConcurrent=Math.max(1,_nullishCoalesce(n.maxConcurrent, () => ((this.mode==="parallel"?4:1)))),this.timeoutMs=n.timeoutMs,this.retry=n.retry;let s=n.cache;this.cacheEnabled=s!==void 0&&s!==!1;let o=typeof s=="object"?s:{};this.cacheMax=_nullishCoalesce(o.maxEntries, () => (50)),this.cacheTtl=_nullishCoalesce(o.ttlMs, () => (0)),this.onStart=n.onStart,this.onSettle=n.onSettle,this.onSuperseded=n.onSuperseded}get isSolving(){return this.inFlight.size>0}get hasPending(){return this.pendingForLatestWins!==null||this.fifoQueue.length>0}get inFlightCount(){return this.inFlight.size}get queueDepth(){return this.fifoQueue.length+(this.pendingForLatestWins?1:0)}get lastResult(){return this._lastResult}get lastError(){return this._lastError}get lastDurationMs(){return this._lastDurationMs}subscribe(e){return this.subscribers.add(e),()=>this.subscribers.delete(e)}notify(){for(let e of this.subscribers)try{e()}catch(t){_chunkVWOEUM7Ccjs.e.call(void 0, ).error("[SolveScheduler] subscriber threw:",t)}}solve(e,t,n){if(this.disposed)return Promise.reject(new (0, _chunkVWOEUM7Ccjs.d)("SolveScheduler has been disposed and cannot be used",_chunkVWOEUM7Ccjs.c.INVALID_STATE));let s=R(e,t),o={key:s,enqueuedAt:Date.now(),startedAt:null};if(this.cacheEnabled){let a=this.readCache(s);if(a){let u={status:"success",response:a,durationMs:0,fromCache:!0};return this._lastResult=a,this._lastError=null,this._lastDurationMs=0,this.runHook(this.onStart,o),this.runHook(this.onSettle,o,u),this.notify(),Promise.resolve(a)}}return new Promise((a,u)=>{let i={definition:e,dataTree:t,ctx:o,resolve:a,reject:u,externalSignal:_optionalChain([n, 'optionalAccess', _2 => _2.signal])};if(_optionalChain([i, 'access', _3 => _3.externalSignal, 'optionalAccess', _4 => _4.aborted])){let c=this.makeAbortError(o);i.settled={error:c},u(c);return}this.enqueue(i)})}enqueue(e){switch(this.mode){case"latest-wins":{this.pendingForLatestWins&&(this.supersede(this.pendingForLatestWins),this.pendingForLatestWins=null);for(let t of this.inFlight)this.supersede(t),t.controller.abort();this.inFlight.size===0?this.execute(e):this.pendingForLatestWins=e;break}case"queue":case"parallel":{this.inFlight.size<this.maxConcurrent?this.execute(e):this.fifoQueue.push(e);break}}this.notify()}async execute(e){let t=new AbortController,n={...e,controller:t};this.inFlight.add(n),e.ctx.startedAt=Date.now();let s=()=>t.abort();_optionalChain([e, 'access', _5 => _5.externalSignal, 'optionalAccess', _6 => _6.addEventListener, 'call', _7 => _7("abort",s,{once:!0})]),this.runHook(this.onStart,e.ctx),this.notify();let o=performance.now();try{let a={...this.baseConfig,signal:t.signal,...this.timeoutMs!==void 0&&{timeoutMs:this.timeoutMs},...this.retry!==void 0&&{retry:this.retry}},u=await this.executor(e.definition,e.dataTree,a),i=performance.now()-o;if(this.cacheEnabled&&this.writeCache(e.ctx.key,u),e.settled)return;e.settled={ok:!0},this._lastResult=u,this._lastError=null,this._lastDurationMs=i,e.resolve(u),this.runHook(this.onSettle,e.ctx,{status:"success",response:u,durationMs:i,fromCache:!1})}catch(a){let u=performance.now()-o,i=this.normalizeExecutionError(a,n),c=!!n.settled;this._lastError=i,this._lastDurationMs=u,c||(n.settled={error:i},e.reject(i),this.runHook(this.onSettle,e.ctx,{status:"error",error:i,durationMs:u}))}finally{_optionalChain([e, 'access', _8 => _8.externalSignal, 'optionalAccess', _9 => _9.removeEventListener, 'call', _10 => _10("abort",s)]),this.inFlight.delete(n),this.drainNext(),this.notify()}}drainNext(){if(!this.disposed){if(this.mode==="latest-wins"){if(this.pendingForLatestWins&&this.inFlight.size===0){let e=this.pendingForLatestWins;this.pendingForLatestWins=null,this.execute(e)}return}for(;this.fifoQueue.length>0&&this.inFlight.size<this.maxConcurrent;){let e=this.fifoQueue.shift();this.execute(e)}}}supersede(e){if(e.settled)return;let t=new (0, _chunkVWOEUM7Ccjs.d)("Superseded by newer solve",_chunkVWOEUM7Ccjs.c.SUPERSEDED,{context:{key:e.ctx.key,enqueuedAt:e.ctx.enqueuedAt}});e.settled={error:t},e.reject(t),this.runHook(this.onSuperseded,e.ctx)}makeAbortError(e){return new (0, _chunkVWOEUM7Ccjs.d)("Request aborted by caller",_chunkVWOEUM7Ccjs.c.ABORTED,{context:{key:e.key,enqueuedAt:e.enqueuedAt}})}isAbortLikeError(e){if(e instanceof Error){if(e.name==="AbortError")return!0;if(typeof DOMException<"u"&&e instanceof DOMException)return e.name==="AbortError"}return!1}normalizeExecutionError(e,t){return t.settled&&"error"in t.settled?t.settled.error:e instanceof _chunkVWOEUM7Ccjs.d?e:this.isAbortLikeError(e)?this.makeAbortError(t.ctx):new (0, _chunkVWOEUM7Ccjs.d)(e instanceof Error?e.message:String(e),_chunkVWOEUM7Ccjs.c.UNKNOWN_ERROR,{originalError:e instanceof Error?e:new Error(String(e))})}cancelAll(){for(this.pendingForLatestWins&&(this.rejectAsAborted(this.pendingForLatestWins),this.pendingForLatestWins=null);this.fifoQueue.length>0;){let e=this.fifoQueue.shift();this.rejectAsAborted(e)}for(let e of this.inFlight){if(!e.settled){let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t),this.runHook(this.onSettle,e.ctx,{status:"error",error:t,durationMs:e.ctx.startedAt?performance.now()-e.ctx.startedAt:0})}e.controller.abort()}this.notify()}rejectAsAborted(e){if(e.settled)return;let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t)}readCache(e){if(!this.cacheEnabled)return null;let t=this.cache.get(e);return t?this.cacheTtl>0&&Date.now()-t.insertedAt>this.cacheTtl?(this.cache.delete(e),null):(this.cache.delete(e),this.cache.set(e,t),t.response):null}writeCache(e,t){if(this.cacheEnabled)for(this.cache.set(e,{response:t,insertedAt:Date.now()});this.cache.size>this.cacheMax;){let n=this.cache.keys().next().value;if(n===void 0)break;this.cache.delete(n)}}clearCache(){this.cache.clear()}dispose(){this.disposed||(this.disposed=!0,this.cancelAll(),this.subscribers.clear(),this.cache.clear())}runHook(e,...t){if(e)try{e(...t)}catch(n){_chunkVWOEUM7Ccjs.e.call(void 0, ).error("[SolveScheduler] hook threw:",n)}}};var x=class r{constructor(e){_chunkVWOEUM7Ccjs.b.call(void 0, this,"config");_chunkVWOEUM7Ccjs.b.call(void 0, this,"serverStats");_chunkVWOEUM7Ccjs.b.call(void 0, this,"disposed",!1);this.config=this.normalizeComputeConfig(e),this.serverStats=new (0, _chunkVWOEUM7Ccjs.i)(this.config.serverUrl,this.config.apiKey)}static async create(e){let t=new r(e);if(!await t.serverStats.isServerOnline())throw new (0, _chunkVWOEUM7Ccjs.d)("Rhino Compute server is not online",_chunkVWOEUM7Ccjs.c.NETWORK_ERROR,{context:{serverUrl:t.config.serverUrl}});return t}getConfig(){return this.ensureNotDisposed(),{...this.config}}async getIO(e){return this.ensureNotDisposed(),E(e,this.config)}async getRawIO(e){return this.ensureNotDisposed(),I(e,this.config)}async solve(e,t,n){this.ensureNotDisposed();try{if(typeof e=="string"&&!_optionalChain([e, 'optionalAccess', _11 => _11.trim, 'call', _12 => _12()]))throw new (0, _chunkVWOEUM7Ccjs.d)("Definition URL/content is required",_chunkVWOEUM7Ccjs.c.INVALID_INPUT,{context:{receivedUrl:e}});if(e instanceof Uint8Array&&e.length===0)throw new (0, _chunkVWOEUM7Ccjs.d)("Definition content is empty",_chunkVWOEUM7Ccjs.c.INVALID_INPUT);let s={...this.config,..._optionalChain([n, 'optionalAccess', _13 => _13.signal])!==void 0&&{signal:n.signal},..._optionalChain([n, 'optionalAccess', _14 => _14.timeoutMs])!==void 0&&{timeoutMs:n.timeoutMs},..._optionalChain([n, 'optionalAccess', _15 => _15.retry])!==void 0&&{retry:n.retry}},o=await C(t,e,s);if(_optionalChain([o, 'optionalAccess', _16 => _16.errors])&&o.errors.length>0)throw new (0, _chunkVWOEUM7Ccjs.d)(o.errors.join("; ")||"Computation failed",_chunkVWOEUM7Ccjs.c.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t,errors:o.errors,warnings:o.warnings}});return o}catch(s){throw this.config.debug&&_chunkVWOEUM7Ccjs.e.call(void 0, ).error("Compute failed:",s),s instanceof _chunkVWOEUM7Ccjs.d?s:new (0, _chunkVWOEUM7Ccjs.d)(s instanceof Error?s.message:String(s),_chunkVWOEUM7Ccjs.c.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t},originalError:s instanceof Error?s:new Error(String(s))})}}createScheduler(e){this.ensureNotDisposed();let t=(n,s,o)=>C(s,n,o);return new b(t,this.config,e)}async dispose(){this.disposed||(this.disposed=!0,await this.serverStats.dispose())}ensureNotDisposed(){if(this.disposed)throw new (0, _chunkVWOEUM7Ccjs.d)("GrasshopperClient has been disposed and cannot be used",_chunkVWOEUM7Ccjs.c.INVALID_STATE)}normalizeComputeConfig(e){if(!_optionalChain([e, 'access', _17 => _17.serverUrl, 'optionalAccess', _18 => _18.trim, 'call', _19 => _19()]))throw new (0, _chunkVWOEUM7Ccjs.d)("serverUrl is required",_chunkVWOEUM7Ccjs.c.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});try{new URL(e.serverUrl)}catch (e2){throw new (0, _chunkVWOEUM7Ccjs.d)("serverUrl must be a valid URL",_chunkVWOEUM7Ccjs.c.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}})}if(e.serverUrl===""||e.serverUrl==="https://compute.rhino3d.com/")throw new (0, _chunkVWOEUM7Ccjs.d)("serverUrl must be set to your Compute server URL. The default public endpoint is not allowed.",_chunkVWOEUM7Ccjs.c.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});return{...e,serverUrl:e.serverUrl.replace(/\/+$/,""),apiKey:e.apiKey,authToken:e.authToken,debug:_nullishCoalesce(e.debug, () => (!1)),suppressBrowserWarning:_nullishCoalesce(e.suppressBrowserWarning, () => (e.suppressClientSideWarning))}}};var J=async(r,e=null)=>{try{return await H(r,e)}catch(t){throw new (0, _chunkVWOEUM7Ccjs.d)("Failed to extract files from compute response",_chunkVWOEUM7Ccjs.c.INVALID_STATE,{context:{originalError:t instanceof Error?t.message:String(t)},originalError:t instanceof Error?t:void 0})}},w= exports.e =async(r,e,t=null)=>{if(typeof document>"u"||typeof Blob>"u")throw new (0, _chunkVWOEUM7Ccjs.d)("File download functionality is only available in browser environments. This function requires the DOM API (document, Blob).",_chunkVWOEUM7Ccjs.c.BROWSER_ONLY,{context:{environment:typeof window<"u"?"browser (SSR)":"Node.js",documentAvailable:typeof document<"u",blobAvailable:typeof Blob<"u"}});try{let n=await H(r,t);await le(n,e)}catch(n){throw n instanceof _chunkVWOEUM7Ccjs.d?n:new (0, _chunkVWOEUM7Ccjs.d)("Failed to download files from compute response",_chunkVWOEUM7Ccjs.c.INVALID_STATE,{context:{originalError:n instanceof Error?n.message:String(n)},originalError:n instanceof Error?n:void 0})}},H=async(r,e)=>{let t=[];if(r.forEach(n=>{let s=`${n.fileName}${n.fileType}`;if(n.subFolder&&n.subFolder.trim()!==""&&(s=`${n.subFolder}/${s}`),n.isBase64Encoded===!0&&n.data){let o=_chunkMZGKJZVPcjs.c.call(void 0, n.data);t.push({fileName:`${n.fileName}${n.fileType}`,content:new Uint8Array(o.buffer),path:s})}else n.isBase64Encoded===!1&&n.data&&t.push({fileName:`${n.fileName}${n.fileType}`,content:n.data,path:s})}),e){let n=Array.isArray(e)?e:[e],s=await Promise.all(n.map(async o=>{try{let a=await fetch(o.filePath);if(!a.ok)return _chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to fetch additional file from URL: ${o.filePath}`),null;let i=await(await a.blob()).arrayBuffer();return{fileName:o.fileName,content:new Uint8Array(i),path:o.fileName}}catch(a){return _chunkVWOEUM7Ccjs.e.call(void 0, ).error(`Error fetching additional file from URL: ${o.filePath}`,a),null}}));t.push(...s.filter(o=>o!==null))}return t};async function le(r,e){let{zipSync:t,strToU8:n}=await Promise.resolve().then(() => _interopRequireWildcard(require("fflate"))),s={};r.forEach(u=>{s[u.path]=typeof u.content=="string"?n(u.content):u.content});let o=t(s,{level:6}),a=new Blob([o],{type:"application/zip"});pe(a,`${e}.zip`)}function pe(r,e){if(typeof document>"u")throw new (0, _chunkVWOEUM7Ccjs.d)("saveFile requires a browser environment with DOM API access.",_chunkVWOEUM7Ccjs.c.BROWSER_ONLY,{context:{function:"saveFile",requiredAPI:"document"}});let t=document.createElement("a");t.href=URL.createObjectURL(r),t.download=e,t.click(),URL.revokeObjectURL(t.href)}var A=new Map;function K(r,e){A.set(r,e)}K("Rhino.Geometry.Point3d",(r,e)=>{let t=e;return!t||typeof t.X!="number"?null:new r.Point([t.X,t.Y,t.Z])});K("Rhino.Geometry.Line",(r,e)=>{let t=e;return!t||!t.From||!t.To?null:new r.Line([t.From.X,t.From.Y,t.From.Z],[t.To.X,t.To.Y,t.To.Z])});function ce(r){if(A.has(r))return A.get(r);for(let[e,t]of A)if(r.startsWith(e))return t}function fe(r){return!r||typeof r!="object"?null:_nullishCoalesce(_nullishCoalesce(r.data, () => (r.value)), () => (null))}function Y(r,e,t){let n=ce(e);if(n)try{return n(t,r)}catch(s){_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to decode Rhino type ${e}:`,s)}try{let s=fe(r);if(s)return t.CommonObject.decode(s)}catch(s){return _chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to decode ${e} with CommonObject:`,s),{__decodeError:!0,type:e,raw:r}}return r}var T={STRING:"System.String",INT:"System.Int32",DOUBLE:"System.Double",BOOL:"System.Boolean"},me="Rhino.Geometry.",de=["WebDisplay"],he="FileData";function Q(r){return de.some(e=>r.includes(e))}function X(r){if(typeof r!="string")return r;let e=r.trim();if(!(e.startsWith("{")||e.startsWith("[")||e.startsWith('"')))return r;try{let n=JSON.parse(e);if(typeof n=="string")try{return JSON.parse(n)}catch (e3){return n}return n}catch (e4){return r}}function ye(r,e,t){switch(e){case T.STRING:return typeof r!="string"?r:r.replace(/^"(.*)"$/,"$1");case T.INT:return Number.parseInt(r,10);case T.DOUBLE:return Number.parseFloat(r);case T.BOOL:return String(r).toLowerCase()==="true";default:return t&&e.startsWith(me)?Y(r,e,t):r}}function Z(r,e,t,n){if(typeof r!="string")return r;let s=t?X(r):r;return ye(s,e,n)}function ge(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.fileName=="string"&&typeof e.fileType=="string"&&"data"in e&&typeof e.isBase64Encoded=="boolean"&&typeof e.subFolder=="string"}function F(r,e){for(let t of Object.values(r))if(Array.isArray(t))for(let n of t)e(n)}function ee(r,e=!1,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a={};for(let u of r.values)F(u.InnerTree,i=>{if(Q(i.type)||o&&i.type!==T.STRING)return;let c=e?i.id:u.ParamName;if(!c)return;let d=Z(i.data,i.type,n,s);a[c]===void 0?a[c]=d:Array.isArray(a[c])?a[c].push(d):a[c]=[a[c],d]});return{values:a}}function re(r){let e=[];for(let t of r.values)F(t.InnerTree,n=>{if(!n.type.includes(he))return;let s=X(n.data);ge(s)&&e.push(s)});return e}function O(r,e,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a;if("byName"in e?a=r.values.find(i=>i.ParamName===e.byName):a=r.values.find(i=>{let c=!1;return F(i.InnerTree,d=>{d.id===e.byId&&(c=!0)}),c}),!a)return;let u=[];if(F(a.InnerTree,i=>{if("byId"in e&&i.id!==e.byId||Q(i.type)||o&&i.type!==T.STRING)return;let c=Z(i.data,i.type,n,s);u.push(c)}),u.length!==0)return u.length===1?u[0]:u}var D=class{constructor(e,t=!1){this.response=e;this.debug=t}getValues(e=!1,t={}){return ee(this.response,e,t)}getValue(e,t){return O(this.response,e,t)}getValueByParamName(e,t){return O(this.response,{byName:e},t)}getValueByParamId(e,t){return O(this.response,{byId:e},t)}async extractMeshesFromResponse(e){let t={debug:this.debug,...e},n;try{({getThreeMeshesFromComputeResponse:n}=await Promise.resolve().then(() => _interopRequireWildcard(require("./visualization-3HFOMXUY.cjs"))))}catch(s){throw new (0, _chunkVWOEUM7Ccjs.d)("Failed to load three.js visualization module. Ensure three.js is installed as a peer dependency.",_chunkVWOEUM7Ccjs.c.INVALID_STATE,{context:{originalError:s instanceof Error?s.message:String(s)}})}return n(this.response,t)}getFileData(){return re(this.response)}getAndDownloadFiles(e,t){let n=this.getFileData();w(n,e,t)}};function N(r,e){e||typeof window<"u"&&_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Warning: ${r} is running on the client side. For better performance and security, consider running this on the server side.`)}async function C(r,e,t){t.debug&&N("solveGrasshopperDefinition",_nullishCoalesce(t.suppressBrowserWarning, () => (t.suppressClientSideWarning)));let n=M(e,r);be(n,t);let s=await _chunkVWOEUM7Ccjs.h.call(void 0, "grasshopper",n,t);if("pointer"in s){let{pointer:o,...a}=s;return a}return s}function M(r,e){let t={algo:null,pointer:null,values:e};return r instanceof Uint8Array?t.algo=_chunkMZGKJZVPcjs.d.call(void 0, r):/^https?:\/\//i.test(r)?t.pointer=r:_chunkMZGKJZVPcjs.b.call(void 0, r)?t.algo=r:t.algo=_chunkMZGKJZVPcjs.a.call(void 0, r),t}function be(r,e){e.cachesolve!=null&&(r.cachesolve=e.cachesolve),e.modelunits!=null&&(r.modelunits=e.modelunits),e.angletolerance!=null&&(r.angletolerance=e.angletolerance),e.absolutetolerance!=null&&(r.absolutetolerance=e.absolutetolerance),e.dataversion!=null&&(r.dataversion=e.dataversion)}function te(r){if(typeof r.default!="object"||r.default===null)return;if(!("innerTree"in r.default)){_chunkVWOEUM7Ccjs.e.call(void 0, ).warn("Unexpected structure in input.default:",r.default),r.default=null;return}let e=r.default.innerTree;if(Object.keys(e).length===0){r.default=void 0;return}if(r.treeAccess||r.atMost&&r.atMost>1){let n={};for(let[s,o]of Object.entries(e))n[s]=o.map(a=>{if(typeof a.data=="string"){if(a.type==="System.Double"||a.type==="System.Int32"){let u=Number(a.data);return Number.isNaN(u)?a.data:u}if(a.type==="System.Boolean")return a.data.toLowerCase()==="true";if(a.type.startsWith("Rhino.Geometry")||a.type==="System.String")try{return JSON.parse(a.data)}catch (e5){return a.data}}return a.data});r.default=n;return}let t=[];for(let n of Object.values(e))Array.isArray(n)&&n.forEach(s=>{s&&typeof s=="object"&&"data"in s&&t.push(s.data)});t.length===0?r.default=void 0:t.length===1?r.default=t[0]:r.default=t}function P(r,e){let{transform:t,setUndefinedOnEmpty:n=!0}=e;if(!(r.default===void 0||r.default===null))if(Array.isArray(r.default)){let s=r.default.map(t).filter(o=>o!==null);r.default=s.length>0?s:void 0}else{let s=t(r.default);s!==null?r.default=s:n&&(r.default=void 0)}}function Te(){return r=>{if(typeof r=="number")return r;if(typeof r=="string"){let e=Number(r.trim());return Number.isNaN(e)?null:e}return null}}function Se(){return r=>{if(typeof r=="boolean")return r;if(typeof r=="string"){let e=r.toLowerCase();if(e==="true")return!0;if(e==="false")return!1;throw new Error(`Invalid boolean string: "${r}"`)}return null}}function xe(){return r=>typeof r=="string"?r.startsWith('"')&&r.endsWith('"')||r.startsWith('"')?r.slice(1,-1):r:null}function Ce(){return r=>{if(typeof r=="string"){let e=r.trim();return e.startsWith('"')&&e.endsWith('"')&&(e=e.slice(1,-1).trim()),e}return null}}function Ie(r){P(r,{transform:Ce(),setUndefinedOnEmpty:!1})}function De(r="unknown"){return e=>{if(typeof e=="object"&&e!==null)return e;if(typeof e=="string"&&e.trim()!=="")try{let t=JSON.parse(e);return typeof t=="object"&&t!==null?t:(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Parsed value for input ${r} is not an object`),null)}catch(t){return _chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to parse object value "${e}" for input ${r}`,t),null}return null}}function ne(r,e,t){let n=Number(r.toFixed(e));return Math.abs(r-n)<t?n:r}function Pe(r,e=1e-8){if(!Number.isFinite(r)||r===0)return .1;let t=Math.abs(r);if(t>=1){let h=String(r).split(".")[1];if(h&&h.length>0){let y=Math.min(h.length,12),g=Math.pow(10,-y),B=Number(g.toFixed(y));return Math.abs(B-g)<e?B:g}return 1}let n=String(r),s=n.toLowerCase().match(/e(-?\d+)/);if(s){let S=Number(s[1]);if(S<0||n.toLowerCase().includes("e-")){let h=Math.abs(S),y=Math.pow(10,-h),g=Number(y.toFixed(h));return Math.abs(g-y)<e?g:y}return .1}let o=12,u=t.toFixed(o).replace(/0+$/,""),i=Math.min((u.split(".")[1]||"").length,o);if(i===0)return .1;let c=Math.pow(10,-i),d=Number(c.toFixed(i));return Math.abs(d-c)<e?d:c}function se(r,e=1e-8){let t=r.paramType==="Integer";if(P(r,{transform:Te()}),t){Array.isArray(r.default)?r.default=r.default.map(o=>typeof o=="number"?Math.round(o):o):typeof r.default=="number"&&(r.default=Math.round(r.default)),r.stepSize=1;return}let n=Array.isArray(r.default)?r.default[0]:r.default,s;if(typeof n=="number"&&Number.isFinite(n)&&n!==0?s=n:typeof r.minimum=="number"&&Number.isFinite(r.minimum)&&r.minimum!==0?s=r.minimum:typeof r.maximum=="number"&&Number.isFinite(r.maximum)&&r.maximum!==0&&(s=r.maximum),s!==void 0?r.stepSize=Pe(s,e):r.stepSize=.1,typeof r.stepSize=="number"){let o=0,a=String(r.stepSize),u=a.toLowerCase().match(/e(-?\d+)/);if(u?o=Math.abs(Number(u[1])):o=_nullishCoalesce(_optionalChain([a, 'access', _20 => _20.split, 'call', _21 => _21("."), 'access', _22 => _22[1], 'optionalAccess', _23 => _23.length]), () => (0)),o===0&&typeof n=="number"&&n!==0&&Math.abs(n)<1){let i=Math.ceil(-Math.log10(Math.abs(n)));Number.isFinite(i)&&i>0&&(o=i)}o=Math.min(Math.max(o,0),12),Array.isArray(r.default)?r.default=r.default.map(i=>typeof i=="number"?ne(i,o,e):i):typeof r.default=="number"&&(r.default=ne(r.default,o,e))}}function ve(r){try{P(r,{transform:Se(),setUndefinedOnEmpty:!1})}catch(e){throw e instanceof Error?new (0, _chunkVWOEUM7Ccjs.d)(e.message):e}}function Re(r){P(r,{transform:xe(),setUndefinedOnEmpty:!1})}function oe(r){P(r,{transform:De(r.nickname||"unnamed"),setUndefinedOnEmpty:!0})}function Ee(r){if(!r.values||typeof r.values!="object"||Object.keys(r.values).length===0)throw _chunkVWOEUM7Ccjs.d.missingValues(r.nickname||"unnamed","ValueList");if(r.default!==void 0&&r.default!==null){let e=String(r.default).toLowerCase();Object.keys(r.values).some(n=>n.toLowerCase()===e)||_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`ValueList input "${r.nickname||"unnamed"}" default value "${r.default}" is not in available values`)}}var ae={Number:se,Integer:se,Boolean:ve,Text:Re,ValueList:Ee,Geometry:oe,File:oe,Color:Ie};function we(r,e){let t=(_nullishCoalesce(r.atMost, () => (1)))>1;switch(r.paramType){case"Number":case"Integer":return{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,default:t?[0]:0};case"Boolean":return{...e,paramType:"Boolean",default:t?[!1]:!1};case"Text":return{...e,paramType:"Text",default:t?[""]:""};case"ValueList":return{...e,paramType:"ValueList",values:_nullishCoalesce(r.values, () => ({})),default:t?[r.default]:r.default};case"File":return{...e,paramType:"File",default:t?[null]:null};case"Color":return{...e,paramType:"Color",default:t?["0, 0, 0"]:"0, 0, 0"};default:return{...e,paramType:"Geometry",default:t?[null]:null}}}function ie(r){return k(r).input}function k(r){let e={description:r.description,name:r.name,nickname:r.nickname,treeAccess:r.treeAccess,groupName:_nullishCoalesce(r.groupName, () => ("")),id:r.id};try{te(r);let t=ae[r.paramType];if(!t)throw _chunkVWOEUM7Ccjs.d.unknownParamType(r.paramType,r.name);switch(t(r),r.paramType){case"Number":case"Integer":return{input:{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,stepSize:r.stepSize,default:r.default}};case"Boolean":return{input:{...e,paramType:"Boolean",default:r.default}};case"Text":return{input:{...e,paramType:"Text",default:r.default}};case"ValueList":return{input:{...e,paramType:"ValueList",values:r.values,default:r.default}};case"Geometry":return{input:{...e,paramType:r.paramType,default:r.default}};case"File":return{input:{...e,paramType:r.paramType,acceptedFormats:r.acceptedFormats,default:r.default}};case"Color":return{input:{...e,paramType:"Color",default:r.default}};default:throw _chunkVWOEUM7Ccjs.d.unknownParamType(r.paramType,r.name)}}catch(t){if(t instanceof _chunkVWOEUM7Ccjs.d)return _chunkVWOEUM7Ccjs.e.call(void 0, ).error(`Validation error for input ${r.name||"unknown"}:`,t.message),{input:we(r,e),error:{inputName:r.name||"unknown",paramType:r.paramType,message:t.message,code:t.code}};throw new (0, _chunkVWOEUM7Ccjs.d)(t instanceof Error?t.message:String(t),"VALIDATION_ERROR",{context:{paramName:r.name,paramType:r.paramType},originalError:t instanceof Error?t:new Error(String(t))})}}function ue(r){return V(r).inputs}function V(r){let e=[],t=[];for(let n of r){let{input:s,error:o}=k(n);e.push(s),o&&t.push(o)}return{inputs:e,parseErrors:t}}async function I(r,e){let t=M(r,[]),n={};if(t.algo&&(n.algo=t.algo),t.pointer&&(n.pointer=t.pointer),!n.algo&&!n.pointer)throw new (0, _chunkVWOEUM7Ccjs.d)("Definition must resolve to either a URL pointer or base64 algo",_chunkVWOEUM7Ccjs.c.INVALID_INPUT,{context:{definition:r}});let s=await _chunkVWOEUM7Ccjs.h.call(void 0, "io",n,e);if(!s||typeof s!="object")throw new (0, _chunkVWOEUM7Ccjs.d)("Invalid IO response structure",_chunkVWOEUM7Ccjs.c.INVALID_INPUT,{context:{response:s,definition:r}});let o=_chunkVWOEUM7Ccjs.k.call(void 0, s,{deep:!0});return{inputs:o.inputs,outputs:o.outputs}}async function E(r,e){N("fetchParsedDefinitionIO",_nullishCoalesce(e.suppressBrowserWarning, () => (e.suppressClientSideWarning)));let{inputs:t,outputs:n}=await I(r,e),{inputs:s,parseErrors:o}=V(t);return o.length>0?{inputs:s,outputs:n,parseErrors:o}:{inputs:s,outputs:n}}var G=class r{constructor(e){_chunkVWOEUM7Ccjs.b.call(void 0, this,"innerTree");_chunkVWOEUM7Ccjs.b.call(void 0, this,"paramName");this.paramName=e,this.innerTree={}}append(e,t){let n=r.formatPathString(e);this.innerTree[n]||(this.innerTree[n]=[]);let s=t.map(o=>({data:r.serializeValue(o)}));return this.innerTree[n].push(...s),this}appendSingle(e,t){return this.append(e,[t])}fromDataTreeDefault(e){this.innerTree={};for(let[t,n]of Object.entries(e)){if(!Array.isArray(n))continue;let s=r.parsePathString(t);this.append(s,n)}return this}appendFlat(e){let t=Array.isArray(e)?e:[e];return this.append([0],t)}flatten(){let e=[];for(let t of Object.values(this.innerTree))if(Array.isArray(t))for(let n of t)e.push(r.deserializeValue(n.data));return e}getPaths(){return Object.keys(this.innerTree)}getPath(e){let t=r.formatPathString(e),n=this.innerTree[t];if(n)return n.map(s=>r.deserializeValue(s.data))}toComputeFormat(){return{ParamName:this.paramName,InnerTree:this.innerTree}}getInnerTree(){return this.innerTree}getParamName(){return this.paramName}static fromInputParams(e){return e.filter(t=>r.hasValidValue(t.default)).map(t=>{let n=new r(t.nickname||"unnamed"),s=t.default;if(t.treeAccess&&r.isDataTreeStructure(s))n.fromDataTreeDefault(s),r.isNumericInput(t)&&n.applyNumericConstraints(t.minimum,t.maximum,t.nickname||"unnamed");else{let o=Array.isArray(s)?s:[s],a=r.processValues(o,t);n.appendFlat(a)}return n.toComputeFormat()})}static fromInputParam(e){return r.hasValidValue(e.default)?r.fromInputParams([e])[0]:void 0}static replaceTreeValue(e,t,n){let s=e.length>0&&e[0]instanceof r,o=r.buildFromValue(t,n);if(s){let c=e,d=c.findIndex(S=>S.getParamName()===t);return d!==-1?c[d]=o:c.push(o),c}let a=e,u=o.toComputeFormat(),i=a.findIndex(c=>c.ParamName===t);return i!==-1?a[i]=u:a.push(u),a}static buildFromValue(e,t){let n=new r(e);return typeof t=="object"&&t!==null&&!Array.isArray(t)&&r.isDataTreeStructure(t)?n.fromDataTreeDefault(t):n.appendFlat(t),n}static getTreeValue(e,t){let s=e.length>0&&e[0]instanceof r?r.readFromBuilders(e,t):r.readFromDataTrees(e,t);return s===null||s.length===0?null:s.length===1?s[0]:s}static readFromBuilders(e,t){let n=e.find(s=>s.getParamName()===t);return n?n.flatten():null}static readFromDataTrees(e,t){let n=e.find(a=>a.ParamName===t);if(!_optionalChain([n, 'optionalAccess', _24 => _24.InnerTree]))return null;let s=Object.keys(n.InnerTree)[0];if(!s)return null;let o=n.InnerTree[s];return Array.isArray(o)?o.map(a=>_optionalChain([a, 'optionalAccess', _25 => _25.data])!==void 0?r.deserializeValue(a.data):null).filter(a=>a!==null):_optionalChain([o, 'optionalAccess', _26 => _26.data])!==void 0?[r.deserializeValue(o.data)]:o!==void 0?[o]:null}static parsePathString(e){let t=e.match(/^\{([\d;]*)\}$/);return t?t[1]===""?[]:t[1].split(";").map(Number):(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Invalid TreeBuilder path format: ${e}, using [0]`),[0])}static formatPathString(e){return`{${e.join(";")}}`}applyNumericConstraints(e,t,n){for(let s of Object.values(this.innerTree))if(Array.isArray(s))for(let o of s){let a=r.deserializeValue(o.data);if(typeof a=="number"){let u=r.clampValue(a,e,t,n);o.data=r.serializeValue(u)}}}static serializeValue(e){return typeof e=="boolean"||typeof e=="number"||typeof e=="string"?e:typeof e=="object"&&e!==null?JSON.stringify(e):String(e)}static deserializeValue(e){if(typeof e=="boolean"||typeof e=="number"||typeof e!="string")return e;if(e.startsWith("{")||e.startsWith("["))try{return JSON.parse(e)}catch (e6){return e}return isNaN(Number(e))?e==="true"?!0:e==="false"?!1:e:Number(e)}static hasValidValue(e){return e==null?!1:typeof e=="string"?!0:!(Array.isArray(e)&&e.length===0||typeof e=="object"&&!Array.isArray(e)&&Object.keys(e).length===0)}static isDataTreeStructure(e){return typeof e!="object"||e===null||Array.isArray(e)?!1:Object.entries(e).every(([t,n])=>typeof t=="string"&&/^\{[\d;]+\}$/.test(t)&&Array.isArray(n))}static isNumericInput(e){return e.paramType==="Number"||e.paramType==="Integer"}static processValues(e,t){return e.map(n=>r.isNumericInput(t)&&typeof n=="number"?r.clampValue(n,t.minimum,t.maximum,t.nickname||"unnamed"):n).filter(n=>n!=null)}static clampValue(e,t,n,s){let o=e;return t!=null&&o<t&&(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`${s}: ${e} below min ${t}, clamping`),o=t),n!=null&&o>n&&(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`${s}: ${e} above max ${n}, clamping`),o=n),o}};exports.a = R; exports.b = b; exports.c = x; exports.d = J; exports.e = w; exports.f = D; exports.g = C; exports.h = ie; exports.i = ue; exports.j = I; exports.k = E; exports.l = G;
|
|
2
|
-
//# sourceMappingURL=chunk-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkMZGKJZVPcjs = require('./chunk-MZGKJZVP.cjs');var _chunkVWOEUM7Ccjs = require('./chunk-VWOEUM7C.cjs');function L(r){let e=new WeakSet,t=n=>{if(n==null)return JSON.stringify(n);if(typeof n=="number")return Number.isFinite(n)?String(n):JSON.stringify(null);if(typeof n=="string"||typeof n=="boolean")return JSON.stringify(n);if(typeof n=="bigint")return JSON.stringify(n.toString());if(n instanceof Uint8Array){let s=n.length>64?Array.from(n.slice(0,32)).concat(Array.from(n.slice(-32))):Array.from(n);return JSON.stringify({__u8:!0,len:n.length,sample:s})}return Array.isArray(n)?`[${n.map(t).join(",")}]`:typeof n=="object"?e.has(n)?JSON.stringify("[Circular]"):(e.add(n),`{${Object.keys(n).sort().map(a=>`${JSON.stringify(a)}:${t(n[a])}`).join(",")}}`):JSON.stringify(null)};return t(r)}function _(r){let e=2166136261;for(let t=0;t<r.length;t++)e^=r.charCodeAt(t),e=e+((e<<1)+(e<<4)+(e<<7)+(e<<8)+(e<<24))>>>0;return e.toString(16).padStart(8,"0")}function R(r,e){let t=typeof r=="string"?r:L({__u8:!0,len:r.length});return _(`${t}|${L(e)}`)}var b=class{constructor(e,t,n={}){_chunkVWOEUM7Ccjs.b.call(void 0, this,"executor");_chunkVWOEUM7Ccjs.b.call(void 0, this,"baseConfig");_chunkVWOEUM7Ccjs.b.call(void 0, this,"mode");_chunkVWOEUM7Ccjs.b.call(void 0, this,"maxConcurrent");_chunkVWOEUM7Ccjs.b.call(void 0, this,"timeoutMs");_chunkVWOEUM7Ccjs.b.call(void 0, this,"retry");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cacheEnabled");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cacheMax");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cacheTtl");_chunkVWOEUM7Ccjs.b.call(void 0, this,"cache",new Map);_chunkVWOEUM7Ccjs.b.call(void 0, this,"onStart");_chunkVWOEUM7Ccjs.b.call(void 0, this,"onSettle");_chunkVWOEUM7Ccjs.b.call(void 0, this,"onSuperseded");_chunkVWOEUM7Ccjs.b.call(void 0, this,"subscribers",new Set);_chunkVWOEUM7Ccjs.b.call(void 0, this,"inFlight",new Set);_chunkVWOEUM7Ccjs.b.call(void 0, this,"pendingForLatestWins",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"fifoQueue",[]);_chunkVWOEUM7Ccjs.b.call(void 0, this,"_lastResult",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"_lastError",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"_lastDurationMs",null);_chunkVWOEUM7Ccjs.b.call(void 0, this,"disposed",!1);this.executor=e,this.baseConfig=t,this.mode=_nullishCoalesce(n.mode, () => ("latest-wins")),this.maxConcurrent=Math.max(1,_nullishCoalesce(n.maxConcurrent, () => ((this.mode==="parallel"?4:1)))),this.timeoutMs=n.timeoutMs,this.retry=n.retry;let s=n.cache;this.cacheEnabled=s!==void 0&&s!==!1;let o=typeof s=="object"?s:{};this.cacheMax=_nullishCoalesce(o.maxEntries, () => (50)),this.cacheTtl=_nullishCoalesce(o.ttlMs, () => (0)),this.onStart=n.onStart,this.onSettle=n.onSettle,this.onSuperseded=n.onSuperseded}get isSolving(){return this.inFlight.size>0}get hasPending(){return this.pendingForLatestWins!==null||this.fifoQueue.length>0}get inFlightCount(){return this.inFlight.size}get queueDepth(){return this.fifoQueue.length+(this.pendingForLatestWins?1:0)}get lastResult(){return this._lastResult}get lastError(){return this._lastError}get lastDurationMs(){return this._lastDurationMs}subscribe(e){return this.subscribers.add(e),()=>this.subscribers.delete(e)}notify(){for(let e of this.subscribers)try{e()}catch(t){_chunkVWOEUM7Ccjs.e.call(void 0, ).error("[SolveScheduler] subscriber threw:",t)}}solve(e,t,n){if(this.disposed)return Promise.reject(new (0, _chunkVWOEUM7Ccjs.d)("SolveScheduler has been disposed and cannot be used",_chunkVWOEUM7Ccjs.c.INVALID_STATE));let s=R(e,t),o={key:s,enqueuedAt:Date.now(),startedAt:null};if(this.cacheEnabled){let a=this.readCache(s);if(a){let u={status:"success",response:a,durationMs:0,fromCache:!0};return this._lastResult=a,this._lastError=null,this._lastDurationMs=0,this.runHook(this.onStart,o),this.runHook(this.onSettle,o,u),this.notify(),Promise.resolve(a)}}return new Promise((a,u)=>{let i={definition:e,dataTree:t,ctx:o,resolve:a,reject:u,externalSignal:_optionalChain([n, 'optionalAccess', _2 => _2.signal])};if(_optionalChain([i, 'access', _3 => _3.externalSignal, 'optionalAccess', _4 => _4.aborted])){let c=this.makeAbortError(o);i.settled={error:c},u(c);return}this.enqueue(i)})}enqueue(e){switch(this.mode){case"latest-wins":{this.pendingForLatestWins&&(this.supersede(this.pendingForLatestWins),this.pendingForLatestWins=null);for(let t of this.inFlight)this.supersede(t),t.controller.abort();this.inFlight.size===0?this.execute(e):this.pendingForLatestWins=e;break}case"queue":case"parallel":{this.inFlight.size<this.maxConcurrent?this.execute(e):this.fifoQueue.push(e);break}}this.notify()}async execute(e){let t=new AbortController,n={...e,controller:t};this.inFlight.add(n),e.ctx.startedAt=Date.now();let s=()=>t.abort();_optionalChain([e, 'access', _5 => _5.externalSignal, 'optionalAccess', _6 => _6.addEventListener, 'call', _7 => _7("abort",s,{once:!0})]),this.runHook(this.onStart,e.ctx),this.notify();let o=performance.now();try{let a={...this.baseConfig,signal:t.signal,...this.timeoutMs!==void 0&&{timeoutMs:this.timeoutMs},...this.retry!==void 0&&{retry:this.retry}},u=await this.executor(e.definition,e.dataTree,a),i=performance.now()-o;if(this.cacheEnabled&&this.writeCache(e.ctx.key,u),e.settled)return;e.settled={ok:!0},this._lastResult=u,this._lastError=null,this._lastDurationMs=i,e.resolve(u),this.runHook(this.onSettle,e.ctx,{status:"success",response:u,durationMs:i,fromCache:!1})}catch(a){let u=performance.now()-o,i=this.normalizeExecutionError(a,n),c=!!n.settled;this._lastError=i,this._lastDurationMs=u,c||(n.settled={error:i},e.reject(i),this.runHook(this.onSettle,e.ctx,{status:"error",error:i,durationMs:u}))}finally{_optionalChain([e, 'access', _8 => _8.externalSignal, 'optionalAccess', _9 => _9.removeEventListener, 'call', _10 => _10("abort",s)]),this.inFlight.delete(n),this.drainNext(),this.notify()}}drainNext(){if(!this.disposed){if(this.mode==="latest-wins"){if(this.pendingForLatestWins&&this.inFlight.size===0){let e=this.pendingForLatestWins;this.pendingForLatestWins=null,this.execute(e)}return}for(;this.fifoQueue.length>0&&this.inFlight.size<this.maxConcurrent;){let e=this.fifoQueue.shift();this.execute(e)}}}supersede(e){if(e.settled)return;let t=new (0, _chunkVWOEUM7Ccjs.d)("Superseded by newer solve",_chunkVWOEUM7Ccjs.c.SUPERSEDED,{context:{key:e.ctx.key,enqueuedAt:e.ctx.enqueuedAt}});e.settled={error:t},e.reject(t),this.runHook(this.onSuperseded,e.ctx)}makeAbortError(e){return new (0, _chunkVWOEUM7Ccjs.d)("Request aborted by caller",_chunkVWOEUM7Ccjs.c.ABORTED,{context:{key:e.key,enqueuedAt:e.enqueuedAt}})}isAbortLikeError(e){if(e instanceof Error){if(e.name==="AbortError")return!0;if(typeof DOMException<"u"&&e instanceof DOMException)return e.name==="AbortError"}return!1}normalizeExecutionError(e,t){return t.settled&&"error"in t.settled?t.settled.error:e instanceof _chunkVWOEUM7Ccjs.d?e:this.isAbortLikeError(e)?this.makeAbortError(t.ctx):new (0, _chunkVWOEUM7Ccjs.d)(e instanceof Error?e.message:String(e),_chunkVWOEUM7Ccjs.c.UNKNOWN_ERROR,{originalError:e instanceof Error?e:new Error(String(e))})}cancelAll(){for(this.pendingForLatestWins&&(this.rejectAsAborted(this.pendingForLatestWins),this.pendingForLatestWins=null);this.fifoQueue.length>0;){let e=this.fifoQueue.shift();this.rejectAsAborted(e)}for(let e of this.inFlight){if(!e.settled){let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t),this.runHook(this.onSettle,e.ctx,{status:"error",error:t,durationMs:e.ctx.startedAt?performance.now()-e.ctx.startedAt:0})}e.controller.abort()}this.notify()}rejectAsAborted(e){if(e.settled)return;let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t)}readCache(e){if(!this.cacheEnabled)return null;let t=this.cache.get(e);return t?this.cacheTtl>0&&Date.now()-t.insertedAt>this.cacheTtl?(this.cache.delete(e),null):(this.cache.delete(e),this.cache.set(e,t),t.response):null}writeCache(e,t){if(this.cacheEnabled)for(this.cache.set(e,{response:t,insertedAt:Date.now()});this.cache.size>this.cacheMax;){let n=this.cache.keys().next().value;if(n===void 0)break;this.cache.delete(n)}}clearCache(){this.cache.clear()}dispose(){this.disposed||(this.disposed=!0,this.cancelAll(),this.subscribers.clear(),this.cache.clear())}runHook(e,...t){if(e)try{e(...t)}catch(n){_chunkVWOEUM7Ccjs.e.call(void 0, ).error("[SolveScheduler] hook threw:",n)}}};var x=class r{constructor(e){_chunkVWOEUM7Ccjs.b.call(void 0, this,"config");_chunkVWOEUM7Ccjs.b.call(void 0, this,"serverStats");_chunkVWOEUM7Ccjs.b.call(void 0, this,"disposed",!1);this.config=this.normalizeComputeConfig(e),this.serverStats=new (0, _chunkVWOEUM7Ccjs.i)(this.config.serverUrl,this.config.apiKey)}static async create(e){let t=new r(e);if(!await t.serverStats.isServerOnline())throw new (0, _chunkVWOEUM7Ccjs.d)("Rhino Compute server is not online",_chunkVWOEUM7Ccjs.c.NETWORK_ERROR,{context:{serverUrl:t.config.serverUrl}});return t}getConfig(){return this.ensureNotDisposed(),{...this.config}}async getIO(e){return this.ensureNotDisposed(),E(e,this.config)}async getRawIO(e){return this.ensureNotDisposed(),I(e,this.config)}async solve(e,t,n){this.ensureNotDisposed();try{if(typeof e=="string"&&!_optionalChain([e, 'optionalAccess', _11 => _11.trim, 'call', _12 => _12()]))throw new (0, _chunkVWOEUM7Ccjs.d)("Definition URL/content is required",_chunkVWOEUM7Ccjs.c.INVALID_INPUT,{context:{receivedUrl:e}});if(e instanceof Uint8Array&&e.length===0)throw new (0, _chunkVWOEUM7Ccjs.d)("Definition content is empty",_chunkVWOEUM7Ccjs.c.INVALID_INPUT);let s={...this.config,..._optionalChain([n, 'optionalAccess', _13 => _13.signal])!==void 0&&{signal:n.signal},..._optionalChain([n, 'optionalAccess', _14 => _14.timeoutMs])!==void 0&&{timeoutMs:n.timeoutMs},..._optionalChain([n, 'optionalAccess', _15 => _15.retry])!==void 0&&{retry:n.retry}},o=await C(t,e,s);if(_optionalChain([o, 'optionalAccess', _16 => _16.errors])&&o.errors.length>0)throw new (0, _chunkVWOEUM7Ccjs.d)(o.errors.join("; ")||"Computation failed",_chunkVWOEUM7Ccjs.c.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t,errors:o.errors,warnings:o.warnings}});return o}catch(s){throw this.config.debug&&_chunkVWOEUM7Ccjs.e.call(void 0, ).error("Compute failed:",s),s instanceof _chunkVWOEUM7Ccjs.d?s:new (0, _chunkVWOEUM7Ccjs.d)(s instanceof Error?s.message:String(s),_chunkVWOEUM7Ccjs.c.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t},originalError:s instanceof Error?s:new Error(String(s))})}}createScheduler(e){this.ensureNotDisposed();let t=(n,s,o)=>C(s,n,o);return new b(t,this.config,e)}async dispose(){this.disposed||(this.disposed=!0,await this.serverStats.dispose())}ensureNotDisposed(){if(this.disposed)throw new (0, _chunkVWOEUM7Ccjs.d)("GrasshopperClient has been disposed and cannot be used",_chunkVWOEUM7Ccjs.c.INVALID_STATE)}normalizeComputeConfig(e){if(!_optionalChain([e, 'access', _17 => _17.serverUrl, 'optionalAccess', _18 => _18.trim, 'call', _19 => _19()]))throw new (0, _chunkVWOEUM7Ccjs.d)("serverUrl is required",_chunkVWOEUM7Ccjs.c.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});try{new URL(e.serverUrl)}catch (e2){throw new (0, _chunkVWOEUM7Ccjs.d)("serverUrl must be a valid URL",_chunkVWOEUM7Ccjs.c.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}})}if(e.serverUrl===""||e.serverUrl==="https://compute.rhino3d.com/")throw new (0, _chunkVWOEUM7Ccjs.d)("serverUrl must be set to your Compute server URL. The default public endpoint is not allowed.",_chunkVWOEUM7Ccjs.c.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});return{...e,serverUrl:e.serverUrl.replace(/\/+$/,""),apiKey:e.apiKey,authToken:e.authToken,debug:_nullishCoalesce(e.debug, () => (!1)),suppressBrowserWarning:_nullishCoalesce(e.suppressBrowserWarning, () => (e.suppressClientSideWarning))}}};var J=async(r,e=null)=>{try{return await H(r,e)}catch(t){throw new (0, _chunkVWOEUM7Ccjs.d)("Failed to extract files from compute response",_chunkVWOEUM7Ccjs.c.INVALID_STATE,{context:{originalError:t instanceof Error?t.message:String(t)},originalError:t instanceof Error?t:void 0})}},w= exports.e =async(r,e,t=null)=>{if(typeof document>"u"||typeof Blob>"u")throw new (0, _chunkVWOEUM7Ccjs.d)("File download functionality is only available in browser environments. This function requires the DOM API (document, Blob).",_chunkVWOEUM7Ccjs.c.BROWSER_ONLY,{context:{environment:typeof window<"u"?"browser (SSR)":"Node.js",documentAvailable:typeof document<"u",blobAvailable:typeof Blob<"u"}});try{let n=await H(r,t);await le(n,e)}catch(n){throw n instanceof _chunkVWOEUM7Ccjs.d?n:new (0, _chunkVWOEUM7Ccjs.d)("Failed to download files from compute response",_chunkVWOEUM7Ccjs.c.INVALID_STATE,{context:{originalError:n instanceof Error?n.message:String(n)},originalError:n instanceof Error?n:void 0})}},H=async(r,e)=>{let t=[];if(r.forEach(n=>{let s=`${n.fileName}${n.fileType}`;if(n.subFolder&&n.subFolder.trim()!==""&&(s=`${n.subFolder}/${s}`),n.isBase64Encoded===!0&&n.data){let o=_chunkMZGKJZVPcjs.c.call(void 0, n.data);t.push({fileName:`${n.fileName}${n.fileType}`,content:new Uint8Array(o.buffer),path:s})}else n.isBase64Encoded===!1&&n.data&&t.push({fileName:`${n.fileName}${n.fileType}`,content:n.data,path:s})}),e){let n=Array.isArray(e)?e:[e],s=await Promise.all(n.map(async o=>{try{let a=await fetch(o.filePath);if(!a.ok)return _chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to fetch additional file from URL: ${o.filePath}`),null;let i=await(await a.blob()).arrayBuffer();return{fileName:o.fileName,content:new Uint8Array(i),path:o.fileName}}catch(a){return _chunkVWOEUM7Ccjs.e.call(void 0, ).error(`Error fetching additional file from URL: ${o.filePath}`,a),null}}));t.push(...s.filter(o=>o!==null))}return t};async function le(r,e){let{zipSync:t,strToU8:n}=await Promise.resolve().then(() => _interopRequireWildcard(require("fflate"))),s={};r.forEach(u=>{s[u.path]=typeof u.content=="string"?n(u.content):u.content});let o=t(s,{level:6}),a=new Blob([o],{type:"application/zip"});pe(a,`${e}.zip`)}function pe(r,e){if(typeof document>"u")throw new (0, _chunkVWOEUM7Ccjs.d)("saveFile requires a browser environment with DOM API access.",_chunkVWOEUM7Ccjs.c.BROWSER_ONLY,{context:{function:"saveFile",requiredAPI:"document"}});let t=document.createElement("a");t.href=URL.createObjectURL(r),t.download=e,t.click(),URL.revokeObjectURL(t.href)}var A=new Map;function K(r,e){A.set(r,e)}K("Rhino.Geometry.Point3d",(r,e)=>{let t=e;return!t||typeof t.X!="number"?null:new r.Point([t.X,t.Y,t.Z])});K("Rhino.Geometry.Line",(r,e)=>{let t=e;return!t||!t.From||!t.To?null:new r.Line([t.From.X,t.From.Y,t.From.Z],[t.To.X,t.To.Y,t.To.Z])});function ce(r){if(A.has(r))return A.get(r);for(let[e,t]of A)if(r.startsWith(e))return t}function fe(r){return!r||typeof r!="object"?null:_nullishCoalesce(_nullishCoalesce(r.data, () => (r.value)), () => (null))}function Y(r,e,t){let n=ce(e);if(n)try{return n(t,r)}catch(s){_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to decode Rhino type ${e}:`,s)}try{let s=fe(r);if(s)return t.CommonObject.decode(s)}catch(s){return _chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to decode ${e} with CommonObject:`,s),{__decodeError:!0,type:e,raw:r}}return r}var T={STRING:"System.String",INT:"System.Int32",DOUBLE:"System.Double",BOOL:"System.Boolean"},me="Rhino.Geometry.",de=["WebDisplay"],he="FileData";function Q(r){return de.some(e=>r.includes(e))}function X(r){if(typeof r!="string")return r;let e=r.trim();if(!(e.startsWith("{")||e.startsWith("[")||e.startsWith('"')))return r;try{let n=JSON.parse(e);if(typeof n=="string")try{return JSON.parse(n)}catch (e3){return n}return n}catch (e4){return r}}function ye(r,e,t){switch(e){case T.STRING:return typeof r!="string"?r:r.replace(/^"(.*)"$/,"$1");case T.INT:return Number.parseInt(r,10);case T.DOUBLE:return Number.parseFloat(r);case T.BOOL:return String(r).toLowerCase()==="true";default:return t&&e.startsWith(me)?Y(r,e,t):r}}function Z(r,e,t,n){if(typeof r!="string")return r;let s=t?X(r):r;return ye(s,e,n)}function ge(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.fileName=="string"&&typeof e.fileType=="string"&&"data"in e&&typeof e.isBase64Encoded=="boolean"&&typeof e.subFolder=="string"}function F(r,e){for(let t of Object.values(r))if(Array.isArray(t))for(let n of t)e(n)}function ee(r,e=!1,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a={};for(let u of r.values)F(u.InnerTree,i=>{if(Q(i.type)||o&&i.type!==T.STRING)return;let c=e?i.id:u.ParamName;if(!c)return;let d=Z(i.data,i.type,n,s);a[c]===void 0?a[c]=d:Array.isArray(a[c])?a[c].push(d):a[c]=[a[c],d]});return{values:a}}function re(r){let e=[];for(let t of r.values)F(t.InnerTree,n=>{if(!n.type.includes(he))return;let s=X(n.data);ge(s)&&e.push(s)});return e}function O(r,e,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a;if("byName"in e?a=r.values.find(i=>i.ParamName===e.byName):a=r.values.find(i=>{let c=!1;return F(i.InnerTree,d=>{d.id===e.byId&&(c=!0)}),c}),!a)return;let u=[];if(F(a.InnerTree,i=>{if("byId"in e&&i.id!==e.byId||Q(i.type)||o&&i.type!==T.STRING)return;let c=Z(i.data,i.type,n,s);u.push(c)}),u.length!==0)return u.length===1?u[0]:u}var D=class{constructor(e,t=!1){this.response=e;this.debug=t}getValues(e=!1,t={}){return ee(this.response,e,t)}getValue(e,t){return O(this.response,e,t)}getValueByParamName(e,t){return O(this.response,{byName:e},t)}getValueByParamId(e,t){return O(this.response,{byId:e},t)}async extractMeshesFromResponse(e){let t={debug:this.debug,...e},n;try{({getThreeMeshesFromComputeResponse:n}=await Promise.resolve().then(() => _interopRequireWildcard(require("./visualization-JYNKROSH.cjs"))))}catch(s){throw new (0, _chunkVWOEUM7Ccjs.d)("Failed to load three.js visualization module. Ensure three.js is installed as a peer dependency.",_chunkVWOEUM7Ccjs.c.INVALID_STATE,{context:{originalError:s instanceof Error?s.message:String(s)}})}return n(this.response,t)}getFileData(){return re(this.response)}getAndDownloadFiles(e,t){let n=this.getFileData();w(n,e,t)}};function N(r,e){e||typeof window<"u"&&_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Warning: ${r} is running on the client side. For better performance and security, consider running this on the server side.`)}async function C(r,e,t){t.debug&&N("solveGrasshopperDefinition",_nullishCoalesce(t.suppressBrowserWarning, () => (t.suppressClientSideWarning)));let n=M(e,r);be(n,t);let s=await _chunkVWOEUM7Ccjs.h.call(void 0, "grasshopper",n,t);if("pointer"in s){let{pointer:o,...a}=s;return a}return s}function M(r,e){let t={algo:null,pointer:null,values:e};return r instanceof Uint8Array?t.algo=_chunkMZGKJZVPcjs.d.call(void 0, r):/^https?:\/\//i.test(r)?t.pointer=r:_chunkMZGKJZVPcjs.b.call(void 0, r)?t.algo=r:t.algo=_chunkMZGKJZVPcjs.a.call(void 0, r),t}function be(r,e){e.cachesolve!=null&&(r.cachesolve=e.cachesolve),e.modelunits!=null&&(r.modelunits=e.modelunits),e.angletolerance!=null&&(r.angletolerance=e.angletolerance),e.absolutetolerance!=null&&(r.absolutetolerance=e.absolutetolerance),e.dataversion!=null&&(r.dataversion=e.dataversion)}function te(r){if(typeof r.default!="object"||r.default===null)return;if(!("innerTree"in r.default)){_chunkVWOEUM7Ccjs.e.call(void 0, ).warn("Unexpected structure in input.default:",r.default),r.default=null;return}let e=r.default.innerTree;if(Object.keys(e).length===0){r.default=void 0;return}if(r.treeAccess||r.atMost&&r.atMost>1){let n={};for(let[s,o]of Object.entries(e))n[s]=o.map(a=>{if(typeof a.data=="string"){if(a.type==="System.Double"||a.type==="System.Int32"){let u=Number(a.data);return Number.isNaN(u)?a.data:u}if(a.type==="System.Boolean")return a.data.toLowerCase()==="true";if(a.type.startsWith("Rhino.Geometry")||a.type==="System.String")try{return JSON.parse(a.data)}catch (e5){return a.data}}return a.data});r.default=n;return}let t=[];for(let n of Object.values(e))Array.isArray(n)&&n.forEach(s=>{s&&typeof s=="object"&&"data"in s&&t.push(s.data)});t.length===0?r.default=void 0:t.length===1?r.default=t[0]:r.default=t}function P(r,e){let{transform:t,setUndefinedOnEmpty:n=!0}=e;if(!(r.default===void 0||r.default===null))if(Array.isArray(r.default)){let s=r.default.map(t).filter(o=>o!==null);r.default=s.length>0?s:void 0}else{let s=t(r.default);s!==null?r.default=s:n&&(r.default=void 0)}}function Te(){return r=>{if(typeof r=="number")return r;if(typeof r=="string"){let e=Number(r.trim());return Number.isNaN(e)?null:e}return null}}function Se(){return r=>{if(typeof r=="boolean")return r;if(typeof r=="string"){let e=r.toLowerCase();if(e==="true")return!0;if(e==="false")return!1;throw new Error(`Invalid boolean string: "${r}"`)}return null}}function xe(){return r=>typeof r=="string"?r.startsWith('"')&&r.endsWith('"')||r.startsWith('"')?r.slice(1,-1):r:null}function Ce(){return r=>{if(typeof r=="string"){let e=r.trim();return e.startsWith('"')&&e.endsWith('"')&&(e=e.slice(1,-1).trim()),e}return null}}function Ie(r){P(r,{transform:Ce(),setUndefinedOnEmpty:!1})}function De(r="unknown"){return e=>{if(typeof e=="object"&&e!==null)return e;if(typeof e=="string"&&e.trim()!=="")try{let t=JSON.parse(e);return typeof t=="object"&&t!==null?t:(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Parsed value for input ${r} is not an object`),null)}catch(t){return _chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Failed to parse object value "${e}" for input ${r}`,t),null}return null}}function ne(r,e,t){let n=Number(r.toFixed(e));return Math.abs(r-n)<t?n:r}function Pe(r,e=1e-8){if(!Number.isFinite(r)||r===0)return .1;let t=Math.abs(r);if(t>=1){let h=String(r).split(".")[1];if(h&&h.length>0){let y=Math.min(h.length,12),g=Math.pow(10,-y),B=Number(g.toFixed(y));return Math.abs(B-g)<e?B:g}return 1}let n=String(r),s=n.toLowerCase().match(/e(-?\d+)/);if(s){let S=Number(s[1]);if(S<0||n.toLowerCase().includes("e-")){let h=Math.abs(S),y=Math.pow(10,-h),g=Number(y.toFixed(h));return Math.abs(g-y)<e?g:y}return .1}let o=12,u=t.toFixed(o).replace(/0+$/,""),i=Math.min((u.split(".")[1]||"").length,o);if(i===0)return .1;let c=Math.pow(10,-i),d=Number(c.toFixed(i));return Math.abs(d-c)<e?d:c}function se(r,e=1e-8){let t=r.paramType==="Integer";if(P(r,{transform:Te()}),t){Array.isArray(r.default)?r.default=r.default.map(o=>typeof o=="number"?Math.round(o):o):typeof r.default=="number"&&(r.default=Math.round(r.default)),r.stepSize=1;return}let n=Array.isArray(r.default)?r.default[0]:r.default,s;if(typeof n=="number"&&Number.isFinite(n)&&n!==0?s=n:typeof r.minimum=="number"&&Number.isFinite(r.minimum)&&r.minimum!==0?s=r.minimum:typeof r.maximum=="number"&&Number.isFinite(r.maximum)&&r.maximum!==0&&(s=r.maximum),s!==void 0?r.stepSize=Pe(s,e):r.stepSize=.1,typeof r.stepSize=="number"){let o=0,a=String(r.stepSize),u=a.toLowerCase().match(/e(-?\d+)/);if(u?o=Math.abs(Number(u[1])):o=_nullishCoalesce(_optionalChain([a, 'access', _20 => _20.split, 'call', _21 => _21("."), 'access', _22 => _22[1], 'optionalAccess', _23 => _23.length]), () => (0)),o===0&&typeof n=="number"&&n!==0&&Math.abs(n)<1){let i=Math.ceil(-Math.log10(Math.abs(n)));Number.isFinite(i)&&i>0&&(o=i)}o=Math.min(Math.max(o,0),12),Array.isArray(r.default)?r.default=r.default.map(i=>typeof i=="number"?ne(i,o,e):i):typeof r.default=="number"&&(r.default=ne(r.default,o,e))}}function ve(r){try{P(r,{transform:Se(),setUndefinedOnEmpty:!1})}catch(e){throw e instanceof Error?new (0, _chunkVWOEUM7Ccjs.d)(e.message):e}}function Re(r){P(r,{transform:xe(),setUndefinedOnEmpty:!1})}function oe(r){P(r,{transform:De(r.nickname||"unnamed"),setUndefinedOnEmpty:!0})}function Ee(r){if(!r.values||typeof r.values!="object"||Object.keys(r.values).length===0)throw _chunkVWOEUM7Ccjs.d.missingValues(r.nickname||"unnamed","ValueList");if(r.default!==void 0&&r.default!==null){let e=String(r.default).toLowerCase();Object.keys(r.values).some(n=>n.toLowerCase()===e)||_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`ValueList input "${r.nickname||"unnamed"}" default value "${r.default}" is not in available values`)}}var ae={Number:se,Integer:se,Boolean:ve,Text:Re,ValueList:Ee,Geometry:oe,File:oe,Color:Ie};function we(r,e){let t=(_nullishCoalesce(r.atMost, () => (1)))>1;switch(r.paramType){case"Number":case"Integer":return{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,default:t?[0]:0};case"Boolean":return{...e,paramType:"Boolean",default:t?[!1]:!1};case"Text":return{...e,paramType:"Text",default:t?[""]:""};case"ValueList":return{...e,paramType:"ValueList",values:_nullishCoalesce(r.values, () => ({})),default:t?[r.default]:r.default};case"File":return{...e,paramType:"File",default:t?[null]:null};case"Color":return{...e,paramType:"Color",default:t?["0, 0, 0"]:"0, 0, 0"};default:return{...e,paramType:"Geometry",default:t?[null]:null}}}function ie(r){return k(r).input}function k(r){let e={description:r.description,name:r.name,nickname:r.nickname,treeAccess:r.treeAccess,groupName:_nullishCoalesce(r.groupName, () => ("")),id:r.id};try{te(r);let t=ae[r.paramType];if(!t)throw _chunkVWOEUM7Ccjs.d.unknownParamType(r.paramType,r.name);switch(t(r),r.paramType){case"Number":case"Integer":return{input:{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,stepSize:r.stepSize,default:r.default}};case"Boolean":return{input:{...e,paramType:"Boolean",default:r.default}};case"Text":return{input:{...e,paramType:"Text",default:r.default}};case"ValueList":return{input:{...e,paramType:"ValueList",values:r.values,default:r.default}};case"Geometry":return{input:{...e,paramType:r.paramType,default:r.default}};case"File":return{input:{...e,paramType:r.paramType,acceptedFormats:r.acceptedFormats,default:r.default}};case"Color":return{input:{...e,paramType:"Color",default:r.default}};default:throw _chunkVWOEUM7Ccjs.d.unknownParamType(r.paramType,r.name)}}catch(t){if(t instanceof _chunkVWOEUM7Ccjs.d)return _chunkVWOEUM7Ccjs.e.call(void 0, ).error(`Validation error for input ${r.name||"unknown"}:`,t.message),{input:we(r,e),error:{inputName:r.name||"unknown",paramType:r.paramType,message:t.message,code:t.code}};throw new (0, _chunkVWOEUM7Ccjs.d)(t instanceof Error?t.message:String(t),"VALIDATION_ERROR",{context:{paramName:r.name,paramType:r.paramType},originalError:t instanceof Error?t:new Error(String(t))})}}function ue(r){return V(r).inputs}function V(r){let e=[],t=[];for(let n of r){let{input:s,error:o}=k(n);e.push(s),o&&t.push(o)}return{inputs:e,parseErrors:t}}async function I(r,e){let t=M(r,[]),n={};if(t.algo&&(n.algo=t.algo),t.pointer&&(n.pointer=t.pointer),!n.algo&&!n.pointer)throw new (0, _chunkVWOEUM7Ccjs.d)("Definition must resolve to either a URL pointer or base64 algo",_chunkVWOEUM7Ccjs.c.INVALID_INPUT,{context:{definition:r}});let s=await _chunkVWOEUM7Ccjs.h.call(void 0, "io",n,e);if(!s||typeof s!="object")throw new (0, _chunkVWOEUM7Ccjs.d)("Invalid IO response structure",_chunkVWOEUM7Ccjs.c.INVALID_INPUT,{context:{response:s,definition:r}});let o=_chunkVWOEUM7Ccjs.k.call(void 0, s,{deep:!0});return{inputs:o.inputs,outputs:o.outputs}}async function E(r,e){N("fetchParsedDefinitionIO",_nullishCoalesce(e.suppressBrowserWarning, () => (e.suppressClientSideWarning)));let{inputs:t,outputs:n}=await I(r,e),{inputs:s,parseErrors:o}=V(t);return o.length>0?{inputs:s,outputs:n,parseErrors:o}:{inputs:s,outputs:n}}var G=class r{constructor(e){_chunkVWOEUM7Ccjs.b.call(void 0, this,"innerTree");_chunkVWOEUM7Ccjs.b.call(void 0, this,"paramName");this.paramName=e,this.innerTree={}}append(e,t){let n=r.formatPathString(e);this.innerTree[n]||(this.innerTree[n]=[]);let s=t.map(o=>({data:r.serializeValue(o)}));return this.innerTree[n].push(...s),this}appendSingle(e,t){return this.append(e,[t])}fromDataTreeDefault(e){this.innerTree={};for(let[t,n]of Object.entries(e)){if(!Array.isArray(n))continue;let s=r.parsePathString(t);this.append(s,n)}return this}appendFlat(e){let t=Array.isArray(e)?e:[e];return this.append([0],t)}flatten(){let e=[];for(let t of Object.values(this.innerTree))if(Array.isArray(t))for(let n of t)e.push(r.deserializeValue(n.data));return e}getPaths(){return Object.keys(this.innerTree)}getPath(e){let t=r.formatPathString(e),n=this.innerTree[t];if(n)return n.map(s=>r.deserializeValue(s.data))}toComputeFormat(){return{ParamName:this.paramName,InnerTree:this.innerTree}}getInnerTree(){return this.innerTree}getParamName(){return this.paramName}static fromInputParams(e){return e.filter(t=>r.hasValidValue(t.default)).map(t=>{let n=new r(t.nickname||"unnamed"),s=t.default;if(t.treeAccess&&r.isDataTreeStructure(s))n.fromDataTreeDefault(s),r.isNumericInput(t)&&n.applyNumericConstraints(t.minimum,t.maximum,t.nickname||"unnamed");else{let o=Array.isArray(s)?s:[s],a=r.processValues(o,t);n.appendFlat(a)}return n.toComputeFormat()})}static fromInputParam(e){return r.hasValidValue(e.default)?r.fromInputParams([e])[0]:void 0}static replaceTreeValue(e,t,n){let s=e.length>0&&e[0]instanceof r,o=r.buildFromValue(t,n);if(s){let c=e,d=c.findIndex(S=>S.getParamName()===t);return d!==-1?c[d]=o:c.push(o),c}let a=e,u=o.toComputeFormat(),i=a.findIndex(c=>c.ParamName===t);return i!==-1?a[i]=u:a.push(u),a}static buildFromValue(e,t){let n=new r(e);return typeof t=="object"&&t!==null&&!Array.isArray(t)&&r.isDataTreeStructure(t)?n.fromDataTreeDefault(t):n.appendFlat(t),n}static getTreeValue(e,t){let s=e.length>0&&e[0]instanceof r?r.readFromBuilders(e,t):r.readFromDataTrees(e,t);return s===null||s.length===0?null:s.length===1?s[0]:s}static readFromBuilders(e,t){let n=e.find(s=>s.getParamName()===t);return n?n.flatten():null}static readFromDataTrees(e,t){let n=e.find(a=>a.ParamName===t);if(!_optionalChain([n, 'optionalAccess', _24 => _24.InnerTree]))return null;let s=Object.keys(n.InnerTree)[0];if(!s)return null;let o=n.InnerTree[s];return Array.isArray(o)?o.map(a=>_optionalChain([a, 'optionalAccess', _25 => _25.data])!==void 0?r.deserializeValue(a.data):null).filter(a=>a!==null):_optionalChain([o, 'optionalAccess', _26 => _26.data])!==void 0?[r.deserializeValue(o.data)]:o!==void 0?[o]:null}static parsePathString(e){let t=e.match(/^\{([\d;]*)\}$/);return t?t[1]===""?[]:t[1].split(";").map(Number):(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`Invalid TreeBuilder path format: ${e}, using [0]`),[0])}static formatPathString(e){return`{${e.join(";")}}`}applyNumericConstraints(e,t,n){for(let s of Object.values(this.innerTree))if(Array.isArray(s))for(let o of s){let a=r.deserializeValue(o.data);if(typeof a=="number"){let u=r.clampValue(a,e,t,n);o.data=r.serializeValue(u)}}}static serializeValue(e){return typeof e=="boolean"||typeof e=="number"||typeof e=="string"?e:typeof e=="object"&&e!==null?JSON.stringify(e):String(e)}static deserializeValue(e){if(typeof e=="boolean"||typeof e=="number"||typeof e!="string")return e;if(e.startsWith("{")||e.startsWith("["))try{return JSON.parse(e)}catch (e6){return e}return isNaN(Number(e))?e==="true"?!0:e==="false"?!1:e:Number(e)}static hasValidValue(e){return e==null?!1:typeof e=="string"?!0:!(Array.isArray(e)&&e.length===0||typeof e=="object"&&!Array.isArray(e)&&Object.keys(e).length===0)}static isDataTreeStructure(e){return typeof e!="object"||e===null||Array.isArray(e)?!1:Object.entries(e).every(([t,n])=>typeof t=="string"&&/^\{[\d;]+\}$/.test(t)&&Array.isArray(n))}static isNumericInput(e){return e.paramType==="Number"||e.paramType==="Integer"}static processValues(e,t){return e.map(n=>r.isNumericInput(t)&&typeof n=="number"?r.clampValue(n,t.minimum,t.maximum,t.nickname||"unnamed"):n).filter(n=>n!=null)}static clampValue(e,t,n,s){let o=e;return t!=null&&o<t&&(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`${s}: ${e} below min ${t}, clamping`),o=t),n!=null&&o>n&&(_chunkVWOEUM7Ccjs.e.call(void 0, ).warn(`${s}: ${e} above max ${n}, clamping`),o=n),o}};exports.a = R; exports.b = b; exports.c = x; exports.d = J; exports.e = w; exports.f = D; exports.g = C; exports.h = ie; exports.i = ue; exports.j = I; exports.k = E; exports.l = G;
|
|
2
|
+
//# sourceMappingURL=chunk-6VF4VVLW.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/selva-compute/selva-compute/dist/chunk-
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/selva-compute/selva-compute/dist/chunk-6VF4VVLW.cjs","../src/features/grasshopper/scheduler/stable-hash.ts"],"names":["stableStringify","value","seen","stringify","v","sample","k"],"mappings":"AAAA,2/BAA6D,wDAAmF,SCShIA,CAAAA,CAAgBC,CAAAA,CAAwB,CACvD,IAAMC,CAAAA,CAAO,IAAI,OAAA,CAEXC,CAAAA,CAAaC,CAAAA,EAAuB,CACzC,EAAA,CAAIA,CAAAA,EAAM,IAAA,CAAyB,OAAO,IAAA,CAAK,SAAA,CAAUA,CAAC,CAAA,CAC1D,EAAA,CAAI,OAAOA,CAAAA,EAAM,QAAA,CAChB,OAAO,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,IAAA,CAAK,CAAA,CACxB,GAAA,CAAKE,CAAAA,EAAM,CAAA,EAAA","file":"/home/runner/work/selva-compute/selva-compute/dist/chunk-6VF4VVLW.cjs","sourcesContent":[null,"/**\n * Stable hashing for solve deduplication and caching.\n * @internal\n */\n\n/**\n * Deterministic stringify with sorted keys. {a:1,b:2} and {b:2,a:1} produce\n * the same string. Safely handles circular references and non-finite numbers.\n */\nexport function stableStringify(value: unknown): string {\n\tconst seen = new WeakSet<object>();\n\n\tconst stringify = (v: unknown): string => {\n\t\tif (v === null || v === undefined) return JSON.stringify(v);\n\t\tif (typeof v === 'number') {\n\t\t\treturn Number.isFinite(v) ? String(v) : JSON.stringify(null);\n\t\t}\n\t\tif (typeof v === 'string' || typeof v === 'boolean') return JSON.stringify(v);\n\t\tif (typeof v === 'bigint') return JSON.stringify(v.toString());\n\t\tif (v instanceof Uint8Array) {\n\t\t\t// Use length + sample instead of full buffer to avoid stringifying large data\n\t\t\tconst sample =\n\t\t\t\tv.length > 64 ? Array.from(v.slice(0, 32)).concat(Array.from(v.slice(-32))) : Array.from(v);\n\t\t\treturn JSON.stringify({ __u8: true, len: v.length, sample });\n\t\t}\n\t\tif (Array.isArray(v)) {\n\t\t\treturn `[${v.map(stringify).join(',')}]`;\n\t\t}\n\t\tif (typeof v === 'object') {\n\t\t\tif (seen.has(v as object)) return JSON.stringify('[Circular]');\n\t\t\tseen.add(v as object);\n\t\t\tconst keys = Object.keys(v as object).sort();\n\t\t\tconst parts = keys.map((k) => `${JSON.stringify(k)}:${stringify((v as any)[k])}`);\n\t\t\treturn `{${parts.join(',')}}`;\n\t\t}\n\t\t// Fallback for functions, symbols, etc.\n\t\treturn JSON.stringify(null);\n\t};\n\n\treturn stringify(value);\n}\n\n/**\n * 32-bit FNV-1a— fast, no dependencies. Returns unsigned hex string.\n */\nexport function fnv1a(input: string): string {\n\tlet hash = 0x811c9dc5;\n\tfor (let i = 0; i < input.length; i++) {\n\t\thash ^= input.charCodeAt(i);\n\t\thash = (hash + ((hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24))) >>> 0;\n\t}\n\treturn hash.toString(16).padStart(8, '0');\n}\n\n/**\n * Hash definition and data tree into a stable cache key.\n * For Uint8Array, uses length + samples to keep hashing fast.\n */\nexport function hashSolveInput(definition: string | Uint8Array, dataTree: unknown): string {\n\tconst defKey =\n\t\ttypeof definition === 'string'\n\t\t\t? definition\n\t\t\t: stableStringify({ __u8: true, len: definition.length });\n\treturn fnv1a(`${defKey}|${stableStringify(dataTree)}`);\n}\n"]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as W,b as $,c as z,d as q}from"./chunk-XLHA5YPH.js";import{b as p,c as f,d as l,e as m,h as v,i as U,k as j}from"./chunk-RHULSS7S.js";function L(r){let e=new WeakSet,t=n=>{if(n==null)return JSON.stringify(n);if(typeof n=="number")return Number.isFinite(n)?String(n):JSON.stringify(null);if(typeof n=="string"||typeof n=="boolean")return JSON.stringify(n);if(typeof n=="bigint")return JSON.stringify(n.toString());if(n instanceof Uint8Array){let s=n.length>64?Array.from(n.slice(0,32)).concat(Array.from(n.slice(-32))):Array.from(n);return JSON.stringify({__u8:!0,len:n.length,sample:s})}return Array.isArray(n)?`[${n.map(t).join(",")}]`:typeof n=="object"?e.has(n)?JSON.stringify("[Circular]"):(e.add(n),`{${Object.keys(n).sort().map(a=>`${JSON.stringify(a)}:${t(n[a])}`).join(",")}}`):JSON.stringify(null)};return t(r)}function _(r){let e=2166136261;for(let t=0;t<r.length;t++)e^=r.charCodeAt(t),e=e+((e<<1)+(e<<4)+(e<<7)+(e<<8)+(e<<24))>>>0;return e.toString(16).padStart(8,"0")}function R(r,e){let t=typeof r=="string"?r:L({__u8:!0,len:r.length});return _(`${t}|${L(e)}`)}var b=class{constructor(e,t,n={}){p(this,"executor");p(this,"baseConfig");p(this,"mode");p(this,"maxConcurrent");p(this,"timeoutMs");p(this,"retry");p(this,"cacheEnabled");p(this,"cacheMax");p(this,"cacheTtl");p(this,"cache",new Map);p(this,"onStart");p(this,"onSettle");p(this,"onSuperseded");p(this,"subscribers",new Set);p(this,"inFlight",new Set);p(this,"pendingForLatestWins",null);p(this,"fifoQueue",[]);p(this,"_lastResult",null);p(this,"_lastError",null);p(this,"_lastDurationMs",null);p(this,"disposed",!1);this.executor=e,this.baseConfig=t,this.mode=n.mode??"latest-wins",this.maxConcurrent=Math.max(1,n.maxConcurrent??(this.mode==="parallel"?4:1)),this.timeoutMs=n.timeoutMs,this.retry=n.retry;let s=n.cache;this.cacheEnabled=s!==void 0&&s!==!1;let o=typeof s=="object"?s:{};this.cacheMax=o.maxEntries??50,this.cacheTtl=o.ttlMs??0,this.onStart=n.onStart,this.onSettle=n.onSettle,this.onSuperseded=n.onSuperseded}get isSolving(){return this.inFlight.size>0}get hasPending(){return this.pendingForLatestWins!==null||this.fifoQueue.length>0}get inFlightCount(){return this.inFlight.size}get queueDepth(){return this.fifoQueue.length+(this.pendingForLatestWins?1:0)}get lastResult(){return this._lastResult}get lastError(){return this._lastError}get lastDurationMs(){return this._lastDurationMs}subscribe(e){return this.subscribers.add(e),()=>this.subscribers.delete(e)}notify(){for(let e of this.subscribers)try{e()}catch(t){m().error("[SolveScheduler] subscriber threw:",t)}}solve(e,t,n){if(this.disposed)return Promise.reject(new l("SolveScheduler has been disposed and cannot be used",f.INVALID_STATE));let s=R(e,t),o={key:s,enqueuedAt:Date.now(),startedAt:null};if(this.cacheEnabled){let a=this.readCache(s);if(a){let u={status:"success",response:a,durationMs:0,fromCache:!0};return this._lastResult=a,this._lastError=null,this._lastDurationMs=0,this.runHook(this.onStart,o),this.runHook(this.onSettle,o,u),this.notify(),Promise.resolve(a)}}return new Promise((a,u)=>{let i={definition:e,dataTree:t,ctx:o,resolve:a,reject:u,externalSignal:n?.signal};if(i.externalSignal?.aborted){let c=this.makeAbortError(o);i.settled={error:c},u(c);return}this.enqueue(i)})}enqueue(e){switch(this.mode){case"latest-wins":{this.pendingForLatestWins&&(this.supersede(this.pendingForLatestWins),this.pendingForLatestWins=null);for(let t of this.inFlight)this.supersede(t),t.controller.abort();this.inFlight.size===0?this.execute(e):this.pendingForLatestWins=e;break}case"queue":case"parallel":{this.inFlight.size<this.maxConcurrent?this.execute(e):this.fifoQueue.push(e);break}}this.notify()}async execute(e){let t=new AbortController,n={...e,controller:t};this.inFlight.add(n),e.ctx.startedAt=Date.now();let s=()=>t.abort();e.externalSignal?.addEventListener("abort",s,{once:!0}),this.runHook(this.onStart,e.ctx),this.notify();let o=performance.now();try{let a={...this.baseConfig,signal:t.signal,...this.timeoutMs!==void 0&&{timeoutMs:this.timeoutMs},...this.retry!==void 0&&{retry:this.retry}},u=await this.executor(e.definition,e.dataTree,a),i=performance.now()-o;if(this.cacheEnabled&&this.writeCache(e.ctx.key,u),e.settled)return;e.settled={ok:!0},this._lastResult=u,this._lastError=null,this._lastDurationMs=i,e.resolve(u),this.runHook(this.onSettle,e.ctx,{status:"success",response:u,durationMs:i,fromCache:!1})}catch(a){let u=performance.now()-o,i=this.normalizeExecutionError(a,n),c=!!n.settled;this._lastError=i,this._lastDurationMs=u,c||(n.settled={error:i},e.reject(i),this.runHook(this.onSettle,e.ctx,{status:"error",error:i,durationMs:u}))}finally{e.externalSignal?.removeEventListener("abort",s),this.inFlight.delete(n),this.drainNext(),this.notify()}}drainNext(){if(!this.disposed){if(this.mode==="latest-wins"){if(this.pendingForLatestWins&&this.inFlight.size===0){let e=this.pendingForLatestWins;this.pendingForLatestWins=null,this.execute(e)}return}for(;this.fifoQueue.length>0&&this.inFlight.size<this.maxConcurrent;){let e=this.fifoQueue.shift();this.execute(e)}}}supersede(e){if(e.settled)return;let t=new l("Superseded by newer solve",f.SUPERSEDED,{context:{key:e.ctx.key,enqueuedAt:e.ctx.enqueuedAt}});e.settled={error:t},e.reject(t),this.runHook(this.onSuperseded,e.ctx)}makeAbortError(e){return new l("Request aborted by caller",f.ABORTED,{context:{key:e.key,enqueuedAt:e.enqueuedAt}})}isAbortLikeError(e){if(e instanceof Error){if(e.name==="AbortError")return!0;if(typeof DOMException<"u"&&e instanceof DOMException)return e.name==="AbortError"}return!1}normalizeExecutionError(e,t){return t.settled&&"error"in t.settled?t.settled.error:e instanceof l?e:this.isAbortLikeError(e)?this.makeAbortError(t.ctx):new l(e instanceof Error?e.message:String(e),f.UNKNOWN_ERROR,{originalError:e instanceof Error?e:new Error(String(e))})}cancelAll(){for(this.pendingForLatestWins&&(this.rejectAsAborted(this.pendingForLatestWins),this.pendingForLatestWins=null);this.fifoQueue.length>0;){let e=this.fifoQueue.shift();this.rejectAsAborted(e)}for(let e of this.inFlight){if(!e.settled){let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t),this.runHook(this.onSettle,e.ctx,{status:"error",error:t,durationMs:e.ctx.startedAt?performance.now()-e.ctx.startedAt:0})}e.controller.abort()}this.notify()}rejectAsAborted(e){if(e.settled)return;let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t)}readCache(e){if(!this.cacheEnabled)return null;let t=this.cache.get(e);return t?this.cacheTtl>0&&Date.now()-t.insertedAt>this.cacheTtl?(this.cache.delete(e),null):(this.cache.delete(e),this.cache.set(e,t),t.response):null}writeCache(e,t){if(this.cacheEnabled)for(this.cache.set(e,{response:t,insertedAt:Date.now()});this.cache.size>this.cacheMax;){let n=this.cache.keys().next().value;if(n===void 0)break;this.cache.delete(n)}}clearCache(){this.cache.clear()}dispose(){this.disposed||(this.disposed=!0,this.cancelAll(),this.subscribers.clear(),this.cache.clear())}runHook(e,...t){if(e)try{e(...t)}catch(n){m().error("[SolveScheduler] hook threw:",n)}}};var x=class r{constructor(e){p(this,"config");p(this,"serverStats");p(this,"disposed",!1);this.config=this.normalizeComputeConfig(e),this.serverStats=new U(this.config.serverUrl,this.config.apiKey)}static async create(e){let t=new r(e);if(!await t.serverStats.isServerOnline())throw new l("Rhino Compute server is not online",f.NETWORK_ERROR,{context:{serverUrl:t.config.serverUrl}});return t}getConfig(){return this.ensureNotDisposed(),{...this.config}}async getIO(e){return this.ensureNotDisposed(),E(e,this.config)}async getRawIO(e){return this.ensureNotDisposed(),I(e,this.config)}async solve(e,t,n){this.ensureNotDisposed();try{if(typeof e=="string"&&!e?.trim())throw new l("Definition URL/content is required",f.INVALID_INPUT,{context:{receivedUrl:e}});if(e instanceof Uint8Array&&e.length===0)throw new l("Definition content is empty",f.INVALID_INPUT);let s={...this.config,...n?.signal!==void 0&&{signal:n.signal},...n?.timeoutMs!==void 0&&{timeoutMs:n.timeoutMs},...n?.retry!==void 0&&{retry:n.retry}},o=await C(t,e,s);if(o?.errors&&o.errors.length>0)throw new l(o.errors.join("; ")||"Computation failed",f.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t,errors:o.errors,warnings:o.warnings}});return o}catch(s){throw this.config.debug&&m().error("Compute failed:",s),s instanceof l?s:new l(s instanceof Error?s.message:String(s),f.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t},originalError:s instanceof Error?s:new Error(String(s))})}}createScheduler(e){this.ensureNotDisposed();let t=(n,s,o)=>C(s,n,o);return new b(t,this.config,e)}async dispose(){this.disposed||(this.disposed=!0,await this.serverStats.dispose())}ensureNotDisposed(){if(this.disposed)throw new l("GrasshopperClient has been disposed and cannot be used",f.INVALID_STATE)}normalizeComputeConfig(e){if(!e.serverUrl?.trim())throw new l("serverUrl is required",f.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});try{new URL(e.serverUrl)}catch{throw new l("serverUrl must be a valid URL",f.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}})}if(e.serverUrl===""||e.serverUrl==="https://compute.rhino3d.com/")throw new l("serverUrl must be set to your Compute server URL. The default public endpoint is not allowed.",f.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});return{...e,serverUrl:e.serverUrl.replace(/\/+$/,""),apiKey:e.apiKey,authToken:e.authToken,debug:e.debug??!1,suppressBrowserWarning:e.suppressBrowserWarning??e.suppressClientSideWarning}}};var J=async(r,e=null)=>{try{return await H(r,e)}catch(t){throw new l("Failed to extract files from compute response",f.INVALID_STATE,{context:{originalError:t instanceof Error?t.message:String(t)},originalError:t instanceof Error?t:void 0})}},w=async(r,e,t=null)=>{if(typeof document>"u"||typeof Blob>"u")throw new l("File download functionality is only available in browser environments. This function requires the DOM API (document, Blob).",f.BROWSER_ONLY,{context:{environment:typeof window<"u"?"browser (SSR)":"Node.js",documentAvailable:typeof document<"u",blobAvailable:typeof Blob<"u"}});try{let n=await H(r,t);await le(n,e)}catch(n){throw n instanceof l?n:new l("Failed to download files from compute response",f.INVALID_STATE,{context:{originalError:n instanceof Error?n.message:String(n)},originalError:n instanceof Error?n:void 0})}},H=async(r,e)=>{let t=[];if(r.forEach(n=>{let s=`${n.fileName}${n.fileType}`;if(n.subFolder&&n.subFolder.trim()!==""&&(s=`${n.subFolder}/${s}`),n.isBase64Encoded===!0&&n.data){let o=z(n.data);t.push({fileName:`${n.fileName}${n.fileType}`,content:new Uint8Array(o.buffer),path:s})}else n.isBase64Encoded===!1&&n.data&&t.push({fileName:`${n.fileName}${n.fileType}`,content:n.data,path:s})}),e){let n=Array.isArray(e)?e:[e],s=await Promise.all(n.map(async o=>{try{let a=await fetch(o.filePath);if(!a.ok)return m().warn(`Failed to fetch additional file from URL: ${o.filePath}`),null;let i=await(await a.blob()).arrayBuffer();return{fileName:o.fileName,content:new Uint8Array(i),path:o.fileName}}catch(a){return m().error(`Error fetching additional file from URL: ${o.filePath}`,a),null}}));t.push(...s.filter(o=>o!==null))}return t};async function le(r,e){let{zipSync:t,strToU8:n}=await import("fflate"),s={};r.forEach(u=>{s[u.path]=typeof u.content=="string"?n(u.content):u.content});let o=t(s,{level:6}),a=new Blob([o],{type:"application/zip"});pe(a,`${e}.zip`)}function pe(r,e){if(typeof document>"u")throw new l("saveFile requires a browser environment with DOM API access.",f.BROWSER_ONLY,{context:{function:"saveFile",requiredAPI:"document"}});let t=document.createElement("a");t.href=URL.createObjectURL(r),t.download=e,t.click(),URL.revokeObjectURL(t.href)}var A=new Map;function K(r,e){A.set(r,e)}K("Rhino.Geometry.Point3d",(r,e)=>{let t=e;return!t||typeof t.X!="number"?null:new r.Point([t.X,t.Y,t.Z])});K("Rhino.Geometry.Line",(r,e)=>{let t=e;return!t||!t.From||!t.To?null:new r.Line([t.From.X,t.From.Y,t.From.Z],[t.To.X,t.To.Y,t.To.Z])});function ce(r){if(A.has(r))return A.get(r);for(let[e,t]of A)if(r.startsWith(e))return t}function fe(r){return!r||typeof r!="object"?null:r.data??r.value??null}function Y(r,e,t){let n=ce(e);if(n)try{return n(t,r)}catch(s){m().warn(`Failed to decode Rhino type ${e}:`,s)}try{let s=fe(r);if(s)return t.CommonObject.decode(s)}catch(s){return m().warn(`Failed to decode ${e} with CommonObject:`,s),{__decodeError:!0,type:e,raw:r}}return r}var T={STRING:"System.String",INT:"System.Int32",DOUBLE:"System.Double",BOOL:"System.Boolean"},me="Rhino.Geometry.",de=["WebDisplay"],he="FileData";function Q(r){return de.some(e=>r.includes(e))}function X(r){if(typeof r!="string")return r;let e=r.trim();if(!(e.startsWith("{")||e.startsWith("[")||e.startsWith('"')))return r;try{let n=JSON.parse(e);if(typeof n=="string")try{return JSON.parse(n)}catch{return n}return n}catch{return r}}function ye(r,e,t){switch(e){case T.STRING:return typeof r!="string"?r:r.replace(/^"(.*)"$/,"$1");case T.INT:return Number.parseInt(r,10);case T.DOUBLE:return Number.parseFloat(r);case T.BOOL:return String(r).toLowerCase()==="true";default:return t&&e.startsWith(me)?Y(r,e,t):r}}function Z(r,e,t,n){if(typeof r!="string")return r;let s=t?X(r):r;return ye(s,e,n)}function ge(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.fileName=="string"&&typeof e.fileType=="string"&&"data"in e&&typeof e.isBase64Encoded=="boolean"&&typeof e.subFolder=="string"}function F(r,e){for(let t of Object.values(r))if(Array.isArray(t))for(let n of t)e(n)}function ee(r,e=!1,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a={};for(let u of r.values)F(u.InnerTree,i=>{if(Q(i.type)||o&&i.type!==T.STRING)return;let c=e?i.id:u.ParamName;if(!c)return;let d=Z(i.data,i.type,n,s);a[c]===void 0?a[c]=d:Array.isArray(a[c])?a[c].push(d):a[c]=[a[c],d]});return{values:a}}function re(r){let e=[];for(let t of r.values)F(t.InnerTree,n=>{if(!n.type.includes(he))return;let s=X(n.data);ge(s)&&e.push(s)});return e}function O(r,e,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a;if("byName"in e?a=r.values.find(i=>i.ParamName===e.byName):a=r.values.find(i=>{let c=!1;return F(i.InnerTree,d=>{d.id===e.byId&&(c=!0)}),c}),!a)return;let u=[];if(F(a.InnerTree,i=>{if("byId"in e&&i.id!==e.byId||Q(i.type)||o&&i.type!==T.STRING)return;let c=Z(i.data,i.type,n,s);u.push(c)}),u.length!==0)return u.length===1?u[0]:u}var D=class{constructor(e,t=!1){this.response=e;this.debug=t}getValues(e=!1,t={}){return ee(this.response,e,t)}getValue(e,t){return O(this.response,e,t)}getValueByParamName(e,t){return O(this.response,{byName:e},t)}getValueByParamId(e,t){return O(this.response,{byId:e},t)}async extractMeshesFromResponse(e){let t={debug:this.debug,...e},n;try{({getThreeMeshesFromComputeResponse:n}=await import("./visualization-XXHESVUI.js"))}catch(s){throw new l("Failed to load three.js visualization module. Ensure three.js is installed as a peer dependency.",f.INVALID_STATE,{context:{originalError:s instanceof Error?s.message:String(s)}})}return n(this.response,t)}getFileData(){return re(this.response)}getAndDownloadFiles(e,t){let n=this.getFileData();w(n,e,t)}};function N(r,e){e||typeof window<"u"&&m().warn(`Warning: ${r} is running on the client side. For better performance and security, consider running this on the server side.`)}async function C(r,e,t){t.debug&&N("solveGrasshopperDefinition",t.suppressBrowserWarning??t.suppressClientSideWarning);let n=M(e,r);be(n,t);let s=await v("grasshopper",n,t);if("pointer"in s){let{pointer:o,...a}=s;return a}return s}function M(r,e){let t={algo:null,pointer:null,values:e};return r instanceof Uint8Array?t.algo=q(r):/^https?:\/\//i.test(r)?t.pointer=r:$(r)?t.algo=r:t.algo=W(r),t}function be(r,e){e.cachesolve!=null&&(r.cachesolve=e.cachesolve),e.modelunits!=null&&(r.modelunits=e.modelunits),e.angletolerance!=null&&(r.angletolerance=e.angletolerance),e.absolutetolerance!=null&&(r.absolutetolerance=e.absolutetolerance),e.dataversion!=null&&(r.dataversion=e.dataversion)}function te(r){if(typeof r.default!="object"||r.default===null)return;if(!("innerTree"in r.default)){m().warn("Unexpected structure in input.default:",r.default),r.default=null;return}let e=r.default.innerTree;if(Object.keys(e).length===0){r.default=void 0;return}if(r.treeAccess||r.atMost&&r.atMost>1){let n={};for(let[s,o]of Object.entries(e))n[s]=o.map(a=>{if(typeof a.data=="string"){if(a.type==="System.Double"||a.type==="System.Int32"){let u=Number(a.data);return Number.isNaN(u)?a.data:u}if(a.type==="System.Boolean")return a.data.toLowerCase()==="true";if(a.type.startsWith("Rhino.Geometry")||a.type==="System.String")try{return JSON.parse(a.data)}catch{return a.data}}return a.data});r.default=n;return}let t=[];for(let n of Object.values(e))Array.isArray(n)&&n.forEach(s=>{s&&typeof s=="object"&&"data"in s&&t.push(s.data)});t.length===0?r.default=void 0:t.length===1?r.default=t[0]:r.default=t}function P(r,e){let{transform:t,setUndefinedOnEmpty:n=!0}=e;if(!(r.default===void 0||r.default===null))if(Array.isArray(r.default)){let s=r.default.map(t).filter(o=>o!==null);r.default=s.length>0?s:void 0}else{let s=t(r.default);s!==null?r.default=s:n&&(r.default=void 0)}}function Te(){return r=>{if(typeof r=="number")return r;if(typeof r=="string"){let e=Number(r.trim());return Number.isNaN(e)?null:e}return null}}function Se(){return r=>{if(typeof r=="boolean")return r;if(typeof r=="string"){let e=r.toLowerCase();if(e==="true")return!0;if(e==="false")return!1;throw new Error(`Invalid boolean string: "${r}"`)}return null}}function xe(){return r=>typeof r=="string"?r.startsWith('"')&&r.endsWith('"')||r.startsWith('"')?r.slice(1,-1):r:null}function Ce(){return r=>{if(typeof r=="string"){let e=r.trim();return e.startsWith('"')&&e.endsWith('"')&&(e=e.slice(1,-1).trim()),e}return null}}function Ie(r){P(r,{transform:Ce(),setUndefinedOnEmpty:!1})}function De(r="unknown"){return e=>{if(typeof e=="object"&&e!==null)return e;if(typeof e=="string"&&e.trim()!=="")try{let t=JSON.parse(e);return typeof t=="object"&&t!==null?t:(m().warn(`Parsed value for input ${r} is not an object`),null)}catch(t){return m().warn(`Failed to parse object value "${e}" for input ${r}`,t),null}return null}}function ne(r,e,t){let n=Number(r.toFixed(e));return Math.abs(r-n)<t?n:r}function Pe(r,e=1e-8){if(!Number.isFinite(r)||r===0)return .1;let t=Math.abs(r);if(t>=1){let h=String(r).split(".")[1];if(h&&h.length>0){let y=Math.min(h.length,12),g=Math.pow(10,-y),B=Number(g.toFixed(y));return Math.abs(B-g)<e?B:g}return 1}let n=String(r),s=n.toLowerCase().match(/e(-?\d+)/);if(s){let S=Number(s[1]);if(S<0||n.toLowerCase().includes("e-")){let h=Math.abs(S),y=Math.pow(10,-h),g=Number(y.toFixed(h));return Math.abs(g-y)<e?g:y}return .1}let o=12,u=t.toFixed(o).replace(/0+$/,""),i=Math.min((u.split(".")[1]||"").length,o);if(i===0)return .1;let c=Math.pow(10,-i),d=Number(c.toFixed(i));return Math.abs(d-c)<e?d:c}function se(r,e=1e-8){let t=r.paramType==="Integer";if(P(r,{transform:Te()}),t){Array.isArray(r.default)?r.default=r.default.map(o=>typeof o=="number"?Math.round(o):o):typeof r.default=="number"&&(r.default=Math.round(r.default)),r.stepSize=1;return}let n=Array.isArray(r.default)?r.default[0]:r.default,s;if(typeof n=="number"&&Number.isFinite(n)&&n!==0?s=n:typeof r.minimum=="number"&&Number.isFinite(r.minimum)&&r.minimum!==0?s=r.minimum:typeof r.maximum=="number"&&Number.isFinite(r.maximum)&&r.maximum!==0&&(s=r.maximum),s!==void 0?r.stepSize=Pe(s,e):r.stepSize=.1,typeof r.stepSize=="number"){let o=0,a=String(r.stepSize),u=a.toLowerCase().match(/e(-?\d+)/);if(u?o=Math.abs(Number(u[1])):o=a.split(".")[1]?.length??0,o===0&&typeof n=="number"&&n!==0&&Math.abs(n)<1){let i=Math.ceil(-Math.log10(Math.abs(n)));Number.isFinite(i)&&i>0&&(o=i)}o=Math.min(Math.max(o,0),12),Array.isArray(r.default)?r.default=r.default.map(i=>typeof i=="number"?ne(i,o,e):i):typeof r.default=="number"&&(r.default=ne(r.default,o,e))}}function ve(r){try{P(r,{transform:Se(),setUndefinedOnEmpty:!1})}catch(e){throw e instanceof Error?new l(e.message):e}}function Re(r){P(r,{transform:xe(),setUndefinedOnEmpty:!1})}function oe(r){P(r,{transform:De(r.nickname||"unnamed"),setUndefinedOnEmpty:!0})}function Ee(r){if(!r.values||typeof r.values!="object"||Object.keys(r.values).length===0)throw l.missingValues(r.nickname||"unnamed","ValueList");if(r.default!==void 0&&r.default!==null){let e=String(r.default).toLowerCase();Object.keys(r.values).some(n=>n.toLowerCase()===e)||m().warn(`ValueList input "${r.nickname||"unnamed"}" default value "${r.default}" is not in available values`)}}var ae={Number:se,Integer:se,Boolean:ve,Text:Re,ValueList:Ee,Geometry:oe,File:oe,Color:Ie};function we(r,e){let t=(r.atMost??1)>1;switch(r.paramType){case"Number":case"Integer":return{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,default:t?[0]:0};case"Boolean":return{...e,paramType:"Boolean",default:t?[!1]:!1};case"Text":return{...e,paramType:"Text",default:t?[""]:""};case"ValueList":return{...e,paramType:"ValueList",values:r.values??{},default:t?[r.default]:r.default};case"File":return{...e,paramType:"File",default:t?[null]:null};case"Color":return{...e,paramType:"Color",default:t?["0, 0, 0"]:"0, 0, 0"};default:return{...e,paramType:"Geometry",default:t?[null]:null}}}function ie(r){return k(r).input}function k(r){let e={description:r.description,name:r.name,nickname:r.nickname,treeAccess:r.treeAccess,groupName:r.groupName??"",id:r.id};try{te(r);let t=ae[r.paramType];if(!t)throw l.unknownParamType(r.paramType,r.name);switch(t(r),r.paramType){case"Number":case"Integer":return{input:{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,stepSize:r.stepSize,default:r.default}};case"Boolean":return{input:{...e,paramType:"Boolean",default:r.default}};case"Text":return{input:{...e,paramType:"Text",default:r.default}};case"ValueList":return{input:{...e,paramType:"ValueList",values:r.values,default:r.default}};case"Geometry":return{input:{...e,paramType:r.paramType,default:r.default}};case"File":return{input:{...e,paramType:r.paramType,acceptedFormats:r.acceptedFormats,default:r.default}};case"Color":return{input:{...e,paramType:"Color",default:r.default}};default:throw l.unknownParamType(r.paramType,r.name)}}catch(t){if(t instanceof l)return m().error(`Validation error for input ${r.name||"unknown"}:`,t.message),{input:we(r,e),error:{inputName:r.name||"unknown",paramType:r.paramType,message:t.message,code:t.code}};throw new l(t instanceof Error?t.message:String(t),"VALIDATION_ERROR",{context:{paramName:r.name,paramType:r.paramType},originalError:t instanceof Error?t:new Error(String(t))})}}function ue(r){return V(r).inputs}function V(r){let e=[],t=[];for(let n of r){let{input:s,error:o}=k(n);e.push(s),o&&t.push(o)}return{inputs:e,parseErrors:t}}async function I(r,e){let t=M(r,[]),n={};if(t.algo&&(n.algo=t.algo),t.pointer&&(n.pointer=t.pointer),!n.algo&&!n.pointer)throw new l("Definition must resolve to either a URL pointer or base64 algo",f.INVALID_INPUT,{context:{definition:r}});let s=await v("io",n,e);if(!s||typeof s!="object")throw new l("Invalid IO response structure",f.INVALID_INPUT,{context:{response:s,definition:r}});let o=j(s,{deep:!0});return{inputs:o.inputs,outputs:o.outputs}}async function E(r,e){N("fetchParsedDefinitionIO",e.suppressBrowserWarning??e.suppressClientSideWarning);let{inputs:t,outputs:n}=await I(r,e),{inputs:s,parseErrors:o}=V(t);return o.length>0?{inputs:s,outputs:n,parseErrors:o}:{inputs:s,outputs:n}}var G=class r{constructor(e){p(this,"innerTree");p(this,"paramName");this.paramName=e,this.innerTree={}}append(e,t){let n=r.formatPathString(e);this.innerTree[n]||(this.innerTree[n]=[]);let s=t.map(o=>({data:r.serializeValue(o)}));return this.innerTree[n].push(...s),this}appendSingle(e,t){return this.append(e,[t])}fromDataTreeDefault(e){this.innerTree={};for(let[t,n]of Object.entries(e)){if(!Array.isArray(n))continue;let s=r.parsePathString(t);this.append(s,n)}return this}appendFlat(e){let t=Array.isArray(e)?e:[e];return this.append([0],t)}flatten(){let e=[];for(let t of Object.values(this.innerTree))if(Array.isArray(t))for(let n of t)e.push(r.deserializeValue(n.data));return e}getPaths(){return Object.keys(this.innerTree)}getPath(e){let t=r.formatPathString(e),n=this.innerTree[t];if(n)return n.map(s=>r.deserializeValue(s.data))}toComputeFormat(){return{ParamName:this.paramName,InnerTree:this.innerTree}}getInnerTree(){return this.innerTree}getParamName(){return this.paramName}static fromInputParams(e){return e.filter(t=>r.hasValidValue(t.default)).map(t=>{let n=new r(t.nickname||"unnamed"),s=t.default;if(t.treeAccess&&r.isDataTreeStructure(s))n.fromDataTreeDefault(s),r.isNumericInput(t)&&n.applyNumericConstraints(t.minimum,t.maximum,t.nickname||"unnamed");else{let o=Array.isArray(s)?s:[s],a=r.processValues(o,t);n.appendFlat(a)}return n.toComputeFormat()})}static fromInputParam(e){return r.hasValidValue(e.default)?r.fromInputParams([e])[0]:void 0}static replaceTreeValue(e,t,n){let s=e.length>0&&e[0]instanceof r,o=r.buildFromValue(t,n);if(s){let c=e,d=c.findIndex(S=>S.getParamName()===t);return d!==-1?c[d]=o:c.push(o),c}let a=e,u=o.toComputeFormat(),i=a.findIndex(c=>c.ParamName===t);return i!==-1?a[i]=u:a.push(u),a}static buildFromValue(e,t){let n=new r(e);return typeof t=="object"&&t!==null&&!Array.isArray(t)&&r.isDataTreeStructure(t)?n.fromDataTreeDefault(t):n.appendFlat(t),n}static getTreeValue(e,t){let s=e.length>0&&e[0]instanceof r?r.readFromBuilders(e,t):r.readFromDataTrees(e,t);return s===null||s.length===0?null:s.length===1?s[0]:s}static readFromBuilders(e,t){let n=e.find(s=>s.getParamName()===t);return n?n.flatten():null}static readFromDataTrees(e,t){let n=e.find(a=>a.ParamName===t);if(!n?.InnerTree)return null;let s=Object.keys(n.InnerTree)[0];if(!s)return null;let o=n.InnerTree[s];return Array.isArray(o)?o.map(a=>a?.data!==void 0?r.deserializeValue(a.data):null).filter(a=>a!==null):o?.data!==void 0?[r.deserializeValue(o.data)]:o!==void 0?[o]:null}static parsePathString(e){let t=e.match(/^\{([\d;]*)\}$/);return t?t[1]===""?[]:t[1].split(";").map(Number):(m().warn(`Invalid TreeBuilder path format: ${e}, using [0]`),[0])}static formatPathString(e){return`{${e.join(";")}}`}applyNumericConstraints(e,t,n){for(let s of Object.values(this.innerTree))if(Array.isArray(s))for(let o of s){let a=r.deserializeValue(o.data);if(typeof a=="number"){let u=r.clampValue(a,e,t,n);o.data=r.serializeValue(u)}}}static serializeValue(e){return typeof e=="boolean"||typeof e=="number"||typeof e=="string"?e:typeof e=="object"&&e!==null?JSON.stringify(e):String(e)}static deserializeValue(e){if(typeof e=="boolean"||typeof e=="number"||typeof e!="string")return e;if(e.startsWith("{")||e.startsWith("["))try{return JSON.parse(e)}catch{return e}return isNaN(Number(e))?e==="true"?!0:e==="false"?!1:e:Number(e)}static hasValidValue(e){return e==null?!1:typeof e=="string"?!0:!(Array.isArray(e)&&e.length===0||typeof e=="object"&&!Array.isArray(e)&&Object.keys(e).length===0)}static isDataTreeStructure(e){return typeof e!="object"||e===null||Array.isArray(e)?!1:Object.entries(e).every(([t,n])=>typeof t=="string"&&/^\{[\d;]+\}$/.test(t)&&Array.isArray(n))}static isNumericInput(e){return e.paramType==="Number"||e.paramType==="Integer"}static processValues(e,t){return e.map(n=>r.isNumericInput(t)&&typeof n=="number"?r.clampValue(n,t.minimum,t.maximum,t.nickname||"unnamed"):n).filter(n=>n!=null)}static clampValue(e,t,n,s){let o=e;return t!=null&&o<t&&(m().warn(`${s}: ${e} below min ${t}, clamping`),o=t),n!=null&&o>n&&(m().warn(`${s}: ${e} above max ${n}, clamping`),o=n),o}};export{R as a,b,x as c,J as d,w as e,D as f,C as g,ie as h,ue as i,I as j,E as k,G as l};
|
|
2
|
-
//# sourceMappingURL=chunk-
|
|
1
|
+
import{a as W,b as $,c as z,d as q}from"./chunk-XLHA5YPH.js";import{b as p,c as f,d as l,e as m,h as v,i as U,k as j}from"./chunk-RHULSS7S.js";function L(r){let e=new WeakSet,t=n=>{if(n==null)return JSON.stringify(n);if(typeof n=="number")return Number.isFinite(n)?String(n):JSON.stringify(null);if(typeof n=="string"||typeof n=="boolean")return JSON.stringify(n);if(typeof n=="bigint")return JSON.stringify(n.toString());if(n instanceof Uint8Array){let s=n.length>64?Array.from(n.slice(0,32)).concat(Array.from(n.slice(-32))):Array.from(n);return JSON.stringify({__u8:!0,len:n.length,sample:s})}return Array.isArray(n)?`[${n.map(t).join(",")}]`:typeof n=="object"?e.has(n)?JSON.stringify("[Circular]"):(e.add(n),`{${Object.keys(n).sort().map(a=>`${JSON.stringify(a)}:${t(n[a])}`).join(",")}}`):JSON.stringify(null)};return t(r)}function _(r){let e=2166136261;for(let t=0;t<r.length;t++)e^=r.charCodeAt(t),e=e+((e<<1)+(e<<4)+(e<<7)+(e<<8)+(e<<24))>>>0;return e.toString(16).padStart(8,"0")}function R(r,e){let t=typeof r=="string"?r:L({__u8:!0,len:r.length});return _(`${t}|${L(e)}`)}var b=class{constructor(e,t,n={}){p(this,"executor");p(this,"baseConfig");p(this,"mode");p(this,"maxConcurrent");p(this,"timeoutMs");p(this,"retry");p(this,"cacheEnabled");p(this,"cacheMax");p(this,"cacheTtl");p(this,"cache",new Map);p(this,"onStart");p(this,"onSettle");p(this,"onSuperseded");p(this,"subscribers",new Set);p(this,"inFlight",new Set);p(this,"pendingForLatestWins",null);p(this,"fifoQueue",[]);p(this,"_lastResult",null);p(this,"_lastError",null);p(this,"_lastDurationMs",null);p(this,"disposed",!1);this.executor=e,this.baseConfig=t,this.mode=n.mode??"latest-wins",this.maxConcurrent=Math.max(1,n.maxConcurrent??(this.mode==="parallel"?4:1)),this.timeoutMs=n.timeoutMs,this.retry=n.retry;let s=n.cache;this.cacheEnabled=s!==void 0&&s!==!1;let o=typeof s=="object"?s:{};this.cacheMax=o.maxEntries??50,this.cacheTtl=o.ttlMs??0,this.onStart=n.onStart,this.onSettle=n.onSettle,this.onSuperseded=n.onSuperseded}get isSolving(){return this.inFlight.size>0}get hasPending(){return this.pendingForLatestWins!==null||this.fifoQueue.length>0}get inFlightCount(){return this.inFlight.size}get queueDepth(){return this.fifoQueue.length+(this.pendingForLatestWins?1:0)}get lastResult(){return this._lastResult}get lastError(){return this._lastError}get lastDurationMs(){return this._lastDurationMs}subscribe(e){return this.subscribers.add(e),()=>this.subscribers.delete(e)}notify(){for(let e of this.subscribers)try{e()}catch(t){m().error("[SolveScheduler] subscriber threw:",t)}}solve(e,t,n){if(this.disposed)return Promise.reject(new l("SolveScheduler has been disposed and cannot be used",f.INVALID_STATE));let s=R(e,t),o={key:s,enqueuedAt:Date.now(),startedAt:null};if(this.cacheEnabled){let a=this.readCache(s);if(a){let u={status:"success",response:a,durationMs:0,fromCache:!0};return this._lastResult=a,this._lastError=null,this._lastDurationMs=0,this.runHook(this.onStart,o),this.runHook(this.onSettle,o,u),this.notify(),Promise.resolve(a)}}return new Promise((a,u)=>{let i={definition:e,dataTree:t,ctx:o,resolve:a,reject:u,externalSignal:n?.signal};if(i.externalSignal?.aborted){let c=this.makeAbortError(o);i.settled={error:c},u(c);return}this.enqueue(i)})}enqueue(e){switch(this.mode){case"latest-wins":{this.pendingForLatestWins&&(this.supersede(this.pendingForLatestWins),this.pendingForLatestWins=null);for(let t of this.inFlight)this.supersede(t),t.controller.abort();this.inFlight.size===0?this.execute(e):this.pendingForLatestWins=e;break}case"queue":case"parallel":{this.inFlight.size<this.maxConcurrent?this.execute(e):this.fifoQueue.push(e);break}}this.notify()}async execute(e){let t=new AbortController,n={...e,controller:t};this.inFlight.add(n),e.ctx.startedAt=Date.now();let s=()=>t.abort();e.externalSignal?.addEventListener("abort",s,{once:!0}),this.runHook(this.onStart,e.ctx),this.notify();let o=performance.now();try{let a={...this.baseConfig,signal:t.signal,...this.timeoutMs!==void 0&&{timeoutMs:this.timeoutMs},...this.retry!==void 0&&{retry:this.retry}},u=await this.executor(e.definition,e.dataTree,a),i=performance.now()-o;if(this.cacheEnabled&&this.writeCache(e.ctx.key,u),e.settled)return;e.settled={ok:!0},this._lastResult=u,this._lastError=null,this._lastDurationMs=i,e.resolve(u),this.runHook(this.onSettle,e.ctx,{status:"success",response:u,durationMs:i,fromCache:!1})}catch(a){let u=performance.now()-o,i=this.normalizeExecutionError(a,n),c=!!n.settled;this._lastError=i,this._lastDurationMs=u,c||(n.settled={error:i},e.reject(i),this.runHook(this.onSettle,e.ctx,{status:"error",error:i,durationMs:u}))}finally{e.externalSignal?.removeEventListener("abort",s),this.inFlight.delete(n),this.drainNext(),this.notify()}}drainNext(){if(!this.disposed){if(this.mode==="latest-wins"){if(this.pendingForLatestWins&&this.inFlight.size===0){let e=this.pendingForLatestWins;this.pendingForLatestWins=null,this.execute(e)}return}for(;this.fifoQueue.length>0&&this.inFlight.size<this.maxConcurrent;){let e=this.fifoQueue.shift();this.execute(e)}}}supersede(e){if(e.settled)return;let t=new l("Superseded by newer solve",f.SUPERSEDED,{context:{key:e.ctx.key,enqueuedAt:e.ctx.enqueuedAt}});e.settled={error:t},e.reject(t),this.runHook(this.onSuperseded,e.ctx)}makeAbortError(e){return new l("Request aborted by caller",f.ABORTED,{context:{key:e.key,enqueuedAt:e.enqueuedAt}})}isAbortLikeError(e){if(e instanceof Error){if(e.name==="AbortError")return!0;if(typeof DOMException<"u"&&e instanceof DOMException)return e.name==="AbortError"}return!1}normalizeExecutionError(e,t){return t.settled&&"error"in t.settled?t.settled.error:e instanceof l?e:this.isAbortLikeError(e)?this.makeAbortError(t.ctx):new l(e instanceof Error?e.message:String(e),f.UNKNOWN_ERROR,{originalError:e instanceof Error?e:new Error(String(e))})}cancelAll(){for(this.pendingForLatestWins&&(this.rejectAsAborted(this.pendingForLatestWins),this.pendingForLatestWins=null);this.fifoQueue.length>0;){let e=this.fifoQueue.shift();this.rejectAsAborted(e)}for(let e of this.inFlight){if(!e.settled){let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t),this.runHook(this.onSettle,e.ctx,{status:"error",error:t,durationMs:e.ctx.startedAt?performance.now()-e.ctx.startedAt:0})}e.controller.abort()}this.notify()}rejectAsAborted(e){if(e.settled)return;let t=this.makeAbortError(e.ctx);e.settled={error:t},e.reject(t)}readCache(e){if(!this.cacheEnabled)return null;let t=this.cache.get(e);return t?this.cacheTtl>0&&Date.now()-t.insertedAt>this.cacheTtl?(this.cache.delete(e),null):(this.cache.delete(e),this.cache.set(e,t),t.response):null}writeCache(e,t){if(this.cacheEnabled)for(this.cache.set(e,{response:t,insertedAt:Date.now()});this.cache.size>this.cacheMax;){let n=this.cache.keys().next().value;if(n===void 0)break;this.cache.delete(n)}}clearCache(){this.cache.clear()}dispose(){this.disposed||(this.disposed=!0,this.cancelAll(),this.subscribers.clear(),this.cache.clear())}runHook(e,...t){if(e)try{e(...t)}catch(n){m().error("[SolveScheduler] hook threw:",n)}}};var x=class r{constructor(e){p(this,"config");p(this,"serverStats");p(this,"disposed",!1);this.config=this.normalizeComputeConfig(e),this.serverStats=new U(this.config.serverUrl,this.config.apiKey)}static async create(e){let t=new r(e);if(!await t.serverStats.isServerOnline())throw new l("Rhino Compute server is not online",f.NETWORK_ERROR,{context:{serverUrl:t.config.serverUrl}});return t}getConfig(){return this.ensureNotDisposed(),{...this.config}}async getIO(e){return this.ensureNotDisposed(),E(e,this.config)}async getRawIO(e){return this.ensureNotDisposed(),I(e,this.config)}async solve(e,t,n){this.ensureNotDisposed();try{if(typeof e=="string"&&!e?.trim())throw new l("Definition URL/content is required",f.INVALID_INPUT,{context:{receivedUrl:e}});if(e instanceof Uint8Array&&e.length===0)throw new l("Definition content is empty",f.INVALID_INPUT);let s={...this.config,...n?.signal!==void 0&&{signal:n.signal},...n?.timeoutMs!==void 0&&{timeoutMs:n.timeoutMs},...n?.retry!==void 0&&{retry:n.retry}},o=await C(t,e,s);if(o?.errors&&o.errors.length>0)throw new l(o.errors.join("; ")||"Computation failed",f.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t,errors:o.errors,warnings:o.warnings}});return o}catch(s){throw this.config.debug&&m().error("Compute failed:",s),s instanceof l?s:new l(s instanceof Error?s.message:String(s),f.COMPUTATION_ERROR,{context:{definition:typeof e=="string"&&e.length<200?e:"...content...",inputs:t},originalError:s instanceof Error?s:new Error(String(s))})}}createScheduler(e){this.ensureNotDisposed();let t=(n,s,o)=>C(s,n,o);return new b(t,this.config,e)}async dispose(){this.disposed||(this.disposed=!0,await this.serverStats.dispose())}ensureNotDisposed(){if(this.disposed)throw new l("GrasshopperClient has been disposed and cannot be used",f.INVALID_STATE)}normalizeComputeConfig(e){if(!e.serverUrl?.trim())throw new l("serverUrl is required",f.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});try{new URL(e.serverUrl)}catch{throw new l("serverUrl must be a valid URL",f.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}})}if(e.serverUrl===""||e.serverUrl==="https://compute.rhino3d.com/")throw new l("serverUrl must be set to your Compute server URL. The default public endpoint is not allowed.",f.INVALID_CONFIG,{context:{receivedServerUrl:e.serverUrl}});return{...e,serverUrl:e.serverUrl.replace(/\/+$/,""),apiKey:e.apiKey,authToken:e.authToken,debug:e.debug??!1,suppressBrowserWarning:e.suppressBrowserWarning??e.suppressClientSideWarning}}};var J=async(r,e=null)=>{try{return await H(r,e)}catch(t){throw new l("Failed to extract files from compute response",f.INVALID_STATE,{context:{originalError:t instanceof Error?t.message:String(t)},originalError:t instanceof Error?t:void 0})}},w=async(r,e,t=null)=>{if(typeof document>"u"||typeof Blob>"u")throw new l("File download functionality is only available in browser environments. This function requires the DOM API (document, Blob).",f.BROWSER_ONLY,{context:{environment:typeof window<"u"?"browser (SSR)":"Node.js",documentAvailable:typeof document<"u",blobAvailable:typeof Blob<"u"}});try{let n=await H(r,t);await le(n,e)}catch(n){throw n instanceof l?n:new l("Failed to download files from compute response",f.INVALID_STATE,{context:{originalError:n instanceof Error?n.message:String(n)},originalError:n instanceof Error?n:void 0})}},H=async(r,e)=>{let t=[];if(r.forEach(n=>{let s=`${n.fileName}${n.fileType}`;if(n.subFolder&&n.subFolder.trim()!==""&&(s=`${n.subFolder}/${s}`),n.isBase64Encoded===!0&&n.data){let o=z(n.data);t.push({fileName:`${n.fileName}${n.fileType}`,content:new Uint8Array(o.buffer),path:s})}else n.isBase64Encoded===!1&&n.data&&t.push({fileName:`${n.fileName}${n.fileType}`,content:n.data,path:s})}),e){let n=Array.isArray(e)?e:[e],s=await Promise.all(n.map(async o=>{try{let a=await fetch(o.filePath);if(!a.ok)return m().warn(`Failed to fetch additional file from URL: ${o.filePath}`),null;let i=await(await a.blob()).arrayBuffer();return{fileName:o.fileName,content:new Uint8Array(i),path:o.fileName}}catch(a){return m().error(`Error fetching additional file from URL: ${o.filePath}`,a),null}}));t.push(...s.filter(o=>o!==null))}return t};async function le(r,e){let{zipSync:t,strToU8:n}=await import("fflate"),s={};r.forEach(u=>{s[u.path]=typeof u.content=="string"?n(u.content):u.content});let o=t(s,{level:6}),a=new Blob([o],{type:"application/zip"});pe(a,`${e}.zip`)}function pe(r,e){if(typeof document>"u")throw new l("saveFile requires a browser environment with DOM API access.",f.BROWSER_ONLY,{context:{function:"saveFile",requiredAPI:"document"}});let t=document.createElement("a");t.href=URL.createObjectURL(r),t.download=e,t.click(),URL.revokeObjectURL(t.href)}var A=new Map;function K(r,e){A.set(r,e)}K("Rhino.Geometry.Point3d",(r,e)=>{let t=e;return!t||typeof t.X!="number"?null:new r.Point([t.X,t.Y,t.Z])});K("Rhino.Geometry.Line",(r,e)=>{let t=e;return!t||!t.From||!t.To?null:new r.Line([t.From.X,t.From.Y,t.From.Z],[t.To.X,t.To.Y,t.To.Z])});function ce(r){if(A.has(r))return A.get(r);for(let[e,t]of A)if(r.startsWith(e))return t}function fe(r){return!r||typeof r!="object"?null:r.data??r.value??null}function Y(r,e,t){let n=ce(e);if(n)try{return n(t,r)}catch(s){m().warn(`Failed to decode Rhino type ${e}:`,s)}try{let s=fe(r);if(s)return t.CommonObject.decode(s)}catch(s){return m().warn(`Failed to decode ${e} with CommonObject:`,s),{__decodeError:!0,type:e,raw:r}}return r}var T={STRING:"System.String",INT:"System.Int32",DOUBLE:"System.Double",BOOL:"System.Boolean"},me="Rhino.Geometry.",de=["WebDisplay"],he="FileData";function Q(r){return de.some(e=>r.includes(e))}function X(r){if(typeof r!="string")return r;let e=r.trim();if(!(e.startsWith("{")||e.startsWith("[")||e.startsWith('"')))return r;try{let n=JSON.parse(e);if(typeof n=="string")try{return JSON.parse(n)}catch{return n}return n}catch{return r}}function ye(r,e,t){switch(e){case T.STRING:return typeof r!="string"?r:r.replace(/^"(.*)"$/,"$1");case T.INT:return Number.parseInt(r,10);case T.DOUBLE:return Number.parseFloat(r);case T.BOOL:return String(r).toLowerCase()==="true";default:return t&&e.startsWith(me)?Y(r,e,t):r}}function Z(r,e,t,n){if(typeof r!="string")return r;let s=t?X(r):r;return ye(s,e,n)}function ge(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.fileName=="string"&&typeof e.fileType=="string"&&"data"in e&&typeof e.isBase64Encoded=="boolean"&&typeof e.subFolder=="string"}function F(r,e){for(let t of Object.values(r))if(Array.isArray(t))for(let n of t)e(n)}function ee(r,e=!1,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a={};for(let u of r.values)F(u.InnerTree,i=>{if(Q(i.type)||o&&i.type!==T.STRING)return;let c=e?i.id:u.ParamName;if(!c)return;let d=Z(i.data,i.type,n,s);a[c]===void 0?a[c]=d:Array.isArray(a[c])?a[c].push(d):a[c]=[a[c],d]});return{values:a}}function re(r){let e=[];for(let t of r.values)F(t.InnerTree,n=>{if(!n.type.includes(he))return;let s=X(n.data);ge(s)&&e.push(s)});return e}function O(r,e,t={}){let{parseValues:n=!0,rhino:s,stringOnly:o=!1}=t,a;if("byName"in e?a=r.values.find(i=>i.ParamName===e.byName):a=r.values.find(i=>{let c=!1;return F(i.InnerTree,d=>{d.id===e.byId&&(c=!0)}),c}),!a)return;let u=[];if(F(a.InnerTree,i=>{if("byId"in e&&i.id!==e.byId||Q(i.type)||o&&i.type!==T.STRING)return;let c=Z(i.data,i.type,n,s);u.push(c)}),u.length!==0)return u.length===1?u[0]:u}var D=class{constructor(e,t=!1){this.response=e;this.debug=t}getValues(e=!1,t={}){return ee(this.response,e,t)}getValue(e,t){return O(this.response,e,t)}getValueByParamName(e,t){return O(this.response,{byName:e},t)}getValueByParamId(e,t){return O(this.response,{byId:e},t)}async extractMeshesFromResponse(e){let t={debug:this.debug,...e},n;try{({getThreeMeshesFromComputeResponse:n}=await import("./visualization-7TK4UEZL.js"))}catch(s){throw new l("Failed to load three.js visualization module. Ensure three.js is installed as a peer dependency.",f.INVALID_STATE,{context:{originalError:s instanceof Error?s.message:String(s)}})}return n(this.response,t)}getFileData(){return re(this.response)}getAndDownloadFiles(e,t){let n=this.getFileData();w(n,e,t)}};function N(r,e){e||typeof window<"u"&&m().warn(`Warning: ${r} is running on the client side. For better performance and security, consider running this on the server side.`)}async function C(r,e,t){t.debug&&N("solveGrasshopperDefinition",t.suppressBrowserWarning??t.suppressClientSideWarning);let n=M(e,r);be(n,t);let s=await v("grasshopper",n,t);if("pointer"in s){let{pointer:o,...a}=s;return a}return s}function M(r,e){let t={algo:null,pointer:null,values:e};return r instanceof Uint8Array?t.algo=q(r):/^https?:\/\//i.test(r)?t.pointer=r:$(r)?t.algo=r:t.algo=W(r),t}function be(r,e){e.cachesolve!=null&&(r.cachesolve=e.cachesolve),e.modelunits!=null&&(r.modelunits=e.modelunits),e.angletolerance!=null&&(r.angletolerance=e.angletolerance),e.absolutetolerance!=null&&(r.absolutetolerance=e.absolutetolerance),e.dataversion!=null&&(r.dataversion=e.dataversion)}function te(r){if(typeof r.default!="object"||r.default===null)return;if(!("innerTree"in r.default)){m().warn("Unexpected structure in input.default:",r.default),r.default=null;return}let e=r.default.innerTree;if(Object.keys(e).length===0){r.default=void 0;return}if(r.treeAccess||r.atMost&&r.atMost>1){let n={};for(let[s,o]of Object.entries(e))n[s]=o.map(a=>{if(typeof a.data=="string"){if(a.type==="System.Double"||a.type==="System.Int32"){let u=Number(a.data);return Number.isNaN(u)?a.data:u}if(a.type==="System.Boolean")return a.data.toLowerCase()==="true";if(a.type.startsWith("Rhino.Geometry")||a.type==="System.String")try{return JSON.parse(a.data)}catch{return a.data}}return a.data});r.default=n;return}let t=[];for(let n of Object.values(e))Array.isArray(n)&&n.forEach(s=>{s&&typeof s=="object"&&"data"in s&&t.push(s.data)});t.length===0?r.default=void 0:t.length===1?r.default=t[0]:r.default=t}function P(r,e){let{transform:t,setUndefinedOnEmpty:n=!0}=e;if(!(r.default===void 0||r.default===null))if(Array.isArray(r.default)){let s=r.default.map(t).filter(o=>o!==null);r.default=s.length>0?s:void 0}else{let s=t(r.default);s!==null?r.default=s:n&&(r.default=void 0)}}function Te(){return r=>{if(typeof r=="number")return r;if(typeof r=="string"){let e=Number(r.trim());return Number.isNaN(e)?null:e}return null}}function Se(){return r=>{if(typeof r=="boolean")return r;if(typeof r=="string"){let e=r.toLowerCase();if(e==="true")return!0;if(e==="false")return!1;throw new Error(`Invalid boolean string: "${r}"`)}return null}}function xe(){return r=>typeof r=="string"?r.startsWith('"')&&r.endsWith('"')||r.startsWith('"')?r.slice(1,-1):r:null}function Ce(){return r=>{if(typeof r=="string"){let e=r.trim();return e.startsWith('"')&&e.endsWith('"')&&(e=e.slice(1,-1).trim()),e}return null}}function Ie(r){P(r,{transform:Ce(),setUndefinedOnEmpty:!1})}function De(r="unknown"){return e=>{if(typeof e=="object"&&e!==null)return e;if(typeof e=="string"&&e.trim()!=="")try{let t=JSON.parse(e);return typeof t=="object"&&t!==null?t:(m().warn(`Parsed value for input ${r} is not an object`),null)}catch(t){return m().warn(`Failed to parse object value "${e}" for input ${r}`,t),null}return null}}function ne(r,e,t){let n=Number(r.toFixed(e));return Math.abs(r-n)<t?n:r}function Pe(r,e=1e-8){if(!Number.isFinite(r)||r===0)return .1;let t=Math.abs(r);if(t>=1){let h=String(r).split(".")[1];if(h&&h.length>0){let y=Math.min(h.length,12),g=Math.pow(10,-y),B=Number(g.toFixed(y));return Math.abs(B-g)<e?B:g}return 1}let n=String(r),s=n.toLowerCase().match(/e(-?\d+)/);if(s){let S=Number(s[1]);if(S<0||n.toLowerCase().includes("e-")){let h=Math.abs(S),y=Math.pow(10,-h),g=Number(y.toFixed(h));return Math.abs(g-y)<e?g:y}return .1}let o=12,u=t.toFixed(o).replace(/0+$/,""),i=Math.min((u.split(".")[1]||"").length,o);if(i===0)return .1;let c=Math.pow(10,-i),d=Number(c.toFixed(i));return Math.abs(d-c)<e?d:c}function se(r,e=1e-8){let t=r.paramType==="Integer";if(P(r,{transform:Te()}),t){Array.isArray(r.default)?r.default=r.default.map(o=>typeof o=="number"?Math.round(o):o):typeof r.default=="number"&&(r.default=Math.round(r.default)),r.stepSize=1;return}let n=Array.isArray(r.default)?r.default[0]:r.default,s;if(typeof n=="number"&&Number.isFinite(n)&&n!==0?s=n:typeof r.minimum=="number"&&Number.isFinite(r.minimum)&&r.minimum!==0?s=r.minimum:typeof r.maximum=="number"&&Number.isFinite(r.maximum)&&r.maximum!==0&&(s=r.maximum),s!==void 0?r.stepSize=Pe(s,e):r.stepSize=.1,typeof r.stepSize=="number"){let o=0,a=String(r.stepSize),u=a.toLowerCase().match(/e(-?\d+)/);if(u?o=Math.abs(Number(u[1])):o=a.split(".")[1]?.length??0,o===0&&typeof n=="number"&&n!==0&&Math.abs(n)<1){let i=Math.ceil(-Math.log10(Math.abs(n)));Number.isFinite(i)&&i>0&&(o=i)}o=Math.min(Math.max(o,0),12),Array.isArray(r.default)?r.default=r.default.map(i=>typeof i=="number"?ne(i,o,e):i):typeof r.default=="number"&&(r.default=ne(r.default,o,e))}}function ve(r){try{P(r,{transform:Se(),setUndefinedOnEmpty:!1})}catch(e){throw e instanceof Error?new l(e.message):e}}function Re(r){P(r,{transform:xe(),setUndefinedOnEmpty:!1})}function oe(r){P(r,{transform:De(r.nickname||"unnamed"),setUndefinedOnEmpty:!0})}function Ee(r){if(!r.values||typeof r.values!="object"||Object.keys(r.values).length===0)throw l.missingValues(r.nickname||"unnamed","ValueList");if(r.default!==void 0&&r.default!==null){let e=String(r.default).toLowerCase();Object.keys(r.values).some(n=>n.toLowerCase()===e)||m().warn(`ValueList input "${r.nickname||"unnamed"}" default value "${r.default}" is not in available values`)}}var ae={Number:se,Integer:se,Boolean:ve,Text:Re,ValueList:Ee,Geometry:oe,File:oe,Color:Ie};function we(r,e){let t=(r.atMost??1)>1;switch(r.paramType){case"Number":case"Integer":return{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,default:t?[0]:0};case"Boolean":return{...e,paramType:"Boolean",default:t?[!1]:!1};case"Text":return{...e,paramType:"Text",default:t?[""]:""};case"ValueList":return{...e,paramType:"ValueList",values:r.values??{},default:t?[r.default]:r.default};case"File":return{...e,paramType:"File",default:t?[null]:null};case"Color":return{...e,paramType:"Color",default:t?["0, 0, 0"]:"0, 0, 0"};default:return{...e,paramType:"Geometry",default:t?[null]:null}}}function ie(r){return k(r).input}function k(r){let e={description:r.description,name:r.name,nickname:r.nickname,treeAccess:r.treeAccess,groupName:r.groupName??"",id:r.id};try{te(r);let t=ae[r.paramType];if(!t)throw l.unknownParamType(r.paramType,r.name);switch(t(r),r.paramType){case"Number":case"Integer":return{input:{...e,paramType:r.paramType,minimum:r.minimum,maximum:r.maximum,atLeast:r.atLeast,atMost:r.atMost,stepSize:r.stepSize,default:r.default}};case"Boolean":return{input:{...e,paramType:"Boolean",default:r.default}};case"Text":return{input:{...e,paramType:"Text",default:r.default}};case"ValueList":return{input:{...e,paramType:"ValueList",values:r.values,default:r.default}};case"Geometry":return{input:{...e,paramType:r.paramType,default:r.default}};case"File":return{input:{...e,paramType:r.paramType,acceptedFormats:r.acceptedFormats,default:r.default}};case"Color":return{input:{...e,paramType:"Color",default:r.default}};default:throw l.unknownParamType(r.paramType,r.name)}}catch(t){if(t instanceof l)return m().error(`Validation error for input ${r.name||"unknown"}:`,t.message),{input:we(r,e),error:{inputName:r.name||"unknown",paramType:r.paramType,message:t.message,code:t.code}};throw new l(t instanceof Error?t.message:String(t),"VALIDATION_ERROR",{context:{paramName:r.name,paramType:r.paramType},originalError:t instanceof Error?t:new Error(String(t))})}}function ue(r){return V(r).inputs}function V(r){let e=[],t=[];for(let n of r){let{input:s,error:o}=k(n);e.push(s),o&&t.push(o)}return{inputs:e,parseErrors:t}}async function I(r,e){let t=M(r,[]),n={};if(t.algo&&(n.algo=t.algo),t.pointer&&(n.pointer=t.pointer),!n.algo&&!n.pointer)throw new l("Definition must resolve to either a URL pointer or base64 algo",f.INVALID_INPUT,{context:{definition:r}});let s=await v("io",n,e);if(!s||typeof s!="object")throw new l("Invalid IO response structure",f.INVALID_INPUT,{context:{response:s,definition:r}});let o=j(s,{deep:!0});return{inputs:o.inputs,outputs:o.outputs}}async function E(r,e){N("fetchParsedDefinitionIO",e.suppressBrowserWarning??e.suppressClientSideWarning);let{inputs:t,outputs:n}=await I(r,e),{inputs:s,parseErrors:o}=V(t);return o.length>0?{inputs:s,outputs:n,parseErrors:o}:{inputs:s,outputs:n}}var G=class r{constructor(e){p(this,"innerTree");p(this,"paramName");this.paramName=e,this.innerTree={}}append(e,t){let n=r.formatPathString(e);this.innerTree[n]||(this.innerTree[n]=[]);let s=t.map(o=>({data:r.serializeValue(o)}));return this.innerTree[n].push(...s),this}appendSingle(e,t){return this.append(e,[t])}fromDataTreeDefault(e){this.innerTree={};for(let[t,n]of Object.entries(e)){if(!Array.isArray(n))continue;let s=r.parsePathString(t);this.append(s,n)}return this}appendFlat(e){let t=Array.isArray(e)?e:[e];return this.append([0],t)}flatten(){let e=[];for(let t of Object.values(this.innerTree))if(Array.isArray(t))for(let n of t)e.push(r.deserializeValue(n.data));return e}getPaths(){return Object.keys(this.innerTree)}getPath(e){let t=r.formatPathString(e),n=this.innerTree[t];if(n)return n.map(s=>r.deserializeValue(s.data))}toComputeFormat(){return{ParamName:this.paramName,InnerTree:this.innerTree}}getInnerTree(){return this.innerTree}getParamName(){return this.paramName}static fromInputParams(e){return e.filter(t=>r.hasValidValue(t.default)).map(t=>{let n=new r(t.nickname||"unnamed"),s=t.default;if(t.treeAccess&&r.isDataTreeStructure(s))n.fromDataTreeDefault(s),r.isNumericInput(t)&&n.applyNumericConstraints(t.minimum,t.maximum,t.nickname||"unnamed");else{let o=Array.isArray(s)?s:[s],a=r.processValues(o,t);n.appendFlat(a)}return n.toComputeFormat()})}static fromInputParam(e){return r.hasValidValue(e.default)?r.fromInputParams([e])[0]:void 0}static replaceTreeValue(e,t,n){let s=e.length>0&&e[0]instanceof r,o=r.buildFromValue(t,n);if(s){let c=e,d=c.findIndex(S=>S.getParamName()===t);return d!==-1?c[d]=o:c.push(o),c}let a=e,u=o.toComputeFormat(),i=a.findIndex(c=>c.ParamName===t);return i!==-1?a[i]=u:a.push(u),a}static buildFromValue(e,t){let n=new r(e);return typeof t=="object"&&t!==null&&!Array.isArray(t)&&r.isDataTreeStructure(t)?n.fromDataTreeDefault(t):n.appendFlat(t),n}static getTreeValue(e,t){let s=e.length>0&&e[0]instanceof r?r.readFromBuilders(e,t):r.readFromDataTrees(e,t);return s===null||s.length===0?null:s.length===1?s[0]:s}static readFromBuilders(e,t){let n=e.find(s=>s.getParamName()===t);return n?n.flatten():null}static readFromDataTrees(e,t){let n=e.find(a=>a.ParamName===t);if(!n?.InnerTree)return null;let s=Object.keys(n.InnerTree)[0];if(!s)return null;let o=n.InnerTree[s];return Array.isArray(o)?o.map(a=>a?.data!==void 0?r.deserializeValue(a.data):null).filter(a=>a!==null):o?.data!==void 0?[r.deserializeValue(o.data)]:o!==void 0?[o]:null}static parsePathString(e){let t=e.match(/^\{([\d;]*)\}$/);return t?t[1]===""?[]:t[1].split(";").map(Number):(m().warn(`Invalid TreeBuilder path format: ${e}, using [0]`),[0])}static formatPathString(e){return`{${e.join(";")}}`}applyNumericConstraints(e,t,n){for(let s of Object.values(this.innerTree))if(Array.isArray(s))for(let o of s){let a=r.deserializeValue(o.data);if(typeof a=="number"){let u=r.clampValue(a,e,t,n);o.data=r.serializeValue(u)}}}static serializeValue(e){return typeof e=="boolean"||typeof e=="number"||typeof e=="string"?e:typeof e=="object"&&e!==null?JSON.stringify(e):String(e)}static deserializeValue(e){if(typeof e=="boolean"||typeof e=="number"||typeof e!="string")return e;if(e.startsWith("{")||e.startsWith("["))try{return JSON.parse(e)}catch{return e}return isNaN(Number(e))?e==="true"?!0:e==="false"?!1:e:Number(e)}static hasValidValue(e){return e==null?!1:typeof e=="string"?!0:!(Array.isArray(e)&&e.length===0||typeof e=="object"&&!Array.isArray(e)&&Object.keys(e).length===0)}static isDataTreeStructure(e){return typeof e!="object"||e===null||Array.isArray(e)?!1:Object.entries(e).every(([t,n])=>typeof t=="string"&&/^\{[\d;]+\}$/.test(t)&&Array.isArray(n))}static isNumericInput(e){return e.paramType==="Number"||e.paramType==="Integer"}static processValues(e,t){return e.map(n=>r.isNumericInput(t)&&typeof n=="number"?r.clampValue(n,t.minimum,t.maximum,t.nickname||"unnamed"):n).filter(n=>n!=null)}static clampValue(e,t,n,s){let o=e;return t!=null&&o<t&&(m().warn(`${s}: ${e} below min ${t}, clamping`),o=t),n!=null&&o>n&&(m().warn(`${s}: ${e} above max ${n}, clamping`),o=n),o}};export{R as a,b,x as c,J as d,w as e,D as f,C as g,ie as h,ue as i,I as j,E as k,G as l};
|
|
2
|
+
//# sourceMappingURL=chunk-RILJ3IA7.js.map
|
package/dist/grasshopper.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunk6VF4VVLWcjs = require('./chunk-6VF4VVLW.cjs');require('./chunk-MZGKJZVP.cjs');var _chunkVWOEUM7Ccjs = require('./chunk-VWOEUM7C.cjs');exports.GrasshopperClient = _chunk6VF4VVLWcjs.c; exports.GrasshopperResponseProcessor = _chunk6VF4VVLWcjs.f; exports.RhinoComputeError = _chunkVWOEUM7Ccjs.d; exports.SolveScheduler = _chunk6VF4VVLWcjs.b; exports.TreeBuilder = _chunk6VF4VVLWcjs.l; exports.downloadFileData = _chunk6VF4VVLWcjs.e; exports.extractFilesFromComputeResponse = _chunk6VF4VVLWcjs.d; exports.fetchDefinitionIO = _chunk6VF4VVLWcjs.j; exports.fetchParsedDefinitionIO = _chunk6VF4VVLWcjs.k; exports.hashSolveInput = _chunk6VF4VVLWcjs.a; exports.processInput = _chunk6VF4VVLWcjs.h; exports.processInputs = _chunk6VF4VVLWcjs.i; exports.solveGrasshopperDefinition = _chunk6VF4VVLWcjs.g;
|
|
2
2
|
//# sourceMappingURL=grasshopper.cjs.map
|
package/dist/grasshopper.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as b,b as c,c as d,d as e,e as f,f as g,g as h,h as i,i as j,j as k,k as l,l as m}from"./chunk-
|
|
1
|
+
import{a as b,b as c,c as d,d as e,e as f,f as g,g as h,h as i,i as j,j as k,k as l,l as m}from"./chunk-RILJ3IA7.js";import"./chunk-XLHA5YPH.js";import{d as a}from"./chunk-RHULSS7S.js";export{d as GrasshopperClient,g as GrasshopperResponseProcessor,a as RhinoComputeError,c as SolveScheduler,m as TreeBuilder,f as downloadFileData,e as extractFilesFromComputeResponse,k as fetchDefinitionIO,l as fetchParsedDefinitionIO,b as hashSolveInput,i as processInput,j as processInputs,h as solveGrasshopperDefinition};
|
|
2
2
|
//# sourceMappingURL=grasshopper.js.map
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunk6VF4VVLWcjs = require('./chunk-6VF4VVLW.cjs');require('./chunk-MZGKJZVP.cjs');var _chunkVWOEUM7Ccjs = require('./chunk-VWOEUM7C.cjs');exports.ComputeServerStats = _chunkVWOEUM7Ccjs.i; exports.ErrorCodes = _chunkVWOEUM7Ccjs.c; exports.GrasshopperClient = _chunk6VF4VVLWcjs.c; exports.GrasshopperResponseProcessor = _chunk6VF4VVLWcjs.f; exports.RhinoComputeError = _chunkVWOEUM7Ccjs.d; exports.SolveScheduler = _chunk6VF4VVLWcjs.b; exports.TreeBuilder = _chunk6VF4VVLWcjs.l; exports.camelcaseKeys = _chunkVWOEUM7Ccjs.k; exports.downloadFileData = _chunk6VF4VVLWcjs.e; exports.enableDebugLogging = _chunkVWOEUM7Ccjs.g; exports.extractFilesFromComputeResponse = _chunk6VF4VVLWcjs.d; exports.fetchDefinitionIO = _chunk6VF4VVLWcjs.j; exports.fetchParsedDefinitionIO = _chunk6VF4VVLWcjs.k; exports.fetchRhinoCompute = _chunkVWOEUM7Ccjs.h; exports.getLogger = _chunkVWOEUM7Ccjs.e; exports.hashSolveInput = _chunk6VF4VVLWcjs.a; exports.processInput = _chunk6VF4VVLWcjs.h; exports.processInputs = _chunk6VF4VVLWcjs.i; exports.setLogger = _chunkVWOEUM7Ccjs.f; exports.solveGrasshopperDefinition = _chunk6VF4VVLWcjs.g; exports.toCamelCase = _chunkVWOEUM7Ccjs.j;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as b,b as c,c as d,d as g,e as h,f as i,g as j,h as k,i as l,j as n,k as q,l as s}from"./chunk-
|
|
1
|
+
import{a as b,b as c,c as d,d as g,e as h,f as i,g as j,h as k,i as l,j as n,k as q,l as s}from"./chunk-RILJ3IA7.js";import"./chunk-XLHA5YPH.js";import{c as o,d as r,e,f,g as m,h as p,i as t,j as x,k as a}from"./chunk-RHULSS7S.js";export{t as ComputeServerStats,o as ErrorCodes,d as GrasshopperClient,i as GrasshopperResponseProcessor,r as RhinoComputeError,c as SolveScheduler,s as TreeBuilder,a as camelcaseKeys,h as downloadFileData,m as enableDebugLogging,g as extractFilesFromComputeResponse,n as fetchDefinitionIO,q as fetchParsedDefinitionIO,p as fetchRhinoCompute,e as getLogger,b as hashSolveInput,k as processInput,l as processInputs,f as setLogger,j as solveGrasshopperDefinition,x as toCamelCase};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import{c as V}from"./chunk-XLHA5YPH.js";import{a as k,b as J,c as v,d as L,e as U,f as j}from"./chunk-DADFXYBV.js";import{c as R,d as F,e as u}from"./chunk-RHULSS7S.js";import*as y from"three";var H=1096174675,O=1,T=1,G=12,N=56;function P(e){let r=W(e),n=new DataView(r.buffer,r.byteOffset,r.byteLength);if(r.byteLength<G)throw x("Blob too small to contain SLVA header.",{expectedBytes:G,availableBytes:r.byteLength});let t=0,s=n.getUint32(t,!0);if(t+=4,s!==H)throw x(`Invalid SLVA magic: 0x${s.toString(16)}`,{expectedMagic:`0x${H.toString(16)}`,actualMagic:`0x${s.toString(16)}`});let a=n.getUint32(t,!0);if(t+=4,a!==O)throw x(`Unsupported SLVA version: ${a}`,{expectedVersion:O,actualVersion:a});let c=n.getUint32(t,!0);if(t+=4,t+c>r.byteLength)throw x("Insufficient data to read metadata JSON.",{expectedBytes:c,availableBytes:r.byteLength-t,offset:t});let l=r.subarray(t,t+c);t+=c;let m;try{m=JSON.parse(X(l))}catch(C){throw x(`Failed to parse metadata JSON: ${C instanceof Error?C.message:String(C)}`,{metadataLen:c})}if(t+N>r.byteLength)throw x("Insufficient data to read geometry header.",{expectedBytes:N,availableBytes:r.byteLength-t,offset:t});let f=n.getUint32(t,!0);t+=4;let p=n.getFloat64(t,!0);t+=8;let o=n.getFloat64(t,!0);t+=8;let d=n.getFloat64(t,!0);t+=8;let h=n.getFloat64(t,!0);t+=8;let i=n.getFloat64(t,!0);t+=8;let A=n.getFloat64(t,!0);t+=8;let w=n.getUint32(t,!0);t+=4;let B=(f&T)!==0,E=w*3,g=E*(B?4:2);if(t+g>r.byteLength)throw x("Insufficient data to read vertices.",{expectedBytes:g,availableBytes:r.byteLength-t,offset:t,useFloat32:B,vertexCount:w});let M=r.byteOffset+t,S=B?K(r.buffer,M,E):Z(r.buffer,M,E);if(t+=g,t+4>r.byteLength)throw x("Insufficient data to read index count.",{expectedBytes:4,availableBytes:r.byteLength-t,offset:t});let I=n.getUint32(t,!0);t+=4;let D=I*4;if(t+D>r.byteLength)throw x("Insufficient data to read indices.",{expectedBytes:D,availableBytes:r.byteLength-t,offset:t,indexCount:I});let Y=Q(r.buffer,r.byteOffset+t,I);return{metadata:m,flags:f,vertices:S,indices:Y,origin:[p,o,d],scale:[h,i,A]}}function W(e){return typeof e=="string"?V(e):e instanceof Uint8Array?e:new Uint8Array(e)}function X(e){if(typeof TextDecoder<"u")return new TextDecoder("utf-8").decode(e);if(typeof globalThis.Buffer<"u")return globalThis.Buffer.from(e).toString("utf-8");throw new F("No UTF-8 decoder available in this environment.",R.INVALID_STATE)}function Z(e,r,n){if(n===0)return new Int16Array(0);if(r%2===0)return new Int16Array(e,r,n);let t=new Uint8Array(n*2);return t.set(new Uint8Array(e,r,n*2)),new Int16Array(t.buffer)}function K(e,r,n){if(n===0)return new Float32Array(0);if(r%4===0)return new Float32Array(e,r,n);let t=new Uint8Array(n*4);return t.set(new Uint8Array(e,r,n*4)),new Float32Array(t.buffer)}function Q(e,r,n){if(n===0)return new Uint32Array(0);if(r%4===0)return new Uint32Array(e,r,n);let t=new Uint8Array(n*4);return t.set(new Uint8Array(e,r,n*4)),new Uint32Array(t.buffer)}function x(e,r){return new F(e,R.VALIDATION_ERROR,{context:r})}async function _(e,r){let{mergeByMaterial:n=!0,applyTransforms:t=!0,debug:s=!1}=r??{},a=s?performance.now():0,c=0;try{let l=performance.now(),m=JSON.parse(e);return c=performance.now()-l,await $(m,{mergeByMaterial:n,applyTransforms:t,debug:s,parseTime:c,perfStart:a})}catch(l){return u().error("Error parsing mesh batch:",l),[]}}async function $(e,r){let{mergeByMaterial:n=!0,applyTransforms:t=!0,scaleFactor:s=1,debug:a=!1,parseTime:c=0,perfStart:l=a?performance.now():0}=r??{},m=0,f=0;try{let p=performance.now(),o=P(e.compressedData);m=performance.now()-p;let d=o.metadata.materials??e.materials,h=o.metadata.groups??e.groups,i=o.metadata.sourceComponentId??e.sourceComponentId,A=(o.flags&T)!==0,w=A?ee(o.vertices,t):q(o.vertices,o.origin,o.scale,t);if(a){let g=oe(e.compressedData),M=o.vertices.byteLength+o.indices.byteLength;u().debug("Mesh Batch Stats:"),u().debug(` Materials: ${d.length} | Groups: ${h.length}`),u().debug(` Vertices: ${o.vertices.length/3} | Indices: ${o.indices.length}`),u().debug(` Format: ${A?"float32":"int16 quantized"}`),u().debug(` Blob: ${(g/1024/1024).toFixed(2)} MB | Geometry on wire: ${(M/1024/1024).toFixed(2)} MB`)}let B=performance.now(),E=d.map(te),b=[];for(let g of h)if(n&&g.meshes.length>1){let M=re(g,w,o.indices,E);M.userData.sourceComponentId=i??null,b.push(M)}else{let M=ne(g,w,o.indices,E);for(let S of M)S.userData.sourceComponentId=i??null;b.push(...M)}if(s!==1)for(let g of b)g.scale.set(s,s,s);if(f=performance.now()-B,a){let g=performance.now()-l;u().debug("Performance:"),c>0&&u().debug(` Parse JSON: ${c.toFixed(2)}ms`),u().debug(` Decode binary: ${m.toFixed(2)}ms`),u().debug(` Create Meshes: ${f.toFixed(2)}ms`),u().debug(` Total: ${g.toFixed(2)}ms`)}return b}catch(p){return u().error("Error parsing mesh batch object:",p),[]}}function q(e,r,n,t){let s=new Float32Array(e.length),a=r[0],c=r[1],l=r[2],m=n[0],f=n[1],p=n[2];if(t)for(let o=0;o<e.length;o+=3){let d=a+(e[o]+32767)*m,h=c+(e[o+1]+32767)*f,i=l+(e[o+2]+32767)*p;s[o]=d,s[o+1]=i,s[o+2]=-h}else for(let o=0;o<e.length;o+=3)s[o]=a+(e[o]+32767)*m,s[o+1]=c+(e[o+1]+32767)*f,s[o+2]=l+(e[o+2]+32767)*p;return s}function ee(e,r){if(!r)return e;let n=new Float32Array(e.length);for(let t=0;t<e.length;t+=3){let s=e[t],a=e[t+1],c=e[t+2];n[t]=s,n[t+1]=c,n[t+2]=-a}return n}function te(e){let r=v(e.color);return new y.MeshPhysicalMaterial({color:r,metalness:e.metalness,roughness:e.roughness,opacity:e.opacity,transparent:e.transparent,side:y.DoubleSide,polygonOffset:!0,polygonOffsetFactor:.5,polygonOffsetUnits:.5,depthWrite:!0,depthTest:!0})}function re(e,r,n,t){let s=0,a=0;for(let i of e.meshes)s+=i.vertexCount,a+=i.indexCount;let c=new Float32Array(s*3),l=new Uint32Array(a),m=0,f=0;for(let i of e.meshes){let A=i.vertexStart*3,w=i.vertexCount*3;c.set(r.subarray(A,A+w),m*3);let B=n.subarray(i.indexStart,i.indexStart+i.indexCount),E=m-i.vertexStart;if(E===0)l.set(B,f);else for(let b=0;b<B.length;b++)l[f+b]=B[b]+E;m+=i.vertexCount,f+=i.indexCount}let p=new y.BufferGeometry;p.setAttribute("position",new y.BufferAttribute(c,3)),p.setIndex(new y.BufferAttribute(l,1)),p.computeVertexNormals();let o=new y.Mesh(p,t[e.materialId]),d=e.meshes[0],h=e.meshes.map(i=>i.name).filter(i=>i&&i.length>0);return o.name=h.length>0?h[0]:`merged_material_${e.materialId}`,o.castShadow=!0,o.receiveShadow=!0,o.userData={name:o.name,layer:d?.layer??"",originalIndex:d?.originalIndex??0,metadata:d?.metadata??{},mergedFrom:e.meshes.slice(1).map(i=>({name:i.name,layer:i.layer,originalIndex:i.originalIndex}))},o}function ne(e,r,n,t){let s=[];for(let a of e.meshes){let c=a.vertexStart*3,l=a.vertexCount*3,m=r.slice(c,c+l),f=n.subarray(a.indexStart,a.indexStart+a.indexCount),p=new Uint32Array(f.length),o=a.vertexStart;for(let i=0;i<f.length;i++)p[i]=f[i]-o;let d=new y.BufferGeometry;d.setAttribute("position",new y.BufferAttribute(m,3)),d.setIndex(new y.BufferAttribute(p,1)),d.computeVertexNormals();let h=new y.Mesh(d,t[e.materialId]);h.name=a.name,h.userData={name:a.name,layer:a.layer??"",originalIndex:a.originalIndex,metadata:a.metadata??{}},h.castShadow=!0,h.receiveShadow=!0,s.push(h)}return s}function oe(e){return Math.floor(e.length*3/4)}var z={Millimeters:1/1e3,Centimeters:1/100,Meters:1,Inches:1/39.37,Feet:1/3.28084},ae="Display";async function se(e,r){let n=performance.now(),t=[],{allowScaling:s=!0,allowAutoPosition:a=!0,debug:c=!1,parsing:l={}}=r??{};try{let m=s?ie(e.modelunits):1;return await ce(e,t,m,l,c),a&&le(t),t}catch(m){throw ue(m,t),m}finally{c&&pe(n)}}function ie(e){return z[e]??1}async function ce(e,r,n,t,s){for(let a of e.values){let c=a.InnerTree;for(let l in c){let m=c[l];m&&await me(m,r,n,t,s)}}}async function me(e,r,n,t,s){for(let a of e)if(a.type.includes(ae)){let c={mergeByMaterial:!0,applyTransforms:!0,debug:!1,...t},l=await _(a.data,c);if(n!==1)for(let m of l)m.scale.set(n,n,n);r.push(...l),s&&u().debug(`Extracted ${l.length} meshes from batch`)}}function le(e){if(e.length===0)return;let n=U(e).min.y;L(e,n)}function ue(e,r){u().error("An unexpected error occurred:",e),fe(r)}function fe(e){for(let r of e)r.geometry&&r.geometry.dispose(),r.material&&(Array.isArray(r.material)?r.material.forEach(n=>n.dispose()):r.material.dispose())}function pe(e){let r=performance.now()-e;u().info("Time to process meshes:",`${r.toFixed(2)}ms`)}export{H as BINARY_MESH_MAGIC,O as BINARY_MESH_VERSION,T as FLAG_FLOAT32,j as Materials,z as SCALE_FACTORS,L as applyOffset,U as computeCombinedBoundingBox,se as getThreeMeshesFromComputeResponse,k as initThree,P as parseBinaryMeshBatch,v as parseColor,_ as parseMeshBatch,$ as parseMeshBatchObject,J as updateScene};
|
|
2
|
-
//# sourceMappingURL=visualization-
|
|
2
|
+
//# sourceMappingURL=visualization-7TK4UEZL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/features/visualization/webdisplay/batch-parser.ts","../src/features/visualization/webdisplay/binary-parser.ts","../src/features/visualization/webdisplay/webdisplay-parser.ts"],"sourcesContent":["import * as THREE from 'three';\n\nimport { parseColor } from '../threejs/three-helpers';\nimport { getLogger } from '@/core';\n\nimport { FLAG_FLOAT32, parseBinaryMeshBatch } from './binary-parser';\n\nimport type { MeshBatch, MaterialGroup, SerializableMaterial } from './types';\n\n/**\n * Parses a batched mesh JSON and creates Three.js meshes.\n *\n * The geometry payload is the binary \"SLVA\" blob produced by the C# `BinaryGeometryWriter`,\n * base64-encoded into the outer JSON envelope. We `JSON.parse` the small envelope, then hand the\n * blob to `parseBinaryMeshBatch` which decodes the geometry without ever turning it into a string.\n *\n * @param batchJson - JSON string containing the batched mesh data\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatch(\n\tbatchJson: string,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst { mergeByMaterial = true, applyTransforms = true, debug = false } = options ?? {};\n\n\tconst perfStart = debug ? performance.now() : 0;\n\tlet parseTime = 0;\n\n\ttry {\n\t\tconst parseStart = performance.now();\n\t\tconst batch: MeshBatch = JSON.parse(batchJson);\n\t\tparseTime = performance.now() - parseStart;\n\n\t\treturn await parseMeshBatchObject(batch, {\n\t\t\tmergeByMaterial,\n\t\t\tapplyTransforms,\n\t\t\tdebug,\n\t\t\tparseTime,\n\t\t\tperfStart\n\t\t});\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch:', error);\n\t\treturn [];\n\t}\n}\n\n/**\n * Parses a MeshBatch object and creates Three.js meshes.\n *\n * The path is synchronous internally — `parseBinaryMeshBatch` does no IO, just typed-array views\n * over the blob. The function stays `async` so callers don't have to change shape if we move\n * parsing into a worker later.\n *\n * @param batch - MeshBatch object\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatchObject(\n\tbatch: MeshBatch,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Scale factor to apply to meshes (e.g., for unit conversion) */\n\t\tscaleFactor?: number;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t\t/** Parse time (optional, for debugging) */\n\t\tparseTime?: number;\n\t\t/** Performance start time (optional, for debugging) */\n\t\tperfStart?: number;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst {\n\t\tmergeByMaterial = true,\n\t\tapplyTransforms = true,\n\t\tscaleFactor = 1,\n\t\tdebug = false,\n\t\tparseTime = 0,\n\t\tperfStart = debug ? performance.now() : 0\n\t} = options ?? {};\n\n\tlet decodeTime = 0;\n\tlet meshCreateTime = 0;\n\n\ttry {\n\t\tconst decodeStart = performance.now();\n\t\tconst parsed = parseBinaryMeshBatch(batch.compressedData);\n\t\tdecodeTime = performance.now() - decodeStart;\n\n\t\t// Prefer materials/groups from the blob's embedded metadata — that's the source of truth\n\t\t// the C# writer emits. Fall back to the outer envelope for resilience (e.g. if a future\n\t\t// transport drops them from the blob's metadata to save bytes).\n\t\tconst materialsSrc = parsed.metadata.materials ?? batch.materials;\n\t\tconst groups = parsed.metadata.groups ?? batch.groups;\n\t\tconst sourceComponentId = parsed.metadata.sourceComponentId ?? batch.sourceComponentId;\n\n\t\tconst isFloat32 = (parsed.flags & FLAG_FLOAT32) !== 0;\n\n\t\t// Dequantize once up-front into a single Float32Array. Downstream code (per-group merging,\n\t\t// computeVertexNormals, ground-offset, scaleFactor) all expect world-unit floats, and a\n\t\t// single linear pass over the int16 buffer is far cheaper than the legacy gunzip + base64\n\t\t// path. The Z-up -> Y-up rotation, when requested, is folded into the same pass.\n\t\tconst worldVertices = isFloat32\n\t\t\t? maybeRotateFloat32Vertices(parsed.vertices as Float32Array, applyTransforms)\n\t\t\t: dequantizeInt16(\n\t\t\t\t\tparsed.vertices as Int16Array,\n\t\t\t\t\tparsed.origin,\n\t\t\t\t\tparsed.scale,\n\t\t\t\t\tapplyTransforms\n\t\t\t\t);\n\n\t\tif (debug) {\n\t\t\tconst blobBytes = approximateBase64DecodedBytes(batch.compressedData);\n\t\t\tconst wireBytes = parsed.vertices.byteLength + parsed.indices.byteLength;\n\t\t\tgetLogger().debug('Mesh Batch Stats:');\n\t\t\tgetLogger().debug(` Materials: ${materialsSrc.length} | Groups: ${groups.length}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Vertices: ${parsed.vertices.length / 3} | Indices: ${parsed.indices.length}`\n\t\t\t);\n\t\t\tgetLogger().debug(` Format: ${isFloat32 ? 'float32' : 'int16 quantized'}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Blob: ${(blobBytes / 1024 / 1024).toFixed(2)} MB | Geometry on wire: ${(wireBytes / 1024 / 1024).toFixed(2)} MB`\n\t\t\t);\n\t\t}\n\n\t\tconst meshCreateStart = performance.now();\n\t\tconst materials = materialsSrc.map(createMaterial);\n\n\t\tconst meshes: THREE.Mesh[] = [];\n\n\t\tfor (const group of groups) {\n\t\t\tif (mergeByMaterial && group.meshes.length > 1) {\n\t\t\t\tconst mergedMesh = createMergedMesh(group, worldVertices, parsed.indices, materials);\n\t\t\t\tmergedMesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\tmeshes.push(mergedMesh);\n\t\t\t} else {\n\t\t\t\tconst individualMeshes = createIndividualMeshes(\n\t\t\t\t\tgroup,\n\t\t\t\t\tworldVertices,\n\t\t\t\t\tparsed.indices,\n\t\t\t\t\tmaterials\n\t\t\t\t);\n\t\t\t\tfor (const mesh of individualMeshes) {\n\t\t\t\t\tmesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\t}\n\t\t\t\tmeshes.push(...individualMeshes);\n\t\t\t}\n\t\t}\n\n\t\tif (scaleFactor !== 1) {\n\t\t\tfor (const mesh of meshes) {\n\t\t\t\tmesh.scale.set(scaleFactor, scaleFactor, scaleFactor);\n\t\t\t}\n\t\t}\n\n\t\tmeshCreateTime = performance.now() - meshCreateStart;\n\n\t\tif (debug) {\n\t\t\tconst totalTime = performance.now() - perfStart;\n\t\t\tgetLogger().debug('Performance:');\n\t\t\tif (parseTime > 0) getLogger().debug(` Parse JSON: ${parseTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Decode binary: ${decodeTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Create Meshes: ${meshCreateTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Total: ${totalTime.toFixed(2)}ms`);\n\t\t}\n\n\t\treturn meshes;\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch object:', error);\n\t\treturn [];\n\t}\n}\n\n// ============================================================================\n// DEQUANTIZATION\n// ============================================================================\n\n/**\n * Reconstructs world-unit float32 positions from int16 quantized values.\n *\n * Mirrors the encoder formula: `world = origin + (q + 32767) * scale`. When\n * `applyCoordinateTransform=true` we fold the Rhino Z-up -> Three Y-up shuffle into the same pass\n * (`(x, y, z) -> (x, z, -y)`), saving a second walk over the buffer.\n */\nfunction dequantizeInt16(\n\tq: Int16Array,\n\torigin: [number, number, number],\n\tscale: [number, number, number],\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tconst out = new Float32Array(q.length);\n\tconst ox = origin[0];\n\tconst oy = origin[1];\n\tconst oz = origin[2];\n\tconst sx = scale[0];\n\tconst sy = scale[1];\n\tconst sz = scale[2];\n\n\tif (applyCoordinateTransform) {\n\t\t// Rotate -90 deg around X: (x, y, z) -> (x, z, -y)\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tconst wx = ox + (q[i]! + 32767) * sx;\n\t\t\tconst wy = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tconst wz = oz + (q[i + 2]! + 32767) * sz;\n\t\t\tout[i] = wx;\n\t\t\tout[i + 1] = wz;\n\t\t\tout[i + 2] = -wy;\n\t\t}\n\t} else {\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tout[i] = ox + (q[i]! + 32767) * sx;\n\t\t\tout[i + 1] = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tout[i + 2] = oz + (q[i + 2]! + 32767) * sz;\n\t\t}\n\t}\n\n\treturn out;\n}\n\n/**\n * For float32 batches: when no transform is needed we can pass through the parser's view; the\n * caller doesn't mutate it. When the rotation is needed we have to allocate.\n */\nfunction maybeRotateFloat32Vertices(\n\tvertices: Float32Array,\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tif (!applyCoordinateTransform) return vertices;\n\n\tconst out = new Float32Array(vertices.length);\n\tfor (let i = 0; i < vertices.length; i += 3) {\n\t\tconst x = vertices[i]!;\n\t\tconst y = vertices[i + 1]!;\n\t\tconst z = vertices[i + 2]!;\n\t\tout[i] = x;\n\t\tout[i + 1] = z;\n\t\tout[i + 2] = -y;\n\t}\n\treturn out;\n}\n\n// ============================================================================\n// MATERIAL CONSTRUCTION\n// ============================================================================\n\nfunction createMaterial(matData: SerializableMaterial): THREE.MeshPhysicalMaterial {\n\tconst color = parseColor(matData.color);\n\n\treturn new THREE.MeshPhysicalMaterial({\n\t\tcolor,\n\t\tmetalness: matData.metalness,\n\t\troughness: matData.roughness,\n\t\topacity: matData.opacity,\n\t\ttransparent: matData.transparent,\n\t\tside: THREE.DoubleSide,\n\t\t// Reduced polygon offset to minimize artifacts\n\t\t// Only use minimal offset to prevent z-fighting on coplanar faces\n\t\tpolygonOffset: true,\n\t\tpolygonOffsetFactor: 0.5,\n\t\tpolygonOffsetUnits: 0.5,\n\t\t// Improve depth rendering\n\t\tdepthWrite: true,\n\t\tdepthTest: true\n\t});\n}\n\n// ============================================================================\n// MESH CONSTRUCTION\n// ============================================================================\n\n/**\n * Creates a merged mesh from multiple meshes sharing the same material.\n *\n * Indices in the parser output already reference offsets into the combined vertex array (the C#\n * pipeline rebases per-mesh local indices into combined-array indices when assembling the batch).\n * For merged meshes we copy the relevant slices into a fresh contiguous buffer and shift indices\n * to match the new layout.\n */\nfunction createMergedMesh(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh {\n\tlet totalVertexCount = 0;\n\tlet totalIndexCount = 0;\n\tfor (const meshMeta of group.meshes) {\n\t\ttotalVertexCount += meshMeta.vertexCount;\n\t\ttotalIndexCount += meshMeta.indexCount;\n\t}\n\n\tconst mergedVertices = new Float32Array(totalVertexCount * 3);\n\tconst mergedIndices = new Uint32Array(totalIndexCount);\n\n\tlet vertexWriteCursor = 0;\n\tlet indexWriteCursor = 0;\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\t\tmergedVertices.set(\n\t\t\tallVertices.subarray(componentStart, componentStart + componentLen),\n\t\t\tvertexWriteCursor * 3\n\t\t);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst indexShift = vertexWriteCursor - meshMeta.vertexStart;\n\t\tif (indexShift === 0) {\n\t\t\tmergedIndices.set(indicesSlice, indexWriteCursor);\n\t\t} else {\n\t\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\t\tmergedIndices[indexWriteCursor + i] = indicesSlice[i]! + indexShift;\n\t\t\t}\n\t\t}\n\n\t\tvertexWriteCursor += meshMeta.vertexCount;\n\t\tindexWriteCursor += meshMeta.indexCount;\n\t}\n\n\tconst geometry = new THREE.BufferGeometry();\n\tgeometry.setAttribute('position', new THREE.BufferAttribute(mergedVertices, 3));\n\tgeometry.setIndex(new THREE.BufferAttribute(mergedIndices, 1));\n\tgeometry.computeVertexNormals();\n\n\tconst threeMesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\tconst firstMesh = group.meshes[0];\n\tconst meshNames = group.meshes.map((m) => m.name).filter((name) => name && name.length > 0);\n\tthreeMesh.name = meshNames.length > 0 ? meshNames[0]! : `merged_material_${group.materialId}`;\n\tthreeMesh.castShadow = true;\n\tthreeMesh.receiveShadow = true;\n\n\tthreeMesh.userData = {\n\t\tname: threeMesh.name,\n\t\tlayer: firstMesh?.layer ?? '',\n\t\toriginalIndex: firstMesh?.originalIndex ?? 0,\n\t\tmetadata: firstMesh?.metadata ?? {},\n\t\tmergedFrom: group.meshes.slice(1).map((m) => ({\n\t\t\tname: m.name,\n\t\t\tlayer: m.layer,\n\t\t\toriginalIndex: m.originalIndex\n\t\t}))\n\t};\n\n\treturn threeMesh;\n}\n\n/**\n * Creates individual meshes from a material group. Each mesh's indices are rebased so they\n * address its own local vertex slice starting from 0.\n */\nfunction createIndividualMeshes(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh[] {\n\tconst meshes: THREE.Mesh[] = [];\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\n\t\t// `subarray` returns a view; copy via `slice` so the BufferAttribute owns its memory and\n\t\t// downstream code (dispose/reuse) can't surprise us by sharing the parser's buffer.\n\t\tconst vertices = allVertices.slice(componentStart, componentStart + componentLen);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst rebasedIndices = new Uint32Array(indicesSlice.length);\n\t\tconst baseIndex = meshMeta.vertexStart;\n\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\trebasedIndices[i] = indicesSlice[i]! - baseIndex;\n\t\t}\n\n\t\tconst geometry = new THREE.BufferGeometry();\n\t\tgeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));\n\t\tgeometry.setIndex(new THREE.BufferAttribute(rebasedIndices, 1));\n\t\tgeometry.computeVertexNormals();\n\n\t\tconst mesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\t\tmesh.name = meshMeta.name;\n\t\tmesh.userData = {\n\t\t\tname: meshMeta.name,\n\t\t\tlayer: meshMeta.layer ?? '',\n\t\t\toriginalIndex: meshMeta.originalIndex,\n\t\t\tmetadata: meshMeta.metadata ?? {}\n\t\t};\n\t\tmesh.castShadow = true;\n\t\tmesh.receiveShadow = true;\n\n\t\tmeshes.push(mesh);\n\t}\n\n\treturn meshes;\n}\n\n// ============================================================================\n// DEBUG HELPERS\n// ============================================================================\n\nfunction approximateBase64DecodedBytes(base64: string): number {\n\treturn Math.floor((base64.length * 3) / 4);\n}\n","import { decodeBase64ToBinary } from '@/core/utils/encoding';\nimport { RhinoComputeError, ErrorCodes } from '@/core/errors';\n\nimport type { MaterialGroup, SerializableMaterial } from './types';\n\n// ============================================================================\n// WIRE FORMAT CONSTANTS\n// ============================================================================\n\n/** \"SLVA\" little-endian. */\nexport const BINARY_MESH_MAGIC = 0x41564c53;\n/** Bumped on any wire-layout change. */\nexport const BINARY_MESH_VERSION = 1;\n/** Bit 0 of the geometry flags word: 0 = int16 quantized, 1 = float32 raw. */\nexport const FLAG_FLOAT32 = 0x1;\n\nconst HEADER_PREAMBLE_BYTES = 4 /* magic */ + 4 /* version */ + 4; /* metadataLen */\nconst GEOMETRY_HEADER_BYTES =\n\t4 /* flags */ + 24 /* origin (3 x f64) */ + 24 /* scale (3 x f64) */ + 4; /* vertexCount */\n\n// ============================================================================\n// PARSED TYPES\n// ============================================================================\n\n/**\n * Metadata JSON embedded inside the binary blob.\n *\n * This is the same shape as a `MeshBatch` minus the `compressedData` field (the blob is opaque to\n * its own metadata header). Kept separate from the public `MeshBatch` type because the blob's\n * metadata never carries `compressedData` itself — it would be circular.\n */\nexport interface BinaryMeshMetadata {\n\tmaterials: SerializableMaterial[];\n\tgroups: MaterialGroup[];\n\tsourceComponentId?: string;\n}\n\n/**\n * Result of parsing a binary mesh blob.\n *\n * `vertices` and `indices` are typed-array views over the original `ArrayBuffer` — zero copies.\n * The consumer is responsible for not mutating the underlying buffer if it cares about safety,\n * or for calling `.slice()` to detach.\n */\nexport interface ParsedBinaryMeshBatch {\n\tmetadata: BinaryMeshMetadata;\n\tflags: number;\n\tvertices: Int16Array | Float32Array;\n\tindices: Uint32Array;\n\torigin: [number, number, number];\n\tscale: [number, number, number];\n}\n\n// ============================================================================\n// PARSER\n// ============================================================================\n\n/**\n * Parses a binary mesh batch blob in the SLVA wire format.\n *\n * The blob layout is:\n * ```\n * [4] magic = \"SLVA\" (0x53 0x4C 0x56 0x41)\n * [4] version = uint32 (currently 1)\n * [4] metadataLen = uint32 byte length of UTF-8 metadata JSON\n * [N] metadata = UTF-8 JSON (materials, groups, sourceComponentId, ...)\n * [4] flags = uint32 (bit 0: 0 = int16 quantized, 1 = float32 raw)\n * [24] origin = 3 x float64\n * [24] scale = 3 x float64 (step per int16 unit; identity for float32)\n * [4] vertexCount = uint32 number of vertices (positions = vertexCount * 3 components)\n * [V] vertices = int16[vertexCount*3] OR float32[vertexCount*3]\n * [4] indexCount = uint32 number of indices\n * [I] indices = uint32[indexCount]\n * ```\n *\n * For int16 vertices: world position = `origin + (q + 32767) * scale`. This matches Three.js\n * `BufferAttribute(arr, 3, true)` (`normalized: true`) semantics when the per-mesh transform\n * encodes `origin + scale`.\n *\n * For float32: `origin = (0, 0, 0)`, `scale = (1, 1, 1)`, vertices are raw world positions.\n *\n * @param input - The blob, as either an `ArrayBuffer`/`Uint8Array` (binary transport) or a\n * base64-encoded string (today's JSON-envelope transport).\n * @returns Decoded metadata plus typed-array views into the geometry payload.\n * @throws {RhinoComputeError} On invalid magic, unknown version, or truncated input.\n */\nexport function parseBinaryMeshBatch(\n\tinput: ArrayBuffer | Uint8Array | string\n): ParsedBinaryMeshBatch {\n\tconst bytes = toUint8Array(input);\n\tconst view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n\n\tif (bytes.byteLength < HEADER_PREAMBLE_BYTES) {\n\t\tthrow fail('Blob too small to contain SLVA header.', {\n\t\t\texpectedBytes: HEADER_PREAMBLE_BYTES,\n\t\t\tavailableBytes: bytes.byteLength\n\t\t});\n\t}\n\n\tlet offset = 0;\n\n\tconst magic = view.getUint32(offset, true);\n\toffset += 4;\n\tif (magic !== BINARY_MESH_MAGIC) {\n\t\tthrow fail(`Invalid SLVA magic: 0x${magic.toString(16)}`, {\n\t\t\texpectedMagic: `0x${BINARY_MESH_MAGIC.toString(16)}`,\n\t\t\tactualMagic: `0x${magic.toString(16)}`\n\t\t});\n\t}\n\n\tconst version = view.getUint32(offset, true);\n\toffset += 4;\n\tif (version !== BINARY_MESH_VERSION) {\n\t\tthrow fail(`Unsupported SLVA version: ${version}`, {\n\t\t\texpectedVersion: BINARY_MESH_VERSION,\n\t\t\tactualVersion: version\n\t\t});\n\t}\n\n\tconst metadataLen = view.getUint32(offset, true);\n\toffset += 4;\n\tif (offset + metadataLen > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read metadata JSON.', {\n\t\t\texpectedBytes: metadataLen,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst metadataBytes = bytes.subarray(offset, offset + metadataLen);\n\toffset += metadataLen;\n\n\tlet metadata: BinaryMeshMetadata;\n\ttry {\n\t\tmetadata = JSON.parse(decodeUtf8(metadataBytes)) as BinaryMeshMetadata;\n\t} catch (error) {\n\t\tthrow fail(\n\t\t\t`Failed to parse metadata JSON: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t{ metadataLen }\n\t\t);\n\t}\n\n\tif (offset + GEOMETRY_HEADER_BYTES > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read geometry header.', {\n\t\t\texpectedBytes: GEOMETRY_HEADER_BYTES,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst flags = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst originX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst scaleX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst vertexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst useFloat32 = (flags & FLAG_FLOAT32) !== 0;\n\tconst componentCount = vertexCount * 3;\n\tconst bytesPerComponent = useFloat32 ? 4 : 2;\n\tconst verticesByteLength = componentCount * bytesPerComponent;\n\n\tif (offset + verticesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read vertices.', {\n\t\t\texpectedBytes: verticesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tuseFloat32,\n\t\t\tvertexCount\n\t\t});\n\t}\n\n\t// Typed-array views require alignment to the element size. The header lays out the geometry\n\t// block such that the vertex byte offset is always 4-aligned (preamble 12 + metadataLen + 4 +\n\t// 48 + 4). float32 needs 4-byte alignment (satisfied), int16 needs 2-byte alignment\n\t// (satisfied). We can take a zero-copy view as long as `bytes.byteOffset + offset` agrees with\n\t// that alignment in the underlying buffer — a wrapper Uint8Array could violate it. Fall back\n\t// to a fresh copy if so.\n\tconst absoluteOffset = bytes.byteOffset + offset;\n\tconst verticesView = useFloat32\n\t\t? readFloat32Vertices(bytes.buffer, absoluteOffset, componentCount)\n\t\t: readInt16Vertices(bytes.buffer, absoluteOffset, componentCount);\n\toffset += verticesByteLength;\n\n\tif (offset + 4 > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read index count.', {\n\t\t\texpectedBytes: 4,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\tconst indexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst indicesByteLength = indexCount * 4;\n\tif (offset + indicesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read indices.', {\n\t\t\texpectedBytes: indicesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tindexCount\n\t\t});\n\t}\n\n\tconst indicesView = readUint32Indices(bytes.buffer, bytes.byteOffset + offset, indexCount);\n\n\treturn {\n\t\tmetadata,\n\t\tflags,\n\t\tvertices: verticesView,\n\t\tindices: indicesView,\n\t\torigin: [originX, originY, originZ],\n\t\tscale: [scaleX, scaleY, scaleZ]\n\t};\n}\n\n// ============================================================================\n// HELPERS\n// ============================================================================\n\nfunction toUint8Array(input: ArrayBuffer | Uint8Array | string): Uint8Array {\n\tif (typeof input === 'string') {\n\t\treturn decodeBase64ToBinary(input);\n\t}\n\tif (input instanceof Uint8Array) {\n\t\treturn input;\n\t}\n\treturn new Uint8Array(input);\n}\n\nfunction decodeUtf8(bytes: Uint8Array): string {\n\tif (typeof TextDecoder !== 'undefined') {\n\t\treturn new TextDecoder('utf-8').decode(bytes);\n\t}\n\t// Node fallback (Buffer is utf-8 by default).\n\tif (\n\t\ttypeof (globalThis as { Buffer?: { from(b: Uint8Array): { toString(enc: string): string } } })\n\t\t\t.Buffer !== 'undefined'\n\t) {\n\t\treturn (\n\t\t\tglobalThis as { Buffer: { from(b: Uint8Array): { toString(enc: string): string } } }\n\t\t).Buffer.from(bytes).toString('utf-8');\n\t}\n\tthrow new RhinoComputeError(\n\t\t'No UTF-8 decoder available in this environment.',\n\t\tErrorCodes.INVALID_STATE\n\t);\n}\n\nfunction readInt16Vertices(buffer: ArrayBufferLike, byteOffset: number, count: number): Int16Array {\n\tif (count === 0) return new Int16Array(0);\n\tif (byteOffset % 2 === 0) {\n\t\treturn new Int16Array(buffer, byteOffset, count);\n\t}\n\t// Misaligned (rare — would require a wrapper Uint8Array with odd byteOffset).\n\tconst copy = new Uint8Array(count * 2);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 2));\n\treturn new Int16Array(copy.buffer);\n}\n\nfunction readFloat32Vertices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Float32Array {\n\tif (count === 0) return new Float32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Float32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Float32Array(copy.buffer);\n}\n\nfunction readUint32Indices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Uint32Array {\n\tif (count === 0) return new Uint32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Uint32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Uint32Array(copy.buffer);\n}\n\nfunction fail(message: string, context: Record<string, unknown>): RhinoComputeError {\n\treturn new RhinoComputeError(message, ErrorCodes.VALIDATION_ERROR, { context });\n}\n","import * as THREE from 'three';\n\nimport { applyOffset, computeCombinedBoundingBox } from '../threejs/three-helpers.js';\nimport { getLogger } from '@/core';\n\nimport { parseMeshBatch } from './batch-parser';\n\nimport type { DataItem, GrasshopperComputeResponse } from '@/features/grasshopper/types';\nimport type { MeshExtractionOptions, MeshBatchParsingOptions } from './types';\n\n// Constants\nexport const SCALE_FACTORS: Record<string, number> = {\n\tMillimeters: 1 / 1000,\n\tCentimeters: 1 / 100,\n\tMeters: 1,\n\tInches: 1 / 39.37,\n\tFeet: 1 / 3.28084\n};\n\nconst DISPLAY_COMPONENT_TYPE = 'Display';\n\n/**\n * Extracts and processes display meshes from a ComputePointerResponse using the Grasshopper WebDisplay component.\n *\n * This is the primary entry point for extracting mesh geometry from Grasshopper compute responses.\n * It handles all aspects of mesh processing: decompression, coordinate transformation, scaling, and positioning.\n *\n * **Note:** Mesh decompression happens asynchronously in a Web Worker to prevent UI blocking.\n *\n * @param data - The ComputePointerResponse containing Grasshopper output trees.\n * @param options - Configuration for mesh extraction and parsing behavior. All options are optional with sensible defaults.\n * @returns Promise resolving to array of THREE.Mesh objects (may be empty).\n * @throws Rethrows unexpected errors after attempting to dispose any created meshes.\n *\n * @remarks\n * - Only works with the WebDisplay component of GHHeadless.\n * - Requires changes to Rhino.Compute (see https://github.com/TheVessen/compute.rhino3d).\n * - Provides a performant way to display mesh data in Three.js.\n * - Decompression is performed in a Web Worker for non-blocking UI updates.\n * - Supports mesh metadata (names, user data) if provided in the compute response.\n *\n * @internal Internal helper: high-level extraction remains public via visualization module, but this\n * function is considered internal implementation detail for mesh extraction.\n *\n * @example\n * ```ts\n * // Simple usage with defaults (all processing enabled)\n * const meshes = await getThreeMeshesFromComputeResponse(response);\n *\n * // With debugging enabled\n * const meshes = await getThreeMeshesFromComputeResponse(response, { debug: true });\n *\n * // With advanced options\n * const meshes = await getThreeMeshesFromComputeResponse(response, {\n * debug: true,\n * allowScaling: true,\n * allowAutoPosition: false,\n * parsing: {\n * mergeByMaterial: false,\n * applyTransforms: true,\n * debug: true,\n * },\n * });\n * ```\n */\nexport async function getThreeMeshesFromComputeResponse(\n\tdata: GrasshopperComputeResponse,\n\toptions?: MeshExtractionOptions\n): Promise<THREE.Mesh[]> {\n\tconst startTime = performance.now();\n\tconst meshes: THREE.Mesh[] = [];\n\n\tconst {\n\t\tallowScaling = true,\n\t\tallowAutoPosition = true,\n\t\tdebug = false,\n\t\tparsing: parsingOptions = {}\n\t} = options ?? {};\n\n\ttry {\n\t\tconst scaleFactor = allowScaling ? getScaleFactor(data.modelunits) : 1;\n\t\tawait extractMeshesFromData(data, meshes, scaleFactor, parsingOptions, debug);\n\n\t\tif (allowAutoPosition) {\n\t\t\tapplyGroundOffset(meshes);\n\t\t}\n\n\t\treturn meshes;\n\t} catch (error) {\n\t\thandleError(error, meshes);\n\t\tthrow error;\n\t} finally {\n\t\tif (debug) {\n\t\t\tlogProcessingTime(startTime);\n\t\t}\n\t}\n}\n\n/**\n * Gets the scale factor for the given unit type.\n */\nfunction getScaleFactor(modelUnits: string): number {\n\treturn SCALE_FACTORS[modelUnits] ?? 1;\n}\n\n/**\n * Extracts meshes from compute response data.\n */\nasync function extractMeshesFromData(\n\tdata: GrasshopperComputeResponse,\n\tmeshes: THREE.Mesh[],\n\tscaleFactor: number,\n\tparsingOptions: MeshBatchParsingOptions,\n\tdebug: boolean\n): Promise<void> {\n\tfor (const value of data.values) {\n\t\tconst innerTree = value.InnerTree as { [key: string]: DataItem[] };\n\n\t\tfor (const path in innerTree) {\n\t\t\tconst branch = innerTree[path];\n\t\t\tif (!branch) continue;\n\n\t\t\tawait processDataBranch(branch, meshes, scaleFactor, parsingOptions, debug);\n\t\t}\n\t}\n}\n\n/**\n * Processes a single data branch to extract MeshBatch display meshes.\n */\nasync function processDataBranch(\n\tbranch: DataItem[],\n\tmeshes: THREE.Mesh[],\n\tscaleFactor: number,\n\tparsingOptions: MeshBatchParsingOptions,\n\tdebug: boolean\n): Promise<void> {\n\tfor (const item of branch) {\n\t\tif (item.type.includes(DISPLAY_COMPONENT_TYPE)) {\n\t\t\tconst mergedParsingOptions = {\n\t\t\t\tmergeByMaterial: true,\n\t\t\t\tapplyTransforms: true,\n\t\t\t\tdebug: false,\n\t\t\t\t...parsingOptions\n\t\t\t};\n\n\t\t\tconst batchMeshes = await parseMeshBatch(item.data, mergedParsingOptions);\n\n\t\t\tif (scaleFactor !== 1) {\n\t\t\t\tfor (const mesh of batchMeshes) {\n\t\t\t\t\tmesh.scale.set(scaleFactor, scaleFactor, scaleFactor);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmeshes.push(...batchMeshes);\n\n\t\t\tif (debug) {\n\t\t\t\tgetLogger().debug(`Extracted ${batchMeshes.length} meshes from batch`);\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Applies vertical offset to position meshes on the Z=0 plane.\n */\nfunction applyGroundOffset(meshes: THREE.Mesh[]): void {\n\tif (meshes.length === 0) return;\n\n\tconst combinedBoundingBox = computeCombinedBoundingBox(meshes);\n\tconst offsetY = combinedBoundingBox.min.y;\n\tapplyOffset(meshes, offsetY);\n}\n\n/**\n * Handles errors by disposing created meshes and logging.\n */\nfunction handleError(error: unknown, meshes: THREE.Mesh[]): void {\n\tgetLogger().error('An unexpected error occurred:', error);\n\tdisposeMeshes(meshes);\n}\n\n/**\n * Disposes of all meshes and their associated resources.\n */\nfunction disposeMeshes(meshes: THREE.Mesh[]): void {\n\tfor (const mesh of meshes) {\n\t\tif (mesh.geometry) {\n\t\t\tmesh.geometry.dispose();\n\t\t}\n\n\t\tif (mesh.material) {\n\t\t\tif (Array.isArray(mesh.material)) {\n\t\t\t\tmesh.material.forEach((material) => material.dispose());\n\t\t\t} else {\n\t\t\t\tmesh.material.dispose();\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Logs the processing time for mesh extraction.\n */\nfunction logProcessingTime(startTime: number): void {\n\tconst elapsed = performance.now() - startTime;\n\tgetLogger().info('Time to process meshes:', `${elapsed.toFixed(2)}ms`);\n}\n"],"mappings":"yKAAA,UAAYA,MAAW,QCUhB,IAAMC,EAAoB,WAEpBC,EAAsB,EAEtBC,EAAe,EAEtBC,EAAwB,GACxBC,EACL,GAoEM,SAASC,EACfC,EACwB,CACxB,IAAMC,EAAQC,EAAaF,CAAK,EAC1BG,EAAO,IAAI,SAASF,EAAM,OAAQA,EAAM,WAAYA,EAAM,UAAU,EAE1E,GAAIA,EAAM,WAAaJ,EACtB,MAAMO,EAAK,yCAA0C,CACpD,cAAeP,EACf,eAAg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QtB,EACdsB,EAAK,UACRA,EAAK,SAAS,QAAQ,EAGnBA,EAAK,WACJ,MAAM,QAAQA,EAAK,QAAQ,EAC9BA,EAAK,SAAS,QAASM,GAAaA,EAAS,QAAQ,CAAC,EAEtDN,EAAK,SAAS,QAAQ,EAI1B,CAKA,SAASX,GAAkBZ,EAAyB,CACnD,IAAM8B,EAAU,YAAY,IAAI,EAAI9B,EACpCwB,EAAU,EAAE,KAAK,0BAA2B,GAAGM,EAAQ,QAAQ,CAAC,CAAC,IAAI,CACtE","names":["THREE","BINARY_MESH_MAGIC","BINARY_MESH_VERSION","FLAG_FLOAT32","HEADER_PREAMBLE_BYTES","GEOMETRY_HEADER_BYTES","parseBinaryMeshBatch","input","bytes","toUint8Array","view","fail","offset","magic","version","metadataLen","metadataBytes","metadata","decodeUtf8","error","flags","originX","originY","originZ","scaleX","scaleY","scaleZ","vertexCount","useFloat32","componentCount","verticesByteLength","absoluteOffset","verticesView","readFloat32Vertices","readInt16Vertices","indexCount","indicesByteLength","indicesView","readUint32Indices","decodeBase64ToBinary","RhinoComputeError","ErrorCodes","buffer","byteOffset","count","copy","message","context","parseMeshBatch","batchJson","options","mergeByMaterial","applyTransforms","debug","perfStart","parseTime","parseStart","batch","parseMeshBatchObject","error","getLogger","scaleFactor","decodeTime","meshCreateTime","decodeStart","parsed","parseBinaryMeshBatch","materialsSrc","groups","sourceComponentId","isFloat32","FLAG_FLOAT32","worldVertices","maybeRotateFloat32Vertices","dequantizeInt16","blobBytes","approximateBase64DecodedBytes","wireBytes","meshCreateStart","materials","createMaterial","meshes","group","mergedMesh","createMergedMesh","individualMeshes","createIndividualMeshes","mesh","totalTime","q","origin","scale","applyCoordinateTransform","out","ox","oy","oz","sx","sy","sz","i","wx","wy","wz","vertices","x","y","z","matData","color","parseColor","allVertices","allIndices","totalVertexCount","totalIndexCount","meshMeta","mergedVertices","mergedIndices","vertexWriteCursor","indexWriteCursor","componentStart","componentLen","indicesSlice","indexShift","geometry","threeMesh","firstMesh","meshNames","m","name","rebasedIndices","baseIndex","base64","SCALE_FACTORS","DISPLAY_COMPONENT_TYPE","getThreeMeshesFromComputeResponse","data","options","startTime","meshes","allowScaling","allowAutoPosition","debug","parsingOptions","scaleFactor","getScaleFactor","extractMeshesFromData","applyGroundOffset","error","handleError","logProcessingTime","modelUnits","value","innerTree","path","branch","processDataBranch","item","mergedParsingOptions","batchMeshes","parseMeshBatch","mesh","getLogger","offsetY","computeCombinedBoundingBox","applyOffset","disposeMeshes","material","elapsed"]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkMZGKJZVPcjs = require('./chunk-MZGKJZVP.cjs');var _chunk5465MDT4cjs = require('./chunk-5465MDT4.cjs');var _chunkVWOEUM7Ccjs = require('./chunk-VWOEUM7C.cjs');var _three = require('three'); var y = _interopRequireWildcard(_three);var H=1096174675,O= exports.BINARY_MESH_VERSION =1,T= exports.FLAG_FLOAT32 =1,G=12,N=56;function P(e){let r=W(e),n=new DataView(r.buffer,r.byteOffset,r.byteLength);if(r.byteLength<G)throw x("Blob too small to contain SLVA header.",{expectedBytes:G,availableBytes:r.byteLength});let t=0,s=n.getUint32(t,!0);if(t+=4,s!==H)throw x(`Invalid SLVA magic: 0x${s.toString(16)}`,{expectedMagic:`0x${H.toString(16)}`,actualMagic:`0x${s.toString(16)}`});let a=n.getUint32(t,!0);if(t+=4,a!==O)throw x(`Unsupported SLVA version: ${a}`,{expectedVersion:O,actualVersion:a});let c=n.getUint32(t,!0);if(t+=4,t+c>r.byteLength)throw x("Insufficient data to read metadata JSON.",{expectedBytes:c,availableBytes:r.byteLength-t,offset:t});let l=r.subarray(t,t+c);t+=c;let m;try{m=JSON.parse(X(l))}catch(C){throw x(`Failed to parse metadata JSON: ${C instanceof Error?C.message:String(C)}`,{metadataLen:c})}if(t+N>r.byteLength)throw x("Insufficient data to read geometry header.",{expectedBytes:N,availableBytes:r.byteLength-t,offset:t});let f=n.getUint32(t,!0);t+=4;let p=n.getFloat64(t,!0);t+=8;let o=n.getFloat64(t,!0);t+=8;let d=n.getFloat64(t,!0);t+=8;let h=n.getFloat64(t,!0);t+=8;let i=n.getFloat64(t,!0);t+=8;let A=n.getFloat64(t,!0);t+=8;let w=n.getUint32(t,!0);t+=4;let B=(f&T)!==0,E=w*3,g=E*(B?4:2);if(t+g>r.byteLength)throw x("Insufficient data to read vertices.",{expectedBytes:g,availableBytes:r.byteLength-t,offset:t,useFloat32:B,vertexCount:w});let M=r.byteOffset+t,S=B?K(r.buffer,M,E):Z(r.buffer,M,E);if(t+=g,t+4>r.byteLength)throw x("Insufficient data to read index count.",{expectedBytes:4,availableBytes:r.byteLength-t,offset:t});let I=n.getUint32(t,!0);t+=4;let D=I*4;if(t+D>r.byteLength)throw x("Insufficient data to read indices.",{expectedBytes:D,availableBytes:r.byteLength-t,offset:t,indexCount:I});let Y=Q(r.buffer,r.byteOffset+t,I);return{metadata:m,flags:f,vertices:S,indices:Y,origin:[p,o,d],scale:[h,i,A]}}function W(e){return typeof e=="string"?_chunkMZGKJZVPcjs.c.call(void 0, e):e instanceof Uint8Array?e:new Uint8Array(e)}function X(e){if(typeof TextDecoder<"u")return new TextDecoder("utf-8").decode(e);if(typeof globalThis.Buffer<"u")return globalThis.Buffer.from(e).toString("utf-8");throw new (0, _chunkVWOEUM7Ccjs.d)("No UTF-8 decoder available in this environment.",_chunkVWOEUM7Ccjs.c.INVALID_STATE)}function Z(e,r,n){if(n===0)return new Int16Array(0);if(r%2===0)return new Int16Array(e,r,n);let t=new Uint8Array(n*2);return t.set(new Uint8Array(e,r,n*2)),new Int16Array(t.buffer)}function K(e,r,n){if(n===0)return new Float32Array(0);if(r%4===0)return new Float32Array(e,r,n);let t=new Uint8Array(n*4);return t.set(new Uint8Array(e,r,n*4)),new Float32Array(t.buffer)}function Q(e,r,n){if(n===0)return new Uint32Array(0);if(r%4===0)return new Uint32Array(e,r,n);let t=new Uint8Array(n*4);return t.set(new Uint8Array(e,r,n*4)),new Uint32Array(t.buffer)}function x(e,r){return new (0, _chunkVWOEUM7Ccjs.d)(e,_chunkVWOEUM7Ccjs.c.VALIDATION_ERROR,{context:r})}async function _(e,r){let{mergeByMaterial:n=!0,applyTransforms:t=!0,debug:s=!1}=_nullishCoalesce(r, () => ({})),a=s?performance.now():0,c=0;try{let l=performance.now(),m=JSON.parse(e);return c=performance.now()-l,await $(m,{mergeByMaterial:n,applyTransforms:t,debug:s,parseTime:c,perfStart:a})}catch(l){return _chunkVWOEUM7Ccjs.e.call(void 0, ).error("Error parsing mesh batch:",l),[]}}async function $(e,r){let{mergeByMaterial:n=!0,applyTransforms:t=!0,scaleFactor:s=1,debug:a=!1,parseTime:c=0,perfStart:l=a?performance.now():0}=_nullishCoalesce(r, () => ({})),m=0,f=0;try{let p=performance.now(),o=P(e.compressedData);m=performance.now()-p;let d=_nullishCoalesce(o.metadata.materials, () => (e.materials)),h=_nullishCoalesce(o.metadata.groups, () => (e.groups)),i=_nullishCoalesce(o.metadata.sourceComponentId, () => (e.sourceComponentId)),A=(o.flags&T)!==0,w=A?ee(o.vertices,t):q(o.vertices,o.origin,o.scale,t);if(a){let g=oe(e.compressedData),M=o.vertices.byteLength+o.indices.byteLength;_chunkVWOEUM7Ccjs.e.call(void 0, ).debug("Mesh Batch Stats:"),_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Materials: ${d.length} | Groups: ${h.length}`),_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Vertices: ${o.vertices.length/3} | Indices: ${o.indices.length}`),_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Format: ${A?"float32":"int16 quantized"}`),_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Blob: ${(g/1024/1024).toFixed(2)} MB | Geometry on wire: ${(M/1024/1024).toFixed(2)} MB`)}let B=performance.now(),E=d.map(te),b=[];for(let g of h)if(n&&g.meshes.length>1){let M=re(g,w,o.indices,E);M.userData.sourceComponentId=_nullishCoalesce(i, () => (null)),b.push(M)}else{let M=ne(g,w,o.indices,E);for(let S of M)S.userData.sourceComponentId=_nullishCoalesce(i, () => (null));b.push(...M)}if(s!==1)for(let g of b)g.scale.set(s,s,s);if(f=performance.now()-B,a){let g=performance.now()-l;_chunkVWOEUM7Ccjs.e.call(void 0, ).debug("Performance:"),c>0&&_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Parse JSON: ${c.toFixed(2)}ms`),_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Decode binary: ${m.toFixed(2)}ms`),_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Create Meshes: ${f.toFixed(2)}ms`),_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(` Total: ${g.toFixed(2)}ms`)}return b}catch(p){return _chunkVWOEUM7Ccjs.e.call(void 0, ).error("Error parsing mesh batch object:",p),[]}}function q(e,r,n,t){let s=new Float32Array(e.length),a=r[0],c=r[1],l=r[2],m=n[0],f=n[1],p=n[2];if(t)for(let o=0;o<e.length;o+=3){let d=a+(e[o]+32767)*m,h=c+(e[o+1]+32767)*f,i=l+(e[o+2]+32767)*p;s[o]=d,s[o+1]=i,s[o+2]=-h}else for(let o=0;o<e.length;o+=3)s[o]=a+(e[o]+32767)*m,s[o+1]=c+(e[o+1]+32767)*f,s[o+2]=l+(e[o+2]+32767)*p;return s}function ee(e,r){if(!r)return e;let n=new Float32Array(e.length);for(let t=0;t<e.length;t+=3){let s=e[t],a=e[t+1],c=e[t+2];n[t]=s,n[t+1]=c,n[t+2]=-a}return n}function te(e){let r=_chunk5465MDT4cjs.c.call(void 0, e.color);return new y.MeshPhysicalMaterial({color:r,metalness:e.metalness,roughness:e.roughness,opacity:e.opacity,transparent:e.transparent,side:y.DoubleSide,polygonOffset:!0,polygonOffsetFactor:.5,polygonOffsetUnits:.5,depthWrite:!0,depthTest:!0})}function re(e,r,n,t){let s=0,a=0;for(let i of e.meshes)s+=i.vertexCount,a+=i.indexCount;let c=new Float32Array(s*3),l=new Uint32Array(a),m=0,f=0;for(let i of e.meshes){let A=i.vertexStart*3,w=i.vertexCount*3;c.set(r.subarray(A,A+w),m*3);let B=n.subarray(i.indexStart,i.indexStart+i.indexCount),E=m-i.vertexStart;if(E===0)l.set(B,f);else for(let b=0;b<B.length;b++)l[f+b]=B[b]+E;m+=i.vertexCount,f+=i.indexCount}let p=new y.BufferGeometry;p.setAttribute("position",new y.BufferAttribute(c,3)),p.setIndex(new y.BufferAttribute(l,1)),p.computeVertexNormals();let o=new y.Mesh(p,t[e.materialId]),d=e.meshes[0],h=e.meshes.map(i=>i.name).filter(i=>i&&i.length>0);return o.name=h.length>0?h[0]:`merged_material_${e.materialId}`,o.castShadow=!0,o.receiveShadow=!0,o.userData={name:o.name,layer:_nullishCoalesce(_optionalChain([d, 'optionalAccess', _2 => _2.layer]), () => ("")),originalIndex:_nullishCoalesce(_optionalChain([d, 'optionalAccess', _3 => _3.originalIndex]), () => (0)),metadata:_nullishCoalesce(_optionalChain([d, 'optionalAccess', _4 => _4.metadata]), () => ({})),mergedFrom:e.meshes.slice(1).map(i=>({name:i.name,layer:i.layer,originalIndex:i.originalIndex}))},o}function ne(e,r,n,t){let s=[];for(let a of e.meshes){let c=a.vertexStart*3,l=a.vertexCount*3,m=r.slice(c,c+l),f=n.subarray(a.indexStart,a.indexStart+a.indexCount),p=new Uint32Array(f.length),o=a.vertexStart;for(let i=0;i<f.length;i++)p[i]=f[i]-o;let d=new y.BufferGeometry;d.setAttribute("position",new y.BufferAttribute(m,3)),d.setIndex(new y.BufferAttribute(p,1)),d.computeVertexNormals();let h=new y.Mesh(d,t[e.materialId]);h.name=a.name,h.userData={name:a.name,layer:_nullishCoalesce(a.layer, () => ("")),originalIndex:a.originalIndex,metadata:_nullishCoalesce(a.metadata, () => ({}))},h.castShadow=!0,h.receiveShadow=!0,s.push(h)}return s}function oe(e){return Math.floor(e.length*3/4)}var z={Millimeters:1/1e3,Centimeters:1/100,Meters:1,Inches:1/39.37,Feet:1/3.28084},ae="Display";async function se(e,r){let n=performance.now(),t=[],{allowScaling:s=!0,allowAutoPosition:a=!0,debug:c=!1,parsing:l={}}=_nullishCoalesce(r, () => ({}));try{let m=s?ie(e.modelunits):1;return await ce(e,t,m,l,c),a&&le(t),t}catch(m){throw ue(m,t),m}finally{c&&pe(n)}}function ie(e){return _nullishCoalesce(z[e], () => (1))}async function ce(e,r,n,t,s){for(let a of e.values){let c=a.InnerTree;for(let l in c){let m=c[l];m&&await me(m,r,n,t,s)}}}async function me(e,r,n,t,s){for(let a of e)if(a.type.includes(ae)){let c={mergeByMaterial:!0,applyTransforms:!0,debug:!1,...t},l=await _(a.data,c);if(n!==1)for(let m of l)m.scale.set(n,n,n);r.push(...l),s&&_chunkVWOEUM7Ccjs.e.call(void 0, ).debug(`Extracted ${l.length} meshes from batch`)}}function le(e){if(e.length===0)return;let n=_chunk5465MDT4cjs.e.call(void 0, e).min.y;_chunk5465MDT4cjs.d.call(void 0, e,n)}function ue(e,r){_chunkVWOEUM7Ccjs.e.call(void 0, ).error("An unexpected error occurred:",e),fe(r)}function fe(e){for(let r of e)r.geometry&&r.geometry.dispose(),r.material&&(Array.isArray(r.material)?r.material.forEach(n=>n.dispose()):r.material.dispose())}function pe(e){let r=performance.now()-e;_chunkVWOEUM7Ccjs.e.call(void 0, ).info("Time to process meshes:",`${r.toFixed(2)}ms`)}exports.BINARY_MESH_MAGIC = H; exports.BINARY_MESH_VERSION = O; exports.FLAG_FLOAT32 = T; exports.Materials = _chunk5465MDT4cjs.f; exports.SCALE_FACTORS = z; exports.applyOffset = _chunk5465MDT4cjs.d; exports.computeCombinedBoundingBox = _chunk5465MDT4cjs.e; exports.getThreeMeshesFromComputeResponse = se; exports.initThree = _chunk5465MDT4cjs.a; exports.parseBinaryMeshBatch = P; exports.parseColor = _chunk5465MDT4cjs.c; exports.parseMeshBatch = _; exports.parseMeshBatchObject = $; exports.updateScene = _chunk5465MDT4cjs.b;
|
|
2
|
-
//# sourceMappingURL=visualization-
|
|
2
|
+
//# sourceMappingURL=visualization-JYNKROSH.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/selva-compute/selva-compute/dist/visualization-JYNKROSH.cjs","../src/features/visualization/webdisplay/batch-parser.ts","../src/features/visualization/webdisplay/binary-parser.ts"],"names":["BINARY_MESH_MAGIC","BINARY_MESH_VERSION","FLAG_FLOAT32","HEADER_PREAMBLE_BYTES","GEOMETRY_HEADER_BYTES","parseBinaryMeshBatch","input","bytes","toUint8Array","view","fail","offset","magic"],"mappings":"AAAA,2/BAAwC,wDAA4E,wDAAuD,uECApJ,ICUVA,CAAAA,CAAoB,UAAA,CAEpBC,CAAAA,+BAAsB,CAAA,CAEtBC,CAAAA,wBAAe,CAAA,CAEtBC,CAAAA,CAAwB,EAAA,CACxBC,CAAAA,CACL,EAAA,CAoEM,SAASC,CAAAA,CACfC,CAAAA,CACwB,CACxB,IAAMC,CAAAA,CAAQC,CAAAA,CAAaF,CAAK,CAAA,CAC1BG,CAAAA,CAAO,IAAI,QAAA,CAASF,CAAAA,CAAM,MAAA,CAAQA,CAAAA,CAAM,UAAA,CAAYA,CAAAA,CAAM,UAAU,CAAA,CAE1E,EAAA,CAAIA,CAAAA,CAAM,UAAA,CAAaJ,CAAAA,CACtB,MAAMO,CAAAA,CAAK,wCAAA,CAA0C,CACpD,aAAA,CAAeP,CAAAA,CACf,cAAA,CAAgBI,CAAAA,CAAM,UACvB,CAAC,CAAA,CAGF,IAAII,CAAAA,CAAS,CAAA,CAEPC,CAAAA,CAAQH,CAAAA,CAAK,SAAA,CAAUE,CAAAA,CAAQ,CAAA,CAAI,CAAA,CAEzC,EAAA,CADAA,CAAAA,EAAU,CAAA,CACNC,CAAAA,GAAUZ,CAAAA,CACb,MAAMU,CAAAA,CAAK,CAAA,sBAAA,EAAyBE,CAAAA,CAAM,QAAA,CAAS,EAAE,CAAC,CAAA,CAAA","file":"/home/runner/work/selva-compute/selva-compute/dist/visualization-JYNKROSH.cjs","sourcesContent":[null,"import * as THREE from 'three';\n\nimport { parseColor } from '../threejs/three-helpers';\nimport { getLogger } from '@/core';\n\nimport { FLAG_FLOAT32, parseBinaryMeshBatch } from './binary-parser';\n\nimport type { MeshBatch, MaterialGroup, SerializableMaterial } from './types';\n\n/**\n * Parses a batched mesh JSON and creates Three.js meshes.\n *\n * The geometry payload is the binary \"SLVA\" blob produced by the C# `BinaryGeometryWriter`,\n * base64-encoded into the outer JSON envelope. We `JSON.parse` the small envelope, then hand the\n * blob to `parseBinaryMeshBatch` which decodes the geometry without ever turning it into a string.\n *\n * @param batchJson - JSON string containing the batched mesh data\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatch(\n\tbatchJson: string,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst { mergeByMaterial = true, applyTransforms = true, debug = false } = options ?? {};\n\n\tconst perfStart = debug ? performance.now() : 0;\n\tlet parseTime = 0;\n\n\ttry {\n\t\tconst parseStart = performance.now();\n\t\tconst batch: MeshBatch = JSON.parse(batchJson);\n\t\tparseTime = performance.now() - parseStart;\n\n\t\treturn await parseMeshBatchObject(batch, {\n\t\t\tmergeByMaterial,\n\t\t\tapplyTransforms,\n\t\t\tdebug,\n\t\t\tparseTime,\n\t\t\tperfStart\n\t\t});\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch:', error);\n\t\treturn [];\n\t}\n}\n\n/**\n * Parses a MeshBatch object and creates Three.js meshes.\n *\n * The path is synchronous internally — `parseBinaryMeshBatch` does no IO, just typed-array views\n * over the blob. The function stays `async` so callers don't have to change shape if we move\n * parsing into a worker later.\n *\n * @param batch - MeshBatch object\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatchObject(\n\tbatch: MeshBatch,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Scale factor to apply to meshes (e.g., for unit conversion) */\n\t\tscaleFactor?: number;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t\t/** Parse time (optional, for debugging) */\n\t\tparseTime?: number;\n\t\t/** Performance start time (optional, for debugging) */\n\t\tperfStart?: number;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst {\n\t\tmergeByMaterial = true,\n\t\tapplyTransforms = true,\n\t\tscaleFactor = 1,\n\t\tdebug = false,\n\t\tparseTime = 0,\n\t\tperfStart = debug ? performance.now() : 0\n\t} = options ?? {};\n\n\tlet decodeTime = 0;\n\tlet meshCreateTime = 0;\n\n\ttry {\n\t\tconst decodeStart = performance.now();\n\t\tconst parsed = parseBinaryMeshBatch(batch.compressedData);\n\t\tdecodeTime = performance.now() - decodeStart;\n\n\t\t// Prefer materials/groups from the blob's embedded metadata — that's the source of truth\n\t\t// the C# writer emits. Fall back to the outer envelope for resilience (e.g. if a future\n\t\t// transport drops them from the blob's metadata to save bytes).\n\t\tconst materialsSrc = parsed.metadata.materials ?? batch.materials;\n\t\tconst groups = parsed.metadata.groups ?? batch.groups;\n\t\tconst sourceComponentId = parsed.metadata.sourceComponentId ?? batch.sourceComponentId;\n\n\t\tconst isFloat32 = (parsed.flags & FLAG_FLOAT32) !== 0;\n\n\t\t// Dequantize once up-front into a single Float32Array. Downstream code (per-group merging,\n\t\t// computeVertexNormals, ground-offset, scaleFactor) all expect world-unit floats, and a\n\t\t// single linear pass over the int16 buffer is far cheaper than the legacy gunzip + base64\n\t\t// path. The Z-up -> Y-up rotation, when requested, is folded into the same pass.\n\t\tconst worldVertices = isFloat32\n\t\t\t? maybeRotateFloat32Vertices(parsed.vertices as Float32Array, applyTransforms)\n\t\t\t: dequantizeInt16(\n\t\t\t\t\tparsed.vertices as Int16Array,\n\t\t\t\t\tparsed.origin,\n\t\t\t\t\tparsed.scale,\n\t\t\t\t\tapplyTransforms\n\t\t\t\t);\n\n\t\tif (debug) {\n\t\t\tconst blobBytes = approximateBase64DecodedBytes(batch.compressedData);\n\t\t\tconst wireBytes = parsed.vertices.byteLength + parsed.indices.byteLength;\n\t\t\tgetLogger().debug('Mesh Batch Stats:');\n\t\t\tgetLogger().debug(` Materials: ${materialsSrc.length} | Groups: ${groups.length}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Vertices: ${parsed.vertices.length / 3} | Indices: ${parsed.indices.length}`\n\t\t\t);\n\t\t\tgetLogger().debug(` Format: ${isFloat32 ? 'float32' : 'int16 quantized'}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Blob: ${(blobBytes / 1024 / 1024).toFixed(2)} MB | Geometry on wire: ${(wireBytes / 1024 / 1024).toFixed(2)} MB`\n\t\t\t);\n\t\t}\n\n\t\tconst meshCreateStart = performance.now();\n\t\tconst materials = materialsSrc.map(createMaterial);\n\n\t\tconst meshes: THREE.Mesh[] = [];\n\n\t\tfor (const group of groups) {\n\t\t\tif (mergeByMaterial && group.meshes.length > 1) {\n\t\t\t\tconst mergedMesh = createMergedMesh(group, worldVertices, parsed.indices, materials);\n\t\t\t\tmergedMesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\tmeshes.push(mergedMesh);\n\t\t\t} else {\n\t\t\t\tconst individualMeshes = createIndividualMeshes(\n\t\t\t\t\tgroup,\n\t\t\t\t\tworldVertices,\n\t\t\t\t\tparsed.indices,\n\t\t\t\t\tmaterials\n\t\t\t\t);\n\t\t\t\tfor (const mesh of individualMeshes) {\n\t\t\t\t\tmesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\t}\n\t\t\t\tmeshes.push(...individualMeshes);\n\t\t\t}\n\t\t}\n\n\t\tif (scaleFactor !== 1) {\n\t\t\tfor (const mesh of meshes) {\n\t\t\t\tmesh.scale.set(scaleFactor, scaleFactor, scaleFactor);\n\t\t\t}\n\t\t}\n\n\t\tmeshCreateTime = performance.now() - meshCreateStart;\n\n\t\tif (debug) {\n\t\t\tconst totalTime = performance.now() - perfStart;\n\t\t\tgetLogger().debug('Performance:');\n\t\t\tif (parseTime > 0) getLogger().debug(` Parse JSON: ${parseTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Decode binary: ${decodeTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Create Meshes: ${meshCreateTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Total: ${totalTime.toFixed(2)}ms`);\n\t\t}\n\n\t\treturn meshes;\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch object:', error);\n\t\treturn [];\n\t}\n}\n\n// ============================================================================\n// DEQUANTIZATION\n// ============================================================================\n\n/**\n * Reconstructs world-unit float32 positions from int16 quantized values.\n *\n * Mirrors the encoder formula: `world = origin + (q + 32767) * scale`. When\n * `applyCoordinateTransform=true` we fold the Rhino Z-up -> Three Y-up shuffle into the same pass\n * (`(x, y, z) -> (x, z, -y)`), saving a second walk over the buffer.\n */\nfunction dequantizeInt16(\n\tq: Int16Array,\n\torigin: [number, number, number],\n\tscale: [number, number, number],\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tconst out = new Float32Array(q.length);\n\tconst ox = origin[0];\n\tconst oy = origin[1];\n\tconst oz = origin[2];\n\tconst sx = scale[0];\n\tconst sy = scale[1];\n\tconst sz = scale[2];\n\n\tif (applyCoordinateTransform) {\n\t\t// Rotate -90 deg around X: (x, y, z) -> (x, z, -y)\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tconst wx = ox + (q[i]! + 32767) * sx;\n\t\t\tconst wy = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tconst wz = oz + (q[i + 2]! + 32767) * sz;\n\t\t\tout[i] = wx;\n\t\t\tout[i + 1] = wz;\n\t\t\tout[i + 2] = -wy;\n\t\t}\n\t} else {\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tout[i] = ox + (q[i]! + 32767) * sx;\n\t\t\tout[i + 1] = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tout[i + 2] = oz + (q[i + 2]! + 32767) * sz;\n\t\t}\n\t}\n\n\treturn out;\n}\n\n/**\n * For float32 batches: when no transform is needed we can pass through the parser's view; the\n * caller doesn't mutate it. When the rotation is needed we have to allocate.\n */\nfunction maybeRotateFloat32Vertices(\n\tvertices: Float32Array,\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tif (!applyCoordinateTransform) return vertices;\n\n\tconst out = new Float32Array(vertices.length);\n\tfor (let i = 0; i < vertices.length; i += 3) {\n\t\tconst x = vertices[i]!;\n\t\tconst y = vertices[i + 1]!;\n\t\tconst z = vertices[i + 2]!;\n\t\tout[i] = x;\n\t\tout[i + 1] = z;\n\t\tout[i + 2] = -y;\n\t}\n\treturn out;\n}\n\n// ============================================================================\n// MATERIAL CONSTRUCTION\n// ============================================================================\n\nfunction createMaterial(matData: SerializableMaterial): THREE.MeshPhysicalMaterial {\n\tconst color = parseColor(matData.color);\n\n\treturn new THREE.MeshPhysicalMaterial({\n\t\tcolor,\n\t\tmetalness: matData.metalness,\n\t\troughness: matData.roughness,\n\t\topacity: matData.opacity,\n\t\ttransparent: matData.transparent,\n\t\tside: THREE.DoubleSide,\n\t\t// Reduced polygon offset to minimize artifacts\n\t\t// Only use minimal offset to prevent z-fighting on coplanar faces\n\t\tpolygonOffset: true,\n\t\tpolygonOffsetFactor: 0.5,\n\t\tpolygonOffsetUnits: 0.5,\n\t\t// Improve depth rendering\n\t\tdepthWrite: true,\n\t\tdepthTest: true\n\t});\n}\n\n// ============================================================================\n// MESH CONSTRUCTION\n// ============================================================================\n\n/**\n * Creates a merged mesh from multiple meshes sharing the same material.\n *\n * Indices in the parser output already reference offsets into the combined vertex array (the C#\n * pipeline rebases per-mesh local indices into combined-array indices when assembling the batch).\n * For merged meshes we copy the relevant slices into a fresh contiguous buffer and shift indices\n * to match the new layout.\n */\nfunction createMergedMesh(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh {\n\tlet totalVertexCount = 0;\n\tlet totalIndexCount = 0;\n\tfor (const meshMeta of group.meshes) {\n\t\ttotalVertexCount += meshMeta.vertexCount;\n\t\ttotalIndexCount += meshMeta.indexCount;\n\t}\n\n\tconst mergedVertices = new Float32Array(totalVertexCount * 3);\n\tconst mergedIndices = new Uint32Array(totalIndexCount);\n\n\tlet vertexWriteCursor = 0;\n\tlet indexWriteCursor = 0;\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\t\tmergedVertices.set(\n\t\t\tallVertices.subarray(componentStart, componentStart + componentLen),\n\t\t\tvertexWriteCursor * 3\n\t\t);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst indexShift = vertexWriteCursor - meshMeta.vertexStart;\n\t\tif (indexShift === 0) {\n\t\t\tmergedIndices.set(indicesSlice, indexWriteCursor);\n\t\t} else {\n\t\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\t\tmergedIndices[indexWriteCursor + i] = indicesSlice[i]! + indexShift;\n\t\t\t}\n\t\t}\n\n\t\tvertexWriteCursor += meshMeta.vertexCount;\n\t\tindexWriteCursor += meshMeta.indexCount;\n\t}\n\n\tconst geometry = new THREE.BufferGeometry();\n\tgeometry.setAttribute('position', new THREE.BufferAttribute(mergedVertices, 3));\n\tgeometry.setIndex(new THREE.BufferAttribute(mergedIndices, 1));\n\tgeometry.computeVertexNormals();\n\n\tconst threeMesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\tconst firstMesh = group.meshes[0];\n\tconst meshNames = group.meshes.map((m) => m.name).filter((name) => name && name.length > 0);\n\tthreeMesh.name = meshNames.length > 0 ? meshNames[0]! : `merged_material_${group.materialId}`;\n\tthreeMesh.castShadow = true;\n\tthreeMesh.receiveShadow = true;\n\n\tthreeMesh.userData = {\n\t\tname: threeMesh.name,\n\t\tlayer: firstMesh?.layer ?? '',\n\t\toriginalIndex: firstMesh?.originalIndex ?? 0,\n\t\tmetadata: firstMesh?.metadata ?? {},\n\t\tmergedFrom: group.meshes.slice(1).map((m) => ({\n\t\t\tname: m.name,\n\t\t\tlayer: m.layer,\n\t\t\toriginalIndex: m.originalIndex\n\t\t}))\n\t};\n\n\treturn threeMesh;\n}\n\n/**\n * Creates individual meshes from a material group. Each mesh's indices are rebased so they\n * address its own local vertex slice starting from 0.\n */\nfunction createIndividualMeshes(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh[] {\n\tconst meshes: THREE.Mesh[] = [];\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\n\t\t// `subarray` returns a view; copy via `slice` so the BufferAttribute owns its memory and\n\t\t// downstream code (dispose/reuse) can't surprise us by sharing the parser's buffer.\n\t\tconst vertices = allVertices.slice(componentStart, componentStart + componentLen);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst rebasedIndices = new Uint32Array(indicesSlice.length);\n\t\tconst baseIndex = meshMeta.vertexStart;\n\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\trebasedIndices[i] = indicesSlice[i]! - baseIndex;\n\t\t}\n\n\t\tconst geometry = new THREE.BufferGeometry();\n\t\tgeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));\n\t\tgeometry.setIndex(new THREE.BufferAttribute(rebasedIndices, 1));\n\t\tgeometry.computeVertexNormals();\n\n\t\tconst mesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\t\tmesh.name = meshMeta.name;\n\t\tmesh.userData = {\n\t\t\tname: meshMeta.name,\n\t\t\tlayer: meshMeta.layer ?? '',\n\t\t\toriginalIndex: meshMeta.originalIndex,\n\t\t\tmetadata: meshMeta.metadata ?? {}\n\t\t};\n\t\tmesh.castShadow = true;\n\t\tmesh.receiveShadow = true;\n\n\t\tmeshes.push(mesh);\n\t}\n\n\treturn meshes;\n}\n\n// ============================================================================\n// DEBUG HELPERS\n// ============================================================================\n\nfunction approximateBase64DecodedBytes(base64: string): number {\n\treturn Math.floor((base64.length * 3) / 4);\n}\n","import { decodeBase64ToBinary } from '@/core/utils/encoding';\nimport { RhinoComputeError, ErrorCodes } from '@/core/errors';\n\nimport type { MaterialGroup, SerializableMaterial } from './types';\n\n// ============================================================================\n// WIRE FORMAT CONSTANTS\n// ============================================================================\n\n/** \"SLVA\" little-endian. */\nexport const BINARY_MESH_MAGIC = 0x41564c53;\n/** Bumped on any wire-layout change. */\nexport const BINARY_MESH_VERSION = 1;\n/** Bit 0 of the geometry flags word: 0 = int16 quantized, 1 = float32 raw. */\nexport const FLAG_FLOAT32 = 0x1;\n\nconst HEADER_PREAMBLE_BYTES = 4 /* magic */ + 4 /* version */ + 4; /* metadataLen */\nconst GEOMETRY_HEADER_BYTES =\n\t4 /* flags */ + 24 /* origin (3 x f64) */ + 24 /* scale (3 x f64) */ + 4; /* vertexCount */\n\n// ============================================================================\n// PARSED TYPES\n// ============================================================================\n\n/**\n * Metadata JSON embedded inside the binary blob.\n *\n * This is the same shape as a `MeshBatch` minus the `compressedData` field (the blob is opaque to\n * its own metadata header). Kept separate from the public `MeshBatch` type because the blob's\n * metadata never carries `compressedData` itself — it would be circular.\n */\nexport interface BinaryMeshMetadata {\n\tmaterials: SerializableMaterial[];\n\tgroups: MaterialGroup[];\n\tsourceComponentId?: string;\n}\n\n/**\n * Result of parsing a binary mesh blob.\n *\n * `vertices` and `indices` are typed-array views over the original `ArrayBuffer` — zero copies.\n * The consumer is responsible for not mutating the underlying buffer if it cares about safety,\n * or for calling `.slice()` to detach.\n */\nexport interface ParsedBinaryMeshBatch {\n\tmetadata: BinaryMeshMetadata;\n\tflags: number;\n\tvertices: Int16Array | Float32Array;\n\tindices: Uint32Array;\n\torigin: [number, number, number];\n\tscale: [number, number, number];\n}\n\n// ============================================================================\n// PARSER\n// ============================================================================\n\n/**\n * Parses a binary mesh batch blob in the SLVA wire format.\n *\n * The blob layout is:\n * ```\n * [4] magic = \"SLVA\" (0x53 0x4C 0x56 0x41)\n * [4] version = uint32 (currently 1)\n * [4] metadataLen = uint32 byte length of UTF-8 metadata JSON\n * [N] metadata = UTF-8 JSON (materials, groups, sourceComponentId, ...)\n * [4] flags = uint32 (bit 0: 0 = int16 quantized, 1 = float32 raw)\n * [24] origin = 3 x float64\n * [24] scale = 3 x float64 (step per int16 unit; identity for float32)\n * [4] vertexCount = uint32 number of vertices (positions = vertexCount * 3 components)\n * [V] vertices = int16[vertexCount*3] OR float32[vertexCount*3]\n * [4] indexCount = uint32 number of indices\n * [I] indices = uint32[indexCount]\n * ```\n *\n * For int16 vertices: world position = `origin + (q + 32767) * scale`. This matches Three.js\n * `BufferAttribute(arr, 3, true)` (`normalized: true`) semantics when the per-mesh transform\n * encodes `origin + scale`.\n *\n * For float32: `origin = (0, 0, 0)`, `scale = (1, 1, 1)`, vertices are raw world positions.\n *\n * @param input - The blob, as either an `ArrayBuffer`/`Uint8Array` (binary transport) or a\n * base64-encoded string (today's JSON-envelope transport).\n * @returns Decoded metadata plus typed-array views into the geometry payload.\n * @throws {RhinoComputeError} On invalid magic, unknown version, or truncated input.\n */\nexport function parseBinaryMeshBatch(\n\tinput: ArrayBuffer | Uint8Array | string\n): ParsedBinaryMeshBatch {\n\tconst bytes = toUint8Array(input);\n\tconst view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n\n\tif (bytes.byteLength < HEADER_PREAMBLE_BYTES) {\n\t\tthrow fail('Blob too small to contain SLVA header.', {\n\t\t\texpectedBytes: HEADER_PREAMBLE_BYTES,\n\t\t\tavailableBytes: bytes.byteLength\n\t\t});\n\t}\n\n\tlet offset = 0;\n\n\tconst magic = view.getUint32(offset, true);\n\toffset += 4;\n\tif (magic !== BINARY_MESH_MAGIC) {\n\t\tthrow fail(`Invalid SLVA magic: 0x${magic.toString(16)}`, {\n\t\t\texpectedMagic: `0x${BINARY_MESH_MAGIC.toString(16)}`,\n\t\t\tactualMagic: `0x${magic.toString(16)}`\n\t\t});\n\t}\n\n\tconst version = view.getUint32(offset, true);\n\toffset += 4;\n\tif (version !== BINARY_MESH_VERSION) {\n\t\tthrow fail(`Unsupported SLVA version: ${version}`, {\n\t\t\texpectedVersion: BINARY_MESH_VERSION,\n\t\t\tactualVersion: version\n\t\t});\n\t}\n\n\tconst metadataLen = view.getUint32(offset, true);\n\toffset += 4;\n\tif (offset + metadataLen > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read metadata JSON.', {\n\t\t\texpectedBytes: metadataLen,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst metadataBytes = bytes.subarray(offset, offset + metadataLen);\n\toffset += metadataLen;\n\n\tlet metadata: BinaryMeshMetadata;\n\ttry {\n\t\tmetadata = JSON.parse(decodeUtf8(metadataBytes)) as BinaryMeshMetadata;\n\t} catch (error) {\n\t\tthrow fail(\n\t\t\t`Failed to parse metadata JSON: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t{ metadataLen }\n\t\t);\n\t}\n\n\tif (offset + GEOMETRY_HEADER_BYTES > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read geometry header.', {\n\t\t\texpectedBytes: GEOMETRY_HEADER_BYTES,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst flags = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst originX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst scaleX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst vertexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst useFloat32 = (flags & FLAG_FLOAT32) !== 0;\n\tconst componentCount = vertexCount * 3;\n\tconst bytesPerComponent = useFloat32 ? 4 : 2;\n\tconst verticesByteLength = componentCount * bytesPerComponent;\n\n\tif (offset + verticesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read vertices.', {\n\t\t\texpectedBytes: verticesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tuseFloat32,\n\t\t\tvertexCount\n\t\t});\n\t}\n\n\t// Typed-array views require alignment to the element size. The header lays out the geometry\n\t// block such that the vertex byte offset is always 4-aligned (preamble 12 + metadataLen + 4 +\n\t// 48 + 4). float32 needs 4-byte alignment (satisfied), int16 needs 2-byte alignment\n\t// (satisfied). We can take a zero-copy view as long as `bytes.byteOffset + offset` agrees with\n\t// that alignment in the underlying buffer — a wrapper Uint8Array could violate it. Fall back\n\t// to a fresh copy if so.\n\tconst absoluteOffset = bytes.byteOffset + offset;\n\tconst verticesView = useFloat32\n\t\t? readFloat32Vertices(bytes.buffer, absoluteOffset, componentCount)\n\t\t: readInt16Vertices(bytes.buffer, absoluteOffset, componentCount);\n\toffset += verticesByteLength;\n\n\tif (offset + 4 > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read index count.', {\n\t\t\texpectedBytes: 4,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\tconst indexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst indicesByteLength = indexCount * 4;\n\tif (offset + indicesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read indices.', {\n\t\t\texpectedBytes: indicesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tindexCount\n\t\t});\n\t}\n\n\tconst indicesView = readUint32Indices(bytes.buffer, bytes.byteOffset + offset, indexCount);\n\n\treturn {\n\t\tmetadata,\n\t\tflags,\n\t\tvertices: verticesView,\n\t\tindices: indicesView,\n\t\torigin: [originX, originY, originZ],\n\t\tscale: [scaleX, scaleY, scaleZ]\n\t};\n}\n\n// ============================================================================\n// HELPERS\n// ============================================================================\n\nfunction toUint8Array(input: ArrayBuffer | Uint8Array | string): Uint8Array {\n\tif (typeof input === 'string') {\n\t\treturn decodeBase64ToBinary(input);\n\t}\n\tif (input instanceof Uint8Array) {\n\t\treturn input;\n\t}\n\treturn new Uint8Array(input);\n}\n\nfunction decodeUtf8(bytes: Uint8Array): string {\n\tif (typeof TextDecoder !== 'undefined') {\n\t\treturn new TextDecoder('utf-8').decode(bytes);\n\t}\n\t// Node fallback (Buffer is utf-8 by default).\n\tif (\n\t\ttypeof (globalThis as { Buffer?: { from(b: Uint8Array): { toString(enc: string): string } } })\n\t\t\t.Buffer !== 'undefined'\n\t) {\n\t\treturn (\n\t\t\tglobalThis as { Buffer: { from(b: Uint8Array): { toString(enc: string): string } } }\n\t\t).Buffer.from(bytes).toString('utf-8');\n\t}\n\tthrow new RhinoComputeError(\n\t\t'No UTF-8 decoder available in this environment.',\n\t\tErrorCodes.INVALID_STATE\n\t);\n}\n\nfunction readInt16Vertices(buffer: ArrayBufferLike, byteOffset: number, count: number): Int16Array {\n\tif (count === 0) return new Int16Array(0);\n\tif (byteOffset % 2 === 0) {\n\t\treturn new Int16Array(buffer, byteOffset, count);\n\t}\n\t// Misaligned (rare — would require a wrapper Uint8Array with odd byteOffset).\n\tconst copy = new Uint8Array(count * 2);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 2));\n\treturn new Int16Array(copy.buffer);\n}\n\nfunction readFloat32Vertices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Float32Array {\n\tif (count === 0) return new Float32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Float32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Float32Array(copy.buffer);\n}\n\nfunction readUint32Indices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Uint32Array {\n\tif (count === 0) return new Uint32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Uint32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Uint32Array(copy.buffer);\n}\n\nfunction fail(message: string, context: Record<string, unknown>): RhinoComputeError {\n\treturn new RhinoComputeError(message, ErrorCodes.VALIDATION_ERROR, { context });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/selva-compute/selva-compute/dist/visualization-3HFOMXUY.cjs","../src/features/visualization/webdisplay/batch-parser.ts","../src/features/visualization/webdisplay/binary-parser.ts"],"names":["BINARY_MESH_MAGIC","BINARY_MESH_VERSION","FLAG_FLOAT32","HEADER_PREAMBLE_BYTES","GEOMETRY_HEADER_BYTES","parseBinaryMeshBatch","input","bytes","toUint8Array","view","fail","offset","magic"],"mappings":"AAAA,2/BAAwC,wDAA4E,wDAAuD,uECApJ,ICUVA,CAAAA,CAAoB,UAAA,CAEpBC,CAAAA,+BAAsB,CAAA,CAEtBC,CAAAA,wBAAe,CAAA,CAEtBC,CAAAA,CAAwB,EAAA,CACxBC,CAAAA,CACL,EAAA,CAoEM,SAASC,CAAAA,CACfC,CAAAA,CACwB,CACxB,IAAMC,CAAAA,CAAQC,CAAAA,CAAaF,CAAK,CAAA,CAC1BG,CAAAA,CAAO,IAAI,QAAA,CAASF,CAAAA,CAAM,MAAA,CAAQA,CAAAA,CAAM,UAAA,CAAYA,CAAAA,CAAM,UAAU,CAAA,CAE1E,EAAA,CAAIA,CAAAA,CAAM,UAAA,CAAaJ,CAAAA,CACtB,MAAMO,CAAAA,CAAK,wCAAA,CAA0C,CACpD,aAAA,CAAeP,CAAAA,CACf,cAAA,CAAgBI,CAAAA,CAAM,UACvB,CAAC,CAAA,CAGF,IAAII,CAAAA,CAAS,CAAA,CAEPC,CAAAA,CAAQH,CAAAA,CAAK,SAAA,CAAUE,CAAAA,CAAQ,CAAA,CAAI,CAAA,CAEzC,EAAA,CADAA,CAAAA,EAAU,CAAA,CACNC,CAAAA,GAAUZ,CAAAA,CACb,MAAMU,CAAAA,CAAK,CAAA,sBAAA,EAAyBE,CAAAA,CAAM,QAAA,CAAS,EAAE,CAAC,CAAA,CAAA","file":"/home/runner/work/selva-compute/selva-compute/dist/visualization-3HFOMXUY.cjs","sourcesContent":[null,"import * as THREE from 'three';\n\nimport { parseColor } from '../threejs/three-helpers';\nimport { getLogger } from '@/core';\n\nimport { FLAG_FLOAT32, parseBinaryMeshBatch } from './binary-parser';\n\nimport type { MeshBatch, MaterialGroup, SerializableMaterial } from './types';\n\n/**\n * Parses a batched mesh JSON and creates Three.js meshes.\n *\n * The geometry payload is the binary \"SLVA\" blob produced by the C# `BinaryGeometryWriter`,\n * base64-encoded into the outer JSON envelope. We `JSON.parse` the small envelope, then hand the\n * blob to `parseBinaryMeshBatch` which decodes the geometry without ever turning it into a string.\n *\n * @internal Low-level mesh parsing — keep internal to `@selvajs/compute`.\n *\n * @param batchJson - JSON string containing the batched mesh data\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatch(\n\tbatchJson: string,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst { mergeByMaterial = true, applyTransforms = true, debug = false } = options ?? {};\n\n\tconst perfStart = debug ? performance.now() : 0;\n\tlet parseTime = 0;\n\n\ttry {\n\t\tconst parseStart = performance.now();\n\t\tconst batch: MeshBatch = JSON.parse(batchJson);\n\t\tparseTime = performance.now() - parseStart;\n\n\t\treturn await parseMeshBatchObject(batch, {\n\t\t\tmergeByMaterial,\n\t\t\tapplyTransforms,\n\t\t\tdebug,\n\t\t\tparseTime,\n\t\t\tperfStart\n\t\t});\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch:', error);\n\t\treturn [];\n\t}\n}\n\n/**\n * Parses a MeshBatch object and creates Three.js meshes.\n *\n * The path is synchronous internally — `parseBinaryMeshBatch` does no IO, just typed-array views\n * over the blob. The function stays `async` so callers don't have to change shape if we move\n * parsing into a worker later.\n *\n * @internal Low-level mesh parsing — keep internal to `@selvajs/compute`.\n *\n * @param batch - MeshBatch object\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatchObject(\n\tbatch: MeshBatch,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Scale factor to apply to meshes (e.g., for unit conversion) */\n\t\tscaleFactor?: number;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t\t/** Parse time (optional, for debugging) */\n\t\tparseTime?: number;\n\t\t/** Performance start time (optional, for debugging) */\n\t\tperfStart?: number;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst {\n\t\tmergeByMaterial = true,\n\t\tapplyTransforms = true,\n\t\tscaleFactor = 1,\n\t\tdebug = false,\n\t\tparseTime = 0,\n\t\tperfStart = debug ? performance.now() : 0\n\t} = options ?? {};\n\n\tlet decodeTime = 0;\n\tlet meshCreateTime = 0;\n\n\ttry {\n\t\tconst decodeStart = performance.now();\n\t\tconst parsed = parseBinaryMeshBatch(batch.compressedData);\n\t\tdecodeTime = performance.now() - decodeStart;\n\n\t\t// Prefer materials/groups from the blob's embedded metadata — that's the source of truth\n\t\t// the C# writer emits. Fall back to the outer envelope for resilience (e.g. if a future\n\t\t// transport drops them from the blob's metadata to save bytes).\n\t\tconst materialsSrc = parsed.metadata.materials ?? batch.materials;\n\t\tconst groups = parsed.metadata.groups ?? batch.groups;\n\t\tconst sourceComponentId = parsed.metadata.sourceComponentId ?? batch.sourceComponentId;\n\n\t\tconst isFloat32 = (parsed.flags & FLAG_FLOAT32) !== 0;\n\n\t\t// Dequantize once up-front into a single Float32Array. Downstream code (per-group merging,\n\t\t// computeVertexNormals, ground-offset, scaleFactor) all expect world-unit floats, and a\n\t\t// single linear pass over the int16 buffer is far cheaper than the legacy gunzip + base64\n\t\t// path. The Z-up -> Y-up rotation, when requested, is folded into the same pass.\n\t\tconst worldVertices = isFloat32\n\t\t\t? maybeRotateFloat32Vertices(parsed.vertices as Float32Array, applyTransforms)\n\t\t\t: dequantizeInt16(\n\t\t\t\t\tparsed.vertices as Int16Array,\n\t\t\t\t\tparsed.origin,\n\t\t\t\t\tparsed.scale,\n\t\t\t\t\tapplyTransforms\n\t\t\t\t);\n\n\t\tif (debug) {\n\t\t\tconst blobBytes = approximateBase64DecodedBytes(batch.compressedData);\n\t\t\tconst wireBytes = parsed.vertices.byteLength + parsed.indices.byteLength;\n\t\t\tgetLogger().debug('Mesh Batch Stats:');\n\t\t\tgetLogger().debug(` Materials: ${materialsSrc.length} | Groups: ${groups.length}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Vertices: ${parsed.vertices.length / 3} | Indices: ${parsed.indices.length}`\n\t\t\t);\n\t\t\tgetLogger().debug(` Format: ${isFloat32 ? 'float32' : 'int16 quantized'}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Blob: ${(blobBytes / 1024 / 1024).toFixed(2)} MB | Geometry on wire: ${(wireBytes / 1024 / 1024).toFixed(2)} MB`\n\t\t\t);\n\t\t}\n\n\t\tconst meshCreateStart = performance.now();\n\t\tconst materials = materialsSrc.map(createMaterial);\n\n\t\tconst meshes: THREE.Mesh[] = [];\n\n\t\tfor (const group of groups) {\n\t\t\tif (mergeByMaterial && group.meshes.length > 1) {\n\t\t\t\tconst mergedMesh = createMergedMesh(group, worldVertices, parsed.indices, materials);\n\t\t\t\tmergedMesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\tmeshes.push(mergedMesh);\n\t\t\t} else {\n\t\t\t\tconst individualMeshes = createIndividualMeshes(\n\t\t\t\t\tgroup,\n\t\t\t\t\tworldVertices,\n\t\t\t\t\tparsed.indices,\n\t\t\t\t\tmaterials\n\t\t\t\t);\n\t\t\t\tfor (const mesh of individualMeshes) {\n\t\t\t\t\tmesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\t}\n\t\t\t\tmeshes.push(...individualMeshes);\n\t\t\t}\n\t\t}\n\n\t\tif (scaleFactor !== 1) {\n\t\t\tfor (const mesh of meshes) {\n\t\t\t\tmesh.scale.set(scaleFactor, scaleFactor, scaleFactor);\n\t\t\t}\n\t\t}\n\n\t\tmeshCreateTime = performance.now() - meshCreateStart;\n\n\t\tif (debug) {\n\t\t\tconst totalTime = performance.now() - perfStart;\n\t\t\tgetLogger().debug('Performance:');\n\t\t\tif (parseTime > 0) getLogger().debug(` Parse JSON: ${parseTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Decode binary: ${decodeTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Create Meshes: ${meshCreateTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Total: ${totalTime.toFixed(2)}ms`);\n\t\t}\n\n\t\treturn meshes;\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch object:', error);\n\t\treturn [];\n\t}\n}\n\n// ============================================================================\n// DEQUANTIZATION\n// ============================================================================\n\n/**\n * Reconstructs world-unit float32 positions from int16 quantized values.\n *\n * Mirrors the encoder formula: `world = origin + (q + 32767) * scale`. When\n * `applyCoordinateTransform=true` we fold the Rhino Z-up -> Three Y-up shuffle into the same pass\n * (`(x, y, z) -> (x, z, -y)`), saving a second walk over the buffer.\n */\nfunction dequantizeInt16(\n\tq: Int16Array,\n\torigin: [number, number, number],\n\tscale: [number, number, number],\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tconst out = new Float32Array(q.length);\n\tconst ox = origin[0];\n\tconst oy = origin[1];\n\tconst oz = origin[2];\n\tconst sx = scale[0];\n\tconst sy = scale[1];\n\tconst sz = scale[2];\n\n\tif (applyCoordinateTransform) {\n\t\t// Rotate -90 deg around X: (x, y, z) -> (x, z, -y)\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tconst wx = ox + (q[i]! + 32767) * sx;\n\t\t\tconst wy = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tconst wz = oz + (q[i + 2]! + 32767) * sz;\n\t\t\tout[i] = wx;\n\t\t\tout[i + 1] = wz;\n\t\t\tout[i + 2] = -wy;\n\t\t}\n\t} else {\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tout[i] = ox + (q[i]! + 32767) * sx;\n\t\t\tout[i + 1] = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tout[i + 2] = oz + (q[i + 2]! + 32767) * sz;\n\t\t}\n\t}\n\n\treturn out;\n}\n\n/**\n * For float32 batches: when no transform is needed we can pass through the parser's view; the\n * caller doesn't mutate it. When the rotation is needed we have to allocate.\n */\nfunction maybeRotateFloat32Vertices(\n\tvertices: Float32Array,\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tif (!applyCoordinateTransform) return vertices;\n\n\tconst out = new Float32Array(vertices.length);\n\tfor (let i = 0; i < vertices.length; i += 3) {\n\t\tconst x = vertices[i]!;\n\t\tconst y = vertices[i + 1]!;\n\t\tconst z = vertices[i + 2]!;\n\t\tout[i] = x;\n\t\tout[i + 1] = z;\n\t\tout[i + 2] = -y;\n\t}\n\treturn out;\n}\n\n// ============================================================================\n// MATERIAL CONSTRUCTION\n// ============================================================================\n\nfunction createMaterial(matData: SerializableMaterial): THREE.MeshPhysicalMaterial {\n\tconst color = parseColor(matData.color);\n\n\treturn new THREE.MeshPhysicalMaterial({\n\t\tcolor,\n\t\tmetalness: matData.metalness,\n\t\troughness: matData.roughness,\n\t\topacity: matData.opacity,\n\t\ttransparent: matData.transparent,\n\t\tside: THREE.DoubleSide,\n\t\t// Reduced polygon offset to minimize artifacts\n\t\t// Only use minimal offset to prevent z-fighting on coplanar faces\n\t\tpolygonOffset: true,\n\t\tpolygonOffsetFactor: 0.5,\n\t\tpolygonOffsetUnits: 0.5,\n\t\t// Improve depth rendering\n\t\tdepthWrite: true,\n\t\tdepthTest: true\n\t});\n}\n\n// ============================================================================\n// MESH CONSTRUCTION\n// ============================================================================\n\n/**\n * Creates a merged mesh from multiple meshes sharing the same material.\n *\n * Indices in the parser output already reference offsets into the combined vertex array (the C#\n * pipeline rebases per-mesh local indices into combined-array indices when assembling the batch).\n * For merged meshes we copy the relevant slices into a fresh contiguous buffer and shift indices\n * to match the new layout.\n */\nfunction createMergedMesh(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh {\n\tlet totalVertexCount = 0;\n\tlet totalIndexCount = 0;\n\tfor (const meshMeta of group.meshes) {\n\t\ttotalVertexCount += meshMeta.vertexCount;\n\t\ttotalIndexCount += meshMeta.indexCount;\n\t}\n\n\tconst mergedVertices = new Float32Array(totalVertexCount * 3);\n\tconst mergedIndices = new Uint32Array(totalIndexCount);\n\n\tlet vertexWriteCursor = 0;\n\tlet indexWriteCursor = 0;\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\t\tmergedVertices.set(\n\t\t\tallVertices.subarray(componentStart, componentStart + componentLen),\n\t\t\tvertexWriteCursor * 3\n\t\t);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst indexShift = vertexWriteCursor - meshMeta.vertexStart;\n\t\tif (indexShift === 0) {\n\t\t\tmergedIndices.set(indicesSlice, indexWriteCursor);\n\t\t} else {\n\t\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\t\tmergedIndices[indexWriteCursor + i] = indicesSlice[i]! + indexShift;\n\t\t\t}\n\t\t}\n\n\t\tvertexWriteCursor += meshMeta.vertexCount;\n\t\tindexWriteCursor += meshMeta.indexCount;\n\t}\n\n\tconst geometry = new THREE.BufferGeometry();\n\tgeometry.setAttribute('position', new THREE.BufferAttribute(mergedVertices, 3));\n\tgeometry.setIndex(new THREE.BufferAttribute(mergedIndices, 1));\n\tgeometry.computeVertexNormals();\n\n\tconst threeMesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\tconst firstMesh = group.meshes[0];\n\tconst meshNames = group.meshes.map((m) => m.name).filter((name) => name && name.length > 0);\n\tthreeMesh.name = meshNames.length > 0 ? meshNames[0]! : `merged_material_${group.materialId}`;\n\tthreeMesh.castShadow = true;\n\tthreeMesh.receiveShadow = true;\n\n\tthreeMesh.userData = {\n\t\tname: threeMesh.name,\n\t\tlayer: firstMesh?.layer ?? '',\n\t\toriginalIndex: firstMesh?.originalIndex ?? 0,\n\t\tmetadata: firstMesh?.metadata ?? {},\n\t\tmergedFrom: group.meshes.slice(1).map((m) => ({\n\t\t\tname: m.name,\n\t\t\tlayer: m.layer,\n\t\t\toriginalIndex: m.originalIndex\n\t\t}))\n\t};\n\n\treturn threeMesh;\n}\n\n/**\n * Creates individual meshes from a material group. Each mesh's indices are rebased so they\n * address its own local vertex slice starting from 0.\n */\nfunction createIndividualMeshes(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh[] {\n\tconst meshes: THREE.Mesh[] = [];\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\n\t\t// `subarray` returns a view; copy via `slice` so the BufferAttribute owns its memory and\n\t\t// downstream code (dispose/reuse) can't surprise us by sharing the parser's buffer.\n\t\tconst vertices = allVertices.slice(componentStart, componentStart + componentLen);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst rebasedIndices = new Uint32Array(indicesSlice.length);\n\t\tconst baseIndex = meshMeta.vertexStart;\n\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\trebasedIndices[i] = indicesSlice[i]! - baseIndex;\n\t\t}\n\n\t\tconst geometry = new THREE.BufferGeometry();\n\t\tgeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));\n\t\tgeometry.setIndex(new THREE.BufferAttribute(rebasedIndices, 1));\n\t\tgeometry.computeVertexNormals();\n\n\t\tconst mesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\t\tmesh.name = meshMeta.name;\n\t\tmesh.userData = {\n\t\t\tname: meshMeta.name,\n\t\t\tlayer: meshMeta.layer ?? '',\n\t\t\toriginalIndex: meshMeta.originalIndex,\n\t\t\tmetadata: meshMeta.metadata ?? {}\n\t\t};\n\t\tmesh.castShadow = true;\n\t\tmesh.receiveShadow = true;\n\n\t\tmeshes.push(mesh);\n\t}\n\n\treturn meshes;\n}\n\n// ============================================================================\n// DEBUG HELPERS\n// ============================================================================\n\nfunction approximateBase64DecodedBytes(base64: string): number {\n\treturn Math.floor((base64.length * 3) / 4);\n}\n","import { decodeBase64ToBinary } from '@/core/utils/encoding';\nimport { RhinoComputeError, ErrorCodes } from '@/core/errors';\n\nimport type { MaterialGroup, SerializableMaterial } from './types';\n\n// ============================================================================\n// WIRE FORMAT CONSTANTS\n// ============================================================================\n\n/** \"SLVA\" little-endian. */\nexport const BINARY_MESH_MAGIC = 0x41564c53;\n/** Bumped on any wire-layout change. */\nexport const BINARY_MESH_VERSION = 1;\n/** Bit 0 of the geometry flags word: 0 = int16 quantized, 1 = float32 raw. */\nexport const FLAG_FLOAT32 = 0x1;\n\nconst HEADER_PREAMBLE_BYTES = 4 /* magic */ + 4 /* version */ + 4; /* metadataLen */\nconst GEOMETRY_HEADER_BYTES =\n\t4 /* flags */ + 24 /* origin (3 x f64) */ + 24 /* scale (3 x f64) */ + 4; /* vertexCount */\n\n// ============================================================================\n// PARSED TYPES\n// ============================================================================\n\n/**\n * Metadata JSON embedded inside the binary blob.\n *\n * This is the same shape as a `MeshBatch` minus the `compressedData` field (the blob is opaque to\n * its own metadata header). Kept separate from the public `MeshBatch` type because the blob's\n * metadata never carries `compressedData` itself — it would be circular.\n */\nexport interface BinaryMeshMetadata {\n\tmaterials: SerializableMaterial[];\n\tgroups: MaterialGroup[];\n\tsourceComponentId?: string;\n}\n\n/**\n * Result of parsing a binary mesh blob.\n *\n * `vertices` and `indices` are typed-array views over the original `ArrayBuffer` — zero copies.\n * The consumer is responsible for not mutating the underlying buffer if it cares about safety,\n * or for calling `.slice()` to detach.\n */\nexport interface ParsedBinaryMeshBatch {\n\tmetadata: BinaryMeshMetadata;\n\tflags: number;\n\tvertices: Int16Array | Float32Array;\n\tindices: Uint32Array;\n\torigin: [number, number, number];\n\tscale: [number, number, number];\n}\n\n// ============================================================================\n// PARSER\n// ============================================================================\n\n/**\n * Parses a binary mesh batch blob in the SLVA wire format.\n *\n * The blob layout is:\n * ```\n * [4] magic = \"SLVA\" (0x53 0x4C 0x56 0x41)\n * [4] version = uint32 (currently 1)\n * [4] metadataLen = uint32 byte length of UTF-8 metadata JSON\n * [N] metadata = UTF-8 JSON (materials, groups, sourceComponentId, ...)\n * [4] flags = uint32 (bit 0: 0 = int16 quantized, 1 = float32 raw)\n * [24] origin = 3 x float64\n * [24] scale = 3 x float64 (step per int16 unit; identity for float32)\n * [4] vertexCount = uint32 number of vertices (positions = vertexCount * 3 components)\n * [V] vertices = int16[vertexCount*3] OR float32[vertexCount*3]\n * [4] indexCount = uint32 number of indices\n * [I] indices = uint32[indexCount]\n * ```\n *\n * For int16 vertices: world position = `origin + (q + 32767) * scale`. This matches Three.js\n * `BufferAttribute(arr, 3, true)` (`normalized: true`) semantics when the per-mesh transform\n * encodes `origin + scale`.\n *\n * For float32: `origin = (0, 0, 0)`, `scale = (1, 1, 1)`, vertices are raw world positions.\n *\n * @param input - The blob, as either an `ArrayBuffer`/`Uint8Array` (binary transport) or a\n * base64-encoded string (today's JSON-envelope transport).\n * @returns Decoded metadata plus typed-array views into the geometry payload.\n * @throws {RhinoComputeError} On invalid magic, unknown version, or truncated input.\n */\nexport function parseBinaryMeshBatch(\n\tinput: ArrayBuffer | Uint8Array | string\n): ParsedBinaryMeshBatch {\n\tconst bytes = toUint8Array(input);\n\tconst view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n\n\tif (bytes.byteLength < HEADER_PREAMBLE_BYTES) {\n\t\tthrow fail('Blob too small to contain SLVA header.', {\n\t\t\texpectedBytes: HEADER_PREAMBLE_BYTES,\n\t\t\tavailableBytes: bytes.byteLength\n\t\t});\n\t}\n\n\tlet offset = 0;\n\n\tconst magic = view.getUint32(offset, true);\n\toffset += 4;\n\tif (magic !== BINARY_MESH_MAGIC) {\n\t\tthrow fail(`Invalid SLVA magic: 0x${magic.toString(16)}`, {\n\t\t\texpectedMagic: `0x${BINARY_MESH_MAGIC.toString(16)}`,\n\t\t\tactualMagic: `0x${magic.toString(16)}`\n\t\t});\n\t}\n\n\tconst version = view.getUint32(offset, true);\n\toffset += 4;\n\tif (version !== BINARY_MESH_VERSION) {\n\t\tthrow fail(`Unsupported SLVA version: ${version}`, {\n\t\t\texpectedVersion: BINARY_MESH_VERSION,\n\t\t\tactualVersion: version\n\t\t});\n\t}\n\n\tconst metadataLen = view.getUint32(offset, true);\n\toffset += 4;\n\tif (offset + metadataLen > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read metadata JSON.', {\n\t\t\texpectedBytes: metadataLen,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst metadataBytes = bytes.subarray(offset, offset + metadataLen);\n\toffset += metadataLen;\n\n\tlet metadata: BinaryMeshMetadata;\n\ttry {\n\t\tmetadata = JSON.parse(decodeUtf8(metadataBytes)) as BinaryMeshMetadata;\n\t} catch (error) {\n\t\tthrow fail(\n\t\t\t`Failed to parse metadata JSON: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t{ metadataLen }\n\t\t);\n\t}\n\n\tif (offset + GEOMETRY_HEADER_BYTES > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read geometry header.', {\n\t\t\texpectedBytes: GEOMETRY_HEADER_BYTES,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst flags = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst originX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst scaleX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst vertexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst useFloat32 = (flags & FLAG_FLOAT32) !== 0;\n\tconst componentCount = vertexCount * 3;\n\tconst bytesPerComponent = useFloat32 ? 4 : 2;\n\tconst verticesByteLength = componentCount * bytesPerComponent;\n\n\tif (offset + verticesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read vertices.', {\n\t\t\texpectedBytes: verticesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tuseFloat32,\n\t\t\tvertexCount\n\t\t});\n\t}\n\n\t// Typed-array views require alignment to the element size. The header lays out the geometry\n\t// block such that the vertex byte offset is always 4-aligned (preamble 12 + metadataLen + 4 +\n\t// 48 + 4). float32 needs 4-byte alignment (satisfied), int16 needs 2-byte alignment\n\t// (satisfied). We can take a zero-copy view as long as `bytes.byteOffset + offset` agrees with\n\t// that alignment in the underlying buffer — a wrapper Uint8Array could violate it. Fall back\n\t// to a fresh copy if so.\n\tconst absoluteOffset = bytes.byteOffset + offset;\n\tconst verticesView = useFloat32\n\t\t? readFloat32Vertices(bytes.buffer, absoluteOffset, componentCount)\n\t\t: readInt16Vertices(bytes.buffer, absoluteOffset, componentCount);\n\toffset += verticesByteLength;\n\n\tif (offset + 4 > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read index count.', {\n\t\t\texpectedBytes: 4,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\tconst indexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst indicesByteLength = indexCount * 4;\n\tif (offset + indicesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read indices.', {\n\t\t\texpectedBytes: indicesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tindexCount\n\t\t});\n\t}\n\n\tconst indicesView = readUint32Indices(bytes.buffer, bytes.byteOffset + offset, indexCount);\n\n\treturn {\n\t\tmetadata,\n\t\tflags,\n\t\tvertices: verticesView,\n\t\tindices: indicesView,\n\t\torigin: [originX, originY, originZ],\n\t\tscale: [scaleX, scaleY, scaleZ]\n\t};\n}\n\n// ============================================================================\n// HELPERS\n// ============================================================================\n\nfunction toUint8Array(input: ArrayBuffer | Uint8Array | string): Uint8Array {\n\tif (typeof input === 'string') {\n\t\treturn decodeBase64ToBinary(input);\n\t}\n\tif (input instanceof Uint8Array) {\n\t\treturn input;\n\t}\n\treturn new Uint8Array(input);\n}\n\nfunction decodeUtf8(bytes: Uint8Array): string {\n\tif (typeof TextDecoder !== 'undefined') {\n\t\treturn new TextDecoder('utf-8').decode(bytes);\n\t}\n\t// Node fallback (Buffer is utf-8 by default).\n\tif (\n\t\ttypeof (globalThis as { Buffer?: { from(b: Uint8Array): { toString(enc: string): string } } })\n\t\t\t.Buffer !== 'undefined'\n\t) {\n\t\treturn (\n\t\t\tglobalThis as { Buffer: { from(b: Uint8Array): { toString(enc: string): string } } }\n\t\t).Buffer.from(bytes).toString('utf-8');\n\t}\n\tthrow new RhinoComputeError(\n\t\t'No UTF-8 decoder available in this environment.',\n\t\tErrorCodes.INVALID_STATE\n\t);\n}\n\nfunction readInt16Vertices(buffer: ArrayBufferLike, byteOffset: number, count: number): Int16Array {\n\tif (count === 0) return new Int16Array(0);\n\tif (byteOffset % 2 === 0) {\n\t\treturn new Int16Array(buffer, byteOffset, count);\n\t}\n\t// Misaligned (rare — would require a wrapper Uint8Array with odd byteOffset).\n\tconst copy = new Uint8Array(count * 2);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 2));\n\treturn new Int16Array(copy.buffer);\n}\n\nfunction readFloat32Vertices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Float32Array {\n\tif (count === 0) return new Float32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Float32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Float32Array(copy.buffer);\n}\n\nfunction readUint32Indices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Uint32Array {\n\tif (count === 0) return new Uint32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Uint32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Uint32Array(copy.buffer);\n}\n\nfunction fail(message: string, context: Record<string, unknown>): RhinoComputeError {\n\treturn new RhinoComputeError(message, ErrorCodes.VALIDATION_ERROR, { context });\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/features/visualization/webdisplay/batch-parser.ts","../src/features/visualization/webdisplay/binary-parser.ts","../src/features/visualization/webdisplay/webdisplay-parser.ts"],"sourcesContent":["import * as THREE from 'three';\n\nimport { parseColor } from '../threejs/three-helpers';\nimport { getLogger } from '@/core';\n\nimport { FLAG_FLOAT32, parseBinaryMeshBatch } from './binary-parser';\n\nimport type { MeshBatch, MaterialGroup, SerializableMaterial } from './types';\n\n/**\n * Parses a batched mesh JSON and creates Three.js meshes.\n *\n * The geometry payload is the binary \"SLVA\" blob produced by the C# `BinaryGeometryWriter`,\n * base64-encoded into the outer JSON envelope. We `JSON.parse` the small envelope, then hand the\n * blob to `parseBinaryMeshBatch` which decodes the geometry without ever turning it into a string.\n *\n * @internal Low-level mesh parsing — keep internal to `@selvajs/compute`.\n *\n * @param batchJson - JSON string containing the batched mesh data\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatch(\n\tbatchJson: string,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst { mergeByMaterial = true, applyTransforms = true, debug = false } = options ?? {};\n\n\tconst perfStart = debug ? performance.now() : 0;\n\tlet parseTime = 0;\n\n\ttry {\n\t\tconst parseStart = performance.now();\n\t\tconst batch: MeshBatch = JSON.parse(batchJson);\n\t\tparseTime = performance.now() - parseStart;\n\n\t\treturn await parseMeshBatchObject(batch, {\n\t\t\tmergeByMaterial,\n\t\t\tapplyTransforms,\n\t\t\tdebug,\n\t\t\tparseTime,\n\t\t\tperfStart\n\t\t});\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch:', error);\n\t\treturn [];\n\t}\n}\n\n/**\n * Parses a MeshBatch object and creates Three.js meshes.\n *\n * The path is synchronous internally — `parseBinaryMeshBatch` does no IO, just typed-array views\n * over the blob. The function stays `async` so callers don't have to change shape if we move\n * parsing into a worker later.\n *\n * @internal Low-level mesh parsing — keep internal to `@selvajs/compute`.\n *\n * @param batch - MeshBatch object\n * @param options - Rendering options\n * @returns Promise resolving to array of Three.js mesh objects\n */\nexport async function parseMeshBatchObject(\n\tbatch: MeshBatch,\n\toptions?: {\n\t\t/** Merge meshes with same material into single geometry*/\n\t\tmergeByMaterial?: boolean;\n\t\t/** Apply coordinate system transformations */\n\t\tapplyTransforms?: boolean;\n\t\t/** Scale factor to apply to meshes (e.g., for unit conversion) */\n\t\tscaleFactor?: number;\n\t\t/** Enable performance monitoring */\n\t\tdebug?: boolean;\n\t\t/** Parse time (optional, for debugging) */\n\t\tparseTime?: number;\n\t\t/** Performance start time (optional, for debugging) */\n\t\tperfStart?: number;\n\t}\n): Promise<THREE.Mesh[]> {\n\tconst {\n\t\tmergeByMaterial = true,\n\t\tapplyTransforms = true,\n\t\tscaleFactor = 1,\n\t\tdebug = false,\n\t\tparseTime = 0,\n\t\tperfStart = debug ? performance.now() : 0\n\t} = options ?? {};\n\n\tlet decodeTime = 0;\n\tlet meshCreateTime = 0;\n\n\ttry {\n\t\tconst decodeStart = performance.now();\n\t\tconst parsed = parseBinaryMeshBatch(batch.compressedData);\n\t\tdecodeTime = performance.now() - decodeStart;\n\n\t\t// Prefer materials/groups from the blob's embedded metadata — that's the source of truth\n\t\t// the C# writer emits. Fall back to the outer envelope for resilience (e.g. if a future\n\t\t// transport drops them from the blob's metadata to save bytes).\n\t\tconst materialsSrc = parsed.metadata.materials ?? batch.materials;\n\t\tconst groups = parsed.metadata.groups ?? batch.groups;\n\t\tconst sourceComponentId = parsed.metadata.sourceComponentId ?? batch.sourceComponentId;\n\n\t\tconst isFloat32 = (parsed.flags & FLAG_FLOAT32) !== 0;\n\n\t\t// Dequantize once up-front into a single Float32Array. Downstream code (per-group merging,\n\t\t// computeVertexNormals, ground-offset, scaleFactor) all expect world-unit floats, and a\n\t\t// single linear pass over the int16 buffer is far cheaper than the legacy gunzip + base64\n\t\t// path. The Z-up -> Y-up rotation, when requested, is folded into the same pass.\n\t\tconst worldVertices = isFloat32\n\t\t\t? maybeRotateFloat32Vertices(parsed.vertices as Float32Array, applyTransforms)\n\t\t\t: dequantizeInt16(\n\t\t\t\t\tparsed.vertices as Int16Array,\n\t\t\t\t\tparsed.origin,\n\t\t\t\t\tparsed.scale,\n\t\t\t\t\tapplyTransforms\n\t\t\t\t);\n\n\t\tif (debug) {\n\t\t\tconst blobBytes = approximateBase64DecodedBytes(batch.compressedData);\n\t\t\tconst wireBytes = parsed.vertices.byteLength + parsed.indices.byteLength;\n\t\t\tgetLogger().debug('Mesh Batch Stats:');\n\t\t\tgetLogger().debug(` Materials: ${materialsSrc.length} | Groups: ${groups.length}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Vertices: ${parsed.vertices.length / 3} | Indices: ${parsed.indices.length}`\n\t\t\t);\n\t\t\tgetLogger().debug(` Format: ${isFloat32 ? 'float32' : 'int16 quantized'}`);\n\t\t\tgetLogger().debug(\n\t\t\t\t` Blob: ${(blobBytes / 1024 / 1024).toFixed(2)} MB | Geometry on wire: ${(wireBytes / 1024 / 1024).toFixed(2)} MB`\n\t\t\t);\n\t\t}\n\n\t\tconst meshCreateStart = performance.now();\n\t\tconst materials = materialsSrc.map(createMaterial);\n\n\t\tconst meshes: THREE.Mesh[] = [];\n\n\t\tfor (const group of groups) {\n\t\t\tif (mergeByMaterial && group.meshes.length > 1) {\n\t\t\t\tconst mergedMesh = createMergedMesh(group, worldVertices, parsed.indices, materials);\n\t\t\t\tmergedMesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\tmeshes.push(mergedMesh);\n\t\t\t} else {\n\t\t\t\tconst individualMeshes = createIndividualMeshes(\n\t\t\t\t\tgroup,\n\t\t\t\t\tworldVertices,\n\t\t\t\t\tparsed.indices,\n\t\t\t\t\tmaterials\n\t\t\t\t);\n\t\t\t\tfor (const mesh of individualMeshes) {\n\t\t\t\t\tmesh.userData.sourceComponentId = sourceComponentId ?? null;\n\t\t\t\t}\n\t\t\t\tmeshes.push(...individualMeshes);\n\t\t\t}\n\t\t}\n\n\t\tif (scaleFactor !== 1) {\n\t\t\tfor (const mesh of meshes) {\n\t\t\t\tmesh.scale.set(scaleFactor, scaleFactor, scaleFactor);\n\t\t\t}\n\t\t}\n\n\t\tmeshCreateTime = performance.now() - meshCreateStart;\n\n\t\tif (debug) {\n\t\t\tconst totalTime = performance.now() - perfStart;\n\t\t\tgetLogger().debug('Performance:');\n\t\t\tif (parseTime > 0) getLogger().debug(` Parse JSON: ${parseTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Decode binary: ${decodeTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Create Meshes: ${meshCreateTime.toFixed(2)}ms`);\n\t\t\tgetLogger().debug(` Total: ${totalTime.toFixed(2)}ms`);\n\t\t}\n\n\t\treturn meshes;\n\t} catch (error) {\n\t\tgetLogger().error('Error parsing mesh batch object:', error);\n\t\treturn [];\n\t}\n}\n\n// ============================================================================\n// DEQUANTIZATION\n// ============================================================================\n\n/**\n * Reconstructs world-unit float32 positions from int16 quantized values.\n *\n * Mirrors the encoder formula: `world = origin + (q + 32767) * scale`. When\n * `applyCoordinateTransform=true` we fold the Rhino Z-up -> Three Y-up shuffle into the same pass\n * (`(x, y, z) -> (x, z, -y)`), saving a second walk over the buffer.\n */\nfunction dequantizeInt16(\n\tq: Int16Array,\n\torigin: [number, number, number],\n\tscale: [number, number, number],\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tconst out = new Float32Array(q.length);\n\tconst ox = origin[0];\n\tconst oy = origin[1];\n\tconst oz = origin[2];\n\tconst sx = scale[0];\n\tconst sy = scale[1];\n\tconst sz = scale[2];\n\n\tif (applyCoordinateTransform) {\n\t\t// Rotate -90 deg around X: (x, y, z) -> (x, z, -y)\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tconst wx = ox + (q[i]! + 32767) * sx;\n\t\t\tconst wy = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tconst wz = oz + (q[i + 2]! + 32767) * sz;\n\t\t\tout[i] = wx;\n\t\t\tout[i + 1] = wz;\n\t\t\tout[i + 2] = -wy;\n\t\t}\n\t} else {\n\t\tfor (let i = 0; i < q.length; i += 3) {\n\t\t\tout[i] = ox + (q[i]! + 32767) * sx;\n\t\t\tout[i + 1] = oy + (q[i + 1]! + 32767) * sy;\n\t\t\tout[i + 2] = oz + (q[i + 2]! + 32767) * sz;\n\t\t}\n\t}\n\n\treturn out;\n}\n\n/**\n * For float32 batches: when no transform is needed we can pass through the parser's view; the\n * caller doesn't mutate it. When the rotation is needed we have to allocate.\n */\nfunction maybeRotateFloat32Vertices(\n\tvertices: Float32Array,\n\tapplyCoordinateTransform: boolean\n): Float32Array {\n\tif (!applyCoordinateTransform) return vertices;\n\n\tconst out = new Float32Array(vertices.length);\n\tfor (let i = 0; i < vertices.length; i += 3) {\n\t\tconst x = vertices[i]!;\n\t\tconst y = vertices[i + 1]!;\n\t\tconst z = vertices[i + 2]!;\n\t\tout[i] = x;\n\t\tout[i + 1] = z;\n\t\tout[i + 2] = -y;\n\t}\n\treturn out;\n}\n\n// ============================================================================\n// MATERIAL CONSTRUCTION\n// ============================================================================\n\nfunction createMaterial(matData: SerializableMaterial): THREE.MeshPhysicalMaterial {\n\tconst color = parseColor(matData.color);\n\n\treturn new THREE.MeshPhysicalMaterial({\n\t\tcolor,\n\t\tmetalness: matData.metalness,\n\t\troughness: matData.roughness,\n\t\topacity: matData.opacity,\n\t\ttransparent: matData.transparent,\n\t\tside: THREE.DoubleSide,\n\t\t// Reduced polygon offset to minimize artifacts\n\t\t// Only use minimal offset to prevent z-fighting on coplanar faces\n\t\tpolygonOffset: true,\n\t\tpolygonOffsetFactor: 0.5,\n\t\tpolygonOffsetUnits: 0.5,\n\t\t// Improve depth rendering\n\t\tdepthWrite: true,\n\t\tdepthTest: true\n\t});\n}\n\n// ============================================================================\n// MESH CONSTRUCTION\n// ============================================================================\n\n/**\n * Creates a merged mesh from multiple meshes sharing the same material.\n *\n * Indices in the parser output already reference offsets into the combined vertex array (the C#\n * pipeline rebases per-mesh local indices into combined-array indices when assembling the batch).\n * For merged meshes we copy the relevant slices into a fresh contiguous buffer and shift indices\n * to match the new layout.\n */\nfunction createMergedMesh(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh {\n\tlet totalVertexCount = 0;\n\tlet totalIndexCount = 0;\n\tfor (const meshMeta of group.meshes) {\n\t\ttotalVertexCount += meshMeta.vertexCount;\n\t\ttotalIndexCount += meshMeta.indexCount;\n\t}\n\n\tconst mergedVertices = new Float32Array(totalVertexCount * 3);\n\tconst mergedIndices = new Uint32Array(totalIndexCount);\n\n\tlet vertexWriteCursor = 0;\n\tlet indexWriteCursor = 0;\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\t\tmergedVertices.set(\n\t\t\tallVertices.subarray(componentStart, componentStart + componentLen),\n\t\t\tvertexWriteCursor * 3\n\t\t);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst indexShift = vertexWriteCursor - meshMeta.vertexStart;\n\t\tif (indexShift === 0) {\n\t\t\tmergedIndices.set(indicesSlice, indexWriteCursor);\n\t\t} else {\n\t\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\t\tmergedIndices[indexWriteCursor + i] = indicesSlice[i]! + indexShift;\n\t\t\t}\n\t\t}\n\n\t\tvertexWriteCursor += meshMeta.vertexCount;\n\t\tindexWriteCursor += meshMeta.indexCount;\n\t}\n\n\tconst geometry = new THREE.BufferGeometry();\n\tgeometry.setAttribute('position', new THREE.BufferAttribute(mergedVertices, 3));\n\tgeometry.setIndex(new THREE.BufferAttribute(mergedIndices, 1));\n\tgeometry.computeVertexNormals();\n\n\tconst threeMesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\tconst firstMesh = group.meshes[0];\n\tconst meshNames = group.meshes.map((m) => m.name).filter((name) => name && name.length > 0);\n\tthreeMesh.name = meshNames.length > 0 ? meshNames[0]! : `merged_material_${group.materialId}`;\n\tthreeMesh.castShadow = true;\n\tthreeMesh.receiveShadow = true;\n\n\tthreeMesh.userData = {\n\t\tname: threeMesh.name,\n\t\tlayer: firstMesh?.layer ?? '',\n\t\toriginalIndex: firstMesh?.originalIndex ?? 0,\n\t\tmetadata: firstMesh?.metadata ?? {},\n\t\tmergedFrom: group.meshes.slice(1).map((m) => ({\n\t\t\tname: m.name,\n\t\t\tlayer: m.layer,\n\t\t\toriginalIndex: m.originalIndex\n\t\t}))\n\t};\n\n\treturn threeMesh;\n}\n\n/**\n * Creates individual meshes from a material group. Each mesh's indices are rebased so they\n * address its own local vertex slice starting from 0.\n */\nfunction createIndividualMeshes(\n\tgroup: MaterialGroup,\n\tallVertices: Float32Array,\n\tallIndices: Uint32Array,\n\tmaterials: THREE.Material[]\n): THREE.Mesh[] {\n\tconst meshes: THREE.Mesh[] = [];\n\n\tfor (const meshMeta of group.meshes) {\n\t\tconst componentStart = meshMeta.vertexStart * 3;\n\t\tconst componentLen = meshMeta.vertexCount * 3;\n\n\t\t// `subarray` returns a view; copy via `slice` so the BufferAttribute owns its memory and\n\t\t// downstream code (dispose/reuse) can't surprise us by sharing the parser's buffer.\n\t\tconst vertices = allVertices.slice(componentStart, componentStart + componentLen);\n\n\t\tconst indicesSlice = allIndices.subarray(\n\t\t\tmeshMeta.indexStart,\n\t\t\tmeshMeta.indexStart + meshMeta.indexCount\n\t\t);\n\t\tconst rebasedIndices = new Uint32Array(indicesSlice.length);\n\t\tconst baseIndex = meshMeta.vertexStart;\n\t\tfor (let i = 0; i < indicesSlice.length; i++) {\n\t\t\trebasedIndices[i] = indicesSlice[i]! - baseIndex;\n\t\t}\n\n\t\tconst geometry = new THREE.BufferGeometry();\n\t\tgeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));\n\t\tgeometry.setIndex(new THREE.BufferAttribute(rebasedIndices, 1));\n\t\tgeometry.computeVertexNormals();\n\n\t\tconst mesh = new THREE.Mesh(geometry, materials[group.materialId]);\n\t\tmesh.name = meshMeta.name;\n\t\tmesh.userData = {\n\t\t\tname: meshMeta.name,\n\t\t\tlayer: meshMeta.layer ?? '',\n\t\t\toriginalIndex: meshMeta.originalIndex,\n\t\t\tmetadata: meshMeta.metadata ?? {}\n\t\t};\n\t\tmesh.castShadow = true;\n\t\tmesh.receiveShadow = true;\n\n\t\tmeshes.push(mesh);\n\t}\n\n\treturn meshes;\n}\n\n// ============================================================================\n// DEBUG HELPERS\n// ============================================================================\n\nfunction approximateBase64DecodedBytes(base64: string): number {\n\treturn Math.floor((base64.length * 3) / 4);\n}\n","import { decodeBase64ToBinary } from '@/core/utils/encoding';\nimport { RhinoComputeError, ErrorCodes } from '@/core/errors';\n\nimport type { MaterialGroup, SerializableMaterial } from './types';\n\n// ============================================================================\n// WIRE FORMAT CONSTANTS\n// ============================================================================\n\n/** \"SLVA\" little-endian. */\nexport const BINARY_MESH_MAGIC = 0x41564c53;\n/** Bumped on any wire-layout change. */\nexport const BINARY_MESH_VERSION = 1;\n/** Bit 0 of the geometry flags word: 0 = int16 quantized, 1 = float32 raw. */\nexport const FLAG_FLOAT32 = 0x1;\n\nconst HEADER_PREAMBLE_BYTES = 4 /* magic */ + 4 /* version */ + 4; /* metadataLen */\nconst GEOMETRY_HEADER_BYTES =\n\t4 /* flags */ + 24 /* origin (3 x f64) */ + 24 /* scale (3 x f64) */ + 4; /* vertexCount */\n\n// ============================================================================\n// PARSED TYPES\n// ============================================================================\n\n/**\n * Metadata JSON embedded inside the binary blob.\n *\n * This is the same shape as a `MeshBatch` minus the `compressedData` field (the blob is opaque to\n * its own metadata header). Kept separate from the public `MeshBatch` type because the blob's\n * metadata never carries `compressedData` itself — it would be circular.\n */\nexport interface BinaryMeshMetadata {\n\tmaterials: SerializableMaterial[];\n\tgroups: MaterialGroup[];\n\tsourceComponentId?: string;\n}\n\n/**\n * Result of parsing a binary mesh blob.\n *\n * `vertices` and `indices` are typed-array views over the original `ArrayBuffer` — zero copies.\n * The consumer is responsible for not mutating the underlying buffer if it cares about safety,\n * or for calling `.slice()` to detach.\n */\nexport interface ParsedBinaryMeshBatch {\n\tmetadata: BinaryMeshMetadata;\n\tflags: number;\n\tvertices: Int16Array | Float32Array;\n\tindices: Uint32Array;\n\torigin: [number, number, number];\n\tscale: [number, number, number];\n}\n\n// ============================================================================\n// PARSER\n// ============================================================================\n\n/**\n * Parses a binary mesh batch blob in the SLVA wire format.\n *\n * The blob layout is:\n * ```\n * [4] magic = \"SLVA\" (0x53 0x4C 0x56 0x41)\n * [4] version = uint32 (currently 1)\n * [4] metadataLen = uint32 byte length of UTF-8 metadata JSON\n * [N] metadata = UTF-8 JSON (materials, groups, sourceComponentId, ...)\n * [4] flags = uint32 (bit 0: 0 = int16 quantized, 1 = float32 raw)\n * [24] origin = 3 x float64\n * [24] scale = 3 x float64 (step per int16 unit; identity for float32)\n * [4] vertexCount = uint32 number of vertices (positions = vertexCount * 3 components)\n * [V] vertices = int16[vertexCount*3] OR float32[vertexCount*3]\n * [4] indexCount = uint32 number of indices\n * [I] indices = uint32[indexCount]\n * ```\n *\n * For int16 vertices: world position = `origin + (q + 32767) * scale`. This matches Three.js\n * `BufferAttribute(arr, 3, true)` (`normalized: true`) semantics when the per-mesh transform\n * encodes `origin + scale`.\n *\n * For float32: `origin = (0, 0, 0)`, `scale = (1, 1, 1)`, vertices are raw world positions.\n *\n * @param input - The blob, as either an `ArrayBuffer`/`Uint8Array` (binary transport) or a\n * base64-encoded string (today's JSON-envelope transport).\n * @returns Decoded metadata plus typed-array views into the geometry payload.\n * @throws {RhinoComputeError} On invalid magic, unknown version, or truncated input.\n */\nexport function parseBinaryMeshBatch(\n\tinput: ArrayBuffer | Uint8Array | string\n): ParsedBinaryMeshBatch {\n\tconst bytes = toUint8Array(input);\n\tconst view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n\n\tif (bytes.byteLength < HEADER_PREAMBLE_BYTES) {\n\t\tthrow fail('Blob too small to contain SLVA header.', {\n\t\t\texpectedBytes: HEADER_PREAMBLE_BYTES,\n\t\t\tavailableBytes: bytes.byteLength\n\t\t});\n\t}\n\n\tlet offset = 0;\n\n\tconst magic = view.getUint32(offset, true);\n\toffset += 4;\n\tif (magic !== BINARY_MESH_MAGIC) {\n\t\tthrow fail(`Invalid SLVA magic: 0x${magic.toString(16)}`, {\n\t\t\texpectedMagic: `0x${BINARY_MESH_MAGIC.toString(16)}`,\n\t\t\tactualMagic: `0x${magic.toString(16)}`\n\t\t});\n\t}\n\n\tconst version = view.getUint32(offset, true);\n\toffset += 4;\n\tif (version !== BINARY_MESH_VERSION) {\n\t\tthrow fail(`Unsupported SLVA version: ${version}`, {\n\t\t\texpectedVersion: BINARY_MESH_VERSION,\n\t\t\tactualVersion: version\n\t\t});\n\t}\n\n\tconst metadataLen = view.getUint32(offset, true);\n\toffset += 4;\n\tif (offset + metadataLen > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read metadata JSON.', {\n\t\t\texpectedBytes: metadataLen,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst metadataBytes = bytes.subarray(offset, offset + metadataLen);\n\toffset += metadataLen;\n\n\tlet metadata: BinaryMeshMetadata;\n\ttry {\n\t\tmetadata = JSON.parse(decodeUtf8(metadataBytes)) as BinaryMeshMetadata;\n\t} catch (error) {\n\t\tthrow fail(\n\t\t\t`Failed to parse metadata JSON: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t{ metadataLen }\n\t\t);\n\t}\n\n\tif (offset + GEOMETRY_HEADER_BYTES > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read geometry header.', {\n\t\t\texpectedBytes: GEOMETRY_HEADER_BYTES,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\n\tconst flags = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst originX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst originZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst scaleX = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleY = view.getFloat64(offset, true);\n\toffset += 8;\n\tconst scaleZ = view.getFloat64(offset, true);\n\toffset += 8;\n\n\tconst vertexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst useFloat32 = (flags & FLAG_FLOAT32) !== 0;\n\tconst componentCount = vertexCount * 3;\n\tconst bytesPerComponent = useFloat32 ? 4 : 2;\n\tconst verticesByteLength = componentCount * bytesPerComponent;\n\n\tif (offset + verticesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read vertices.', {\n\t\t\texpectedBytes: verticesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tuseFloat32,\n\t\t\tvertexCount\n\t\t});\n\t}\n\n\t// Typed-array views require alignment to the element size. The header lays out the geometry\n\t// block such that the vertex byte offset is always 4-aligned (preamble 12 + metadataLen + 4 +\n\t// 48 + 4). float32 needs 4-byte alignment (satisfied), int16 needs 2-byte alignment\n\t// (satisfied). We can take a zero-copy view as long as `bytes.byteOffset + offset` agrees with\n\t// that alignment in the underlying buffer — a wrapper Uint8Array could violate it. Fall back\n\t// to a fresh copy if so.\n\tconst absoluteOffset = bytes.byteOffset + offset;\n\tconst verticesView = useFloat32\n\t\t? readFloat32Vertices(bytes.buffer, absoluteOffset, componentCount)\n\t\t: readInt16Vertices(bytes.buffer, absoluteOffset, componentCount);\n\toffset += verticesByteLength;\n\n\tif (offset + 4 > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read index count.', {\n\t\t\texpectedBytes: 4,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset\n\t\t});\n\t}\n\tconst indexCount = view.getUint32(offset, true);\n\toffset += 4;\n\n\tconst indicesByteLength = indexCount * 4;\n\tif (offset + indicesByteLength > bytes.byteLength) {\n\t\tthrow fail('Insufficient data to read indices.', {\n\t\t\texpectedBytes: indicesByteLength,\n\t\t\tavailableBytes: bytes.byteLength - offset,\n\t\t\toffset,\n\t\t\tindexCount\n\t\t});\n\t}\n\n\tconst indicesView = readUint32Indices(bytes.buffer, bytes.byteOffset + offset, indexCount);\n\n\treturn {\n\t\tmetadata,\n\t\tflags,\n\t\tvertices: verticesView,\n\t\tindices: indicesView,\n\t\torigin: [originX, originY, originZ],\n\t\tscale: [scaleX, scaleY, scaleZ]\n\t};\n}\n\n// ============================================================================\n// HELPERS\n// ============================================================================\n\nfunction toUint8Array(input: ArrayBuffer | Uint8Array | string): Uint8Array {\n\tif (typeof input === 'string') {\n\t\treturn decodeBase64ToBinary(input);\n\t}\n\tif (input instanceof Uint8Array) {\n\t\treturn input;\n\t}\n\treturn new Uint8Array(input);\n}\n\nfunction decodeUtf8(bytes: Uint8Array): string {\n\tif (typeof TextDecoder !== 'undefined') {\n\t\treturn new TextDecoder('utf-8').decode(bytes);\n\t}\n\t// Node fallback (Buffer is utf-8 by default).\n\tif (\n\t\ttypeof (globalThis as { Buffer?: { from(b: Uint8Array): { toString(enc: string): string } } })\n\t\t\t.Buffer !== 'undefined'\n\t) {\n\t\treturn (\n\t\t\tglobalThis as { Buffer: { from(b: Uint8Array): { toString(enc: string): string } } }\n\t\t).Buffer.from(bytes).toString('utf-8');\n\t}\n\tthrow new RhinoComputeError(\n\t\t'No UTF-8 decoder available in this environment.',\n\t\tErrorCodes.INVALID_STATE\n\t);\n}\n\nfunction readInt16Vertices(buffer: ArrayBufferLike, byteOffset: number, count: number): Int16Array {\n\tif (count === 0) return new Int16Array(0);\n\tif (byteOffset % 2 === 0) {\n\t\treturn new Int16Array(buffer, byteOffset, count);\n\t}\n\t// Misaligned (rare — would require a wrapper Uint8Array with odd byteOffset).\n\tconst copy = new Uint8Array(count * 2);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 2));\n\treturn new Int16Array(copy.buffer);\n}\n\nfunction readFloat32Vertices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Float32Array {\n\tif (count === 0) return new Float32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Float32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Float32Array(copy.buffer);\n}\n\nfunction readUint32Indices(\n\tbuffer: ArrayBufferLike,\n\tbyteOffset: number,\n\tcount: number\n): Uint32Array {\n\tif (count === 0) return new Uint32Array(0);\n\tif (byteOffset % 4 === 0) {\n\t\treturn new Uint32Array(buffer, byteOffset, count);\n\t}\n\tconst copy = new Uint8Array(count * 4);\n\tcopy.set(new Uint8Array(buffer, byteOffset, count * 4));\n\treturn new Uint32Array(copy.buffer);\n}\n\nfunction fail(message: string, context: Record<string, unknown>): RhinoComputeError {\n\treturn new RhinoComputeError(message, ErrorCodes.VALIDATION_ERROR, { context });\n}\n","import * as THREE from 'three';\n\nimport { applyOffset, computeCombinedBoundingBox } from '../threejs/three-helpers.js';\nimport { getLogger } from '@/core';\n\nimport { parseMeshBatch } from './batch-parser';\n\nimport type { DataItem, GrasshopperComputeResponse } from '@/features/grasshopper/types';\nimport type { MeshExtractionOptions, MeshBatchParsingOptions } from './types';\n\n// Constants\nexport const SCALE_FACTORS: Record<string, number> = {\n\tMillimeters: 1 / 1000,\n\tCentimeters: 1 / 100,\n\tMeters: 1,\n\tInches: 1 / 39.37,\n\tFeet: 1 / 3.28084\n};\n\nconst DISPLAY_COMPONENT_TYPE = 'Display';\n\n/**\n * Extracts and processes display meshes from a ComputePointerResponse using the Grasshopper WebDisplay component.\n *\n * This is the primary entry point for extracting mesh geometry from Grasshopper compute responses.\n * It handles all aspects of mesh processing: decompression, coordinate transformation, scaling, and positioning.\n *\n * **Note:** Mesh decompression happens asynchronously in a Web Worker to prevent UI blocking.\n *\n * @param data - The ComputePointerResponse containing Grasshopper output trees.\n * @param options - Configuration for mesh extraction and parsing behavior. All options are optional with sensible defaults.\n * @returns Promise resolving to array of THREE.Mesh objects (may be empty).\n * @throws Rethrows unexpected errors after attempting to dispose any created meshes.\n *\n * @remarks\n * - Only works with the WebDisplay component of GHHeadless.\n * - Requires changes to Rhino.Compute (see https://github.com/TheVessen/compute.rhino3d).\n * - Provides a performant way to display mesh data in Three.js.\n * - Decompression is performed in a Web Worker for non-blocking UI updates.\n * - Supports mesh metadata (names, user data) if provided in the compute response.\n *\n * @internal Internal helper: high-level extraction remains public via visualization module, but this\n * function is considered internal implementation detail for mesh extraction.\n *\n * @example\n * ```ts\n * // Simple usage with defaults (all processing enabled)\n * const meshes = await getThreeMeshesFromComputeResponse(response);\n *\n * // With debugging enabled\n * const meshes = await getThreeMeshesFromComputeResponse(response, { debug: true });\n *\n * // With advanced options\n * const meshes = await getThreeMeshesFromComputeResponse(response, {\n * debug: true,\n * allowScaling: true,\n * allowAutoPosition: false,\n * parsing: {\n * mergeByMaterial: false,\n * applyTransforms: true,\n * debug: true,\n * },\n * });\n * ```\n */\nexport async function getThreeMeshesFromComputeResponse(\n\tdata: GrasshopperComputeResponse,\n\toptions?: MeshExtractionOptions\n): Promise<THREE.Mesh[]> {\n\tconst startTime = performance.now();\n\tconst meshes: THREE.Mesh[] = [];\n\n\tconst {\n\t\tallowScaling = true,\n\t\tallowAutoPosition = true,\n\t\tdebug = false,\n\t\tparsing: parsingOptions = {}\n\t} = options ?? {};\n\n\ttry {\n\t\tconst scaleFactor = allowScaling ? getScaleFactor(data.modelunits) : 1;\n\t\tawait extractMeshesFromData(data, meshes, scaleFactor, parsingOptions, debug);\n\n\t\tif (allowAutoPosition) {\n\t\t\tapplyGroundOffset(meshes);\n\t\t}\n\n\t\treturn meshes;\n\t} catch (error) {\n\t\thandleError(error, meshes);\n\t\tthrow error;\n\t} finally {\n\t\tif (debug) {\n\t\t\tlogProcessingTime(startTime);\n\t\t}\n\t}\n}\n\n/**\n * Gets the scale factor for the given unit type.\n */\nfunction getScaleFactor(modelUnits: string): number {\n\treturn SCALE_FACTORS[modelUnits] ?? 1;\n}\n\n/**\n * Extracts meshes from compute response data.\n */\nasync function extractMeshesFromData(\n\tdata: GrasshopperComputeResponse,\n\tmeshes: THREE.Mesh[],\n\tscaleFactor: number,\n\tparsingOptions: MeshBatchParsingOptions,\n\tdebug: boolean\n): Promise<void> {\n\tfor (const value of data.values) {\n\t\tconst innerTree = value.InnerTree as { [key: string]: DataItem[] };\n\n\t\tfor (const path in innerTree) {\n\t\t\tconst branch = innerTree[path];\n\t\t\tif (!branch) continue;\n\n\t\t\tawait processDataBranch(branch, meshes, scaleFactor, parsingOptions, debug);\n\t\t}\n\t}\n}\n\n/**\n * Processes a single data branch to extract MeshBatch display meshes.\n */\nasync function processDataBranch(\n\tbranch: DataItem[],\n\tmeshes: THREE.Mesh[],\n\tscaleFactor: number,\n\tparsingOptions: MeshBatchParsingOptions,\n\tdebug: boolean\n): Promise<void> {\n\tfor (const item of branch) {\n\t\tif (item.type.includes(DISPLAY_COMPONENT_TYPE)) {\n\t\t\tconst mergedParsingOptions = {\n\t\t\t\tmergeByMaterial: true,\n\t\t\t\tapplyTransforms: true,\n\t\t\t\tdebug: false,\n\t\t\t\t...parsingOptions\n\t\t\t};\n\n\t\t\tconst batchMeshes = await parseMeshBatch(item.data, mergedParsingOptions);\n\n\t\t\tif (scaleFactor !== 1) {\n\t\t\t\tfor (const mesh of batchMeshes) {\n\t\t\t\t\tmesh.scale.set(scaleFactor, scaleFactor, scaleFactor);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmeshes.push(...batchMeshes);\n\n\t\t\tif (debug) {\n\t\t\t\tgetLogger().debug(`Extracted ${batchMeshes.length} meshes from batch`);\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Applies vertical offset to position meshes on the Z=0 plane.\n */\nfunction applyGroundOffset(meshes: THREE.Mesh[]): void {\n\tif (meshes.length === 0) return;\n\n\tconst combinedBoundingBox = computeCombinedBoundingBox(meshes);\n\tconst offsetY = combinedBoundingBox.min.y;\n\tapplyOffset(meshes, offsetY);\n}\n\n/**\n * Handles errors by disposing created meshes and logging.\n */\nfunction handleError(error: unknown, meshes: THREE.Mesh[]): void {\n\tgetLogger().error('An unexpected error occurred:', error);\n\tdisposeMeshes(meshes);\n}\n\n/**\n * Disposes of all meshes and their associated resources.\n */\nfunction disposeMeshes(meshes: THREE.Mesh[]): void {\n\tfor (const mesh of meshes) {\n\t\tif (mesh.geometry) {\n\t\t\tmesh.geometry.dispose();\n\t\t}\n\n\t\tif (mesh.material) {\n\t\t\tif (Array.isArray(mesh.material)) {\n\t\t\t\tmesh.material.forEach((material) => material.dispose());\n\t\t\t} else {\n\t\t\t\tmesh.material.dispose();\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Logs the processing time for mesh extraction.\n */\nfunction logProcessingTime(startTime: number): void {\n\tconst elapsed = performance.now() - startTime;\n\tgetLogger().info('Time to process meshes:', `${elapsed.toFixed(2)}ms`);\n}\n"],"mappings":"yKAAA,UAAYA,MAAW,QCUhB,IAAMC,EAAoB,WAEpBC,EAAsB,EAEtBC,EAAe,EAEtBC,EAAwB,GACxBC,EACL,GAoEM,SAASC,EACfC,EACwB,CACxB,IAAMC,EAAQC,EAAaF,CAAK,EAC1BG,EAAO,IAAI,SAASF,EAAM,OAAQA,EAAM,WAAYA,EAAM,UAAU,EAE1E,GAAIA,EAAM,WAAaJ,EACtB,MAAMO,EAAK,yCAA0C,CACpD,cAAeP,EACf,eAAgBI,EAAM,UACvB,CAAC,EAGF,IAAII,EAAS,EAEPC,EAAQH,EAAK,UAAUE,EAAQ,EAAI,EAEzC,GADAA,GAAU,EACNC,IAAUZ,EACb,MAAMU,EAAK,yBAAyBE,EAAM,SAAS,EAAE,CAAC,GAAI,CACzD,cAAe,KAAKZ,EAAkB,SAAS,EAAE,CAAC,GAClD,YAAa,KAAKY,EAAM,SAAS,EAAE,CAAC,EACrC,CAAC,EAGF,IAAMC,EAAUJ,EAAK,UAAUE,EAAQ,EAAI,EAE3C,GADAA,GAAU,EACNE,IAAYZ,EACf,MAAMS,EAAK,6BAA6BG,CAAO,GAAI,CAClD,gBAAiBZ,EACjB,cAAeY,CAChB,CAAC,EAGF,IAAMC,EAAcL,EAAK,UAAUE,EAAQ,EAAI,EAE/C,GADAA,GAAU,EACNA,EAASG,EAAcP,EAAM,WAChC,MAAMG,EAAK,2CAA4C,CACtD,cAAeI,EACf,eAAgBP,EAAM,WAAaI,EACnC,OAAAA,CACD,CAAC,EAGF,IAAMI,EAAgBR,EAAM,SAASI,EAAQA,EAASG,CAAW,EACjEH,GAAUG,EAEV,IAAIE,EACJ,GAAI,CACHA,EAAW,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CAAoB,EAExE,GAAId,IAAgB,EACnB,QAAWiB,KAAQF,EAClBE,EAAK,MAAM,IAAIjB,EAAaA,EAAaA,CAAW,EAItDL,EAAO,KAAK,GAAGoB,CAAW,EAEtBjB,GACHoB,EAAU,EAAE,MAAM,aAAaH,EAAY,MAAM,oBAAoB,CAEvE,CAEF,CAKA,SAASZ,GAAkBR,EAA4B,CACtD,GAAIA,EAAO,SAAW,EAAG,OAGzB,IAAMwB,EADsBC,EAA2BzB,CAAM,EACzB,IAAI,EACxC0B,EAAY1B,EAAQwB,CAAO,CAC5B,CAKA,SAASd,GAAYD,EAAgBT,EAA4B,CAChEuB,EAAU,EAAE,MAAM,gCAAiCd,CAAK,EACxDkB,GAAc3B,CAAM,CACrB,CAKA,SAAS2B,GAAc3B,EAA4B,CAClD,QAAWsB,KAAQtB,EACdsB,EAAK,UACRA,EAAK,SAAS,QAAQ,EAGnBA,EAAK,WACJ,MAAM,QAAQA,EAAK,QAAQ,EAC9BA,EAAK,SAAS,QAASM,GAAaA,EAAS,QAAQ,CAAC,EAEtDN,EAAK,SAAS,QAAQ,EAI1B,CAKA,SAASX,GAAkBZ,EAAyB,CACnD,IAAM8B,EAAU,YAAY,IAAI,EAAI9B,EACpCwB,EAAU,EAAE,KAAK,0BAA2B,GAAGM,EAAQ,QAAQ,CAAC,CAAC,IAAI,CACtE","names":["THREE","BINARY_MESH_MAGIC","BINARY_MESH_VERSION","FLAG_FLOAT32","HEADER_PREAMBLE_BYTES","GEOMETRY_HEADER_BYTES","parseBinaryMeshBatch","input","bytes","toUint8Array","view","fail","offset","magic","version","metadataLen","metadataBytes","metadata","decodeUtf8","error","flags","originX","originY","originZ","scaleX","scaleY","scaleZ","vertexCount","useFloat32","componentCount","verticesByteLength","absoluteOffset","verticesView","readFloat32Vertices","readInt16Vertices","indexCount","indicesByteLength","indicesView","readUint32Indices","decodeBase64ToBinary","RhinoComputeError","ErrorCodes","buffer","byteOffset","count","copy","message","context","parseMeshBatch","batchJson","options","mergeByMaterial","applyTransforms","debug","perfStart","parseTime","parseStart","batch","parseMeshBatchObject","error","getLogger","scaleFactor","decodeTime","meshCreateTime","decodeStart","parsed","parseBinaryMeshBatch","materialsSrc","groups","sourceComponentId","isFloat32","FLAG_FLOAT32","worldVertices","maybeRotateFloat32Vertices","dequantizeInt16","blobBytes","approximateBase64DecodedBytes","wireBytes","meshCreateStart","materials","createMaterial","meshes","group","mergedMesh","createMergedMesh","individualMeshes","createIndividualMeshes","mesh","totalTime","q","origin","scale","applyCoordinateTransform","out","ox","oy","oz","sx","sy","sz","i","wx","wy","wz","vertices","x","y","z","matData","color","parseColor","allVertices","allIndices","totalVertexCount","totalIndexCount","meshMeta","mergedVertices","mergedIndices","vertexWriteCursor","indexWriteCursor","componentStart","componentLen","indicesSlice","indexShift","geometry","threeMesh","firstMesh","meshNames","m","name","rebasedIndices","baseIndex","base64","SCALE_FACTORS","DISPLAY_COMPONENT_TYPE","getThreeMeshesFromComputeResponse","data","options","startTime","meshes","allowScaling","allowAutoPosition","debug","parsingOptions","scaleFactor","getScaleFactor","extractMeshesFromData","applyGroundOffset","error","handleError","logProcessingTime","modelUnits","value","innerTree","path","branch","processDataBranch","item","mergedParsingOptions","batchMeshes","parseMeshBatch","mesh","getLogger","offsetY","computeCombinedBoundingBox","applyOffset","disposeMeshes","material","elapsed"]}
|
|
File without changes
|