Skip to content

Commit

Permalink
refactor(core): port buffer-to-typed-array and deps to package
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Dec 1, 2023
1 parent 276a3f5 commit 1cd72bd
Show file tree
Hide file tree
Showing 21 changed files with 357 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/wasm_modules/

cypress/screenshots/
cypress/videos/

packages/core/typescript/itk-wasm/dist
75 changes: 75 additions & 0 deletions packages/core/typescript/itk-wasm/src/buffer-to-typed-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import IntTypes from './interface-types/int-types.js'
import FloatTypes from './interface-types/float-types.js'
import type TypedArray from './typed-array.js'

function bufferToTypedArray (wasmType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] | 'null' | null, buffer: ArrayBuffer): null | TypedArray {
let typedArray: null | TypedArray = null
switch (wasmType) {
case IntTypes.UInt8: {
typedArray = new Uint8Array(buffer)
break
}
case IntTypes.Int8: {
typedArray = new Int8Array(buffer)
break
}
case IntTypes.UInt16: {
typedArray = new Uint16Array(buffer)
break
}
case IntTypes.Int16: {
typedArray = new Int16Array(buffer)
break
}
case IntTypes.UInt32: {
typedArray = new Uint32Array(buffer)
break
}
case IntTypes.Int32: {
typedArray = new Int32Array(buffer)
break
}
case IntTypes.UInt64: {
if (typeof globalThis.BigUint64Array === 'function') {
typedArray = new BigUint64Array(buffer)
} else {
// Sub with reasonable default. Will get cast to Uint8Array when
// transferred to WebAssembly.
typedArray = new Uint8Array(buffer)
}
break
}
case IntTypes.Int64: {
if (typeof globalThis.BigInt64Array === 'function') {
typedArray = new BigInt64Array(buffer)
} else {
// Sub with reasonable default. Will get cast to Uint8Array when
// transferred to WebAssembly.
typedArray = new Uint8Array(buffer)
}
break
}
case FloatTypes.Float32: {
typedArray = new Float32Array(buffer)
break
}
case FloatTypes.Float64: {
typedArray = new Float64Array(buffer)
break
}
case 'null': {
typedArray = null
break
}
case null: {
typedArray = null
break
}
default:
throw new Error('Type is not supported as a TypedArray')
}

return typedArray
}

export default bufferToTypedArray
3 changes: 3 additions & 0 deletions packages/core/typescript/itk-wasm/src/index-common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Core API interfaces, data structures, and functions

export { default as bufferToTypedArray } from './buffer-to-typed-array.js'
3 changes: 3 additions & 0 deletions packages/core/typescript/itk-wasm/src/index-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// itk-wasm Node API interfaces, data structures, and functions

export * from './index-common.js'
3 changes: 3 additions & 0 deletions packages/core/typescript/itk-wasm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// itk-wasm Browser API interfaces, data structures, and functions

export * from './index-common.js'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface BinaryFile {
data: Uint8Array
path: string
}

export default BinaryFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface BinaryStream {
data: Uint8Array
}

export default BinaryStream
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const FloatTypes = {
Float32: 'float32',
Float64: 'float64',

SpacePrecisionType: 'float64'
} as const

export default FloatTypes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import IntTypes from './int-types.js'
import FloatTypes from './float-types.js'
import PixelTypes from './pixel-types.js'

class ImageType {
constructor (
public readonly dimension: number = 2,
public readonly componentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = IntTypes.UInt8,
public readonly pixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar,
public readonly components: number = 1
) {}
}

export default ImageType
44 changes: 44 additions & 0 deletions packages/core/typescript/itk-wasm/src/interface-types/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import ImageType from './image-type.js'
import type TypedArray from '../TypedArray.js'
import setMatrixElement from '../setMatrixElement.js'
import Metadata from './metadata.js'

class Image {
name: string = 'image'

origin: number[]

spacing: number[]

direction: TypedArray

size: number[]

metadata: Metadata

data: null | TypedArray

constructor (public readonly imageType = new ImageType()) {
const dimension = imageType.dimension
this.origin = new Array(dimension)
this.origin.fill(0.0)

this.spacing = new Array(dimension)
this.spacing.fill(1.0)

this.direction = new Float64Array(dimension * dimension)
this.direction.fill(0.0)
for (let ii = 0; ii < dimension; ii++) {
setMatrixElement(this.direction, dimension, ii, ii, 1.0)
}

this.size = new Array(dimension)
this.size.fill(0)

this.metadata = new Map()

this.data = null
}
}

export default Image
17 changes: 17 additions & 0 deletions packages/core/typescript/itk-wasm/src/interface-types/int-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const IntTypes = {
Int8: 'int8',
UInt8: 'uint8',
Int16: 'int16',
UInt16: 'uint16',
Int32: 'int32',
UInt32: 'uint32',
Int64: 'int64',
UInt64: 'uint64',

SizeValueType: 'uint64',
IdentifierType: 'uint64',
IndexValueType: 'int64',
OffsetValueType: 'int64'
} as const

export default IntTypes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type JsonCompatible =
| null
| string
| number
| boolean
| { [key: string]: JsonCompatible }
| JsonCompatible[]

export default JsonCompatible
18 changes: 18 additions & 0 deletions packages/core/typescript/itk-wasm/src/interface-types/mesh-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import IntTypes from './int-types.js'
import FloatTypes from './float-types.js'
import PixelTypes from './pixel-types.js'

class MeshType {
constructor (
public readonly dimension: number = 2,
public readonly pointComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32,
public readonly pointPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32,
public readonly pointPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar,
public readonly pointPixelComponents: number = 1,
public readonly cellComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = IntTypes.Int32,
public readonly cellPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32,
public readonly cellPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar,
public readonly cellPixelComponents: number = 1) {}
}

export default MeshType
42 changes: 42 additions & 0 deletions packages/core/typescript/itk-wasm/src/interface-types/mesh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import MeshType from './mesh-type.js'
import type TypedArray from '../TypedArray.js'

class Mesh {
meshType: MeshType

name: string = 'mesh'

numberOfPoints: number
points: null | TypedArray

numberOfPointPixels: number
pointData: null | TypedArray

numberOfCells: number
cells: null | TypedArray
cellBufferSize: number

numberOfCellPixels: number
cellData: null | TypedArray

constructor (public readonly mt = new MeshType()) {
this.meshType = mt

this.name = 'mesh'

this.numberOfPoints = 0
this.points = null

this.numberOfPointPixels = 0
this.pointData = null

this.numberOfCells = 0
this.cellBufferSize = 0
this.cells = null

this.numberOfCellPixels = 0
this.cellData = null
}
}

export default Mesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Metadata = Map<string, string | string[] | number | number[] | number[][]>

export default Metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const PixelTypes = {
Unknown: 'Unknown',
Scalar: 'Scalar',
RGB: 'RGB',
RGBA: 'RGBA',
Offset: 'Offset',
Vector: 'Vector',
Point: 'Point',
CovariantVector: 'CovariantVector',
SymmetricSecondRankTensor: 'SymmetricSecondRankTensor',
DiffusionTensor3D: 'DiffusionTensor3D',
Complex: 'Complex',
FixedArray: 'FixedArray',
Array: 'Array',
Matrix: 'Matrix',
VariableLengthVector: 'VariableLengthVector',
VariableSizeMatrix: 'VariableSizeMatrix'
} as const

export default PixelTypes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import IntTypes from './int-types.js'
import FloatTypes from './float-types.js'
import PixelTypes from './pixel-types.js'

class PolyDataType {
constructor (
public readonly pointPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32,
public readonly pointPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar,
public readonly pointPixelComponents: number = 1,
public readonly cellPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32,
public readonly cellPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar,
public readonly cellPixelComponents: number = 1) {}
}

export default PolyDataType
56 changes: 56 additions & 0 deletions packages/core/typescript/itk-wasm/src/interface-types/poly-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import PolyDataType from './poly-data-type.js'
import TypedArray from '../TypedArray.js'

class PolyData {
name: string = 'PolyData'

numberOfPoints: number
points: Float32Array

verticesBufferSize: number
vertices: null | Uint32Array

linesBufferSize: number
lines: null | Uint32Array

polygonsBufferSize: number
polygons: null | Uint32Array

triangleStripsBufferSize: number
triangleStrips: null | Uint32Array

numberOfPointPixels: number
pointData: null | TypedArray

numberOfCellPixels: number
cellData: null | TypedArray

constructor (public readonly polyDataType = new PolyDataType()) {
this.polyDataType = polyDataType

this.name = 'PolyData'

this.numberOfPoints = 0
this.points = new Float32Array()

this.verticesBufferSize = 0
this.vertices = null

this.linesBufferSize = 0
this.lines = null

this.polygonsBufferSize = 0
this.polygons = null

this.triangleStripsBufferSize = 0
this.triangleStrips = null

this.numberOfPointPixels = 0
this.pointData = null

this.numberOfCellPixels = 0
this.cellData = null
}
}

export default PolyData
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface TextFile {
data: string
path: string
}

export default TextFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface TextStream {
data: string
}

export default TextStream
3 changes: 3 additions & 0 deletions packages/core/typescript/itk-wasm/src/typed-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | BigInt64Array | BigUint64Array

export default TypedArray

0 comments on commit 1cd72bd

Please sign in to comment.