import {z} from 'zod' export type TreeSweepParameters = {sweep_type: 'tree', base_prim_path?: string | undefined}; export type SphereSweepParameters = {sweep_type: 'sphere', radius?: number | undefined, position?: Array | undefined, direction?: Array | undefined, max_distance?: number | undefined}; export type BoxSweepParameters = {sweep_type: 'box', half_extent?: Array | undefined, sphere_radius?: number | undefined, position?: Array | undefined, rotation?: Array | undefined, direction?: Array | undefined, max_distance?: number | undefined}; export type CreateApiPhysicsCollisionSweepArgs = {relative_to_prim?: (string | null) | undefined, body: TreeSweepParameters | SphereSweepParameters | BoxSweepParameters}; export type CreateApiPhysicsCollisionSweepOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const convexHull = z.object({shape_type: z.literal("convex_hull").optional(), vertices: z.array(z.array(z.number()))}); export const sphere = z.object({shape_type: z.literal("sphere").optional(), radius: z.number()}); export const box = z.object({shape_type: z.literal("box").optional(), size_x: z.number(), size_y: z.number(), size_z: z.number(), box_type: z.literal("FULL").optional()}); export const capsule = z.object({shape_type: z.literal("capsule").optional(), radius: z.number(), height: z.number()}); export const cylinder = z.object({shape_type: z.literal("cylinder").optional(), radius: z.number(), height: z.number()}); export const plane = z.object({shape_type: z.literal("plane").optional()}); export const pose = z.object({position: z.array(z.union([z.number(), z.number().int()])).nullable().optional(), orientation: z.array(z.union([z.number(), z.number().int()])).nullable().optional()}); export const collider = z.object({shape: z.union([convexHull, sphere, box, capsule, cylinder, plane]), pose: pose, prim_path: z.string()}); export const createApiPhysicsCollisionSweepResponse = z.record(z.string(), collider); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Sweep Collisions * * POST /physics/collision/sweep */ export const createApiPhysicsCollisionSweep = async (args: CreateApiPhysicsCollisionSweepArgs, options: CreateApiPhysicsCollisionSweepOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.relative_to_prim != null) query.append("relative_to_prim", String(args.relative_to_prim)) const url = `${baseUrl.replace(/\/+$/, '')}/physics/collision/sweep${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiPhysicsCollisionSweepResponse, await res.json()) };