spatial-trees-0.2/0000755000175000017500000000000010421414374013770 5ustar crhodescrhodesspatial-trees-0.2/LICENCE0000644000175000017500000000271310153067463014765 0ustar crhodescrhodesCopyright (c) 2004, Christophe Rhodes All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Christophe Rhodes nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. spatial-trees-0.2/BUGS0000644000175000017500000000075410160257422014460 0ustar crhodescrhodes1: R* trees sometimes fail their self-tests, with an error message of the form There is no applicable method for the generic function # when called with arguments (# (# # #)> #S(LEAF-NODE-ENTRY :RECTANGLE #<(0.373667,0.62124753) - (0.58179224,1.5787126)> :DATUM #<(0.373667,0.62124753) - (0.58179224,1.5787126)>)). spatial-trees-0.2/spatial-trees.asd0000644000175000017500000000241710421413427017240 0ustar crhodescrhodes;;; -*- mode: lisp -*- (asdf:defsystem :spatial-trees :components ((:module base :pathname #.(make-pathname :directory '(:relative)) :components ((:file "package") (:file "basedefs" :depends-on ("package")) (:file "rectangles" :depends-on ("package")))) (:module tree-impls :depends-on (base) :pathname #.(make-pathname :directory '(:relative)) :components ((:file "r-trees") (:file "greene-trees" :depends-on ("r-trees")) (:file "rstar-trees" :depends-on ("r-trees")) (:file "rplus-trees" :depends-on ("r-trees")) (:file "x-trees" :depends-on ("r-trees" "rstar-trees")))) (:module viz :depends-on (base) :pathname #.(make-pathname :directory '(:relative)) :components ((:static-file "spatial-tree-viz" :pathname #p"spatial-tree-viz.lisp"))) (:module tests :depends-on (base) :pathname #.(make-pathname :directory '(:relative)) :components ((:static-file "spatial-tree-test" :pathname #p"spatial-tree-test.lisp"))) (:static-file "LICENCE") (:static-file "TODO"))) spatial-trees-0.2/rstar-trees.lisp0000644000175000017500000002473310150655132017143 0ustar crhodescrhodes;;; Modifications to the basic R-tree following "The R*-tree: An ;;; Efficient and Robust Access Method for Points and Rectangles", ;;; Beckmann, Kriegel, Schneider and Seeger, Proc. ACM Int. Conf. on ;;; Management of Data, 1990 (in-package "SPATIAL-TREES-IMPL") (defclass r*-tree (r-tree) ((removed-number :initarg :removed-number :accessor removed-number))) (defmethod initialize-instance :after ((tree r*-tree) &rest args) (declare (ignore args)) (unless (slot-boundp tree 'removed-number) (setf (slot-value tree 'removed-number) (round (* (max-per-node tree) 3/10))))) (defmethod make-spatial-tree ((kind (eql :r*)) &rest initargs) (apply #'make-instance 'r*-tree :root-node (make-instance 'spatial-tree-leaf-node :records nil) initargs)) ;;; 4.1 Algorithm ChooseSubtree (defun overlap (x others tree) (loop for y in others sum (let ((i (intersection x (mbr y tree)))) (if i (area i) 0)))) (defun height (tree) (labels ((height-above (node) (if (typep node 'spatial-tree-leaf-node) 0 (1+ (height-above (car (children node))))))) (height-above (root-node tree)))) (defun choose-subtree (thing node level tree) (cond ((= level 0) node) ((typep (car (children node)) 'spatial-tree-leaf-node) (check (= level 1) "Search in CHOOSE-SUBTREE too deep") ;; NOTE: does not contain the probabilistic optimization near the ;; end of section 4.1 (do* ((children (children node) (cdr children)) (child (car children) (car children)) (candidate child) (min-overlap-extension (- (overlap (minimum-bound (mbr thing tree) (mbr child tree)) (remove child (children node)) tree) (overlap (mbr child tree) (remove child (children node)) tree)))) ((null children) (check (typep candidate 'spatial-tree-leaf-node) "CHOOSE-SUBTREE candidate ~S is not a leaf node" candidate) candidate) (let* ((new-overlap (overlap (minimum-bound (mbr thing tree) (mbr child tree)) (remove child (children node)) tree)) (old-overlap (overlap (mbr child tree) (remove child (children node)) tree)) (extension (- new-overlap old-overlap))) (when (or (< extension min-overlap-extension) (and (= extension min-overlap-extension) (< (- (area (minimum-bound (mbr thing tree) (mbr child tree))) (area (mbr child tree))) (- (area (minimum-bound (mbr thing tree) (mbr candidate tree))) (area (mbr candidate tree)))))) (setf min-overlap-extension extension candidate child))))) (t (do* ((children (children node) (cdr children)) (child (car children) (car children)) (candidate child) (min-extension (- (area (minimum-bound (mbr child tree) (mbr thing tree))) (area (mbr child tree))))) ((null children) (choose-subtree thing candidate (1- level) tree)) (let* ((new-area (area (minimum-bound (mbr child tree) (mbr thing tree)))) (old-area (area (mbr child tree))) (extension (- new-area old-area))) (when (or (< extension min-extension) (and (= extension min-extension) (< old-area (area (mbr candidate tree))))) (setf min-extension extension candidate child))))))) (defmethod choose-leaf (r (tree r*-tree)) (choose-subtree r (root-node tree) (height tree) tree)) ;;; 4.2 Split of the R*-tree (defun margin (rectangle) (let ((result 0)) (do ((lows (lows rectangle) (cdr lows)) (highs (highs rectangle) (cdr highs))) ((null lows) result) (incf result (- (car highs) (car lows)))))) (defun choose-split-axis (entries tree) (let ((max-per-node (max-per-node tree)) (min-per-node (min-per-node tree))) (do ((lows (lows (mbr (car entries) tree)) (cdr lows)) (axis 0 (1+ axis)) (min-margin) (min-axis 0)) ((null lows) min-axis) (let ((sort-by-low (sort (copy-list entries) #'< :key (lambda (x) (nth axis (lows (mbr x tree)))))) (sort-by-high (sort (copy-list entries) #'< :key (lambda (x) (nth axis (highs (mbr x tree))))))) (do ((k 1 (1+ k))) ((> k (- (+ 2 max-per-node) (* 2 min-per-node)))) (cond ((null min-margin) (setf min-margin (min (+ (margin (minimum-bound-of (subseq sort-by-low 0 (+ min-per-node k -1)) tree)) (margin (minimum-bound-of (subseq sort-by-low (+ min-per-node k -1)) tree))) (+ (margin (minimum-bound-of (subseq sort-by-high 0 (+ min-per-node k -1)) tree)) (margin (minimum-bound-of (subseq sort-by-high (+ min-per-node k -1)) tree)))))) (t (let ((min (min (+ (margin (minimum-bound-of (subseq sort-by-low 0 (+ min-per-node k -1)) tree)) (margin (minimum-bound-of (subseq sort-by-low (+ min-per-node k -1)) tree))) (+ (margin (minimum-bound-of (subseq sort-by-high 0 (+ min-per-node k -1)) tree)) (margin (minimum-bound-of (subseq sort-by-high (+ min-per-node k -1)) tree)))))) (when (< min min-margin) (setf min-margin min min-axis axis)))))))))) (defun choose-split-index (entries axis tree) (let ((max-per-node (max-per-node tree)) (min-per-node (min-per-node tree))) (let ((one) (two) ;; this is a safe upper bound to the minimum overlap value (min-overlap-value (area (minimum-bound-of entries tree)))) (let ((sort-by-low (sort (copy-list entries) #'< :key (lambda (x) (nth axis (lows (mbr x tree)))))) (sort-by-high (sort (copy-list entries) #'< :key (lambda (x) (nth axis (highs (mbr x tree))))))) (do ((k 1 (1+ k))) ((> k (- (+ 2 max-per-node) (* 2 min-per-node))) (values one two)) (let ((a (subseq sort-by-low 0 (+ min-per-node k -1))) (b (subseq sort-by-low (+ min-per-node k -1)))) (let* ((i (intersection (minimum-bound-of a tree) (minimum-bound-of b tree))) (overlap-value (if i (area i) 0))) (when (or (< overlap-value min-overlap-value) (and (= overlap-value min-overlap-value) (< (+ (area (minimum-bound-of a tree)) (area (minimum-bound-of b tree)))))) (setf min-overlap-value overlap-value one a two b)))) (let ((a (subseq sort-by-high 0 (+ min-per-node k -1))) (b (subseq sort-by-high (+ min-per-node k -1)))) (let* ((i (intersection (minimum-bound-of a tree) (minimum-bound-of b tree))) (overlap-value (if i (area i) 0))) (when (or (< overlap-value min-overlap-value) (and (= overlap-value min-overlap-value) (< (+ (area (minimum-bound-of a tree)) (area (minimum-bound-of b tree)))))) (setf min-overlap-value overlap-value one a two b))))))))) (defmethod split-node ((tree r*-tree) new node) (let ((new-node (make-node-like node)) (entries (cons new (children node)))) (let ((axis (choose-split-axis entries tree))) (multiple-value-bind (one two) (choose-split-index entries axis tree) (setf (children node) one (slot-value node 'mbr) (minimum-bound-of one tree)) (setf (children new-node) two (slot-value new-node 'mbr) (minimum-bound-of two tree)) new-node)))) ;;; 4.3 Forced Reinsert (defvar *data-rectangle*) (defvar *overflowed-levels* nil) (defun reinsert (thing tree node level) (let* ((entries (cons thing (children node))) (mbr (minimum-bound-of entries tree)) (cmbr (mapcar #'- (highs mbr) (lows mbr))) (sorted (sort (copy-list entries) #'> :key (lambda (entry) (let* ((r (mbr entry tree)) (cs (mapcar #'- (highs r) (lows r)))) (loop for cm in cmbr for c in cs ;; squared distance is fine sum (expt (- cm c) 2))))))) (let ((removed (subseq sorted 0 (removed-number tree)))) (setf (children node) (subseq sorted (removed-number tree)) (slot-value node 'mbr) (minimum-bound-of (children node) tree)) (dolist (entry (reverse removed)) (%insert entry tree level))))) (defun overflow-treatment (thing tree node level) (if (member level *overflowed-levels*) (split-node tree thing node) (let ((*overflowed-levels* (cons level *overflowed-levels*))) (reinsert thing tree node level)))) (defun %insert (thing tree level) (let ((node (choose-subtree thing (root-node tree) level tree))) (cond ((< (length (children node)) (max-per-node tree)) (push thing (children node)) (adjust-tree tree node)) (t (let ((new-node (overflow-treatment thing tree node level))) (let ((new (adjust-tree tree node new-node))) (when new (let ((new-root (make-instance 'spatial-tree-node :children (list (root-node tree) new)))) (setf (parent (root-node tree)) new-root (root-node tree) new-root (parent new) new-root))))))))) (defmethod insert ((r t) (tree r*-tree)) (let ((*data-rectangle* r)) (%insert (make-leaf-node-entry :datum r :rectangle (funcall (rectfun tree) r)) tree (height tree))) tree) spatial-trees-0.2/package.lisp0000644000175000017500000000165210153117451016256 0ustar crhodescrhodes(cl:defpackage "SPATIAL-TREES" (:use "CL") (:shadow "DELETE" "SEARCH") (:export "DELETE" "INSERT" "SEARCH" "MAKE-SPATIAL-TREE")) (cl:defpackage "SPATIAL-TREES-PROTOCOL" (:use "CL" "SPATIAL-TREES") (:shadowing-import-from "SPATIAL-TREES" "DELETE" "SEARCH") (:export ;; interface definitions "DELETE" "INSERT" "SEARCH" ;; protocol functions "CHOOSE-LEAF" "SPLIT-NODE" "CHILDREN" "RECORDS" "ROOT-NODE" ;; protocol classes "SPATIAL-TREE" "SPATIAL-TREE-NODE" "SPATIAL-TREE-LEAF-NODE" )) (cl:defpackage "RECTANGLES" (:use "CL") (:shadow "INTERSECTION") (:export "RECTANGLE" "MAKE-RECTANGLE" "INTERSECTION" "INTERSECTP" "AREA" "MINIMUM-BOUND" "LOWS" "HIGHS")) (cl:defpackage "SPATIAL-TREES-IMPL" (:use "CL" "SPATIAL-TREES" "SPATIAL-TREES-PROTOCOL" "RECTANGLES") (:shadowing-import-from "SPATIAL-TREES" "DELETE" "SEARCH") (:shadowing-import-from "RECTANGLES" "INTERSECTION")) spatial-trees-0.2/basedefs.lisp0000644000175000017500000000473710153067105016446 0ustar crhodescrhodes;;; The base definitions for protocol classes and functions for ;;; spatial trees. (in-package "SPATIAL-TREES-IMPL") (defclass spatial-tree () ((root-node :initarg :root-node :accessor root-node) (rectfun :initarg :rectfun :reader rectfun) (max-per-node :initform 7 :reader max-per-node) (min-per-node :initform 3 :reader min-per-node))) (defmethod print-object ((o spatial-tree) s) (print-unreadable-object (o s :type t) (format s "~1I~_~W" (root-node o)))) (defclass spatial-tree-node () ((mbr :initarg :mbr) (children :initarg :children :accessor children) (parent :initarg :parent :accessor parent))) (defmethod print-object ((o spatial-tree-node) s) (print-unreadable-object (o s :type t) (when (slot-boundp o 'mbr) (format s "~W " (slot-value o 'mbr))) (format s "~1I~_~W" (children o)))) (defclass spatial-tree-leaf-node (spatial-tree-node) ((children :initarg :records :accessor records))) (define-condition internal-error (simple-error) () (:report (lambda (c s) (format s "~@" (simple-condition-format-control c) (simple-condition-format-arguments c))))) (defmacro check (form control &rest args) `(assert ,form () 'internal-error :format-control ,control :format-arguments (list ,@args))) (define-condition protocol-error (error) ((function :initarg :function :reader protocol-error-function) (tree :initarg :tree :reader protocol-error-tree)) (:report (lambda (c s) (format s "~@" (protocol-error-function c) (protocol-error-tree c))))) (defmacro define-protocol-function (name lambda-list) (let ((method-lambda-list (loop for x in lambda-list if (eq x 'tree) collect '(tree spatial-tree) else collect x))) `(defgeneric ,name ,lambda-list (:method ,method-lambda-list (error 'protocol-error :function ',name :tree tree))))) (define-protocol-function search (object tree)) (define-protocol-function insert (object tree)) (define-protocol-function delete (object tree)) (define-protocol-function choose-leaf (r tree)) (define-protocol-function split-node (tree new node)) (defgeneric make-spatial-tree (kind &rest initargs &key &allow-other-keys)) (defgeneric check-consistency (tree) (:method-combination progn)) spatial-trees-0.2/TODO0000644000175000017500000000400210153120014014437 0ustar crhodescrhodesIssues of code quality: * MBR and MINIMUM-BOUND-OF no longer need the tree as an argument, because we precompute rectangle data for a leaf entry. * There are numerous instances of minimization loops where there is no good upper bound for the value, so that the initial value for comparison must come from the first iteration of the loop. This is dealt with in a number of different ways throughout the code, none of which is particularly elegant. * The test harness is reasonable at the moment; it could be improved, and of course more tests should be added. * The CHECK-CONSISTENCY function should be used in the test suite. However, a means needs to be developed to be able to say that the X-tree, say, obeys all of the invariants of the R-tree except for the maximum number of children of a node in certain circumstances. This subtraction of invariants appears to be a poor fit with the simple class hierarchy. * The visualiser is a reasonable barometer onto the tightness of the protocol; the current verdict is "not terribly". Aim to reduce initmate knowledge of internals in the CLIM inspector in as much as that's possible. Issues of performance: * MBR seems a little too low-level to be implemented as an (expensive, at least in SBCL) generic function. A simple profile on 2004-11-30 showed it to be the most time-expensive function in running the tests on R-trees. Maybe it should be replaced by an inline function calling the leaf-node-entry accessor or the spatial-tree-node SLOT-VALUE as appropriate. * REDUCE :KEY #'MBR (or the equivalent) takes a surprisingly long time. The compiler's understanding of REDUCE could be improved. Issues of generality: * The dynamic insertion algorithm for R+ trees is broken. It would be nice to fix it. * MORE TREES ** TV-tree ** kd-tree ** kd-B-tree ** hB-tree ** LSD-tree ** SS-tree ** SR-tree Miscellaneous issues: * The visualiser should present and describe the object in a leaf-node-entry, not just rectangles. spatial-trees-0.2/spatial-tree-viz.lisp0000644000175000017500000001467210153117562020074 0ustar crhodescrhodes(in-package :clim-user) ;;;; spatial-tree Visualization Toy. Mostly by Andy Hefner; some ;;;; modifications by Christophe Rhodes ;; For best results, use a McCLIM newer than Nov.11, 2004 :) (define-presentation-type spatial-tree-node ()) (define-presentation-type entry ()) (define-application-frame spatial-tree-viz () ((tree :initarg :tree :reader tree) (scale :initarg :scale :initform 200 :accessor scale) (expanded-nodes :initform (make-hash-table) :reader expanded-nodes)) (:panes (hierarchy-pane (make-pane 'application-pane :display-time t :end-of-page-action :allow :end-of-line-action :allow :text-style (make-text-style :sans-serif :roman :normal) :display-function 'print-tree-hierarchy)) (inspect (make-pane 'application-pane :display-time nil :end-of-page-action :allow :end-of-line-action :allow)) (viz (make-pane 'application-pane :display-time t :display-function 'draw-layout)) (zoom-in (make-pane 'push-button :label "Zoom In" :activate-callback 'zoom-in)) (zoom-out (make-pane 'push-button :label "Zoom Out" :activate-callback 'zoom-out))) (:command-table (spatial-tree-viz)) (:pointer-documentation t) (:layouts (default (vertically () (horizontally () (labelling (:label "Hierarchy") (scrolling (:scroll-bars :vertical) hierarchy-pane)) (make-pane 'clim-extensions:box-adjuster-gadget) (labelling (:label "Layout" :width +fill+) (vertically () (scrolling (:suggested-width 500 :suggested-height 500) viz) (horizontally () zoom-in zoom-out)))) (make-pane 'clim-extensions:box-adjuster-gadget) (labelling (:label "Details") (scrolling (:suggested-width 600) inspect)))))) ;;; Display Code (defun print-tree-node (frame pane node &key (indent 0)) (indenting-output (pane indent) (etypecase node (spatial-trees-protocol:spatial-tree-node (with-output-as-presentation (pane node 'spatial-tree-node) (format pane "~A (~A children)~%" (type-of node) (length (spatial-trees-protocol:children node))))) (spatial-trees-impl::leaf-node-entry ;; FIXME: this should also be presented as the object in the ;; LEAF-NODE-ENTRY-DATUM slot (with-output-as-presentation (pane node 'entry) (multiple-value-call #'format pane "Rectangle (~1,2F,~1,2F)-(~1,2F,~1,2F)~%" (rect* (spatial-trees-impl::leaf-node-entry-rectangle node)))))) (when (gethash node (expanded-nodes frame)) (dolist (child (spatial-trees-protocol:children node)) (print-tree-node frame pane child :indent (+ indent 16)))))) (defun print-tree-hierarchy (frame pane) (print-tree-node frame pane (spatial-trees-protocol:root-node (tree frame)))) (defun rect* (rectangle) (values (first (rectangles:lows rectangle)) (second (rectangles:lows rectangle)) (first (rectangles:highs rectangle)) (second (rectangles:highs rectangle)))) (defun draw-layout (frame pane &optional (node (tree frame))) (etypecase node (spatial-trees-protocol:spatial-tree (with-room-for-graphics (pane :first-quadrant nil) (with-scaling (pane (scale frame)) (draw-layout frame pane (spatial-trees-protocol:root-node node)))) (change-space-requirements pane ;; FIXME: McCLIM should do this itself. :width (bounding-rectangle-width (stream-output-history pane)) :height (bounding-rectangle-height (stream-output-history pane)))) (spatial-trees-protocol:spatial-tree-leaf-node (dolist (child (spatial-trees-protocol:records node)) (draw-layout frame pane child)) (when (slot-boundp node 'spatial-trees-impl::mbr) (multiple-value-call #'draw-rectangle* pane (rect* (slot-value node 'spatial-trees-impl::mbr)) :ink +red+ :filled nil))) (spatial-trees-protocol:spatial-tree-node (dolist (child (spatial-trees-protocol:children node)) (draw-layout frame pane child)) (when (slot-boundp node 'spatial-trees-impl::mbr) (multiple-value-call #'draw-rectangle* pane (rect* (slot-value node 'spatial-trees-impl::mbr)) :ink +black+ :filled nil))) (spatial-trees-impl::leaf-node-entry (with-output-as-presentation (pane node 'entry) (multiple-value-call #'draw-rectangle* pane (rect* (spatial-trees-impl::leaf-node-entry-rectangle node)) :ink +blue+ :filled nil :line-dashes #(1 1)))))) ;;; Callbacks (defun zoom-in (pane) (declare (ignore pane)) (setf (scale *application-frame*) (* 2 (scale *application-frame*))) (redisplay-frame-pane *application-frame* (get-frame-pane *application-frame* 'viz) :force-p t)) (defun zoom-out (pane) (declare (ignore pane)) (setf (scale *application-frame*) (/ (scale *application-frame*) 2)) (redisplay-frame-pane *application-frame* (get-frame-pane *application-frame* 'viz) :force-p t)) ;;; Commands (define-spatial-tree-viz-command (com-toggle-node :name "Toggle Expand Node") ((node 'spatial-tree-node :prompt :node :gesture :select)) (if (gethash node (expanded-nodes *application-frame*)) (remhash node (expanded-nodes *application-frame*)) (setf (gethash node (expanded-nodes *application-frame*)) t)) (setf (pane-needs-redisplay (get-frame-pane *application-frame* 'hierarchy-pane)) t)) (define-spatial-tree-viz-command (com-describe-node :name "Describe Node") ((node 'spatial-tree-node :prompt :node :gesture :describe)) (describe node (get-frame-pane *application-frame* 'inspect))) (define-spatial-tree-viz-command (com-describe-entry :name "Describe Entry") ((node 'entry :prompt :node :gesture :describe)) (describe node (get-frame-pane *application-frame* 'inspect))) ;;; Foo (defun inspect-spatial-tree (tree) (run-frame-top-level (make-application-frame 'spatial-tree-viz :tree tree :pretty-name "Spatial Tree Visualizer"))) spatial-trees-0.2/r-trees.lisp0000644000175000017500000002722710153102061016241 0ustar crhodescrhodes;;;; A more-or-less direct implementation of "R-TREES: A DYNAMIC INDEX ;;;; STRUCTURE FOR SPATIAL SEARCHING", Antonin Guttman, Proc. ACM ;;;; SIGMOD Int. Conf. on Management of Data, 1984. ;;;; ;;;; Differences with the algorithms described in that paper: ;;;; ;;;; * we keep the minimum bounding rectangle of a node's children in ;;;; the node itself and not in an index pointing to the node, except ;;;; for leaves where there is one extra level of indirection (to ;;;; assist in implementation of R+-trees). ;;;; ;;;; * we implement (in SPLIT-NODE) a guarantee of the stated ;;;; invariant that each node contains at least m elements. This is ;;;; referred to in Section "4. Performance Tests", "... decreased ;;;; cost of insertion with a stricter node balance reflects the fact ;;;; that when one group becomes too full, all split algorithms simply ;;;; put the remaining elements in the other group without further ;;;; comparisons." (in-package "SPATIAL-TREES-IMPL") (defgeneric mbr (thing tree)) (defmethod mbr ((n spatial-tree-node) (tree spatial-tree)) (declare (ignore tree)) #+nil (check (not (eq n (root-node tree))) "Root of ~S asked for its MBR" tree) (slot-value n 'mbr)) (defun minimum-bound-of (objects tree) (reduce #'minimum-bound objects :key (lambda (x) (mbr x tree)))) (defstruct leaf-node-entry rectangle datum) (defmethod mbr ((o leaf-node-entry) (tree spatial-tree)) (declare (ignore tree)) (leaf-node-entry-rectangle o)) (defclass r-tree (spatial-tree) ()) (defmethod make-spatial-tree ((kind (eql :r)) &rest initargs) (apply #'make-instance 'r-tree :root-node (make-instance 'spatial-tree-leaf-node :records nil) initargs)) ;;; 3.1. Searching (defmethod search ((o t) (tree r-tree)) (search (funcall (rectfun tree) o) tree)) (defmethod search ((r rectangle) (tree r-tree)) (labels ((%search (r node) (cond ((typep node 'spatial-tree-leaf-node) (let (result) (dolist (entry (records node) (nreverse result)) (when (intersectp r (leaf-node-entry-rectangle entry)) (push (leaf-node-entry-datum entry) result))))) (t (let (result) (dolist (child (children node) result) (when (intersectp r (mbr child tree)) (setq result (append (%search r child) result))))))))) (let ((root (root-node tree))) (%search r root)))) ;;; 3.2. Insertion (defmethod choose-leaf (r (tree r-tree)) (labels ((%choose-leaf (r node) (cond ((typep node 'spatial-tree-leaf-node) node) (t (do* ((children (children node) (cdr children)) (child (car children) (car children)) (candidate child) (min-extension (- (area (minimum-bound (mbr child tree) (mbr r tree))) (area (mbr child tree))))) ((null children) (%choose-leaf r candidate)) (let* ((new-area (area (minimum-bound (mbr child tree) (mbr r tree)))) (old-area (area (mbr child tree))) (extension (- new-area old-area))) (when (or (< extension min-extension) (and (= extension min-extension) (< old-area (area (mbr candidate tree))))) (setf min-extension extension candidate child)))))))) (let ((n (root-node tree))) (%choose-leaf r n)))) (defmethod insert ((r t) (tree r-tree)) (let* ((r (make-leaf-node-entry :datum r :rectangle (funcall (rectfun tree) r))) (leaf-node (choose-leaf r tree))) (cond ((< (length (records leaf-node)) (max-per-node tree)) (push r (records leaf-node)) (adjust-tree tree leaf-node)) (t (let ((new-node (split-node tree r leaf-node))) (check (<= (min-per-node tree) (length (records new-node)) (max-per-node tree)) "invariant (1) violated for the new node ~S in ~S" new-node tree) (check (<= (min-per-node tree) (length (records leaf-node)) (max-per-node tree)) "invariant (1) violated for the old node ~S in ~S" leaf-node tree) (let ((new (adjust-tree tree leaf-node new-node))) (when new (let ((new-root (make-instance 'spatial-tree-node :children (list (root-node tree) new)))) (setf (parent (root-node tree)) new-root (root-node tree) new-root (parent new) new-root)))))))) tree) (defgeneric adjust-tree (tree node &optional new)) (defmethod adjust-tree ((tree r-tree) node &optional new) (cond ((eq node (root-node tree)) new) (t (setf (slot-value node 'mbr) (minimum-bound-of (children node) tree)) (let ((parent (parent node))) (if new (cond ((< (length (children parent)) (max-per-node tree)) (push new (children parent)) (setf (parent new) parent) (adjust-tree tree parent)) (t (let ((new-parent (split-node tree new parent))) (check (<= (min-per-node tree) (length (children new-parent)) (max-per-node tree)) "invariant (3) violated for the new parent node ~S in ~S" new-parent tree) (check (<= (min-per-node tree) (length (children parent)) (max-per-node tree)) "invariant (3) violated for the old parent node ~S in ~S" parent tree) (dolist (child (children parent)) (setf (parent child) parent)) (dolist (child (children new-parent)) (setf (parent child) new-parent)) (adjust-tree tree parent new-parent)))) (adjust-tree tree parent)))))) ;;; 3.3. Deletion (defmethod delete ((r t) (tree r-tree)) (let ((leaf-node (find-leaf r (root-node tree) tree))) (when leaf-node (setf (records leaf-node) (remove r (records leaf-node) :key #'leaf-node-entry-datum)) (condense-tree leaf-node tree) (unless (typep (root-node tree) 'spatial-tree-leaf-node) (when (null (cdr (children (root-node tree)))) (check (car (children (root-node tree))) "non-leaf root node with no children") (setf (root-node tree) (car (children (root-node tree)))) (slot-makunbound (root-node tree) 'parent) (slot-makunbound (root-node tree) 'mbr))) tree))) (defun find-leaf (obj node tree) (labels ((%find-leaf (leaf-entry node tree) (if (typep node 'spatial-tree-leaf-node) (when (member (leaf-node-entry-datum leaf-entry) (records node) :key #'leaf-node-entry-datum) (return-from find-leaf node)) (dolist (entry (children node)) (when (intersectp (mbr leaf-entry tree) (mbr entry tree)) (%find-leaf leaf-entry entry tree)))))) (%find-leaf (make-leaf-node-entry :datum obj :rectangle (funcall (rectfun tree) obj)) node tree))) (defun condense-tree (node tree) (labels ((all-leaves-below (node) (if (typep node 'spatial-tree-leaf-node) (records node) (apply #'append (mapcar #'all-leaves-below (children node)))))) (do ((node node (parent node)) (q nil)) ((eq node (root-node tree)) (dolist (orphan q) ;; NOTE: this interpretation (reinsert every leaf) ;; disagrees with BKSS (R*-trees), section 4.3, "... is ;; based on the ability of the insert routine to insert ;; entries on every level of the tree as already required ;; by the deletion algorithm [Gut 84]." (dolist (oleaf (all-leaves-below orphan)) (insert (leaf-node-entry-datum oleaf) tree)))) (cond ((< (length (children node)) (min-per-node tree)) (setf (children (parent node)) (remove node (children (parent node)))) (push node q)) (t (setf (slot-value node 'mbr) (minimum-bound-of (children node) tree))))))) ;;; 3.5. Node Splitting (defun d (r1 r2 tree) (- (area (minimum-bound (mbr r1 tree) (mbr r2 tree))) (area (mbr r1 tree)) (area (mbr r2 tree)))) (defun pick-seeds (entries tree) (do* ((entry1 (car entries) (car entries)) (entries (cdr entries) (cdr entries)) (maxentry1 entry1) (maxentry2 (car entries)) (maxd (d maxentry1 maxentry2 tree))) ((null entries) (values maxentry1 maxentry2)) (dolist (entry2 entries) (when (> (d entry1 entry2 tree) maxd) (setf maxd (d entry1 entry2 tree) maxentry1 entry1 maxentry2 entry2))))) (defun pick-next (entries node new-node tree) (let* ((maxentry (car entries)) (maxdelta (- (d maxentry new-node tree) (d maxentry node tree)))) (dolist (entry (cdr entries) (values maxentry maxdelta)) (let ((delta (- (d entry new-node tree) (d entry node tree)))) (when (> (abs delta) (abs maxdelta)) (setf maxentry entry maxdelta delta)))))) (defun make-node-like (node) (make-instance (class-of node))) (defmethod split-node ((tree r-tree) new node) (let ((new-node (make-node-like node)) (entries (cons new (children node)))) (multiple-value-bind (s1 s2) (pick-seeds entries tree) (setf (children node) (list s1) (slot-value node 'mbr) (mbr s1 tree)) (setf (children new-node) (list s2) (slot-value new-node 'mbr) (mbr s2 tree)) (do ((entries (remove s1 (remove s2 entries)))) ((null entries) new-node) (multiple-value-bind (maxentry maxdelta) (pick-next entries node new-node tree) (cond ((or (plusp maxdelta) (and (zerop maxdelta) (or (< (area (mbr node tree)) (area (mbr new-node tree))) (and (= (area (mbr node tree)) (area (mbr new-node tree))) (> (length (children new-node)) (length (children node))))))) (push maxentry (children node)) (setf (slot-value node 'mbr) (minimum-bound-of (children node) tree))) (t (push maxentry (children new-node)) (setf (slot-value new-node 'mbr) (minimum-bound-of (children new-node) tree)))) (setf entries (remove maxentry entries)) ;; the extra bit (see notes at head of file) (when (= (length entries) (- (min-per-node tree) (length (children node)))) (setf (children node) (append entries (children node)) (slot-value node 'mbr) (minimum-bound-of (children node) tree)) (return new-node)) (when (= (length entries) (- (min-per-node tree) (length (children new-node)))) (setf (children new-node) (append entries (children new-node)) (slot-value new-node 'mbr) (minimum-bound-of (children new-node) tree)) (return new-node))))))) spatial-trees-0.2/rplus-trees.lisp0000644000175000017500000001665110153070013017145 0ustar crhodescrhodes;;; Modifications to the basic R-tree following "THE R+-TREE: A ;;; DYNAMIC INDEX FOR MULTI-DIMENSIONAL OBJECTS", Sellis, Roussopoulos ;;; and Faloutsos, Proc. 15th Int. Conf. on Very Large Databases, ;;; 1987 ;;; ;;; By this stage, not very much of the original R-tree is left, mind ;;; you. (in-package "SPATIAL-TREES-IMPL") (defclass r+-tree (r-tree) ((fill-factor :initarg :fill-factor :accessor fill-factor))) (defmethod make-spatial-tree :around ((kind (eql :r+)) &rest initargs) (declare (ignore initargs)) (cerror "Make an R+-tree nevertheless" "R+-trees' dynamic insertion is broken.") (call-next-method)) (defmethod make-spatial-tree ((kind (eql :r+)) &rest initargs) (apply #'make-instance 'r+-tree :root-node (make-instance 'spatial-tree-leaf-node :records nil) initargs)) (defmethod initialize-instance :after ((tree r+-tree) &rest args) (declare (ignore args)) (unless (slot-boundp tree 'fill-factor) (setf (slot-value tree 'fill-factor) (round (* (max-per-node tree) 1/3))))) (defmethod search :around ((r rectangle) (tree r+-tree)) (remove-duplicates (call-next-method))) ;;; 3.3. Insertion (defmethod insert ((r t) (tree r+-tree)) (labels ((%insert (r node tree) (cond ((typep node 'spatial-tree-leaf-node) (if (>= (length (records node)) (max-per-node tree)) (split-node tree r node) (push r (records node)))) (t (dolist (child (children node)) (when (intersectp (mbr child tree) (mbr r tree)) (%insert r child tree))))))) (%insert r (root-node tree) tree) tree)) ;;; 3.4. Deletion (defun unlink-node (node tree) (cond ((eq node (root-node tree))) (t (let ((parent (parent node))) (cond ((= (length (children parent)) 1) (if (eq parent (root-node tree)) (setf (root-node tree) (make-instance 'spatial-tree-leaf-node :records nil)) (unlink-node parent tree))) (t (setf (children parent) (remove node (children parent)) (slot-value parent 'mbr) (minimum-bound-of (children parent) tree)))))))) (defmethod delete ((r t) (tree r+-tree)) (labels ((%delete (r node tree) (cond ((typep node 'spatial-tree-leaf-node) (setf (records node) (remove r (records node))) (when (null (records node)) (unlink-node node tree))) (t (dolist (child (children node)) (when (intersectp (mbr child tree) (mbr r tree)) (%delete r child tree))))))) (%delete r (root-node tree) tree) tree)) ;;; 3.5. Node Splitting (defun cost (r1 r2) (+ (area r1) (area r2))) (defun generate-partition-rectangles (entries tree) (let ((mbr (minimum-bound-of entries tree))) (do* ((lows (lows (mbr (car entries) tree)) (cdr lows)) (axis 0 (1+ axis)) (mincost) (low) (high)) ((null lows) (values low high)) (let ((sorted (sort (copy-list entries) #'< :key (lambda (x) (nth axis (lows (mbr x tree))))))) (cond ;; FIXME: this is the wrong way to write a loop which needs ;; initialization. There are a number of these dotted around ;; now. ((null mincost) (let* ((one (subseq sorted 0 (fill-factor tree))) (two (subseq sorted (fill-factor tree))) (new-low-coordinate (/ (+ (nth axis (highs (mbr (car (last one)) tree))) (nth axis (lows (mbr (car two) tree)))) 2))) ;; FIXME: this does not guarantee optimality, because if ;; one and two do not overlap we can shrink this high ;; rectangle to the MBR of two. (setf low (make-rectangle :lows (lows mbr) :highs (substitute-if new-low-coordinate (constantly t) (highs mbr) :start axis :count 1)) high (make-rectangle :lows (substitute-if new-low-coordinate (constantly t) (lows mbr) :start axis :count 1) :highs (highs mbr)) mincost (cost low high)))) (t (let* ((one (subseq sorted 0 (fill-factor tree))) (two (subseq sorted (fill-factor tree))) (new-low-coordinate (/ (+ (nth axis (highs (mbr (car (last one)) tree))) (nth axis (lows (mbr (car two) tree)))) 2)) (new-low (make-rectangle :lows (lows mbr) :highs (substitute-if new-low-coordinate (constantly t) (highs mbr) :start axis :count 1))) (new-high (make-rectangle :lows (substitute-if new-low-coordinate (constantly t) (lows mbr) :start axis :count 1) :highs (highs mbr))) (cost (cost new-low new-high))) (when (< cost mincost) (setf mincost cost low new-low high new-high))))))))) (defun split-node-by (tree node entries rone rtwo) (let ((new-node (make-node-like node))) (setf (children node) nil (children new-node) nil) (dolist (entry entries) (cond ((null (intersection rone (mbr entry tree))) (push entry (children new-node)) (unless (typep node 'spatial-tree-leaf-node) (setf (parent entry) new-node))) ((null (intersection rtwo (mbr entry tree))) (push entry (children node)) (unless (typep node 'spatial-tree-leaf-node) (setf (parent entry) node))) ((typep node 'spatial-tree-leaf-node) (push entry (records new-node)) (push entry (records node))) (t (let ((new (split-node-by tree entry (children entry) rone rtwo))) (push new (children new-node)) (setf (parent new) new-node) (push entry (children node)) (setf (parent entry) node))))) (setf (slot-value node 'mbr) (intersection rone (minimum-bound-of (children node) tree)) (slot-value new-node 'mbr) (intersection rtwo (minimum-bound-of (children new-node) tree))) new-node)) (defmethod split-node ((tree r+-tree) new node) (let ((entries (cons new (children node)))) (multiple-value-bind (rone rtwo) (generate-partition-rectangles entries tree) (let ((new-node (split-node-by tree node entries rone rtwo))) (if (eq (root-node tree) node) (let ((new-root (make-instance 'spatial-tree-node :children (list node new-node)))) (setf (root-node tree) new-root (parent node) new-root (parent new-node) new-root)) (let ((pr (parent node))) (if (>= (length (children pr)) (max-per-node tree)) (split-node tree new-node pr) (progn (push new-node (children pr)) (setf (parent new-node) pr))))))))) spatial-trees-0.2/greene-trees.lisp0000644000175000017500000000671710150655132017257 0ustar crhodescrhodes;;; Modification to the basic R-tree version of SPLIT-NODE to use ;;; Greene's algorithm from "An Implementation and Performance ;;; Analysis of Spatial Data Access Methods", Diane Greene, Proc. 5th ;;; IEEE Int. Conf. on Data Engineering, 1989. (in-package "SPATIAL-TREES-IMPL") (defclass greene-tree (r-tree) ()) (defmethod make-spatial-tree ((kind (eql :greene)) &rest initargs) (apply #'make-instance 'greene-tree :root-node (make-instance 'spatial-tree-leaf-node :records nil) initargs)) (defun choose-axis (entries node tree) (multiple-value-bind (seed1 seed2) (pick-seeds entries tree) (let ((lows1 (lows (mbr seed1 tree))) (highs1 (highs (mbr seed1 tree))) (lows2 (lows (mbr seed2 tree))) (highs2 (highs (mbr seed2 tree))) (mbr (if (slot-boundp node 'mbr) (mbr node tree) (minimum-bound-of (children node) tree)))) (let ((max-dist (/ (if (> (car highs1) (car highs2)) (- (car lows1) (car highs2)) (- (car lows2) (car highs1))) ;; erm, so what if a node has zero extent in ;; a given dimension? (- (car (highs mbr)) (car (lows mbr))))) (max-axis 0)) (do ((lows1 (cdr lows1) (cdr lows1)) (highs1 (cdr highs1) (cdr highs1)) (lows2 (cdr lows2) (cdr lows2)) (highs2 (cdr highs2) (cdr highs2)) (lowsmbr (cdr (lows mbr)) (cdr lowsmbr)) (highsmbr (cdr (highs mbr)) (cdr highsmbr)) (axis 1 (1+ axis))) ((null lows1) max-axis) (let ((distance (/ (if (> (car highs1) (car highs2)) (- (car lows1) (car highs2)) (- (car lows2) (car highs1))) (- (car highsmbr) (car lowsmbr))))) (when (> distance max-dist) (setf max-dist distance max-axis axis)))))))) (defun distribute (entries axis node new-node tree) (let* ((length (length entries)) (entries (sort entries #'< :key (lambda (x) (nth axis (lows (mbr x tree))))))) (setf (children node) (subseq entries 0 (truncate length 2)) (slot-value node 'mbr) (minimum-bound-of (children node) tree)) (setf (children new-node) (subseq entries (truncate (1+ length) 2)) (slot-value new-node 'mbr) (minimum-bound-of (children new-node) tree)) (when (oddp length) (let ((elt (nth (truncate length 2) entries))) (let ((area1 (area (mbr node tree))) (area2 (area (mbr new-node tree)))) (let* ((newmb1 (minimum-bound (mbr node tree) (mbr elt tree))) (newmb2 (minimum-bound (mbr new-node tree) (mbr elt tree))) (newarea1 (area newmb1)) (newarea2 (area newmb2))) (if (< (- newarea2 area2) (- newarea1 area1)) (setf (children new-node) (cons elt (children new-node)) (slot-value new-node 'mbr) newmb2) (setf (children node) (cons elt (children node)) (slot-value node 'mbr) newmb1)))))))) (defmethod split-node ((tree greene-tree) new node) (let ((new-node (make-node-like node)) (entries (cons new (children node)))) (let ((axis (choose-axis entries node tree))) (distribute entries axis node new-node tree) new-node))) spatial-trees-0.2/spatial-tree-test.lisp0000644000175000017500000000716010153102061020221 0ustar crhodescrhodes;;; Somewhat rudimentary tests of external functionality (in-package "SPATIAL-TREES-IMPL") (defvar *kinds* '(:r :greene :r* :x)) (defun make-random-rectangle (&optional (x-bias 0.0) (y-bias 0.0)) (let* ((lx (+ (random 1.0) x-bias)) (ly (+ (random 1.0) y-bias)) (hx (+ (random 1.0) lx)) (hy (+ (random 1.0) ly))) (make-rectangle :lows (list lx ly) :highs (list hx hy)))) (dolist (kind *kinds*) (format *trace-output* "~&Random search for kind ~S..." kind) (finish-output *trace-output*) (let* ((list (loop repeat 1000 collect (make-random-rectangle))) (tree (make-spatial-tree kind :rectfun #'identity))) (dolist (r list) (insert r tree)) (let* ((r (make-random-rectangle)) (result (search r tree)) (expected (remove-if-not (lambda (x) (intersectp x r)) list))) (unless (null (set-difference result expected :key (lambda (x) (list (lows x) (highs x))) :test #'equal)) (error "aargh: ~S and ~S differ" result expected)))) (format *trace-output* " passed.~%")) (dolist (kind *kinds*) (format *trace-output* "~&Trisected search for kind ~S..." kind) (finish-output *trace-output*) (let* ((n 1000) (list (loop repeat n collect (make-random-rectangle) collect (make-random-rectangle -2.0 -2.0) collect (make-random-rectangle 2.0 2.0))) (tree (make-spatial-tree kind :rectfun #'identity))) (dolist (r list) (insert r tree)) (let ((r (make-rectangle :lows '(0.0 0.0) :highs '(1.0 1.0)))) ;; FIXME: find a way to test the relative speed of the following ;; (sbcl-specifically if necessary). (search r tree) (remove-if-not (lambda (x) (intersectp x r)) list) (assert (= (length (search r tree)) n)))) (format *trace-output* " passed.~%")) (dolist (kind *kinds*) (format *trace-output* "~&Arbitrary object search for kind ~S..." kind) (finish-output *trace-output*) (let* ((n 100) (list (loop repeat n for r = (make-random-rectangle) collect (cons (lows r) (highs r)))) (rectfun (lambda (x) (make-rectangle :lows (car x) :highs (cdr x)))) (tree (make-spatial-tree kind :rectfun rectfun))) (dolist (r list) (insert r tree)) (let* ((r (make-random-rectangle)) (result (search r tree)) (expected (remove-if-not (lambda (x) (intersectp (funcall rectfun x) r)) list))) (unless (null (set-difference result expected :key (lambda (x) (let ((r (funcall rectfun x))) (list (lows r) (highs r)))) :test #'equal)) (error "aargh: ~S and ~S differ" result expected)))) (format *trace-output* " passed.~%")) (dolist (kind *kinds*) (format *trace-output* "~&Deletion test for kind ~S..." kind) (finish-output *trace-output*) (let* ((n 100) (list (loop repeat n collect (make-random-rectangle))) (tree (make-spatial-tree kind :rectfun #'identity))) (dolist (r list) (insert r tree)) (dolist (r (cdr list)) (delete r tree)) (let* ((results (search (car list) tree)) (length (length results))) (unless (= (length results) 1) (error "aargh: wrong amount of stuff (~D entries) in ~S" length tree)))) (format *trace-output* " passed.~%"))spatial-trees-0.2/rectangles.lisp0000644000175000017500000000771510421220726017016 0ustar crhodescrhodes(in-package "SPATIAL-TREES-IMPL") ;;; Because of the need to represent infinite dimensions, a bound is ;;; either cl:- (representing negative infinity), cl:+ (representing ;;; positive infinity) or a number (representing itself). All bounds ;;; are inclusive at present; it's possible that this should change at ;;; some point, but solutions of the form "lower bound -> inclusive, ;;; upper -> exclusive" are undesireable for a number of reasons: ;;; chiefly introduction of an asymmetry into an otherwise symmetric ;;; space, and an inability to represent point data. (defun bound= (x y) (case x ((- +) (eql y x)) (t (case y ((- +) nil) (t (= x y)))))) (defun bound< (x y) (case x ((-) (not (eql y '-))) ((+) nil) (t (case y ((-) nil) ((+) t) (t (< x y)))))) (defun bound<= (x y) (case x ((-) t) ((+) (eql y '+)) (t (case y ((-) nil) ((+) t) (t (<= x y)))))) (defun boundmax (x y) (if (bound< x y) y x)) (defun boundmin (x y) (if (bound< x y) x y)) #+(or) (progn (defclass rectangle () ((lows :initarg :lows :reader lows) (highs :initarg :highs :reader highs))) (defmethod initialize-instance :after ((o rectangle) &rest args) (declare (ignore args)) #+slow (unless (every #'bound<= (lows o) (highs o)) (error "Bad coordinates for rectangle: ~S ~S" (lows o) (highs o)))) (defun make-rectangle (&key lows highs) (make-instance 'rectangle :lows lows :highs highs))) #+(and) (progn (defstruct (rectangle (:conc-name nil) (:constructor %make-rectangle (lows highs))) (lows nil :read-only t) (highs nil :read-only t)) (defun make-rectangle (&key lows highs) #+slow (unless (every #'bound<= lows highs) (error "Bad coordinates for rectangle: ~S ~S" lows highs)) (%make-rectangle lows highs))) (defmethod print-object ((o rectangle) s) (print-unreadable-object (o s) (format s "(~{~D~^,~}) - (~{~D~^,~})" (lows o) (highs o)))) (define-condition rectangle-infinite (error) ((rectangle :initarg :rectangle :reader rectangle-infinite-rectangle)) (:report (lambda (c s) (format s "The rectangle ~S is infinite in at least one dimension" (rectangle-infinite-rectangle c))))) (defun %intersection/1d (l1 h1 l2 h2) (cond ((and (bound<= l1 l2) (bound<= l2 h1)) (cons l2 (boundmin h1 h2))) ((and (bound<= l2 l1) (bound<= l1 h2)) (cons l1 (boundmin h1 h2))))) (defgeneric intersectp (one two)) (defmethod intersectp ((r1 rectangle) (r2 rectangle)) (every #'%intersection/1d (lows r1) (highs r1) (lows r2) (highs r2))) (defgeneric intersection (one two)) (defmethod intersection ((r1 rectangle) (r2 rectangle)) (let ((intersections (mapcar #'%intersection/1d (lows r1) (highs r1) (lows r2) (highs r2)))) (make-rectangle :lows (mapcar (lambda (x) (when (null x) (return-from intersection nil)) (car x)) intersections) :highs (mapcar #'cdr intersections)))) (defgeneric minimum-bound (one two)) (defmethod minimum-bound ((r1 rectangle) (r2 rectangle)) (make-rectangle :lows #+slow (mapcar #'boundmin (lows r1) (lows r2)) (loop for l1 in (lows r1) for l2 in (lows r2) collect (boundmin l1 l2)) :highs #+slow (mapcar #'boundmax (highs r1) (highs r2)) (loop for h1 in (highs r1) for h2 in (highs r2) collect (boundmax h1 h2)))) (defgeneric area (object)) (defmethod area ((r rectangle)) #+slow ; unbearably slow and consy(!) (reduce #'* (mapcar #'- (highs r) (lows r))) (do* ((lows (lows r) (cdr lows)) (low (car lows) (car lows)) (highs (highs r) (cdr highs)) (high (car highs) (car highs)) (result 1)) ((null lows) result) (when (or (symbolp high) (symbolp low)) (error 'rectangle-infinite :rectangle r)) (setf result (* result (- high low))))) spatial-trees-0.2/x-trees.lisp0000644000175000017500000002612010153067145016253 0ustar crhodescrhodes;;; Modifications to the R*-tree as in "The X-tree: An Index Structure ;;; for High-Dimensional Data", Berchtold, Keim and Kriegel, ;;; Proc. 22th Int. Conf. on Very Large Databases, 1996 (in-package "SPATIAL-TREES-IMPL") (defclass x-tree (r*-tree) ((max-overlap :initarg :max-overlap :reader max-overlap))) (defmethod initialize-instance :after ((tree x-tree) &rest args) (declare (ignore args)) (unless (slot-boundp tree 'max-overlap) (setf (slot-value tree 'max-overlap) 1/5))) (defmethod make-spatial-tree ((kind (eql :x)) &rest initargs) (apply #'make-instance 'x-tree :root-node (make-instance 'spatial-tree-leaf-node :records nil) initargs)) (defclass x-tree-node (spatial-tree-node) ((split-tree :initarg :split-tree :accessor split-tree))) ;;; Do we actually need to keep track of what is and what isn't a ;;; supernode? I'm not sure that we do... (defclass x-tree-supernode (x-tree-node) ()) ;;; FIXME: leaf supernodes (and leaf nodes) don't need a split tree. (defclass x-tree-leaf-supernode (x-tree-supernode spatial-tree-leaf-node) ()) ;;; Split trees. ;;; ;;; In the X-tree algorithms, we are required to store the 'split ;;; history' of a node. In typical academic fashion, this is ;;; extremely badly explained in the paper itself; I think I've ;;; reconstructed what's necessary, but it's not obvious that it's a ;;; huge win. ;;; ;;; In clearer terms, then, when we split a node, we record in the ;;; node's parent (which is possibly the new root node of the tree) ;;; the split in a 'split tree', which we represent using ;;; non-NULL-terminated conses. When a node overflows, we replace it ;;; in its parent's split tree with a cons of the original node and ;;; the new one; when the root node overflows, the new root node ;;; acquires a split tree of (old . new). ;;; ;;; A new non-leaf node, meanwhile, must result from a split of a ;;; previous such non-leaf node with its own split-tree information. ;;; We construct two new split trees from the original node's split ;;; tree, such that the tree structure is preserved as much as ;;; possible while retaining only those children contained in the ;;; redistributed nodes. ;;; ;;; When a node comes to be split, one potentially good split of its ;;; children is into the two sets defined by its left- and right-split ;;; trees; this is exploited, unless the split is too unbalanced, if ;;; the ordinary topological split fails to find a sufficiently good ;;; partition. (defun find-cons-with-leaf (object conses) (cond ((atom conses) nil) ((eq (car conses) object) conses) ((eq (cdr conses) object) conses) (t (or (find-cons-with-leaf object (car conses)) (find-cons-with-leaf object (cdr conses)))))) (defun leaves-of-split-tree (split-tree) (cond ((atom split-tree) (list split-tree)) (t (append (leaves-of-split-tree (car split-tree)) (leaves-of-split-tree (cdr split-tree)))))) (defun split-tree-from-set (set split-tree) (cond ((atom split-tree) (find split-tree set)) (t (let ((car (split-tree-from-set set (car split-tree))) (cdr (split-tree-from-set set (cdr split-tree)))) (cond ((null car) cdr) ((null cdr) car) (t (cons car cdr))))))) (defvar *split*) ;;; Figure 7: X-tree Insertion Algorithm for Directory Nodes (defmethod adjust-tree ((tree x-tree) node &optional new) (check (or (null new) (or (and (typep node 'spatial-tree-leaf-node) (typep new 'spatial-tree-leaf-node)) (and (not (typep node 'spatial-tree-leaf-node)) (not (typep new 'spatial-tree-leaf-node))))) "oh dear") (cond ((eq node (root-node tree)) new) (t (setf (slot-value node 'mbr) (minimum-bound-of (children node) tree)) (let ((parent (parent node))) (if new (cond ((< (length (children parent)) (max-per-node tree)) (push new (children parent)) (setf (parent new) parent) (let ((cons (find-cons-with-leaf node (split-tree parent)))) (if (eq node (car cons)) (rplaca cons (cons node new)) (progn (check (eq node (cdr cons)) "Aargh1") (rplacd cons (cons node new))))) (adjust-tree tree parent)) (t (let ((cons (find-cons-with-leaf node (split-tree parent)))) (if (eq node (car cons)) (rplaca cons (cons node new)) (progn (check (eq node (cdr cons)) "Aargh2") (rplacd cons (cons node new))))) (let ((new-parent (let ((*split* node)) (split-node tree new parent)))) (dolist (child (children parent)) (setf (parent child) parent)) (when new-parent (dolist (child (children new-parent)) (setf (parent child) new-parent))) (adjust-tree tree parent new-parent)))) (adjust-tree tree parent)))))) (defmethod insert ((o t) (tree x-tree)) (let* ((entry (make-leaf-node-entry :datum o :rectangle (funcall (rectfun tree) o))) (node (choose-subtree entry (root-node tree) (height tree) tree))) (cond ((< (length (children node)) (max-per-node tree)) (push entry (children node)) (adjust-tree tree node)) (t (let ((new-node (split-node tree entry node))) (let ((new (adjust-tree tree node new-node))) (when new (let ((new-root (make-instance 'x-tree-node :children (list (root-node tree) new)))) (setf (parent (root-node tree)) new-root (split-tree new-root) (cons (root-node tree) new) (root-node tree) new-root (parent new) new-root))))))) tree)) ;;; Figure 8: X-tree Split Algorithm for Directory Nodes (defmethod split-node ((tree x-tree) new node) (let ((new-node (make-node-like node)) (entries (cons new (children node)))) (let ((axis (choose-split-axis entries tree))) (multiple-value-bind (one two) (choose-split-index entries axis tree) (let* ((bone (minimum-bound-of one tree)) (btwo (minimum-bound-of two tree)) (intersection (intersection bone btwo)) (mb (minimum-bound bone btwo))) (cond ((or (null intersection) (< (area intersection) (* (area mb) (max-overlap tree)))) (setf (children node) one (slot-value node 'mbr) bone) (when (> (length one) (max-per-node tree)) (check (typep node 'x-tree-supernode) "AARGH")) (setf (children new-node) two (slot-value new-node 'mbr) btwo) (when (> (length two) (max-per-node tree)) (change-class new-node (if (typep node 'spatial-tree-leaf-node) 'x-tree-leaf-supernode 'x-tree-supernode))) (when (< (length two) (max-per-node tree)) (change-class new-node (if (typep node 'spatial-tree-leaf-node) 'spatial-tree-leaf-node 'x-tree-node))) (unless (typep node 'spatial-tree-leaf-node) (let ((split-tree (split-tree node))) (setf (split-tree node) (split-tree-from-set one split-tree) (split-tree new-node) (split-tree-from-set two split-tree)))) new-node) ((and (not (typep node 'spatial-tree-leaf-node)) (let ((split-tree (split-tree node))) (destructuring-bind (one . two) split-tree (let ((l1 (leaves-of-split-tree one)) (l2 (leaves-of-split-tree two))) (if (find *split* l1) (push new l1) (progn (check (find *split* l2) "Missing node!!") (push new l2))) (and (>= (min-per-node tree) (length l1)) (>= (min-per-node tree) (length l2)) (progn (setf (children node) l1 (slot-value node 'mbr) (minimum-bound-of l1 tree) (split-tree node) (if (find new l1) (let ((cons (find-cons-with-leaf *split* one))) (if (eq (car cons) *split*) (rplaca cons (cons *split* new)) (rplacd cons (cons *split* new))) one) one)) (setf (children new-node) l2 (slot-value new-node 'mbr) (minimum-bound-of l2 tree) (split-tree node) (if (find new l2) (let ((cons (find-cons-with-leaf *split* two))) (if (eq (car cons) *split*) (rplaca cons (cons *split* new)) (rplacd cons (cons *split* new))) two) two)) new-node))))))) (t (change-class node (etypecase node (spatial-tree-leaf-node 'x-tree-leaf-supernode) (spatial-tree-node 'x-tree-supernode))) (push new (children node)) (when (not (typep node 'spatial-tree-leaf-node)) (let ((cons (find-cons-with-leaf *split* (split-tree node)))) (if (eq *split* (car cons)) (rplaca cons (cons *split* new)) (progn (check (eq *split* (cdr cons)) "Aargh2") (rplacd cons (cons *split* new)))))) nil))))))) (defmethod check-consistency progn ((tree x-tree)) (labels ((%check (node) (assert (or (typep node 'spatial-tree-leaf-node) (null (set-difference (children node) (leaves-of-split-tree (split-tree node)))))) (unless (typep node 'spatial-tree-leaf-node) (dolist (child (children node)) (%check child))))) (%check (root-node tree))))