@deepblend/dsh-blender-contracts 0.9.0 → 0.10.0

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.
@@ -1,7 +1,10 @@
1
1
  import { BlenderError, BlenderErrorCode } from './index.js'
2
2
 
3
3
  export const GEOMETRY_INSPECTION_VERSION = 'deepblend.geometry-inspection/v1'
4
+ export const SURFACE_INSPECTION_VERSION = 'deepblend.geometry-inspection/v2'
4
5
  export const GEOMETRY_INSPECTION_LIMITS = Object.freeze({ entities: 8, checkpointBytes: 1024 ** 3, reportBytes: 64 * 1024 })
6
+ export const SURFACE_INSPECTION_LIMITS = Object.freeze({ samples: 8, vertices: 250_000, polygons: 250_000,
7
+ searchDistanceMm: 500, chordDistanceMm: 100, separationDistanceMm: 100, epsilonMm: .0001 })
5
8
 
6
9
  function refuse(message) {
7
10
  throw new BlenderError(BlenderErrorCode.SCRIPT_ERROR, `Geometry inspection: ${message}`)
@@ -13,6 +16,81 @@ export function validateGeometrySelection(request) {
13
16
  new Set(request.entityIds).size !== request.entityIds.length) refuse('select 1–8 distinct non-empty entity IDs.')
14
17
  if (!Number.isSafeInteger(request.frame) || request.frame < 0 || request.frame > 1_000_000) refuse('an explicit integer frame from 0 to 1000000 is required.')
15
18
  if (typeof request.checkpointSha256 !== 'string' || !/^[a-f0-9]{64}$/.test(request.checkpointSha256)) refuse('the expected checkpoint SHA256 is required.')
19
+ if (request.surfaceSamples !== undefined) validateSurfaceSelection(request)
20
+ }
21
+
22
+ const vector3 = v => Array.isArray(v) && v.length === 3 && v.every(Number.isFinite)
23
+ const length = v => Math.hypot(...v)
24
+ const subtract = (a, b) => a.map((v, i) => v - b[i])
25
+ const near = (a, b, tolerance = 1e-6) => length(subtract(a, b)) <= tolerance
26
+ const only = (value, keys) => value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).every(k => keys.includes(k))
27
+ const unit = v => v.map(x => x / length(v))
28
+ const unavailable = ['not-mesh', 'viewport-disabled', 'empty-or-invalid-geometry', 'geometry-budget-exceeded', 'unsupported-topology-or-winding']
29
+ const sourceFailures = [...unavailable, 'no-entry', 'entry-not-inward', 'no-opposite-surface', 'exit-not-outward', 'below-resolution']
30
+ const targetFailures = [...unavailable, 'no-target-entry', 'target-not-inward', 'source-surface-before-target', 'below-resolution']
31
+
32
+ export function validateSurfaceSelection(request) {
33
+ const samples = request?.surfaceSamples
34
+ if (!Array.isArray(request?.entityIds)) refuse('surface samples require an explicit entity selection.')
35
+ if (!Array.isArray(samples) || samples.length < 1 || samples.length > SURFACE_INSPECTION_LIMITS.samples ||
36
+ new Set(samples.map(s => s?.sampleId)).size !== samples.length) refuse('select 1–8 distinct surface samples.')
37
+ for (const sample of samples) {
38
+ if (!only(sample, ['sampleId', 'entityId', 'targetEntityId', 'originMeters', 'direction', 'searchDistanceMm']) ||
39
+ typeof sample.sampleId !== 'string' || !/^[a-zA-Z0-9_-]{1,64}$/.test(sample.sampleId) ||
40
+ !request.entityIds.includes(sample.entityId) ||
41
+ (sample.targetEntityId !== undefined && (!request.entityIds.includes(sample.targetEntityId) || sample.targetEntityId === sample.entityId)) ||
42
+ !vector3(sample.originMeters) || sample.originMeters.some(x => Math.abs(x) > 1000) ||
43
+ !vector3(sample.direction) || length(sample.direction) < 1e-12 || length(sample.direction) > 1e6 ||
44
+ !Number.isFinite(sample.searchDistanceMm) || sample.searchDistanceMm < .01 || sample.searchDistanceMm > SURFACE_INSPECTION_LIMITS.searchDistanceMm) {
45
+ refuse('surface samples require selected source/target IDs, finite world seeds/directions and explicit searchDistanceMm from 0.01 to 500; arbitrary fields are refused.')
46
+ }
47
+ }
48
+ }
49
+
50
+ export function verifySurfaceSamples(samples, request, objects) {
51
+ validateSurfaceSelection(request)
52
+ if (!Array.isArray(samples) || samples.length !== request.surfaceSamples.length) refuse('surface sample count differs from the request.')
53
+ const point = v => vector3(v) && v.every(x => Math.abs(x) <= 1001)
54
+ const distance = (a, b, measured, maximum) => Number.isFinite(measured) && measured > 2 * SURFACE_INSPECTION_LIMITS.epsilonMm &&
55
+ measured <= maximum + .001 && Math.abs(length(subtract(a, b)) * 1000 - measured) <= .001
56
+ const along = (a, b, direction, maximum) => {
57
+ const delta = subtract(b, a), projection = delta.reduce((sum, v, i) => sum + v * direction[i], 0)
58
+ return projection >= -1e-6 && projection <= maximum / 1000 + 1e-6 && near(delta, direction.map(x => x * projection))
59
+ }
60
+ for (const [i, result] of samples.entries()) {
61
+ const q = request.surfaceSamples[i]
62
+ if (!result || result.sampleId !== q.sampleId || result.entityId !== q.entityId || result.targetEntityId !== q.targetEntityId ||
63
+ !vector3(result.originMeters) || !near(result.originMeters, q.originMeters, 0) || !vector3(result.direction) ||
64
+ !near(result.direction, unit(q.direction), 1e-6) || result.searchDistanceMm !== q.searchDistanceMm) refuse('surface identity, seed or search distance differs from the request.')
65
+ const base = ['sampleId', 'entityId', 'targetEntityId', 'originMeters', 'direction', 'searchDistanceMm', 'status']
66
+ if (result.status !== 'sampled') {
67
+ const object = objects.find(o => o.entityId === q.entityId)
68
+ if (!sourceFailures.includes(result.status) || !only(result, base) ||
69
+ (object?.available === false && result.status !== object.unavailableReason)) refuse('unavailable surface samples must not contain measurements or contradict object availability.')
70
+ continue
71
+ }
72
+ if (!only(result, [...base, 'entryMeters', 'exitMeters', 'inwardUnitNormal', 'normalSurfaceChordMm', 'gap']) ||
73
+ !point(result.entryMeters) || !point(result.exitMeters) || !vector3(result.inwardUnitNormal) ||
74
+ Math.abs(length(result.inwardUnitNormal) - 1) > 1e-6 ||
75
+ result.inwardUnitNormal.reduce((sum, v, axis) => sum + v * result.direction[axis], 0) <= 1e-6 ||
76
+ !along(q.originMeters, result.entryMeters, result.direction, q.searchDistanceMm) ||
77
+ !along(result.entryMeters, result.exitMeters, result.inwardUnitNormal, SURFACE_INSPECTION_LIMITS.chordDistanceMm) ||
78
+ !distance(result.entryMeters, result.exitMeters, result.normalSurfaceChordMm, SURFACE_INSPECTION_LIMITS.chordDistanceMm) ||
79
+ objects.find(o => o.entityId === q.entityId)?.available !== true) refuse('surface points, normal, distance or source availability disagree.')
80
+ if (q.targetEntityId === undefined) {
81
+ if (Object.hasOwn(result, 'gap')) refuse('an unrequested target separation is not accepted.')
82
+ } else {
83
+ const gap = result.gap
84
+ if (gap?.status === 'sampled') {
85
+ if (!only(gap, ['status', 'targetEntryMeters', 'normalRaySeparationMm']) || !point(gap.targetEntryMeters) ||
86
+ !along(result.exitMeters, gap.targetEntryMeters, result.inwardUnitNormal, SURFACE_INSPECTION_LIMITS.separationDistanceMm) ||
87
+ !distance(result.exitMeters, gap.targetEntryMeters, gap.normalRaySeparationMm, SURFACE_INSPECTION_LIMITS.separationDistanceMm) ||
88
+ objects.find(o => o.entityId === q.targetEntityId)?.available !== true) refuse('target surface points or distance disagree.')
89
+ } else if (!only(gap, ['status']) || !targetFailures.includes(gap.status) ||
90
+ (objects.find(o => o.entityId === q.targetEntityId)?.available === false && gap.status !== objects.find(o => o.entityId === q.targetEntityId).unavailableReason)) refuse('unavailable target separations must not contain measurements or contradict object availability.')
91
+ }
92
+ }
93
+ return samples
16
94
  }
17
95
 
18
96
  /** Verify measured values rather than trusting a successful process exit. */
@@ -20,11 +98,11 @@ export function verifyGeometryReport(report, request) {
20
98
  validateGeometrySelection(request)
21
99
  const allowed = (value, keys) => value && typeof value === 'object' && Object.keys(value).every(key => keys.includes(key))
22
100
  if (!report || Buffer.byteLength(JSON.stringify(report)) > GEOMETRY_INSPECTION_LIMITS.reportBytes ||
23
- report.schemaVersion !== GEOMETRY_INSPECTION_VERSION || report.checkpointSha256 !== request.checkpointSha256 ||
101
+ report.schemaVersion !== (request.surfaceSamples === undefined ? GEOMETRY_INSPECTION_VERSION : SURFACE_INSPECTION_VERSION) || report.checkpointSha256 !== request.checkpointSha256 ||
24
102
  report.frame !== request.frame || report.checkpointUnchanged !== true ||
25
103
  typeof report.blenderVersion !== 'string' || !report.blenderVersion ||
26
104
  !Array.isArray(report.objects) || report.objects.length !== request.entityIds.length ||
27
- !allowed(report, ['schemaVersion', 'checkpointSha256', 'checkpointUnchanged', 'frame', 'blenderVersion', 'objects'])) refuse('the report does not match the requested source, frame or selection.')
105
+ !allowed(report, ['schemaVersion', 'checkpointSha256', 'checkpointUnchanged', 'frame', 'blenderVersion', 'objects', ...(request.surfaceSamples === undefined ? [] : ['surfaceSamples'])])) refuse('the report does not match the requested source, frame or selection.')
28
106
  const vector = (v, length) => Array.isArray(v) && v.length === length && v.every(Number.isFinite)
29
107
  for (const [index, object] of report.objects.entries()) {
30
108
  if (object?.entityId !== request.entityIds[index] || typeof object.type !== 'string' || !object.type) refuse('object identity differs from the selection.')
@@ -46,5 +124,6 @@ export function verifyGeometryReport(report, request) {
46
124
  if (extent < 0 || Math.abs(extent - object.worldAxisExtentMm[axis]) > Math.max(1e-6, Math.abs(extent) * 1e-9)) refuse('axis extents disagree with the measured world bounds.')
47
125
  }
48
126
  }
127
+ if (request.surfaceSamples !== undefined) verifySurfaceSamples(report.surfaceSamples, request, report.objects)
49
128
  return report
50
129
  }
package/lib/index.js CHANGED
@@ -880,4 +880,5 @@ export {
880
880
  export { RECIPE_SCHEMA_VERSION, RECIPE_CAPABILITIES, RECIPE_LIMITS, RecipeError, recipeCapabilitiesForScene, validateRecipeManifest, validateRecipePackage, instantiateRecipe, instantiateRecipeLock } from './recipe.js'
881
881
  export { planRecipeGeometryEdit, recipeGeometryParameters } from './recipe-parameters.js'
882
882
 
883
- export { GEOMETRY_INSPECTION_VERSION, GEOMETRY_INSPECTION_LIMITS, validateGeometrySelection, verifyGeometryReport } from './geometry-inspection.js'
883
+ export { GEOMETRY_INSPECTION_VERSION, GEOMETRY_INSPECTION_LIMITS, SURFACE_INSPECTION_VERSION, SURFACE_INSPECTION_LIMITS,
884
+ validateGeometrySelection, validateSurfaceSelection, verifySurfaceSamples, verifyGeometryReport } from './geometry-inspection.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepblend/dsh-blender-contracts",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "description": "DeepBlend Studio Service Definition, protocol contracts, and stable error codes.",
6
6
  "main": "lib/index.js",