Skip to content

Commit

Permalink
typescript types are done; added support for wgsl loading and typescr…
Browse files Browse the repository at this point in the history
…ipt parsing; lots more test coverage; space module cleaned up; rust cleaned up
  • Loading branch information
Mr Martian committed Oct 3, 2024
1 parent 4b9fdc5 commit 42f8f76
Show file tree
Hide file tree
Showing 42 changed files with 2,072 additions and 2,984 deletions.
Binary file modified bun.lockb
Binary file not shown.
54 changes: 54 additions & 0 deletions config/wgsl-loader/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { BunPlugin } from 'bun';

// TODO: Some shaders may #include inside an already #included file. This is not supported yet.

const WgslPlugin: BunPlugin = {
name: 'WGSL loader',
/**
* @param build - bun setup tool to parse wgsl files
*/
setup(build) {
build.onLoad({ filter: /\.wgsl$/ }, async (args) => {
const { path } = args;
// load the file:
const file = await Bun.file(path).text();
const basePath = path.split('/').slice(0, -1).join('/');
// for each line, if a line starts with #include, replace it with the contents of the file
const lines = file.split('\n');
const contents: string[] = [];
for (const line of lines) {
if (line.startsWith('#include')) {
let includePath = line.split(' ')[1];
includePath = includePath.slice(0, -1);
const includeFile = await Bun.file(`${basePath}/${includePath}`).text();
contents.push(includeFile);
} else {
contents.push(line);
}
}
const result = contents.join('\n');
return {
contents: `export default "${sanitizeStringForExport(result)}"`,
loader: 'js',
};
});
},
};

/**
* @param str - string to sanitize
* @returns sanitized string
*/
function sanitizeStringForExport(str: string): string {
// Remove single-line comments
str = str.replace(/\/\/.*$/gm, '');
// Remove multi-line comments
str = str.replace(/\/\*[\s\S]*?\*\//g, '');
// Replace line breaks with '\n'
str = str.replace(/\n/g, '\\n');
// Escape double quotes
str = str.replace(/"/g, '\\"');
return str;
}

export default WgslPlugin;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@types/bun": "^1.1.10",
"@types/node": "^22.5.5",
"@types/tmp": "^0.2.6",
"@webgpu/types": "^0.1.48",
"eslint": "^9.11.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsdoc": "^50.3.0",
Expand Down
27 changes: 24 additions & 3 deletions rust/geometry/src/s2/cellid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,17 +650,28 @@ 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 Expand Up @@ -707,6 +718,16 @@ impl S2CellId {

neighbors
}

/// Return the low 32 bits of the cell id
pub fn low_bits(&self) -> u32 {
self.id as u32
}

/// Return the high 32 bits of the cell id
pub fn high_bits(&self) -> u32 {
(self.id >> 32) as u32
}
}
impl From<u64> for S2CellId {
fn from(value: u64) -> Self {
Expand Down
15 changes: 10 additions & 5 deletions rust/geometry/src/s2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
mod cellid;
mod convert;
mod coords;
mod coords_internal;
mod point;
/// S2 Cell ID
pub mod cellid;
/// S2 Conversion tools
pub mod convert;
/// S2 Coordinates
pub mod coords;
/// S2 Coordinates internal methods
pub mod coords_internal;
/// S2 Point
pub mod point;

pub use cellid::*;
pub use coords::*;
Expand Down
12 changes: 6 additions & 6 deletions rust/s2cell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use alloc::boxed::Box;
use s2::cellid::S2CellId;
// use lonlat::LonLat;

use core::cmp::Ordering;

#[cfg(target_arch = "wasm32")]
use lol_alloc::{AssumeSingleThreaded, FreeListAllocator};

Expand Down Expand Up @@ -61,12 +63,10 @@ pub unsafe extern "C" fn high_bits(ptr: *const S2CellId) -> u32 {
pub unsafe extern "C" fn compare_s2_cell_id(id1: *const S2CellId, id2: *const S2CellId) -> i32 {
let id1 = &*id1;
let id2 = &*id2;
if id1 < id2 {
-1
} else if id1 == id2 {
0
} else {
1
match id1.cmp(id2) {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export * from './dataStructures';
export * from './geometry';
export * from './proj4';
export * from './readers';
export * from './space';
export * from './tools';
export * from './util';
// export * from './writers';
export * from './writers';
12 changes: 10 additions & 2 deletions src/proj4/constants/derives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { EPSLN, RA4, RA6, SIXTH } from './values';

import type { Ellipsoid } from './ellipsoid';

import match from '../util/match';

/** Describes an ellipsoid's eccentricity */
export interface EccentricityParams {
a?: number;
Expand Down Expand Up @@ -73,3 +71,13 @@ export function deriveSphere(obj: SphereParams): void {
obj.b = obj.a;
}
}

/**
* @param obj - the object to search
* @param key - the key to search with
* @returns - the value of the key
*/
function match<T>(obj: Record<string, T>, key?: string): T | undefined {
if (key === undefined) return;
if (obj[key] !== undefined) return obj[key];
}
21 changes: 0 additions & 21 deletions src/proj4/util/match.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/readers/geotiff/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function imageDecoder(buffer: ArrayBufferLike): Promise<ArrayBufferLike> {
const blob = new Blob([buffer as ArrayBuffer]); // e.g. { type: 'image/png' }
const imageBitmap = await createImageBitmap(blob);
// Create OffscreenCanvas and draw
const canvas: OffscreenCanvas = new OffscreenCanvas(imageBitmap.width, imageBitmap.height);
const canvas = new OffscreenCanvas(imageBitmap.width, imageBitmap.height);
const ctx = canvas.getContext('2d');
if (ctx === null) throw new Error('Could not get 2d context');
ctx.drawImage(imageBitmap, 0, 0);
Expand Down
1 change: 0 additions & 1 deletion src/readers/geotiff/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ export class GeoTIFFImage {
case 3:
switch (bitsPerSample) {
case 16:
// @ts-expect-error - getFloat16 is polyfilled
return DataView.prototype.getFloat16;
case 32:
return DataView.prototype.getFloat32;
Expand Down
Loading

0 comments on commit 42f8f76

Please sign in to comment.