{"id":17508218,"url":"https://github.com/iraikov/chicken-kdtree","last_synced_at":"2026-02-27T00:02:15.496Z","repository":{"id":31679959,"uuid":"35245510","full_name":"iraikov/chicken-kdtree","owner":"iraikov","description":"KD-Tree implementation in Chicken Scheme","archived":false,"fork":false,"pushed_at":"2019-02-14T14:42:49.000Z","size":39,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-30T16:27:59.758Z","etag":null,"topics":["chicken-scheme","chicken-scheme-eggs","kd-tree","scheme","scheme-language","spatial","spatial-data-analysis","spatial-index"],"latest_commit_sha":null,"homepage":null,"language":"Scheme","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"waylybaye/hyperapp-ocserv","license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iraikov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-07T21:54:43.000Z","updated_at":"2022-06-06T04:20:57.000Z","dependencies_parsed_at":"2022-09-06T16:30:33.005Z","dependency_job_id":null,"html_url":"https://github.com/iraikov/chicken-kdtree","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-kdtree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-kdtree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-kdtree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-kdtree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iraikov","download_url":"https://codeload.github.com/iraikov/chicken-kdtree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245525735,"owners_count":20629830,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chicken-scheme","chicken-scheme-eggs","kd-tree","scheme","scheme-language","spatial","spatial-data-analysis","spatial-index"],"created_at":"2024-10-20T04:35:08.693Z","updated_at":"2025-10-24T16:46:59.444Z","avatar_url":"https://github.com/iraikov.png","language":"Scheme","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kd-tree\n\nK-D tree implementation in Chicken Scheme.\n\n## Documentation\n\nThis library implements a K-D tree\n(http://en.wikipedia.org/wiki/K-d_tree), which is a data structure for\norganizing and searching points in k-dimensional space.\n\nThe K-D tree is a binary search tree in which every branching node\ncontains a k-dimensional point, and every leaf node contains a set of\npoints. Every branching node represents a splitting hyperplane that\ndivides the space into two parts, known as half-spaces.\n\nPoints to the left of the splitting hyperplane are contained in the\nleft subtree of the node and points right of the hyperplane are\ncontained in the right subtree. The splitting hyperplane is chosen so\nas to be perpendicular to one of the axes in the k-dimensional\nspace. The axis at each branching level is chosen in a round-robin\nfashion. For instance, in 3-D space, at level 0, the chosen axis is X,\nso points are divided according to their X-coordinates; at level 1,\nthe chosen axis is Y, so the points are divided according to their\nY-coordinates; at the next branch level the chosen axis is Z, and so\non.\n\n\n### K-dimensional point space\n\nModule `kspace` provides facilities for managament of K-dimensional point spaces.\n\n\u003cprocedure\u003emake-space:: COORDS -\u003e SPACE\u003c/procedure\u003e\n\nGiven a list of coordinate collections of length K, constructs a yasos  K-dimensional point space object. The coordinate collections can be SRFI-4 f32vectors, or collection objects as defined in the yasos collections module.\n\n\u003cprocedure\u003espace? :: OBJECT -\u003e BOOL\u003c/procedure\u003e\n\nK-dimensional point space predicate.\n\n\u003cprocedure\u003edimension :: OBJECT -\u003e INT\u003c/procedure\u003e\nReturns the dimensionality of the point space.\n\n\u003cprocedure\u003epoint :: OBJECT * INT -\u003e REAL LIST\u003c/procedure\u003e\nReturns the coordinates of the point at the given index.\n\n\u003cprocedure\u003ecoord :: OBJECT * INT * INT -\u003e FLOAT\u003c/procedure\u003e\nReturns the k'th coordinate of i'th point, starting from 0.\n\n\n\u003cprocedure\u003ecompare-coord :: SPACE * INT * INT * INT -\u003e INT\u003c/procedure\u003e\n\nGiven the indices of two points and a coordinate index, compares the\nrespective coordinates of the two points and returns -1, 0, or 1,\ndepending on whether the coordinates are less than, equal, or greater\nthan each other.\n\n\u003cprocedure\u003esquared-distance :: SPACE * INT * INT -\u003e FLOAT\u003c/procedure\u003e\n\nReturns the square of the Euclidean distance between the points at the given indices.\n\n\u003cprocedure\u003ecompare-distance :: SPACE * INT * INT -\u003e INT\u003c/procedure\u003e\n\nCompares the square of the Euclidean distance between the points at the given indices.\n\n### K-D tree interface\n\n#### Constructors\n   \n\u003cprocedure\u003emake-kd-tree:: SPACE -\u003e OBJECT\u003c/procedure\u003e\n\nGiven a `kspace` object, constructs and returns a yasos spatial map object.\n\n#### Predicates\n\n\u003cprocedure\u003espatial-map? :: OBJECT -\u003e BOOL \u003c/procedure\u003e\n\nReturns `#t` if the given object is a spatial map, `#f` otherwise.\n\n\u003cprocedure\u003eempty? :: OBJECT -\u003e BOOL  \u003c/procedure\u003e\n\nReturns `#t` if the given spatial map object is empty, `#f` otherwise.\n\n\u003cprocedure\u003ekd-tree-is-valid? :: OBJECT -\u003e BOOL  \u003c/procedure\u003e\n\nChecks whether the K-D tree property holds for the given spatial map.\nSpecifically, it tests that all points in the left subtree lie to the\nleft of the plane, p is on the plane, and all points in the right\nsubtree lie to the right.\n\n\u003cprocedure\u003ekd-tree-all-subtrees-are-valid? :: KD-TREE -\u003e BOOL \u003c/procedure\u003e\n\nChecks whether the K-D tree property holds for the given spatial map\nand all subtrees.\n\n#### Accessors\n\n\u003cprocedure\u003eget-kspace :: OBJECT -\u003e KSPACE\u003c/procedure\u003e\n\nReturns the underlying `kspace` object of the map.\n\n\u003cprocedure\u003espatial-map-\u003elist :: KD-TREE -\u003e POINT LIST\u003c/procedure\u003e\n\nReturns a list with the points contained in the spatial map.\n\n#### Query procedures\n\n\u003cprocedure\u003enearest-neighbor :: SPATIAL-MAP * POINT -\u003e POINT\u003c/procedure\u003e\n\nNearest neighbor of a point.\n\n\u003cprocedure\u003enear-neighbors :: SPATIAL-MAP * POINT * RADIUS -\u003e POINT LIST \u003c/procedure\u003e\n\nNeighbors of a point within radius r.\n\n\u003cprocedure\u003ek-nearest-neighbors :: SPATIAL-MAP * POINT * INT -\u003e POINT LIST \u003c/procedure\u003e\n\nK nearest neighbors of a point.\n\n\u003cprocedure\u003eslice :: SPATIAL-MAP * AXIS * COORD * COORD -\u003e POINT LIST \u003c/procedure\u003e\n\nReturns all points between two planes.\n\n#### Combinators\n\n\u003cprocedure\u003espatial-map-for-each :: SPATIAL-MAP * F -\u003e VOID \u003c/procedure\u003e\n\nPoint iterator.\n\n\u003cprocedure\u003espatial-map-fold :: SPATIAL-MAP * F -\u003e AX \u003c/procedure\u003e\n\n\u003cprocedure\u003espatial-map-fold-right :: SPATIAL-MAP * F * INIT -\u003e INIT \u003c/procedure\u003e\n\nFold on points.\n\n\u003cprocedure\u003espatial-map-fold-right* :: SPATIAL-MAP * F * INIT -\u003e INIT \u003c/procedure\u003e\n\nFold on points and their indices.\n\n#### Modifiers\n\n\u003cprocedure\u003ekd-tree-remove :: SPATIAL-MAP * POINT -\u003e SPATIAL-MAP \u003c/procedure\u003e\n\n\n## Examples\n\n\n```scheme\n\n(import scheme (chicken base) \n        yasos random-mtzig kspace kd-tree)\n\n(let* (\n       (n (inexact-\u003eexact 1e5)) (k 40) (r 1.0) (randst (init 9))\n       \n       ;; generate random coordinates\n       (xs (randn/f32! n randst))\n       (ys (randn/f32! n randst))\n       (zs (randn/f32! n randst))\n       ;; create a kspace\n       (pts (list xs ys zs))\n       (kspace3d (make-space pts))\n       ;; create the spatial map\n       (kdt (make-kd-tree kspace3d))\n       ;; choose a random point index\n       (xi (inexact-\u003eexact (modulo (random! randst) n)))\n       ;; retrieve the coordinates of the chosen point\n       (x  (point kspace3d xi))\n       )\n\n    (print \"nearest-neighbor of \" x \": \" (nearest-neighbor kdt x))\n    (print k \" nearest neighbors of \" x \": \" (k-nearest-neighbors kdt x k))\n    (print \"near neighbors of \" x \" within \" r \": \" (near-neighbors kdt k r))\n\n)\n\n\n```\n\n## Version history\n\n- 6.0 : refactored to use yasos, ported to CHICKEN 5\n- 5.0 : added list-\u003ekd-tree* procedure to KdTree class\n- 4.1-4.8 : Using f64vector for internal point representation\n- 4.0-4.1 : Added with-distance? flag to kd-tree-near-neighbors\n- 3.2 : Bug fix in kd-tree-near-neighbors\n- 2.0 : Improvements to internal representation\n- 1.0 : Initial release\n\n## License\n\n\u003e\n\u003e Copyright 2012-2019 Ivan Raikov\n\u003e \n\u003e  This program is free software: you can redistribute it and/or modify\n\u003e  it under the terms of the GNU General Public License as published by\n\u003e  the Free Software Foundation, either version 3 of the License, or (at\n\u003e  your option) any later version.\n\u003e  \n\u003e  This program is distributed in the hope that it will be useful, but\n\u003e  WITHOUT ANY WARRANTY; without even the implied warranty of\n\u003e  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n\u003e  General Public License for more details.\n\u003e \n\u003e  A full copy of the GPL license can be found at\n\u003e  \u003chttp://www.gnu.org/licenses/\u003e.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firaikov%2Fchicken-kdtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Firaikov%2Fchicken-kdtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firaikov%2Fchicken-kdtree/lists"}