KDTree with VB.net

346 views
Skip to first unread message

Eric

unread,
Sep 22, 2013, 6:37:06 PM9/22/13
to accor...@googlegroups.com
I am looking at the accord.net framework for use in vb.net. Specifically I need to sort a bunch of points according to nearest neighbor. I understand that KDTree may be the way to go rather than brute force according to a number of posts out there.

I have been trying to get Accord's KDTree working but am running into probloems. The help file for that page looks like C???.

I have the following code. pointArray is an array where each value is an array of doubles:
pointArray(0) = new array (1) {2,5]
pointArray(1) = new array (1) {7,2]
pointArray(2) = new array (1) {100,5]

Dim tree As KDTree(Of Array) = KDTree.FromData(Of Array)(pointArray)

I keep getting an error:
Unable to cast object of type 'System.Array[]' to type 'System.Double[][]'.

I have tried as many combinations of array and doubles as I can but to no avail. Can anybody help me use this in VB.net?

César

unread,
Sep 22, 2013, 9:02:30 PM9/22/13
to accor...@googlegroups.com
Hi Eric,

I've translated the kd-tree sample code available at the documentation into vb.net. From what I could see, I suppose the problem is that in VB.NET you have to specify the type of the points as 'Double', otherwise the compiler will complain. Please let me know if it helps!

By the way, if you are going to use the kd-trees, I would suggest to stick with the radius method for the time being. There is currently a small issue with the nearest neighbors method in the current version. The fix will be available in the next release, which should be available soon.

Best regards,
Cesar

   ' This is the same example found in Wikipedia page on
   '
k-d trees: http://en.wikipedia.org/wiki/K-d_tree

   
' Suppose we have the following set of points:

   Dim points =
   {
       New Double() {2, 3},
       New Double() {5, 4},
       New Double() {9, 6},
       New Double() {4, 7},
       New Double() {8, 1},
       New Double() {7, 2}
   }

   '
To create a tree from a set of points, we use
   
Dim tree = KDTree.FromData(Of Integer)(points)

   
' Now we can manually navigate the tree
   Dim node = tree.Root.Left.Right

   '
Or traverse it automatically
   
For Each n As KDTreeNode(Of Integer) In tree
       
Dim location = n.Position
       
Console.WriteLine(location.Length)
   
Next

   
' Given a query point, we can also query for other
   '
points which are near this point within a radius

   
Dim query = New Double() {5, 3}

   
' Locate all nearby points within an Euclidean distance of 1.5
   '
(answer should be a single point located at position (5,4))
   
Dim result = tree.Nearest(query, radius:=1.5)


   
' We can also use alternate distance functions
   tree.Distance = Function(a, b) Accord.Math.Distance.Manhattan(a, b)

   '
And also query for a fixed number of neighbor points
   
' (answer should be the points at (5,4), (7,2), (2,3))

   Dim neighbors = tree.Nearest(query, neighbors:=3)

er...@janasnyder.com

unread,
Sep 22, 2013, 10:55:00 PM9/22/13
to accor...@googlegroups.com
Wow César!

Thank you very much. I will try tomorrow when I am back on the computer with VS 2012.

er...@sharpline.com

unread,
Sep 23, 2013, 12:40:17 PM9/23/13
to accor...@googlegroups.com
César:

That worked beautifully! Thank you. If I have more questions I'll post again.

Eric

unread,
Sep 23, 2013, 1:01:35 PM9/23/13
to accor...@googlegroups.com
By the way, if you are going to use the kd-trees, I would suggest to stick with the radius method for the time being. There is currently a small issue with the nearest neighbors method in the current version. The fix will be available in the next release, which should be available soon.

Hmmm...What I really need is to get the single closest point to a search point. I don't know the radii ahead of time. The only way I can see using radius method in a loop and expanding the radius each time it loops with zero results until I get one or multiple results and then figure out which one is closer. Unfortunately, not all that helpful in my situation.

What is the issue with the neighbor method?

César

unread,
Sep 23, 2013, 2:11:43 PM9/23/13
to accor...@googlegroups.com
If you looking for a single point, then it should work fine. The problem may arise if you ask for more points (more neighbors). The current implementation was providing an approximate version of the k-nn search, so there are cases where not all possible neighbors would be included within a search. I've already fixed this on the current git development branch, I just need to package a new release with the fix. You can, however, also try to checkout the git repository and compile the sources there (it is not too difficult, I can help if you really need!).

Best regards,
Cesar
Reply all
Reply to author
Forward
0 new messages