{"version":3,"file":"ai-model/shared/model-locate-result/pixel-bbox-mapper.mjs","sources":["../../../../../src/ai-model/shared/model-locate-result/pixel-bbox-mapper.ts"],"sourcesContent":["import {\n  expandPointToBbox,\n  mapNormalizedCoordinatesToPixelBbox,\n  maxPixelIndex,\n} from './bbox';\nimport type {\n  LocateResultBbox,\n  LocateResultValue,\n  PixelBbox,\n  ResolvedLocateResultCoordinates,\n} from './types';\n\ntype Point = [number, number];\n\nconst defaultBboxSize = 20; // must be even number\n\nfunction resolveCoordinateLimits(\n  result: LocateResultValue,\n  width: number,\n  height: number,\n): number[] {\n  const resolvedCoordinates = result.coordinatesMeta;\n  const normalizedBy = resolvedCoordinates.normalizedBy;\n  if (normalizedBy !== undefined) {\n    return result.coordinates.map(() => normalizedBy);\n  }\n\n  if (resolvedCoordinates.shape === 'bbox') {\n    return resolvedCoordinates.order === 'yx'\n      ? [height, width, height, width]\n      : [width, height, width, height];\n  }\n\n  return resolvedCoordinates.order === 'yx' ? [height, width] : [width, height];\n}\n\nfunction assertLocateResultCoordinates(\n  result: LocateResultValue,\n  width: number,\n  height: number,\n) {\n  const resolvedCoordinates = result.coordinatesMeta;\n  const normalizedBy = resolvedCoordinates.normalizedBy;\n  const limits = resolveCoordinateLimits(result, width, height);\n  const outOfRange = result.coordinates.some((value, index) => {\n    const limit = limits[index];\n    return (\n      typeof value !== 'number' ||\n      !Number.isFinite(value) ||\n      value < 0 ||\n      value > limit\n    );\n  });\n\n  if (!outOfRange) {\n    return;\n  }\n\n  const source =\n    normalizedBy !== undefined\n      ? `normalized range [0, ${normalizedBy}]`\n      : `image size [0, ${width}]x[0, ${height}]`;\n  const normalizedInfo =\n    normalizedBy !== undefined ? ` normalizedBy=${normalizedBy}` : '';\n  throw new Error(\n    `locate result coordinates ${JSON.stringify(\n      result.coordinates,\n    )} exceed ${source}. shape=${\n      resolvedCoordinates.shape\n    } order=${resolvedCoordinates.order}${normalizedInfo} limits=${JSON.stringify(\n      limits,\n    )}`,\n  );\n}\n\nfunction reorderCoordinatesToXy(\n  coordinates: LocateResultBbox,\n  order: ResolvedLocateResultCoordinates['order'],\n): LocateResultBbox;\nfunction reorderCoordinatesToXy(\n  coordinates: Point,\n  order: ResolvedLocateResultCoordinates['order'],\n): Point;\nfunction reorderCoordinatesToXy(\n  coordinates: LocateResultBbox | Point,\n  order: ResolvedLocateResultCoordinates['order'],\n): LocateResultBbox | Point;\nfunction reorderCoordinatesToXy(\n  coordinates: LocateResultBbox | Point,\n  order: ResolvedLocateResultCoordinates['order'],\n): LocateResultBbox | Point {\n  if (order !== 'yx') {\n    return coordinates;\n  }\n\n  if (coordinates.length === 4) {\n    const [top, left, bottom, right] = coordinates;\n    return [left, top, right, bottom];\n  }\n\n  const [y, x] = coordinates;\n  return [x, y];\n}\n\nexport function mapLocateResultToPixelBboxByCoordinates(\n  result: LocateResultValue,\n  { preparedSize }: { preparedSize: { width: number; height: number } },\n): PixelBbox {\n  // The parsed result metadata decides whether this maps a bbox or expands a\n  // point, and how to interpret coordinate order and normalization.\n  const { width, height } = preparedSize;\n  const resolvedCoordinates = result.coordinatesMeta;\n  const normalizedBy = resolvedCoordinates.normalizedBy;\n  assertLocateResultCoordinates(result, width, height);\n\n  const xyCoordinates = reorderCoordinatesToXy(\n    result.coordinates,\n    resolvedCoordinates.order,\n  );\n\n  const xyBbox =\n    xyCoordinates.length === 4\n      ? xyCoordinates\n      : expandPointToBbox(\n          xyCoordinates[0],\n          xyCoordinates[1],\n          normalizedBy ?? maxPixelIndex(width),\n          normalizedBy ?? maxPixelIndex(height),\n          normalizedBy === undefined ? defaultBboxSize / 2 : normalizedBy / 100,\n        );\n\n  return normalizedBy === undefined\n    ? xyBbox\n    : mapNormalizedCoordinatesToPixelBbox(xyBbox, normalizedBy, width, height);\n}\n"],"names":["defaultBboxSize","resolveCoordinateLimits","result","width","height","resolvedCoordinates","normalizedBy","undefined","assertLocateResultCoordinates","limits","outOfRange","value","index","limit","Number","source","normalizedInfo","Error","JSON","reorderCoordinatesToXy","coordinates","order","top","left","bottom","right","y","x","mapLocateResultToPixelBboxByCoordinates","preparedSize","xyCoordinates","xyBbox","expandPointToBbox","maxPixelIndex","mapNormalizedCoordinatesToPixelBbox"],"mappings":";AAcA,MAAMA,kBAAkB;AAExB,SAASC,wBACPC,MAAyB,EACzBC,KAAa,EACbC,MAAc;IAEd,MAAMC,sBAAsBH,OAAO,eAAe;IAClD,MAAMI,eAAeD,oBAAoB,YAAY;IACrD,IAAIC,AAAiBC,WAAjBD,cACF,OAAOJ,OAAO,WAAW,CAAC,GAAG,CAAC,IAAMI;IAGtC,IAAID,AAA8B,WAA9BA,oBAAoB,KAAK,EAC3B,OAAOA,AAA8B,SAA9BA,oBAAoB,KAAK,GAC5B;QAACD;QAAQD;QAAOC;QAAQD;KAAM,GAC9B;QAACA;QAAOC;QAAQD;QAAOC;KAAO;IAGpC,OAAOC,AAA8B,SAA9BA,oBAAoB,KAAK,GAAY;QAACD;QAAQD;KAAM,GAAG;QAACA;QAAOC;KAAO;AAC/E;AAEA,SAASI,8BACPN,MAAyB,EACzBC,KAAa,EACbC,MAAc;IAEd,MAAMC,sBAAsBH,OAAO,eAAe;IAClD,MAAMI,eAAeD,oBAAoB,YAAY;IACrD,MAAMI,SAASR,wBAAwBC,QAAQC,OAAOC;IACtD,MAAMM,aAAaR,OAAO,WAAW,CAAC,IAAI,CAAC,CAACS,OAAOC;QACjD,MAAMC,QAAQJ,MAAM,CAACG,MAAM;QAC3B,OACE,AAAiB,YAAjB,OAAOD,SACP,CAACG,OAAO,QAAQ,CAACH,UACjBA,QAAQ,KACRA,QAAQE;IAEZ;IAEA,IAAI,CAACH,YACH;IAGF,MAAMK,SACJT,AAAiBC,WAAjBD,eACI,CAAC,qBAAqB,EAAEA,aAAa,CAAC,CAAC,GACvC,CAAC,eAAe,EAAEH,MAAM,MAAM,EAAEC,OAAO,CAAC,CAAC;IAC/C,MAAMY,iBACJV,AAAiBC,WAAjBD,eAA6B,CAAC,cAAc,EAAEA,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