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

Feature request: allow the KDTree to return the KNN points indices instead of coordinates. #288

Open
Brainkite opened this issue Jan 23, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Brainkite
Copy link

Motivation

The KDtree queries should allow to return the indices of the points instead of the coords.
Do you think this implementation could be easily updated for this feature or the structure is incompatible with idices tracking?

Help make it happen!

I am not willing to submit a PR to implement this change.

@Brainkite Brainkite added the enhancement New feature or request label Jan 23, 2023
@AlexandreBrown
Copy link

AlexandreBrown commented Mar 6, 2023

Hello @edwinRNDR ,
We would also be interested in this kind of feature since for us finding which index corresponds to the outputed nearest neighbors x,y,z is not stable and a pain at the moment. It would be much more efficient and stable if the lib could return the indexes.
Any chance this could be added to the lib?
Cheers

@edwinRNDR
Copy link
Member

Hey. I will have a look at getting that functionality in but I have limited capacity to get this done.

In case you need a quick solution that's relatively inexpensive you can use a hash map.

import org.openrndr.application
import org.openrndr.extra.kdtree.kdTree
import org.openrndr.extra.noise.scatter

fun main() {
    application {
        program {
            val p = drawer.bounds.scatter(100.0)
            val kd = p.kdTree()
            val indices = p.withIndex().associate { it.value to it.index }

            extend {
                val nearestIndices = kd.findKNearest(mouse.position, 3).map { indices[it] ?: error("mystery point") }
            }
        }
    }
}

@edwinRNDR edwinRNDR self-assigned this Mar 6, 2023
@AlexandreBrown
Copy link

Thank you @edwinRNDR , we were getting unstable hashmap due to the key we were using (we tried using maps of maps with double as key and we also tried to have a list of double as key) but we didn't try a data class with the x,y,z.
We tested your map approach and it is super stable thanks to the data class hashcode stability.
Thank you for your quick response this was very helpful.
Cheers!
PS: I can help contribute to this repo to add the indices, won't have time now but in a few months I will be more free to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants