Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from_file and write_to_file to SpatialData #265

Merged
merged 7 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .typo-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ excluded_words:
- shutil
- functools
- boltons
- meshio
# matplotlib ----------------------------------
- gca
- mpl
Expand Down Expand Up @@ -118,6 +119,9 @@ excluded_words:
- numpydoc
# markdown / latex ----------------------------
- cdot
# file extensions -----------------------------
- vtk
- stl
# other ---------------------------------------
- addopts
- csm
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
pending [ASDF #916](https://github.com/asdf-format/asdf/issues/916) [[#224]](https://github.com/BAMWelDX/weldx/pull/224)
- set minimum Python version to 3.8 [[#229]](https://github.com/BAMWelDX/weldx/pull/229)[[#255]](https://github.com/BAMWelDX/weldx/pull/255)
- only import some packages upon first use [[#247]](https://github.com/BAMWelDX/weldx/pull/247)
- Add [meshio](https://pypi.org/project/meshio/) as new dependency [#265](https://github.com/BAMWelDX/weldx/pull/265)

## 0.2.2 (30.11.2020)

Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
- asdf>=2.7
- openpyxl
- fs
- meshio
# graph packages
- networkx
# Code quality
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ install_requires =
fs
ipywidgets
k3d
meshio

include_package_data = True

Expand Down
33 changes: 33 additions & 0 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import copy
import math
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List, Union

import numpy as np
Expand Down Expand Up @@ -2927,3 +2929,34 @@ def test_class_creation_exceptions(arguments, exception_type, test_name):
"""
with pytest.raises(exception_type):
SpatialData(*arguments)

@staticmethod
@pytest.mark.parametrize(
"filename",
["test.ply", "test.stl", "test.vtk", Path("test.stl")],
)
def test_read_write_file(filename: Union[str, Path]):
"""Test the `from_file` and `write_to_file` functions.

The test simply creates a `SpatialData` instance, writes it to a file and reads
it back. The result is compared to the original object.

Parameters
----------
filename :
Name of the file

"""
points = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]
triangles = [[0, 1, 2], [2, 3, 0]]

data = SpatialData(points, triangles)
with TemporaryDirectory(dir=Path(__file__).parent) as tmpdirname:
filepath = f"{tmpdirname}/{filename}"
if isinstance(filename, Path):
filepath = Path(filepath)
data.write_to_file(filepath)
data_read = SpatialData.from_file(filepath)

assert np.allclose(data.coordinates, data_read.coordinates)
assert np.allclose(data.triangles, data_read.triangles)
38 changes: 38 additions & 0 deletions weldx/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import copy
import math
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Tuple, Union

import meshio
import numpy as np
import pint
from xarray import DataArray
Expand Down Expand Up @@ -2328,6 +2330,26 @@ def __post_init__(self):
if not self.triangles.ndim == 2:
raise ValueError("SpatialData triangulation must be a 2d array")

@staticmethod
def from_file(file_name: Union[str, Path]) -> "SpatialData":
"""Create an instance from a file.

Parameters
----------
file_name :
Name of the source file.

Returns
-------
SpatialData:
New `SpatialData` instance

"""
mesh = meshio.read(file_name)
triangles = mesh.cells_dict.get("triangle")

return SpatialData(mesh.points, triangles)

@staticmethod
def from_geometry_raster(geometry_raster: np.ndarray) -> "SpatialData":
"""Triangulate rasterized Geometry Profile.
Expand Down Expand Up @@ -2399,3 +2421,19 @@ def plot(
label=label,
show_wireframe=show_wireframe,
)

def write_to_file(self, file_name: Union[str, Path]):
"""Write spatial data into a file.

The extension prescribes the output format.

Parameters
----------
file_name :
Name of the file

"""
mesh = meshio.Mesh(
points=self.coordinates.data, cells={"triangle": self.triangles}
)
mesh.write(file_name)