Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting back bounding box coords of found indices? #45

Open
leeoniya opened this issue Aug 30, 2022 · 1 comment
Open

getting back bounding box coords of found indices? #45

leeoniya opened this issue Aug 30, 2022 · 1 comment
Labels
question Further information is requested

Comments

@leeoniya
Copy link

leeoniya commented Aug 30, 2022

hey @mourner

i'm currently reading back bounding box coords to render a hover rect over the matched index, it looks something like below bound to mousemove. this works well enough for my-sized datasets. i assume there isn't anything more efficient than let offs = index._indices.indexOf(idx) * 4; besides wasting mem/cpu to store some kind of additional mapping between returned index and coords externally?

function searchFlat(xMin, yMin, xMax, yMax) {
  let flatIdxs = index.search(xMin, yMin, xMax ?? xMin + 1, yMax ?? yMin + 1);

  if (flatIdxs.length > 0) {
    for (let j = 0; j < flatIdxs.length; j++) {
      let idx = flatIdxs[j];

      let offs = index._indices.indexOf(idx) * 4;
      let minX = index._boxes[offs++];
      let minY = index._boxes[offs++];
      let maxX = index._boxes[offs++];
      let maxY = index._boxes[offs++];

      if (hoveredIdx !== idx) {
        hoverStyle.display = "block";

        hoverStyle.top = `${Math.round(minY / pxRatio)}px`;
        hoverStyle.left = `${Math.round(minX / pxRatio)}px`;
        hoverStyle.width = `${Math.round((maxX - minX) / pxRatio)}px`;
        hoverStyle.height = `${Math.round((maxY - minY) / pxRatio)}px`;

        hoveredIdx = idx;
      }

      break;
    }
  }
  else {
    hoverStyle.display = "none";
    hoveredIdx = null;
  }
}
@mourner mourner added the question Further information is requested label Sep 2, 2022
@mourner
Copy link
Owner

mourner commented Apr 24, 2023

Yeah, there's no fast way to get original coords from the index. indexOf is gross — as a workaround, I'd rather generate a Uint16/32Array that maps an id to its position in _indices/_boxes array. Linear to generate (just a loop over ids once after indexing), doesn't take much space, and allows for constant time lookups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants