Skip to content

Commit

Permalink
improving rust setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr Martian committed Nov 1, 2024
1 parent a8cf3f5 commit 3ea09e6
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 159 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0

- name: Set up Bun
uses: oven-sh/setup-bun@v1
Expand All @@ -29,27 +32,27 @@ jobs:
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
toolchain: nightly
override: true

- name: Install Rustfmt
run: rustup component add rustfmt
run: rustup component add rustfmt --toolchain nightly

- name: Install Clippy
run: rustup component add clippy
run: rustup component add clippy --toolchain nightly

- name: Run Clippy
run: cargo clippy -- -D warnings
run: cargo clippy --workspace --package geometry -- -D warnings
shell: bash

- name: Build Rust project
run: cargo build
run: cargo +nightly build
shell: bash

- name: Check Formatting
run: cargo fmt -- --check
run: cargo +nightly fmt -- --check
shell: bash

- name: Run Rust tests
run: cargo test --lib
run: cargo +nightly test --lib
shell: bash
55 changes: 0 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
resolver = "2"
members = [
"rust/geometry",
"rust/s2cell",
"rust/uint64"
# "rust/s2cell",
# "rust/uint64"
]

default-members = [
Expand All @@ -15,7 +15,7 @@ name = "s2-tools"
version = "1.0.0"
edition = "2021"
authors = ["Craig O'Connor <[email protected]>"]
description = "Triangle mesh designed to be fast, efficient, and sphere capable."
description = "A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2."
documentation = "https://docs.rs/s2-tools"
homepage = "https://github.com/OpenS2/s2-tools"
repository = "https://github.com/OpenS2/s2-tools"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "stable"
channel = "nightly"
components = ["rustc", "rustfmt", "clippy", "rust-docs", "rust-analyzer", "miri"]
targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown", "wasm32-wasi", "aarch64-apple-darwin"]
profile = "complete"
1 change: 1 addition & 0 deletions rust/geometry/cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ keywords = ["open", "vector", "tile", "gis", "low-cost-code"]
default = ["quadratic"] # Default feature
quadratic = [] # Feature for quadratic projection
tan = [] # Feature for tangential projection
linear = [] # Feature for linear projection

[dependencies]
libm = "0.2"
Expand Down
15 changes: 12 additions & 3 deletions rust/geometry/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,11 @@ mod tests {
}
);
let point_str = serde_json::to_string(&point).unwrap();
assert_eq!(point_str, "{\"type\":\"Point3D\",\"coordinates\":[0.0,0.0,0.0],\"bbox\":[0.0,0.0,1.0,1.0,0.0,1.0]}");
assert_eq!(
point_str,
"{\"type\":\"Point3D\",\"coordinates\":[0.0,0.0,0.0],\"bbox\":[0.0,0.0,1.0,1.0,0.0,1.\
0]}"
);
let str_point: Point3DGeometry = serde_json::from_str(&point_str).unwrap();
assert_eq!(str_point, point);
}
Expand Down Expand Up @@ -1075,7 +1079,11 @@ mod tests {
}
);
let polygon_str = serde_json::to_string(&polygon).unwrap();
assert_eq!(polygon_str, "{\"type\":\"Polygon3D\",\"coordinates\":[[[0.0,0.0,0.0],[1.0,1.0,1.0],[0.0,1.0,1.0]]]}");
assert_eq!(
polygon_str,
"{\"type\":\"Polygon3D\",\"coordinates\":[[[0.0,0.0,0.0],[1.0,1.0,1.0],[0.0,1.0,1.\
0]]]}"
);
let str_polygon: Polygon3DGeometry = serde_json::from_str(&polygon_str).unwrap();
assert_eq!(str_polygon, polygon);
}
Expand Down Expand Up @@ -1127,7 +1135,8 @@ mod tests {
let multi_polygon_str = serde_json::to_string(&multi_polygon).unwrap();
assert_eq!(
multi_polygon_str,
"{\"type\":\"MultiPolygon3D\",\"coordinates\":[[[[0.0,0.0,0.0],[1.0,1.0,1.0],[0.0,1.0,1.0]]]]}"
"{\"type\":\"MultiPolygon3D\",\"coordinates\":[[[[0.0,0.0,0.0],[1.0,1.0,1.0],[0.0,1.0,\
1.0]]]]}"
);
let str_multi_polygon: MultiPolygon3DGeometry =
serde_json::from_str(&multi_polygon_str).unwrap();
Expand Down
17 changes: 2 additions & 15 deletions rust/geometry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,7 @@ impl<M> Feature<M> {
geometry: Geometry,
metadata: Option<M>,
) -> Self {
Self {
_type: "Feature".to_string(),
id,
properties,
geometry,
metadata,
}
Self { _type: "Feature".to_string(), id, properties, geometry, metadata }
}
}

Expand Down Expand Up @@ -248,14 +242,7 @@ impl<M> VectorFeature<M> {
geometry: VectorGeometry,
metadata: Option<M>,
) -> Self {
Self {
_type: "S2Feature".to_string(),
face,
id,
properties,
geometry,
metadata,
}
Self { _type: "S2Feature".to_string(), face, id, properties, geometry, metadata }
}

/// Create a new VectorFeature using an input VectorFeature. Assign new geometry if provided
Expand Down
29 changes: 19 additions & 10 deletions rust/geometry/src/lonlat.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// const S1Angle = @import("s1/angle.zig").S1Angle;
// const S2Point = @import("point.zig").S2Point;

use libm::{fabs, cos, sin, atan2, sqrt, asin};
use core::f64::consts::PI;
use crate::s2::point::S2Point;
use crate::s1::angle::S1Angle;
use crate::s2::point::S2Point;
use core::f64::consts::PI;
use libm::{asin, atan2, cos, fabs, sin, sqrt};

/// This class represents a point on the unit sphere as a pair
/// of latitude-longitude coordinates. Like the rest of the "geometry"
Expand All @@ -22,7 +22,7 @@ impl LonLat {
/// The default constructor sets the latitude and longitude to zero. This is
/// mainly useful when declaring arrays, STL containers, etc.
pub fn new(lon: f64, lat: f64) -> Self {
LonLat{ lon, lat }
LonLat { lon, lat }
}

/// Constructor. The latitude and longitude are allowed to be outside
Expand All @@ -35,7 +35,9 @@ impl LonLat {
// Convert a direction vector (not necessarily unit length) to an LonLat.
pub fn from_s2_point(p: &S2Point) -> LonLat {
let ll = LonLat::new(LonLat::latitude(p).radians, LonLat::longitude(p).radians);
if !ll.is_valid() { unreachable!(); }
if !ll.is_valid() {
unreachable!();
}
ll
}

Expand Down Expand Up @@ -95,7 +97,11 @@ impl LonLat {
self.lon_angle().degrees()
}
pub fn from_axis(&self, axis: u8) -> f64 {
if axis == 0 { self.lon } else { self.lat }
if axis == 0 {
self.lon
} else {
self.lat
}
}

/// Return the latitude or longitude coordinates in degrees.
Expand All @@ -106,8 +112,7 @@ impl LonLat {
/// Return true if the latitude is between -90 and 90 degrees inclusive
/// and the longitude is between -180 and 180 degrees inclusive.
pub fn is_valid(&self) -> bool {
fabs(self.lat_degrees()) <= (PI / 2.0) &&
fabs(self.lon_degrees()) <= PI
fabs(self.lat_degrees()) <= (PI / 2.0) && fabs(self.lon_degrees()) <= PI
}

// // Clamps the latitude to the range [-90, 90] degrees, and adds or subtracts
Expand All @@ -129,7 +134,9 @@ impl LonLat {
// cap.AddPoint(S2Point(latlon));
pub fn to_point(&self) -> S2Point {
// TODO:
if !self.is_valid() { unreachable!(); }
if !self.is_valid() {
unreachable!();
}
// S2_DLOG_IF(ERROR, !is_valid())
// << "Invalid LonLat in LonLat::ToPoint: " << *this;
let phi: f64 = self.lat;
Expand Down Expand Up @@ -157,7 +164,9 @@ impl LonLat {
// you might as well just convert both arguments to S2Points and compute the
// distance that way (which gives about 15 digits of accuracy for all
// distances).
if !self.is_valid() || !b.is_valid() { unreachable!(); }
if !self.is_valid() || !b.is_valid() {
unreachable!();
}

let lat1 = self.lat;
let lat2 = b.lat;
Expand Down
2 changes: 1 addition & 1 deletion rust/geometry/src/planets/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod earth;
pub mod earth;
8 changes: 4 additions & 4 deletions rust/geometry/src/s1/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ use core::f64::consts::PI;
/// the default copy constructor and assignment operator.
#[derive(Copy, Clone, Default, PartialEq, Debug)]
pub struct S1Angle {
pub radians: f64
pub radians: f64,
}
impl S1Angle {
pub fn new(radians: f64) -> Self {
Self{ radians }
Self { radians }
}

pub fn from_degrees(degrees: f64) -> Self {
Self{ radians: degrees * (PI / 180.0) }
Self { radians: degrees * (PI / 180.0) }
}

pub fn infinity() -> Self {
Self{ radians: f64::INFINITY }
Self { radians: f64::INFINITY }
}

pub fn degrees(&self) -> f64 {
Expand Down
2 changes: 1 addition & 1 deletion rust/geometry/src/s1/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod angle;
pub mod angle;
17 changes: 3 additions & 14 deletions rust/geometry/src/s2/cellid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,28 +650,17 @@ impl S2CellId {
let k_limit = 1. + 2.220_446_049_250_313e-16;
let u = fmax(
-k_limit,
fmin(
k_limit,
k_scale * (2. * (i as f64 - (K_MAX_SIZE as f64) / 2.) + 1.),
),
fmin(k_limit, k_scale * (2. * (i as f64 - (K_MAX_SIZE as f64) / 2.) + 1.)),
);
let v = fmax(
-k_limit,
fmin(
k_limit,
k_scale * (2. * (j as f64 - (K_MAX_SIZE as f64) / 2.) + 1.),
),
fmin(k_limit, k_scale * (2. * (j as f64 - (K_MAX_SIZE as f64) / 2.) + 1.)),
);

// Find the leaf cell coordinates on the adjacent face, and convert
// them to a cell id at the appropriate level.
let (n_face, nu, nv) = xyz_to_face_uv(&face_uv_to_xyz(face, u, v));
S2CellId::from_face_ij(
n_face,
st_to_ij(0.5 * (nu + 1.)),
st_to_ij(0.5 * (nv + 1.)),
None,
)
S2CellId::from_face_ij(n_face, st_to_ij(0.5 * (nu + 1.)), st_to_ij(0.5 * (nv + 1.)), None)
}

/// Given an S2CellID, find it's nearest neighbors associated with it
Expand Down
Loading

0 comments on commit 3ea09e6

Please sign in to comment.