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]]],
  ],
}
Feature には多面体も多胞体もその先のポリトープも入り、入れ子の深さから TypeScript が型を追えます。
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)
}
入れ子の深さを間違えると、TypeScript が教えてくれます。
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 で閉じていないリングが通るのと同じです。