{"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  resolvedCoordinates: ResolvedLocateResultCoordinates,\n  width: number,\n  height: number,\n): number[] {\n  const normalizedBy = resolvedCoordinates.normalizedBy;\n  if (normalizedBy !== undefined) {\n    return result.coordinates.map(() => normalizedBy);\n  }\n\n  if (result.type === '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  resolvedCoordinates: ResolvedLocateResultCoordinates,\n  width: number,\n  height: number,\n) {\n  const normalizedBy = resolvedCoordinates.normalizedBy;\n  const limits = resolveCoordinateLimits(\n    result,\n    resolvedCoordinates,\n    width,\n    height,\n  );\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  resolvedCoordinates: ResolvedLocateResultCoordinates,\n): PixelBbox {\n  // The parsed result type decides whether this maps a bbox or expands a point.\n  // `resolvedCoordinates` describes coordinate order and normalization only.\n  const { width, height } = preparedSize;\n  const normalizedBy = resolvedCoordinates.normalizedBy;\n  assertLocateResultCoordinates(result, resolvedCoordinates, 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","resolvedCoordinates","width","height","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,mBAAoD,EACpDC,KAAa,EACbC,MAAc;IAEd,MAAMC,eAAeH,oBAAoB,YAAY;IACrD,IAAIG,AAAiBC,WAAjBD,cACF,OAAOJ,OAAO,WAAW,CAAC,GAAG,CAAC,IAAMI;IAGtC,IAAIJ,AAAgB,WAAhBA,OAAO,IAAI,EACb,OAAOC,AAA8B,SAA9BA,oBAAoB,KAAK,GAC5B;QAACE;QAAQD;QAAOC;QAAQD;KAAM,GAC9B;QAACA;QAAOC;QAAQD;QAAOC;KAAO;IAGpC,OAAOF,AAA8B,SAA9BA,oBAAoB,KAAK,GAAY;QAACE;QAAQD;KAAM,GAAG;QAACA;QAAOC;KAAO;AAC/E;AAEA,SAASG,8BACPN,MAAyB,EACzBC,mBAAoD,EACpDC,KAAa,EACbC,MAAc;IAEd,MAAMC,eAAeH,oBAAoB,YAAY;IACrD,MAAMM,SAASR,wBACbC,QACAC,qBACAC,OACAC;IAEF,MAAMK,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,EAAEF,MAAM,MAAM,EAAEC,OAAO,CAAC,CAAC;IAC/C,MAAMW,iBACJV,AAAiBC,WAAjBD,eAA6B,CAA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