Skip to content

Commit 31a8407

Browse files
authored
Better distance_2 docs (#136)
1 parent c130fe0 commit 31a8407

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

rstar/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
## Changed
77
- Fixed a stack overflow error in `DrainIterator::next`
8+
- Clarified that the distance measure in `distance_2` is not restricted to euclidean distance
89

910
# 0.11.0
1011

rstar/src/envelope.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ pub trait Envelope: Clone + PartialEq + ::core::fmt::Debug {
3333
/// Returns this envelope's area. Must be at least 0.
3434
fn area(&self) -> <Self::Point as Point>::Scalar;
3535

36-
/// Returns the euclidean distance to the envelope's border.
36+
/// Returns the squared distance between the envelope's border and a point.
37+
///
38+
/// # Notes
39+
/// - While euclidean distance will be the correct choice for most use cases, any distance metric
40+
/// fulfilling the [usual axioms](https://en.wikipedia.org/wiki/Metric_space)
41+
/// can be used when implementing this method
42+
/// - Implementers **must** ensure that the distance metric used matches that of [crate::PointDistance::distance_2]
3743
fn distance_2(&self, point: &Self::Point) -> <Self::Point as Point>::Scalar;
3844

3945
/// Returns the squared min-max distance, a concept that helps to find nearest neighbors efficiently.

rstar/src/object.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ pub trait RTreeObject {
145145
/// assert!(circle.contains_point(&[1.0, 0.0]));
146146
/// ```
147147
pub trait PointDistance: RTreeObject {
148-
/// Returns the squared euclidean distance between an object to a point.
148+
/// Returns the squared distance between an object and a point.
149+
///
150+
/// # Notes
151+
/// - While euclidean distance will be the correct choice for most use cases, any distance metric
152+
/// fulfilling the [usual axioms](https://en.wikipedia.org/wiki/Metric_space)
153+
/// can be used when implementing this method
154+
/// - Implementers **must** ensure that the distance metric used matches that of [crate::Envelope::distance_2]
149155
fn distance_2(
150156
&self,
151157
point: &<Self::Envelope as Envelope>::Point,

rstar/src/rtree.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ where
4141
///
4242
/// However, creating an r-tree is time consuming
4343
/// and runs in `O(n * log(n))`. Thus, r-trees are suited best if many queries and only few
44-
/// insertions are made. rstar also supports [bulk loading](RTree::bulk_load),
44+
/// insertions are made. `rstar` also supports [bulk loading](RTree::bulk_load),
4545
/// which cuts down the constant factors when creating an r-tree significantly compared to
4646
/// sequential insertions.
4747
///
@@ -53,16 +53,13 @@ where
5353
/// on fast insertion operations, the resulting r-trees were often suboptimally structured. Another
5454
/// heuristic, called `R*-tree` (r-star-tree), was proposed to improve the tree structure at the cost of
5555
/// longer insertion operations and is currently the crate's only implemented
56-
/// [insertion strategy].
57-
///
58-
/// ## Further reading
59-
/// For more information refer to the [wikipedia article](https://en.wikipedia.org/wiki/R-tree).
56+
/// [InsertionStrategy].
6057
///
6158
/// # Usage
6259
/// The items inserted into an r-tree must implement the [RTreeObject]
6360
/// trait. To support nearest neighbor queries, implement the [PointDistance]
6461
/// trait. Some useful geometric primitives that implement the above traits can be found in the
65-
/// [crate::primitives]x module. Several primitives in the [`geo-types`](https://docs.rs/geo-types/) crate also
62+
/// [crate::primitives] module. Several primitives in the [`geo-types`](https://docs.rs/geo-types/) crate also
6663
/// implement these traits.
6764
///
6865
/// ## Example
@@ -89,39 +86,21 @@ where
8986
/// All types implementing the [Point] trait can be used as underlying point type.
9087
/// By default, fixed size arrays can be used as points.
9188
///
92-
/// ## Type Parameters
93-
/// * `T`: The type of objects stored in the r-tree.
94-
/// * `Params`: Compile time parameters that change the r-tree's internal layout. Refer to the
95-
/// [RTreeParams] trait for more information.
96-
///
97-
/// ## Defining methods generic over r-trees
98-
/// If a library defines a method that should be generic over the r-tree type signature, make
99-
/// sure to include both type parameters like this:
100-
/// ```
101-
/// # use rstar::{RTree,RTreeObject, RTreeParams};
102-
/// pub fn generic_rtree_function<T, Params>(tree: &mut RTree<T, Params>)
103-
/// where
104-
/// T: RTreeObject,
105-
/// Params: RTreeParams
106-
/// {
107-
/// // ...
108-
/// }
109-
/// ```
110-
/// Otherwise, any user of `generic_rtree_function` would be forced to use
111-
/// a tree with default parameters.
89+
/// # Associating Data with Geometries
90+
/// Users wishing to store associated data with geometries can use [crate::primitives::GeomWithData].
11291
///
11392
/// # Runtime and Performance
11493
/// The runtime of query operations (e.g. `nearest neighbor` or `contains`) is usually
11594
/// `O(log(n))`, where `n` refers to the number of elements contained in the r-tree.
11695
/// A naive sequential algorithm would take `O(n)` time. However, r-trees incur higher
11796
/// build up times: inserting an element into an r-tree costs `O(log(n))` time.
11897
///
119-
/// ## Bulk loading
98+
/// # Bulk loading
12099
/// In many scenarios, insertion is only carried out once for many points. In this case,
121100
/// [RTree::bulk_load] will be considerably faster. Its total run time
122-
/// is still `O(log(n))`.
101+
/// is still `O(log(n))`. **Note the performance caveat related to the computation of the envelope**.
123102
///
124-
/// ## Element distribution
103+
/// # Element distribution
125104
/// The tree's performance heavily relies on the spatial distribution of its elements.
126105
/// Best performance is achieved if:
127106
/// * No element is inserted more than once
@@ -131,9 +110,33 @@ where
131110
/// For the edge case that all elements are overlapping (e.g, one and the same element
132111
/// is contained `n` times), the performance of most operations usually degrades to `O(n)`.
133112
///
113+
/// # Type Parameters
114+
/// * `T`: The type of objects stored in the r-tree.
115+
/// * `Params`: Compile time parameters that change the r-tree's internal layout. Refer to the
116+
/// [RTreeParams] trait for more information.
117+
///
118+
/// # Defining methods generic over r-trees
119+
/// If a library defines a method that should be generic over the r-tree type signature, make
120+
/// sure to include both type parameters like this:
121+
/// ```
122+
/// # use rstar::{RTree,RTreeObject, RTreeParams};
123+
/// pub fn generic_rtree_function<T, Params>(tree: &mut RTree<T, Params>)
124+
/// where
125+
/// T: RTreeObject,
126+
/// Params: RTreeParams
127+
/// {
128+
/// // ...
129+
/// }
130+
/// ```
131+
/// Otherwise, any user of `generic_rtree_function` would be forced to use
132+
/// a tree with default parameters.
133+
///
134134
/// # (De)Serialization
135135
/// Enable the `serde` feature for [Serde](https://crates.io/crates/serde) support.
136136
///
137+
/// ## Further reading
138+
/// For more information refer to the [wikipedia article](https://en.wikipedia.org/wiki/R-tree).
139+
///
137140
#[derive(Clone)]
138141
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
139142
#[cfg_attr(
@@ -209,8 +212,9 @@ where
209212
/// Bulk loading runs in `O(n * log(n))`, where `n` is the number of loaded
210213
/// elements.
211214
///
212-
/// Note that the envelope of each element will be accessed many times,
213-
/// so if that computation is expensive, consider memoizing it using [`CachedEnvelope`][crate::primitives::CachedEnvelope].
215+
/// # Note
216+
/// The envelope of each element will be accessed many times during loading. If that computation
217+
/// is expensive, **consider memoizing it** using [`CachedEnvelope`][crate::primitives::CachedEnvelope].
214218
pub fn bulk_load(elements: Vec<T>) -> Self {
215219
Self::bulk_load_with_params(elements)
216220
}

0 commit comments

Comments
 (0)