mtex2gmsh 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mtex2gmsh-1.0.0/LICENSE +21 -0
- mtex2gmsh-1.0.0/PKG-INFO +99 -0
- mtex2gmsh-1.0.0/README.md +64 -0
- mtex2gmsh-1.0.0/pyproject.toml +54 -0
- mtex2gmsh-1.0.0/setup.cfg +4 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/__init__.py +49 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/bspline.py +95 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/geo_writer.py +500 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/gmsh_geo.py +579 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/gmsh_runner.py +192 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/graph.py +108 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/local_size.py +127 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/loops.py +164 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/measure.py +31 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/mesh_plot.py +76 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/model.py +36 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/mtex_compat.py +124 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/regularize.py +172 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/roi.py +274 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/segments.py +159 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh/singular_points.py +135 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh.egg-info/PKG-INFO +99 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh.egg-info/SOURCES.txt +29 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh.egg-info/dependency_links.txt +1 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh.egg-info/requires.txt +15 -0
- mtex2gmsh-1.0.0/src/mtex2gmsh.egg-info/top_level.txt +1 -0
- mtex2gmsh-1.0.0/tests/test_bspline.py +49 -0
- mtex2gmsh-1.0.0/tests/test_gmsh_geo.py +193 -0
- mtex2gmsh-1.0.0/tests/test_graph.py +42 -0
- mtex2gmsh-1.0.0/tests/test_mesh.py +216 -0
- mtex2gmsh-1.0.0/tests/test_segments.py +51 -0
mtex2gmsh-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dorian Depriester
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mtex2gmsh-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mtex2gmsh
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Generate conforming 2D/3D meshes from EBSD data processed with the Python port of MTEX (mtex) and Gmsh
|
|
5
|
+
Author: Dorian Depriester
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/DorianDepriester/pyMTEX2Gmsh
|
|
8
|
+
Project-URL: Documentation, https://pymtex2gmsh.readthedocs.io/
|
|
9
|
+
Project-URL: Repository, https://github.com/DorianDepriester/pyMTEX2Gmsh
|
|
10
|
+
Project-URL: Issues, https://github.com/DorianDepriester/pyMTEX2Gmsh/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/DorianDepriester/pyMTEX2Gmsh/releases
|
|
12
|
+
Keywords: EBSD,MTEX,gmsh,mesh,polycrystal,FEA
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: numpy
|
|
22
|
+
Requires-Dist: scipy
|
|
23
|
+
Requires-Dist: mtex>=1.0
|
|
24
|
+
Requires-Dist: gmsh>=4.11
|
|
25
|
+
Requires-Dist: matplotlib
|
|
26
|
+
Provides-Extra: test
|
|
27
|
+
Requires-Dist: pytest; extra == "test"
|
|
28
|
+
Provides-Extra: doc
|
|
29
|
+
Requires-Dist: sphinx>=7; extra == "doc"
|
|
30
|
+
Requires-Dist: myst-parser>=3; extra == "doc"
|
|
31
|
+
Requires-Dist: sphinx-gallery>=0.16; extra == "doc"
|
|
32
|
+
Requires-Dist: sphinx-rtd-theme>=2; extra == "doc"
|
|
33
|
+
Requires-Dist: sphinx-rtd-dark-mode>=1.3; extra == "doc"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# MTEX2Gmsh for Python
|
|
37
|
+
|
|
38
|
+
[](https://github.com/DorianDepriester/pyMTEX2Gmsh/actions/workflows/tests.yml)
|
|
39
|
+
[](https://pymtex2gmsh.readthedocs.io/)
|
|
40
|
+
|
|
41
|
+
Generate a conforming mesh from EBSD data processed with Python and MTEX.
|
|
42
|
+
|
|
43
|
+
**MTEX2Gmsh** (Python package `mtex2gmsh`, this repository `pyMTEX2Gmsh`) is a 100% Python port of the Matlab toolbox
|
|
44
|
+
[MTEX2Gmsh](https://github.com/DorianDepriester/MTEX2Gmsh) (Depriester & Kubler,
|
|
45
|
+
JOSS 2020, [doi:10.21105/joss.02094](https://doi.org/10.21105/joss.02094)). It
|
|
46
|
+
converts grains reconstructed with the [Python port of
|
|
47
|
+
MTEX](https://pypi.org/project/mtex/) (`pip install mtex`) into a Gmsh-readable
|
|
48
|
+
geometry, then meshes it with [Gmsh](https://gmsh.info) through its official
|
|
49
|
+
Python SDK (`pip install gmsh`) — no Matlab, no external binary required.
|
|
50
|
+
|
|
51
|
+
It is intended to perform Finite Element Analysis (FEA) at grain scale on
|
|
52
|
+
polycrystals imaged by EBSD.
|
|
53
|
+
|
|
54
|
+
📖 **Documentation (with executed examples): <https://pymtex2gmsh.readthedocs.io/>**
|
|
55
|
+
|
|
56
|
+
## Install
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install mtex2gmsh # everything included: mtex, numpy, scipy, Gmsh SDK, matplotlib
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Requirements: Python >= 3.12 (required by `mtex` 1.0). On headless machines, also
|
|
63
|
+
install the GL libraries used by the Gmsh SDK (`sudo apt-get install libgl1 libglu1-mesa`).
|
|
64
|
+
|
|
65
|
+
## Quickstart
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import mtex
|
|
69
|
+
from mtex2gmsh import GmshGeo, plot_mesh
|
|
70
|
+
|
|
71
|
+
ebsd = mtex.mtexdata('small') # example dataset shipped with MTEX
|
|
72
|
+
grains = mtex.calcGrains(ebsd[ebsd.isIndexed])
|
|
73
|
+
|
|
74
|
+
G = GmshGeo(grains) # Gmsh-readable description of the grains
|
|
75
|
+
print(G.size()) # number of grains and ROI size
|
|
76
|
+
|
|
77
|
+
G.mesh('small.msh') # 3D mesh of linear wedge elements (default)
|
|
78
|
+
G.mesh('small_tri.msh', element_type='Tri', element_size=100) # 2D triangular mesh
|
|
79
|
+
plot_mesh('small_tri.msh', output_file='small_tri.png') # visualize the mesh, export a PNG
|
|
80
|
+
G.savegeo('geometry.geo') # dump only the Gmsh geometry
|
|
81
|
+
G.export_grain_props('grains.csv') # grain ids, phases, Euler orientations
|
|
82
|
+
|
|
83
|
+
G.plot() # plot the B-spline geometry (matplotlib)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
A complete runnable script is provided in [`examples/example.py`](examples/example.py);
|
|
87
|
+
the full documentation (installation, all meshing options, API reference, executed
|
|
88
|
+
examples) lives in [`docs/`](docs/) and is built with Sphinx on
|
|
89
|
+
[ReadTheDocs](https://pymtex2gmsh.readthedocs.io/).
|
|
90
|
+
|
|
91
|
+
## Citation
|
|
92
|
+
|
|
93
|
+
If you use this toolbox, please cite the original paper (Depriester & Kubler,
|
|
94
|
+
JOSS 2020, [doi:10.21105/joss.02094](https://doi.org/10.21105/joss.02094)) — see
|
|
95
|
+
[CITATION.cff](CITATION.cff), which GitHub picks up for its “Cite this repository” button.
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT (see [LICENSE](LICENSE)).
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# MTEX2Gmsh for Python
|
|
2
|
+
|
|
3
|
+
[](https://github.com/DorianDepriester/pyMTEX2Gmsh/actions/workflows/tests.yml)
|
|
4
|
+
[](https://pymtex2gmsh.readthedocs.io/)
|
|
5
|
+
|
|
6
|
+
Generate a conforming mesh from EBSD data processed with Python and MTEX.
|
|
7
|
+
|
|
8
|
+
**MTEX2Gmsh** (Python package `mtex2gmsh`, this repository `pyMTEX2Gmsh`) is a 100% Python port of the Matlab toolbox
|
|
9
|
+
[MTEX2Gmsh](https://github.com/DorianDepriester/MTEX2Gmsh) (Depriester & Kubler,
|
|
10
|
+
JOSS 2020, [doi:10.21105/joss.02094](https://doi.org/10.21105/joss.02094)). It
|
|
11
|
+
converts grains reconstructed with the [Python port of
|
|
12
|
+
MTEX](https://pypi.org/project/mtex/) (`pip install mtex`) into a Gmsh-readable
|
|
13
|
+
geometry, then meshes it with [Gmsh](https://gmsh.info) through its official
|
|
14
|
+
Python SDK (`pip install gmsh`) — no Matlab, no external binary required.
|
|
15
|
+
|
|
16
|
+
It is intended to perform Finite Element Analysis (FEA) at grain scale on
|
|
17
|
+
polycrystals imaged by EBSD.
|
|
18
|
+
|
|
19
|
+
📖 **Documentation (with executed examples): <https://pymtex2gmsh.readthedocs.io/>**
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install mtex2gmsh # everything included: mtex, numpy, scipy, Gmsh SDK, matplotlib
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Requirements: Python >= 3.12 (required by `mtex` 1.0). On headless machines, also
|
|
28
|
+
install the GL libraries used by the Gmsh SDK (`sudo apt-get install libgl1 libglu1-mesa`).
|
|
29
|
+
|
|
30
|
+
## Quickstart
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import mtex
|
|
34
|
+
from mtex2gmsh import GmshGeo, plot_mesh
|
|
35
|
+
|
|
36
|
+
ebsd = mtex.mtexdata('small') # example dataset shipped with MTEX
|
|
37
|
+
grains = mtex.calcGrains(ebsd[ebsd.isIndexed])
|
|
38
|
+
|
|
39
|
+
G = GmshGeo(grains) # Gmsh-readable description of the grains
|
|
40
|
+
print(G.size()) # number of grains and ROI size
|
|
41
|
+
|
|
42
|
+
G.mesh('small.msh') # 3D mesh of linear wedge elements (default)
|
|
43
|
+
G.mesh('small_tri.msh', element_type='Tri', element_size=100) # 2D triangular mesh
|
|
44
|
+
plot_mesh('small_tri.msh', output_file='small_tri.png') # visualize the mesh, export a PNG
|
|
45
|
+
G.savegeo('geometry.geo') # dump only the Gmsh geometry
|
|
46
|
+
G.export_grain_props('grains.csv') # grain ids, phases, Euler orientations
|
|
47
|
+
|
|
48
|
+
G.plot() # plot the B-spline geometry (matplotlib)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
A complete runnable script is provided in [`examples/example.py`](examples/example.py);
|
|
52
|
+
the full documentation (installation, all meshing options, API reference, executed
|
|
53
|
+
examples) lives in [`docs/`](docs/) and is built with Sphinx on
|
|
54
|
+
[ReadTheDocs](https://pymtex2gmsh.readthedocs.io/).
|
|
55
|
+
|
|
56
|
+
## Citation
|
|
57
|
+
|
|
58
|
+
If you use this toolbox, please cite the original paper (Depriester & Kubler,
|
|
59
|
+
JOSS 2020, [doi:10.21105/joss.02094](https://doi.org/10.21105/joss.02094)) — see
|
|
60
|
+
[CITATION.cff](CITATION.cff), which GitHub picks up for its “Cite this repository” button.
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT (see [LICENSE](LICENSE)).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mtex2gmsh"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Generate conforming 2D/3D meshes from EBSD data processed with the Python port of MTEX (mtex) and Gmsh"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12" # mtex (Python port of MTEX) 1.0 requires Python >= 3.12
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Dorian Depriester" }]
|
|
14
|
+
keywords = ["EBSD", "MTEX", "gmsh", "mesh", "polycrystal", "FEA"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Topic :: Scientific/Engineering",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"numpy",
|
|
24
|
+
"scipy",
|
|
25
|
+
"mtex>=1.0",
|
|
26
|
+
"gmsh>=4.11", # Gmsh Python SDK, used to run the meshing
|
|
27
|
+
"matplotlib", # used by GmshGeo.plot() / plot_singular_points()
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/DorianDepriester/pyMTEX2Gmsh"
|
|
32
|
+
Documentation = "https://pymtex2gmsh.readthedocs.io/"
|
|
33
|
+
Repository = "https://github.com/DorianDepriester/pyMTEX2Gmsh"
|
|
34
|
+
Issues = "https://github.com/DorianDepriester/pyMTEX2Gmsh/issues"
|
|
35
|
+
Changelog = "https://github.com/DorianDepriester/pyMTEX2Gmsh/releases"
|
|
36
|
+
|
|
37
|
+
[project.optional-dependencies]
|
|
38
|
+
test = ["pytest"] # only to run the test suite
|
|
39
|
+
doc = [ # to build the Sphinx documentation (docs/)
|
|
40
|
+
"sphinx>=7",
|
|
41
|
+
"myst-parser>=3",
|
|
42
|
+
"sphinx-gallery>=0.16", # executes the examples at build time
|
|
43
|
+
"sphinx-rtd-theme>=2",
|
|
44
|
+
"sphinx-rtd-dark-mode>=1.3", # dark theme (default) on the RTD theme
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.packages.find]
|
|
48
|
+
where = ["src"]
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
testpaths = ["tests"]
|
|
52
|
+
markers = [
|
|
53
|
+
"integration: tests that download sample data or run the Gmsh SDK",
|
|
54
|
+
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""MTEX2Gmsh: generate conforming meshes from EBSD data processed with the Python port of MTEX.
|
|
2
|
+
|
|
3
|
+
Python port of the Matlab toolbox MTEX2Gmsh (Depriester & Kubler, JOSS 2020,
|
|
4
|
+
https://doi.org/10.21105/joss.02094), based on the ``mtex`` Python package and
|
|
5
|
+
the Gmsh Python SDK.
|
|
6
|
+
|
|
7
|
+
Examples
|
|
8
|
+
--------
|
|
9
|
+
>>> import mtex
|
|
10
|
+
>>> from mtex2gmsh import GmshGeo, LocalSize
|
|
11
|
+
>>> ebsd = mtex.mtexdata('small')
|
|
12
|
+
>>> grains = mtex.calcGrains(ebsd[ebsd.isIndexed])
|
|
13
|
+
>>> G = GmshGeo(grains)
|
|
14
|
+
>>> G.mesh('small.msh') # 3D wedge mesh, size = EBSD resolution
|
|
15
|
+
>>> G.mesh('small_tri.msh', element_type='Tri') # 2D triangular mesh
|
|
16
|
+
>>> G.savegeo('geometry.geo') # dump the Gmsh geometry
|
|
17
|
+
>>> G.export_grain_props('grains.csv') # grain ids, phases, orientations
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from .bspline import bspline, bspline_auto
|
|
21
|
+
from .gmsh_geo import GeoSize, GmshGeo
|
|
22
|
+
from .graph import euler_path
|
|
23
|
+
from .local_size import LocalSize, LocalSizeEntry
|
|
24
|
+
from .mesh_plot import plot_mesh
|
|
25
|
+
from .model import Grain
|
|
26
|
+
from .segments import compute_segments
|
|
27
|
+
from .singular_points import SingularPoints, find_singular_points
|
|
28
|
+
from .mtex_compat import grain_num_pixel, grain_vertices, vtx_on_bounds
|
|
29
|
+
|
|
30
|
+
__version__ = "1.0.0"
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"GmshGeo",
|
|
34
|
+
"GeoSize",
|
|
35
|
+
"Grain",
|
|
36
|
+
"LocalSize",
|
|
37
|
+
"LocalSizeEntry",
|
|
38
|
+
"SingularPoints",
|
|
39
|
+
"bspline",
|
|
40
|
+
"bspline_auto",
|
|
41
|
+
"compute_segments",
|
|
42
|
+
"euler_path",
|
|
43
|
+
"find_singular_points",
|
|
44
|
+
"grain_num_pixel",
|
|
45
|
+
"grain_vertices",
|
|
46
|
+
"plot_mesh",
|
|
47
|
+
"vtx_on_bounds",
|
|
48
|
+
"__version__",
|
|
49
|
+
]
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""B-spline approximation of a set of knots.
|
|
2
|
+
|
|
3
|
+
Thin wrapper around :class:`scipy.interpolate.BSpline` (de Boor's algorithm),
|
|
4
|
+
adding a ``periodic`` option for periodic approximation, as in the Matlab
|
|
5
|
+
``BSpline.m`` and ``bspline_auto.m`` of MTEX2Gmsh.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
from scipy.interpolate import BSpline
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def bspline(knots: np.ndarray, order: int = 3, nint: int = 10, periodic: bool = False) -> np.ndarray:
|
|
15
|
+
"""B-spline approximation from a set of coordinates.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
knots : (n, 2) array_like
|
|
20
|
+
Knot coordinates: ``knots[i, j]`` is the ``j``-th coordinate of the
|
|
21
|
+
``i``-th knot.
|
|
22
|
+
order : int, optional
|
|
23
|
+
Spline order (polynomial degree ``order - 1``), by default 3
|
|
24
|
+
(quadratic B-splines).
|
|
25
|
+
nint : int, optional
|
|
26
|
+
Number of sampled points per knot interval, by default 10.
|
|
27
|
+
periodic : bool, optional
|
|
28
|
+
If True, periodic conditions are used at the end knots. This remains
|
|
29
|
+
an approximation, not an interpolation: the knots stay control
|
|
30
|
+
points. By default False.
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
(m, 2) ndarray
|
|
35
|
+
The sampled B-spline curve (``(nint - 1) * (n - 1) + 1`` points for
|
|
36
|
+
open splines; only the periodic part is kept for closed ones).
|
|
37
|
+
"""
|
|
38
|
+
knots = np.asarray(knots, dtype=float)
|
|
39
|
+
knots_in = knots
|
|
40
|
+
if periodic:
|
|
41
|
+
knots = np.vstack([knots[-order:], knots, knots[:order]])
|
|
42
|
+
|
|
43
|
+
p, q = knots.shape
|
|
44
|
+
if p <= 2:
|
|
45
|
+
return knots.copy()
|
|
46
|
+
|
|
47
|
+
n = (nint - 1) * (p - 1) + 1 # overall number of queried points
|
|
48
|
+
y = np.linspace(0, 1, n)
|
|
49
|
+
|
|
50
|
+
order = min(order, p)
|
|
51
|
+
t = np.concatenate([np.zeros(order - 1), np.linspace(0, 1, p - order + 2), np.ones(order - 1)])
|
|
52
|
+
|
|
53
|
+
BS = BSpline(t, knots, order - 1, extrapolate=False)(y)
|
|
54
|
+
BS[0, :] = knots[0, :]
|
|
55
|
+
BS[-1, :] = knots[-1, :]
|
|
56
|
+
|
|
57
|
+
if periodic:
|
|
58
|
+
# 1-based bounds of the periodic part, as in the Matlab code
|
|
59
|
+
k = np.searchsorted(t, y[:-1], side="right") - 1
|
|
60
|
+
m = np.arange(1, n)
|
|
61
|
+
low = k <= order
|
|
62
|
+
high = k >= p - order + 1
|
|
63
|
+
m_min = int(m[low].max()) if low.any() else 1
|
|
64
|
+
m_max = int(m[high].min()) if high.any() else n
|
|
65
|
+
BS = BS[m_min - 1 : m_max - 1, :]
|
|
66
|
+
if BS.shape[0] == 0:
|
|
67
|
+
# Degenerate (too short) periodic input: fall back to the knots
|
|
68
|
+
return knots_in.copy()
|
|
69
|
+
BS[-1, :] = BS[0, :]
|
|
70
|
+
return BS
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def bspline_auto(V: np.ndarray, nint: int = 10) -> np.ndarray:
|
|
74
|
+
"""Automatic choice of the B-spline order for a polyline ``V``.
|
|
75
|
+
|
|
76
|
+
Closed polylines (first knot == last knot) use a periodic B-spline,
|
|
77
|
+
open ones a clamped B-spline of order 4.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
V : (n, 2) array_like
|
|
82
|
+
Coordinates of the polyline vertices.
|
|
83
|
+
nint : int, optional
|
|
84
|
+
Number of sampled points per knot interval, by default 10.
|
|
85
|
+
|
|
86
|
+
Returns
|
|
87
|
+
-------
|
|
88
|
+
(m, 2) ndarray
|
|
89
|
+
The sampled B-spline curve.
|
|
90
|
+
"""
|
|
91
|
+
V = np.asarray(V, dtype=float)
|
|
92
|
+
if np.array_equal(V[0], V[-1]):
|
|
93
|
+
order = 3 if len(V) == 4 else 4
|
|
94
|
+
return bspline(V[:-1], order=order, nint=nint, periodic=True)
|
|
95
|
+
return bspline(V, order=4, nint=nint)
|