GeoJSON の3次元および超次元拡張。
インストール
pnpm add -D @types/hyper-geojson@github:kamataryo/types__hyper-geojson#main
使い方
cube.ts
import type { Polyhedron } from 'hyper-geojson'
// A unit cube: six square faces, each turned to face outward
export const cube: Polyhedron = {
type: 'Polyhedron',
coordinates: [
[[[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0], [0, 0, 0]]],
[[[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1], [0, 0, 0]]],
[[[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0], [0, 0, 0]]],
[[[1, 1, 1], [1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]]],
[[[1, 1, 1], [1, 1, 0], [0, 1, 0], [0, 1, 1], [1, 1, 1]]],
[[[1, 1, 1], [0, 1, 1], [0, 0, 1], [1, 0, 1], [1, 1, 1]]],
],
}corners.ts
import type { HyperFeature, HyperPosition, Polychoron } from 'hyper-geojson'
// Every corner of a polychoron: cells, then faces, then rings, then positions
export function corners(feature: HyperFeature<Polychoron>): HyperPosition[] {
return feature.geometry.coordinates.flat(3)
}depth.ts
import type { Polytope } from 'hyper-geojson'
// A 5-polytope nests its positions five levels deep
export const fine: Polytope<5> = {
type: 'Polytope5',
coordinates: [[[[[[0, 0, 0, 0, 0]]]]]],
}
export const tooShallow: Polytope<5> = {
type: 'Polytope5',
// @ts-expect-error four levels are one too few
coordinates: [[[[[0, 0, 0, 0, 0]]]]],
}ビューア
サンプル
cells
ライブエディタ
向きの規約
どの境界も、それが囲むものの外側を向きます。GeoJSON の右手の法則を、あらゆる次元に広げたものです。
- ポリゴンの外周は反時計回り、穴は時計回りです。
- 多面体の面は、右手の法則に従って法線を立体の外側に向けます。内部に空洞があれば、法線は空洞の中を向きます。
- 多胞体のセルも、同じように外側を向きます。
そのため、共有された境界は隣り合う2つの要素から逆向きにたどられます。多面体の辺は2つの面から、多胞体の面は2つのセルからです。このとき点の巻き数は、立体の内部で 1、外部と空洞の中で 0 になります。
空洞のために入れ子を1段増やす必要はありません。空洞は、逆を向いた境界です。型が検査するのは入れ子の深さだけなので、ねじれた境界や閉じていない境界も型検査を通ります。GeoJSON で閉じていないリングが通るのと同じです。