Code Monkey home page Code Monkey logo

Comments (2)

timohausmann avatar timohausmann commented on May 22, 2024

Hey, I think the easiest way to achieve this is to modify the retrieve function and collect the nodes too while retrieving the objects.

I put together a demo showcasing this: https://timohausmann.github.io/quadtree-js/retrieve-nodes.html

In this example the retrieve function now returns an object with both objects and nodes, like { objects: Rect[], nodes: Quadtree[] } Note that multiple nodes can be retrieved, or none if there are no matching objects. Does this work for you?

Modified branch: https://github.com/timohausmann/quadtree-js/tree/retrieve-nodes

If you use the npm package you could simply overwrite the Quadtree.prototype.retrieve function with the modified version after importing it.

from quadtree-js.

etienne-1985 avatar etienne-1985 commented on May 22, 2024

Thanks for the quick implementation! Looking at your sample it seems it does what I'm looking for.
Just in case you decided to include this feature later, I just thought in the same way getIndex retrieve the first node index, perhaps a method like getLastIndex could get the deepest node in the tree,

As an aside, I put another method which collects all nodes indexes along the path leading to the object.
It only works for my specific case without objects overlaping over several nodes, and a unique path to the object
Also this is a quick non optimized implementation but in case it would be useful for someone else , here it is

/**
   * Path in subtree leading to the specified element
   * @param rootNode 
   * @param targetElt 
   * @returns node indices
   */
  retrievePath(rootNode: Quadtree, targetElt: Rect) {
    if (rootNode.retrieve(targetElt).length) {
      let nodesIndices: any = [];
      let nodeIndex ;
      let currentNode = rootNode;

      // until we reach the deepest node (=leaf)
      while (currentNode.nodes.length) {
        // look for the index of the next child node containing the object
        nodeIndex = currentNode.nodes.findIndex((childNode, i) => childNode.retrieve(targetElt).length);
        nodesIndices.push(nodeIndex);
        // update current node
        currentNode = currentNode.nodes[nodeIndex];
      }

      // print found path
      console.log("Matching path found: " + nodesIndices.reduce((concat, nodeIndex) => concat + " => " + nodeIndex));
      return nodesIndices;
    } else return null;
  }

from quadtree-js.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.