forked from InsightSoftwareConsortium/itk-wasm
/
writeMeshEmscriptenFSFile.js
98 lines (80 loc) · 3.58 KB
/
writeMeshEmscriptenFSFile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const meshJSComponentToIOComponent = require('./meshJSComponentToIOComponent.js')
const meshJSPixelTypeToIOPixelType = require('./meshJSPixelTypeToIOPixelType.js')
const writeMeshEmscriptenFSFile = (module, { useCompression, binaryFileType }, mesh, filePath) => {
const meshIO = new module.ITKMeshIO()
meshIO.SetFileName(filePath)
if (!meshIO.CanWriteFile(filePath)) {
throw new Error('Could not write file: ' + filePath)
}
const dimension = mesh.meshType.dimension
meshIO.SetPointDimension(dimension)
const pointIOComponentType = meshJSComponentToIOComponent(module, mesh.meshType.pointComponentType)
meshIO.SetPointComponentType(pointIOComponentType)
const cellIOComponentType = meshJSComponentToIOComponent(module, mesh.meshType.cellComponentType)
meshIO.SetCellComponentType(cellIOComponentType)
const pointIOPixelType = meshJSPixelTypeToIOPixelType(module, mesh.meshType.pointPixelType)
meshIO.SetPointPixelType(pointIOPixelType)
meshIO.SetNumberOfPointPixelComponents(mesh.meshType.pointPixelComponents)
const cellIOPixelType = meshJSPixelTypeToIOPixelType(module, mesh.meshType.cellPixelType)
meshIO.SetCellPixelType(cellIOPixelType)
meshIO.SetNumberOfCellPixelComponents(mesh.meshType.cellPixelComponents)
meshIO.SetUseCompression(!!useCompression)
if (binaryFileType) {
meshIO.SetFileType(module.FileType.BINARY)
} else {
meshIO.SetFileType(module.FileType.ASCII)
}
meshIO.SetByteOrder(module.ByteOrder.LittleEndian)
meshIO.SetNumberOfPoints(mesh.numberOfPoints)
if (mesh.numberOfPoints > 0) {
meshIO.SetUpdatePoints(true)
}
meshIO.SetNumberOfPointPixels(mesh.numberOfPointPixels)
if (mesh.numberOfPointPixels > 0) {
meshIO.SetUpdatePointData(true)
}
meshIO.SetNumberOfCells(mesh.numberOfCells)
if (mesh.numberOfCells > 0) {
meshIO.SetUpdateCells(true)
}
meshIO.SetNumberOfCellPixels(mesh.numberOfCellPixels)
meshIO.SetCellBufferSize(mesh.cellBufferSize)
if (mesh.numberOfCellPixels > 0) {
meshIO.SetUpdatePointData(true)
}
meshIO.WriteMeshInformation()
if (mesh.numberOfPoints > 0) {
let numberOfBytes = mesh.points.length * mesh.points.BYTES_PER_ELEMENT
let dataPtr = module._malloc(numberOfBytes)
let dataHeap = new Uint8Array(module.HEAPU8.buffer, dataPtr, numberOfBytes)
dataHeap.set(new Uint8Array(mesh.points.buffer))
meshIO.WritePoints(dataHeap.byteOffset)
module._free(dataHeap.byteOffset)
}
if (mesh.numberOfCells > 0) {
let numberOfBytes = mesh.cells.length * mesh.cells.BYTES_PER_ELEMENT
let dataPtr = module._malloc(numberOfBytes)
let dataHeap = new Uint8Array(module.HEAPU8.buffer, dataPtr, numberOfBytes)
dataHeap.set(new Uint8Array(mesh.cells.buffer))
meshIO.WriteCells(dataHeap.byteOffset)
module._free(dataHeap.byteOffset)
}
if (mesh.numberOfPointPixels > 0) {
let numberOfBytes = mesh.pointData.length * mesh.pointData.BYTES_PER_ELEMENT
let dataPtr = module._malloc(numberOfBytes)
let dataHeap = new Uint8Array(module.HEAPU8.buffer, dataPtr, numberOfBytes)
dataHeap.set(new Uint8Array(mesh.pointData.buffer))
meshIO.WritePointData(dataHeap.byteOffset)
module._free(dataHeap.byteOffset)
}
if (mesh.numberOfCellPixels > 0) {
let numberOfBytes = mesh.cellData.length * mesh.cellData.BYTES_PER_ELEMENT
let dataPtr = module._malloc(numberOfBytes)
let dataHeap = new Uint8Array(module.HEAPU8.buffer, dataPtr, numberOfBytes)
dataHeap.set(new Uint8Array(mesh.cellData.buffer))
meshIO.WriteCellData(dataHeap.byteOffset)
module._free(dataHeap.byteOffset)
}
meshIO.Write()
}
module.exports = writeMeshEmscriptenFSFile