Skip to content

Commit b2164f3

Browse files
committed
feat(itkwasm): Mesh, MeshType
1 parent 5956335 commit b2164f3

File tree

8 files changed

+4347
-11
lines changed

8 files changed

+4347
-11
lines changed

doc/content/api/Mesh.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ An `Mesh` is the N-dimensional data structure to represent points sets and meshe
55

66
**meshType**: The [MeshType](./MeshType.html) for this mesh.
77
**name**: An optional name, a `String`, that describes this mesh.
8-
**dimension**: An integer that describes the dimension for the mesh, typically 2 or 3.
98
**numberOfPoints**: The number of points in the mesh.
109
**points**: A [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) containing the point locations.
1110
**numberOfPointPixels**: The number of points pixels in the mesh.

src/core/Image.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type TypedArray from './TypedArray.js'
33
import setMatrixElement from './setMatrixElement.js'
44

55
class Image {
6-
name: string = 'Image'
6+
name: string = 'image'
77

88
origin: number[]
99

src/core/Mesh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Mesh {
2222
constructor (public readonly mt = new MeshType()) {
2323
this.meshType = mt
2424

25-
this.name = 'Mesh'
25+
this.name = 'mesh'
2626

2727
this.numberOfPoints = 0
2828
this.points = null

src/python/itkwasm/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
__version__ = "1.0a0"
44

55
from .image import Image, ImageType
6+
from .mesh import Mesh, MeshType
67

78
__all__ = [
89
"Image",
910
"ImageType",
11+
"Mesh",
12+
"MeshType",
1013
]

src/python/itkwasm/mesh.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from dataclasses import dataclass
2+
3+
from typing import Sequence, Optional
4+
5+
try:
6+
from numpy.typing import ArrayLike
7+
except ImportError:
8+
from numpy import ndarray as ArrayLike
9+
10+
@dataclass
11+
class MeshType:
12+
dimension: int
13+
14+
pointComponentType: str
15+
pointPixelComponentType: str
16+
pointPixelType: str
17+
pointPixelComponents: int
18+
19+
cellComponentType: str
20+
cellPixelComponentType: str
21+
cellPixelType: str
22+
cellPixelComponents: int
23+
24+
25+
@dataclass
26+
class Mesh:
27+
meshType: MeshType
28+
29+
name: str
30+
31+
numberOfPoints: int
32+
points: Optional[ArrayLike]
33+
34+
numberOfPointPixels: int
35+
pointData: Optional[ArrayLike]
36+
37+
numberOfCells: int
38+
cells: Optional[ArrayLike]
39+
cellBufferSize: int
40+
41+
numberOfCellPixels: int
42+
cellData: Optional[ArrayLike]

src/python/test/input/cow.vtk

Lines changed: 4245 additions & 0 deletions
Large diffs are not rendered by default.

src/python/test/test_image.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import numpy as np
88

99
def test_image():
10-
data = Path(__file__).absolute().parent / "input" / "cthead1.png"
11-
itk_image = itk.imread(data, itk.UC)
12-
itk_image_dict = itk.dict_from_image(itk_image)
13-
itkwasm_image = Image(**itk_image_dict)
14-
itkwasm_image_dict = asdict(itkwasm_image)
15-
itk_image_roundtrip = itk.image_from_dict(itkwasm_image_dict)
16-
difference = np.sum(itk.comparison_image_filter(itk_image, itk_image_roundtrip))
17-
assert difference == 0.0
10+
data = Path(__file__).absolute().parent / "input" / "cthead1.png"
11+
itk_image = itk.imread(data, itk.UC)
12+
itk_image_dict = itk.dict_from_image(itk_image)
13+
itkwasm_image = Image(**itk_image_dict)
14+
itkwasm_image_dict = asdict(itkwasm_image)
15+
itk_image_roundtrip = itk.image_from_dict(itkwasm_image_dict)
16+
difference = np.sum(itk.comparison_image_filter(itk_image, itk_image_roundtrip))
17+
assert difference == 0.0

src/python/test/test_mesh.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from pathlib import Path
2+
3+
import itk
4+
5+
from itkwasm import Mesh
6+
from dataclasses import asdict
7+
import numpy as np
8+
9+
def test_mesh():
10+
data = Path(__file__).absolute().parent / "input" / "cow.vtk"
11+
itk_mesh = itk.meshread(data)
12+
13+
itk_mesh_dict = itk.dict_from_mesh(itk_mesh)
14+
# Bug, to be fixed by 5.3.0
15+
itk_mesh_dict.pop('dimension', None)
16+
itkwasm_mesh = Mesh(**itk_mesh_dict)
17+
itkwasm_mesh_dict = asdict(itkwasm_mesh)
18+
itk_mesh_roundtrip = itk.mesh_from_dict(itkwasm_mesh_dict)
19+
itk_mesh_roundtrip_dict = itk.dict_from_mesh(itk_mesh_roundtrip)
20+
21+
meshType = itk_mesh_dict["meshType"]
22+
meshType_roundtrip = itk_mesh_roundtrip_dict["meshType"]
23+
assert meshType["dimension"] == meshType_roundtrip["dimension"]
24+
assert meshType["pointComponentType"] == meshType_roundtrip["pointComponentType"]
25+
assert meshType["pointPixelComponentType"] == meshType_roundtrip["pointPixelComponentType"]
26+
assert meshType["pointPixelType"] == meshType_roundtrip["pointPixelType"]
27+
assert meshType["pointPixelComponents"] == meshType_roundtrip["pointPixelComponents"]
28+
29+
assert meshType["cellComponentType"] == meshType_roundtrip["cellComponentType"]
30+
assert meshType["cellPixelComponentType"] == meshType_roundtrip["cellPixelComponentType"]
31+
assert meshType["cellPixelType"] == meshType_roundtrip["cellPixelType"]
32+
assert meshType["cellPixelComponents"] == meshType_roundtrip["cellPixelComponents"]
33+
34+
assert itk_mesh_dict["name"] == itk_mesh_roundtrip_dict["name"]
35+
36+
assert itk_mesh_dict["numberOfPoints"] == itk_mesh_roundtrip_dict["numberOfPoints"]
37+
assert np.array_equal(itk_mesh_dict["points"], itk_mesh_roundtrip_dict["points"])
38+
39+
assert itk_mesh_dict["numberOfPointPixels"] == itk_mesh_roundtrip_dict["numberOfPointPixels"]
40+
assert np.array_equal(itk_mesh_dict["pointData"], itk_mesh_roundtrip_dict["pointData"])
41+
42+
assert itk_mesh_dict["numberOfCells"] == itk_mesh_roundtrip_dict["numberOfCells"]
43+
assert np.array_equal(itk_mesh_dict["cells"], itk_mesh_roundtrip_dict["cells"])
44+
assert itk_mesh_dict["cellBufferSize"] == itk_mesh_roundtrip_dict["cellBufferSize"]
45+
46+
assert itk_mesh_dict["numberOfCellPixels"] == itk_mesh_roundtrip_dict["numberOfCellPixels"]
47+
assert np.array_equal(itk_mesh_dict["cellData"], itk_mesh_roundtrip_dict["cellData"])

0 commit comments

Comments
 (0)