Skip to content

Commit

Permalink
docs: add description
Browse files Browse the repository at this point in the history
  • Loading branch information
slavik-pastushenko committed Jan 2, 2025
1 parent b753c21 commit c7e6f5e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 43 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ jobs:
components: clippy, rustfmt
- name: Run build
run: cargo build --locked
- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2
- name: Run clippy
run: cargo clippy --all-targets --all-features --no-deps -- -D warnings
- name: Run rustfmt
Expand Down
53 changes: 50 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,71 @@ const OFFSET_NUM: usize = 5;
/// An offset index used to access the properties associated with a cluster in the data arrays.
const OFFSET_PROP: usize = 6;

/// The range of the incoming data if choosing the cartesian coordinate system
/// The range of the incoming data if choosing the cartesian coordinate system.
#[derive(Clone, Debug)]
pub struct DataRange {
/// The minimum x-coordinate value.
pub min_x: f64,

/// The minimum y-coordinate value.
pub min_y: f64,

/// The maximum x-coordinate value.
pub max_x: f64,

/// The maximum y-coordinate value.
pub max_y: f64,
}

impl DataRange {
/// Normalize the x-coordinate value to the range [0, 1].
///
/// # Arguments
///
/// - `x`: The x-coordinate value to be normalized.
///
/// # Returns
///
/// The normalized x-coordinate value.
fn normalize_x(&self, x: f64) -> f64 {
(x - self.min_x) / (self.max_x - self.min_x)
}

/// Normalize the y-coordinate value to the range [0, 1].
///
/// # Arguments
///
/// - `y`: The y-coordinate value to be normalized.
///
/// # Returns
///
/// The normalized y-coordinate value.
fn normalize_y(&self, y: f64) -> f64 {
(y - self.min_y) / (self.max_y - self.min_y)
}

/// Denormalize the x-coordinate value from the range [0, 1] to the original range.
///
/// # Arguments
///
/// - `x_scaled`: The scaled x-coordinate value to be denormalized.
///
/// # Returns
///
/// The denormalized x-coordinate value.
fn denormalize_x(&self, x_scaled: f64) -> f64 {
x_scaled * (self.max_x - self.min_x) + self.min_x
}

/// Denormalize the y-coordinate value from the range [0, 1] to the original range.
///
/// # Arguments
///
/// - `y_scaled`: The scaled y-coordinate value to be denormalized.
///
/// # Returns
///
/// The denormalized y-coordinate value.
fn denormalize_y(&self, y_scaled: f64) -> f64 {
y_scaled * (self.max_y - self.min_y) + self.min_y
}
Expand All @@ -52,8 +95,11 @@ impl DataRange {
/// Coordinate system for clustering.
#[derive(Clone, Debug)]
pub enum CoordinateSystem {
LatLng, // Choose this for geo-spatial data
Cartesian { data_range: DataRange }, // Chose this for non-geospatial (i.e. microscopy, etc.) data
/// Latitude and longitude coordinates. Used for geo-spatial data.
LatLng,

/// Cartesian coordinates. Used for non-geo-spatial (i.e. microscopy, etc.) data.
Cartesian { data_range: DataRange },
}

/// Supercluster configuration options.
Expand Down Expand Up @@ -752,6 +798,7 @@ impl Supercluster {
/// - `data`: A reference to the flat numeric arrays representing point data.
/// - `i`: The index in the data array for the cluster.
/// - `cluster_props`: A reference to a vector of cluster properties.
/// - `coordinate_system`: The coordinate system used for clustering.
///
/// # Returns
///
Expand Down
35 changes: 33 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use geojson::{Feature, FeatureCollection};
use geojson::{Feature, FeatureCollection, Value};
use std::{fs, path::Path};
use supercluster::{CoordinateSystem, Options};
use supercluster::{CoordinateSystem, DataRange, Options};

pub fn get_options(
radius: f64,
Expand All @@ -20,6 +20,37 @@ pub fn get_options(
}
}

pub fn get_data_range(data: &Vec<Feature>) -> Option<DataRange> {
let mut min_x = f64::INFINITY;
let mut min_y = f64::INFINITY;
let mut max_x = f64::NEG_INFINITY;
let mut max_y = f64::NEG_INFINITY;

for feature in data {
if let Some(geometry) = &feature.geometry {
if let Value::Point(ref coords) = geometry.value {
let x = coords[0];
let y = coords[1];
min_x = min_x.min(x);
min_y = min_y.min(y);
max_x = max_x.max(x);
max_y = max_y.max(y);
}
}
}

if min_x.is_finite() && min_y.is_finite() && max_x.is_finite() && max_y.is_finite() {
Some(DataRange {
min_x,
max_x,
min_y,
max_y,
})
} else {
None
}
}

pub fn load_places() -> Vec<Feature> {
let file_path = Path::new("./tests/common/places.json");
let json_string = fs::read_to_string(file_path).expect("places.json was not found");
Expand Down
5 changes: 2 additions & 3 deletions tests/supercluster_integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
mod common;
mod util;

use common::{
get_options, load_cartesian, load_places, load_tile_places, load_tile_places_with_min_5,
get_data_range, get_options, load_cartesian, load_places, load_tile_places,
load_tile_places_with_min_5,
};
use geojson::{Feature, Geometry, JsonObject, Value::Point};
use serde_json::json;
use supercluster::{CoordinateSystem, Supercluster};
use util::get_data_range;

#[test]
fn test_generate_clusters() {
Expand Down
33 changes: 0 additions & 33 deletions tests/util.rs

This file was deleted.

0 comments on commit c7e6f5e

Please sign in to comment.