backup 04_physworld 2026-11-08 10:11:26 (part 20)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +9 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/INSTALLER +1 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/METADATA +249 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/RECORD +261 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/REQUESTED +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/WHEEL +5 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/entry_points.txt +2 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/licenses/LICENSE.md +21 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/top_level.txt +1 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/geometry.py +470 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/graph.py +1017 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/grouping.py +877 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/inertia.py +363 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__init__.py +3 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__pycache__/__init__.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__pycache__/blender.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__pycache__/generic.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/blender.py +142 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/generic.py +101 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/intersections.py +805 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interval.py +106 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/iteration.py +187 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/nsphere.py +181 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/parent.py +419 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__init__.py +18 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/__init__.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/arc.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/creation.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/curve.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/entities.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/intersections.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/packing.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/path.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/polygons.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/raster.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/repair.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/segments.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/simplify.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/traversal.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/util.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/arc.py +245 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/creation.py +294 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/curve.py +136 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/entities.py +838 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__init__.py +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/__init__.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/dxf.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/export.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/load.cpython-311.pyc +0 -0
- physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/misc.cpython-311.pyc +0 -0
.gitattributes
CHANGED
|
@@ -3777,3 +3777,12 @@ physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/setuptools/_vendor/mo
|
|
| 3777 |
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/setuptools/config/_validate_pyproject/__pycache__/fastjsonschema_validations.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3778 |
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/__pycache__/base.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3779 |
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/__pycache__/transformations.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3777 |
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/setuptools/config/_validate_pyproject/__pycache__/fastjsonschema_validations.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3778 |
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/__pycache__/base.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3779 |
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/__pycache__/transformations.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3780 |
+
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/vhacdx.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 3781 |
+
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/yaml/_yaml.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 3782 |
+
physworld_p1_20261018/envs/p1/lib/python3.11/test/support/__pycache__/__init__.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3783 |
+
physworld_p1_20261018/envs/p1/lib/python3.11/tkinter/__pycache__/__init__.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3784 |
+
physworld_p1_20261018/envs/p1/lib/python3.11/tkinter/__pycache__/tix.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3785 |
+
physworld_p1_20261018/envs/p1/lib/python3.11/unittest/__pycache__/mock.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3786 |
+
physworld_p1_20261018/envs/p1/lib/python3.11/urllib/__pycache__/request.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 3787 |
+
physworld_p1_20261018/envs/p1/lib/sqlite3.40.0/libsqlite3.40.0.so filter=lfs diff=lfs merge=lfs -text
|
| 3788 |
+
physworld_p1_20261018/envs/p1/lib/tk8.6/demos/images/teapot.ppm filter=lfs diff=lfs merge=lfs -text
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/INSTALLER
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pip
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/METADATA
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: trimesh
|
| 3 |
+
Version: 5.1.1
|
| 4 |
+
Summary: Import, export, process, analyze and view triangular meshes.
|
| 5 |
+
Author-email: Michael Dawson-Haggerty <mikedh@kerfed.com>
|
| 6 |
+
License: The MIT License (MIT)
|
| 7 |
+
|
| 8 |
+
Copyright (c) 2023 Michael Dawson-Haggerty
|
| 9 |
+
|
| 10 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 11 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 12 |
+
in the Software without restriction, including without limitation the rights
|
| 13 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 14 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 15 |
+
furnished to do so, subject to the following conditions:
|
| 16 |
+
|
| 17 |
+
The above copyright notice and this permission notice shall be included in all
|
| 18 |
+
copies or substantial portions of the Software.
|
| 19 |
+
|
| 20 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 21 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 22 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 23 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 24 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 25 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 26 |
+
SOFTWARE.
|
| 27 |
+
|
| 28 |
+
Project-URL: homepage, https://github.com/mikedh/trimesh
|
| 29 |
+
Project-URL: documentation, https://trimesh.org
|
| 30 |
+
Keywords: graphics,mesh,geometry,3D
|
| 31 |
+
Classifier: Development Status :: 4 - Beta
|
| 32 |
+
Classifier: License :: OSI Approved :: MIT License
|
| 33 |
+
Classifier: Programming Language :: Python
|
| 34 |
+
Classifier: Programming Language :: Python :: 3.10
|
| 35 |
+
Classifier: Programming Language :: Python :: 3.11
|
| 36 |
+
Classifier: Programming Language :: Python :: 3.12
|
| 37 |
+
Classifier: Programming Language :: Python :: 3.13
|
| 38 |
+
Classifier: Natural Language :: English
|
| 39 |
+
Classifier: Topic :: Scientific/Engineering
|
| 40 |
+
Classifier: Topic :: Multimedia :: Graphics
|
| 41 |
+
Classifier: Topic :: Multimedia :: Graphics :: 3D Modeling
|
| 42 |
+
Requires-Python: >=3.10
|
| 43 |
+
Description-Content-Type: text/markdown
|
| 44 |
+
License-File: LICENSE.md
|
| 45 |
+
Requires-Dist: numpy>=1.21
|
| 46 |
+
Provides-Extra: easy
|
| 47 |
+
Requires-Dist: colorlog; extra == "easy"
|
| 48 |
+
Requires-Dist: manifold3d>=2.3.0; extra == "easy"
|
| 49 |
+
Requires-Dist: charset-normalizer; extra == "easy"
|
| 50 |
+
Requires-Dist: lxml; extra == "easy"
|
| 51 |
+
Requires-Dist: jsonschema; extra == "easy"
|
| 52 |
+
Requires-Dist: networkx; extra == "easy"
|
| 53 |
+
Requires-Dist: svg.path; extra == "easy"
|
| 54 |
+
Requires-Dist: pycollada; extra == "easy"
|
| 55 |
+
Requires-Dist: shapely; extra == "easy"
|
| 56 |
+
Requires-Dist: xxhash; extra == "easy"
|
| 57 |
+
Requires-Dist: rtree; extra == "easy"
|
| 58 |
+
Requires-Dist: httpx; extra == "easy"
|
| 59 |
+
Requires-Dist: scipy; extra == "easy"
|
| 60 |
+
Requires-Dist: embreex; platform_machine != "aarch64" and extra == "easy"
|
| 61 |
+
Requires-Dist: pillow; extra == "easy"
|
| 62 |
+
Requires-Dist: vhacdx; extra == "easy"
|
| 63 |
+
Requires-Dist: mapbox_earcut>=1.0.2; extra == "easy"
|
| 64 |
+
Provides-Extra: recommend
|
| 65 |
+
Requires-Dist: sympy; extra == "recommend"
|
| 66 |
+
Requires-Dist: pyglet<2; extra == "recommend"
|
| 67 |
+
Requires-Dist: scikit-image; extra == "recommend"
|
| 68 |
+
Requires-Dist: fast-simplification; extra == "recommend"
|
| 69 |
+
Requires-Dist: python-fcl; extra == "recommend"
|
| 70 |
+
Requires-Dist: cascadio; extra == "recommend"
|
| 71 |
+
Provides-Extra: test
|
| 72 |
+
Requires-Dist: pytest-cov; extra == "test"
|
| 73 |
+
Requires-Dist: pytest; extra == "test"
|
| 74 |
+
Requires-Dist: pyinstrument; extra == "test"
|
| 75 |
+
Requires-Dist: ruff; extra == "test"
|
| 76 |
+
Provides-Extra: test-more
|
| 77 |
+
Requires-Dist: ezdxf; extra == "test-more"
|
| 78 |
+
Requires-Dist: meshio; extra == "test-more"
|
| 79 |
+
Requires-Dist: xatlas; extra == "test-more"
|
| 80 |
+
Requires-Dist: pytest-beartype; extra == "test-more"
|
| 81 |
+
Requires-Dist: matplotlib; extra == "test-more"
|
| 82 |
+
Requires-Dist: pymeshlab; python_version < "3.14" and extra == "test-more"
|
| 83 |
+
Requires-Dist: triangle; python_version < "3.14" and extra == "test-more"
|
| 84 |
+
Requires-Dist: ipython; extra == "test-more"
|
| 85 |
+
Requires-Dist: marimo; extra == "test-more"
|
| 86 |
+
Requires-Dist: requests; extra == "test-more"
|
| 87 |
+
Requires-Dist: aiohttp; extra == "test-more"
|
| 88 |
+
Requires-Dist: DracoPy>=1.7.0; extra == "test-more"
|
| 89 |
+
Provides-Extra: deprecated
|
| 90 |
+
Provides-Extra: all
|
| 91 |
+
Requires-Dist: trimesh[deprecated,easy,recommend,test,test_more]; extra == "all"
|
| 92 |
+
Dynamic: license-file
|
| 93 |
+
|
| 94 |
+
[](http://trimesh.org)
|
| 95 |
+
|
| 96 |
+
-----------
|
| 97 |
+
[](https://github.com/mikedh/trimesh/actions) [](https://trimesh.org/coverage/) [](https://hub.docker.com/r/trimesh/trimesh/tags) [](https://badge.fury.io/py/trimesh)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
Trimesh is a pure Python 3.10+ library for loading and using [triangular meshes](https://en.wikipedia.org/wiki/Triangle_mesh) with an emphasis on watertight surfaces. The goal of the library is to provide a full featured and well tested Trimesh object which allows for easy manipulation and analysis, in the style of the Polygon object in the [Shapely library](https://github.com/Toblerity/Shapely).
|
| 101 |
+
|
| 102 |
+
The API is mostly stable, but this should not be relied on and is not guaranteed: install a specific version if you plan on deploying something using `trimesh`.
|
| 103 |
+
|
| 104 |
+
Pull requests are appreciated and responded to promptly! If you'd like to contribute, here a quick [development and contributing guide.](https://trimesh.org/contributing.html)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
## Basic Installation
|
| 108 |
+
|
| 109 |
+
Keeping `trimesh` easy to install is a core goal, thus the *only* hard dependency is [`numpy`](http://www.numpy.org/). Installing other packages adds functionality but is not required. For the easiest install with just `numpy`:
|
| 110 |
+
|
| 111 |
+
```bash
|
| 112 |
+
pip install trimesh
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
The minimal install can load many supported formats (STL, PLY, OBJ, GLTF/GLB) into `numpy.ndarray` values. More functionality is available when [soft dependencies are installed](https://trimesh.org/install#dependency-overview), including convex hulls (`scipy`), graph operations (`networkx`), fast ray queries (`embreex`), vector path handling (`shapely` and `rtree`), XML formats like 3DXML/XAML/3MF (`lxml`), preview windows (`pyglet`), faster cache checks (`xxhash`), etc.
|
| 116 |
+
|
| 117 |
+
To install `trimesh` with the soft dependencies that generally install cleanly from binaries on Linux x86_64, MacOS ARM, and Windows x86_64 using `pip`:
|
| 118 |
+
```bash
|
| 119 |
+
pip install trimesh[easy]
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
If you are supporting a different platform or are freezing dependencies for an application we recommend you do not use extras, i.e. depend on `trimesh scipy` versus `trimesh[easy]`. Further information is available in the [advanced installation documentation](https://trimesh.org/install.html).
|
| 123 |
+
|
| 124 |
+
## Quick Start
|
| 125 |
+
|
| 126 |
+
Here is an example of loading a mesh from file and colorizing its faces ([nicely formatted notebook version](https://trimesh.org/quick_start.html) of this example.
|
| 127 |
+
|
| 128 |
+
```python
|
| 129 |
+
import numpy as np
|
| 130 |
+
import trimesh
|
| 131 |
+
|
| 132 |
+
# attach to logger so trimesh messages will be printed to console
|
| 133 |
+
trimesh.util.attach_to_log()
|
| 134 |
+
|
| 135 |
+
# mesh objects can be created from existing faces and vertex data
|
| 136 |
+
mesh = trimesh.Trimesh(vertices=[[0, 0, 0], [0, 0, 1], [0, 1, 0]], faces=[[0, 1, 2]])
|
| 137 |
+
|
| 138 |
+
# by default, Trimesh will do a light processing, which will
|
| 139 |
+
# remove any NaN values and merge vertices that share position
|
| 140 |
+
# if you want to not do this on load, you can pass `process=False`
|
| 141 |
+
mesh = trimesh.Trimesh(
|
| 142 |
+
vertices=[[0, 0, 0], [0, 0, 1], [0, 1, 0]], faces=[[0, 1, 2]], process=False
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# some formats like `glb` represent multiple meshes with multiple instances
|
| 146 |
+
# and `load_mesh` will concatenate irreversibly, load it as a Scene
|
| 147 |
+
# if you need instance information:
|
| 148 |
+
# `scene = trimesh.load_scene('models/CesiumMilkTruck.glb')`
|
| 149 |
+
mesh = trimesh.load_mesh("models/CesiumMilkTruck.glb")
|
| 150 |
+
|
| 151 |
+
# is the current mesh watertight?
|
| 152 |
+
mesh.is_watertight
|
| 153 |
+
|
| 154 |
+
# what's the euler number for the mesh?
|
| 155 |
+
mesh.euler_number
|
| 156 |
+
|
| 157 |
+
# the convex hull is another Trimesh object that is available as a property
|
| 158 |
+
# lets compare the volume of our mesh with the volume of its convex hull
|
| 159 |
+
print(mesh.volume / mesh.convex_hull.volume)
|
| 160 |
+
|
| 161 |
+
# since the mesh is watertight it means there is a volume
|
| 162 |
+
# with a center of mass calculated from a surface integral approach
|
| 163 |
+
# which we can set as the origin for our mesh. It's perfectly fine to
|
| 164 |
+
# alter the vertices directly:
|
| 165 |
+
# mesh.vertices -= mesh.center_mass
|
| 166 |
+
# although this will completely clear the cache including face normals
|
| 167 |
+
# as we don't know that they're still valid. Using the translation
|
| 168 |
+
# method will try to save cached values that are still valid:
|
| 169 |
+
mesh.apply_translation(-mesh.center_mass)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
# what's the (3, 3) moment of inertia for the mesh?
|
| 173 |
+
mesh.moment_inertia
|
| 174 |
+
|
| 175 |
+
# if there are multiple bodies in the mesh we can split the mesh by
|
| 176 |
+
# connected components of face adjacency
|
| 177 |
+
# since this example mesh is a single watertight body we get a list of one mesh
|
| 178 |
+
mesh.split()
|
| 179 |
+
|
| 180 |
+
# facets are groups of coplanar adjacent faces
|
| 181 |
+
# set each facet to a random color
|
| 182 |
+
# colors are 8 bit RGBA by default (n, 4) np.uint8
|
| 183 |
+
for facet in mesh.facets:
|
| 184 |
+
mesh.visual.face_colors[facet] = trimesh.visual.random_color()
|
| 185 |
+
|
| 186 |
+
# preview mesh in an opengl window if you installed pyglet and scipy with pip
|
| 187 |
+
mesh.show()
|
| 188 |
+
|
| 189 |
+
# transform method can be passed a (4, 4) matrix and will cleanly apply the transform
|
| 190 |
+
mesh.apply_transform(trimesh.transformations.random_rotation_matrix())
|
| 191 |
+
|
| 192 |
+
# axis aligned bounding box is available
|
| 193 |
+
mesh.bounding_box.extents
|
| 194 |
+
|
| 195 |
+
# a minimum volume oriented bounding box also available
|
| 196 |
+
# primitives are subclasses of Trimesh objects which automatically generate
|
| 197 |
+
# faces and vertices from data stored in the 'primitive' attribute
|
| 198 |
+
mesh.bounding_box_oriented.primitive.extents
|
| 199 |
+
mesh.bounding_box_oriented.primitive.transform
|
| 200 |
+
|
| 201 |
+
# show the mesh appended with its oriented bounding box
|
| 202 |
+
# the bounding box is a trimesh.primitives.Box object, which subclasses
|
| 203 |
+
# Trimesh and lazily evaluates to fill in vertices and faces when requested
|
| 204 |
+
# (press w in viewer to see triangles)
|
| 205 |
+
(mesh + mesh.bounding_box_oriented).show()
|
| 206 |
+
|
| 207 |
+
# bounding spheres and bounding cylinders of meshes are also
|
| 208 |
+
# available, and will be the minimum volume version of each
|
| 209 |
+
# except in certain degenerate cases, where they will be no worse
|
| 210 |
+
# than a least squares fit version of the primitive.
|
| 211 |
+
print(
|
| 212 |
+
mesh.bounding_box_oriented.volume,
|
| 213 |
+
mesh.bounding_cylinder.volume,
|
| 214 |
+
mesh.bounding_sphere.volume,
|
| 215 |
+
)
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
## Features
|
| 219 |
+
|
| 220 |
+
* Import meshes from binary/ASCII STL, Wavefront OBJ, ASCII OFF, binary/ASCII PLY, GLTF/GLB 2.0, 3MF, XAML, 3DXML, etc.
|
| 221 |
+
* Export meshes as GLB/GLTF, binary STL, binary PLY, ASCII OFF, OBJ, COLLADA, etc.
|
| 222 |
+
* Import and export 2D or 3D vector paths with DXF or SVG files
|
| 223 |
+
* Preview meshes using an OpenGL `pyglet` window, or in-line in jupyter or marimo notebooks using `three.js`
|
| 224 |
+
* Automatic hashing from a subclassed numpy array for change tracking using MD5, zlib CRC, or xxhash, and internal caching of expensive values.
|
| 225 |
+
* Calculate face adjacencies, face angles, vertex defects, convex hulls, etc.
|
| 226 |
+
* Calculate cross sections for a 2D outline, or slice a mesh for a 3D remainder mesh, i.e. slicing for 3D-printing.
|
| 227 |
+
* Split mesh based on face connectivity using networkx, or scipy.sparse
|
| 228 |
+
* Calculate mass properties, including volume, center of mass, moment of inertia, principal components of inertia, etc.
|
| 229 |
+
* Repair simple problems with triangle winding, normals, and quad/triangle holes
|
| 230 |
+
* Compute rotation/translation/tessellation invariant identifier and find duplicate meshes
|
| 231 |
+
* Check if a mesh is watertight, convex, etc.
|
| 232 |
+
* Sample the surface of a mesh
|
| 233 |
+
* Ray-mesh queries including location, triangle index, etc.
|
| 234 |
+
* Boolean operations on meshes (intersection, union, difference) using Manifold3D or Blender.
|
| 235 |
+
* Voxelize watertight meshes
|
| 236 |
+
* Smooth watertight meshes using Laplacian smoothing algorithms (Classic, Taubin, Humphrey)
|
| 237 |
+
* Subdivide faces of a mesh
|
| 238 |
+
* Approximate minimum volume oriented bounding boxes and spheres for meshes.
|
| 239 |
+
* Calculate nearest point on mesh surface and signed distance
|
| 240 |
+
* Primitive objects (Box, Cylinder, Sphere, Extrusion) which are subclassed Trimesh objects and have all the same features (inertia, viewers, etc)
|
| 241 |
+
* Simple scene graph and transform tree which can be rendered (pyglet window, three.js in a jupyter/marimo notebook or exported.
|
| 242 |
+
* Many utility functions, like transforming points, unitizing vectors, aligning vectors, tracking numpy arrays for changes, grouping rows, etc.
|
| 243 |
+
|
| 244 |
+
## Additional Notes
|
| 245 |
+
|
| 246 |
+
- Check out some cool stuff people have done in the [GitHub network](https://github.com/mikedh/trimesh/network/dependents).
|
| 247 |
+
- Generally `trimesh` API changes should have a one-year period of [printing a `warnings.DeprecationWarning`](https://trimesh.org/contributing.html#deprecations) although that's not always possible (i.e. the pyglet2 viewer rewrite that's been back-burnered for several years.)
|
| 248 |
+
- Docker containers are available on Docker Hub as [`trimesh/trimesh`](https://hub.docker.com/r/trimesh/trimesh/tags) and there's a [container guide](https://trimesh.org/docker.html) in the docs.
|
| 249 |
+
- If you're choosing which format to use, you may want to try [GLB](https://trimesh.org/formats.html) as a fast modern option.
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/RECORD
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
../../../bin/trimesh,sha256=6WWrPPhYHZQyedABrLWPV3Rqxv6J19eQ_CcTD9OsoMk,255
|
| 2 |
+
trimesh-5.1.1.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
| 3 |
+
trimesh-5.1.1.dist-info/METADATA,sha256=g_9dq972Sj7oLlYPQZwGx9XUBwTjzv4YB-oTbbebAbc,13280
|
| 4 |
+
trimesh-5.1.1.dist-info/RECORD,,
|
| 5 |
+
trimesh-5.1.1.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 6 |
+
trimesh-5.1.1.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
| 7 |
+
trimesh-5.1.1.dist-info/entry_points.txt,sha256=E8JnT07gjUrdCQjuWPKNykjis4Aa2QWCFN6gVFWNNh0,50
|
| 8 |
+
trimesh-5.1.1.dist-info/licenses/LICENSE.md,sha256=_ie174cQZ2zIUlXaKKMvOU4u8_TORwdaHHMqnqHXvQM,1090
|
| 9 |
+
trimesh-5.1.1.dist-info/top_level.txt,sha256=bHXSeALt9l4__maYNLp93GRZSOJ5iApS2fe3jibIV1U,8
|
| 10 |
+
trimesh/__init__.py,sha256=pQ2KnENG-gxfbKDCNO_9EsVIsb7lgZeruBBsteedM2A,2429
|
| 11 |
+
trimesh/__main__.py,sha256=1QP7e1idyhXpLIj_8BAgJ6wCXGgWh7-HobJxdJ_EdDs,1461
|
| 12 |
+
trimesh/__pycache__/__init__.cpython-311.pyc,,
|
| 13 |
+
trimesh/__pycache__/__main__.cpython-311.pyc,,
|
| 14 |
+
trimesh/__pycache__/base.cpython-311.pyc,,
|
| 15 |
+
trimesh/__pycache__/boolean.cpython-311.pyc,,
|
| 16 |
+
trimesh/__pycache__/bounds.cpython-311.pyc,,
|
| 17 |
+
trimesh/__pycache__/caching.cpython-311.pyc,,
|
| 18 |
+
trimesh/__pycache__/collision.cpython-311.pyc,,
|
| 19 |
+
trimesh/__pycache__/comparison.cpython-311.pyc,,
|
| 20 |
+
trimesh/__pycache__/constants.cpython-311.pyc,,
|
| 21 |
+
trimesh/__pycache__/convex.cpython-311.pyc,,
|
| 22 |
+
trimesh/__pycache__/creation.cpython-311.pyc,,
|
| 23 |
+
trimesh/__pycache__/curvature.cpython-311.pyc,,
|
| 24 |
+
trimesh/__pycache__/decomposition.cpython-311.pyc,,
|
| 25 |
+
trimesh/__pycache__/exceptions.cpython-311.pyc,,
|
| 26 |
+
trimesh/__pycache__/geometry.cpython-311.pyc,,
|
| 27 |
+
trimesh/__pycache__/graph.cpython-311.pyc,,
|
| 28 |
+
trimesh/__pycache__/grouping.cpython-311.pyc,,
|
| 29 |
+
trimesh/__pycache__/inertia.cpython-311.pyc,,
|
| 30 |
+
trimesh/__pycache__/intersections.cpython-311.pyc,,
|
| 31 |
+
trimesh/__pycache__/interval.cpython-311.pyc,,
|
| 32 |
+
trimesh/__pycache__/iteration.cpython-311.pyc,,
|
| 33 |
+
trimesh/__pycache__/nsphere.cpython-311.pyc,,
|
| 34 |
+
trimesh/__pycache__/parent.cpython-311.pyc,,
|
| 35 |
+
trimesh/__pycache__/permutate.cpython-311.pyc,,
|
| 36 |
+
trimesh/__pycache__/points.cpython-311.pyc,,
|
| 37 |
+
trimesh/__pycache__/poses.cpython-311.pyc,,
|
| 38 |
+
trimesh/__pycache__/primitives.cpython-311.pyc,,
|
| 39 |
+
trimesh/__pycache__/proximity.cpython-311.pyc,,
|
| 40 |
+
trimesh/__pycache__/registration.cpython-311.pyc,,
|
| 41 |
+
trimesh/__pycache__/remesh.cpython-311.pyc,,
|
| 42 |
+
trimesh/__pycache__/rendering.cpython-311.pyc,,
|
| 43 |
+
trimesh/__pycache__/repair.cpython-311.pyc,,
|
| 44 |
+
trimesh/__pycache__/resolvers.cpython-311.pyc,,
|
| 45 |
+
trimesh/__pycache__/sample.cpython-311.pyc,,
|
| 46 |
+
trimesh/__pycache__/schemas.cpython-311.pyc,,
|
| 47 |
+
trimesh/__pycache__/smoothing.cpython-311.pyc,,
|
| 48 |
+
trimesh/__pycache__/transformations.cpython-311.pyc,,
|
| 49 |
+
trimesh/__pycache__/triangles.cpython-311.pyc,,
|
| 50 |
+
trimesh/__pycache__/typed.cpython-311.pyc,,
|
| 51 |
+
trimesh/__pycache__/units.cpython-311.pyc,,
|
| 52 |
+
trimesh/__pycache__/util.cpython-311.pyc,,
|
| 53 |
+
trimesh/__pycache__/version.cpython-311.pyc,,
|
| 54 |
+
trimesh/base.py,sha256=tNFkLFqFsdgDhcjCVT_JM3tpJh4UxfUSiOm2wW6rwJg,110248
|
| 55 |
+
trimesh/boolean.py,sha256=cMcnOPcdCxIGnjTDvcUd__RK-BioWbaxTQV82XTYwUU,5898
|
| 56 |
+
trimesh/bounds.py,sha256=e6FJj3AhbGb6baXMyLJkHekdvZGFpjacTNeVrp1NZhk,20977
|
| 57 |
+
trimesh/caching.py,sha256=SqI5pyXpep3JRAjmzlTLhQ1GECa5lAkqjyV6Qz7aEAc,19966
|
| 58 |
+
trimesh/collision.py,sha256=JTfGM-VK6VvLmZuygnXUz8V4L_KGLwUxieKe_uQzTCY,24034
|
| 59 |
+
trimesh/comparison.py,sha256=mQM24IEeq6lFdarFRtGSWzcDnadgK2KUFFA_TAxUQUE,5233
|
| 60 |
+
trimesh/constants.py,sha256=xJ1ln2A1outZwxiniEgY7eaKheZCsJGvn6S-ro36y3g,4937
|
| 61 |
+
trimesh/convex.py,sha256=0BtRUcKQs9rfqESUXg4LtJuiZTBLrrs1o2ZC0m2Jqmo,11644
|
| 62 |
+
trimesh/creation.py,sha256=5DqE1zHk9kqN-15JvqtOaR06Wv9wfrva9LBTYFzMMuo,49403
|
| 63 |
+
trimesh/curvature.py,sha256=W84yWS37-SJIXbxDpPnAm6ir2qJwTLnwCpBwwxooczs,6643
|
| 64 |
+
trimesh/decomposition.py,sha256=TEIQITE_dMBV7yZcQomHYyxB17fIuPhMBpKOtfOqGLs,1472
|
| 65 |
+
trimesh/exceptions.py,sha256=GzU9xHcbuTuMMw1_AmcgPaSB8Q7FmCUfKa3x1Xvxb3A,1464
|
| 66 |
+
trimesh/exchange/__init__.py,sha256=mM-22o0bUVOJdj3EMUGdN3d1cvRI1JD_2TQs57jugIw,297
|
| 67 |
+
trimesh/exchange/__pycache__/__init__.cpython-311.pyc,,
|
| 68 |
+
trimesh/exchange/__pycache__/binvox.cpython-311.pyc,,
|
| 69 |
+
trimesh/exchange/__pycache__/cascade.cpython-311.pyc,,
|
| 70 |
+
trimesh/exchange/__pycache__/common.cpython-311.pyc,,
|
| 71 |
+
trimesh/exchange/__pycache__/dae.cpython-311.pyc,,
|
| 72 |
+
trimesh/exchange/__pycache__/export.cpython-311.pyc,,
|
| 73 |
+
trimesh/exchange/__pycache__/load.cpython-311.pyc,,
|
| 74 |
+
trimesh/exchange/__pycache__/misc.cpython-311.pyc,,
|
| 75 |
+
trimesh/exchange/__pycache__/obj.cpython-311.pyc,,
|
| 76 |
+
trimesh/exchange/__pycache__/off.cpython-311.pyc,,
|
| 77 |
+
trimesh/exchange/__pycache__/ply.cpython-311.pyc,,
|
| 78 |
+
trimesh/exchange/__pycache__/stl.cpython-311.pyc,,
|
| 79 |
+
trimesh/exchange/__pycache__/threedxml.cpython-311.pyc,,
|
| 80 |
+
trimesh/exchange/__pycache__/threemf.cpython-311.pyc,,
|
| 81 |
+
trimesh/exchange/__pycache__/urdf.cpython-311.pyc,,
|
| 82 |
+
trimesh/exchange/__pycache__/xaml.cpython-311.pyc,,
|
| 83 |
+
trimesh/exchange/__pycache__/xyz.cpython-311.pyc,,
|
| 84 |
+
trimesh/exchange/binvox.py,sha256=h8ITHHDE0Pd6kEdMavhk0h61pNQON-UqBxQJJKKyx88,18602
|
| 85 |
+
trimesh/exchange/cascade.py,sha256=kOApQAk6pqH_Uyd9F9_PJGv6bZ06329A73IadvfSBfM,2097
|
| 86 |
+
trimesh/exchange/common.py,sha256=WKm7MsW1Rgt_4-yMsLF2REm0YyxIIs_JHaZkL0C_viA,457
|
| 87 |
+
trimesh/exchange/dae.py,sha256=jYDDo4NcjHrKlMFJYk8bTQ_L7CgAy074V8yIXOh8pFM,14910
|
| 88 |
+
trimesh/exchange/export.py,sha256=9zgkbp05jsK-PLi4kVb6ldVG4tJWFgsf8_pN-zU0874,10460
|
| 89 |
+
trimesh/exchange/gltf/__init__.py,sha256=C-urs6KKnnV3Pd9BNaURmNwQetYbu89N4oAZFBfiFZw,79987
|
| 90 |
+
trimesh/exchange/gltf/__pycache__/__init__.cpython-311.pyc,,
|
| 91 |
+
trimesh/exchange/gltf/__pycache__/extensions.cpython-311.pyc,,
|
| 92 |
+
trimesh/exchange/gltf/extensions.py,sha256=Jc9E7vRAoRNOq9XOEOzixAJUuk3nYeWdkzNpFVITpyo,15074
|
| 93 |
+
trimesh/exchange/load.py,sha256=azE8Hw_5KV4c32pVZ1iPXffVwC7qEjU_ydFXQNGank4,21516
|
| 94 |
+
trimesh/exchange/misc.py,sha256=nMNJCchtfRCr6UtOYyIolwxwc1nWoB2ANUvHPPGDNvw,4762
|
| 95 |
+
trimesh/exchange/obj.py,sha256=KFo4CYCnW3T76FhdtrmGlnorm-ZoDiNast0rRqK8tO0,37496
|
| 96 |
+
trimesh/exchange/off.py,sha256=56CDVdrHo5nf1oaF_gYDNMVSSy6dfQtXi40PmYkVdmU,2958
|
| 97 |
+
trimesh/exchange/ply.py,sha256=gTWeQ4ENC_lGZcDvn1bqGr4gy8pNs2mCn6yY5VPvz9Y,37760
|
| 98 |
+
trimesh/exchange/stl.py,sha256=DsnDQGEXLJ_f1Lfp-_pb3qsWqd_QADSN6t524YOOfyg,9957
|
| 99 |
+
trimesh/exchange/threedxml.py,sha256=zV2yftXAS5CdBdJ49qYsaSu04R7bH8FWrBoiOj-zNoo,15995
|
| 100 |
+
trimesh/exchange/threemf.py,sha256=xj13A04L3RKCUy9oqx0nlhA4gcRGRdxC-bX4aPTg-vQ,19650
|
| 101 |
+
trimesh/exchange/urdf.py,sha256=3Uz9TvL3k9e3mdOlZAF50EfAEcSqsjr_MG0inBScchw,5361
|
| 102 |
+
trimesh/exchange/xaml.py,sha256=HgfS_zrHckm0588PtvWM2HsmLXotqdXOGV-m82luwLY,5436
|
| 103 |
+
trimesh/exchange/xyz.py,sha256=p8ilPbWhuwy7kLMHXWABT5BqrzwdwJzDKGAQmNIeIwI,2961
|
| 104 |
+
trimesh/geometry.py,sha256=NTAibuYEGSKHxtgQtpUysLJSKTtdfmpDWV8K60pUNsE,14805
|
| 105 |
+
trimesh/graph.py,sha256=JjQSL5WnfEo6dfN3mIvzCRc5_iZFwYSMDhPapfJdtoU,31460
|
| 106 |
+
trimesh/grouping.py,sha256=9-pbqtMh9wTQfzfcmbj98SulMW-14JotT9VEh-kblXk,27131
|
| 107 |
+
trimesh/inertia.py,sha256=rg9uT0VQC31NnrczngvwsqjP3_2uJjhWJidId5jJMr4,10931
|
| 108 |
+
trimesh/interfaces/__init__.py,sha256=17dhVVvYQG-0ynilMhw2HX1X8vTByVksjwASnNtwoN4,45
|
| 109 |
+
trimesh/interfaces/__pycache__/__init__.cpython-311.pyc,,
|
| 110 |
+
trimesh/interfaces/__pycache__/blender.cpython-311.pyc,,
|
| 111 |
+
trimesh/interfaces/__pycache__/generic.cpython-311.pyc,,
|
| 112 |
+
trimesh/interfaces/blender.py,sha256=6jMWjAjc54eq5hj6IFxvlvxMYn6sWtdpCmn_XfwoFCU,4704
|
| 113 |
+
trimesh/interfaces/generic.py,sha256=7jCalpc1KfTi9ws-BmwMW1wAXQp9N2PGmepDZWNsRRs,3670
|
| 114 |
+
trimesh/intersections.py,sha256=E2T-SxiPCEOqVm63ExfxAUS2zNgQ0rDx4-boliBcfvg,28536
|
| 115 |
+
trimesh/interval.py,sha256=mi0Gny6nfzSZBVBQVSowIMhntlXj0FTdv4V5BAF4iuA,2746
|
| 116 |
+
trimesh/iteration.py,sha256=wQ9V1IAaldh7AvTm02m_c2jhG8-lDrhQXWsaRx5I4B0,5550
|
| 117 |
+
trimesh/nsphere.py,sha256=EVcDMIkUT9lzNpIvZ1JID_ib9b14lN9BJoqhcvDlX2I,5882
|
| 118 |
+
trimesh/parent.py,sha256=7ZJU6D0Pj7KoX4X6JnGaTfRtEw64-FSKPnP7HBe8PFU,12028
|
| 119 |
+
trimesh/path/__init__.py,sha256=Sfnb4j9VssDXhC45phbrFtcR5lw4k3MXQJg0UDz1y58,378
|
| 120 |
+
trimesh/path/__pycache__/__init__.cpython-311.pyc,,
|
| 121 |
+
trimesh/path/__pycache__/arc.cpython-311.pyc,,
|
| 122 |
+
trimesh/path/__pycache__/creation.cpython-311.pyc,,
|
| 123 |
+
trimesh/path/__pycache__/curve.cpython-311.pyc,,
|
| 124 |
+
trimesh/path/__pycache__/entities.cpython-311.pyc,,
|
| 125 |
+
trimesh/path/__pycache__/intersections.cpython-311.pyc,,
|
| 126 |
+
trimesh/path/__pycache__/packing.cpython-311.pyc,,
|
| 127 |
+
trimesh/path/__pycache__/path.cpython-311.pyc,,
|
| 128 |
+
trimesh/path/__pycache__/polygons.cpython-311.pyc,,
|
| 129 |
+
trimesh/path/__pycache__/raster.cpython-311.pyc,,
|
| 130 |
+
trimesh/path/__pycache__/repair.cpython-311.pyc,,
|
| 131 |
+
trimesh/path/__pycache__/segments.cpython-311.pyc,,
|
| 132 |
+
trimesh/path/__pycache__/simplify.cpython-311.pyc,,
|
| 133 |
+
trimesh/path/__pycache__/traversal.cpython-311.pyc,,
|
| 134 |
+
trimesh/path/__pycache__/util.cpython-311.pyc,,
|
| 135 |
+
trimesh/path/arc.py,sha256=9PxotC2p98OXTmYquHNy1eG2ipnrWhKaQQzPTwKyCLg,7689
|
| 136 |
+
trimesh/path/creation.py,sha256=bP5_piTXXdEWwlBod7tKXs-anAgVwsqanNR8JsoJE_c,8553
|
| 137 |
+
trimesh/path/curve.py,sha256=VER431Q72eFiX7ClyYtUDwGpx6Vc-T2XDpdgZjbL47g,3881
|
| 138 |
+
trimesh/path/entities.py,sha256=yFL2M1tjO6gxh-lBojwydYBuh-AOUiY4bUgshV9dgWk,22568
|
| 139 |
+
trimesh/path/exchange/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 140 |
+
trimesh/path/exchange/__pycache__/__init__.cpython-311.pyc,,
|
| 141 |
+
trimesh/path/exchange/__pycache__/dxf.cpython-311.pyc,,
|
| 142 |
+
trimesh/path/exchange/__pycache__/export.cpython-311.pyc,,
|
| 143 |
+
trimesh/path/exchange/__pycache__/load.cpython-311.pyc,,
|
| 144 |
+
trimesh/path/exchange/__pycache__/misc.cpython-311.pyc,,
|
| 145 |
+
trimesh/path/exchange/__pycache__/svg_io.cpython-311.pyc,,
|
| 146 |
+
trimesh/path/exchange/dxf.py,sha256=0_VFx8RZ786WewD4KHNMma8iIb8cT3o-1VtdqmiWS80,33130
|
| 147 |
+
trimesh/path/exchange/export.py,sha256=i0hHHk87-xzJo-aftc7O6yp57zN6yppr-eGIh4-hT54,2004
|
| 148 |
+
trimesh/path/exchange/load.py,sha256=pDeXLbGnjeVFHZs_UyPqxBxPscsISBwrNP8OHyzgRG4,2881
|
| 149 |
+
trimesh/path/exchange/misc.py,sha256=6zAm5HxX1uu8Qhm6HASF0l0ow8FNtRwFBWtJYhuABtQ,6540
|
| 150 |
+
trimesh/path/exchange/svg_io.py,sha256=duQIVwfvBMBx1P6NiB5aKgM5-q4EdzTQYgw3sgUZTBs,26446
|
| 151 |
+
trimesh/path/intersections.py,sha256=G5AGVQxKxmpgHO4JQg44D-Ldfg6IkxtgzEePee_WGC4,2238
|
| 152 |
+
trimesh/path/packing.py,sha256=_cthqydIedFJe6tFcxZ5u_phGcI8HhNuhIV_1v9v_1E,26347
|
| 153 |
+
trimesh/path/path.py,sha256=lP6vHhYoUMAzTpOWgtDQChIFbw3LFfY821IujbWoR_s,48469
|
| 154 |
+
trimesh/path/polygons.py,sha256=dvDi3rUr7aQvEC4qKjWgsuJF5D-laxrL3g-VTg5Flzo,32695
|
| 155 |
+
trimesh/path/raster.py,sha256=V4oucSE0kx1o8UV9kZuLw28AgLyCvGcr8mIf2zRp2dc,3175
|
| 156 |
+
trimesh/path/repair.py,sha256=nxreZyvWvRB3v1LimbgNEAww-ZoNkr5A-CaklyBFrk8,3237
|
| 157 |
+
trimesh/path/segments.py,sha256=KkgbavWND24Z6tg0HAR4Rp2Hc8HIoBjHzoMo-1Kbs1o,16604
|
| 158 |
+
trimesh/path/simplify.py,sha256=zJfUxCRbCnsetjHsltp-bHr8SnhhSo2StK_AsIAXzSw,12553
|
| 159 |
+
trimesh/path/traversal.py,sha256=OHDqMq5nEusvlF049NP1mVdt2cU6GjJyJY6Wnm8BuNo,16259
|
| 160 |
+
trimesh/path/util.py,sha256=aoHsefWw6TFfNCfbjTNLJc4V5ePTL1JTziu2-3MPJwI,1767
|
| 161 |
+
trimesh/permutate.py,sha256=1nNfIzwuIcgYWXktPiGCuXqsDaxSoiGViqc2r13CGPQ,5141
|
| 162 |
+
trimesh/points.py,sha256=zd4m96sCktwElejFisT7JRZzku9yJ3n_s8jQkVJ6WwQ,22283
|
| 163 |
+
trimesh/poses.py,sha256=FX5nBJswYM1Wq-m3zAPClop0JnVdk_XOZjZwnZYJFh8,10437
|
| 164 |
+
trimesh/primitives.py,sha256=qYYZq1Z4VW4RgGxS5-n3a5-wYwVSygxxNqJGzh-_PG8,35345
|
| 165 |
+
trimesh/proximity.py,sha256=6cLcx2jWnSJvlE7p6xLRsgNR9G4IBufvuLR8Kvf7e3o,19445
|
| 166 |
+
trimesh/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 167 |
+
trimesh/ray/__init__.py,sha256=5lNzMoKlQygP28tnIQgXNuoB9sjWdO92ZSdE44l_EI0,346
|
| 168 |
+
trimesh/ray/__pycache__/__init__.cpython-311.pyc,,
|
| 169 |
+
trimesh/ray/__pycache__/ray_pyembree.cpython-311.pyc,,
|
| 170 |
+
trimesh/ray/__pycache__/ray_triangle.cpython-311.pyc,,
|
| 171 |
+
trimesh/ray/__pycache__/ray_util.cpython-311.pyc,,
|
| 172 |
+
trimesh/ray/ray_pyembree.py,sha256=ikR-4VB09DvQTVvw81ZXxBbl6vukbBPJkYjE5KoqSng,11555
|
| 173 |
+
trimesh/ray/ray_triangle.py,sha256=Dz5qOH7FXGKsmULoGZ3LpHDQ90P-y-M1-urNC4UjIQE,12741
|
| 174 |
+
trimesh/ray/ray_util.py,sha256=vAJgDSI7q25YK7Geqrh1UJwAwP_RVH6AN4bECMdge7Q,4438
|
| 175 |
+
trimesh/registration.py,sha256=h6e6yea8JKW61yVBY13ipkKYMWEWwEB27DMgLwW8Q2I,41820
|
| 176 |
+
trimesh/remesh.py,sha256=I5BlDSlsE_T4w1No81H-GJEOQb4qx65P0mXkk8e5b1w,16950
|
| 177 |
+
trimesh/rendering.py,sha256=gww6NqyK9tE5o4VKUcqStief6sOYLfgip9I-uCIgyuM,12296
|
| 178 |
+
trimesh/repair.py,sha256=q9uMeEfnL-vEwGRB4EYlAqysN85pjWV2TXrmtgliTdo,12321
|
| 179 |
+
trimesh/resolvers.py,sha256=cuujk5MMvnLVLzSSYDOYO1yBZKHhhPLVvaT2Tu0efiU,21119
|
| 180 |
+
trimesh/resources/__init__.py,sha256=gNYXMa6f-GDU3RFnBl-d0LH4pItbvrfTn0lgrQWAnjU,2597
|
| 181 |
+
trimesh/resources/__pycache__/__init__.cpython-311.pyc,,
|
| 182 |
+
trimesh/resources/color_map.json.gzip,sha256=0Edta1aXREyLNZ_ZM-fv_eEAIzKa7dvr8TLYA-wDFzU,16762
|
| 183 |
+
trimesh/resources/creation.json,sha256=i72dHuxNOk2gbnSjYhWGTEL-WzeWdWR1e3t_3iDP0Ys,1084
|
| 184 |
+
trimesh/resources/schema/README.md,sha256=ZwcnLfRg6Qd1eRMm3Kt9S-oi2yYh2RZT8pllnbYe0J0,443
|
| 185 |
+
trimesh/resources/schema/gltf2.schema.zip,sha256=y9Fpkpfke71DmIXJO2gH5X4_gws7LXBvw6lEiOswjKg,24785
|
| 186 |
+
trimesh/resources/schema/primitive/box.schema.json,sha256=STE93MwWVDyIbBTltXkVTXh6RmcALWO3JKAPrW5BpGQ,814
|
| 187 |
+
trimesh/resources/schema/primitive/capsule.schema.json,sha256=347KHrkcRoGmwbTRxmOTDOTNFuf0pGRlOwGMGxOAeJc,774
|
| 188 |
+
trimesh/resources/schema/primitive/cylinder.schema.json,sha256=Qvc2GhCsFDPLLx0PpD_zZE3I09lAjRbK7ZMDvyY7S34,785
|
| 189 |
+
trimesh/resources/schema/primitive/extrusion.schema.json,sha256=HtKJPxN8DYFXDzVocthjZazbyx94TZlumVu_eRSKm0U,688
|
| 190 |
+
trimesh/resources/schema/primitive/primitive.schema.json,sha256=GlsChe96ci2ptVzAn4_VU52xrvNdBewq77qzQQVWtEg,479
|
| 191 |
+
trimesh/resources/schema/primitive/scenegraph.schema.json,sha256=Gn8inknthqkoXDyWjtVZdgO0nvC6aqWP58ESttGPDAU,791
|
| 192 |
+
trimesh/resources/schema/primitive/sphere.schema.json,sha256=VvCqHrhVMSSyTBowtjBnUkeh6SX-pAK66_t6XfMLLvk,522
|
| 193 |
+
trimesh/resources/schema/primitive/transform.schema.json,sha256=y4UBFIkUQ56ZUHcezFKLLnr68-ZQbWRtAGPKQsxSAlw,469
|
| 194 |
+
trimesh/resources/schema/primitive/trimesh.schema.json,sha256=ioYYpvcrxKQirkbyHxKxh2plBtRXaV9fQU0Oq3WYt8U,859
|
| 195 |
+
trimesh/resources/schema/primitive/wkt.polygon.schema.json,sha256=ryizpoS5vuRWC1YA_LrfFgE4D-32wjUu4X_7_opottM,413
|
| 196 |
+
trimesh/resources/schema/urdf.xsd,sha256=qiA_TEiCkEc8JOL91auwwSpa8xPy49OF0eWAtn1BEkE,11705
|
| 197 |
+
trimesh/resources/templates/base.svg,sha256=MYfPqEHyr5ik4RVyHhludqVaPFJ5Wxjd_Pd32XK6y8c,190
|
| 198 |
+
trimesh/resources/templates/blender_boolean.py.tmpl,sha256=uJ8W5QN-Alq-sRpA7MaJ1Xm3BxiIsEWbsUmTlHRPwIg,2012
|
| 199 |
+
trimesh/resources/templates/blender_unwrap.py.template,sha256=GJOEzRKEtskHwD9PfyvNdEgA3umFeKlURrS0m0kp1As,1147
|
| 200 |
+
trimesh/resources/templates/dxf.json,sha256=E3CfXHa_S5NIyt3FPd4WhLBrhJCQqFSdq9LCUtmLxlo,15667
|
| 201 |
+
trimesh/resources/templates/path.svg,sha256=JPEKKh2lPcwuM0JrOl6iCUIjci-yrlUqq8T-AWUYyX0,36
|
| 202 |
+
trimesh/resources/templates/ply.json,sha256=MxeHS5AqDqw4eQb_frkAoywxpkJxjSdcHu9DyPXpNkA,559
|
| 203 |
+
trimesh/resources/templates/viewer.zip,sha256=chvLtgz_KYjO3-VHWSmU3tdAo0W6QJDhft3jgZMHScU,173435
|
| 204 |
+
trimesh/resources/units_to_inches.json,sha256=rq_7NUdr-xptFoMX9ahCu4OLPGLxccKragKi5PdbT2w,1265
|
| 205 |
+
trimesh/sample.py,sha256=jf7T9gbqMUbzOGTbGYkbOTk164JT0tpXaZVKxjfBkFE,8629
|
| 206 |
+
trimesh/scene/__init__.py,sha256=vLW7-tLswsCiiANiRFeZ8LZpCyiEbeBXw9ZzDC64bTg,141
|
| 207 |
+
trimesh/scene/__pycache__/__init__.cpython-311.pyc,,
|
| 208 |
+
trimesh/scene/__pycache__/cameras.cpython-311.pyc,,
|
| 209 |
+
trimesh/scene/__pycache__/lighting.cpython-311.pyc,,
|
| 210 |
+
trimesh/scene/__pycache__/scene.cpython-311.pyc,,
|
| 211 |
+
trimesh/scene/__pycache__/transforms.cpython-311.pyc,,
|
| 212 |
+
trimesh/scene/cameras.py,sha256=NpkKA6lc5feW8RvO6QvdEjro3ZZ3ScVNx-lsOeaCmi0,12166
|
| 213 |
+
trimesh/scene/lighting.py,sha256=PbjxN_yUxFHIWKzrvTyD2Mnq2nJhiK7kkn97VSExjQE,8723
|
| 214 |
+
trimesh/scene/scene.py,sha256=bdAe_Qntrlj502Q9c_nKlDkEyzU6UvnY9mMyE7aE4Vs,54380
|
| 215 |
+
trimesh/scene/transforms.py,sha256=84vrEYl0FyxCJw0DXzu3frXTdN6MJRqre84aM6--jqI,28938
|
| 216 |
+
trimesh/schemas.py,sha256=EKTwX16mVeQ5IPRQhXCoiDIIet_9DaYVNKnUx0MoMKo,1287
|
| 217 |
+
trimesh/smoothing.py,sha256=5uret7Yp6MGTnwxIspVQDHJuaoDxrtctuAJ6bpTy6h0,12231
|
| 218 |
+
trimesh/transformations.py,sha256=ZEsRJzYSS3gDwCiiSCeZJtEPdIZJY0AG1JOpByLq42A,74801
|
| 219 |
+
trimesh/triangles.py,sha256=uBXNJOqeLAJSzd5ulgJsxH6MfzfUYpbbp-TvD5jORyc,22828
|
| 220 |
+
trimesh/typed.py,sha256=ljmWfdx978-PW8GVmPicqM58Bk-ptSipqcT7aNndWOs,4572
|
| 221 |
+
trimesh/units.py,sha256=B_n8Gp2DC2wvjOM8xQZFtswfoMGmAKaXgUfJVVepyXA,4600
|
| 222 |
+
trimesh/util.py,sha256=JR8stHxeNUfTQUGqWDmwJjjUtJIfhXEIpe1oWvYhP3Y,75569
|
| 223 |
+
trimesh/version.py,sha256=een2lq8n1QFi2zucBxUkfirUmKB7jhHyCDSumq5UeEM,1352
|
| 224 |
+
trimesh/viewer/__init__.py,sha256=GexDehAA_EQNOHrD9v7D50UHzjRb-x6-8YrPCcx87GM,842
|
| 225 |
+
trimesh/viewer/__pycache__/__init__.cpython-311.pyc,,
|
| 226 |
+
trimesh/viewer/__pycache__/notebook.cpython-311.pyc,,
|
| 227 |
+
trimesh/viewer/__pycache__/trackball.cpython-311.pyc,,
|
| 228 |
+
trimesh/viewer/__pycache__/windowed.cpython-311.pyc,,
|
| 229 |
+
trimesh/viewer/notebook.py,sha256=yQieeH6aP0T2VrfxhZXUxJfZwmR8PThTJWaY43KU-Xk,4652
|
| 230 |
+
trimesh/viewer/trackball.py,sha256=Ac223FA8TuI5NqtylRjsbwIrKOvohy5k2m3FyFSOXW4,8233
|
| 231 |
+
trimesh/viewer/windowed.py,sha256=N2R8o5S05_Ni4CEtvTLdxlQa2fHHcww2_grm3ReMTas,33883
|
| 232 |
+
trimesh/visual/__init__.py,sha256=dGTdTpHWt66Rt7IvzqQivdlWNgH-ofHUD9iTfRm78_E,828
|
| 233 |
+
trimesh/visual/__pycache__/__init__.cpython-311.pyc,,
|
| 234 |
+
trimesh/visual/__pycache__/base.cpython-311.pyc,,
|
| 235 |
+
trimesh/visual/__pycache__/color.cpython-311.pyc,,
|
| 236 |
+
trimesh/visual/__pycache__/gloss.cpython-311.pyc,,
|
| 237 |
+
trimesh/visual/__pycache__/material.cpython-311.pyc,,
|
| 238 |
+
trimesh/visual/__pycache__/objects.cpython-311.pyc,,
|
| 239 |
+
trimesh/visual/__pycache__/texture.cpython-311.pyc,,
|
| 240 |
+
trimesh/visual/base.py,sha256=rLBIbRocEqx_NNgh-YK1Hgd5tY1V5PIEOTLKrd4Bajk,1014
|
| 241 |
+
trimesh/visual/color.py,sha256=Lrbu0Qpe9iZtl_qJL_mvMKAcQ5wif9XnSuoEFVjHWC4,36652
|
| 242 |
+
trimesh/visual/gloss.py,sha256=_jlKRmscbrmuJ_EKdIvMrMXh2GxI25PfWU5ASR0F6Pg,14576
|
| 243 |
+
trimesh/visual/material.py,sha256=6P9cED6GXE6PkD6yG9SIJoBzfXYwtdHOyOjrqUnIilE,36062
|
| 244 |
+
trimesh/visual/objects.py,sha256=zqoB_BEBoMM06_8IlcEZmJCudN7-I6oIYelO2fXZNzs,2554
|
| 245 |
+
trimesh/visual/texture.py,sha256=SiNtJFo_nW3YohFwDqTo14tkT6vx_BK5u5ZyF3BwnFU,10677
|
| 246 |
+
trimesh/voxel/__init__.py,sha256=cRHtVx79ffVWI7GDV9YURMYzuRYqgcGQ4MKBXTFiLjk,88
|
| 247 |
+
trimesh/voxel/__pycache__/__init__.cpython-311.pyc,,
|
| 248 |
+
trimesh/voxel/__pycache__/base.cpython-311.pyc,,
|
| 249 |
+
trimesh/voxel/__pycache__/creation.cpython-311.pyc,,
|
| 250 |
+
trimesh/voxel/__pycache__/encoding.cpython-311.pyc,,
|
| 251 |
+
trimesh/voxel/__pycache__/morphology.cpython-311.pyc,,
|
| 252 |
+
trimesh/voxel/__pycache__/ops.cpython-311.pyc,,
|
| 253 |
+
trimesh/voxel/__pycache__/runlength.cpython-311.pyc,,
|
| 254 |
+
trimesh/voxel/__pycache__/transforms.cpython-311.pyc,,
|
| 255 |
+
trimesh/voxel/base.py,sha256=XXvxPQ9bFEsCfyQobpiXfg1omJoMdprGfPRSo6VOW0s,12663
|
| 256 |
+
trimesh/voxel/creation.py,sha256=lkFLzbeHzZnrdzu_j1Tu7MnlP__V3o9gyTyHz_ASE8U,9653
|
| 257 |
+
trimesh/voxel/encoding.py,sha256=6pvX8fAHmWtKC6HyHdEOoxLkJqIXmL3qjDnHb2Dm4Is,27740
|
| 258 |
+
trimesh/voxel/morphology.py,sha256=Pv1uqH3NVfGi1oUtt8alywmiAsqVOBGJDlvYpS5qj20,5455
|
| 259 |
+
trimesh/voxel/ops.py,sha256=nrTvlwntHvV1LHHGjckLDxFIVREw-HOojaeJfT9Lo3c,13619
|
| 260 |
+
trimesh/voxel/runlength.py,sha256=hVNppnBruSCQsnTJ7Au9WtHaW0YUKsWg7OiZf_OOwZs,20386
|
| 261 |
+
trimesh/voxel/transforms.py,sha256=EoyMBMIF1F6f5hGf0sgcTKKEh4dR8YbdzntUrHl5ejw,5259
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/REQUESTED
ADDED
|
File without changes
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: setuptools (84.0.0)
|
| 3 |
+
Root-Is-Purelib: true
|
| 4 |
+
Tag: py3-none-any
|
| 5 |
+
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/entry_points.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[console_scripts]
|
| 2 |
+
trimesh = trimesh:__main__.main
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/licenses/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The MIT License (MIT)
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2023 Michael Dawson-Haggerty
|
| 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.
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh-5.1.1.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
trimesh
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/geometry.py
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from . import util
|
| 4 |
+
from .constants import log
|
| 5 |
+
from .typed import NDArray
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
import scipy.sparse
|
| 9 |
+
except BaseException as E:
|
| 10 |
+
from . import exceptions
|
| 11 |
+
|
| 12 |
+
# raise E again if anyone tries to use sparse
|
| 13 |
+
scipy = exceptions.ExceptionWrapper(E)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def plane_transform(origin, normal):
|
| 17 |
+
"""
|
| 18 |
+
Given the origin and normal of a plane find the transform
|
| 19 |
+
that will move that plane to be coplanar with the XY plane.
|
| 20 |
+
|
| 21 |
+
Parameters
|
| 22 |
+
----------
|
| 23 |
+
origin : (3,) float
|
| 24 |
+
Point that lies on the plane
|
| 25 |
+
normal : (3,) float
|
| 26 |
+
Vector that points along normal of plane
|
| 27 |
+
|
| 28 |
+
Returns
|
| 29 |
+
---------
|
| 30 |
+
transform: (4,4) float
|
| 31 |
+
Transformation matrix to move points onto XY plane
|
| 32 |
+
"""
|
| 33 |
+
transform = align_vectors(normal, [0, 0, 1])
|
| 34 |
+
if origin is not None:
|
| 35 |
+
transform[:3, 3] = -np.dot(transform, np.append(origin, 1))[:3]
|
| 36 |
+
return transform
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def align_vectors(a, b, return_angle=False):
|
| 40 |
+
"""
|
| 41 |
+
Find the rotation matrix that transforms one 3D vector
|
| 42 |
+
to another.
|
| 43 |
+
|
| 44 |
+
Parameters
|
| 45 |
+
------------
|
| 46 |
+
a : (3,) float
|
| 47 |
+
Unit vector
|
| 48 |
+
b : (3,) float
|
| 49 |
+
Unit vector
|
| 50 |
+
return_angle : bool
|
| 51 |
+
Return the angle between vectors or not
|
| 52 |
+
|
| 53 |
+
Returns
|
| 54 |
+
-------------
|
| 55 |
+
matrix : (4, 4) float
|
| 56 |
+
Homogeneous transform to rotate from `a` to `b`
|
| 57 |
+
angle : float
|
| 58 |
+
If `return_angle` angle in radians between `a` and `b`
|
| 59 |
+
|
| 60 |
+
"""
|
| 61 |
+
a = np.array(a, dtype=np.float64)
|
| 62 |
+
b = np.array(b, dtype=np.float64)
|
| 63 |
+
if a.shape != (3,) or b.shape != (3,):
|
| 64 |
+
raise ValueError("vectors must be (3,)!")
|
| 65 |
+
|
| 66 |
+
# find the SVD of the two vectors
|
| 67 |
+
au = np.linalg.svd(a.reshape((-1, 1)))[0]
|
| 68 |
+
bu = np.linalg.svd(b.reshape((-1, 1)))[0]
|
| 69 |
+
|
| 70 |
+
if np.linalg.det(au) < 0:
|
| 71 |
+
au[:, -1] *= -1.0
|
| 72 |
+
if np.linalg.det(bu) < 0:
|
| 73 |
+
bu[:, -1] *= -1.0
|
| 74 |
+
|
| 75 |
+
# put rotation into homogeneous transformation
|
| 76 |
+
matrix = np.eye(4)
|
| 77 |
+
matrix[:3, :3] = bu.dot(au.T)
|
| 78 |
+
|
| 79 |
+
if return_angle:
|
| 80 |
+
# projection of a onto b
|
| 81 |
+
# first column of SVD result is normalized source vector
|
| 82 |
+
dot = np.dot(au[:, 0], bu[:, 0])
|
| 83 |
+
# clip to avoid floating point error
|
| 84 |
+
angle = np.arccos(np.clip(dot, -1.0, 1.0))
|
| 85 |
+
return matrix, angle
|
| 86 |
+
|
| 87 |
+
return matrix
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def faces_to_edges(faces, return_index=False):
|
| 91 |
+
"""
|
| 92 |
+
Given a list of faces (n,3), return a list of edges (n*3,2)
|
| 93 |
+
|
| 94 |
+
Parameters
|
| 95 |
+
-----------
|
| 96 |
+
faces : (n, 3) int
|
| 97 |
+
Vertex indices representing faces
|
| 98 |
+
|
| 99 |
+
Returns
|
| 100 |
+
-----------
|
| 101 |
+
edges : (n*3, 2) int
|
| 102 |
+
Vertex indices representing edges
|
| 103 |
+
"""
|
| 104 |
+
faces = np.asanyarray(faces, np.int64)
|
| 105 |
+
|
| 106 |
+
# each face has three edges
|
| 107 |
+
edges = faces[:, [0, 1, 1, 2, 2, 0]].reshape((-1, 2))
|
| 108 |
+
|
| 109 |
+
if return_index:
|
| 110 |
+
# edges are in order of faces due to reshape
|
| 111 |
+
face_index = np.tile(np.arange(len(faces)), (3, 1)).T.reshape(-1)
|
| 112 |
+
return edges, face_index
|
| 113 |
+
return edges
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def vector_angle(pairs):
|
| 117 |
+
"""
|
| 118 |
+
Find the angles between pairs of unit vectors.
|
| 119 |
+
|
| 120 |
+
Parameters
|
| 121 |
+
----------
|
| 122 |
+
pairs : (n, 2, 3) float
|
| 123 |
+
Unit vector pairs
|
| 124 |
+
|
| 125 |
+
Returns
|
| 126 |
+
----------
|
| 127 |
+
angles : (n,) float
|
| 128 |
+
Angles between vectors in radians
|
| 129 |
+
"""
|
| 130 |
+
pairs = np.asanyarray(pairs, dtype=np.float64)
|
| 131 |
+
if len(pairs) == 0:
|
| 132 |
+
return np.array([])
|
| 133 |
+
elif util.is_shape(pairs, (2, 3)):
|
| 134 |
+
pairs = pairs.reshape((-1, 2, 3))
|
| 135 |
+
elif not util.is_shape(pairs, (-1, 2, (2, 3))):
|
| 136 |
+
raise ValueError("pairs must be (n,2,(2|3))!")
|
| 137 |
+
|
| 138 |
+
# do the dot product between vectors
|
| 139 |
+
dots = util.diagonal_dot(pairs[:, 0], pairs[:, 1])
|
| 140 |
+
# clip for floating point error
|
| 141 |
+
dots = np.clip(dots, -1.0, 1.0)
|
| 142 |
+
# do cos and remove arbitrary sign
|
| 143 |
+
angles = np.abs(np.arccos(dots))
|
| 144 |
+
|
| 145 |
+
return angles
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def triangulate_quads(quads, dtype=np.int64, use_fan: bool = True) -> NDArray:
|
| 149 |
+
"""
|
| 150 |
+
Given an array of quad faces return them as triangle faces,
|
| 151 |
+
also handles pure triangles and mixed triangles and quads.
|
| 152 |
+
|
| 153 |
+
Parameters
|
| 154 |
+
-----------
|
| 155 |
+
quads: (n, 4) int
|
| 156 |
+
Vertex indices of quad faces.
|
| 157 |
+
dtype
|
| 158 |
+
Data type requested for the return
|
| 159 |
+
use_fan
|
| 160 |
+
Triangulate holes larger than quads with fans,
|
| 161 |
+
which may be wrong if the holes are non-convex
|
| 162 |
+
|
| 163 |
+
Returns
|
| 164 |
+
-----------
|
| 165 |
+
faces : (m, 3) int
|
| 166 |
+
Vertex indices of triangular faces.
|
| 167 |
+
"""
|
| 168 |
+
|
| 169 |
+
if len(quads) == 0:
|
| 170 |
+
return np.zeros(0, dtype=dtype)
|
| 171 |
+
|
| 172 |
+
try:
|
| 173 |
+
# this will fail in newer versions of numpy
|
| 174 |
+
# if there are mixed quads and tris
|
| 175 |
+
quads = np.array(quads, dtype=dtype)
|
| 176 |
+
|
| 177 |
+
if len(quads.shape) == 2 and quads.shape[1] == 3:
|
| 178 |
+
# if they are just triangles return immediately
|
| 179 |
+
return quads.astype(dtype)
|
| 180 |
+
|
| 181 |
+
if len(quads.shape) == 2 and quads.shape[1] == 4:
|
| 182 |
+
# if they are just quads stack and return
|
| 183 |
+
return np.vstack((quads[:, [0, 1, 2]], quads[:, [2, 3, 0]])).astype(dtype)
|
| 184 |
+
except ValueError:
|
| 185 |
+
# new numpy raises an error for sequences
|
| 186 |
+
pass
|
| 187 |
+
|
| 188 |
+
# we made it here so we have mixed tris/quads/polygons
|
| 189 |
+
# do one pass to get the lengths
|
| 190 |
+
lengths = np.array([len(i) for i in quads], dtype=np.int64)
|
| 191 |
+
|
| 192 |
+
# get triangles and quads as clean constant-row numpy arrays
|
| 193 |
+
tri = np.array([quads[i] for i in np.nonzero(lengths == 3)[0]], dtype=np.int64)
|
| 194 |
+
quad = np.array([quads[i] for i in np.nonzero(lengths == 4)[0]], dtype=np.int64)
|
| 195 |
+
|
| 196 |
+
if use_fan:
|
| 197 |
+
# get arbitrary polygons as a ragged sequence
|
| 198 |
+
poly = [quads[i] for i in np.nonzero(lengths > 4)[0]]
|
| 199 |
+
else:
|
| 200 |
+
# skip any hole larger than a quad
|
| 201 |
+
poly = []
|
| 202 |
+
|
| 203 |
+
if len(quad) == 0 and len(poly) == 0:
|
| 204 |
+
# only triangles, return triangles
|
| 205 |
+
return tri.astype(dtype)
|
| 206 |
+
if len(poly) > 0:
|
| 207 |
+
# use numpy slicing to triangulate ragged-sequence polygons
|
| 208 |
+
poly = util.triangle_fans_to_faces(poly)
|
| 209 |
+
if len(quad) > 0:
|
| 210 |
+
# trivially tessellate quads
|
| 211 |
+
quad = np.vstack((quad[:, [0, 1, 2]], quad[:, [2, 3, 0]]))
|
| 212 |
+
# stack triangles from all three cases
|
| 213 |
+
return util.vstack_empty([tri, quad, poly]).astype(dtype)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def vertex_face_indices(vertex_count, faces, faces_sparse):
|
| 217 |
+
"""
|
| 218 |
+
Find vertex face indices from the faces array of vertices
|
| 219 |
+
|
| 220 |
+
Parameters
|
| 221 |
+
-----------
|
| 222 |
+
vertex_count : int
|
| 223 |
+
The number of vertices faces refer to
|
| 224 |
+
faces : (n, 3) int
|
| 225 |
+
List of vertex indices
|
| 226 |
+
faces_sparse : scipy.sparse.COO
|
| 227 |
+
Sparse matrix
|
| 228 |
+
|
| 229 |
+
Returns
|
| 230 |
+
-----------
|
| 231 |
+
vertex_faces : (vertex_count, ) int
|
| 232 |
+
Face indices for every vertex
|
| 233 |
+
Array padded with -1 in each row for all vertices with fewer
|
| 234 |
+
face indices than the max number of face indices.
|
| 235 |
+
"""
|
| 236 |
+
# Create 2D array with row for each vertex and
|
| 237 |
+
# length of max number of faces for a vertex
|
| 238 |
+
try:
|
| 239 |
+
counts = np.bincount(faces.flatten(), minlength=vertex_count)
|
| 240 |
+
except TypeError:
|
| 241 |
+
# casting failed on 32 bit Windows
|
| 242 |
+
log.warning("casting failed, falling back!")
|
| 243 |
+
# fall back to np.unique (usually ~35x slower than bincount)
|
| 244 |
+
counts = np.unique(faces.flatten(), return_counts=True)[1]
|
| 245 |
+
assert len(counts) == vertex_count
|
| 246 |
+
assert faces.max() < vertex_count
|
| 247 |
+
|
| 248 |
+
# start cumulative sum at zero and clip off the last value
|
| 249 |
+
starts = np.append(0, np.cumsum(counts)[:-1])
|
| 250 |
+
# pack incrementing array into final shape
|
| 251 |
+
pack = np.arange(counts.max()) + starts[:, None]
|
| 252 |
+
# pad each row with -1 to pad to the max length
|
| 253 |
+
padded = -(pack >= (starts + counts)[:, None]).astype(np.int64)
|
| 254 |
+
|
| 255 |
+
try:
|
| 256 |
+
# do most of the work with a sparse dot product
|
| 257 |
+
identity = scipy.sparse.identity(len(faces), dtype=int)
|
| 258 |
+
sorted_faces = faces_sparse.dot(identity).nonzero()[1]
|
| 259 |
+
# this will fail if any face was degenerate
|
| 260 |
+
# TODO
|
| 261 |
+
# figure out how to filter out degenerate faces from sparse
|
| 262 |
+
# result if sorted_faces.size != faces.size
|
| 263 |
+
padded[padded == 0] = sorted_faces
|
| 264 |
+
except BaseException:
|
| 265 |
+
# fall back to a slow loop
|
| 266 |
+
log.warning(
|
| 267 |
+
"vertex_faces falling back to slow loop! "
|
| 268 |
+
+ "mesh probably has degenerate faces",
|
| 269 |
+
exc_info=True,
|
| 270 |
+
)
|
| 271 |
+
sort = np.zeros(faces.size, dtype=np.int64)
|
| 272 |
+
flat = faces.flatten()
|
| 273 |
+
for v in range(vertex_count):
|
| 274 |
+
# assign the data in order
|
| 275 |
+
sort[starts[v] : starts[v] + counts[v]] = (np.where(flat == v)[0] // 3)[::-1]
|
| 276 |
+
padded[padded == 0] = sort
|
| 277 |
+
return padded
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
def mean_vertex_normals(vertex_count, faces, face_normals, sparse=None, **kwargs):
|
| 281 |
+
"""
|
| 282 |
+
Find vertex normals from the mean of the faces that contain
|
| 283 |
+
that vertex.
|
| 284 |
+
|
| 285 |
+
Parameters
|
| 286 |
+
-----------
|
| 287 |
+
vertex_count : int
|
| 288 |
+
The number of vertices faces refer to
|
| 289 |
+
faces : (n, 3) int
|
| 290 |
+
List of vertex indices
|
| 291 |
+
face_normals : (n, 3) float
|
| 292 |
+
Normal vector for each face
|
| 293 |
+
|
| 294 |
+
Returns
|
| 295 |
+
-----------
|
| 296 |
+
vertex_normals : (vertex_count, 3) float
|
| 297 |
+
Normals for every vertex
|
| 298 |
+
Vertices unreferenced by faces will be zero.
|
| 299 |
+
"""
|
| 300 |
+
|
| 301 |
+
def summed_sparse():
|
| 302 |
+
# use a sparse matrix of which face contains each vertex to
|
| 303 |
+
# figure out the summed normal at each vertex
|
| 304 |
+
# allow cached sparse matrix to be passed
|
| 305 |
+
if sparse is None:
|
| 306 |
+
matrix = index_sparse(vertex_count, faces)
|
| 307 |
+
else:
|
| 308 |
+
matrix = sparse
|
| 309 |
+
summed = matrix.dot(face_normals)
|
| 310 |
+
return summed
|
| 311 |
+
|
| 312 |
+
def summed_loop():
|
| 313 |
+
# loop through every face, in tests was ~50x slower than
|
| 314 |
+
# doing this with a sparse matrix
|
| 315 |
+
summed = np.zeros((vertex_count, 3))
|
| 316 |
+
for face, normal in zip(faces, face_normals):
|
| 317 |
+
summed[face] += normal
|
| 318 |
+
return summed
|
| 319 |
+
|
| 320 |
+
try:
|
| 321 |
+
summed = summed_sparse()
|
| 322 |
+
except BaseException:
|
| 323 |
+
log.warning("unable to use sparse matrix, falling back!", exc_info=True)
|
| 324 |
+
summed = summed_loop()
|
| 325 |
+
|
| 326 |
+
# invalid normals will be returned as zero
|
| 327 |
+
vertex_normals = util.unitize(summed)
|
| 328 |
+
|
| 329 |
+
return vertex_normals
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
def weighted_vertex_normals(
|
| 333 |
+
vertex_count, faces, face_normals, face_angles, use_loop=False
|
| 334 |
+
):
|
| 335 |
+
"""
|
| 336 |
+
Compute vertex normals from the faces that contain that vertex.
|
| 337 |
+
The contribution of a face's normal to a vertex normal is the
|
| 338 |
+
ratio of the corner-angle in which the vertex is, with respect
|
| 339 |
+
to the sum of all corner-angles surrounding the vertex.
|
| 340 |
+
|
| 341 |
+
Grit Thuerrner & Charles A. Wuethrich (1998)
|
| 342 |
+
Computing Vertex Normals from Polygonal Facets,
|
| 343 |
+
Journal of Graphics Tools, 3:1, 43-46
|
| 344 |
+
|
| 345 |
+
Parameters
|
| 346 |
+
-----------
|
| 347 |
+
vertex_count : int
|
| 348 |
+
The number of vertices faces refer to
|
| 349 |
+
faces : (n, 3) int
|
| 350 |
+
List of vertex indices
|
| 351 |
+
face_normals : (n, 3) float
|
| 352 |
+
Normal vector for each face
|
| 353 |
+
face_angles : (n, 3) float
|
| 354 |
+
Angles at each vertex in the face
|
| 355 |
+
|
| 356 |
+
Returns
|
| 357 |
+
-----------
|
| 358 |
+
vertex_normals : (vertex_count, 3) float
|
| 359 |
+
Normals for every vertex
|
| 360 |
+
Vertices unreferenced by faces will be zero.
|
| 361 |
+
"""
|
| 362 |
+
|
| 363 |
+
def summed_sparse():
|
| 364 |
+
# use a sparse matrix of which face contains each vertex to
|
| 365 |
+
# figure out the summed normal at each vertex
|
| 366 |
+
# allow cached sparse matrix to be passed
|
| 367 |
+
# fill the matrix with vertex-corner angles as weights
|
| 368 |
+
matrix = index_sparse(vertex_count, faces, data=face_angles.ravel())
|
| 369 |
+
return matrix.dot(face_normals)
|
| 370 |
+
|
| 371 |
+
def summed_loop():
|
| 372 |
+
summed = np.zeros((vertex_count, 3), np.float64)
|
| 373 |
+
for vertex_idx in np.arange(vertex_count):
|
| 374 |
+
# loop over all vertices
|
| 375 |
+
# compute normal contributions from surrounding faces
|
| 376 |
+
# obviously slower than with the sparse matrix
|
| 377 |
+
face_idxs, inface_idxs = np.where(faces == vertex_idx)
|
| 378 |
+
surrounding_angles = face_angles[face_idxs, inface_idxs]
|
| 379 |
+
summed[vertex_idx] = np.dot(
|
| 380 |
+
surrounding_angles / surrounding_angles.sum(), face_normals[face_idxs]
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
return summed
|
| 384 |
+
|
| 385 |
+
# normals should be unit vectors
|
| 386 |
+
face_ok = (face_normals**2).sum(axis=1) > 0.5
|
| 387 |
+
# don't consider faces with invalid normals
|
| 388 |
+
faces = faces[face_ok]
|
| 389 |
+
face_normals = face_normals[face_ok]
|
| 390 |
+
face_angles = face_angles[face_ok]
|
| 391 |
+
|
| 392 |
+
if not use_loop:
|
| 393 |
+
try:
|
| 394 |
+
return util.unitize(summed_sparse())
|
| 395 |
+
except BaseException:
|
| 396 |
+
log.warning("unable to use sparse matrix, falling back!", exc_info=True)
|
| 397 |
+
# we either crashed or were asked to loop
|
| 398 |
+
return util.unitize(summed_loop())
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
def index_sparse(columns, indices, data=None, dtype=None):
|
| 402 |
+
"""
|
| 403 |
+
Return a sparse matrix for which vertices are contained in which faces.
|
| 404 |
+
A data vector can be passed which is then used instead of booleans
|
| 405 |
+
|
| 406 |
+
Parameters
|
| 407 |
+
------------
|
| 408 |
+
columns : int
|
| 409 |
+
Number of columns, usually number of vertices
|
| 410 |
+
indices : (m, d) int
|
| 411 |
+
Usually mesh.faces
|
| 412 |
+
|
| 413 |
+
Returns
|
| 414 |
+
---------
|
| 415 |
+
sparse: scipy.sparse.coo_matrix of shape (columns, len(faces))
|
| 416 |
+
dtype is boolean
|
| 417 |
+
|
| 418 |
+
Examples
|
| 419 |
+
----------
|
| 420 |
+
In [1]: sparse = faces_sparse(len(mesh.vertices), mesh.faces)
|
| 421 |
+
|
| 422 |
+
In [2]: sparse.shape
|
| 423 |
+
Out[2]: (12, 20)
|
| 424 |
+
|
| 425 |
+
In [3]: mesh.faces.shape
|
| 426 |
+
Out[3]: (20, 3)
|
| 427 |
+
|
| 428 |
+
In [4]: mesh.vertices.shape
|
| 429 |
+
Out[4]: (12, 3)
|
| 430 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3, 3, 3, 3, 3, 3, 3, 3, 3])
|
| 450 |
+
"""
|
| 451 |
+
indices = np.asanyarray(indices)
|
| 452 |
+
columns = int(columns)
|
| 453 |
+
|
| 454 |
+
# flattened list
|
| 455 |
+
row = indices.reshape(-1)
|
| 456 |
+
col = np.tile(
|
| 457 |
+
np.arange(len(indices)).reshape((-1, 1)), (1, indices.shape[1])
|
| 458 |
+
).reshape(-1)
|
| 459 |
+
|
| 460 |
+
shape = (columns, len(indices))
|
| 461 |
+
if data is None:
|
| 462 |
+
data = np.ones(len(col), dtype=bool)
|
| 463 |
+
elif len(data) != len(col):
|
| 464 |
+
raise ValueError("data incorrect length")
|
| 465 |
+
|
| 466 |
+
if dtype is not None:
|
| 467 |
+
data = data.astype(dtype)
|
| 468 |
+
|
| 469 |
+
# assemble into sparse matrix
|
| 470 |
+
return scipy.sparse.coo_matrix((data, (row, col)), shape=shape, dtype=data.dtype)
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/graph.py
ADDED
|
@@ -0,0 +1,1017 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
graph.py
|
| 3 |
+
-------------
|
| 4 |
+
|
| 5 |
+
Deal with graph operations. Primarily deal with graphs in (n, 2)
|
| 6 |
+
edge list form, and abstract the backend graph library being used.
|
| 7 |
+
|
| 8 |
+
Currently uses networkx or scipy.sparse.csgraph backend.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import collections
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from . import exceptions, grouping, util
|
| 16 |
+
from .constants import log, tol
|
| 17 |
+
from .geometry import faces_to_edges
|
| 18 |
+
from .typed import (
|
| 19 |
+
ArrayLike,
|
| 20 |
+
GraphEngineType,
|
| 21 |
+
Integer,
|
| 22 |
+
NDArray,
|
| 23 |
+
Number,
|
| 24 |
+
Sequence,
|
| 25 |
+
int64,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
from scipy.sparse import coo_matrix, csgraph
|
| 30 |
+
from scipy.spatial import cKDTree
|
| 31 |
+
except BaseException as E:
|
| 32 |
+
# re-raise exception when used
|
| 33 |
+
cKDTree = exceptions.ExceptionWrapper(E)
|
| 34 |
+
csgraph = exceptions.ExceptionWrapper(E)
|
| 35 |
+
coo_matrix = exceptions.ExceptionWrapper(E)
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
import networkx as nx
|
| 39 |
+
except BaseException as E:
|
| 40 |
+
# create a dummy module which will raise the ImportError
|
| 41 |
+
# or other exception only when someone tries to use networkx
|
| 42 |
+
nx = exceptions.ExceptionWrapper(E)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def face_adjacency(faces=None, mesh=None, return_edges=False):
|
| 46 |
+
"""
|
| 47 |
+
Returns an (n, 2) list of face indices.
|
| 48 |
+
Each pair of faces in the list shares an edge, making them adjacent.
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
Parameters
|
| 52 |
+
-----------
|
| 53 |
+
faces : (n, 3) int, or None
|
| 54 |
+
Vertex indices representing triangles
|
| 55 |
+
mesh : Trimesh object
|
| 56 |
+
If passed will used cached edges
|
| 57 |
+
instead of generating from faces
|
| 58 |
+
return_edges : bool
|
| 59 |
+
Return the edges shared by adjacent faces
|
| 60 |
+
|
| 61 |
+
Returns
|
| 62 |
+
----------
|
| 63 |
+
adjacency : (m, 2) int
|
| 64 |
+
Indexes of faces that are adjacent
|
| 65 |
+
edges: (m, 2) int
|
| 66 |
+
Only returned if return_edges is True
|
| 67 |
+
Indexes of vertices which make up the
|
| 68 |
+
edges shared by the adjacent faces
|
| 69 |
+
|
| 70 |
+
Examples
|
| 71 |
+
----------
|
| 72 |
+
This is useful for lots of things such as finding
|
| 73 |
+
face- connected components:
|
| 74 |
+
```python
|
| 75 |
+
>>> graph = nx.Graph()
|
| 76 |
+
>>> graph.add_edges_from(mesh.face_adjacency)
|
| 77 |
+
>>> groups = nx.connected_components(graph_connected)
|
| 78 |
+
```
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
if mesh is None:
|
| 82 |
+
# first generate the list of edges for the current faces
|
| 83 |
+
# also return the index for which face the edge is from
|
| 84 |
+
edges, edges_face = faces_to_edges(faces, return_index=True)
|
| 85 |
+
# make sure edge rows are sorted
|
| 86 |
+
edges.sort(axis=1)
|
| 87 |
+
else:
|
| 88 |
+
# if passed a mesh, used the cached values
|
| 89 |
+
edges = mesh.edges_sorted
|
| 90 |
+
edges_face = mesh.edges_face
|
| 91 |
+
|
| 92 |
+
# this will return the indices for duplicate edges
|
| 93 |
+
# every edge appears twice in a well constructed mesh
|
| 94 |
+
# so for every row in edge_idx:
|
| 95 |
+
# edges[edge_idx[*][0]] == edges[edge_idx[*][1]]
|
| 96 |
+
# in this call to group rows we discard edges which
|
| 97 |
+
# don't occur twice
|
| 98 |
+
edge_groups = grouping.group_rows(edges, require_count=2)
|
| 99 |
+
|
| 100 |
+
if len(edge_groups) == 0:
|
| 101 |
+
log.debug("No adjacent faces detected! Did you merge vertices?")
|
| 102 |
+
|
| 103 |
+
# the pairs of all adjacent faces
|
| 104 |
+
# so for every row in face_idx, self.faces[face_idx[*][0]] and
|
| 105 |
+
# self.faces[face_idx[*][1]] will share an edge
|
| 106 |
+
adjacency = edges_face[edge_groups]
|
| 107 |
+
|
| 108 |
+
# degenerate faces may appear in adjacency as the same value
|
| 109 |
+
nondegenerate = adjacency[:, 0] != adjacency[:, 1]
|
| 110 |
+
adjacency = adjacency[nondegenerate]
|
| 111 |
+
|
| 112 |
+
# sort pairs in-place so we can search for indexes with ordered pairs
|
| 113 |
+
adjacency.sort(axis=1)
|
| 114 |
+
|
| 115 |
+
if return_edges:
|
| 116 |
+
adjacency_edges = edges[edge_groups[:, 0][nondegenerate]]
|
| 117 |
+
assert len(adjacency_edges) == len(adjacency)
|
| 118 |
+
return adjacency, adjacency_edges
|
| 119 |
+
|
| 120 |
+
return adjacency
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def face_neighborhood(mesh) -> NDArray[int64]:
|
| 124 |
+
"""
|
| 125 |
+
Find faces that share a vertex i.e. 'neighbors' faces.
|
| 126 |
+
Relies on the fact that an adjacency matrix at a power p
|
| 127 |
+
contains the number of paths of length p connecting two nodes.
|
| 128 |
+
Here we take the bipartite graph from mesh.faces_sparse to the power 2.
|
| 129 |
+
The non-zeros are the faces connected by one vertex.
|
| 130 |
+
|
| 131 |
+
Returns
|
| 132 |
+
----------
|
| 133 |
+
neighborhood : (n, 2) int
|
| 134 |
+
Pairs of faces which share a vertex
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
VT = mesh.faces_sparse
|
| 138 |
+
TT = VT.T * VT
|
| 139 |
+
TT.setdiag(0)
|
| 140 |
+
TT.eliminate_zeros()
|
| 141 |
+
TT = TT.tocoo()
|
| 142 |
+
neighborhood = np.concatenate(
|
| 143 |
+
(TT.row[:, None], TT.col[:, None]), axis=-1, dtype=np.int64
|
| 144 |
+
)
|
| 145 |
+
return neighborhood
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def face_adjacency_unshared(mesh):
|
| 149 |
+
"""
|
| 150 |
+
Return the vertex index of the two vertices not in the shared
|
| 151 |
+
edge between two adjacent faces
|
| 152 |
+
|
| 153 |
+
Parameters
|
| 154 |
+
----------
|
| 155 |
+
mesh : Trimesh object
|
| 156 |
+
Input mesh
|
| 157 |
+
|
| 158 |
+
Returns
|
| 159 |
+
-----------
|
| 160 |
+
vid_unshared : (len(mesh.face_adjacency), 2) int
|
| 161 |
+
Indexes of mesh.vertices
|
| 162 |
+
for degenerate faces without exactly
|
| 163 |
+
one unshared vertex per face it will be -1
|
| 164 |
+
"""
|
| 165 |
+
|
| 166 |
+
# the non- shared vertex index is the same shape
|
| 167 |
+
# as face_adjacency holding vertex indices vs face indices
|
| 168 |
+
vid_unshared = np.zeros_like(mesh.face_adjacency, dtype=np.int64) - 1
|
| 169 |
+
# get the shared edges between adjacent faces
|
| 170 |
+
edges = mesh.face_adjacency_edges
|
| 171 |
+
|
| 172 |
+
# loop through the two columns of face adjacency
|
| 173 |
+
for i, fid in enumerate(mesh.face_adjacency.T):
|
| 174 |
+
# faces from the current column of face adjacency
|
| 175 |
+
faces = mesh.faces[fid]
|
| 176 |
+
# should have one True per row of (3,)
|
| 177 |
+
# index of vertex not included in shared edge
|
| 178 |
+
unshared = np.logical_not(
|
| 179 |
+
np.logical_or(
|
| 180 |
+
faces == edges[:, 0].reshape((-1, 1)),
|
| 181 |
+
faces == edges[:, 1].reshape((-1, 1)),
|
| 182 |
+
)
|
| 183 |
+
)
|
| 184 |
+
# each row should have exactly one uncontained verted
|
| 185 |
+
row_ok = unshared.sum(axis=1) == 1
|
| 186 |
+
# any degenerate row should be ignored
|
| 187 |
+
unshared[~row_ok, :] = False
|
| 188 |
+
# set the
|
| 189 |
+
vid_unshared[row_ok, i] = faces[unshared]
|
| 190 |
+
|
| 191 |
+
return vid_unshared
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def face_adjacency_radius(mesh):
|
| 195 |
+
"""
|
| 196 |
+
Compute an approximate radius between adjacent faces.
|
| 197 |
+
|
| 198 |
+
Parameters
|
| 199 |
+
--------------
|
| 200 |
+
mesh : trimesh.Trimesh
|
| 201 |
+
|
| 202 |
+
Returns
|
| 203 |
+
-------------
|
| 204 |
+
radii : (len(self.face_adjacency),) float
|
| 205 |
+
Approximate radius between faces
|
| 206 |
+
Parallel faces will have a value of np.inf
|
| 207 |
+
span : (len(self.face_adjacency),) float
|
| 208 |
+
Perpendicular projection distance of two
|
| 209 |
+
unshared vertices onto the shared edge
|
| 210 |
+
"""
|
| 211 |
+
|
| 212 |
+
# solve for the radius of the adjacent faces
|
| 213 |
+
# distance
|
| 214 |
+
# R = ---------------
|
| 215 |
+
# 2 * sin(theta)
|
| 216 |
+
nonzero = mesh.face_adjacency_angles > np.radians(0.01)
|
| 217 |
+
denominator = np.abs(2.0 * np.sin(mesh.face_adjacency_angles[nonzero]))
|
| 218 |
+
|
| 219 |
+
# consider the distance between the non- shared vertices of the
|
| 220 |
+
# face adjacency pair as the key distance
|
| 221 |
+
point_pairs = mesh.vertices[mesh.face_adjacency_unshared]
|
| 222 |
+
vectors = np.diff(point_pairs, axis=1).reshape((-1, 3))
|
| 223 |
+
|
| 224 |
+
# the vertex indices of the shared edge for the adjacency pairx
|
| 225 |
+
edges = mesh.face_adjacency_edges
|
| 226 |
+
# unit vector along shared the edge
|
| 227 |
+
edges_vec = util.unitize(np.diff(mesh.vertices[edges], axis=1).reshape((-1, 3)))
|
| 228 |
+
|
| 229 |
+
# the vector of the perpendicular projection to the shared edge
|
| 230 |
+
perp = np.subtract(
|
| 231 |
+
vectors, (util.diagonal_dot(vectors, edges_vec).reshape((-1, 1)) * edges_vec)
|
| 232 |
+
)
|
| 233 |
+
# the length of the perpendicular projection
|
| 234 |
+
span = util.row_norm(perp)
|
| 235 |
+
|
| 236 |
+
# complete the values for non- infinite radii
|
| 237 |
+
radii = np.ones(len(mesh.face_adjacency)) * np.inf
|
| 238 |
+
radii[nonzero] = span[nonzero] / denominator
|
| 239 |
+
|
| 240 |
+
return radii, span
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def vertex_adjacency_graph(mesh):
|
| 244 |
+
"""
|
| 245 |
+
Returns a networkx graph representing the vertices and
|
| 246 |
+
their connections in the mesh.
|
| 247 |
+
|
| 248 |
+
Parameters
|
| 249 |
+
----------
|
| 250 |
+
mesh : Trimesh object
|
| 251 |
+
|
| 252 |
+
Returns
|
| 253 |
+
---------
|
| 254 |
+
graph : networkx.Graph
|
| 255 |
+
Graph representing vertices and edges between
|
| 256 |
+
them where vertices are nodes and edges are edges
|
| 257 |
+
|
| 258 |
+
Examples
|
| 259 |
+
----------
|
| 260 |
+
This is useful for getting nearby vertices for a given vertex,
|
| 261 |
+
potentially for some simple smoothing techniques.
|
| 262 |
+
>>> graph = mesh.vertex_adjacency_graph
|
| 263 |
+
>>> graph.neighbors(0)
|
| 264 |
+
> [1, 3, 4]
|
| 265 |
+
"""
|
| 266 |
+
g = nx.Graph()
|
| 267 |
+
g.add_edges_from(mesh.edges_unique)
|
| 268 |
+
return g
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def shared_edges(faces_a, faces_b):
|
| 272 |
+
"""
|
| 273 |
+
Given two sets of faces, find the edges which are in both sets.
|
| 274 |
+
|
| 275 |
+
Parameters
|
| 276 |
+
---------
|
| 277 |
+
faces_a : (n, 3) int
|
| 278 |
+
Array of faces
|
| 279 |
+
faces_b : (m, 3) int
|
| 280 |
+
Array of faces
|
| 281 |
+
|
| 282 |
+
Returns
|
| 283 |
+
---------
|
| 284 |
+
shared : (p, 2) int
|
| 285 |
+
Edges shared between faces
|
| 286 |
+
"""
|
| 287 |
+
e_a = np.sort(faces_to_edges(faces_a), axis=1)
|
| 288 |
+
e_b = np.sort(faces_to_edges(faces_b), axis=1)
|
| 289 |
+
shared = grouping.boolean_rows(e_a, e_b, operation=np.intersect1d)
|
| 290 |
+
return shared
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def facets(mesh, engine: GraphEngineType = None, facet_threshold: Number | None = None):
|
| 294 |
+
"""
|
| 295 |
+
Find the list of parallel adjacent faces.
|
| 296 |
+
|
| 297 |
+
Parameters
|
| 298 |
+
-----------
|
| 299 |
+
mesh : trimesh.Trimesh
|
| 300 |
+
engine
|
| 301 |
+
Which graph engine to use
|
| 302 |
+
facet_threshold : float
|
| 303 |
+
Threshold for two facets to be considered coplanar
|
| 304 |
+
|
| 305 |
+
Returns
|
| 306 |
+
---------
|
| 307 |
+
facets : sequence of (n,) int
|
| 308 |
+
Groups of face indexes of
|
| 309 |
+
parallel adjacent faces.
|
| 310 |
+
"""
|
| 311 |
+
if facet_threshold is None:
|
| 312 |
+
facet_threshold = tol.facet_threshold
|
| 313 |
+
# what is the radius of a circle that passes through the perpendicular
|
| 314 |
+
# projection of the vector between the two non- shared vertices
|
| 315 |
+
# onto the shared edge, with the face normal from the two adjacent faces
|
| 316 |
+
radii = mesh.face_adjacency_radius
|
| 317 |
+
# what is the span perpendicular to the shared edge
|
| 318 |
+
span = mesh.face_adjacency_span
|
| 319 |
+
# a very arbitrary formula for declaring two adjacent faces
|
| 320 |
+
# parallel in a way that is hopefully (and anecdotally) robust
|
| 321 |
+
# to numeric error
|
| 322 |
+
# a common failure mode is two faces that are very narrow with a slight
|
| 323 |
+
# angle between them, so here we divide by the perpendicular span
|
| 324 |
+
# to penalize very narrow faces, and then square it just for fun
|
| 325 |
+
parallel = np.ones(len(radii), dtype=bool)
|
| 326 |
+
# if span is zero we know faces are small/parallel
|
| 327 |
+
nonzero = np.abs(span) > tol.zero
|
| 328 |
+
# faces with a radii/span ratio larger than a threshold pass
|
| 329 |
+
parallel[nonzero] = (radii[nonzero] / span[nonzero]) ** 2 > facet_threshold
|
| 330 |
+
|
| 331 |
+
# run connected components on the parallel faces to group them
|
| 332 |
+
components = connected_components(
|
| 333 |
+
mesh.face_adjacency[parallel],
|
| 334 |
+
nodes=np.arange(len(mesh.faces)),
|
| 335 |
+
min_len=2,
|
| 336 |
+
engine=engine,
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
return components
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
def split(
|
| 343 |
+
mesh,
|
| 344 |
+
only_watertight: bool = True,
|
| 345 |
+
repair: bool = True,
|
| 346 |
+
adjacency: ArrayLike | None = None,
|
| 347 |
+
engine: GraphEngineType = None,
|
| 348 |
+
**kwargs,
|
| 349 |
+
) -> list:
|
| 350 |
+
"""
|
| 351 |
+
Split a mesh into multiple meshes from face
|
| 352 |
+
connectivity.
|
| 353 |
+
|
| 354 |
+
If only_watertight is true it will only return
|
| 355 |
+
watertight meshes and will attempt to repair
|
| 356 |
+
single triangle or quad holes.
|
| 357 |
+
|
| 358 |
+
Parameters
|
| 359 |
+
----------
|
| 360 |
+
mesh : trimesh.Trimesh
|
| 361 |
+
The source multibody mesh to split
|
| 362 |
+
only_watertight
|
| 363 |
+
Only return watertight components and discard
|
| 364 |
+
any connected component that isn't fully watertight.
|
| 365 |
+
repair
|
| 366 |
+
If set try to fill small holes in a mesh, before the
|
| 367 |
+
discard step in `only_watertight.
|
| 368 |
+
adjacency : (n, 2) int
|
| 369 |
+
If passed will be used instead of `mesh.face_adjacency`
|
| 370 |
+
engine
|
| 371 |
+
Which graph engine to use for the connected components.
|
| 372 |
+
|
| 373 |
+
Returns
|
| 374 |
+
----------
|
| 375 |
+
meshes : (m,) trimesh.Trimesh
|
| 376 |
+
Results of splitting based on parameters.
|
| 377 |
+
"""
|
| 378 |
+
if adjacency is None:
|
| 379 |
+
adjacency = mesh.face_adjacency
|
| 380 |
+
|
| 381 |
+
if only_watertight:
|
| 382 |
+
# the smallest watertight mesh has 4 faces
|
| 383 |
+
min_len = 4
|
| 384 |
+
else:
|
| 385 |
+
# allow any size of connected component
|
| 386 |
+
min_len = 1
|
| 387 |
+
|
| 388 |
+
components = connected_components(
|
| 389 |
+
edges=adjacency, nodes=np.arange(len(mesh.faces)), min_len=min_len, engine=engine
|
| 390 |
+
)
|
| 391 |
+
return mesh.submesh(
|
| 392 |
+
components, only_watertight=only_watertight, repair=repair, **kwargs
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
def connected_components(
|
| 397 |
+
edges, min_len: Integer = 1, nodes=None, engine: GraphEngineType = None
|
| 398 |
+
):
|
| 399 |
+
"""
|
| 400 |
+
Find groups of connected nodes from an edge list.
|
| 401 |
+
|
| 402 |
+
Parameters
|
| 403 |
+
-----------
|
| 404 |
+
edges : (n, 2) int
|
| 405 |
+
Edges between nodes
|
| 406 |
+
nodes : (m, ) int or None
|
| 407 |
+
List of nodes that exist
|
| 408 |
+
min_len : int
|
| 409 |
+
Minimum length of a component group to return
|
| 410 |
+
engine : str or None
|
| 411 |
+
Which graph engine to use (None for automatic):
|
| 412 |
+
(None, 'networkx', 'scipy')
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
Returns
|
| 416 |
+
-----------
|
| 417 |
+
components : (n,) sequence of (*,) int
|
| 418 |
+
Nodes which are connected
|
| 419 |
+
"""
|
| 420 |
+
|
| 421 |
+
def components_networkx():
|
| 422 |
+
"""
|
| 423 |
+
Find connected components using networkx
|
| 424 |
+
"""
|
| 425 |
+
graph = nx.from_edgelist(edges)
|
| 426 |
+
# make sure every face has a node, so single triangles
|
| 427 |
+
# aren't discarded (as they aren't adjacent to anything)
|
| 428 |
+
if min_len <= 1:
|
| 429 |
+
graph.add_nodes_from(nodes)
|
| 430 |
+
return [list(i) for i in nx.connected_components(graph)]
|
| 431 |
+
|
| 432 |
+
def components_csgraph():
|
| 433 |
+
"""
|
| 434 |
+
Find connected components using scipy.sparse.csgraph
|
| 435 |
+
"""
|
| 436 |
+
# label each node
|
| 437 |
+
labels = connected_component_labels(edges, node_count=node_count)
|
| 438 |
+
|
| 439 |
+
# we have to remove results that contain nodes outside
|
| 440 |
+
# of the specified node set and reindex
|
| 441 |
+
contained = np.zeros(node_count, dtype=bool)
|
| 442 |
+
contained[nodes] = True
|
| 443 |
+
index = np.arange(node_count, dtype=np.int64)[contained]
|
| 444 |
+
components = grouping.group(labels[contained], min_len=min_len)
|
| 445 |
+
return [index[c] for c in components]
|
| 446 |
+
|
| 447 |
+
return components
|
| 448 |
+
|
| 449 |
+
# check input edges
|
| 450 |
+
edges = np.asanyarray(edges, dtype=np.int64)
|
| 451 |
+
# if no nodes were specified just use unique
|
| 452 |
+
if nodes is None:
|
| 453 |
+
nodes = np.unique(edges)
|
| 454 |
+
|
| 455 |
+
# exit early if we have no nodes
|
| 456 |
+
if len(nodes) == 0:
|
| 457 |
+
return []
|
| 458 |
+
elif len(edges) == 0:
|
| 459 |
+
if min_len <= 1:
|
| 460 |
+
return np.reshape(nodes, (-1, 1)).tolist()
|
| 461 |
+
else:
|
| 462 |
+
return []
|
| 463 |
+
|
| 464 |
+
if not util.is_shape(edges, (-1, 2)):
|
| 465 |
+
raise ValueError("edges must be (n, 2)!")
|
| 466 |
+
|
| 467 |
+
# find the maximum index referenced in either nodes or edges
|
| 468 |
+
counts = [0]
|
| 469 |
+
if len(edges) > 0:
|
| 470 |
+
counts.append(edges.max())
|
| 471 |
+
if len(nodes) > 0:
|
| 472 |
+
counts.append(nodes.max())
|
| 473 |
+
node_count = np.max(counts) + 1
|
| 474 |
+
|
| 475 |
+
# remove edges that don't have both nodes in the node set
|
| 476 |
+
mask = np.zeros(node_count, dtype=bool)
|
| 477 |
+
mask[nodes] = True
|
| 478 |
+
edges_ok = mask[edges].all(axis=1)
|
| 479 |
+
edges = edges[edges_ok]
|
| 480 |
+
|
| 481 |
+
# networkx is pure python and is usually 5-10x slower than scipy
|
| 482 |
+
engines = collections.OrderedDict(
|
| 483 |
+
(("scipy", components_csgraph), ("networkx", components_networkx))
|
| 484 |
+
)
|
| 485 |
+
|
| 486 |
+
# if a graph engine has explicitly been requested use it
|
| 487 |
+
if engine in engines:
|
| 488 |
+
return engines[engine]()
|
| 489 |
+
|
| 490 |
+
# otherwise, go through our ordered list of graph engines
|
| 491 |
+
# until we get to one that has actually been installed
|
| 492 |
+
for function in engines.values():
|
| 493 |
+
try:
|
| 494 |
+
return function()
|
| 495 |
+
# will be raised if the library didn't import correctly above
|
| 496 |
+
except BaseException:
|
| 497 |
+
continue
|
| 498 |
+
raise ImportError("no graph engines available!")
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
def connected_component_labels(edges, node_count=None):
|
| 502 |
+
"""
|
| 503 |
+
Label graph nodes from an edge list, using scipy.sparse.csgraph
|
| 504 |
+
|
| 505 |
+
Parameters
|
| 506 |
+
-----------
|
| 507 |
+
edges : (n, 2) int
|
| 508 |
+
Edges of a graph
|
| 509 |
+
node_count : int, or None
|
| 510 |
+
The largest node in the graph.
|
| 511 |
+
|
| 512 |
+
Returns
|
| 513 |
+
----------
|
| 514 |
+
labels : (node_count,) int
|
| 515 |
+
Component labels for each node
|
| 516 |
+
"""
|
| 517 |
+
matrix = edges_to_coo(edges, node_count)
|
| 518 |
+
_body_count, labels = csgraph.connected_components(matrix, directed=False)
|
| 519 |
+
|
| 520 |
+
if node_count is not None:
|
| 521 |
+
assert len(labels) == node_count
|
| 522 |
+
|
| 523 |
+
return labels
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
def _split_traversal(traversal: NDArray, edges_tree) -> list[NDArray]:
|
| 527 |
+
"""
|
| 528 |
+
Given a traversal as a list of nodes split the traversal
|
| 529 |
+
if a sequential index pair is not in the given edges.
|
| 530 |
+
Useful since the implementation of DFS we're using will
|
| 531 |
+
happily return disconnected values in a flat traversal.
|
| 532 |
+
|
| 533 |
+
Parameters
|
| 534 |
+
--------------
|
| 535 |
+
traversal : (m,) int
|
| 536 |
+
Traversal through edges
|
| 537 |
+
edges_tree : cKDTree
|
| 538 |
+
A way to reconstruct original edge indices from
|
| 539 |
+
sorted (n, 2) edge values. This is a slight misuse of a
|
| 540 |
+
kdtree since one could just hash the tuples of the integer
|
| 541 |
+
edges, but that isn't possible with numpy arrays easily
|
| 542 |
+
and this allows a vectorized reconstruction.
|
| 543 |
+
|
| 544 |
+
Returns
|
| 545 |
+
---------------
|
| 546 |
+
split : sequence of (p,) int
|
| 547 |
+
Traversals split into only connected paths.
|
| 548 |
+
"""
|
| 549 |
+
# turn the (n,) traversal into (n-1, 2) edges
|
| 550 |
+
# save the original order of the edges for reconstruction
|
| 551 |
+
trav_edge = np.column_stack((traversal[:-1], traversal[1:]))
|
| 552 |
+
|
| 553 |
+
# check to see if the traversal edges exists in the original edges
|
| 554 |
+
# the tree is holding the sorted edges so query after sorting
|
| 555 |
+
exists = edges_tree.query(np.sort(trav_edge, axis=1))[0] < 1e-10
|
| 556 |
+
|
| 557 |
+
if exists.all():
|
| 558 |
+
split = [traversal]
|
| 559 |
+
else:
|
| 560 |
+
# contiguous groups of edges
|
| 561 |
+
blocks = grouping.blocks(exists, min_len=1, only_nonzero=True)
|
| 562 |
+
split = [
|
| 563 |
+
np.concatenate([trav_edge[:, 0][b], trav_edge[b[-1]][1:]]) for b in blocks
|
| 564 |
+
]
|
| 565 |
+
|
| 566 |
+
# a traversal may be effectively closed so check
|
| 567 |
+
# to see if we need to add on the first index to the end
|
| 568 |
+
needs_close = np.array(
|
| 569 |
+
[
|
| 570 |
+
len(s) > 2
|
| 571 |
+
and s[0] != s[-1]
|
| 572 |
+
and edges_tree.query(sorted([s[0], s[-1]]))[0] < 1e-10
|
| 573 |
+
for s in split
|
| 574 |
+
]
|
| 575 |
+
)
|
| 576 |
+
|
| 577 |
+
if needs_close.any():
|
| 578 |
+
for i in np.nonzero(needs_close)[0]:
|
| 579 |
+
split[i] = np.concatenate([split[i], split[i][:1]])
|
| 580 |
+
|
| 581 |
+
if tol.strict:
|
| 582 |
+
for s in split:
|
| 583 |
+
check_edge = np.sort(np.column_stack((s[:-1], s[1:])), axis=1)
|
| 584 |
+
assert (edges_tree.query(check_edge)[0] < 1e-10).all()
|
| 585 |
+
|
| 586 |
+
return split
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
def fill_traversals(traversals: Sequence, edges: ArrayLike) -> Sequence | NDArray:
|
| 590 |
+
"""
|
| 591 |
+
Convert a traversal of a list of edges into a sequence of
|
| 592 |
+
traversals where every pair of consecutive node indexes
|
| 593 |
+
is an edge in a passed edge list
|
| 594 |
+
|
| 595 |
+
Parameters
|
| 596 |
+
-------------
|
| 597 |
+
traversals : sequence of (m,) int
|
| 598 |
+
Node indexes of traversals of a graph
|
| 599 |
+
edges : (n, 2) int
|
| 600 |
+
Pairs of connected node indexes
|
| 601 |
+
|
| 602 |
+
Returns
|
| 603 |
+
--------------
|
| 604 |
+
splits : sequence of (p,) int
|
| 605 |
+
Node indexes of connected traversals
|
| 606 |
+
"""
|
| 607 |
+
# make sure edges are correct type
|
| 608 |
+
edges = np.sort(edges, axis=1)
|
| 609 |
+
edges_tree = cKDTree(edges)
|
| 610 |
+
|
| 611 |
+
# if there are no traversals just return edges
|
| 612 |
+
if len(traversals) == 0:
|
| 613 |
+
return edges.copy()
|
| 614 |
+
|
| 615 |
+
splits = []
|
| 616 |
+
for nodes in traversals:
|
| 617 |
+
# split traversals to remove edges that don't actually exist
|
| 618 |
+
splits.extend(_split_traversal(traversal=nodes, edges_tree=edges_tree))
|
| 619 |
+
|
| 620 |
+
# turn the split traversals back into (n, 2) edges
|
| 621 |
+
included = util.vstack_empty([np.column_stack((i[:-1], i[1:])) for i in splits])
|
| 622 |
+
if len(included) > 0:
|
| 623 |
+
# sort included edges in place
|
| 624 |
+
included.sort(axis=1)
|
| 625 |
+
# make sure any edges not included in split traversals
|
| 626 |
+
# are just added as a length 2 traversal
|
| 627 |
+
splits.extend(grouping.boolean_rows(edges, included, operation=np.setdiff1d))
|
| 628 |
+
else:
|
| 629 |
+
# no edges were included, so our filled traversal
|
| 630 |
+
# is just the original edges copied over
|
| 631 |
+
splits = edges.copy()
|
| 632 |
+
|
| 633 |
+
return splits
|
| 634 |
+
|
| 635 |
+
|
| 636 |
+
def traversals(edges, mode="bfs"):
|
| 637 |
+
"""
|
| 638 |
+
Given an edge list generate a sequence of ordered depth
|
| 639 |
+
first search traversals using scipy.csgraph routines.
|
| 640 |
+
|
| 641 |
+
Parameters
|
| 642 |
+
------------
|
| 643 |
+
edges : (n, 2) int
|
| 644 |
+
Undirected edges of a graph
|
| 645 |
+
mode : str
|
| 646 |
+
Traversal type, 'bfs' or 'dfs'
|
| 647 |
+
|
| 648 |
+
Returns
|
| 649 |
+
-----------
|
| 650 |
+
traversals : (m,) sequence of (p,) int
|
| 651 |
+
Ordered DFS or BFS traversals of the graph.
|
| 652 |
+
"""
|
| 653 |
+
edges = np.array(edges, dtype=np.int64)
|
| 654 |
+
if len(edges) == 0:
|
| 655 |
+
return []
|
| 656 |
+
elif not util.is_shape(edges, (-1, 2)):
|
| 657 |
+
raise ValueError("edges are not (n, 2)!")
|
| 658 |
+
|
| 659 |
+
# pick the traversal method
|
| 660 |
+
mode = str(mode).lower().strip()
|
| 661 |
+
if mode == "bfs":
|
| 662 |
+
func = csgraph.breadth_first_order
|
| 663 |
+
elif mode == "dfs":
|
| 664 |
+
func = csgraph.depth_first_order
|
| 665 |
+
else:
|
| 666 |
+
raise ValueError("traversal mode must be either dfs or bfs")
|
| 667 |
+
|
| 668 |
+
# make sure edges are sorted so we can query
|
| 669 |
+
# an ordered pair later
|
| 670 |
+
edges.sort(axis=1)
|
| 671 |
+
# coo_matrix for csgraph routines
|
| 672 |
+
graph = edges_to_coo(edges)
|
| 673 |
+
|
| 674 |
+
# get unique nodes AND leaf node info from bincount
|
| 675 |
+
bincount = np.bincount(edges.ravel())
|
| 676 |
+
|
| 677 |
+
# a leaf node occurs exactly once
|
| 678 |
+
nodes_leaf = np.nonzero(bincount == 1)[0]
|
| 679 |
+
# a non-leaf node occurs more than once
|
| 680 |
+
nodes = np.nonzero(bincount > 1)[0]
|
| 681 |
+
|
| 682 |
+
# collect traversals
|
| 683 |
+
traversals = []
|
| 684 |
+
# keep track of which nodes have been visited
|
| 685 |
+
visited = np.zeros(len(bincount) + 1, dtype=np.bool_)
|
| 686 |
+
|
| 687 |
+
# process leaf nodes first to avoid DFS backtracking
|
| 688 |
+
# starting DFS from an interior node of an open path causes
|
| 689 |
+
# backtracking, which produces non-edge consecutive pairs that
|
| 690 |
+
# fill_traversals then splits a single path into pieces
|
| 691 |
+
for start in np.concatenate((nodes_leaf, nodes)):
|
| 692 |
+
if visited[start]:
|
| 693 |
+
continue
|
| 694 |
+
|
| 695 |
+
# get an (n,) ordered traversal from this start
|
| 696 |
+
ordered = func(
|
| 697 |
+
graph, i_start=start, return_predecessors=False, directed=False
|
| 698 |
+
).astype(np.int64)
|
| 699 |
+
|
| 700 |
+
# store the traversal and mark all nodes as visited
|
| 701 |
+
traversals.append(ordered)
|
| 702 |
+
visited[ordered] = True
|
| 703 |
+
|
| 704 |
+
return traversals
|
| 705 |
+
|
| 706 |
+
|
| 707 |
+
def edges_to_coo(edges, count=None, data=None):
|
| 708 |
+
"""
|
| 709 |
+
Given an edge list, return a boolean scipy.sparse.coo_matrix
|
| 710 |
+
representing the edges in matrix form.
|
| 711 |
+
|
| 712 |
+
Parameters
|
| 713 |
+
------------
|
| 714 |
+
edges : (n, 2) int
|
| 715 |
+
Edges of a graph
|
| 716 |
+
count : int
|
| 717 |
+
The total number of nodes in the graph
|
| 718 |
+
if None: count = edges.max() + 1
|
| 719 |
+
data : (n,) any
|
| 720 |
+
Assign data to each edge, if None will
|
| 721 |
+
be bool True for each specified edge
|
| 722 |
+
|
| 723 |
+
Returns
|
| 724 |
+
------------
|
| 725 |
+
matrix: (count, count) scipy.sparse.coo_matrix
|
| 726 |
+
Sparse COO
|
| 727 |
+
"""
|
| 728 |
+
edges = np.asanyarray(edges, dtype=np.int64)
|
| 729 |
+
if not (len(edges) == 0 or util.is_shape(edges, (-1, 2))):
|
| 730 |
+
raise ValueError("edges must be (n, 2)!")
|
| 731 |
+
|
| 732 |
+
# if count isn't specified just set it to largest
|
| 733 |
+
# value referenced in edges
|
| 734 |
+
if count is None:
|
| 735 |
+
count = edges.max() + 1
|
| 736 |
+
count = int(count)
|
| 737 |
+
|
| 738 |
+
# if no data is specified set every specified edge
|
| 739 |
+
# to True
|
| 740 |
+
if data is None:
|
| 741 |
+
data = np.ones(len(edges), dtype=bool)
|
| 742 |
+
|
| 743 |
+
return coo_matrix((data, edges.T), dtype=data.dtype, shape=(count, count))
|
| 744 |
+
|
| 745 |
+
|
| 746 |
+
def neighbors(edges, max_index=None, directed=False):
|
| 747 |
+
"""
|
| 748 |
+
Find the neighbors for each node in an edgelist graph.
|
| 749 |
+
|
| 750 |
+
TODO : re-write this with sparse matrix operations
|
| 751 |
+
|
| 752 |
+
Parameters
|
| 753 |
+
------------
|
| 754 |
+
edges : (n, 2) int
|
| 755 |
+
Connected nodes
|
| 756 |
+
directed : bool
|
| 757 |
+
If True, only connect edges in one direction
|
| 758 |
+
|
| 759 |
+
Returns
|
| 760 |
+
---------
|
| 761 |
+
neighbors : sequence
|
| 762 |
+
Vertex index corresponds to set of other vertex indices
|
| 763 |
+
"""
|
| 764 |
+
neighbors = collections.defaultdict(set)
|
| 765 |
+
if directed:
|
| 766 |
+
[neighbors[edge[0]].add(edge[1]) for edge in edges]
|
| 767 |
+
else:
|
| 768 |
+
[
|
| 769 |
+
(neighbors[edge[0]].add(edge[1]), neighbors[edge[1]].add(edge[0]))
|
| 770 |
+
for edge in edges
|
| 771 |
+
]
|
| 772 |
+
|
| 773 |
+
if max_index is None:
|
| 774 |
+
max_index = edges.max() + 1
|
| 775 |
+
array = [list(neighbors[i]) for i in range(max_index)]
|
| 776 |
+
|
| 777 |
+
return array
|
| 778 |
+
|
| 779 |
+
|
| 780 |
+
def smooth_shade(mesh, angle: Number | None = None, facet_minarea: Number | None = 10.0):
|
| 781 |
+
"""
|
| 782 |
+
Return a non-watertight version of the mesh which
|
| 783 |
+
will render nicely with smooth shading by
|
| 784 |
+
disconnecting faces at sharp angles to each other.
|
| 785 |
+
|
| 786 |
+
Parameters
|
| 787 |
+
-----------
|
| 788 |
+
mesh : trimesh.Trimesh
|
| 789 |
+
Source geometry
|
| 790 |
+
angle : float or None
|
| 791 |
+
Angle in radians face pairs with angles
|
| 792 |
+
smaller than this will appear smoothed
|
| 793 |
+
facet_minarea : float or None
|
| 794 |
+
Minimum area fraction to consider
|
| 795 |
+
IE for `facets_minarea=25` only facets larger
|
| 796 |
+
than `mesh.area / 25` will be considered.
|
| 797 |
+
|
| 798 |
+
Returns
|
| 799 |
+
---------
|
| 800 |
+
smooth : trimesh.Trimesh
|
| 801 |
+
Geometry with disconnected face patches
|
| 802 |
+
"""
|
| 803 |
+
if angle is None:
|
| 804 |
+
angle = np.radians(20)
|
| 805 |
+
|
| 806 |
+
# if the mesh has no adjacent faces return a copy
|
| 807 |
+
if len(mesh.face_adjacency) == 0:
|
| 808 |
+
return mesh.copy()
|
| 809 |
+
|
| 810 |
+
# face pairs below angle threshold
|
| 811 |
+
angle_ok = mesh.face_adjacency_angles < angle
|
| 812 |
+
# subset of face adjacency
|
| 813 |
+
adjacency = mesh.face_adjacency[angle_ok]
|
| 814 |
+
|
| 815 |
+
# coplanar groups of faces
|
| 816 |
+
facets = []
|
| 817 |
+
nodes = None
|
| 818 |
+
# collect coplanar regions for smoothing
|
| 819 |
+
if facet_minarea is not None:
|
| 820 |
+
areas = mesh.area_faces
|
| 821 |
+
min_area = mesh.area / facet_minarea
|
| 822 |
+
try:
|
| 823 |
+
# we can survive not knowing facets
|
| 824 |
+
# exclude facets with few faces
|
| 825 |
+
facets = [f for f in mesh.facets if areas[f].sum() > min_area]
|
| 826 |
+
if len(facets) > 0:
|
| 827 |
+
# mask for removing adjacency pairs where
|
| 828 |
+
# one of the faces is contained in a facet
|
| 829 |
+
mask = np.ones(len(mesh.faces), dtype=bool)
|
| 830 |
+
mask[np.hstack(facets)] = False
|
| 831 |
+
# apply the mask to adjacency
|
| 832 |
+
adjacency = adjacency[mask[adjacency].all(axis=1)]
|
| 833 |
+
# nodes are no longer every faces
|
| 834 |
+
nodes = np.unique(adjacency)
|
| 835 |
+
except BaseException:
|
| 836 |
+
log.warning("failed to calculate facets", exc_info=True)
|
| 837 |
+
# run connected components on facet adjacency
|
| 838 |
+
components = connected_components(adjacency, min_len=2, nodes=nodes)
|
| 839 |
+
|
| 840 |
+
# add back coplanar groups if any exist
|
| 841 |
+
if len(facets) > 0:
|
| 842 |
+
components.extend(facets)
|
| 843 |
+
|
| 844 |
+
if len(components) == 0:
|
| 845 |
+
# if no components for some reason
|
| 846 |
+
# just return a copy of the original mesh
|
| 847 |
+
return mesh.copy()
|
| 848 |
+
|
| 849 |
+
# add back any faces that were missed
|
| 850 |
+
unique = np.unique(np.hstack(components))
|
| 851 |
+
if len(unique) != len(mesh.faces):
|
| 852 |
+
# things like single loose faces
|
| 853 |
+
# or groups below facet_minlen
|
| 854 |
+
broke = np.setdiff1d(np.arange(len(mesh.faces)), unique)
|
| 855 |
+
components.extend(broke.reshape((-1, 1)))
|
| 856 |
+
|
| 857 |
+
# get a submesh as a single appended Trimesh
|
| 858 |
+
smooth = mesh.submesh(components, only_watertight=False, append=True)
|
| 859 |
+
# store face indices from original mesh
|
| 860 |
+
smooth.metadata["original_components"] = components
|
| 861 |
+
# smoothed should have exactly the same number of faces
|
| 862 |
+
if len(smooth.faces) != len(mesh.faces):
|
| 863 |
+
log.warning("face count in smooth wrong!")
|
| 864 |
+
return smooth
|
| 865 |
+
|
| 866 |
+
|
| 867 |
+
def is_watertight(
|
| 868 |
+
edges: ArrayLike, edges_sorted: ArrayLike | None = None
|
| 869 |
+
) -> tuple[bool, bool]:
|
| 870 |
+
"""
|
| 871 |
+
Parameters
|
| 872 |
+
-----------
|
| 873 |
+
edges : (n, 2) int
|
| 874 |
+
List of vertex indices
|
| 875 |
+
edges_sorted : (n, 2) int
|
| 876 |
+
Pass vertex indices sorted on axis 1 as a speedup
|
| 877 |
+
|
| 878 |
+
Returns
|
| 879 |
+
---------
|
| 880 |
+
watertight : boolean
|
| 881 |
+
Whether every edge is shared by an even
|
| 882 |
+
number of faces
|
| 883 |
+
winding : boolean
|
| 884 |
+
Whether every shared edge is reversed
|
| 885 |
+
"""
|
| 886 |
+
# passing edges_sorted is a speedup only
|
| 887 |
+
if edges_sorted is None:
|
| 888 |
+
edges_sorted = np.sort(edges, axis=1)
|
| 889 |
+
|
| 890 |
+
# group sorted edges throwing away any edge
|
| 891 |
+
# that doesn't appear exactly twice
|
| 892 |
+
groups = grouping.group_rows(edges_sorted, require_count=2)
|
| 893 |
+
# if we didn't throw away any edges that means
|
| 894 |
+
# every edge shows up exactly twice
|
| 895 |
+
watertight = bool((len(groups) * 2) == len(edges))
|
| 896 |
+
|
| 897 |
+
# check that the un-sorted duplicate edges are reversed
|
| 898 |
+
opposing = edges[groups].reshape((-1, 4))[:, 1:3].T
|
| 899 |
+
winding = bool(np.equal(*opposing).all())
|
| 900 |
+
|
| 901 |
+
return watertight, winding
|
| 902 |
+
|
| 903 |
+
|
| 904 |
+
def graph_to_svg(graph):
|
| 905 |
+
"""
|
| 906 |
+
Turn a networkx graph into an SVG string
|
| 907 |
+
using graphviz `dot`.
|
| 908 |
+
|
| 909 |
+
Parameters
|
| 910 |
+
----------
|
| 911 |
+
graph: networkx graph
|
| 912 |
+
|
| 913 |
+
Returns
|
| 914 |
+
---------
|
| 915 |
+
svg: string, pictoral layout in SVG format
|
| 916 |
+
"""
|
| 917 |
+
|
| 918 |
+
import subprocess
|
| 919 |
+
import tempfile
|
| 920 |
+
|
| 921 |
+
with tempfile.NamedTemporaryFile() as dot_file:
|
| 922 |
+
nx.drawing.nx_agraph.write_dot(graph, dot_file.name)
|
| 923 |
+
svg = subprocess.check_output(["dot", dot_file.name, "-Tsvg"])
|
| 924 |
+
return svg
|
| 925 |
+
|
| 926 |
+
|
| 927 |
+
def multigraph_paths(G, source, cutoff=None):
|
| 928 |
+
"""
|
| 929 |
+
For a networkx MultiDiGraph, find all paths from a source node
|
| 930 |
+
to leaf nodes. This function returns edge instance numbers
|
| 931 |
+
in addition to nodes, unlike networkx.all_simple_paths.
|
| 932 |
+
|
| 933 |
+
Parameters
|
| 934 |
+
---------------
|
| 935 |
+
G : networkx.MultiDiGraph
|
| 936 |
+
Graph to evaluate
|
| 937 |
+
source : hashable
|
| 938 |
+
Node to start traversal at
|
| 939 |
+
cutoff : int
|
| 940 |
+
Number of nodes to visit
|
| 941 |
+
If None will visit all nodes
|
| 942 |
+
|
| 943 |
+
Returns
|
| 944 |
+
----------
|
| 945 |
+
traversals : (n,) list of [(node, edge instance index), ] paths
|
| 946 |
+
Traversals of the multigraph
|
| 947 |
+
"""
|
| 948 |
+
if cutoff is None:
|
| 949 |
+
cutoff = (len(G.edges()) * len(G.nodes())) + 1
|
| 950 |
+
|
| 951 |
+
# the path starts at the node specified
|
| 952 |
+
current = [(source, 0)]
|
| 953 |
+
# traversals we need to go back and do
|
| 954 |
+
queue = []
|
| 955 |
+
# completed paths
|
| 956 |
+
traversals = []
|
| 957 |
+
|
| 958 |
+
for _ in range(cutoff):
|
| 959 |
+
# paths are stored as (node, instance) so
|
| 960 |
+
# get the node of the last place visited
|
| 961 |
+
current_node = current[-1][0]
|
| 962 |
+
# get all the children of the current node
|
| 963 |
+
child = G[current_node]
|
| 964 |
+
|
| 965 |
+
if len(child) == 0:
|
| 966 |
+
# we have no children, so we are at the end of this path
|
| 967 |
+
# save the path as a completed traversal
|
| 968 |
+
traversals.append(current)
|
| 969 |
+
# if there is nothing on the queue, we are done
|
| 970 |
+
if len(queue) == 0:
|
| 971 |
+
break
|
| 972 |
+
# otherwise continue traversing with the next path
|
| 973 |
+
# on the queue
|
| 974 |
+
current = queue.pop()
|
| 975 |
+
else:
|
| 976 |
+
# oh no, we have multiple edges from current -> child
|
| 977 |
+
start = True
|
| 978 |
+
# iterate through child nodes and edge instances
|
| 979 |
+
for node in child.keys():
|
| 980 |
+
for instance in child[node].keys():
|
| 981 |
+
if start:
|
| 982 |
+
# if this is the first edge, keep it on the
|
| 983 |
+
# current traversal and save the others for later
|
| 984 |
+
current.append((node, instance))
|
| 985 |
+
start = False
|
| 986 |
+
else:
|
| 987 |
+
# this child has multiple instances
|
| 988 |
+
# so we will need to traverse them multiple times
|
| 989 |
+
# we appended a node to current, so only take the
|
| 990 |
+
# first n-1 visits
|
| 991 |
+
queue.append(current[:-1] + [(node, instance)])
|
| 992 |
+
return traversals
|
| 993 |
+
|
| 994 |
+
|
| 995 |
+
def multigraph_collect(G, traversal, attrib=None):
|
| 996 |
+
"""
|
| 997 |
+
Given a MultiDiGraph traversal, collect attributes along it.
|
| 998 |
+
|
| 999 |
+
Parameters
|
| 1000 |
+
-------------
|
| 1001 |
+
G: networkx.MultiDiGraph
|
| 1002 |
+
traversal: (n) list of (node, instance) tuples
|
| 1003 |
+
attrib: dict key, name to collect. If None, will return all
|
| 1004 |
+
|
| 1005 |
+
Returns
|
| 1006 |
+
-------------
|
| 1007 |
+
collected: (len(traversal) - 1) list of attributes
|
| 1008 |
+
"""
|
| 1009 |
+
|
| 1010 |
+
collected = []
|
| 1011 |
+
for u, v in util.pairwise(traversal):
|
| 1012 |
+
attribs = G[u[0]][v[0]][v[1]]
|
| 1013 |
+
if attrib is None:
|
| 1014 |
+
collected.append(attribs)
|
| 1015 |
+
else:
|
| 1016 |
+
collected.append(attribs[attrib])
|
| 1017 |
+
return collected
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/grouping.py
ADDED
|
@@ -0,0 +1,877 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
grouping.py
|
| 3 |
+
-------------
|
| 4 |
+
|
| 5 |
+
Functions for grouping values and rows.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from . import util
|
| 11 |
+
from .constants import log, tol
|
| 12 |
+
from .typed import ArrayLike, Integer, NDArray, Number, Sequence
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
from scipy.spatial import cKDTree
|
| 16 |
+
except BaseException as E:
|
| 17 |
+
# wrapping just ImportError fails in some cases
|
| 18 |
+
# will raise the error when someone tries to use KDtree
|
| 19 |
+
from . import exceptions
|
| 20 |
+
|
| 21 |
+
cKDTree = exceptions.ExceptionWrapper(E)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def merge_vertices(
|
| 25 |
+
mesh,
|
| 26 |
+
merge_tex: bool | None = None,
|
| 27 |
+
merge_norm: bool | None = None,
|
| 28 |
+
digits_vertex: Integer | None = None,
|
| 29 |
+
digits_norm: Integer | None = None,
|
| 30 |
+
digits_uv: Integer | None = None,
|
| 31 |
+
):
|
| 32 |
+
"""
|
| 33 |
+
Removes duplicate vertices, grouped by position and
|
| 34 |
+
optionally texture coordinate and normal.
|
| 35 |
+
|
| 36 |
+
Parameters
|
| 37 |
+
-------------
|
| 38 |
+
mesh : Trimesh object
|
| 39 |
+
Mesh to merge vertices on
|
| 40 |
+
merge_tex : bool
|
| 41 |
+
If True textured meshes with UV coordinates will
|
| 42 |
+
have vertices merged regardless of UV coordinates
|
| 43 |
+
merge_norm : bool
|
| 44 |
+
If True, meshes with vertex normals will have
|
| 45 |
+
vertices merged ignoring different normals
|
| 46 |
+
digits_vertex : None or int
|
| 47 |
+
Number of digits to consider for vertex position
|
| 48 |
+
digits_norm : int
|
| 49 |
+
Number of digits to consider for unit normals
|
| 50 |
+
digits_uv : int
|
| 51 |
+
Number of digits to consider for UV coordinates
|
| 52 |
+
"""
|
| 53 |
+
# no vertices so exit early
|
| 54 |
+
if len(mesh.vertices) == 0:
|
| 55 |
+
return
|
| 56 |
+
if merge_tex is None:
|
| 57 |
+
merge_tex = False
|
| 58 |
+
if merge_norm is None:
|
| 59 |
+
merge_norm = False
|
| 60 |
+
if digits_norm is None:
|
| 61 |
+
digits_norm = 2
|
| 62 |
+
if digits_uv is None:
|
| 63 |
+
digits_uv = 4
|
| 64 |
+
if digits_vertex is None:
|
| 65 |
+
# use tol.merge if digit precision not passed
|
| 66 |
+
digits_vertex = util.decimal_to_digits(tol.merge)
|
| 67 |
+
|
| 68 |
+
# if we have a ton of unreferenced vertices it will
|
| 69 |
+
# make the unique_rows call super slow so cull first
|
| 70 |
+
if hasattr(mesh, "faces") and len(mesh.faces) > 0:
|
| 71 |
+
referenced = np.zeros(len(mesh.vertices), dtype=bool)
|
| 72 |
+
referenced[mesh.faces] = True
|
| 73 |
+
else:
|
| 74 |
+
# this is used for geometry without faces
|
| 75 |
+
referenced = np.ones(len(mesh.vertices), dtype=bool)
|
| 76 |
+
|
| 77 |
+
# collect vertex attributes into sequence we can stack
|
| 78 |
+
stacked = [mesh.vertices * (10**digits_vertex)]
|
| 79 |
+
|
| 80 |
+
# UV texture visuals require us to update the
|
| 81 |
+
# vertices and normals differently
|
| 82 |
+
if (
|
| 83 |
+
not merge_tex
|
| 84 |
+
and mesh.visual.defined
|
| 85 |
+
and mesh.visual.kind == "texture"
|
| 86 |
+
and mesh.visual.uv is not None
|
| 87 |
+
and len(mesh.visual.uv) == len(mesh.vertices)
|
| 88 |
+
):
|
| 89 |
+
# get an array with vertices and UV coordinates
|
| 90 |
+
# converted to integers at requested precision
|
| 91 |
+
stacked.append(mesh.visual.uv * (10**digits_uv))
|
| 92 |
+
|
| 93 |
+
# check to see if we have vertex normals
|
| 94 |
+
normals = mesh._cache["vertex_normals"]
|
| 95 |
+
if not merge_norm and np.shape(normals) == mesh.vertices.shape:
|
| 96 |
+
stacked.append(normals * (10**digits_norm))
|
| 97 |
+
|
| 98 |
+
# stack collected vertex properties and round to integer
|
| 99 |
+
stacked = np.column_stack(stacked).round().astype(np.int64)
|
| 100 |
+
|
| 101 |
+
# check unique rows of referenced vertices
|
| 102 |
+
u, i = unique_rows(stacked[referenced], keep_order=True)
|
| 103 |
+
|
| 104 |
+
# construct an inverse using the subset
|
| 105 |
+
inverse = np.zeros(len(mesh.vertices), dtype=np.int64)
|
| 106 |
+
inverse[referenced] = i
|
| 107 |
+
# get the vertex mask
|
| 108 |
+
mask = np.nonzero(referenced)[0][u]
|
| 109 |
+
# run the update including normals and UV coordinates
|
| 110 |
+
mesh.update_vertices(mask=mask, inverse=inverse)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def group(values, min_len: Integer | None = None, max_len: Integer | None = None):
|
| 114 |
+
"""
|
| 115 |
+
Return the indices of values that are identical
|
| 116 |
+
|
| 117 |
+
Parameters
|
| 118 |
+
----------
|
| 119 |
+
values : (n,) int
|
| 120 |
+
Values to group
|
| 121 |
+
min_len : int
|
| 122 |
+
The shortest group allowed
|
| 123 |
+
All groups will have len >= min_length
|
| 124 |
+
max_len : int
|
| 125 |
+
The longest group allowed
|
| 126 |
+
All groups will have len <= max_length
|
| 127 |
+
|
| 128 |
+
Returns
|
| 129 |
+
----------
|
| 130 |
+
groups : sequence
|
| 131 |
+
Contains indices to form groups
|
| 132 |
+
IE [0,1,0,1] returns [[0,2], [1,3]]
|
| 133 |
+
"""
|
| 134 |
+
original = np.asanyarray(values)
|
| 135 |
+
|
| 136 |
+
# save the sorted order and then apply it
|
| 137 |
+
order = original.argsort()
|
| 138 |
+
values = original[order]
|
| 139 |
+
|
| 140 |
+
# find the indexes which are duplicates
|
| 141 |
+
if values.dtype.kind == "f":
|
| 142 |
+
# for floats in a sorted array, neighbors are not duplicates
|
| 143 |
+
# if the difference between them is greater than approximate zero
|
| 144 |
+
nondupe = np.greater(np.abs(np.diff(values)), tol.zero)
|
| 145 |
+
else:
|
| 146 |
+
# for ints and strings we can check exact non- equality
|
| 147 |
+
# for all other types this will only work if they defined
|
| 148 |
+
# an __eq__
|
| 149 |
+
nondupe = values[1:] != values[:-1]
|
| 150 |
+
|
| 151 |
+
dupe_idx = np.append(0, np.nonzero(nondupe)[0] + 1)
|
| 152 |
+
|
| 153 |
+
# start with a mask that marks everything as ok
|
| 154 |
+
dupe_ok = np.ones(len(dupe_idx), dtype=bool)
|
| 155 |
+
|
| 156 |
+
# calculate the length of each group from their index
|
| 157 |
+
dupe_len = np.diff(np.concatenate((dupe_idx, [len(values)])))
|
| 158 |
+
|
| 159 |
+
# cull by length if requested
|
| 160 |
+
if min_len is not None or max_len is not None:
|
| 161 |
+
if min_len is not None:
|
| 162 |
+
dupe_ok &= dupe_len >= min_len
|
| 163 |
+
if max_len is not None:
|
| 164 |
+
dupe_ok &= dupe_len <= max_len
|
| 165 |
+
|
| 166 |
+
groups = [order[i : (i + j)] for i, j in zip(dupe_idx[dupe_ok], dupe_len[dupe_ok])]
|
| 167 |
+
return groups
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def hashable_rows(
|
| 171 |
+
data: ArrayLike, digits: Integer | None = None, allow_int: bool = True
|
| 172 |
+
) -> NDArray:
|
| 173 |
+
"""
|
| 174 |
+
We turn our array into integers based on the precision
|
| 175 |
+
given by digits and then put them in a hashable format.
|
| 176 |
+
|
| 177 |
+
Parameters
|
| 178 |
+
---------
|
| 179 |
+
data : (n, m) array
|
| 180 |
+
Input data
|
| 181 |
+
digits : int or None
|
| 182 |
+
How many digits to add to hash if data is floating point
|
| 183 |
+
If None, tol.merge will be used
|
| 184 |
+
|
| 185 |
+
Returns
|
| 186 |
+
---------
|
| 187 |
+
hashable : (n,)
|
| 188 |
+
May return as a `np.void` or a `np.uint64`
|
| 189 |
+
"""
|
| 190 |
+
# if there is no data return immediately
|
| 191 |
+
if len(data) == 0:
|
| 192 |
+
return np.array([], dtype=np.uint64)
|
| 193 |
+
|
| 194 |
+
# get array as integer to precision we care about
|
| 195 |
+
as_int = float_to_int(data, digits=digits)
|
| 196 |
+
|
| 197 |
+
# if it is flat integers already return
|
| 198 |
+
if len(as_int.shape) == 1:
|
| 199 |
+
return as_int
|
| 200 |
+
|
| 201 |
+
# rows without any columns are all identical: return a constant per row
|
| 202 |
+
# rather than dividing by the column count below
|
| 203 |
+
if as_int.shape[1] == 0:
|
| 204 |
+
return np.zeros(len(as_int), dtype=np.uint64)
|
| 205 |
+
|
| 206 |
+
# if array is 2D and smallish, we can try bitbanging
|
| 207 |
+
# this is significantly faster than the custom dtype
|
| 208 |
+
if allow_int and len(as_int.shape) == 2 and as_int.shape[1] <= 4:
|
| 209 |
+
# can we pack the whole row into a single 64 bit integer
|
| 210 |
+
precision = int(np.floor(64 / as_int.shape[1]))
|
| 211 |
+
|
| 212 |
+
# get the extreme values of the data set
|
| 213 |
+
d_min, d_max = as_int.min(), as_int.max()
|
| 214 |
+
# since we are quantizing the data down we need every value
|
| 215 |
+
# to fit in a partial integer so we have to check against extrema
|
| 216 |
+
threshold = (2 ** (precision - 1)) - 1
|
| 217 |
+
|
| 218 |
+
# if the data is within the range of our precision threshold
|
| 219 |
+
if d_max < threshold and d_min > -threshold:
|
| 220 |
+
# the resulting package
|
| 221 |
+
hashable = np.zeros(len(as_int), dtype=np.uint64)
|
| 222 |
+
# offset to the middle of the unsigned integer range
|
| 223 |
+
# this array should contain only positive values
|
| 224 |
+
bitbang = (as_int.T + (threshold + 1)).astype(np.uint64)
|
| 225 |
+
# loop through each column and bitwise xor to combine
|
| 226 |
+
# make sure as_int is int64 otherwise bit offset won't work
|
| 227 |
+
for offset, column in enumerate(bitbang):
|
| 228 |
+
# will modify hashable in place
|
| 229 |
+
np.bitwise_xor(hashable, column << (offset * precision), out=hashable)
|
| 230 |
+
return hashable
|
| 231 |
+
|
| 232 |
+
# reshape array into magical data type that is weird but works with unique
|
| 233 |
+
dtype = np.dtype((np.void, as_int.dtype.itemsize * as_int.shape[1]))
|
| 234 |
+
# make sure result is contiguous and flat
|
| 235 |
+
result = np.ascontiguousarray(as_int).view(dtype).reshape(-1)
|
| 236 |
+
result.flags["WRITEABLE"] = False
|
| 237 |
+
|
| 238 |
+
return result
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def float_to_int(data, digits: Integer | None = None) -> NDArray[np.int64]:
|
| 242 |
+
"""
|
| 243 |
+
Given a numpy array of float/bool/int, return as integers.
|
| 244 |
+
|
| 245 |
+
Parameters
|
| 246 |
+
-------------
|
| 247 |
+
data : (n, d) float, int, or bool
|
| 248 |
+
Input data
|
| 249 |
+
digits : float or int
|
| 250 |
+
Precision for float conversion
|
| 251 |
+
|
| 252 |
+
Returns
|
| 253 |
+
-------------
|
| 254 |
+
as_int : (n, d) int
|
| 255 |
+
Data as integers
|
| 256 |
+
"""
|
| 257 |
+
# convert to any numpy array
|
| 258 |
+
data = np.asanyarray(data)
|
| 259 |
+
|
| 260 |
+
# we can early-exit if we've been passed data that is already
|
| 261 |
+
# an integer, unsigned integer, boolean, or empty
|
| 262 |
+
if data.dtype == np.int64:
|
| 263 |
+
return data
|
| 264 |
+
elif data.dtype.kind in "iub" or data.size == 0:
|
| 265 |
+
return data.astype(np.int64)
|
| 266 |
+
elif data.dtype.kind != "f":
|
| 267 |
+
# if it's not a floating point try to make it one
|
| 268 |
+
data = data.astype(np.float64)
|
| 269 |
+
|
| 270 |
+
if digits is None:
|
| 271 |
+
# get digits from `tol.merge`
|
| 272 |
+
digits = util.decimal_to_digits(tol.merge)
|
| 273 |
+
elif not isinstance(digits, (int, np.integer)):
|
| 274 |
+
raise TypeError(f"Digits must be `None` or `int`, not `{type(digits)}`")
|
| 275 |
+
|
| 276 |
+
# multiply by requested power of ten
|
| 277 |
+
# then subtract small epsilon to avoid "go either way" rounding
|
| 278 |
+
# then do the rounding and convert to integer
|
| 279 |
+
return np.round((data * 10**digits) - 1e-6).astype(np.int64)
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
def unique_ordered(
|
| 283 |
+
data: ArrayLike, return_index: bool = False, return_inverse: bool = False
|
| 284 |
+
):
|
| 285 |
+
"""
|
| 286 |
+
Returns the same as np.unique, but ordered as per the
|
| 287 |
+
first occurrence of the unique value in data.
|
| 288 |
+
|
| 289 |
+
Examples
|
| 290 |
+
---------
|
| 291 |
+
In [1]: a = [0, 3, 3, 4, 1, 3, 0, 3, 2, 1]
|
| 292 |
+
|
| 293 |
+
In [2]: np.unique(a)
|
| 294 |
+
Out[2]: array([0, 1, 2, 3, 4])
|
| 295 |
+
|
| 296 |
+
In [3]: trimesh.grouping.unique_ordered(a)
|
| 297 |
+
Out[3]: array([0, 3, 4, 1, 2])
|
| 298 |
+
"""
|
| 299 |
+
# uniques are the values, sorted
|
| 300 |
+
# index is the value in the original `data`
|
| 301 |
+
# i.e. `data[index] == unique`
|
| 302 |
+
# inverse is how to re-construct `data` from `unique`
|
| 303 |
+
# i.e. `unique[inverse] == data`
|
| 304 |
+
unique, index, inverse = np.unique(data, return_index=True, return_inverse=True)
|
| 305 |
+
|
| 306 |
+
# we want to maintain the original index order
|
| 307 |
+
order = index.argsort()
|
| 308 |
+
|
| 309 |
+
if not return_index and not return_inverse:
|
| 310 |
+
return unique[order]
|
| 311 |
+
|
| 312 |
+
# collect return values
|
| 313 |
+
# start with the unique values in original order
|
| 314 |
+
result = [unique[order]]
|
| 315 |
+
# the new index values
|
| 316 |
+
if return_index:
|
| 317 |
+
# re-order the index in the original array
|
| 318 |
+
result.append(index[order])
|
| 319 |
+
if return_inverse:
|
| 320 |
+
# create the new inverse from the order of the order
|
| 321 |
+
result.append(order.argsort()[inverse])
|
| 322 |
+
|
| 323 |
+
return result
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
def unique_bincount(
|
| 327 |
+
values: ArrayLike,
|
| 328 |
+
minlength: Integer = 0,
|
| 329 |
+
return_inverse: bool = False,
|
| 330 |
+
return_counts: bool = False,
|
| 331 |
+
):
|
| 332 |
+
"""
|
| 333 |
+
For arrays of integers find unique values using bin counting.
|
| 334 |
+
Roughly 10x faster for correct input than np.unique
|
| 335 |
+
|
| 336 |
+
Parameters
|
| 337 |
+
--------------
|
| 338 |
+
values : (n,) int
|
| 339 |
+
Values to find unique members of
|
| 340 |
+
minlength : int
|
| 341 |
+
Maximum value that will occur in values (values.max())
|
| 342 |
+
return_inverse : bool
|
| 343 |
+
If True, return an inverse such that unique[inverse] == values
|
| 344 |
+
return_counts : bool
|
| 345 |
+
If True, also return the number of times each
|
| 346 |
+
unique item appears in values
|
| 347 |
+
|
| 348 |
+
Returns
|
| 349 |
+
------------
|
| 350 |
+
unique : (m,) int
|
| 351 |
+
Unique values in original array
|
| 352 |
+
inverse : (n,) int, optional
|
| 353 |
+
An array such that unique[inverse] == values
|
| 354 |
+
Only returned if return_inverse is True
|
| 355 |
+
counts : (m,) int, optional
|
| 356 |
+
An array holding the counts of each unique item in values
|
| 357 |
+
Only returned if return_counts is True
|
| 358 |
+
"""
|
| 359 |
+
values = np.asanyarray(values)
|
| 360 |
+
if len(values.shape) != 1 or values.dtype.kind != "i":
|
| 361 |
+
raise ValueError("input must be 1D integers!")
|
| 362 |
+
|
| 363 |
+
try:
|
| 364 |
+
# count the number of occurrences of each value
|
| 365 |
+
counts = np.bincount(values, minlength=minlength)
|
| 366 |
+
except TypeError:
|
| 367 |
+
# casting failed on 32 bit windows
|
| 368 |
+
log.warning("casting failed, falling back!")
|
| 369 |
+
# fall back to numpy unique
|
| 370 |
+
return np.unique(
|
| 371 |
+
values, return_inverse=return_inverse, return_counts=return_counts
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
# which bins are occupied at all
|
| 375 |
+
# counts are integers so this works
|
| 376 |
+
unique_bin = counts.astype(bool)
|
| 377 |
+
|
| 378 |
+
# which values are unique
|
| 379 |
+
# indexes correspond to original values
|
| 380 |
+
unique = np.where(unique_bin)[0]
|
| 381 |
+
ret = (unique,)
|
| 382 |
+
|
| 383 |
+
if return_inverse:
|
| 384 |
+
# find the inverse to reconstruct original
|
| 385 |
+
inverse = (np.cumsum(unique_bin) - 1)[values]
|
| 386 |
+
ret += (inverse,)
|
| 387 |
+
|
| 388 |
+
if return_counts:
|
| 389 |
+
unique_counts = counts[unique]
|
| 390 |
+
ret += (unique_counts,)
|
| 391 |
+
|
| 392 |
+
if len(ret) == 1:
|
| 393 |
+
return ret[0]
|
| 394 |
+
return ret
|
| 395 |
+
|
| 396 |
+
|
| 397 |
+
def merge_runs(data: ArrayLike, digits: Integer | None = None):
|
| 398 |
+
"""
|
| 399 |
+
Merge duplicate sequential values. This differs from unique_ordered
|
| 400 |
+
in that values can occur in multiple places in the sequence, but
|
| 401 |
+
only consecutive repeats are removed
|
| 402 |
+
|
| 403 |
+
Parameters
|
| 404 |
+
-----------
|
| 405 |
+
data: (n,) float or int
|
| 406 |
+
|
| 407 |
+
Returns
|
| 408 |
+
--------
|
| 409 |
+
merged: (m,) float or int
|
| 410 |
+
|
| 411 |
+
Examples
|
| 412 |
+
---------
|
| 413 |
+
In [1]: a
|
| 414 |
+
Out[1]:
|
| 415 |
+
array([-1, -1, -1, 0, 0, 1, 1, 2, 0,
|
| 416 |
+
3, 3, 4, 4, 5, 5, 6, 6, 7,
|
| 417 |
+
7, 8, 8, 9, 9, 9])
|
| 418 |
+
|
| 419 |
+
In [2]: trimesh.grouping.merge_runs(a)
|
| 420 |
+
Out[2]: array([-1, 0, 1, 2, 0, 3, 4, 5, 6, 7, 8, 9])
|
| 421 |
+
"""
|
| 422 |
+
if digits is None:
|
| 423 |
+
epsilon = tol.merge
|
| 424 |
+
else:
|
| 425 |
+
epsilon = 10 ** (-digits)
|
| 426 |
+
|
| 427 |
+
data = np.asanyarray(data)
|
| 428 |
+
mask = np.zeros(len(data), dtype=bool)
|
| 429 |
+
mask[0] = True
|
| 430 |
+
mask[1:] = np.abs(data[1:] - data[:-1]) > epsilon
|
| 431 |
+
|
| 432 |
+
return data[mask]
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
def unique_float(
|
| 436 |
+
data,
|
| 437 |
+
return_index: bool = False,
|
| 438 |
+
return_inverse: bool = False,
|
| 439 |
+
digits: Integer | None = None,
|
| 440 |
+
):
|
| 441 |
+
"""
|
| 442 |
+
Identical to the numpy.unique command, except evaluates floating point
|
| 443 |
+
numbers, using a specified number of digits.
|
| 444 |
+
|
| 445 |
+
If digits isn't specified, the library default TOL_MERGE will be used.
|
| 446 |
+
"""
|
| 447 |
+
data = np.asanyarray(data)
|
| 448 |
+
as_int = float_to_int(data, digits)
|
| 449 |
+
_junk, unique, inverse = np.unique(as_int, return_index=True, return_inverse=True)
|
| 450 |
+
|
| 451 |
+
if (not return_index) and (not return_inverse):
|
| 452 |
+
return data[unique]
|
| 453 |
+
|
| 454 |
+
result = [data[unique]]
|
| 455 |
+
|
| 456 |
+
if return_index:
|
| 457 |
+
result.append(unique)
|
| 458 |
+
if return_inverse:
|
| 459 |
+
result.append(inverse)
|
| 460 |
+
return tuple(result)
|
| 461 |
+
|
| 462 |
+
|
| 463 |
+
def unique_rows(data, digits=None, keep_order=False):
|
| 464 |
+
"""
|
| 465 |
+
Returns indices of unique rows. It will return the
|
| 466 |
+
first occurrence of a row that is duplicated:
|
| 467 |
+
[[1,2], [3,4], [1,2]] will return [0,1]
|
| 468 |
+
|
| 469 |
+
Parameters
|
| 470 |
+
---------
|
| 471 |
+
data : (n, m) array
|
| 472 |
+
Floating point data
|
| 473 |
+
digits : int or None
|
| 474 |
+
How many digits to consider
|
| 475 |
+
|
| 476 |
+
Returns
|
| 477 |
+
--------
|
| 478 |
+
unique : (j,) int
|
| 479 |
+
Index in data which is a unique row
|
| 480 |
+
inverse : (n,) int
|
| 481 |
+
Array to reconstruct original
|
| 482 |
+
Example: data[unique][inverse] == data
|
| 483 |
+
"""
|
| 484 |
+
# get rows hashable so we can run unique function on it
|
| 485 |
+
rows = hashable_rows(data, digits=digits)
|
| 486 |
+
|
| 487 |
+
# we are throwing away the first value which is the
|
| 488 |
+
# garbage row-hash and only returning index and inverse
|
| 489 |
+
if keep_order:
|
| 490 |
+
# keeps order of original occurrence
|
| 491 |
+
return unique_ordered(rows, return_index=True, return_inverse=True)[1:]
|
| 492 |
+
# returns values sorted by row-hash but since our row-hash
|
| 493 |
+
# were pretty much garbage the sort order isn't meaningful
|
| 494 |
+
return np.unique(rows, return_index=True, return_inverse=True)[1:]
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
def unique_value_in_row(data, unique=None):
|
| 498 |
+
"""
|
| 499 |
+
For a 2D array of integers find the position of a
|
| 500 |
+
value in each row which only occurs once.
|
| 501 |
+
|
| 502 |
+
If there are more than one value per row which
|
| 503 |
+
occur once, the last one is returned.
|
| 504 |
+
|
| 505 |
+
Parameters
|
| 506 |
+
----------
|
| 507 |
+
data : (n, d) int
|
| 508 |
+
Data to check values
|
| 509 |
+
unique : (m,) int
|
| 510 |
+
List of unique values contained in data.
|
| 511 |
+
Generated from np.unique if not passed
|
| 512 |
+
|
| 513 |
+
Returns
|
| 514 |
+
---------
|
| 515 |
+
result : (n, d) bool
|
| 516 |
+
With one or zero True values per row.
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
Examples
|
| 520 |
+
-------------------------------------
|
| 521 |
+
In [0]: r = np.array([[-1, 1, 1],
|
| 522 |
+
[-1, 1, -1],
|
| 523 |
+
[-1, 1, 1],
|
| 524 |
+
[-1, 1, -1],
|
| 525 |
+
[-1, 1, -1]], dtype=np.int8)
|
| 526 |
+
|
| 527 |
+
In [1]: unique_value_in_row(r)
|
| 528 |
+
Out[1]:
|
| 529 |
+
array([[ True, False, False],
|
| 530 |
+
[False, True, False],
|
| 531 |
+
[ True, False, False],
|
| 532 |
+
[False, True, False],
|
| 533 |
+
[False, True, False]], dtype=bool)
|
| 534 |
+
|
| 535 |
+
In [2]: unique_value_in_row(r).sum(axis=1)
|
| 536 |
+
Out[2]: array([1, 1, 1, 1, 1])
|
| 537 |
+
|
| 538 |
+
In [3]: r[unique_value_in_row(r)]
|
| 539 |
+
Out[3]: array([-1, 1, -1, 1, 1], dtype=int8)
|
| 540 |
+
"""
|
| 541 |
+
if unique is None:
|
| 542 |
+
unique = np.unique(data)
|
| 543 |
+
data = np.asanyarray(data)
|
| 544 |
+
result = np.zeros_like(data, dtype=bool, subok=False)
|
| 545 |
+
for value in unique:
|
| 546 |
+
test = np.equal(data, value)
|
| 547 |
+
test_ok = test.sum(axis=1) == 1
|
| 548 |
+
result[test_ok] = test[test_ok]
|
| 549 |
+
return result
|
| 550 |
+
|
| 551 |
+
|
| 552 |
+
def group_rows(data, require_count=None, digits=None):
|
| 553 |
+
"""
|
| 554 |
+
Returns index groups of duplicate rows, for example:
|
| 555 |
+
[[1,2], [3,4], [1,2]] will return [[0,2], [1]]
|
| 556 |
+
|
| 557 |
+
|
| 558 |
+
Note that using require_count allows numpy advanced
|
| 559 |
+
indexing to be used in place of looping and
|
| 560 |
+
checking hashes and is ~10x faster.
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
Parameters
|
| 564 |
+
----------
|
| 565 |
+
data : (n, m) array
|
| 566 |
+
Data to group
|
| 567 |
+
require_count : None or int
|
| 568 |
+
Only return groups of a specified length, eg:
|
| 569 |
+
require_count = 2
|
| 570 |
+
[[1,2], [3,4], [1,2]] will return [[0,2]]
|
| 571 |
+
digits : None or int
|
| 572 |
+
If data is floating point how many decimals
|
| 573 |
+
to consider, or calculated from tol.merge
|
| 574 |
+
|
| 575 |
+
Returns
|
| 576 |
+
----------
|
| 577 |
+
groups : sequence (*,) int
|
| 578 |
+
Indices from in indicating identical rows.
|
| 579 |
+
"""
|
| 580 |
+
|
| 581 |
+
# start with getting a sortable format
|
| 582 |
+
hashable = hashable_rows(data, digits=digits)
|
| 583 |
+
|
| 584 |
+
# if there isn't a constant column size use more complex logic
|
| 585 |
+
if require_count is None:
|
| 586 |
+
return group(hashable)
|
| 587 |
+
|
| 588 |
+
# record the order of the rows so we can get the original indices back
|
| 589 |
+
order = hashable.argsort()
|
| 590 |
+
# but for now, we want our hashes sorted
|
| 591 |
+
hashable = hashable[order]
|
| 592 |
+
# this is checking each neighbour for equality, example:
|
| 593 |
+
# example: hashable = [1, 1, 1]; dupe = [0, 0]
|
| 594 |
+
dupe = hashable[1:] != hashable[:-1]
|
| 595 |
+
# we want the first index of a group, so we can slice from that location
|
| 596 |
+
# example: hashable = [0 1 1]; dupe = [1,0]; dupe_idx = [0,1]
|
| 597 |
+
dupe_idx = np.append(0, np.nonzero(dupe)[0] + 1)
|
| 598 |
+
# if you wanted to use this one function to deal with non- regular groups
|
| 599 |
+
# you could use: np.array_split(dupe_idx)
|
| 600 |
+
# this is roughly 3x slower than using the group_dict method above.
|
| 601 |
+
start_ok = np.diff(np.concatenate((dupe_idx, [len(hashable)]))) == require_count
|
| 602 |
+
groups = np.tile(dupe_idx[start_ok].reshape((-1, 1)), require_count) + np.arange(
|
| 603 |
+
require_count
|
| 604 |
+
)
|
| 605 |
+
groups_idx = order[groups]
|
| 606 |
+
|
| 607 |
+
if require_count == 1:
|
| 608 |
+
return groups_idx.reshape(-1)
|
| 609 |
+
return groups_idx
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
def boolean_rows(
|
| 613 |
+
a: ArrayLike, b: ArrayLike, operation=np.intersect1d
|
| 614 |
+
) -> NDArray[np.int64]:
|
| 615 |
+
"""
|
| 616 |
+
Find the rows in two arrays which occur in both rows.
|
| 617 |
+
|
| 618 |
+
Parameters
|
| 619 |
+
---------
|
| 620 |
+
a: (n, d) int
|
| 621 |
+
Array with row vectors
|
| 622 |
+
b: (m, d) int
|
| 623 |
+
Array with row vectors
|
| 624 |
+
operation : function
|
| 625 |
+
Numpy boolean set operation function:
|
| 626 |
+
-np.intersect1d
|
| 627 |
+
-np.setdiff1d
|
| 628 |
+
|
| 629 |
+
Returns
|
| 630 |
+
--------
|
| 631 |
+
shared : (p, d) int64
|
| 632 |
+
Array containing requested rows in both a and b
|
| 633 |
+
"""
|
| 634 |
+
a = np.asanyarray(a, dtype=np.int64)
|
| 635 |
+
b = np.asanyarray(b, dtype=np.int64)
|
| 636 |
+
|
| 637 |
+
av = a.view([("", a.dtype)] * a.shape[1]).ravel()
|
| 638 |
+
bv = b.view([("", b.dtype)] * b.shape[1]).ravel()
|
| 639 |
+
return operation(av, bv).view(a.dtype).reshape(-1, a.shape[1])
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
def group_vectors(vectors, angle=1e-4, include_negative=False):
|
| 643 |
+
"""
|
| 644 |
+
Group vectors based on an angle tolerance, with the option to
|
| 645 |
+
include negative vectors.
|
| 646 |
+
|
| 647 |
+
Parameters
|
| 648 |
+
-----------
|
| 649 |
+
vectors : (n,3) float
|
| 650 |
+
Direction vector
|
| 651 |
+
angle : float
|
| 652 |
+
Group vectors closer than this angle in radians
|
| 653 |
+
include_negative : bool
|
| 654 |
+
If True consider the same:
|
| 655 |
+
[0,0,1] and [0,0,-1]
|
| 656 |
+
|
| 657 |
+
Returns
|
| 658 |
+
------------
|
| 659 |
+
new_vectors : (m,3) float
|
| 660 |
+
Direction vector
|
| 661 |
+
groups : (m,) sequence of int
|
| 662 |
+
Indices of source vectors
|
| 663 |
+
"""
|
| 664 |
+
|
| 665 |
+
vectors = np.asanyarray(vectors, dtype=np.float64)
|
| 666 |
+
angle = float(angle)
|
| 667 |
+
|
| 668 |
+
if include_negative:
|
| 669 |
+
vectors = util.vector_hemisphere(vectors)
|
| 670 |
+
|
| 671 |
+
spherical = util.vector_to_spherical(vectors)
|
| 672 |
+
angles, groups = group_distance(spherical, angle)
|
| 673 |
+
new_vectors = util.spherical_to_vector(angles)
|
| 674 |
+
return new_vectors, groups
|
| 675 |
+
|
| 676 |
+
|
| 677 |
+
def group_distance(
|
| 678 |
+
values: ArrayLike, distance: Number
|
| 679 |
+
) -> tuple[NDArray[np.float64], Sequence]:
|
| 680 |
+
"""
|
| 681 |
+
Find non-overlapping groups of points where no two points in a
|
| 682 |
+
group are farther than 2*distance apart.
|
| 683 |
+
|
| 684 |
+
Parameters
|
| 685 |
+
---------
|
| 686 |
+
values : (n, d) float
|
| 687 |
+
Points of dimension d
|
| 688 |
+
distance : float
|
| 689 |
+
Max distance between points in a cluster
|
| 690 |
+
|
| 691 |
+
Returns
|
| 692 |
+
----------
|
| 693 |
+
unique : (m, d) float
|
| 694 |
+
Median value of each group
|
| 695 |
+
groups : (m) sequence of int
|
| 696 |
+
Indexes of points that make up a group
|
| 697 |
+
|
| 698 |
+
"""
|
| 699 |
+
values = np.asanyarray(values, dtype=np.float64)
|
| 700 |
+
|
| 701 |
+
consumed = np.zeros(len(values), dtype=bool)
|
| 702 |
+
tree = cKDTree(values)
|
| 703 |
+
|
| 704 |
+
# (n, d) set of values that are unique
|
| 705 |
+
unique = []
|
| 706 |
+
# (n) sequence of indices in values
|
| 707 |
+
groups = []
|
| 708 |
+
|
| 709 |
+
for index, value in enumerate(values):
|
| 710 |
+
if consumed[index]:
|
| 711 |
+
continue
|
| 712 |
+
group = np.array(tree.query_ball_point(value, distance), dtype=np.int64)
|
| 713 |
+
group = group[~consumed[group]]
|
| 714 |
+
consumed[group] = True
|
| 715 |
+
unique.append(np.median(values[group], axis=0))
|
| 716 |
+
groups.append(group)
|
| 717 |
+
return np.array(unique), groups
|
| 718 |
+
|
| 719 |
+
|
| 720 |
+
def clusters(points, radius):
|
| 721 |
+
"""
|
| 722 |
+
Find clusters of points which have neighbours closer than radius
|
| 723 |
+
|
| 724 |
+
Parameters
|
| 725 |
+
---------
|
| 726 |
+
points : (n, d) float
|
| 727 |
+
Points of dimension d
|
| 728 |
+
radius : float
|
| 729 |
+
Max distance between points in a cluster
|
| 730 |
+
|
| 731 |
+
Returns
|
| 732 |
+
----------
|
| 733 |
+
groups : (m,) sequence of int
|
| 734 |
+
Indices of points in a cluster
|
| 735 |
+
|
| 736 |
+
"""
|
| 737 |
+
from . import graph
|
| 738 |
+
|
| 739 |
+
tree = cKDTree(points)
|
| 740 |
+
|
| 741 |
+
# some versions return pairs as a set of tuples
|
| 742 |
+
pairs = tree.query_pairs(r=radius, output_type="ndarray")
|
| 743 |
+
# group connected components
|
| 744 |
+
groups = graph.connected_components(pairs)
|
| 745 |
+
|
| 746 |
+
return groups
|
| 747 |
+
|
| 748 |
+
|
| 749 |
+
def blocks(data, min_len=2, max_len=np.inf, wrap=False, digits=None, only_nonzero=False):
|
| 750 |
+
"""
|
| 751 |
+
Find the indices in an array of contiguous blocks
|
| 752 |
+
of equal values.
|
| 753 |
+
|
| 754 |
+
Parameters
|
| 755 |
+
------------
|
| 756 |
+
data : (n,) array
|
| 757 |
+
Data to find blocks on
|
| 758 |
+
min_len : int
|
| 759 |
+
The minimum length group to be returned
|
| 760 |
+
max_len : int
|
| 761 |
+
The maximum length group to be retuurned
|
| 762 |
+
wrap : bool
|
| 763 |
+
Combine blocks on both ends of 1D array
|
| 764 |
+
digits : None or int
|
| 765 |
+
If dealing with floats how many digits to consider
|
| 766 |
+
only_nonzero : bool
|
| 767 |
+
Only return blocks of non- zero values
|
| 768 |
+
|
| 769 |
+
Returns
|
| 770 |
+
---------
|
| 771 |
+
blocks : (m) sequence of (*,) int
|
| 772 |
+
Indices referencing data
|
| 773 |
+
"""
|
| 774 |
+
data = float_to_int(data, digits=digits)
|
| 775 |
+
|
| 776 |
+
# keep an integer range around so we can slice
|
| 777 |
+
arange = np.arange(len(data))
|
| 778 |
+
arange.flags["WRITEABLE"] = False
|
| 779 |
+
|
| 780 |
+
nonzero = arange[1:][data[1:] != data[:-1]]
|
| 781 |
+
infl = np.zeros(len(nonzero) + 2, dtype=int)
|
| 782 |
+
infl[-1] = len(data)
|
| 783 |
+
infl[1:-1] = nonzero
|
| 784 |
+
|
| 785 |
+
# the length of each chunk
|
| 786 |
+
infl_len = infl[1:] - infl[:-1]
|
| 787 |
+
|
| 788 |
+
# check the length of each group
|
| 789 |
+
infl_ok = np.logical_and(infl_len >= min_len, infl_len <= max_len)
|
| 790 |
+
|
| 791 |
+
if only_nonzero:
|
| 792 |
+
# check to make sure the values of each contiguous block
|
| 793 |
+
# are True by checking the first value of each block
|
| 794 |
+
infl_ok = np.logical_and(infl_ok, data[infl[:-1]])
|
| 795 |
+
|
| 796 |
+
# inflate start/end indexes into full ranges of values
|
| 797 |
+
blocks = [arange[infl[i] : infl[i + 1]] for i, ok in enumerate(infl_ok) if ok]
|
| 798 |
+
|
| 799 |
+
if wrap:
|
| 800 |
+
# wrap only matters if first and last points are the same
|
| 801 |
+
if data[0] != data[-1]:
|
| 802 |
+
return blocks
|
| 803 |
+
# if we are only grouping nonzero things and
|
| 804 |
+
# the first and last point are zero we can exit
|
| 805 |
+
if only_nonzero and not bool(data[0]):
|
| 806 |
+
return blocks
|
| 807 |
+
|
| 808 |
+
# if all values are True or False we can exit
|
| 809 |
+
if len(blocks) == 1 and len(blocks[0]) == len(data):
|
| 810 |
+
return blocks
|
| 811 |
+
|
| 812 |
+
# so now first point equals last point, so the cases are:
|
| 813 |
+
# - first and last point are in a block: combine two blocks
|
| 814 |
+
# - first OR last point are in block: add other point to block
|
| 815 |
+
# - neither are in a block: check if combined is eligible block
|
| 816 |
+
|
| 817 |
+
# first point is in a block
|
| 818 |
+
first = len(blocks) > 0 and blocks[0][0] == 0
|
| 819 |
+
# last point is in a block
|
| 820 |
+
last = len(blocks) > 0 and blocks[-1][-1] == (len(data) - 1)
|
| 821 |
+
|
| 822 |
+
# CASE: first and last point are BOTH in block: combine blocks
|
| 823 |
+
if first and last:
|
| 824 |
+
blocks[0] = np.append(blocks[-1], blocks[0])
|
| 825 |
+
blocks.pop()
|
| 826 |
+
else:
|
| 827 |
+
# combined length
|
| 828 |
+
combined = infl_len[0] + infl_len[-1]
|
| 829 |
+
# exit if lengths aren't OK
|
| 830 |
+
if combined < min_len or combined > max_len:
|
| 831 |
+
return blocks
|
| 832 |
+
# new block combines both ends
|
| 833 |
+
new_block = np.append(
|
| 834 |
+
np.arange(infl[-2], infl[-1]), np.arange(infl[0], infl[1])
|
| 835 |
+
)
|
| 836 |
+
# we are in a first OR last situation now
|
| 837 |
+
if first:
|
| 838 |
+
# first was already in a block so replace it with combined
|
| 839 |
+
blocks[0] = new_block
|
| 840 |
+
elif last:
|
| 841 |
+
# last was already in a block so replace with superset
|
| 842 |
+
blocks[-1] = new_block
|
| 843 |
+
else:
|
| 844 |
+
# both are false
|
| 845 |
+
# combined length generated new block
|
| 846 |
+
blocks.append(new_block)
|
| 847 |
+
|
| 848 |
+
return blocks
|
| 849 |
+
|
| 850 |
+
|
| 851 |
+
def group_min(groups, data):
|
| 852 |
+
"""
|
| 853 |
+
Given a list of groups find the minimum element of data
|
| 854 |
+
within each group
|
| 855 |
+
|
| 856 |
+
Parameters
|
| 857 |
+
-----------
|
| 858 |
+
groups : (n,) sequence of (q,) int
|
| 859 |
+
Indexes of each group corresponding to each element in data
|
| 860 |
+
data : (m,)
|
| 861 |
+
The data that groups indexes reference
|
| 862 |
+
|
| 863 |
+
Returns
|
| 864 |
+
-----------
|
| 865 |
+
minimums : (n,)
|
| 866 |
+
Minimum value of data per group
|
| 867 |
+
|
| 868 |
+
"""
|
| 869 |
+
# sort with major key groups, minor key data
|
| 870 |
+
order = np.lexsort((data, groups))
|
| 871 |
+
groups = groups[order] # this is only needed if groups is unsorted
|
| 872 |
+
data = data[order]
|
| 873 |
+
# construct an index which marks borders between groups
|
| 874 |
+
index = np.zeros(len(groups), "bool")
|
| 875 |
+
index[0] = True
|
| 876 |
+
index[1:] = groups[1:] != groups[:-1]
|
| 877 |
+
return data[index]
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/inertia.py
ADDED
|
@@ -0,0 +1,363 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
inertia.py
|
| 3 |
+
-------------
|
| 4 |
+
|
| 5 |
+
Functions for dealing with inertia tensors.
|
| 6 |
+
|
| 7 |
+
Results validated against known geometries and checked for
|
| 8 |
+
internal consistency.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
from .typed import ArrayLike, NDArray, Number, float64
|
| 14 |
+
from .util import multi_dot
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def cylinder_inertia(
|
| 18 |
+
mass: Number, radius: Number, height: Number, transform: ArrayLike | None = None
|
| 19 |
+
) -> NDArray[float64]:
|
| 20 |
+
"""
|
| 21 |
+
Return the inertia tensor of a cylinder.
|
| 22 |
+
|
| 23 |
+
Parameters
|
| 24 |
+
------------
|
| 25 |
+
mass : float
|
| 26 |
+
Mass of cylinder
|
| 27 |
+
radius : float
|
| 28 |
+
Radius of cylinder
|
| 29 |
+
height : float
|
| 30 |
+
Height of cylinder
|
| 31 |
+
transform : (4, 4) float
|
| 32 |
+
Transformation of cylinder
|
| 33 |
+
|
| 34 |
+
Returns
|
| 35 |
+
------------
|
| 36 |
+
inertia : (3, 3) float
|
| 37 |
+
Inertia tensor
|
| 38 |
+
"""
|
| 39 |
+
h2, r2 = height**2, radius**2
|
| 40 |
+
diagonal = np.array(
|
| 41 |
+
[
|
| 42 |
+
((mass * h2) / 12) + ((mass * r2) / 4),
|
| 43 |
+
((mass * h2) / 12) + ((mass * r2) / 4),
|
| 44 |
+
(mass * r2) / 2,
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
inertia = diagonal * np.eye(3)
|
| 48 |
+
|
| 49 |
+
if transform is not None:
|
| 50 |
+
inertia = transform_inertia(transform, inertia)
|
| 51 |
+
|
| 52 |
+
return inertia
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def sphere_inertia(mass: Number, radius: Number) -> NDArray[float64]:
|
| 56 |
+
"""
|
| 57 |
+
Return the inertia tensor of a sphere.
|
| 58 |
+
|
| 59 |
+
Parameters
|
| 60 |
+
------------
|
| 61 |
+
mass : float
|
| 62 |
+
Mass of sphere
|
| 63 |
+
radius : float
|
| 64 |
+
Radius of sphere
|
| 65 |
+
|
| 66 |
+
Returns
|
| 67 |
+
------------
|
| 68 |
+
inertia : (3, 3) float
|
| 69 |
+
Inertia tensor
|
| 70 |
+
"""
|
| 71 |
+
return (2.0 / 5.0) * (radius**2) * mass * np.eye(3)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def points_inertia(
|
| 75 |
+
points: ArrayLike,
|
| 76 |
+
weights: ArrayLike | Number | None = None,
|
| 77 |
+
at_center_mass: bool = True,
|
| 78 |
+
) -> NDArray[float64]:
|
| 79 |
+
"""
|
| 80 |
+
Calculate an inertia tensor for an array of point masses
|
| 81 |
+
at the center of mass.
|
| 82 |
+
|
| 83 |
+
Parameters
|
| 84 |
+
----------
|
| 85 |
+
points : (n, 3)
|
| 86 |
+
Points in space.
|
| 87 |
+
weights : (n,) or number
|
| 88 |
+
Per-point weight to use.
|
| 89 |
+
at_center_mass
|
| 90 |
+
Calculate at the center of mass of the points, or if False
|
| 91 |
+
at the original origin.
|
| 92 |
+
|
| 93 |
+
Returns
|
| 94 |
+
-----------
|
| 95 |
+
tensor : (3, 3)
|
| 96 |
+
Inertia tensor for point masses.
|
| 97 |
+
"""
|
| 98 |
+
if weights is None:
|
| 99 |
+
# by default make the total weight 1.0 to match
|
| 100 |
+
# the default mass in other functions, and so that
|
| 101 |
+
# if a user didn't specify anything it doesn't blow
|
| 102 |
+
# up the scale depending on the number of points
|
| 103 |
+
weights = np.full(len(points), 1.0 / float(len(points)), dtype=np.float64)
|
| 104 |
+
elif isinstance(weights, (float, np.integer, int)):
|
| 105 |
+
# "is it a number" check
|
| 106 |
+
weights = np.full(len(points), float(weights), dtype=np.float64)
|
| 107 |
+
else:
|
| 108 |
+
weights = np.array(weights)
|
| 109 |
+
if len(weights) != len(points):
|
| 110 |
+
raise ValueError(
|
| 111 |
+
f"Weights must correspond to points! {len(weights)} != {len(points)}"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# make sure the points are an array of correct shape
|
| 115 |
+
points = np.asanyarray(points, dtype=np.float64)
|
| 116 |
+
if len(points.shape) != 2 or points.shape[1] != 3:
|
| 117 |
+
raise ValueError(f"Points must be `(n, 3)` not {points.shape}")
|
| 118 |
+
|
| 119 |
+
if at_center_mass:
|
| 120 |
+
# get the center of mass of the points
|
| 121 |
+
center_mass = np.average(points, weights=weights, axis=0)
|
| 122 |
+
# get the points with the origin at their center of mass
|
| 123 |
+
points_com = points - center_mass
|
| 124 |
+
else:
|
| 125 |
+
# calculate at original origin
|
| 126 |
+
points_com = points
|
| 127 |
+
|
| 128 |
+
# expand into shorthand for the expressions
|
| 129 |
+
x, y, z = points_com.T
|
| 130 |
+
x2, y2, z2 = (points_com**2).T
|
| 131 |
+
|
| 132 |
+
# calculate tensors per-point in a flattened (9, n) array
|
| 133 |
+
# from physics.stackexchange.com/questions/614094
|
| 134 |
+
tensors = np.array(
|
| 135 |
+
[y2 + z2, -x * y, -x * z, -x * y, x2 + z2, -y * z, -x * z, -y * z, x2 + y2],
|
| 136 |
+
dtype=np.float64,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# combine the weighted tensors and reshape
|
| 140 |
+
tensor = (tensors * weights).sum(axis=1).reshape((3, 3))
|
| 141 |
+
|
| 142 |
+
return tensor
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def principal_axis(inertia: ArrayLike):
|
| 146 |
+
"""
|
| 147 |
+
Find the principal components and principal axis
|
| 148 |
+
of inertia from the inertia tensor.
|
| 149 |
+
|
| 150 |
+
Parameters
|
| 151 |
+
------------
|
| 152 |
+
inertia : (3, 3) float
|
| 153 |
+
Inertia tensor
|
| 154 |
+
|
| 155 |
+
Returns
|
| 156 |
+
------------
|
| 157 |
+
components : (3,) float
|
| 158 |
+
Principal components of inertia
|
| 159 |
+
vectors : (3, 3) float
|
| 160 |
+
Row vectors pointing along the
|
| 161 |
+
principal axes of inertia
|
| 162 |
+
"""
|
| 163 |
+
inertia = np.asanyarray(inertia, dtype=np.float64)
|
| 164 |
+
if inertia.shape != (3, 3):
|
| 165 |
+
raise ValueError("inertia tensor must be (3, 3)!")
|
| 166 |
+
|
| 167 |
+
# you could any of the following to calculate this:
|
| 168 |
+
# np.linalg.svd, np.linalg.eig, np.linalg.eigh
|
| 169 |
+
# moment of inertia is square symmetric matrix
|
| 170 |
+
# eigh has the best precision in tests
|
| 171 |
+
components, vectors = np.linalg.eigh(inertia)
|
| 172 |
+
|
| 173 |
+
# eigh returns them as column vectors, change them to row vectors
|
| 174 |
+
vectors = vectors.T
|
| 175 |
+
|
| 176 |
+
return components, vectors
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def transform_inertia(
|
| 180 |
+
transform: ArrayLike,
|
| 181 |
+
inertia_tensor: ArrayLike,
|
| 182 |
+
parallel_axis: bool = False,
|
| 183 |
+
mass: Number | None = None,
|
| 184 |
+
):
|
| 185 |
+
"""
|
| 186 |
+
Transform an inertia tensor to a new frame.
|
| 187 |
+
|
| 188 |
+
Note that in trimesh `mesh.moment_inertia` is *axis aligned*
|
| 189 |
+
and at `mesh.center_mass`.
|
| 190 |
+
|
| 191 |
+
So to transform to a new frame and get the moment of inertia at
|
| 192 |
+
the center of mass the translation should be ignored and only
|
| 193 |
+
rotation applied.
|
| 194 |
+
|
| 195 |
+
If parallel axis is enabled it will compute the inertia
|
| 196 |
+
about a new location.
|
| 197 |
+
|
| 198 |
+
More details in the MIT OpenCourseWare PDF:
|
| 199 |
+
` MIT16_07F09_Lec26.pdf`
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
Parameters
|
| 203 |
+
------------
|
| 204 |
+
transform : (3, 3) or (4, 4) float
|
| 205 |
+
Transformation matrix
|
| 206 |
+
inertia_tensor : (3, 3) float
|
| 207 |
+
Inertia tensor.
|
| 208 |
+
parallel_axis : bool
|
| 209 |
+
Apply the parallel axis theorum or not.
|
| 210 |
+
If the passed inertia tensor is at the center of mass
|
| 211 |
+
and you want the new post-transform tensor also at the
|
| 212 |
+
center of mass you DON'T want this enabled as you *only*
|
| 213 |
+
want to apply the rotation. Use this to get moment of
|
| 214 |
+
inertia at an arbitrary frame that isn't the center of mass.
|
| 215 |
+
|
| 216 |
+
Returns
|
| 217 |
+
------------
|
| 218 |
+
transformed : (3, 3) float
|
| 219 |
+
Inertia tensor in new frame.
|
| 220 |
+
"""
|
| 221 |
+
# check inputs and extract rotation
|
| 222 |
+
transform = np.asanyarray(transform, dtype=np.float64)
|
| 223 |
+
if transform.shape == (4, 4):
|
| 224 |
+
rotation = transform[:3, :3]
|
| 225 |
+
elif transform.shape == (3, 3):
|
| 226 |
+
rotation = transform
|
| 227 |
+
else:
|
| 228 |
+
raise ValueError("transform must be (3, 3) or (4, 4)!")
|
| 229 |
+
|
| 230 |
+
inertia_tensor = np.asanyarray(inertia_tensor, dtype=np.float64)
|
| 231 |
+
if inertia_tensor.shape != (3, 3):
|
| 232 |
+
raise ValueError("inertia_tensor must be (3, 3)!")
|
| 233 |
+
|
| 234 |
+
if parallel_axis:
|
| 235 |
+
if transform.shape == (3, 3):
|
| 236 |
+
# shorthand for "translation"
|
| 237 |
+
a = np.zeros(3, dtype=np.float64)
|
| 238 |
+
else:
|
| 239 |
+
# get the translation
|
| 240 |
+
a = transform[:3, 3]
|
| 241 |
+
# First the changed origin of the new transform is taken into
|
| 242 |
+
# account. To calculate the inertia tensor
|
| 243 |
+
# the parallel axis theorem is used
|
| 244 |
+
M = np.array(
|
| 245 |
+
[
|
| 246 |
+
[a[1] ** 2 + a[2] ** 2, -a[0] * a[1], -a[0] * a[2]],
|
| 247 |
+
[-a[0] * a[1], a[0] ** 2 + a[2] ** 2, -a[1] * a[2]],
|
| 248 |
+
[-a[0] * a[2], -a[1] * a[2], a[0] ** 2 + a[1] ** 2],
|
| 249 |
+
]
|
| 250 |
+
)
|
| 251 |
+
aligned_inertia = inertia_tensor + mass * M
|
| 252 |
+
|
| 253 |
+
return multi_dot([rotation.T, aligned_inertia, rotation])
|
| 254 |
+
|
| 255 |
+
return multi_dot([rotation, inertia_tensor, rotation.T])
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def radial_symmetry(mesh):
|
| 259 |
+
"""
|
| 260 |
+
Check whether a mesh has radial symmetry.
|
| 261 |
+
|
| 262 |
+
Returns
|
| 263 |
+
-----------
|
| 264 |
+
symmetry : None or str
|
| 265 |
+
None No rotational symmetry
|
| 266 |
+
'radial' Symmetric around an axis
|
| 267 |
+
'spherical' Symmetric around a point
|
| 268 |
+
axis : None or (3,) float
|
| 269 |
+
Rotation axis or point
|
| 270 |
+
section : None or (3, 2) float
|
| 271 |
+
If radial symmetry provide vectors
|
| 272 |
+
to get cross section
|
| 273 |
+
"""
|
| 274 |
+
|
| 275 |
+
# shortcuts to avoid typing and hitting cache
|
| 276 |
+
scalar = mesh.principal_inertia_components.copy()
|
| 277 |
+
|
| 278 |
+
# exit early if inertia components are all zero
|
| 279 |
+
if (scalar < 1e-30).any():
|
| 280 |
+
return None, None, None
|
| 281 |
+
|
| 282 |
+
# normalize the PCI so we can compare them
|
| 283 |
+
scalar = scalar / np.linalg.norm(scalar)
|
| 284 |
+
vector = mesh.principal_inertia_vectors
|
| 285 |
+
# the sorted order of the principal components
|
| 286 |
+
order = scalar.argsort()
|
| 287 |
+
|
| 288 |
+
# we are checking if a geometry has radial symmetry
|
| 289 |
+
# if 2 of the PCI are equal, it is a revolved 2D profile
|
| 290 |
+
# if 3 of the PCI (all of them) are equal it is a sphere
|
| 291 |
+
diff = np.abs(np.diff(scalar[order]))
|
| 292 |
+
# diffs that are within tol of zero
|
| 293 |
+
diff_zero = diff < 1e-4
|
| 294 |
+
|
| 295 |
+
if diff_zero.all():
|
| 296 |
+
# this is the case where all 3 PCI are identical
|
| 297 |
+
# this means that the geometry is symmetric about a point
|
| 298 |
+
# examples of this are a sphere, icosahedron, etc
|
| 299 |
+
axis = vector[0]
|
| 300 |
+
section = vector[1:]
|
| 301 |
+
|
| 302 |
+
return "spherical", axis, section
|
| 303 |
+
|
| 304 |
+
elif diff_zero.any():
|
| 305 |
+
# this is the case for 2/3 PCI are identical
|
| 306 |
+
# this means the geometry is symmetric about an axis
|
| 307 |
+
# probably a revolved 2D profile
|
| 308 |
+
|
| 309 |
+
# we know that only 1/2 of the diff values are True
|
| 310 |
+
# if the first diff is 0, it means if we take the first element
|
| 311 |
+
# in the ordered PCI we will have one of the non- revolve axis
|
| 312 |
+
# if the second diff is 0, we take the last element of
|
| 313 |
+
# the ordered PCI for the section axis
|
| 314 |
+
# if we wanted the revolve axis we would just switch [0,-1] to
|
| 315 |
+
# [-1,0]
|
| 316 |
+
|
| 317 |
+
# since two vectors are the same, we know the middle
|
| 318 |
+
# one is one of those two
|
| 319 |
+
section_index = order[np.array([[0, 1], [1, -1]])[diff_zero]].flatten()
|
| 320 |
+
section = vector[section_index]
|
| 321 |
+
|
| 322 |
+
# we know the rotation axis is the sole unique value
|
| 323 |
+
# and is either first or last of the sorted values
|
| 324 |
+
axis_index = order[np.array([-1, 0])[diff_zero]][0]
|
| 325 |
+
axis = vector[axis_index]
|
| 326 |
+
return "radial", axis, section
|
| 327 |
+
|
| 328 |
+
return None, None, None
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def scene_inertia(scene, transform: ArrayLike | None = None) -> NDArray[float64]:
|
| 332 |
+
"""
|
| 333 |
+
Calculate the inertia of a scene about a specific frame.
|
| 334 |
+
|
| 335 |
+
Parameters
|
| 336 |
+
------------
|
| 337 |
+
scene : trimesh.Scene
|
| 338 |
+
Scene with geometry.
|
| 339 |
+
transform : None or (4, 4) float
|
| 340 |
+
Homogeneous transform to compute inertia at.
|
| 341 |
+
|
| 342 |
+
Returns
|
| 343 |
+
----------
|
| 344 |
+
moment : (3, 3)
|
| 345 |
+
Inertia tensor about requested frame
|
| 346 |
+
"""
|
| 347 |
+
# shortcuts for tight loop
|
| 348 |
+
graph = scene.graph
|
| 349 |
+
geoms = scene.geometry
|
| 350 |
+
|
| 351 |
+
# get the matrix ang geometry name for
|
| 352 |
+
nodes = [graph[n] for n in graph.nodes_geometry]
|
| 353 |
+
# get the moment of inertia with the mesh moved to a location
|
| 354 |
+
moments = np.array(
|
| 355 |
+
[
|
| 356 |
+
geoms[g].moment_inertia_frame(np.dot(np.linalg.inv(mat), transform))
|
| 357 |
+
for mat, g in nodes
|
| 358 |
+
if hasattr(geoms[g], "moment_inertia_frame")
|
| 359 |
+
],
|
| 360 |
+
dtype=np.float64,
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
return moments.sum(axis=0)
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from . import blender
|
| 2 |
+
|
| 3 |
+
__all__ = ["blender"]
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (329 Bytes). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__pycache__/blender.cpython-311.pyc
ADDED
|
Binary file (6.93 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/__pycache__/generic.cpython-311.pyc
ADDED
|
Binary file (6.5 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/blender.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import platform
|
| 3 |
+
|
| 4 |
+
from .. import util
|
| 5 |
+
from ..constants import log
|
| 6 |
+
from ..resources import get_string
|
| 7 |
+
from ..typed import BooleanOperationType, Iterable
|
| 8 |
+
from .generic import MeshScript
|
| 9 |
+
|
| 10 |
+
if platform.system() == "Windows":
|
| 11 |
+
# try to find Blender install on Windows
|
| 12 |
+
# split existing path by delimiter
|
| 13 |
+
_search_path = [i for i in os.environ.get("PATH", "").split(";") if len(i) > 0]
|
| 14 |
+
for pf in [r"C:\Program Files", r"C:\Program Files (x86)"]:
|
| 15 |
+
pf = os.path.join(pf, "Blender Foundation")
|
| 16 |
+
if os.path.exists(pf):
|
| 17 |
+
for p in os.listdir(pf):
|
| 18 |
+
if "Blender" in p:
|
| 19 |
+
_search_path.append(os.path.join(pf, p))
|
| 20 |
+
_search_path = ";".join(set(_search_path))
|
| 21 |
+
log.debug("searching for blender in: %s", _search_path)
|
| 22 |
+
elif platform.system() == "Darwin":
|
| 23 |
+
# try to find Blender on Mac OSX
|
| 24 |
+
_search_path = [i for i in os.environ.get("PATH", "").split(":") if len(i) > 0]
|
| 25 |
+
_search_path.extend(
|
| 26 |
+
[
|
| 27 |
+
"/Applications/blender.app/Contents/MacOS",
|
| 28 |
+
"/Applications/Blender.app/Contents/MacOS",
|
| 29 |
+
"/Applications/Blender/blender.app/Contents/MacOS",
|
| 30 |
+
]
|
| 31 |
+
)
|
| 32 |
+
_search_path = ":".join(set(_search_path))
|
| 33 |
+
log.debug("searching for blender in: %s", _search_path)
|
| 34 |
+
else:
|
| 35 |
+
_search_path = os.environ.get("PATH", "")
|
| 36 |
+
|
| 37 |
+
_blender_executable = util.which("blender", path=_search_path)
|
| 38 |
+
exists = _blender_executable is not None
|
| 39 |
+
|
| 40 |
+
# a map that translates:
|
| 41 |
+
# `trimesh.BooleanOperationType` -> blender value
|
| 42 |
+
_blender_bool = {
|
| 43 |
+
"union": "UNION",
|
| 44 |
+
"difference": "DIFFERENCE",
|
| 45 |
+
"intersection": "INTERSECT",
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def boolean(
|
| 50 |
+
meshes: Iterable,
|
| 51 |
+
operation: BooleanOperationType = "difference",
|
| 52 |
+
use_exact: bool = True,
|
| 53 |
+
use_self: bool = False,
|
| 54 |
+
debug: bool = False,
|
| 55 |
+
check_volume: bool = True,
|
| 56 |
+
):
|
| 57 |
+
"""
|
| 58 |
+
Run a boolean operation with multiple meshes using Blender.
|
| 59 |
+
|
| 60 |
+
Parameters
|
| 61 |
+
-----------
|
| 62 |
+
meshes
|
| 63 |
+
List of mesh objects to be operated on
|
| 64 |
+
operation
|
| 65 |
+
Type of boolean operation ("difference", "union", "intersect").
|
| 66 |
+
use_exact
|
| 67 |
+
Use the "exact" mode as opposed to the "fast" mode.
|
| 68 |
+
use_self
|
| 69 |
+
Whether to consider self-intersections.
|
| 70 |
+
debug
|
| 71 |
+
Provide additional output for troubleshooting.
|
| 72 |
+
check_volume
|
| 73 |
+
Raise an error if not all meshes are watertight
|
| 74 |
+
positive volumes. Advanced users may want to ignore
|
| 75 |
+
this check as it is expensive.
|
| 76 |
+
|
| 77 |
+
Returns
|
| 78 |
+
----------
|
| 79 |
+
result
|
| 80 |
+
The result of the boolean operation on the provided meshes.
|
| 81 |
+
"""
|
| 82 |
+
if not exists:
|
| 83 |
+
raise ValueError("No blender available!")
|
| 84 |
+
if check_volume and not all(m.is_volume for m in meshes):
|
| 85 |
+
raise ValueError("Not all meshes are volumes!")
|
| 86 |
+
|
| 87 |
+
# conversions from the trimesh `BooleanOperationType` to the blender option
|
| 88 |
+
key = operation.lower().strip()
|
| 89 |
+
if key not in _blender_bool:
|
| 90 |
+
raise ValueError(
|
| 91 |
+
f"`{operation}` is not a valid boolean: `{_blender_bool.keys()}`"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if use_exact:
|
| 95 |
+
solver_options = "EXACT"
|
| 96 |
+
else:
|
| 97 |
+
solver_options = "FAST"
|
| 98 |
+
|
| 99 |
+
# get the template from our resources folder
|
| 100 |
+
template = get_string("templates/blender_boolean.py.tmpl")
|
| 101 |
+
# use string substitutions rather than `string.Template` as we aren't going
|
| 102 |
+
# to be filling in all the values here, `MeshScript` is going to be
|
| 103 |
+
# the source of `$MESH_PRE`, etc.
|
| 104 |
+
script = (
|
| 105 |
+
template.replace("$OPERATION", _blender_bool[key])
|
| 106 |
+
.replace("$SOLVER_OPTIONS", solver_options)
|
| 107 |
+
.replace("$USE_SELF", f"{use_self}")
|
| 108 |
+
)
|
| 109 |
+
with MeshScript(meshes=meshes, script=script, debug=debug) as blend:
|
| 110 |
+
result = blend.run(_blender_executable + " --background --python $SCRIPT")
|
| 111 |
+
|
| 112 |
+
result = util.make_sequence(result)
|
| 113 |
+
for m in result:
|
| 114 |
+
# blender returns actively incorrect face normals
|
| 115 |
+
m.face_normals = None
|
| 116 |
+
|
| 117 |
+
return util.concatenate(result)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def unwrap(
|
| 121 |
+
mesh, angle_limit: float = 66.0, island_margin: float = 0.0, debug: bool = False
|
| 122 |
+
):
|
| 123 |
+
"""
|
| 124 |
+
Run an unwrap operation using blender.
|
| 125 |
+
"""
|
| 126 |
+
if not exists:
|
| 127 |
+
raise ValueError("No blender available!")
|
| 128 |
+
|
| 129 |
+
# get the template from our resources folder
|
| 130 |
+
template = get_string("templates/blender_unwrap.py.template")
|
| 131 |
+
script = template.replace("$ANGLE_LIMIT", f"{angle_limit:.6f}").replace(
|
| 132 |
+
"$ISLAND_MARGIN", f"{island_margin:.6f}"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
with MeshScript(meshes=[mesh], script=script, exchange="obj", debug=debug) as blend:
|
| 136 |
+
result = blend.run(_blender_executable + " --background --python $SCRIPT")
|
| 137 |
+
|
| 138 |
+
for m in util.make_sequence(result):
|
| 139 |
+
# blender returns actively incorrect face normals
|
| 140 |
+
m.face_normals = None
|
| 141 |
+
|
| 142 |
+
return result
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interfaces/generic.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import platform
|
| 3 |
+
import subprocess
|
| 4 |
+
from string import Template
|
| 5 |
+
from subprocess import CalledProcessError, check_output
|
| 6 |
+
from tempfile import NamedTemporaryFile
|
| 7 |
+
|
| 8 |
+
from .. import exchange
|
| 9 |
+
from ..util import log
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class MeshScript:
|
| 13 |
+
def __init__(self, meshes, script, exchange="stl", debug=False, **kwargs):
|
| 14 |
+
self.debug = debug
|
| 15 |
+
self.kwargs = kwargs
|
| 16 |
+
self.meshes = meshes
|
| 17 |
+
self.script = script
|
| 18 |
+
self.exchange = exchange
|
| 19 |
+
|
| 20 |
+
def __enter__(self):
|
| 21 |
+
# windows has problems with multiple programs using open files so we close
|
| 22 |
+
# them at the end of the enter call, and delete them ourselves at exit
|
| 23 |
+
# Blender sorts its objects alphabetically
|
| 24 |
+
# so prefix the mesh number on the file name
|
| 25 |
+
digit_count = len(str(len(self.meshes)))
|
| 26 |
+
self.mesh_pre = [
|
| 27 |
+
NamedTemporaryFile(
|
| 28 |
+
suffix=f".{self.exchange}",
|
| 29 |
+
prefix=f"{str(i).zfill(digit_count)}_",
|
| 30 |
+
mode="wb",
|
| 31 |
+
delete=False,
|
| 32 |
+
)
|
| 33 |
+
for i in range(len(self.meshes))
|
| 34 |
+
]
|
| 35 |
+
self.mesh_post = NamedTemporaryFile(
|
| 36 |
+
suffix=f".{self.exchange}", mode="rb", delete=False
|
| 37 |
+
)
|
| 38 |
+
self.script_out = NamedTemporaryFile(mode="wb", delete=False)
|
| 39 |
+
|
| 40 |
+
# export the meshes to a temporary STL container
|
| 41 |
+
for mesh, file_obj in zip(self.meshes, self.mesh_pre):
|
| 42 |
+
mesh.export(file_obj=file_obj.name)
|
| 43 |
+
|
| 44 |
+
self.replacement = {"MESH_" + str(i): m.name for i, m in enumerate(self.mesh_pre)}
|
| 45 |
+
self.replacement["MESH_PRE"] = str([i.name for i in self.mesh_pre])
|
| 46 |
+
self.replacement["MESH_POST"] = self.mesh_post.name
|
| 47 |
+
self.replacement["SCRIPT"] = self.script_out.name
|
| 48 |
+
|
| 49 |
+
script_text = Template(self.script).substitute(self.replacement)
|
| 50 |
+
if platform.system() == "Windows":
|
| 51 |
+
script_text = script_text.replace("\\", "\\\\")
|
| 52 |
+
self.script_out.write(script_text.encode("utf-8"))
|
| 53 |
+
|
| 54 |
+
# close all temporary files
|
| 55 |
+
self.script_out.close()
|
| 56 |
+
self.mesh_post.close()
|
| 57 |
+
for file_obj in self.mesh_pre:
|
| 58 |
+
file_obj.close()
|
| 59 |
+
return self
|
| 60 |
+
|
| 61 |
+
def run(self, command):
|
| 62 |
+
# substitute per-token so a replacement containing whitespace
|
| 63 |
+
# stays a single argv entry
|
| 64 |
+
command_run = [
|
| 65 |
+
Template(part).substitute(self.replacement) for part in command.split()
|
| 66 |
+
]
|
| 67 |
+
# run the binary
|
| 68 |
+
startupinfo = None
|
| 69 |
+
if platform.system() == "Windows":
|
| 70 |
+
startupinfo = subprocess.STARTUPINFO()
|
| 71 |
+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
| 72 |
+
|
| 73 |
+
if self.debug:
|
| 74 |
+
log.info("executing: {}".format(" ".join(command_run)))
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
output = check_output(
|
| 78 |
+
command_run, stderr=subprocess.STDOUT, startupinfo=startupinfo
|
| 79 |
+
)
|
| 80 |
+
except CalledProcessError as E:
|
| 81 |
+
# raise with the output from the process
|
| 82 |
+
raise RuntimeError(E.output.decode())
|
| 83 |
+
|
| 84 |
+
if self.debug:
|
| 85 |
+
log.info(output.decode())
|
| 86 |
+
|
| 87 |
+
# bring the binaries result back as a set of Trimesh kwargs
|
| 88 |
+
mesh_results = exchange.load.load_mesh(self.mesh_post.name, **self.kwargs)
|
| 89 |
+
|
| 90 |
+
return mesh_results
|
| 91 |
+
|
| 92 |
+
def __exit__(self, *args, **kwargs):
|
| 93 |
+
if self.debug:
|
| 94 |
+
log.info(f"MeshScript.debug: not deleting {self.script_out.name}")
|
| 95 |
+
return
|
| 96 |
+
# delete all the temporary files by name
|
| 97 |
+
# they are closed but their names are still available
|
| 98 |
+
os.remove(self.script_out.name)
|
| 99 |
+
for file_obj in self.mesh_pre:
|
| 100 |
+
os.remove(file_obj.name)
|
| 101 |
+
os.remove(self.mesh_post.name)
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/intersections.py
ADDED
|
@@ -0,0 +1,805 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
intersections.py
|
| 3 |
+
------------------
|
| 4 |
+
|
| 5 |
+
Primarily mesh-plane intersections (slicing).
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from . import geometry, grouping, util
|
| 11 |
+
from . import transformations as tf
|
| 12 |
+
from . import triangles as tm
|
| 13 |
+
from .constants import tol
|
| 14 |
+
from .triangles import points_to_barycentric
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def mesh_plane(
|
| 18 |
+
mesh,
|
| 19 |
+
plane_normal,
|
| 20 |
+
plane_origin,
|
| 21 |
+
return_faces=False,
|
| 22 |
+
local_faces=None,
|
| 23 |
+
cached_dots=None,
|
| 24 |
+
):
|
| 25 |
+
"""
|
| 26 |
+
Find a the intersections between a mesh and a plane,
|
| 27 |
+
returning a set of line segments on that plane.
|
| 28 |
+
|
| 29 |
+
Parameters
|
| 30 |
+
---------
|
| 31 |
+
mesh : Trimesh object
|
| 32 |
+
Source mesh to slice
|
| 33 |
+
plane_normal : (3,) float
|
| 34 |
+
Normal vector of plane to intersect with mesh
|
| 35 |
+
plane_origin : (3,) float
|
| 36 |
+
Point on plane to intersect with mesh
|
| 37 |
+
return_faces : bool
|
| 38 |
+
If True return face index each line is from
|
| 39 |
+
local_faces : None or (m,) int
|
| 40 |
+
Limit section to just these faces.
|
| 41 |
+
cached_dots : (n, 3) float
|
| 42 |
+
If an external function has stored dot
|
| 43 |
+
products pass them here to avoid recomputing.
|
| 44 |
+
|
| 45 |
+
Returns
|
| 46 |
+
----------
|
| 47 |
+
lines : (m, 2, 3) float
|
| 48 |
+
List of 3D line segments in space.
|
| 49 |
+
face_index : (m,) int
|
| 50 |
+
Index of mesh.faces for each line
|
| 51 |
+
Only returned if return_faces was True
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
def triangle_cases(signs):
|
| 55 |
+
"""
|
| 56 |
+
Figure out which faces correspond to which intersection
|
| 57 |
+
case from the signs of the dot product of each vertex.
|
| 58 |
+
Does this by bitbang each row of signs into an 8 bit
|
| 59 |
+
integer.
|
| 60 |
+
|
| 61 |
+
code : signs : intersects
|
| 62 |
+
0 : [-1 -1 -1] : No
|
| 63 |
+
2 : [-1 -1 0] : No
|
| 64 |
+
4 : [-1 -1 1] : Yes; 2 on one side, 1 on the other
|
| 65 |
+
6 : [-1 0 0] : Yes; one edge fully on plane
|
| 66 |
+
8 : [-1 0 1] : Yes; one vertex on plane 2 on different sides
|
| 67 |
+
12 : [-1 1 1] : Yes; 2 on one side, 1 on the other
|
| 68 |
+
14 : [0 0 0] : No (on plane fully)
|
| 69 |
+
16 : [0 0 1] : Yes; one edge fully on plane
|
| 70 |
+
20 : [0 1 1] : No
|
| 71 |
+
28 : [1 1 1] : No
|
| 72 |
+
|
| 73 |
+
Parameters
|
| 74 |
+
----------
|
| 75 |
+
signs: (n,3) int, all values are -1,0, or 1
|
| 76 |
+
Each row contains the dot product of all three vertices
|
| 77 |
+
in a face with respect to the plane
|
| 78 |
+
|
| 79 |
+
Returns
|
| 80 |
+
---------
|
| 81 |
+
basic : (n,) bool
|
| 82 |
+
Which faces are in the basic intersection case
|
| 83 |
+
one_vertex : (n,) bool
|
| 84 |
+
Which faces are in the one vertex case
|
| 85 |
+
one_edge : (n,) bool
|
| 86 |
+
Which faces are in the one edge case
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
signs_sorted = np.sort(signs, axis=1)
|
| 90 |
+
coded = np.zeros(len(signs_sorted), dtype=np.int8) + 14
|
| 91 |
+
for i in range(3):
|
| 92 |
+
coded += signs_sorted[:, i] << 3 - i
|
| 93 |
+
|
| 94 |
+
# one edge fully on the plane
|
| 95 |
+
# note that we are only accepting *one* of the on- edge cases,
|
| 96 |
+
# where the other vertex has a positive dot product (16) instead
|
| 97 |
+
# of both on- edge cases ([6, 16])
|
| 98 |
+
# this is so that for regions that are co-planar with the the section plane
|
| 99 |
+
# we don't end up with an invalid boundary
|
| 100 |
+
key = np.zeros(29, dtype=bool)
|
| 101 |
+
key[16] = True
|
| 102 |
+
one_edge = key[coded]
|
| 103 |
+
|
| 104 |
+
# one vertex on plane, other two on different sides
|
| 105 |
+
key[:] = False
|
| 106 |
+
key[8] = True
|
| 107 |
+
one_vertex = key[coded]
|
| 108 |
+
|
| 109 |
+
# one vertex on one side of the plane, two on the other
|
| 110 |
+
key[:] = False
|
| 111 |
+
key[[4, 12]] = True
|
| 112 |
+
basic = key[coded]
|
| 113 |
+
|
| 114 |
+
return basic, one_vertex, one_edge
|
| 115 |
+
|
| 116 |
+
def handle_on_vertex(signs, faces, vertices):
|
| 117 |
+
# case where one vertex is on plane
|
| 118 |
+
# and two are on different sides
|
| 119 |
+
vertex_plane = faces[signs == 0]
|
| 120 |
+
edge_thru = faces[signs != 0].reshape((-1, 2))
|
| 121 |
+
point_intersect, valid = plane_lines(
|
| 122 |
+
plane_origin, plane_normal, vertices[edge_thru.T], line_segments=False
|
| 123 |
+
)
|
| 124 |
+
lines = np.column_stack((vertices[vertex_plane[valid]], point_intersect)).reshape(
|
| 125 |
+
(-1, 2, 3)
|
| 126 |
+
)
|
| 127 |
+
return lines
|
| 128 |
+
|
| 129 |
+
def handle_on_edge(signs, faces, vertices):
|
| 130 |
+
# case where two vertices are on the plane and one is off
|
| 131 |
+
edges = faces[signs == 0].reshape((-1, 2))
|
| 132 |
+
points = vertices[edges]
|
| 133 |
+
return points
|
| 134 |
+
|
| 135 |
+
def handle_basic(signs, faces, vertices):
|
| 136 |
+
# case where one vertex is on one side and two are on the other
|
| 137 |
+
unique_element = grouping.unique_value_in_row(signs, unique=[-1, 1])
|
| 138 |
+
edges = np.column_stack(
|
| 139 |
+
(
|
| 140 |
+
faces[unique_element],
|
| 141 |
+
faces[np.roll(unique_element, 1, axis=1)],
|
| 142 |
+
faces[unique_element],
|
| 143 |
+
faces[np.roll(unique_element, 2, axis=1)],
|
| 144 |
+
)
|
| 145 |
+
).reshape((-1, 2))
|
| 146 |
+
intersections, valid = plane_lines(
|
| 147 |
+
plane_origin, plane_normal, vertices[edges.T], line_segments=False
|
| 148 |
+
)
|
| 149 |
+
# since the data has been pre- culled, any invalid intersections at all
|
| 150 |
+
# means the culling was done incorrectly and thus things are broken
|
| 151 |
+
assert valid.all()
|
| 152 |
+
return intersections.reshape((-1, 2, 3))
|
| 153 |
+
|
| 154 |
+
# check input plane
|
| 155 |
+
plane_normal = np.asanyarray(plane_normal, dtype=np.float64)
|
| 156 |
+
plane_origin = np.asanyarray(plane_origin, dtype=np.float64)
|
| 157 |
+
if plane_origin.shape != (3,) or plane_normal.shape != (3,):
|
| 158 |
+
raise ValueError("Plane origin and normal must be (3,)!")
|
| 159 |
+
|
| 160 |
+
if local_faces is None:
|
| 161 |
+
# do a cross section against all faces
|
| 162 |
+
faces = mesh.faces
|
| 163 |
+
else:
|
| 164 |
+
local_faces = np.asanyarray(local_faces, dtype=np.int64)
|
| 165 |
+
# only take the subset of faces if passed
|
| 166 |
+
faces = mesh.faces[local_faces]
|
| 167 |
+
|
| 168 |
+
if cached_dots is not None:
|
| 169 |
+
dots = cached_dots
|
| 170 |
+
else:
|
| 171 |
+
# dot product of each vertex with the plane normal indexed by face
|
| 172 |
+
# so for each face the dot product of each vertex is a row
|
| 173 |
+
# shape is the same as mesh.faces (n,3)
|
| 174 |
+
dots = np.dot(mesh.vertices - plane_origin, plane_normal)
|
| 175 |
+
|
| 176 |
+
# sign of the dot product is -1, 0, or 1
|
| 177 |
+
# shape is the same as mesh.faces (n,3)
|
| 178 |
+
signs = np.zeros(len(mesh.vertices), dtype=np.int8)
|
| 179 |
+
signs[dots < -tol.merge] = -1
|
| 180 |
+
signs[dots > tol.merge] = 1
|
| 181 |
+
signs = signs[faces]
|
| 182 |
+
|
| 183 |
+
# figure out which triangles are in the cross section,
|
| 184 |
+
# and which of the three intersection cases they are in
|
| 185 |
+
cases = triangle_cases(signs)
|
| 186 |
+
# handlers for each case
|
| 187 |
+
handlers = (handle_basic, handle_on_vertex, handle_on_edge)
|
| 188 |
+
|
| 189 |
+
# the (m, 2, 3) line segments
|
| 190 |
+
lines = np.vstack(
|
| 191 |
+
[h(signs[c], faces[c], mesh.vertices) for c, h in zip(cases, handlers)]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
if return_faces:
|
| 195 |
+
# everything that hit something
|
| 196 |
+
index = np.hstack([np.nonzero(c)[0] for c in cases])
|
| 197 |
+
assert index.dtype.kind == "i"
|
| 198 |
+
if local_faces is None:
|
| 199 |
+
return lines, index
|
| 200 |
+
# we are considering a subset of faces
|
| 201 |
+
# so we need to take the indexes from original
|
| 202 |
+
return lines, local_faces[index]
|
| 203 |
+
return lines
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def mesh_multiplane(mesh, plane_origin, plane_normal, heights):
|
| 207 |
+
"""
|
| 208 |
+
A utility function for slicing a mesh by multiple
|
| 209 |
+
parallel planes which caches the dot product operation.
|
| 210 |
+
|
| 211 |
+
Parameters
|
| 212 |
+
-------------
|
| 213 |
+
mesh : trimesh.Trimesh
|
| 214 |
+
Geometry to be sliced by planes
|
| 215 |
+
plane_origin : (3,) float
|
| 216 |
+
Point on a plane
|
| 217 |
+
plane_normal : (3,) float
|
| 218 |
+
Normal vector of plane
|
| 219 |
+
heights : (m,) float
|
| 220 |
+
Offset distances from plane to slice at:
|
| 221 |
+
at `height=0` it will be exactly on the passed plane.
|
| 222 |
+
|
| 223 |
+
Returns
|
| 224 |
+
--------------
|
| 225 |
+
lines : (m,) sequence of (n, 2, 2) float
|
| 226 |
+
Lines in space for m planes
|
| 227 |
+
to_3D : (m, 4, 4) float
|
| 228 |
+
Transform to move each section back to 3D
|
| 229 |
+
face_index : (m,) sequence of (n,) int
|
| 230 |
+
Indexes of mesh.faces for each segment
|
| 231 |
+
"""
|
| 232 |
+
# check input plane
|
| 233 |
+
plane_normal = util.unitize(plane_normal)
|
| 234 |
+
plane_origin = np.asanyarray(plane_origin, dtype=np.float64)
|
| 235 |
+
heights = np.asanyarray(heights, dtype=np.float64)
|
| 236 |
+
|
| 237 |
+
# dot product of every vertex with plane
|
| 238 |
+
vertex_dots = np.dot(plane_normal, (mesh.vertices - plane_origin).T)
|
| 239 |
+
|
| 240 |
+
# reconstruct transforms for each 2D section
|
| 241 |
+
base_transform = geometry.plane_transform(origin=plane_origin, normal=plane_normal)
|
| 242 |
+
base_transform = np.linalg.inv(base_transform)
|
| 243 |
+
|
| 244 |
+
# alter translation Z inside loop
|
| 245 |
+
translation = np.eye(4)
|
| 246 |
+
|
| 247 |
+
# store results
|
| 248 |
+
transforms = []
|
| 249 |
+
face_index = []
|
| 250 |
+
segments = []
|
| 251 |
+
|
| 252 |
+
# loop through user specified heights
|
| 253 |
+
for height in heights:
|
| 254 |
+
# offset the origin by the height
|
| 255 |
+
new_origin = plane_origin + (plane_normal * height)
|
| 256 |
+
# offset the dot products by height and index by faces
|
| 257 |
+
new_dots = vertex_dots - height
|
| 258 |
+
# run the intersection with the cached dot products
|
| 259 |
+
lines, index = mesh_plane(
|
| 260 |
+
mesh=mesh,
|
| 261 |
+
plane_origin=new_origin,
|
| 262 |
+
plane_normal=plane_normal,
|
| 263 |
+
return_faces=True,
|
| 264 |
+
cached_dots=new_dots,
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
# get the transforms to 3D space and back
|
| 268 |
+
translation[2, 3] = height
|
| 269 |
+
to_3D = np.dot(base_transform, translation)
|
| 270 |
+
to_2D = np.linalg.inv(to_3D)
|
| 271 |
+
transforms.append(to_3D)
|
| 272 |
+
|
| 273 |
+
# transform points to 2D frame
|
| 274 |
+
lines_2D = tf.transform_points(lines.reshape((-1, 3)), to_2D)
|
| 275 |
+
|
| 276 |
+
# if we didn't screw up the transform all
|
| 277 |
+
# of the Z values should be zero
|
| 278 |
+
# assert np.allclose(lines_2D[:, 2], 0.0)
|
| 279 |
+
|
| 280 |
+
# reshape back in to lines and discard Z
|
| 281 |
+
lines_2D = lines_2D[:, :2].reshape((-1, 2, 2))
|
| 282 |
+
# store (n, 2, 2) float lines
|
| 283 |
+
segments.append(lines_2D)
|
| 284 |
+
# store (n,) int indexes of mesh.faces
|
| 285 |
+
face_index.append(index)
|
| 286 |
+
|
| 287 |
+
# (n, 4, 4) transforms from 2D to 3D
|
| 288 |
+
transforms = np.array(transforms, dtype=np.float64)
|
| 289 |
+
|
| 290 |
+
return segments, transforms, face_index
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def plane_lines(plane_origin, plane_normal, endpoints, line_segments=True):
|
| 294 |
+
"""
|
| 295 |
+
Calculate plane-line intersections
|
| 296 |
+
|
| 297 |
+
Parameters
|
| 298 |
+
---------
|
| 299 |
+
plane_origin : (3,) float
|
| 300 |
+
Point on plane
|
| 301 |
+
plane_normal : (3,) float
|
| 302 |
+
Plane normal vector
|
| 303 |
+
endpoints : (2, n, 3) float
|
| 304 |
+
Points defining lines to be tested
|
| 305 |
+
line_segments : bool
|
| 306 |
+
If True, only returns intersections as valid if
|
| 307 |
+
vertices from endpoints are on different sides
|
| 308 |
+
of the plane.
|
| 309 |
+
|
| 310 |
+
Returns
|
| 311 |
+
---------
|
| 312 |
+
intersections : (m, 3) float
|
| 313 |
+
Cartesian intersection points
|
| 314 |
+
valid : (n, 3) bool
|
| 315 |
+
Indicate whether a valid intersection exists
|
| 316 |
+
for each input line segment
|
| 317 |
+
"""
|
| 318 |
+
endpoints = np.asanyarray(endpoints)
|
| 319 |
+
plane_origin = np.asanyarray(plane_origin).reshape(3)
|
| 320 |
+
line_dir = util.unitize(endpoints[1] - endpoints[0])
|
| 321 |
+
plane_normal = util.unitize(np.asanyarray(plane_normal).reshape(3))
|
| 322 |
+
|
| 323 |
+
t = np.dot(plane_normal, (plane_origin - endpoints[0]).T)
|
| 324 |
+
b = np.dot(plane_normal, line_dir.T)
|
| 325 |
+
|
| 326 |
+
# If the plane normal and line direction are perpendicular, it means
|
| 327 |
+
# the vector is 'on plane', and there isn't a valid intersection.
|
| 328 |
+
# We discard on-plane vectors by checking that the dot product is nonzero
|
| 329 |
+
valid = np.abs(b) > tol.zero
|
| 330 |
+
if line_segments:
|
| 331 |
+
test = np.dot(plane_normal, np.transpose(plane_origin - endpoints[1]))
|
| 332 |
+
different_sides = np.sign(t) != np.sign(test)
|
| 333 |
+
nonzero = np.logical_or(np.abs(t) > tol.zero, np.abs(test) > tol.zero)
|
| 334 |
+
valid = np.logical_and(valid, different_sides)
|
| 335 |
+
valid = np.logical_and(valid, nonzero)
|
| 336 |
+
|
| 337 |
+
d = np.divide(t[valid], b[valid])
|
| 338 |
+
intersection = endpoints[0][valid]
|
| 339 |
+
intersection = intersection + np.reshape(d, (-1, 1)) * line_dir[valid]
|
| 340 |
+
|
| 341 |
+
return intersection, valid
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
def planes_lines(
|
| 345 |
+
plane_origins,
|
| 346 |
+
plane_normals,
|
| 347 |
+
line_origins,
|
| 348 |
+
line_directions,
|
| 349 |
+
return_distance=False,
|
| 350 |
+
return_denom=False,
|
| 351 |
+
):
|
| 352 |
+
"""
|
| 353 |
+
Given one line per plane find the intersection points.
|
| 354 |
+
|
| 355 |
+
Parameters
|
| 356 |
+
-----------
|
| 357 |
+
plane_origins : (n,3) float
|
| 358 |
+
Point on each plane
|
| 359 |
+
plane_normals : (n,3) float
|
| 360 |
+
Normal vector of each plane
|
| 361 |
+
line_origins : (n,3) float
|
| 362 |
+
Point at origin of each line
|
| 363 |
+
line_directions : (n,3) float
|
| 364 |
+
Direction vector of each line
|
| 365 |
+
return_distance : bool
|
| 366 |
+
Return distance from origin to point also
|
| 367 |
+
return_denom : bool
|
| 368 |
+
Return denominator, so you can check for small values
|
| 369 |
+
|
| 370 |
+
Returns
|
| 371 |
+
----------
|
| 372 |
+
on_plane : (n,3) float
|
| 373 |
+
Points on specified planes
|
| 374 |
+
valid : (n,) bool
|
| 375 |
+
Did plane intersect line or not
|
| 376 |
+
distance : (n,) float
|
| 377 |
+
[OPTIONAL] Distance from point
|
| 378 |
+
denom : (n,) float
|
| 379 |
+
[OPTIONAL] Denominator
|
| 380 |
+
"""
|
| 381 |
+
|
| 382 |
+
# check input types
|
| 383 |
+
plane_origins = np.asanyarray(plane_origins, dtype=np.float64)
|
| 384 |
+
plane_normals = np.asanyarray(plane_normals, dtype=np.float64)
|
| 385 |
+
line_origins = np.asanyarray(line_origins, dtype=np.float64)
|
| 386 |
+
line_directions = np.asanyarray(line_directions, dtype=np.float64)
|
| 387 |
+
|
| 388 |
+
# vector from line to plane
|
| 389 |
+
origin_vectors = plane_origins - line_origins
|
| 390 |
+
|
| 391 |
+
projection_ori = util.diagonal_dot(origin_vectors, plane_normals)
|
| 392 |
+
projection_dir = util.diagonal_dot(line_directions, plane_normals)
|
| 393 |
+
|
| 394 |
+
valid = np.abs(projection_dir) > 1e-5
|
| 395 |
+
|
| 396 |
+
distance = np.divide(projection_ori[valid], projection_dir[valid])
|
| 397 |
+
|
| 398 |
+
on_plane = line_directions[valid] * distance.reshape((-1, 1))
|
| 399 |
+
on_plane += line_origins[valid]
|
| 400 |
+
|
| 401 |
+
result = [on_plane, valid]
|
| 402 |
+
|
| 403 |
+
if return_distance:
|
| 404 |
+
result.append(distance)
|
| 405 |
+
if return_denom:
|
| 406 |
+
result.append(projection_dir)
|
| 407 |
+
|
| 408 |
+
return result
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
def slice_faces_plane(
|
| 412 |
+
vertices,
|
| 413 |
+
faces,
|
| 414 |
+
plane_normal,
|
| 415 |
+
plane_origin,
|
| 416 |
+
uv=None,
|
| 417 |
+
face_index=None,
|
| 418 |
+
cached_dots=None,
|
| 419 |
+
):
|
| 420 |
+
"""
|
| 421 |
+
Slice a mesh (given as a set of faces and vertices) with a plane, returning a
|
| 422 |
+
new mesh (again as a set of faces and vertices) that is the
|
| 423 |
+
portion of the original mesh to the positive normal side of the plane.
|
| 424 |
+
|
| 425 |
+
Parameters
|
| 426 |
+
---------
|
| 427 |
+
vertices : (n, 3) float
|
| 428 |
+
Vertices of source mesh to slice
|
| 429 |
+
faces : (n, 3) int
|
| 430 |
+
Faces of source mesh to slice
|
| 431 |
+
plane_normal : (3,) float
|
| 432 |
+
Normal vector of plane to intersect with mesh
|
| 433 |
+
plane_origin : (3,) float
|
| 434 |
+
Point on plane to intersect with mesh
|
| 435 |
+
uv : (n, 2) float, optional
|
| 436 |
+
UV coordinates of source mesh to slice
|
| 437 |
+
face_index : ((m,) int)
|
| 438 |
+
Indexes of faces to slice. When no mask is provided, the
|
| 439 |
+
default is to slice all faces.
|
| 440 |
+
cached_dots : (n, 3) float
|
| 441 |
+
If an external function has stored dot
|
| 442 |
+
products pass them here to avoid recomputing
|
| 443 |
+
|
| 444 |
+
Returns
|
| 445 |
+
----------
|
| 446 |
+
new_vertices : (n, 3) float
|
| 447 |
+
Vertices of sliced mesh
|
| 448 |
+
new_faces : (n, 3) int
|
| 449 |
+
Faces of sliced mesh
|
| 450 |
+
new_uv : (n, 2) int or None
|
| 451 |
+
UV coordinates of sliced mesh
|
| 452 |
+
"""
|
| 453 |
+
|
| 454 |
+
if len(vertices) == 0:
|
| 455 |
+
return vertices, faces, uv
|
| 456 |
+
|
| 457 |
+
have_uv = uv is not None
|
| 458 |
+
|
| 459 |
+
# Construct a mask for the faces to slice.
|
| 460 |
+
if face_index is not None:
|
| 461 |
+
faces = faces[face_index]
|
| 462 |
+
|
| 463 |
+
if cached_dots is not None:
|
| 464 |
+
dots = cached_dots
|
| 465 |
+
else:
|
| 466 |
+
# dot product of each vertex with the plane normal indexed by face
|
| 467 |
+
# so for each face the dot product of each vertex is a row
|
| 468 |
+
# shape is the same as faces (n,3)
|
| 469 |
+
dots = np.dot(vertices - plane_origin, plane_normal)
|
| 470 |
+
|
| 471 |
+
# Find vertex orientations w.r.t. faces for all triangles:
|
| 472 |
+
# -1 -> vertex "inside" plane (positive normal direction)
|
| 473 |
+
# 0 -> vertex on plane
|
| 474 |
+
# 1 -> vertex "outside" plane (negative normal direction)
|
| 475 |
+
signs = np.zeros(len(vertices), dtype=np.int8)
|
| 476 |
+
signs[dots < -tol.merge] = 1
|
| 477 |
+
signs[dots > tol.merge] = -1
|
| 478 |
+
signs = signs[faces]
|
| 479 |
+
|
| 480 |
+
# Find all triangles that intersect this plane
|
| 481 |
+
# onedge <- indices of all triangles intersecting the plane
|
| 482 |
+
# inside <- indices of all triangles "inside" the plane (positive normal)
|
| 483 |
+
signs_sum = signs.sum(axis=1, dtype=np.int8)
|
| 484 |
+
signs_asum = np.abs(signs).sum(axis=1, dtype=np.int8)
|
| 485 |
+
|
| 486 |
+
# Cases:
|
| 487 |
+
# (0,0,0), (-1,0,0), (-1,-1,0), (-1,-1,-1) <- inside
|
| 488 |
+
# (1,0,0), (1,1,0), (1,1,1) <- outside
|
| 489 |
+
# (1,0,-1), (1,-1,-1), (1,1,-1) <- onedge
|
| 490 |
+
onedge = np.logical_and(signs_asum >= 2, np.abs(signs_sum) <= 1)
|
| 491 |
+
|
| 492 |
+
inside = signs_sum == -signs_asum
|
| 493 |
+
|
| 494 |
+
# for any faces that lie exactly on-the-plane
|
| 495 |
+
# we want to only include them if their normal
|
| 496 |
+
# is backwards from the slicing normal
|
| 497 |
+
on_plane = signs_asum == 0
|
| 498 |
+
if on_plane.any():
|
| 499 |
+
# compute the normals and whether
|
| 500 |
+
# face is degenerate here
|
| 501 |
+
check, valid = tm.normals(vertices[faces[on_plane]])
|
| 502 |
+
# only include faces back from normal
|
| 503 |
+
dot_check = np.dot(check, plane_normal)
|
| 504 |
+
# exclude any degenerate faces from the result
|
| 505 |
+
inside[on_plane] = valid
|
| 506 |
+
# exclude the degenerate face from our mask
|
| 507 |
+
on_plane[on_plane] = valid
|
| 508 |
+
# apply results for this subset
|
| 509 |
+
inside[on_plane] = dot_check < 0.0
|
| 510 |
+
|
| 511 |
+
# Automatically include all faces that are "inside"
|
| 512 |
+
new_faces = faces[inside]
|
| 513 |
+
|
| 514 |
+
# Separate faces on the edge into two cases: those which will become
|
| 515 |
+
# quads (two vertices inside plane) and those which will become triangles
|
| 516 |
+
# (one vertex inside plane)
|
| 517 |
+
triangles = vertices[faces]
|
| 518 |
+
cut_triangles = triangles[onedge]
|
| 519 |
+
cut_faces_quad = faces[np.logical_and(onedge, signs_sum < 0)]
|
| 520 |
+
cut_faces_tri = faces[np.logical_and(onedge, signs_sum >= 0)]
|
| 521 |
+
cut_signs_quad = signs[np.logical_and(onedge, signs_sum < 0)]
|
| 522 |
+
cut_signs_tri = signs[np.logical_and(onedge, signs_sum >= 0)]
|
| 523 |
+
|
| 524 |
+
# If no faces to cut, the surface is not in contact with this plane.
|
| 525 |
+
# Thus, return a mesh with only the inside faces
|
| 526 |
+
if len(cut_faces_quad) + len(cut_faces_tri) == 0:
|
| 527 |
+
if len(new_faces) == 0:
|
| 528 |
+
# if no new faces at all return empty arrays
|
| 529 |
+
empty = (
|
| 530 |
+
np.zeros((0, 3), dtype=np.float64),
|
| 531 |
+
np.zeros((0, 3), dtype=np.int64),
|
| 532 |
+
np.zeros((0, 2), dtype=np.float64) if have_uv else None,
|
| 533 |
+
)
|
| 534 |
+
return empty
|
| 535 |
+
|
| 536 |
+
# find the unique indices in the new faces
|
| 537 |
+
# using an integer-only unique function
|
| 538 |
+
unique, inverse = grouping.unique_bincount(
|
| 539 |
+
new_faces.reshape(-1), minlength=len(vertices), return_inverse=True
|
| 540 |
+
)
|
| 541 |
+
|
| 542 |
+
# use the unique indices for our final vertices and faces
|
| 543 |
+
final_vert = vertices[unique]
|
| 544 |
+
final_face = inverse.reshape((-1, 3))
|
| 545 |
+
final_uv = uv[unique] if have_uv else None
|
| 546 |
+
|
| 547 |
+
return final_vert, final_face, final_uv
|
| 548 |
+
|
| 549 |
+
# Extract the intersections of each triangle's edges with the plane
|
| 550 |
+
o = cut_triangles # origins
|
| 551 |
+
d = np.roll(o, -1, axis=1) - o # directions
|
| 552 |
+
num = (plane_origin - o).dot(plane_normal) # compute num/denom
|
| 553 |
+
denom = np.dot(d, plane_normal)
|
| 554 |
+
denom[denom == 0.0] = 1e-12 # prevent division by zero
|
| 555 |
+
dist = np.divide(num, denom)
|
| 556 |
+
# intersection points for each segment
|
| 557 |
+
int_points = dist[:, :, None] * d + o
|
| 558 |
+
|
| 559 |
+
# Initialize the array of new vertices with the current vertices
|
| 560 |
+
new_vertices = vertices
|
| 561 |
+
new_quad_vertices = np.zeros((0, 3))
|
| 562 |
+
new_tri_vertices = np.zeros((0, 3))
|
| 563 |
+
|
| 564 |
+
# Handle the case where a new quad is formed by the intersection
|
| 565 |
+
# First, extract the intersection points belonging to a new quad
|
| 566 |
+
quad_int_points = int_points[(signs_sum < 0)[onedge], :, :]
|
| 567 |
+
num_quads = len(quad_int_points)
|
| 568 |
+
if num_quads > 0:
|
| 569 |
+
# Extract the vertex on the outside of the plane, then get the vertices
|
| 570 |
+
# (in CCW order of the inside vertices)
|
| 571 |
+
quad_int_inds = np.where(cut_signs_quad == 1)[1]
|
| 572 |
+
quad_int_verts = cut_faces_quad[
|
| 573 |
+
np.stack((range(num_quads), range(num_quads)), axis=1),
|
| 574 |
+
np.stack(((quad_int_inds + 1) % 3, (quad_int_inds + 2) % 3), axis=1),
|
| 575 |
+
]
|
| 576 |
+
|
| 577 |
+
# Fill out new quad faces with the intersection points as vertices
|
| 578 |
+
new_quad_faces = np.append(
|
| 579 |
+
quad_int_verts,
|
| 580 |
+
np.arange(len(new_vertices), len(new_vertices) + 2 * num_quads).reshape(
|
| 581 |
+
num_quads, 2
|
| 582 |
+
),
|
| 583 |
+
axis=1,
|
| 584 |
+
)
|
| 585 |
+
|
| 586 |
+
# Extract correct intersection points from int_points and order them in
|
| 587 |
+
# the same way as they were added to faces
|
| 588 |
+
new_quad_vertices = quad_int_points[
|
| 589 |
+
np.stack((range(num_quads), range(num_quads)), axis=1),
|
| 590 |
+
np.stack((((quad_int_inds + 2) % 3).T, quad_int_inds.T), axis=1),
|
| 591 |
+
:,
|
| 592 |
+
].reshape(2 * num_quads, 3)
|
| 593 |
+
|
| 594 |
+
# Add new vertices to existing vertices, triangulate quads, and add the
|
| 595 |
+
# resulting triangles to the new faces
|
| 596 |
+
new_vertices = np.append(new_vertices, new_quad_vertices, axis=0)
|
| 597 |
+
new_tri_faces_from_quads = geometry.triangulate_quads(new_quad_faces)
|
| 598 |
+
new_faces = np.append(new_faces, new_tri_faces_from_quads, axis=0)
|
| 599 |
+
|
| 600 |
+
# Handle the case where a new triangle is formed by the intersection
|
| 601 |
+
# First, extract the intersection points belonging to a new triangle
|
| 602 |
+
tri_int_points = int_points[(signs_sum >= 0)[onedge], :, :]
|
| 603 |
+
num_tris = len(tri_int_points)
|
| 604 |
+
if num_tris > 0:
|
| 605 |
+
# Extract the single vertex for each triangle inside the plane and get the
|
| 606 |
+
# inside vertices (CCW order)
|
| 607 |
+
tri_int_inds = np.where(cut_signs_tri == -1)[1]
|
| 608 |
+
tri_int_verts = cut_faces_tri[range(num_tris), tri_int_inds].reshape(num_tris, 1)
|
| 609 |
+
|
| 610 |
+
# Fill out new triangles with the intersection points as vertices
|
| 611 |
+
new_tri_faces = np.append(
|
| 612 |
+
tri_int_verts,
|
| 613 |
+
np.arange(len(new_vertices), len(new_vertices) + 2 * num_tris).reshape(
|
| 614 |
+
num_tris, 2
|
| 615 |
+
),
|
| 616 |
+
axis=1,
|
| 617 |
+
)
|
| 618 |
+
|
| 619 |
+
# Extract correct intersection points and order them in the same way as
|
| 620 |
+
# the vertices were added to the faces
|
| 621 |
+
new_tri_vertices = tri_int_points[
|
| 622 |
+
np.stack((range(num_tris), range(num_tris)), axis=1),
|
| 623 |
+
np.stack((tri_int_inds.T, ((tri_int_inds + 2) % 3).T), axis=1),
|
| 624 |
+
:,
|
| 625 |
+
].reshape(2 * num_tris, 3)
|
| 626 |
+
|
| 627 |
+
# Append new vertices and new faces
|
| 628 |
+
new_vertices = np.append(new_vertices, new_tri_vertices, axis=0)
|
| 629 |
+
new_faces = np.append(new_faces, new_tri_faces, axis=0)
|
| 630 |
+
|
| 631 |
+
# find the unique indices in the new faces
|
| 632 |
+
# using an integer-only unique function
|
| 633 |
+
unique, inverse = grouping.unique_bincount(
|
| 634 |
+
new_faces.reshape(-1), minlength=len(new_vertices), return_inverse=True
|
| 635 |
+
)
|
| 636 |
+
|
| 637 |
+
# use the unique indexes for our final vertex and faces
|
| 638 |
+
final_vert = new_vertices[unique]
|
| 639 |
+
final_face = inverse.reshape((-1, 3))
|
| 640 |
+
|
| 641 |
+
final_uv = None
|
| 642 |
+
if have_uv:
|
| 643 |
+
# Generate barycentric coordinates for intersection vertices
|
| 644 |
+
quad_barycentrics = points_to_barycentric(
|
| 645 |
+
np.repeat(vertices[cut_faces_quad], 2, axis=0), new_quad_vertices
|
| 646 |
+
)
|
| 647 |
+
tri_barycentrics = points_to_barycentric(
|
| 648 |
+
np.repeat(vertices[cut_faces_tri], 2, axis=0), new_tri_vertices
|
| 649 |
+
)
|
| 650 |
+
all_barycentrics = np.concatenate([quad_barycentrics, tri_barycentrics])
|
| 651 |
+
|
| 652 |
+
# Interpolate UVs
|
| 653 |
+
cut_uv = np.concatenate([uv[cut_faces_quad], uv[cut_faces_tri]])
|
| 654 |
+
new_uv = np.einsum("ijk,ij->ik", np.repeat(cut_uv, 2, axis=0), all_barycentrics)
|
| 655 |
+
final_uv = np.concatenate([uv, new_uv])[unique]
|
| 656 |
+
|
| 657 |
+
return final_vert, final_face, final_uv
|
| 658 |
+
|
| 659 |
+
|
| 660 |
+
def slice_mesh_plane(
|
| 661 |
+
mesh,
|
| 662 |
+
plane_normal,
|
| 663 |
+
plane_origin,
|
| 664 |
+
face_index=None,
|
| 665 |
+
cap=False,
|
| 666 |
+
engine=None,
|
| 667 |
+
**kwargs,
|
| 668 |
+
):
|
| 669 |
+
"""
|
| 670 |
+
Slice a mesh with a plane returning a new mesh that is the
|
| 671 |
+
portion of the original mesh to the positive normal side
|
| 672 |
+
of the plane.
|
| 673 |
+
|
| 674 |
+
Parameters
|
| 675 |
+
---------
|
| 676 |
+
mesh : Trimesh object
|
| 677 |
+
Source mesh to slice
|
| 678 |
+
plane_normal : (3,) float
|
| 679 |
+
Normal vector of plane to intersect with mesh
|
| 680 |
+
plane_origin : (3,) float
|
| 681 |
+
Point on plane to intersect with mesh
|
| 682 |
+
cap : bool
|
| 683 |
+
If True, cap the result with a triangulated polygon
|
| 684 |
+
face_index : ((m,) int)
|
| 685 |
+
Indexes of mesh.faces to slice. When no mask is provided, the
|
| 686 |
+
default is to slice all faces.
|
| 687 |
+
cached_dots : (n, 3) float
|
| 688 |
+
If an external function has stored dot
|
| 689 |
+
products pass them here to avoid recomputing
|
| 690 |
+
engine : None or str
|
| 691 |
+
Triangulation engine passed to `triangulate_polygon`
|
| 692 |
+
kwargs : dict
|
| 693 |
+
Passed to the newly created sliced mesh
|
| 694 |
+
|
| 695 |
+
Returns
|
| 696 |
+
----------
|
| 697 |
+
new_mesh : Trimesh object
|
| 698 |
+
Sliced mesh
|
| 699 |
+
"""
|
| 700 |
+
# check input for none
|
| 701 |
+
if mesh is None:
|
| 702 |
+
return None
|
| 703 |
+
|
| 704 |
+
# avoid circular import
|
| 705 |
+
from scipy.spatial import cKDTree
|
| 706 |
+
|
| 707 |
+
from .base import Trimesh
|
| 708 |
+
from .creation import triangulate_polygon
|
| 709 |
+
from .path import polygons
|
| 710 |
+
from .visual import TextureVisuals
|
| 711 |
+
|
| 712 |
+
# check input plane
|
| 713 |
+
plane_normal = np.asanyarray(plane_normal, dtype=np.float64)
|
| 714 |
+
plane_origin = np.asanyarray(plane_origin, dtype=np.float64)
|
| 715 |
+
|
| 716 |
+
# check to make sure origins and normals have acceptable shape
|
| 717 |
+
shape_ok = (
|
| 718 |
+
(plane_origin.shape == (3,) or util.is_shape(plane_origin, (-1, 3)))
|
| 719 |
+
and (plane_normal.shape == (3,) or util.is_shape(plane_normal, (-1, 3)))
|
| 720 |
+
and plane_origin.shape == plane_normal.shape
|
| 721 |
+
)
|
| 722 |
+
if not shape_ok:
|
| 723 |
+
raise ValueError("plane origins and normals must be (n, 3)!")
|
| 724 |
+
|
| 725 |
+
# start with copy of original mesh, faces, and vertices
|
| 726 |
+
vertices = mesh.vertices.copy()
|
| 727 |
+
faces = mesh.faces.copy()
|
| 728 |
+
|
| 729 |
+
# We copy the UV coordinates if available
|
| 730 |
+
has_uv = (
|
| 731 |
+
hasattr(mesh.visual, "uv") and np.shape(mesh.visual.uv) == (len(mesh.vertices), 2)
|
| 732 |
+
) and not cap
|
| 733 |
+
uv = mesh.visual.uv.copy() if has_uv else None
|
| 734 |
+
|
| 735 |
+
if "process" not in kwargs:
|
| 736 |
+
kwargs["process"] = False
|
| 737 |
+
|
| 738 |
+
# slice away specified planes
|
| 739 |
+
for origin, normal in zip(
|
| 740 |
+
plane_origin.reshape((-1, 3)), plane_normal.reshape((-1, 3))
|
| 741 |
+
):
|
| 742 |
+
# save the new vertices and faces
|
| 743 |
+
vertices, faces, uv = slice_faces_plane(
|
| 744 |
+
vertices=vertices,
|
| 745 |
+
faces=faces,
|
| 746 |
+
uv=uv,
|
| 747 |
+
plane_normal=normal,
|
| 748 |
+
plane_origin=origin,
|
| 749 |
+
face_index=face_index,
|
| 750 |
+
)
|
| 751 |
+
# check if cap arg specified
|
| 752 |
+
if cap:
|
| 753 |
+
if face_index:
|
| 754 |
+
# This hasn't been implemented yet.
|
| 755 |
+
raise NotImplementedError("face_index and cap can't be used together")
|
| 756 |
+
|
| 757 |
+
# start by deduplicating vertices again
|
| 758 |
+
unique, inverse = grouping.unique_rows(vertices)
|
| 759 |
+
vertices = vertices[unique]
|
| 760 |
+
# will collect additional faces
|
| 761 |
+
f = inverse[faces]
|
| 762 |
+
# remove degenerate faces by checking to make sure
|
| 763 |
+
# that each face has three unique indices
|
| 764 |
+
f = f[(f[:, :1] != f[:, 1:]).all(axis=1)]
|
| 765 |
+
# transform to the cap plane
|
| 766 |
+
to_2D = geometry.plane_transform(origin=origin, normal=-normal)
|
| 767 |
+
to_3D = np.linalg.inv(to_2D)
|
| 768 |
+
|
| 769 |
+
vertices_2D = tf.transform_points(vertices, to_2D)
|
| 770 |
+
edges = geometry.faces_to_edges(f)
|
| 771 |
+
edges.sort(axis=1)
|
| 772 |
+
|
| 773 |
+
on_plane = np.abs(vertices_2D[:, 2]) < 1e-8
|
| 774 |
+
edges = edges[on_plane[edges].all(axis=1)]
|
| 775 |
+
edges = edges[edges[:, 0] != edges[:, 1]]
|
| 776 |
+
|
| 777 |
+
unique_edge = grouping.group_rows(edges, require_count=1)
|
| 778 |
+
if len(unique) < 3:
|
| 779 |
+
continue
|
| 780 |
+
|
| 781 |
+
tree = cKDTree(vertices)
|
| 782 |
+
# collect new faces
|
| 783 |
+
faces = [f]
|
| 784 |
+
for p in polygons.edges_to_polygons(edges[unique_edge], vertices_2D[:, :2]):
|
| 785 |
+
# triangulate cap and raise an error if any new vertices were inserted
|
| 786 |
+
vn, fn = triangulate_polygon(p, engine=engine, force_vertices=True)
|
| 787 |
+
# collect the original index for the new vertices
|
| 788 |
+
vn3 = tf.transform_points(util.stack_3D(vn), to_3D)
|
| 789 |
+
distance, vid = tree.query(vn3)
|
| 790 |
+
if distance.max() > 1e-8:
|
| 791 |
+
util.log.debug("triangulate may have inserted vertex!")
|
| 792 |
+
# triangulation should not have inserted vertices
|
| 793 |
+
nf = vid[fn]
|
| 794 |
+
# hmm but it may have returned faces that are now degenerate
|
| 795 |
+
nf_ok = (nf[:, 1:] != nf[:, :1]).all(axis=1) & (nf[:, 1] != nf[:, 2])
|
| 796 |
+
faces.append(nf[nf_ok])
|
| 797 |
+
|
| 798 |
+
faces = np.vstack(faces)
|
| 799 |
+
|
| 800 |
+
visual = (
|
| 801 |
+
TextureVisuals(uv=uv, material=mesh.visual.material.copy()) if has_uv else None
|
| 802 |
+
)
|
| 803 |
+
|
| 804 |
+
# return the sliced mesh
|
| 805 |
+
return Trimesh(vertices=vertices, faces=faces, visual=visual, **kwargs)
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/interval.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
interval.py
|
| 3 |
+
--------------
|
| 4 |
+
|
| 5 |
+
Deal with 1D intervals which are defined by:
|
| 6 |
+
[start position, end position]
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from .typed import ArrayLike, NDArray, float64
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def intersection(a: ArrayLike, b: NDArray[float64]) -> NDArray[float64]:
|
| 15 |
+
"""
|
| 16 |
+
Given pairs of ranges merge them in to
|
| 17 |
+
one range if they overlap.
|
| 18 |
+
|
| 19 |
+
Parameters
|
| 20 |
+
--------------
|
| 21 |
+
a : (2, ) or (n, 2)
|
| 22 |
+
Start and end of a 1D interval
|
| 23 |
+
b : (2, ) float
|
| 24 |
+
Start and end of a 1D interval
|
| 25 |
+
|
| 26 |
+
Returns
|
| 27 |
+
--------------
|
| 28 |
+
inter : (2, ) or (2, 2) float
|
| 29 |
+
The unioned range from the two inputs,
|
| 30 |
+
if not np.ptp(`inter, axis=1)` will be zero.
|
| 31 |
+
"""
|
| 32 |
+
a = np.array(a, dtype=np.float64)
|
| 33 |
+
b = np.array(b, dtype=np.float64)
|
| 34 |
+
|
| 35 |
+
# convert to vectorized form
|
| 36 |
+
is_1D = a.shape == (2,)
|
| 37 |
+
a = a.reshape((-1, 2))
|
| 38 |
+
b = b.reshape((-1, 2))
|
| 39 |
+
|
| 40 |
+
# make sure they're min-max
|
| 41 |
+
a.sort(axis=1)
|
| 42 |
+
b.sort(axis=1)
|
| 43 |
+
a_low, a_high = a.T
|
| 44 |
+
b_low, b_high = b.T
|
| 45 |
+
|
| 46 |
+
# do the checks
|
| 47 |
+
check = np.logical_not(np.logical_or(b_low >= a_high, a_low >= b_high))
|
| 48 |
+
overlap = np.zeros(a.shape, dtype=np.float64)
|
| 49 |
+
overlap[check] = np.column_stack(
|
| 50 |
+
(
|
| 51 |
+
np.array([a_low[check], b_low[check]]).max(axis=0),
|
| 52 |
+
np.array([a_high[check], b_high[check]]).min(axis=0),
|
| 53 |
+
)
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if is_1D:
|
| 57 |
+
return overlap[0]
|
| 58 |
+
|
| 59 |
+
return overlap
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def union(intervals: ArrayLike, sort: bool = True) -> NDArray[float64]:
|
| 63 |
+
"""
|
| 64 |
+
For array of multiple intervals union them all into
|
| 65 |
+
the subset of intervals.
|
| 66 |
+
|
| 67 |
+
For example:
|
| 68 |
+
`intervals = [[1,2], [2,3]] -> [[1, 3]]`
|
| 69 |
+
`intervals = [[1,2], [2.5,3]] -> [[1, 2], [2.5, 3]]`
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
Parameters
|
| 73 |
+
------------
|
| 74 |
+
intervals : (n, 2)
|
| 75 |
+
Pairs of `(min, max)` values.
|
| 76 |
+
sort
|
| 77 |
+
If the array is already ordered into (min, max) pairs
|
| 78 |
+
and then pairs sorted by minimum value you can skip the
|
| 79 |
+
sorting in this function.
|
| 80 |
+
|
| 81 |
+
Returns
|
| 82 |
+
----------
|
| 83 |
+
unioned : (m, 2)
|
| 84 |
+
New intervals where `m <= n`
|
| 85 |
+
"""
|
| 86 |
+
if len(intervals) == 0:
|
| 87 |
+
return np.zeros(0)
|
| 88 |
+
|
| 89 |
+
# if the intervals have not been pre-sorted we should apply our sorting logic
|
| 90 |
+
# you would only skip this if you are subsetting a larger list elsewhere.
|
| 91 |
+
if sort:
|
| 92 |
+
# copy inputs and make sure they are (min, max) pairs
|
| 93 |
+
intervals = np.sort(intervals, axis=1)
|
| 94 |
+
# order them by lowest starting point
|
| 95 |
+
intervals = intervals[intervals[:, 0].argsort()]
|
| 96 |
+
|
| 97 |
+
# we know we will have at least one interval
|
| 98 |
+
unions = [intervals[0].tolist()]
|
| 99 |
+
|
| 100 |
+
for begin, end in intervals[1:]:
|
| 101 |
+
if unions[-1][1] >= begin:
|
| 102 |
+
unions[-1][1] = max(unions[-1][1], end)
|
| 103 |
+
else:
|
| 104 |
+
unions.append([begin, end])
|
| 105 |
+
|
| 106 |
+
return np.array(unions)
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/iteration.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import OrderedDict
|
| 2 |
+
from math import log2
|
| 3 |
+
from typing import Any
|
| 4 |
+
|
| 5 |
+
from .typed import Callable, Iterable, NDArray, Sequence
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def reduce_cascade(operation: Callable, items: Sequence | NDArray):
|
| 9 |
+
"""
|
| 10 |
+
Call an operation function in a cascaded pairwise way against a
|
| 11 |
+
flat list of items.
|
| 12 |
+
|
| 13 |
+
This should produce the same result as `functools.reduce`
|
| 14 |
+
if `operation` is commutable like addition or multiplication.
|
| 15 |
+
This may be faster for an `operation` that runs with a speed
|
| 16 |
+
proportional to its largest input, which mesh booleans appear to.
|
| 17 |
+
|
| 18 |
+
The union of a large number of small meshes appears to be
|
| 19 |
+
"much faster" using this method.
|
| 20 |
+
|
| 21 |
+
This only differs from `functools.reduce` for commutative `operation`
|
| 22 |
+
in that it returns `None` on empty inputs rather than `functools.reduce`
|
| 23 |
+
which raises a `TypeError`.
|
| 24 |
+
|
| 25 |
+
For example on `a b c d e f g` this function would run and return:
|
| 26 |
+
a b
|
| 27 |
+
c d
|
| 28 |
+
e f
|
| 29 |
+
ab cd
|
| 30 |
+
ef g
|
| 31 |
+
abcd efg
|
| 32 |
+
-> abcdefg
|
| 33 |
+
|
| 34 |
+
Where `functools.reduce` would run and return:
|
| 35 |
+
a b
|
| 36 |
+
ab c
|
| 37 |
+
abc d
|
| 38 |
+
abcd e
|
| 39 |
+
abcde f
|
| 40 |
+
abcdef g
|
| 41 |
+
-> abcdefg
|
| 42 |
+
|
| 43 |
+
Parameters
|
| 44 |
+
----------
|
| 45 |
+
operation
|
| 46 |
+
The function to call on pairs of items.
|
| 47 |
+
items
|
| 48 |
+
The flat list of items to apply operation against.
|
| 49 |
+
"""
|
| 50 |
+
if len(items) == 0:
|
| 51 |
+
return None
|
| 52 |
+
elif len(items) == 1:
|
| 53 |
+
# skip the loop overhead for a single item
|
| 54 |
+
return items[0]
|
| 55 |
+
elif len(items) == 2:
|
| 56 |
+
# skip the loop overhead for a single pair
|
| 57 |
+
return operation(items[0], items[1])
|
| 58 |
+
|
| 59 |
+
for _ in range(int(1 + log2(len(items)))):
|
| 60 |
+
results = []
|
| 61 |
+
|
| 62 |
+
# loop over pairs of items.
|
| 63 |
+
items_mod = len(items) % 2
|
| 64 |
+
for i in range(0, len(items) - items_mod, 2):
|
| 65 |
+
results.append(operation(items[i], items[i + 1]))
|
| 66 |
+
|
| 67 |
+
# if we had a non-even number of items it will have been
|
| 68 |
+
# skipped by the loop so append it to our list
|
| 69 |
+
if items_mod != 0:
|
| 70 |
+
results.append(items[-1])
|
| 71 |
+
|
| 72 |
+
items = results
|
| 73 |
+
|
| 74 |
+
# logic should have reduced to a single item
|
| 75 |
+
assert len(results) == 1
|
| 76 |
+
|
| 77 |
+
return results[0]
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def chain(*args: Iterable[Any] | Any | None) -> list[Any]:
|
| 81 |
+
"""
|
| 82 |
+
A less principled version of `list(itertools.chain(*args))` that
|
| 83 |
+
accepts non-iterable values, filters `None`, and returns a list
|
| 84 |
+
rather than yielding values.
|
| 85 |
+
|
| 86 |
+
If all passed values are iterables this will return identical
|
| 87 |
+
results to `list(itertools.chain(*args))`.
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
Examples
|
| 91 |
+
----------
|
| 92 |
+
|
| 93 |
+
In [1]: list(itertools.chain([1,2], [3]))
|
| 94 |
+
Out[1]: [1, 2, 3]
|
| 95 |
+
|
| 96 |
+
In [2]: trimesh.util.chain([1,2], [3])
|
| 97 |
+
Out[2]: [1, 2, 3]
|
| 98 |
+
|
| 99 |
+
In [3]: trimesh.util.chain([1,2], [3], 4)
|
| 100 |
+
Out[3]: [1, 2, 3, 4]
|
| 101 |
+
|
| 102 |
+
In [4]: list(itertools.chain([1,2], [3], 4))
|
| 103 |
+
----> 1 list(itertools.chain([1,2], [3], 4))
|
| 104 |
+
TypeError: 'int' object is not iterable
|
| 105 |
+
|
| 106 |
+
In [5]: trimesh.util.chain([1,2], None, 3, None, [4], [], [], 5, [])
|
| 107 |
+
Out[5]: [1, 2, 3, 4, 5]
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
Parameters
|
| 111 |
+
-----------
|
| 112 |
+
args
|
| 113 |
+
Will be individually checked to see if they're iterable
|
| 114 |
+
before either being appended or extended to a flat list.
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
Returns
|
| 118 |
+
----------
|
| 119 |
+
chained
|
| 120 |
+
The values in a flat list.
|
| 121 |
+
"""
|
| 122 |
+
# collect values to a flat list
|
| 123 |
+
chained = []
|
| 124 |
+
# extend if it's a sequence, otherwise append
|
| 125 |
+
[
|
| 126 |
+
chained.extend(a)
|
| 127 |
+
if (hasattr(a, "__iter__") and not isinstance(a, (str, bytes)))
|
| 128 |
+
else chained.append(a)
|
| 129 |
+
for a in args
|
| 130 |
+
if a is not None
|
| 131 |
+
]
|
| 132 |
+
return chained
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
class IndexedDict(OrderedDict):
|
| 136 |
+
"""
|
| 137 |
+
An append-only `OrderedDict` which knows what position a key was inserted at.
|
| 138 |
+
|
| 139 |
+
Useful anywhere values are referenced by *position* but keyed by content so
|
| 140 |
+
duplicates are only stored once: the only other spelling is
|
| 141 |
+
`list(d.keys()).index(key)`, which allocates every key and scans it, i.e.
|
| 142 |
+
quadratic. Looking up the position of all `n` keys once each:
|
| 143 |
+
|
| 144 |
+
n list(keys()).index() this class
|
| 145 |
+
2000 0.061s 0.00007s
|
| 146 |
+
4000 0.262s 0.00012s
|
| 147 |
+
8000 1.135s 0.00026s
|
| 148 |
+
|
| 149 |
+
Removing or reordering a key would shift the position of every key after it,
|
| 150 |
+
so `__delitem__`, `pop`, `popitem`, and `move_to_end` raise: the supported
|
| 151 |
+
way to remove is `clear` followed by `update`.
|
| 152 |
+
|
| 153 |
+
Examples
|
| 154 |
+
----------
|
| 155 |
+
|
| 156 |
+
In [1]: IndexedDict({"a": 1, "b": 2, "c": 3}).index("c")
|
| 157 |
+
Out[1]: 2
|
| 158 |
+
"""
|
| 159 |
+
|
| 160 |
+
# subclasses `OrderedDict` rather than `dict` as it routes `__init__`, `update`,
|
| 161 |
+
# `setdefault`, `|=`, and `copy` through `__setitem__`: one place records a position
|
| 162 |
+
|
| 163 |
+
def __init__(self, *args, **kwargs):
|
| 164 |
+
# must exist before `super` starts routing through `__setitem__`
|
| 165 |
+
self._position = {}
|
| 166 |
+
super().__init__(*args, **kwargs)
|
| 167 |
+
|
| 168 |
+
def __setitem__(self, key, value):
|
| 169 |
+
if key not in self:
|
| 170 |
+
self._position[key] = len(self)
|
| 171 |
+
super().__setitem__(key, value)
|
| 172 |
+
|
| 173 |
+
def _forbidden(self, *args, **kwargs):
|
| 174 |
+
raise TypeError("`IndexedDict` is append-only: use `clear` and `update`")
|
| 175 |
+
|
| 176 |
+
# removing or reordering a key shifts the position of every key after it
|
| 177 |
+
__delitem__ = pop = popitem = move_to_end = _forbidden
|
| 178 |
+
|
| 179 |
+
def clear(self) -> None:
|
| 180 |
+
super().clear()
|
| 181 |
+
self._position.clear()
|
| 182 |
+
|
| 183 |
+
def index(self, key) -> int:
|
| 184 |
+
"""
|
| 185 |
+
Which position in insertion order was `key` inserted at.
|
| 186 |
+
"""
|
| 187 |
+
return self._position[key]
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/nsphere.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
nsphere.py
|
| 3 |
+
--------------
|
| 4 |
+
|
| 5 |
+
Functions for fitting and minimizing nspheres:
|
| 6 |
+
circles, spheres, hyperspheres, etc.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
from . import convex, util
|
| 12 |
+
from .constants import log, tol
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
# scipy is a soft dependency
|
| 16 |
+
from scipy import spatial
|
| 17 |
+
from scipy.optimize import leastsq
|
| 18 |
+
except BaseException as E:
|
| 19 |
+
# raise the exception when someone tries to use it
|
| 20 |
+
from . import exceptions
|
| 21 |
+
|
| 22 |
+
leastsq = exceptions.ExceptionWrapper(E)
|
| 23 |
+
spatial = exceptions.ExceptionWrapper(E)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def minimum_nsphere(obj):
|
| 27 |
+
"""
|
| 28 |
+
Compute the minimum n- sphere for a mesh or a set of points.
|
| 29 |
+
|
| 30 |
+
Uses the fact that the minimum n- sphere will be centered at one of
|
| 31 |
+
the vertices of the furthest site voronoi diagram, which is n*log(n)
|
| 32 |
+
but should be pretty fast due to using the scipy/qhull implementations
|
| 33 |
+
of convex hulls and voronoi diagrams.
|
| 34 |
+
|
| 35 |
+
Parameters
|
| 36 |
+
----------
|
| 37 |
+
obj : (n, d) float or trimesh.Trimesh
|
| 38 |
+
Points or mesh to find minimum bounding nsphere
|
| 39 |
+
|
| 40 |
+
Returns
|
| 41 |
+
----------
|
| 42 |
+
center : (d,) float
|
| 43 |
+
Center of fitted n- sphere
|
| 44 |
+
radius : float
|
| 45 |
+
Radius of fitted n-sphere
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
# reduce the input points or mesh to the vertices of the convex hull
|
| 49 |
+
# since we are computing the furthest site voronoi diagram this reduces
|
| 50 |
+
# the input complexity substantially and returns the same value
|
| 51 |
+
points = convex.hull_points(obj)
|
| 52 |
+
|
| 53 |
+
# we are scaling the mesh to a unit cube
|
| 54 |
+
# this used to pass qhull_options 'QbB' to Voronoi however this had a bug somewhere
|
| 55 |
+
# to avoid this we scale to a unit cube ourselves inside this function
|
| 56 |
+
points_origin = points.min(axis=0)
|
| 57 |
+
points_scale = np.ptp(points, axis=0).min()
|
| 58 |
+
points = (points - points_origin) / points_scale
|
| 59 |
+
|
| 60 |
+
# if all of the points are on an n-sphere already the voronoi
|
| 61 |
+
# method will fail so we check a least squares fit before
|
| 62 |
+
# bothering to compute the voronoi diagram
|
| 63 |
+
fit_C, fit_R, fit_E = fit_nsphere(points)
|
| 64 |
+
# return fit radius and center to global scale
|
| 65 |
+
fit_R = (((points - fit_C) ** 2).sum(axis=1).max() ** 0.5) * points_scale
|
| 66 |
+
fit_C = (fit_C * points_scale) + points_origin
|
| 67 |
+
|
| 68 |
+
if fit_E < 1e-6:
|
| 69 |
+
# points were on an n-sphere so just return fit
|
| 70 |
+
return fit_C, fit_R
|
| 71 |
+
|
| 72 |
+
# calculate a furthest site voronoi diagram
|
| 73 |
+
# this will fail if the points are ALL on the surface of
|
| 74 |
+
# the n-sphere but hopefully the least squares check caught those cases
|
| 75 |
+
# , qhull_options='QbB Pp')
|
| 76 |
+
voronoi = spatial.Voronoi(points, furthest_site=True)
|
| 77 |
+
|
| 78 |
+
# find the maximum radius^2 point for each of the voronoi vertices
|
| 79 |
+
# this is worst case quite expensive but we have taken
|
| 80 |
+
# convex hull to reduce n for this operation
|
| 81 |
+
# we are doing comparisons on the radius squared then rooting once
|
| 82 |
+
try:
|
| 83 |
+
# cdist is massivly faster than looping or tiling methods
|
| 84 |
+
# although it does create an extremely large intermediate array
|
| 85 |
+
# first, get an order of magnitude memory size estimate
|
| 86 |
+
# a float64 would be 8 bytes per entry plus overhead
|
| 87 |
+
memory_estimate = len(voronoi.vertices) * len(points) * 9
|
| 88 |
+
# if this `cdist` would require more than 4gb of memory fall
|
| 89 |
+
# back to the more memory-efficent but much slower loop
|
| 90 |
+
if memory_estimate > 4e9:
|
| 91 |
+
raise MemoryError
|
| 92 |
+
radii_2 = spatial.distance.cdist(
|
| 93 |
+
voronoi.vertices, points, metric="sqeuclidean"
|
| 94 |
+
).max(axis=1)
|
| 95 |
+
except MemoryError:
|
| 96 |
+
# log the MemoryError
|
| 97 |
+
log.warning("MemoryError: falling back to slower check!")
|
| 98 |
+
# fall back to a potentially very slow list comprehension
|
| 99 |
+
radii_2 = np.array(
|
| 100 |
+
[((points - v) ** 2).sum(axis=1).max() for v in voronoi.vertices]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# we want the smallest sphere so take the min of the radii
|
| 104 |
+
radii_idx = radii_2.argmin()
|
| 105 |
+
|
| 106 |
+
# return voronoi radius and center to global scale
|
| 107 |
+
radius_v = np.sqrt(radii_2[radii_idx]) * points_scale
|
| 108 |
+
center_v = (voronoi.vertices[radii_idx] * points_scale) + points_origin
|
| 109 |
+
|
| 110 |
+
if radius_v > fit_R:
|
| 111 |
+
return fit_C, fit_R
|
| 112 |
+
|
| 113 |
+
return center_v, radius_v
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def fit_nsphere(points, prior=None):
|
| 117 |
+
"""
|
| 118 |
+
Fit an n-sphere to a set of points using least squares.
|
| 119 |
+
|
| 120 |
+
Parameters
|
| 121 |
+
------------
|
| 122 |
+
points : (n, d) float
|
| 123 |
+
Points in space
|
| 124 |
+
prior : (d,) float
|
| 125 |
+
Best guess for center of nsphere
|
| 126 |
+
|
| 127 |
+
Returns
|
| 128 |
+
---------
|
| 129 |
+
center : (d,) float
|
| 130 |
+
Location of center
|
| 131 |
+
radius : float
|
| 132 |
+
Mean radius across circle
|
| 133 |
+
error : float
|
| 134 |
+
Peak to peak value of deviation from mean radius
|
| 135 |
+
"""
|
| 136 |
+
# make sure points are numpy array
|
| 137 |
+
points = np.asanyarray(points, dtype=np.float64)
|
| 138 |
+
# create ones so we can dot instead of using slower sum
|
| 139 |
+
ones = np.ones(points.shape[1])
|
| 140 |
+
|
| 141 |
+
def residuals(center):
|
| 142 |
+
# do the axis sum with a dot
|
| 143 |
+
# this gets called a LOT so worth optimizing
|
| 144 |
+
radii_sq = np.dot((points - center) ** 2, ones)
|
| 145 |
+
# residuals are difference between mean
|
| 146 |
+
# use our sum mean vs .mean() as it is slightly faster
|
| 147 |
+
return radii_sq - (radii_sq.sum() / len(radii_sq))
|
| 148 |
+
|
| 149 |
+
if prior is None:
|
| 150 |
+
guess = points.mean(axis=0)
|
| 151 |
+
else:
|
| 152 |
+
guess = np.asanyarray(prior)
|
| 153 |
+
|
| 154 |
+
center_result, return_code = leastsq(residuals, guess, xtol=1e-8)
|
| 155 |
+
|
| 156 |
+
if return_code not in [1, 2, 3, 4]:
|
| 157 |
+
raise ValueError("Least square fit failed!")
|
| 158 |
+
|
| 159 |
+
radii = util.row_norm(points - center_result)
|
| 160 |
+
radius = radii.mean()
|
| 161 |
+
error = np.ptp(radii)
|
| 162 |
+
return center_result, radius, error
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def is_nsphere(points):
|
| 166 |
+
"""
|
| 167 |
+
Check if a list of points is an nsphere.
|
| 168 |
+
|
| 169 |
+
Parameters
|
| 170 |
+
-----------
|
| 171 |
+
points : (n, dimension) float
|
| 172 |
+
Points in space
|
| 173 |
+
|
| 174 |
+
Returns
|
| 175 |
+
-----------
|
| 176 |
+
check : bool
|
| 177 |
+
True if input points are on an nsphere
|
| 178 |
+
"""
|
| 179 |
+
_center, _radius, error = fit_nsphere(points)
|
| 180 |
+
check = error < tol.merge
|
| 181 |
+
return check
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/parent.py
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
parent.py
|
| 3 |
+
-------------
|
| 4 |
+
|
| 5 |
+
The base class for Trimesh, PointCloud, and Scene objects
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import abc
|
| 9 |
+
import os
|
| 10 |
+
from dataclasses import dataclass
|
| 11 |
+
from typing import Any
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from . import bounds, caching
|
| 16 |
+
from . import transformations as tf
|
| 17 |
+
from .caching import cache_decorator
|
| 18 |
+
from .constants import tol
|
| 19 |
+
from .resolvers import ResolverLike
|
| 20 |
+
from .typed import ArrayLike, NDArray, float64
|
| 21 |
+
from .util import ABC
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class LoadSource:
|
| 26 |
+
"""
|
| 27 |
+
Save information about where a particular object was loaded from.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
# a file-like object that can be accessed
|
| 31 |
+
file_obj: Any | None = None
|
| 32 |
+
|
| 33 |
+
# a cleaned file type string, i.e. "stl"
|
| 34 |
+
file_type: str | None = None
|
| 35 |
+
|
| 36 |
+
# if this was originally loaded from a file path
|
| 37 |
+
# save it here so we can check it later.
|
| 38 |
+
file_path: str | None = None
|
| 39 |
+
|
| 40 |
+
# did we open `file_obj` ourselves?
|
| 41 |
+
was_opened: bool = False
|
| 42 |
+
|
| 43 |
+
# a resolver for loading assets next to the file
|
| 44 |
+
resolver: ResolverLike | None = None
|
| 45 |
+
|
| 46 |
+
@property
|
| 47 |
+
def file_name(self) -> str | None:
|
| 48 |
+
"""
|
| 49 |
+
Get just the file name from the path if available.
|
| 50 |
+
|
| 51 |
+
Returns
|
| 52 |
+
---------
|
| 53 |
+
file_name
|
| 54 |
+
Just the file name, i.e. for file_path="/a/b/c.stl" -> "c.stl"
|
| 55 |
+
"""
|
| 56 |
+
if self.file_path is None:
|
| 57 |
+
return None
|
| 58 |
+
return os.path.basename(self.file_path)
|
| 59 |
+
|
| 60 |
+
def __getstate__(self) -> dict[str, Any]:
|
| 61 |
+
# this overrides the `pickle.dump` behavior for this class
|
| 62 |
+
# we cannot pickle a file object so return `file_obj: None` for pickles
|
| 63 |
+
return {k: v if k != "file_obj" else None for k, v in self.__dict__.items()}
|
| 64 |
+
|
| 65 |
+
def __deepcopy__(self, *args):
|
| 66 |
+
return LoadSource(**self.__getstate__())
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class Geometry(ABC):
|
| 70 |
+
"""
|
| 71 |
+
`Geometry` is the parent class for all geometry.
|
| 72 |
+
|
| 73 |
+
By decorating a method with `abc.abstractmethod` it means
|
| 74 |
+
the objects that inherit from `Geometry` MUST implement
|
| 75 |
+
those methods.
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
# geometry should have a dict to store loose metadata
|
| 79 |
+
metadata: dict[str, Any]
|
| 80 |
+
|
| 81 |
+
@property
|
| 82 |
+
def source(self) -> LoadSource:
|
| 83 |
+
"""
|
| 84 |
+
Where and what was this current geometry loaded from?
|
| 85 |
+
|
| 86 |
+
Returns
|
| 87 |
+
--------
|
| 88 |
+
source
|
| 89 |
+
If loaded from a file, has the path, type, etc.
|
| 90 |
+
"""
|
| 91 |
+
# this should have been tacked on by the loader
|
| 92 |
+
# but we want to *always* be able to access
|
| 93 |
+
# a value like `mesh.source.file_type` so add a default
|
| 94 |
+
current = getattr(self, "_source", None)
|
| 95 |
+
if current is not None:
|
| 96 |
+
return current
|
| 97 |
+
self._source = LoadSource()
|
| 98 |
+
return self._source
|
| 99 |
+
|
| 100 |
+
@property
|
| 101 |
+
@abc.abstractmethod
|
| 102 |
+
def identifier_hash(self) -> str:
|
| 103 |
+
pass
|
| 104 |
+
|
| 105 |
+
@property
|
| 106 |
+
@abc.abstractmethod
|
| 107 |
+
def bounds(self) -> NDArray[np.float64]:
|
| 108 |
+
pass
|
| 109 |
+
|
| 110 |
+
@property
|
| 111 |
+
@abc.abstractmethod
|
| 112 |
+
def extents(self) -> NDArray[np.float64]:
|
| 113 |
+
pass
|
| 114 |
+
|
| 115 |
+
@abc.abstractmethod
|
| 116 |
+
def apply_transform(self, matrix: ArrayLike) -> Any:
|
| 117 |
+
pass
|
| 118 |
+
|
| 119 |
+
@property
|
| 120 |
+
@abc.abstractmethod
|
| 121 |
+
def is_empty(self) -> bool:
|
| 122 |
+
pass
|
| 123 |
+
|
| 124 |
+
def __hash__(self):
|
| 125 |
+
"""
|
| 126 |
+
Get a hash of the current geometry.
|
| 127 |
+
|
| 128 |
+
Returns
|
| 129 |
+
---------
|
| 130 |
+
hash
|
| 131 |
+
Hash of current graph and geometry.
|
| 132 |
+
"""
|
| 133 |
+
return self._data.__hash__() # type: ignore
|
| 134 |
+
|
| 135 |
+
@abc.abstractmethod
|
| 136 |
+
def copy(self):
|
| 137 |
+
pass
|
| 138 |
+
|
| 139 |
+
@abc.abstractmethod
|
| 140 |
+
def show(self):
|
| 141 |
+
pass
|
| 142 |
+
|
| 143 |
+
@abc.abstractmethod
|
| 144 |
+
def __add__(self, other):
|
| 145 |
+
pass
|
| 146 |
+
|
| 147 |
+
@abc.abstractmethod
|
| 148 |
+
def export(self, file_obj, file_type=None):
|
| 149 |
+
pass
|
| 150 |
+
|
| 151 |
+
def __repr__(self) -> str:
|
| 152 |
+
"""
|
| 153 |
+
Print quick summary of the current geometry without
|
| 154 |
+
computing properties.
|
| 155 |
+
|
| 156 |
+
Returns
|
| 157 |
+
-----------
|
| 158 |
+
repr : str
|
| 159 |
+
Human readable quick look at the geometry.
|
| 160 |
+
"""
|
| 161 |
+
elements = []
|
| 162 |
+
if hasattr(self, "vertices"):
|
| 163 |
+
# for Trimesh and PointCloud
|
| 164 |
+
elements.append(f"vertices.shape={self.vertices.shape}")
|
| 165 |
+
if hasattr(self, "faces"):
|
| 166 |
+
# for Trimesh
|
| 167 |
+
elements.append(f"faces.shape={self.faces.shape}")
|
| 168 |
+
if hasattr(self, "geometry") and isinstance(self.geometry, dict):
|
| 169 |
+
# for Scene
|
| 170 |
+
elements.append(f"len(geometry)={len(self.geometry)}")
|
| 171 |
+
if "Voxel" in type(self).__name__:
|
| 172 |
+
# for VoxelGrid objects
|
| 173 |
+
elements.append(str(self.shape)[1:-1])
|
| 174 |
+
if "file_name" in self.metadata:
|
| 175 |
+
display = self.metadata["file_name"]
|
| 176 |
+
elements.append(f"name=`{display}`")
|
| 177 |
+
return "<trimesh.{}({})>".format(type(self).__name__, ", ".join(elements))
|
| 178 |
+
|
| 179 |
+
def apply_translation(self, translation: ArrayLike):
|
| 180 |
+
"""
|
| 181 |
+
Translate the current mesh.
|
| 182 |
+
|
| 183 |
+
Parameters
|
| 184 |
+
----------
|
| 185 |
+
translation : (3,) float
|
| 186 |
+
Translation in XYZ
|
| 187 |
+
"""
|
| 188 |
+
translation = np.asanyarray(translation, dtype=np.float64)
|
| 189 |
+
if translation.shape == (2,):
|
| 190 |
+
# create a planar matrix if we were passed a 2D offset
|
| 191 |
+
return self.apply_transform(tf.planar_matrix(offset=translation))
|
| 192 |
+
elif translation.shape != (3,):
|
| 193 |
+
raise ValueError("Translation must be (3,) or (2,)!")
|
| 194 |
+
|
| 195 |
+
# manually create a translation matrix
|
| 196 |
+
matrix = np.eye(4)
|
| 197 |
+
matrix[:3, 3] = translation
|
| 198 |
+
return self.apply_transform(matrix)
|
| 199 |
+
|
| 200 |
+
def apply_scale(self, scaling):
|
| 201 |
+
"""
|
| 202 |
+
Scale the mesh.
|
| 203 |
+
|
| 204 |
+
Parameters
|
| 205 |
+
----------
|
| 206 |
+
scaling : float or (3,) float
|
| 207 |
+
Scale factor to apply to the mesh
|
| 208 |
+
"""
|
| 209 |
+
matrix = tf.scale_and_translate(scale=scaling)
|
| 210 |
+
# apply_transform will work nicely even on negative scales
|
| 211 |
+
return self.apply_transform(matrix)
|
| 212 |
+
|
| 213 |
+
def __radd__(self, other):
|
| 214 |
+
"""
|
| 215 |
+
Concatenate the geometry allowing concatenation with
|
| 216 |
+
built in `sum()` function:
|
| 217 |
+
`sum(Iterable[trimesh.Trimesh])`
|
| 218 |
+
|
| 219 |
+
Parameters
|
| 220 |
+
------------
|
| 221 |
+
other : Geometry
|
| 222 |
+
Geometry or 0
|
| 223 |
+
|
| 224 |
+
Returns
|
| 225 |
+
----------
|
| 226 |
+
concat : Geometry
|
| 227 |
+
Geometry of combined result
|
| 228 |
+
"""
|
| 229 |
+
|
| 230 |
+
if other == 0:
|
| 231 |
+
# adding 0 to a geometry never makes sense
|
| 232 |
+
return self
|
| 233 |
+
# otherwise just use the regular add function
|
| 234 |
+
return self.__add__(type(self)(other))
|
| 235 |
+
|
| 236 |
+
@cache_decorator
|
| 237 |
+
def scale(self) -> float:
|
| 238 |
+
"""
|
| 239 |
+
A loosely specified "order of magnitude scale" for the
|
| 240 |
+
geometry which always returns a value and can be used
|
| 241 |
+
to make code more robust to large scaling differences.
|
| 242 |
+
|
| 243 |
+
It returns the diagonal of the axis aligned bounding box
|
| 244 |
+
or if anything is invalid or undefined, `1.0`.
|
| 245 |
+
|
| 246 |
+
Returns
|
| 247 |
+
----------
|
| 248 |
+
scale : float
|
| 249 |
+
Approximate order of magnitude scale of the geometry.
|
| 250 |
+
"""
|
| 251 |
+
# if geometry is empty return 1.0
|
| 252 |
+
if self.extents is None:
|
| 253 |
+
return 1.0
|
| 254 |
+
|
| 255 |
+
# get the length of the AABB diagonal
|
| 256 |
+
scale = float((self.extents**2).sum() ** 0.5)
|
| 257 |
+
if scale < tol.zero:
|
| 258 |
+
return 1.0
|
| 259 |
+
|
| 260 |
+
return scale
|
| 261 |
+
|
| 262 |
+
@property
|
| 263 |
+
def units(self) -> str | None:
|
| 264 |
+
"""
|
| 265 |
+
Definition of units for the mesh.
|
| 266 |
+
|
| 267 |
+
Returns
|
| 268 |
+
----------
|
| 269 |
+
units : str
|
| 270 |
+
Unit system mesh is in, or None if not defined
|
| 271 |
+
"""
|
| 272 |
+
return self.metadata.get("units", None)
|
| 273 |
+
|
| 274 |
+
@units.setter
|
| 275 |
+
def units(self, value: str) -> None:
|
| 276 |
+
"""
|
| 277 |
+
Define the units of the current mesh.
|
| 278 |
+
"""
|
| 279 |
+
self.metadata["units"] = str(value).lower().strip()
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
class Geometry3D(Geometry):
|
| 283 |
+
"""
|
| 284 |
+
The `Geometry3D` object is the parent object of geometry objects
|
| 285 |
+
which are three dimensional, including Trimesh, PointCloud,
|
| 286 |
+
and Scene objects.
|
| 287 |
+
"""
|
| 288 |
+
|
| 289 |
+
@caching.cache_decorator
|
| 290 |
+
def bounding_box(self):
|
| 291 |
+
"""
|
| 292 |
+
An axis aligned bounding box for the current mesh.
|
| 293 |
+
|
| 294 |
+
Returns
|
| 295 |
+
----------
|
| 296 |
+
aabb : trimesh.primitives.Box
|
| 297 |
+
Box object with transform and extents defined
|
| 298 |
+
representing the axis aligned bounding box of the mesh
|
| 299 |
+
"""
|
| 300 |
+
from . import primitives
|
| 301 |
+
|
| 302 |
+
transform = np.eye(4)
|
| 303 |
+
# translate to center of axis aligned bounds
|
| 304 |
+
transform[:3, 3] = self.bounds.mean(axis=0)
|
| 305 |
+
|
| 306 |
+
return primitives.Box(transform=transform, extents=self.extents, mutable=False)
|
| 307 |
+
|
| 308 |
+
@caching.cache_decorator
|
| 309 |
+
def bounding_box_oriented(self):
|
| 310 |
+
"""
|
| 311 |
+
An oriented bounding box for the current mesh.
|
| 312 |
+
|
| 313 |
+
Returns
|
| 314 |
+
---------
|
| 315 |
+
obb : trimesh.primitives.Box
|
| 316 |
+
Box object with transform and extents defined
|
| 317 |
+
representing the minimum volume oriented
|
| 318 |
+
bounding box of the mesh
|
| 319 |
+
"""
|
| 320 |
+
from . import bounds, primitives
|
| 321 |
+
|
| 322 |
+
to_origin, extents = bounds.oriented_bounds(self)
|
| 323 |
+
return primitives.Box(
|
| 324 |
+
transform=np.linalg.inv(to_origin), extents=extents, mutable=False
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
@caching.cache_decorator
|
| 328 |
+
def bounding_sphere(self):
|
| 329 |
+
"""
|
| 330 |
+
A minimum volume bounding sphere for the current mesh.
|
| 331 |
+
|
| 332 |
+
Note that the Sphere primitive returned has an unpadded
|
| 333 |
+
exact `sphere_radius` so while the distance of every vertex
|
| 334 |
+
of the current mesh from sphere_center will be less than
|
| 335 |
+
sphere_radius, the faceted sphere primitive may not
|
| 336 |
+
contain every vertex.
|
| 337 |
+
|
| 338 |
+
Returns
|
| 339 |
+
--------
|
| 340 |
+
minball : trimesh.primitives.Sphere
|
| 341 |
+
Sphere primitive containing current mesh
|
| 342 |
+
"""
|
| 343 |
+
from . import nsphere, primitives
|
| 344 |
+
|
| 345 |
+
center, radius = nsphere.minimum_nsphere(self)
|
| 346 |
+
return primitives.Sphere(center=center, radius=radius, mutable=False)
|
| 347 |
+
|
| 348 |
+
@caching.cache_decorator
|
| 349 |
+
def bounding_cylinder(self):
|
| 350 |
+
"""
|
| 351 |
+
A minimum volume bounding cylinder for the current mesh.
|
| 352 |
+
|
| 353 |
+
Returns
|
| 354 |
+
--------
|
| 355 |
+
mincyl : trimesh.primitives.Cylinder
|
| 356 |
+
Cylinder primitive containing current mesh
|
| 357 |
+
"""
|
| 358 |
+
from . import bounds, primitives
|
| 359 |
+
|
| 360 |
+
kwargs = bounds.minimum_cylinder(self)
|
| 361 |
+
return primitives.Cylinder(mutable=False, **kwargs)
|
| 362 |
+
|
| 363 |
+
@caching.cache_decorator
|
| 364 |
+
def bounding_primitive(self):
|
| 365 |
+
"""
|
| 366 |
+
The minimum volume primitive (box, sphere, or cylinder) that
|
| 367 |
+
bounds the mesh.
|
| 368 |
+
|
| 369 |
+
Returns
|
| 370 |
+
---------
|
| 371 |
+
bounding_primitive : object
|
| 372 |
+
Smallest primitive which bounds the mesh:
|
| 373 |
+
trimesh.primitives.Sphere
|
| 374 |
+
trimesh.primitives.Box
|
| 375 |
+
trimesh.primitives.Cylinder
|
| 376 |
+
"""
|
| 377 |
+
options = [
|
| 378 |
+
self.bounding_box_oriented,
|
| 379 |
+
self.bounding_sphere,
|
| 380 |
+
self.bounding_cylinder,
|
| 381 |
+
]
|
| 382 |
+
volume_min = np.argmin([i.volume for i in options])
|
| 383 |
+
return options[volume_min]
|
| 384 |
+
|
| 385 |
+
def apply_obb(self, **kwargs) -> NDArray[float64]:
|
| 386 |
+
"""
|
| 387 |
+
Apply the oriented bounding box transform to the current mesh.
|
| 388 |
+
|
| 389 |
+
This will result in a mesh with an AABB centered at the
|
| 390 |
+
origin and the same dimensions as the OBB.
|
| 391 |
+
|
| 392 |
+
Parameters
|
| 393 |
+
------------
|
| 394 |
+
kwargs
|
| 395 |
+
Passed through to `bounds.oriented_bounds`
|
| 396 |
+
|
| 397 |
+
Returns
|
| 398 |
+
----------
|
| 399 |
+
matrix : (4, 4) float
|
| 400 |
+
Transformation matrix that was applied
|
| 401 |
+
to mesh to move it into OBB frame
|
| 402 |
+
"""
|
| 403 |
+
# save the pre-transform volume
|
| 404 |
+
if tol.strict and hasattr(self, "volume"):
|
| 405 |
+
volume = self.volume
|
| 406 |
+
|
| 407 |
+
# calculate the OBB passing keyword arguments through
|
| 408 |
+
matrix, extents = bounds.oriented_bounds(self, **kwargs)
|
| 409 |
+
# apply the transform
|
| 410 |
+
self.apply_transform(matrix)
|
| 411 |
+
|
| 412 |
+
if tol.strict:
|
| 413 |
+
# obb transform should not have changed volume
|
| 414 |
+
if hasattr(self, "volume") and getattr(self, "is_watertight", False):
|
| 415 |
+
assert np.isclose(self.volume, volume)
|
| 416 |
+
# overall extents should match what we expected
|
| 417 |
+
assert np.allclose(self.extents, extents)
|
| 418 |
+
|
| 419 |
+
return matrix
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
trimesh.path
|
| 3 |
+
-------------
|
| 4 |
+
|
| 5 |
+
Handle 2D and 3D vector paths such as those contained in an
|
| 6 |
+
SVG or DXF file.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
from .path import Path2D, Path3D
|
| 11 |
+
except BaseException as E:
|
| 12 |
+
from .. import exceptions
|
| 13 |
+
|
| 14 |
+
Path2D = exceptions.ExceptionWrapper(E)
|
| 15 |
+
Path3D = exceptions.ExceptionWrapper(E)
|
| 16 |
+
|
| 17 |
+
# explicitly add objects to all as per pep8
|
| 18 |
+
__all__ = ["Path2D", "Path3D"]
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (791 Bytes). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/arc.cpython-311.pyc
ADDED
|
Binary file (9.54 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/creation.cpython-311.pyc
ADDED
|
Binary file (10.7 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/curve.cpython-311.pyc
ADDED
|
Binary file (5.86 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/entities.cpython-311.pyc
ADDED
|
Binary file (31.8 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/intersections.cpython-311.pyc
ADDED
|
Binary file (2.76 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/packing.cpython-311.pyc
ADDED
|
Binary file (32 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/path.cpython-311.pyc
ADDED
|
Binary file (65.1 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/polygons.cpython-311.pyc
ADDED
|
Binary file (40.6 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/raster.cpython-311.pyc
ADDED
|
Binary file (4.33 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/repair.cpython-311.pyc
ADDED
|
Binary file (5.16 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/segments.cpython-311.pyc
ADDED
|
Binary file (20.8 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/simplify.cpython-311.pyc
ADDED
|
Binary file (15.7 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/traversal.cpython-311.pyc
ADDED
|
Binary file (18.9 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/__pycache__/util.cpython-311.pyc
ADDED
|
Binary file (2.92 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/arc.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from .. import util
|
| 6 |
+
from ..constants import res_path as res
|
| 7 |
+
from ..constants import tol_path as tol
|
| 8 |
+
from ..typed import ArrayLike, NDArray, Number, float64
|
| 9 |
+
|
| 10 |
+
# floating point zero
|
| 11 |
+
_TOL_ZERO = 1e-12
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class ArcInfo:
|
| 16 |
+
# What is the radius of the circular arc?
|
| 17 |
+
radius: float
|
| 18 |
+
|
| 19 |
+
# what is the center of the circular arc
|
| 20 |
+
# it is either 2D or 3D depending on input.
|
| 21 |
+
center: NDArray[float64]
|
| 22 |
+
|
| 23 |
+
# what is the 3D normal vector of the plane the arc lies on
|
| 24 |
+
normal: NDArray[float64] | None = None
|
| 25 |
+
|
| 26 |
+
# what is the starting and ending angle of the arc.
|
| 27 |
+
angles: NDArray[float64] | None = None
|
| 28 |
+
|
| 29 |
+
# what is the angular span of this circular arc.
|
| 30 |
+
span: Number | None = None
|
| 31 |
+
|
| 32 |
+
def __getitem__(self, item):
|
| 33 |
+
# add for backwards compatibility
|
| 34 |
+
return getattr(self, item)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def arc_center(
|
| 38 |
+
points: ArrayLike, return_normal: bool = True, return_angle: bool = True
|
| 39 |
+
) -> ArcInfo:
|
| 40 |
+
"""
|
| 41 |
+
Given three points on a 2D or 3D arc find the center,
|
| 42 |
+
radius, normal, and angular span.
|
| 43 |
+
|
| 44 |
+
Parameters
|
| 45 |
+
---------
|
| 46 |
+
points : (3, dimension) float
|
| 47 |
+
Points in space, where dimension is either 2 or 3
|
| 48 |
+
return_normal : bool
|
| 49 |
+
If True calculate the 3D normal unit vector
|
| 50 |
+
return_angle : bool
|
| 51 |
+
If True calculate the start and stop angle and span
|
| 52 |
+
|
| 53 |
+
Returns
|
| 54 |
+
---------
|
| 55 |
+
info
|
| 56 |
+
Arc center, radius, and other information.
|
| 57 |
+
"""
|
| 58 |
+
points = np.asanyarray(points, dtype=np.float64)
|
| 59 |
+
|
| 60 |
+
# get the non-unit vectors of the three points
|
| 61 |
+
vectors = points[[2, 0, 1]] - points[[1, 2, 0]]
|
| 62 |
+
# we need both the squared row sum and the non-squared
|
| 63 |
+
abc2 = np.dot(vectors**2, [1] * points.shape[1])
|
| 64 |
+
# same as np.linalg.norm(vectors, axis=1)
|
| 65 |
+
abc = np.sqrt(abc2)
|
| 66 |
+
|
| 67 |
+
# perform radius calculation scaled to shortest edge
|
| 68 |
+
# to avoid precision issues with small or large arcs
|
| 69 |
+
scale = abc.min()
|
| 70 |
+
# get the edge lengths scaled to the smallest
|
| 71 |
+
edges = abc / scale
|
| 72 |
+
# half the total length of the edges
|
| 73 |
+
half = edges.sum() / 2.0
|
| 74 |
+
# check the denominator for the radius calculation
|
| 75 |
+
denom = half * np.prod(half - edges)
|
| 76 |
+
if denom < tol.merge:
|
| 77 |
+
raise ValueError("arc is colinear!")
|
| 78 |
+
# find the radius and scale back after the operation
|
| 79 |
+
radius = scale * ((np.prod(edges) / 4.0) / np.sqrt(denom))
|
| 80 |
+
|
| 81 |
+
# use a barycentric approach to get the center
|
| 82 |
+
ba2 = (abc2[[1, 2, 0, 0, 2, 1, 0, 1, 2]] * [1, 1, -1, 1, 1, -1, 1, 1, -1]).reshape(
|
| 83 |
+
(3, 3)
|
| 84 |
+
).sum(axis=1) * abc2
|
| 85 |
+
center = points.T.dot(ba2) / ba2.sum()
|
| 86 |
+
|
| 87 |
+
if tol.strict:
|
| 88 |
+
# all points should be at the calculated radius from center
|
| 89 |
+
assert util.allclose(np.linalg.norm(points - center, axis=1), radius)
|
| 90 |
+
|
| 91 |
+
# start with initial results
|
| 92 |
+
result = {"center": center, "radius": radius}
|
| 93 |
+
if return_normal:
|
| 94 |
+
if points.shape == (3, 2):
|
| 95 |
+
# for 2D arcs still use the cross product so that
|
| 96 |
+
# the sign of the normal vector is consistent
|
| 97 |
+
result["normal"] = util.unitize(
|
| 98 |
+
np.cross(np.append(-vectors[1], 0), np.append(vectors[2], 0))
|
| 99 |
+
)
|
| 100 |
+
else:
|
| 101 |
+
# otherwise just take the cross product
|
| 102 |
+
result["normal"] = util.unitize(np.cross(-vectors[1], vectors[2]))
|
| 103 |
+
|
| 104 |
+
if return_angle:
|
| 105 |
+
# vectors from points on arc to center point
|
| 106 |
+
vector = util.unitize(points - center)
|
| 107 |
+
edge_direction = np.diff(points, axis=0)
|
| 108 |
+
# find the angle between the first and last vector
|
| 109 |
+
dot = np.dot(*vector[[0, 2]])
|
| 110 |
+
if dot < (_TOL_ZERO - 1):
|
| 111 |
+
angle = np.pi
|
| 112 |
+
elif dot > 1 - _TOL_ZERO:
|
| 113 |
+
angle = 0.0
|
| 114 |
+
else:
|
| 115 |
+
angle = np.arccos(dot)
|
| 116 |
+
# if the angle is nonzero and vectors are opposite direction
|
| 117 |
+
# it means we have a long arc rather than the short path
|
| 118 |
+
if abs(angle) > _TOL_ZERO and np.dot(*edge_direction) < 0.0:
|
| 119 |
+
angle = (np.pi * 2) - angle
|
| 120 |
+
# convoluted angle logic
|
| 121 |
+
angles = np.arctan2(*vector[:, :2].T[::-1]) + np.pi * 2
|
| 122 |
+
angles_sorted = np.sort(angles[[0, 2]])
|
| 123 |
+
reverse = angles_sorted[0] < angles[1] < angles_sorted[1]
|
| 124 |
+
angles_sorted = angles_sorted[:: (1 - int(not reverse) * 2)]
|
| 125 |
+
result["angles"] = angles_sorted
|
| 126 |
+
result["span"] = angle
|
| 127 |
+
|
| 128 |
+
return ArcInfo(**result)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def discretize_arc(points, close=False, scale=1.0):
|
| 132 |
+
"""
|
| 133 |
+
Returns a version of a three point arc consisting of
|
| 134 |
+
line segments.
|
| 135 |
+
|
| 136 |
+
Parameters
|
| 137 |
+
---------
|
| 138 |
+
points : (3, d) float
|
| 139 |
+
Points on the arc where d in [2,3]
|
| 140 |
+
close : boolean
|
| 141 |
+
If True close the arc into a circle
|
| 142 |
+
scale : float
|
| 143 |
+
What is the approximate overall drawing scale
|
| 144 |
+
Used to establish order of magnitude for precision
|
| 145 |
+
|
| 146 |
+
Returns
|
| 147 |
+
---------
|
| 148 |
+
discrete : (m, d) float
|
| 149 |
+
Connected points in space
|
| 150 |
+
"""
|
| 151 |
+
# make sure points are (n, 3)
|
| 152 |
+
points, is_2D = util.stack_3D(points, return_2D=True)
|
| 153 |
+
# find the center of the points
|
| 154 |
+
try:
|
| 155 |
+
# try to find the center from the arc points
|
| 156 |
+
center_info = arc_center(points)
|
| 157 |
+
except BaseException:
|
| 158 |
+
# if we hit an exception return a very bad but
|
| 159 |
+
# technically correct discretization of the arc
|
| 160 |
+
if is_2D:
|
| 161 |
+
return points[:, :2]
|
| 162 |
+
return points
|
| 163 |
+
|
| 164 |
+
center, R, N, angle = (
|
| 165 |
+
center_info.center,
|
| 166 |
+
center_info.radius,
|
| 167 |
+
center_info.normal,
|
| 168 |
+
center_info.span,
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# if requested, close arc into a circle
|
| 172 |
+
if close:
|
| 173 |
+
angle = np.pi * 2
|
| 174 |
+
|
| 175 |
+
# the number of facets for the arc
|
| 176 |
+
# which is the maximum of angle and linear criteria
|
| 177 |
+
count = max(angle / res.seg_angle, (R * angle) / (res.seg_frac * scale))
|
| 178 |
+
|
| 179 |
+
# force at LEAST 4 points for the arc
|
| 180 |
+
# otherwise the endpoints will diverge
|
| 181 |
+
count = int(np.ceil(np.clip(count, 4, np.inf)))
|
| 182 |
+
|
| 183 |
+
V1 = util.unitize(points[0] - center)
|
| 184 |
+
V2 = util.unitize(np.cross(-N, V1))
|
| 185 |
+
# angle spacing
|
| 186 |
+
t = np.linspace(0.0, angle, count).reshape((-1, 1))
|
| 187 |
+
|
| 188 |
+
# apply the vector formula
|
| 189 |
+
discrete = (np.cos(t) * V1 + np.sin(t) * V2) * R + center
|
| 190 |
+
|
| 191 |
+
if close:
|
| 192 |
+
# snap closed circles to exactly float-equal
|
| 193 |
+
discrete[-1] = discrete[0]
|
| 194 |
+
else:
|
| 195 |
+
# snap the discrete result to exact control points
|
| 196 |
+
discrete[[0, -1]] = points[[0, -1]]
|
| 197 |
+
|
| 198 |
+
# clip to the dimension of input
|
| 199 |
+
discrete = discrete[:, : (3 - is_2D)]
|
| 200 |
+
|
| 201 |
+
return discrete
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def to_threepoint(center, radius, angles=None):
|
| 205 |
+
"""
|
| 206 |
+
For 2D arcs, given a center and radius convert them to three
|
| 207 |
+
points on the arc.
|
| 208 |
+
|
| 209 |
+
Parameters
|
| 210 |
+
-----------
|
| 211 |
+
center : (2,) float
|
| 212 |
+
Center point on the plane
|
| 213 |
+
radius : float
|
| 214 |
+
Radius of arc
|
| 215 |
+
angles : (2,) float
|
| 216 |
+
Angles in radians for start and end angle
|
| 217 |
+
if not specified, will default to (0.0, pi)
|
| 218 |
+
|
| 219 |
+
Returns
|
| 220 |
+
----------
|
| 221 |
+
three : (3, 2) float
|
| 222 |
+
Arc control points
|
| 223 |
+
"""
|
| 224 |
+
# if no angles provided assume we want a half circle
|
| 225 |
+
if angles is None:
|
| 226 |
+
angles = [0.0, np.pi]
|
| 227 |
+
# force angles to float64
|
| 228 |
+
angles = np.asanyarray(angles, dtype=np.float64)
|
| 229 |
+
if angles.shape != (2,):
|
| 230 |
+
raise ValueError("angles must be (2,)!")
|
| 231 |
+
# provide the wrap around
|
| 232 |
+
if angles[1] < angles[0]:
|
| 233 |
+
angles[1] += np.pi * 2
|
| 234 |
+
|
| 235 |
+
center = np.asanyarray(center, dtype=np.float64)
|
| 236 |
+
if center.shape != (2,):
|
| 237 |
+
raise ValueError("only valid on 2D arcs!")
|
| 238 |
+
|
| 239 |
+
# turn the angles of [start, end]
|
| 240 |
+
# into [start, middle, end]
|
| 241 |
+
angles = np.array([angles[0], angles.mean(), angles[1]], dtype=np.float64)
|
| 242 |
+
# turn angles into (3, 2) points
|
| 243 |
+
three = (np.column_stack((np.cos(angles), np.sin(angles))) * radius) + center
|
| 244 |
+
|
| 245 |
+
return three
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/creation.py
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from .. import transformations, util
|
| 4 |
+
from ..geometry import plane_transform
|
| 5 |
+
from . import arc
|
| 6 |
+
from .entities import Arc, Line
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def circle_pattern(
|
| 10 |
+
pattern_radius, circle_radius, count, center=None, angle=None, **kwargs
|
| 11 |
+
):
|
| 12 |
+
"""
|
| 13 |
+
Create a Path2D representing a circle pattern.
|
| 14 |
+
|
| 15 |
+
Parameters
|
| 16 |
+
------------
|
| 17 |
+
pattern_radius : float
|
| 18 |
+
Radius of circle centers
|
| 19 |
+
circle_radius : float
|
| 20 |
+
The radius of each circle
|
| 21 |
+
count : int
|
| 22 |
+
Number of circles in the pattern
|
| 23 |
+
center : (2,) float
|
| 24 |
+
Center of pattern
|
| 25 |
+
angle : float
|
| 26 |
+
If defined pattern will span this angle
|
| 27 |
+
If None, pattern will be evenly spaced
|
| 28 |
+
|
| 29 |
+
Returns
|
| 30 |
+
-------------
|
| 31 |
+
pattern : trimesh.path.Path2D
|
| 32 |
+
Path containing circular pattern
|
| 33 |
+
"""
|
| 34 |
+
from .path import Path2D
|
| 35 |
+
|
| 36 |
+
if angle is None:
|
| 37 |
+
angles = np.linspace(0.0, np.pi * 2.0, count + 1)[:-1]
|
| 38 |
+
elif isinstance(angle, float) or isinstance(angle, int):
|
| 39 |
+
angles = np.linspace(0.0, angle, count)
|
| 40 |
+
else:
|
| 41 |
+
raise ValueError("angle must be float or int!")
|
| 42 |
+
|
| 43 |
+
if center is None:
|
| 44 |
+
center = [0.0, 0.0]
|
| 45 |
+
|
| 46 |
+
# centers of circles
|
| 47 |
+
centers = np.column_stack((np.cos(angles), np.sin(angles))) * pattern_radius
|
| 48 |
+
|
| 49 |
+
vert = []
|
| 50 |
+
ents = []
|
| 51 |
+
for circle_center in centers:
|
| 52 |
+
# (3,3) center points of arc
|
| 53 |
+
three = arc.to_threepoint(
|
| 54 |
+
angles=[0, np.pi], center=circle_center, radius=circle_radius
|
| 55 |
+
)
|
| 56 |
+
# add a single circle entity
|
| 57 |
+
ents.append(Arc(points=np.arange(3) + len(vert), closed=True))
|
| 58 |
+
# keep flat array by extend instead of append
|
| 59 |
+
vert.extend(three)
|
| 60 |
+
|
| 61 |
+
# translate vertices to pattern center
|
| 62 |
+
vert = np.array(vert) + center
|
| 63 |
+
pattern = Path2D(entities=ents, vertices=vert, **kwargs)
|
| 64 |
+
return pattern
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def circle(radius, center=None, **kwargs):
|
| 68 |
+
"""
|
| 69 |
+
Create a Path2D containing circle with the specified
|
| 70 |
+
radius.
|
| 71 |
+
|
| 72 |
+
Parameters
|
| 73 |
+
--------------
|
| 74 |
+
radius : float
|
| 75 |
+
The radius of the circle
|
| 76 |
+
center : None or (2,) float
|
| 77 |
+
Center of the circle, origin by default
|
| 78 |
+
** kwargs : dict
|
| 79 |
+
Passed to trimesh.path.Path2D constructor
|
| 80 |
+
|
| 81 |
+
Returns
|
| 82 |
+
-------------
|
| 83 |
+
circle : Path2D
|
| 84 |
+
Path containing specified circle
|
| 85 |
+
"""
|
| 86 |
+
from .path import Path2D
|
| 87 |
+
|
| 88 |
+
if center is None:
|
| 89 |
+
center = [0.0, 0.0]
|
| 90 |
+
else:
|
| 91 |
+
center = np.asanyarray(center, dtype=np.float64)
|
| 92 |
+
# make sure radius is a float
|
| 93 |
+
radius = float(radius)
|
| 94 |
+
|
| 95 |
+
# (3, 2) float, points on arc
|
| 96 |
+
three = arc.to_threepoint(angles=[0, np.pi], center=center, radius=radius)
|
| 97 |
+
# generate the path object
|
| 98 |
+
result = Path2D(
|
| 99 |
+
entities=[Arc(points=np.arange(3), closed=True)], vertices=three, **kwargs
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return result
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def rectangle(bounds, **kwargs):
|
| 106 |
+
"""
|
| 107 |
+
Create a Path2D containing a single or multiple rectangles
|
| 108 |
+
with the specified bounds.
|
| 109 |
+
|
| 110 |
+
Parameters
|
| 111 |
+
--------------
|
| 112 |
+
bounds : (2, 2) float, or (m, 2, 2) float
|
| 113 |
+
Minimum XY, Maximum XY
|
| 114 |
+
|
| 115 |
+
Returns
|
| 116 |
+
-------------
|
| 117 |
+
rect : Path2D
|
| 118 |
+
Path containing specified rectangles
|
| 119 |
+
"""
|
| 120 |
+
from .path import Path2D
|
| 121 |
+
|
| 122 |
+
# data should be float
|
| 123 |
+
bounds = np.asanyarray(bounds, dtype=np.float64)
|
| 124 |
+
|
| 125 |
+
# bounds are extents, re- shape to origin- centered rectangle
|
| 126 |
+
if bounds.shape == (2,):
|
| 127 |
+
half = np.abs(bounds) / 2.0
|
| 128 |
+
bounds = np.array([-half, half])
|
| 129 |
+
|
| 130 |
+
# should have one bounds or multiple bounds
|
| 131 |
+
if not (util.is_shape(bounds, (2, 2)) or util.is_shape(bounds, (-1, 2, 2))):
|
| 132 |
+
raise ValueError("bounds must be (m, 2, 2) or (2, 2)")
|
| 133 |
+
|
| 134 |
+
# hold Line objects
|
| 135 |
+
lines = []
|
| 136 |
+
# hold (n, 2) cartesian points
|
| 137 |
+
vertices = []
|
| 138 |
+
|
| 139 |
+
# loop through each rectangle
|
| 140 |
+
for lower, upper in bounds.reshape((-1, 2, 2)):
|
| 141 |
+
lines.append(Line((np.arange(5) % 4) + len(vertices)))
|
| 142 |
+
vertices.extend([lower, [upper[0], lower[1]], upper, [lower[0], upper[1]]])
|
| 143 |
+
|
| 144 |
+
# create the Path2D with specified rectangles
|
| 145 |
+
rect = Path2D(entities=lines, vertices=vertices, **kwargs)
|
| 146 |
+
|
| 147 |
+
return rect
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def box_outline(extents=None, transform=None, **kwargs):
|
| 151 |
+
"""
|
| 152 |
+
Return a cuboid.
|
| 153 |
+
|
| 154 |
+
Parameters
|
| 155 |
+
------------
|
| 156 |
+
extents : float, or (3,) float
|
| 157 |
+
Edge lengths
|
| 158 |
+
transform: (4, 4) float
|
| 159 |
+
Transformation matrix
|
| 160 |
+
**kwargs:
|
| 161 |
+
passed to Trimesh to create box
|
| 162 |
+
|
| 163 |
+
Returns
|
| 164 |
+
------------
|
| 165 |
+
geometry : trimesh.Path3D
|
| 166 |
+
Path outline of a cuboid geometry
|
| 167 |
+
"""
|
| 168 |
+
from .exchange.load import load_path
|
| 169 |
+
|
| 170 |
+
# create vertices for the box
|
| 171 |
+
vertices = [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1]
|
| 172 |
+
vertices = np.array(vertices, order="C", dtype=np.float64).reshape((-1, 3))
|
| 173 |
+
vertices -= 0.5
|
| 174 |
+
|
| 175 |
+
# resize the vertices based on passed size
|
| 176 |
+
if extents is not None:
|
| 177 |
+
extents = np.asanyarray(extents, dtype=np.float64)
|
| 178 |
+
if extents.shape != (3,):
|
| 179 |
+
raise ValueError("Extents must be (3,)!")
|
| 180 |
+
vertices *= extents
|
| 181 |
+
|
| 182 |
+
# apply transform if passed
|
| 183 |
+
if transform is not None:
|
| 184 |
+
vertices = transformations.transform_points(vertices, transform)
|
| 185 |
+
|
| 186 |
+
# vertex indices
|
| 187 |
+
indices = [0, 1, 3, 2, 0, 4, 5, 7, 6, 4, 0, 2, 6, 7, 3, 1, 5]
|
| 188 |
+
outline = load_path(vertices[indices])
|
| 189 |
+
|
| 190 |
+
return outline
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def grid(
|
| 194 |
+
side,
|
| 195 |
+
count=5,
|
| 196 |
+
transform=None,
|
| 197 |
+
plane_origin=None,
|
| 198 |
+
plane_normal=None,
|
| 199 |
+
include_circle=True,
|
| 200 |
+
sections_circle=32,
|
| 201 |
+
):
|
| 202 |
+
"""
|
| 203 |
+
Create a Path3D for a grid visualization of a plane.
|
| 204 |
+
|
| 205 |
+
Parameters
|
| 206 |
+
-----------
|
| 207 |
+
side : float
|
| 208 |
+
Length of half of a grid side
|
| 209 |
+
count : int
|
| 210 |
+
Number of grid lines per grid half
|
| 211 |
+
transform : None or (4, 4) float
|
| 212 |
+
Transformation matrix to move grid location.
|
| 213 |
+
Takes precedence over plane_origin if both are passed.
|
| 214 |
+
plane_origin : None or (3,) float
|
| 215 |
+
Plane origin
|
| 216 |
+
plane_normal : None or (3,) float
|
| 217 |
+
Unit normal vector
|
| 218 |
+
include_circle : bool
|
| 219 |
+
Include a circular pattern inside the grid
|
| 220 |
+
sections_circle : int
|
| 221 |
+
How many sections should the smallest circle have
|
| 222 |
+
|
| 223 |
+
Returns
|
| 224 |
+
----------
|
| 225 |
+
grid : trimesh.path.Path3D
|
| 226 |
+
Path containing grid plane visualization
|
| 227 |
+
"""
|
| 228 |
+
from .path import Path3D
|
| 229 |
+
|
| 230 |
+
# change full side length to half-side
|
| 231 |
+
side = float(side)
|
| 232 |
+
# make sure count is an integer
|
| 233 |
+
count = int(count)
|
| 234 |
+
# get a spaced sequence of radius
|
| 235 |
+
radii = np.linspace(0.0, side, count + 1)[1:]
|
| 236 |
+
# what's the maximum radius
|
| 237 |
+
rmax = radii[-1]
|
| 238 |
+
|
| 239 |
+
# keep a count of the current vertex count
|
| 240 |
+
current = 0
|
| 241 |
+
# collect vertices and entities
|
| 242 |
+
vertices = []
|
| 243 |
+
entities = []
|
| 244 |
+
for r in radii:
|
| 245 |
+
if include_circle:
|
| 246 |
+
# scale the section count by radius
|
| 247 |
+
circle_res = int((r / radii[0]) * sections_circle)
|
| 248 |
+
# generate a circule pattern
|
| 249 |
+
theta = np.linspace(0.0, np.pi * 2, circle_res)
|
| 250 |
+
circle = np.column_stack((np.cos(theta), np.sin(theta))) * r
|
| 251 |
+
# append the circle pattern
|
| 252 |
+
vertices.append(circle)
|
| 253 |
+
entities.append(Line(points=np.arange(len(circle)) + current))
|
| 254 |
+
# keep the vertex count correct
|
| 255 |
+
current += len(circle)
|
| 256 |
+
# generate a series of grid lines
|
| 257 |
+
vertices.append(
|
| 258 |
+
[
|
| 259 |
+
[-rmax, r],
|
| 260 |
+
[rmax, r],
|
| 261 |
+
[-rmax, -r],
|
| 262 |
+
[rmax, -r],
|
| 263 |
+
[r, -rmax],
|
| 264 |
+
[r, rmax],
|
| 265 |
+
[-r, -rmax],
|
| 266 |
+
[-r, rmax],
|
| 267 |
+
]
|
| 268 |
+
)
|
| 269 |
+
# append an entity per grid line
|
| 270 |
+
for i in [0, 2, 4, 6]:
|
| 271 |
+
entities.append(Line(points=np.arange(2) + current + i))
|
| 272 |
+
current += len(vertices[-1])
|
| 273 |
+
|
| 274 |
+
# add the middle lines which were skipped
|
| 275 |
+
vertices.append([[0, rmax], [0, -rmax], [-rmax, 0], [rmax, 0]])
|
| 276 |
+
entities.append(Line(points=np.arange(2) + current))
|
| 277 |
+
entities.append(Line(points=np.arange(2) + current + 2))
|
| 278 |
+
# stack vertices into clean (n, 3) float
|
| 279 |
+
vertices = np.vstack(vertices)
|
| 280 |
+
|
| 281 |
+
# if plane was passed instead of transform create the matrix here
|
| 282 |
+
if transform is None and plane_origin is not None and plane_normal is not None:
|
| 283 |
+
transform = np.linalg.inv(
|
| 284 |
+
plane_transform(origin=plane_origin, normal=plane_normal)
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
# stack vertices to 3D
|
| 288 |
+
vertices = np.column_stack((vertices, np.zeros(len(vertices))))
|
| 289 |
+
# apply transform if passed
|
| 290 |
+
if transform is not None:
|
| 291 |
+
vertices = transformations.transform_points(vertices, matrix=transform)
|
| 292 |
+
# combine result into a Path3D object
|
| 293 |
+
grid_path = Path3D(entities=entities, vertices=vertices)
|
| 294 |
+
return grid_path
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/curve.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from ..constants import res_path as res
|
| 4 |
+
from ..constants import tol_path as tol
|
| 5 |
+
from ..typed import Integer
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def discretize_bezier(points, count=None, scale=1.0):
|
| 9 |
+
"""
|
| 10 |
+
Parameters
|
| 11 |
+
----------
|
| 12 |
+
points : (order, dimension) float
|
| 13 |
+
Control points of the bezier curve
|
| 14 |
+
For a 2D cubic bezier, order=3, dimension=2
|
| 15 |
+
count : int, or None
|
| 16 |
+
Number of segments
|
| 17 |
+
scale : float
|
| 18 |
+
Scale of curve
|
| 19 |
+
Returns
|
| 20 |
+
----------
|
| 21 |
+
discrete: (n, dimension) float
|
| 22 |
+
Points forming a a polyline representation
|
| 23 |
+
"""
|
| 24 |
+
# make sure we have a numpy array
|
| 25 |
+
points = np.asanyarray(points, dtype=np.float64)
|
| 26 |
+
|
| 27 |
+
if count is None:
|
| 28 |
+
# how much distance does a small percentage of the curve take
|
| 29 |
+
# this is so we can figure out how finely we have to sample t
|
| 30 |
+
norm = np.linalg.norm(np.diff(points, axis=0), axis=1).sum()
|
| 31 |
+
count = np.ceil(norm / (res.seg_frac * scale))
|
| 32 |
+
count = int(
|
| 33 |
+
np.clip(count, res.min_sections * len(points), res.max_sections * len(points))
|
| 34 |
+
)
|
| 35 |
+
count = int(count)
|
| 36 |
+
|
| 37 |
+
# parameterize incrementing 0.0 - 1.0
|
| 38 |
+
t = np.linspace(0.0, 1.0, count)
|
| 39 |
+
# decrementing 1.0-0.0
|
| 40 |
+
t_d = 1.0 - t
|
| 41 |
+
n = len(points) - 1
|
| 42 |
+
# binomial coefficients, i, and each point
|
| 43 |
+
iterable = zip(binomial(n), np.arange(len(points)), points)
|
| 44 |
+
# run the actual interpolation
|
| 45 |
+
stacked = [
|
| 46 |
+
((t**i) * (t_d ** (n - i))).reshape((-1, 1)) * p * c for c, i, p in iterable
|
| 47 |
+
]
|
| 48 |
+
result = np.sum(stacked, axis=0)
|
| 49 |
+
|
| 50 |
+
# a bezier curve always starts and ends on control points
|
| 51 |
+
if tol.strict:
|
| 52 |
+
# test to make sure end points are correct
|
| 53 |
+
test = np.sum((result[[0, -1]] - points[[0, -1]]) ** 2, axis=1)
|
| 54 |
+
assert (test < tol.merge).all()
|
| 55 |
+
assert len(result) >= 2
|
| 56 |
+
|
| 57 |
+
# snap the first and last points to the exact control point
|
| 58 |
+
result[[0, -1]] = points[[0, -1]]
|
| 59 |
+
|
| 60 |
+
return result
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def discretize_bspline(control, knots, count=None, scale=1.0):
|
| 64 |
+
"""
|
| 65 |
+
Given a B-Splines control points and knot vector, return
|
| 66 |
+
a sampled version of the curve.
|
| 67 |
+
|
| 68 |
+
Parameters
|
| 69 |
+
----------
|
| 70 |
+
control : (o, d) float
|
| 71 |
+
Control points of the b- spline
|
| 72 |
+
knots : (j,) float
|
| 73 |
+
B-spline knots
|
| 74 |
+
count : int
|
| 75 |
+
Number of line segments to discretize the spline
|
| 76 |
+
If not specified will be calculated as something reasonable
|
| 77 |
+
|
| 78 |
+
Returns
|
| 79 |
+
----------
|
| 80 |
+
discrete : (count, dimension) float
|
| 81 |
+
Points on a polyline version of the B-spline
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
# evaluate the b-spline using scipy/fitpack
|
| 85 |
+
from scipy.interpolate import splev
|
| 86 |
+
|
| 87 |
+
# (n, d) control points where d is the dimension of vertices
|
| 88 |
+
control = np.asanyarray(control, dtype=np.float64)
|
| 89 |
+
degree = len(knots) - len(control) - 1
|
| 90 |
+
if count is None:
|
| 91 |
+
norm = np.linalg.norm(np.diff(control, axis=0), axis=1).sum()
|
| 92 |
+
count = int(
|
| 93 |
+
np.clip(
|
| 94 |
+
norm / (res.seg_frac * scale),
|
| 95 |
+
res.min_sections * len(control),
|
| 96 |
+
res.max_sections * len(control),
|
| 97 |
+
)
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
ipl = np.linspace(knots[0], knots[-1], count)
|
| 101 |
+
discrete = splev(ipl, [knots, control.T, degree])
|
| 102 |
+
discrete = np.column_stack(discrete)
|
| 103 |
+
|
| 104 |
+
return discrete
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def binomial(n: Integer) -> list:
|
| 108 |
+
"""
|
| 109 |
+
Return all binomial coefficients for a given order.
|
| 110 |
+
|
| 111 |
+
For n > 5, scipy.special.binom is used, below we hardcode.
|
| 112 |
+
|
| 113 |
+
Parameters
|
| 114 |
+
--------------
|
| 115 |
+
n : int
|
| 116 |
+
Order of binomial
|
| 117 |
+
|
| 118 |
+
Returns
|
| 119 |
+
---------------
|
| 120 |
+
binom : (n + 1,) int
|
| 121 |
+
Binomial coefficients of a given order
|
| 122 |
+
"""
|
| 123 |
+
if n == 1:
|
| 124 |
+
return [1, 1]
|
| 125 |
+
elif n == 2:
|
| 126 |
+
return [1, 2, 1]
|
| 127 |
+
elif n == 3:
|
| 128 |
+
return [1, 3, 3, 1]
|
| 129 |
+
elif n == 4:
|
| 130 |
+
return [1, 4, 6, 4, 1]
|
| 131 |
+
elif n == 5:
|
| 132 |
+
return [1, 5, 10, 10, 5, 1]
|
| 133 |
+
else:
|
| 134 |
+
from scipy.special import binom
|
| 135 |
+
|
| 136 |
+
return binom(n, np.arange(n + 1))
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/entities.py
ADDED
|
@@ -0,0 +1,838 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
entities.py
|
| 3 |
+
--------------
|
| 4 |
+
|
| 5 |
+
Basic geometric primitives which only store references to
|
| 6 |
+
vertex indices rather than vertices themselves.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from copy import deepcopy
|
| 10 |
+
from logging import getLogger
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
from .. import util
|
| 15 |
+
from ..util import ABC
|
| 16 |
+
from .arc import arc_center, discretize_arc
|
| 17 |
+
from .curve import discretize_bezier, discretize_bspline
|
| 18 |
+
|
| 19 |
+
log = getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class Entity(ABC):
|
| 23 |
+
def __init__(
|
| 24 |
+
self, points, closed=None, layer=None, metadata=None, color=None, **kwargs
|
| 25 |
+
):
|
| 26 |
+
# points always reference vertex indices and are int
|
| 27 |
+
self.points = np.asanyarray(points, dtype=np.int64)
|
| 28 |
+
# save explicit closed
|
| 29 |
+
if closed is not None:
|
| 30 |
+
self.closed = closed
|
| 31 |
+
# save the passed layer
|
| 32 |
+
if layer is not None:
|
| 33 |
+
self.layer = layer
|
| 34 |
+
if metadata is not None:
|
| 35 |
+
self.metadata.update(metadata)
|
| 36 |
+
|
| 37 |
+
self._cache = {}
|
| 38 |
+
|
| 39 |
+
# save the passed color
|
| 40 |
+
self.color = color
|
| 41 |
+
# save any other kwargs for general use
|
| 42 |
+
self.kwargs = kwargs
|
| 43 |
+
|
| 44 |
+
@property
|
| 45 |
+
def metadata(self):
|
| 46 |
+
"""
|
| 47 |
+
Get any metadata about the entity.
|
| 48 |
+
|
| 49 |
+
Returns
|
| 50 |
+
---------
|
| 51 |
+
metadata : dict
|
| 52 |
+
Bag of properties.
|
| 53 |
+
"""
|
| 54 |
+
if not hasattr(self, "_metadata"):
|
| 55 |
+
self._metadata = {}
|
| 56 |
+
# note that we don't let a new dict be assigned
|
| 57 |
+
return self._metadata
|
| 58 |
+
|
| 59 |
+
@property
|
| 60 |
+
def layer(self):
|
| 61 |
+
"""
|
| 62 |
+
Set the layer the entity resides on as a shortcut
|
| 63 |
+
to putting it in the entity metadata.
|
| 64 |
+
|
| 65 |
+
Returns
|
| 66 |
+
----------
|
| 67 |
+
layer : any
|
| 68 |
+
Hashable layer identifier.
|
| 69 |
+
"""
|
| 70 |
+
return self.metadata.get("layer")
|
| 71 |
+
|
| 72 |
+
@layer.setter
|
| 73 |
+
def layer(self, value):
|
| 74 |
+
"""
|
| 75 |
+
Set the current layer of the entity.
|
| 76 |
+
|
| 77 |
+
Returns
|
| 78 |
+
----------
|
| 79 |
+
layer : any
|
| 80 |
+
Hashable layer indicator
|
| 81 |
+
"""
|
| 82 |
+
self.metadata["layer"] = value
|
| 83 |
+
|
| 84 |
+
def to_dict(self) -> dict:
|
| 85 |
+
"""
|
| 86 |
+
Returns a dictionary with all of the information
|
| 87 |
+
about the entity.
|
| 88 |
+
|
| 89 |
+
Returns
|
| 90 |
+
-----------
|
| 91 |
+
as_dict : dict
|
| 92 |
+
Has keys 'type', 'points', 'closed'
|
| 93 |
+
"""
|
| 94 |
+
return {
|
| 95 |
+
"type": self.__class__.__name__,
|
| 96 |
+
"points": self.points.tolist(),
|
| 97 |
+
"closed": self.closed,
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
@property
|
| 101 |
+
def closed(self):
|
| 102 |
+
"""
|
| 103 |
+
If the first point is the same as the end point
|
| 104 |
+
the entity is closed
|
| 105 |
+
|
| 106 |
+
Returns
|
| 107 |
+
-----------
|
| 108 |
+
closed : bool
|
| 109 |
+
Is the entity closed or not?
|
| 110 |
+
"""
|
| 111 |
+
return len(self.points) > 2 and self.points[0] == self.points[-1]
|
| 112 |
+
|
| 113 |
+
@property
|
| 114 |
+
def nodes(self):
|
| 115 |
+
"""
|
| 116 |
+
Returns an (n,2) list of nodes, or vertices on the path.
|
| 117 |
+
Note that this generic class function assumes that all of the
|
| 118 |
+
reference points are on the path which is true for lines and
|
| 119 |
+
three point arcs.
|
| 120 |
+
|
| 121 |
+
If you were to define another class where that wasn't the case
|
| 122 |
+
(for example, the control points of a bezier curve),
|
| 123 |
+
you would need to implement an entity- specific version of this
|
| 124 |
+
function.
|
| 125 |
+
|
| 126 |
+
The purpose of having a list of nodes is so that they can then be
|
| 127 |
+
added as edges to a graph so we can use functions to check
|
| 128 |
+
connectivity, extract paths, etc.
|
| 129 |
+
|
| 130 |
+
The slicing on this function is essentially just tiling points
|
| 131 |
+
so the first and last vertices aren't repeated. Example:
|
| 132 |
+
|
| 133 |
+
self.points = [0,1,2]
|
| 134 |
+
returns: [[0,1], [1,2]]
|
| 135 |
+
"""
|
| 136 |
+
return (
|
| 137 |
+
np.column_stack((self.points, self.points)).reshape(-1)[1:-1].reshape((-1, 2))
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
@property
|
| 141 |
+
def end_points(self):
|
| 142 |
+
"""
|
| 143 |
+
Returns the first and last points. Also note that if you
|
| 144 |
+
define a new entity class where the first and last vertices
|
| 145 |
+
in self.points aren't the endpoints of the curve you need to
|
| 146 |
+
implement this function for your class.
|
| 147 |
+
|
| 148 |
+
Returns
|
| 149 |
+
-------------
|
| 150 |
+
ends : (2,) int
|
| 151 |
+
Indices of the two end points of the entity
|
| 152 |
+
"""
|
| 153 |
+
return self.points[[0, -1]]
|
| 154 |
+
|
| 155 |
+
@property
|
| 156 |
+
def is_valid(self):
|
| 157 |
+
"""
|
| 158 |
+
Is the current entity valid.
|
| 159 |
+
|
| 160 |
+
Returns
|
| 161 |
+
-----------
|
| 162 |
+
valid : bool
|
| 163 |
+
Is the current entity well formed
|
| 164 |
+
"""
|
| 165 |
+
return True
|
| 166 |
+
|
| 167 |
+
def reverse(self, direction=-1):
|
| 168 |
+
"""
|
| 169 |
+
Reverse the current entity in place.
|
| 170 |
+
|
| 171 |
+
Parameters
|
| 172 |
+
----------------
|
| 173 |
+
direction : int
|
| 174 |
+
If positive will not touch direction
|
| 175 |
+
If negative will reverse self.points
|
| 176 |
+
"""
|
| 177 |
+
if direction < 0:
|
| 178 |
+
self._direction = -1
|
| 179 |
+
else:
|
| 180 |
+
self._direction = 1
|
| 181 |
+
|
| 182 |
+
def _orient(self, curve):
|
| 183 |
+
"""
|
| 184 |
+
Reverse a curve if a flag is set.
|
| 185 |
+
|
| 186 |
+
Parameters
|
| 187 |
+
--------------
|
| 188 |
+
curve : (n, dimension) float
|
| 189 |
+
Curve made up of line segments in space
|
| 190 |
+
|
| 191 |
+
Returns
|
| 192 |
+
------------
|
| 193 |
+
orient : (n, dimension) float
|
| 194 |
+
Original curve, but possibly reversed
|
| 195 |
+
"""
|
| 196 |
+
if hasattr(self, "_direction") and self._direction < 0:
|
| 197 |
+
return curve[::-1]
|
| 198 |
+
return curve
|
| 199 |
+
|
| 200 |
+
def bounds(self, vertices):
|
| 201 |
+
"""
|
| 202 |
+
Return the AABB of the current entity.
|
| 203 |
+
|
| 204 |
+
Parameters
|
| 205 |
+
-----------
|
| 206 |
+
vertices : (n, dimension) float
|
| 207 |
+
Vertices in space
|
| 208 |
+
|
| 209 |
+
Returns
|
| 210 |
+
-----------
|
| 211 |
+
bounds : (2, dimension) float
|
| 212 |
+
Coordinates of AABB, in (min, max) form
|
| 213 |
+
"""
|
| 214 |
+
bounds = np.array(
|
| 215 |
+
[vertices[self.points].min(axis=0), vertices[self.points].max(axis=0)]
|
| 216 |
+
)
|
| 217 |
+
return bounds
|
| 218 |
+
|
| 219 |
+
def length(self, vertices):
|
| 220 |
+
"""
|
| 221 |
+
Return the total length of the entity.
|
| 222 |
+
|
| 223 |
+
Parameters
|
| 224 |
+
--------------
|
| 225 |
+
vertices : (n, dimension) float
|
| 226 |
+
Vertices in space
|
| 227 |
+
|
| 228 |
+
Returns
|
| 229 |
+
---------
|
| 230 |
+
length : float
|
| 231 |
+
Total length of entity
|
| 232 |
+
"""
|
| 233 |
+
diff = np.diff(self.discrete(vertices), axis=0) ** 2
|
| 234 |
+
length = (np.dot(diff, [1] * vertices.shape[1]) ** 0.5).sum()
|
| 235 |
+
return length
|
| 236 |
+
|
| 237 |
+
def explode(self):
|
| 238 |
+
"""
|
| 239 |
+
Split the entity into multiple entities.
|
| 240 |
+
|
| 241 |
+
Returns
|
| 242 |
+
------------
|
| 243 |
+
explode : list of Entity
|
| 244 |
+
Current entity split into multiple entities.
|
| 245 |
+
"""
|
| 246 |
+
return [self.copy()]
|
| 247 |
+
|
| 248 |
+
def copy(self):
|
| 249 |
+
"""
|
| 250 |
+
Return a copy of the current entity.
|
| 251 |
+
|
| 252 |
+
Returns
|
| 253 |
+
------------
|
| 254 |
+
copied : Entity
|
| 255 |
+
Copy of current entity
|
| 256 |
+
"""
|
| 257 |
+
copied = deepcopy(self)
|
| 258 |
+
# only copy metadata if set
|
| 259 |
+
if hasattr(self, "_metadata"):
|
| 260 |
+
copied._metadata = deepcopy(self._metadata)
|
| 261 |
+
# check for very annoying subtle copy failures
|
| 262 |
+
assert id(copied._metadata) != id(self._metadata)
|
| 263 |
+
assert id(copied.points) != id(self.points)
|
| 264 |
+
return copied
|
| 265 |
+
|
| 266 |
+
def __hash__(self):
|
| 267 |
+
"""
|
| 268 |
+
Return a hash that represents the current entity.
|
| 269 |
+
|
| 270 |
+
Returns
|
| 271 |
+
----------
|
| 272 |
+
hashed : int
|
| 273 |
+
Hash of current class name, points, and closed
|
| 274 |
+
"""
|
| 275 |
+
return hash(self._bytes())
|
| 276 |
+
|
| 277 |
+
def _bytes(self):
|
| 278 |
+
"""
|
| 279 |
+
Get hashable bytes that define the current entity.
|
| 280 |
+
|
| 281 |
+
Returns
|
| 282 |
+
------------
|
| 283 |
+
data : bytes
|
| 284 |
+
Hashable data defining the current entity
|
| 285 |
+
"""
|
| 286 |
+
# give consistent ordering of points for hash
|
| 287 |
+
if self.points[0] > self.points[-1]:
|
| 288 |
+
return self.__class__.__name__.encode("utf-8") + self.points.tobytes()
|
| 289 |
+
else:
|
| 290 |
+
return self.__class__.__name__.encode("utf-8") + self.points[::-1].tobytes()
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
class Text(Entity):
|
| 294 |
+
"""
|
| 295 |
+
Text to annotate a 2D or 3D path.
|
| 296 |
+
"""
|
| 297 |
+
|
| 298 |
+
def __init__(
|
| 299 |
+
self,
|
| 300 |
+
origin,
|
| 301 |
+
text,
|
| 302 |
+
height=None,
|
| 303 |
+
vector=None,
|
| 304 |
+
normal=None,
|
| 305 |
+
align=None,
|
| 306 |
+
layer=None,
|
| 307 |
+
color=None,
|
| 308 |
+
metadata=None,
|
| 309 |
+
):
|
| 310 |
+
"""
|
| 311 |
+
An entity for text labels.
|
| 312 |
+
|
| 313 |
+
Parameters
|
| 314 |
+
--------------
|
| 315 |
+
origin : int
|
| 316 |
+
Index of a single vertex for text origin
|
| 317 |
+
text : str
|
| 318 |
+
The text to label
|
| 319 |
+
height : float or None
|
| 320 |
+
The height of text
|
| 321 |
+
vector : int or None
|
| 322 |
+
An vertex index for which direction text
|
| 323 |
+
is written along unitized: vector - origin
|
| 324 |
+
normal : int or None
|
| 325 |
+
A vertex index for the plane normal:
|
| 326 |
+
vector is along unitized: normal - origin
|
| 327 |
+
align : (2,) str or None
|
| 328 |
+
Where to draw from for [horizontal, vertical]:
|
| 329 |
+
'center', 'left', 'right'
|
| 330 |
+
"""
|
| 331 |
+
# where is text placed
|
| 332 |
+
self.origin = origin
|
| 333 |
+
# what direction is the text pointing
|
| 334 |
+
self.vector = vector
|
| 335 |
+
# what is the normal of the text plane
|
| 336 |
+
self.normal = normal
|
| 337 |
+
# how high is the text entity
|
| 338 |
+
self.height = height
|
| 339 |
+
# what layer is the entity on
|
| 340 |
+
if layer is not None:
|
| 341 |
+
self.layer = layer
|
| 342 |
+
|
| 343 |
+
if metadata is not None:
|
| 344 |
+
self.metadata.update(metadata)
|
| 345 |
+
|
| 346 |
+
# what color is the entity
|
| 347 |
+
self.color = color
|
| 348 |
+
|
| 349 |
+
# None or (2,) str
|
| 350 |
+
if align is None:
|
| 351 |
+
# if not set make everything centered
|
| 352 |
+
align = ["center", "center"]
|
| 353 |
+
elif isinstance(align, str):
|
| 354 |
+
# if only one is passed set for both
|
| 355 |
+
# horizontal and vertical
|
| 356 |
+
align = [align, align]
|
| 357 |
+
elif len(align) != 2:
|
| 358 |
+
# otherwise raise rror
|
| 359 |
+
raise ValueError("align must be (2,) str")
|
| 360 |
+
|
| 361 |
+
self.align = align
|
| 362 |
+
|
| 363 |
+
# make sure text is a string
|
| 364 |
+
if hasattr(text, "decode"):
|
| 365 |
+
self.text = text.decode("utf-8")
|
| 366 |
+
else:
|
| 367 |
+
self.text = str(text)
|
| 368 |
+
|
| 369 |
+
@property
|
| 370 |
+
def origin(self):
|
| 371 |
+
"""
|
| 372 |
+
The origin point of the text.
|
| 373 |
+
|
| 374 |
+
Returns
|
| 375 |
+
-----------
|
| 376 |
+
origin : int
|
| 377 |
+
Index of vertices
|
| 378 |
+
"""
|
| 379 |
+
return self.points[0]
|
| 380 |
+
|
| 381 |
+
@origin.setter
|
| 382 |
+
def origin(self, value):
|
| 383 |
+
value = int(value)
|
| 384 |
+
if not hasattr(self, "points") or np.ptp(self.points) == 0:
|
| 385 |
+
self.points = np.ones(3, dtype=np.int64) * value
|
| 386 |
+
else:
|
| 387 |
+
self.points[0] = value
|
| 388 |
+
|
| 389 |
+
@property
|
| 390 |
+
def vector(self):
|
| 391 |
+
"""
|
| 392 |
+
A point representing the text direction
|
| 393 |
+
along the vector: vertices[vector] - vertices[origin]
|
| 394 |
+
|
| 395 |
+
Returns
|
| 396 |
+
----------
|
| 397 |
+
vector : int
|
| 398 |
+
Index of vertex
|
| 399 |
+
"""
|
| 400 |
+
return self.points[1]
|
| 401 |
+
|
| 402 |
+
@vector.setter
|
| 403 |
+
def vector(self, value):
|
| 404 |
+
if value is None:
|
| 405 |
+
return
|
| 406 |
+
self.points[1] = int(value)
|
| 407 |
+
|
| 408 |
+
@property
|
| 409 |
+
def normal(self):
|
| 410 |
+
"""
|
| 411 |
+
A point representing the plane normal along the
|
| 412 |
+
vector: vertices[normal] - vertices[origin]
|
| 413 |
+
|
| 414 |
+
Returns
|
| 415 |
+
------------
|
| 416 |
+
normal : int
|
| 417 |
+
Index of vertex
|
| 418 |
+
"""
|
| 419 |
+
return self.points[2]
|
| 420 |
+
|
| 421 |
+
@normal.setter
|
| 422 |
+
def normal(self, value):
|
| 423 |
+
if value is None:
|
| 424 |
+
return
|
| 425 |
+
self.points[2] = int(value)
|
| 426 |
+
|
| 427 |
+
def plot(self, vertices, show=False):
|
| 428 |
+
"""
|
| 429 |
+
Plot the text using matplotlib.
|
| 430 |
+
|
| 431 |
+
Parameters
|
| 432 |
+
--------------
|
| 433 |
+
vertices : (n, 2) float
|
| 434 |
+
Vertices in space
|
| 435 |
+
show : bool
|
| 436 |
+
If True, call plt.show()
|
| 437 |
+
"""
|
| 438 |
+
if vertices.shape[1] != 2:
|
| 439 |
+
raise ValueError("only for 2D points!")
|
| 440 |
+
|
| 441 |
+
import matplotlib.pyplot as plt # noqa
|
| 442 |
+
|
| 443 |
+
# get rotation angle in degrees
|
| 444 |
+
angle = np.degrees(self.angle(vertices))
|
| 445 |
+
|
| 446 |
+
# TODO: handle text size better
|
| 447 |
+
plt.text(
|
| 448 |
+
*vertices[self.origin],
|
| 449 |
+
s=self.text,
|
| 450 |
+
rotation=angle,
|
| 451 |
+
ha=self.align[0],
|
| 452 |
+
va=self.align[1],
|
| 453 |
+
size=18,
|
| 454 |
+
)
|
| 455 |
+
|
| 456 |
+
if show:
|
| 457 |
+
plt.show()
|
| 458 |
+
|
| 459 |
+
def angle(self, vertices):
|
| 460 |
+
"""
|
| 461 |
+
If Text is 2D, get the rotation angle in radians.
|
| 462 |
+
|
| 463 |
+
Parameters
|
| 464 |
+
-----------
|
| 465 |
+
vertices : (n, 2) float
|
| 466 |
+
Vertices in space referenced by self.points
|
| 467 |
+
|
| 468 |
+
Returns
|
| 469 |
+
---------
|
| 470 |
+
angle : float
|
| 471 |
+
Rotation angle in radians
|
| 472 |
+
"""
|
| 473 |
+
|
| 474 |
+
if vertices.shape[1] != 2:
|
| 475 |
+
raise ValueError("angle only valid for 2D points!")
|
| 476 |
+
|
| 477 |
+
# get the vector from origin
|
| 478 |
+
direction = vertices[self.vector] - vertices[self.origin]
|
| 479 |
+
# get the rotation angle in radians
|
| 480 |
+
angle = np.arctan2(*direction[::-1])
|
| 481 |
+
|
| 482 |
+
return angle
|
| 483 |
+
|
| 484 |
+
def length(self, vertices):
|
| 485 |
+
return 0.0
|
| 486 |
+
|
| 487 |
+
def discrete(self, *args, **kwargs):
|
| 488 |
+
return np.array([])
|
| 489 |
+
|
| 490 |
+
@property
|
| 491 |
+
def closed(self):
|
| 492 |
+
return False
|
| 493 |
+
|
| 494 |
+
@property
|
| 495 |
+
def is_valid(self):
|
| 496 |
+
return True
|
| 497 |
+
|
| 498 |
+
@property
|
| 499 |
+
def nodes(self):
|
| 500 |
+
return np.array([])
|
| 501 |
+
|
| 502 |
+
@property
|
| 503 |
+
def end_points(self):
|
| 504 |
+
return np.array([])
|
| 505 |
+
|
| 506 |
+
def _bytes(self):
|
| 507 |
+
data = b"".join([b"Text", self.points.tobytes(), self.text.encode("utf-8")])
|
| 508 |
+
return data
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
class Line(Entity):
|
| 512 |
+
"""
|
| 513 |
+
A line or poly-line entity
|
| 514 |
+
"""
|
| 515 |
+
|
| 516 |
+
def discrete(self, vertices, scale=1.0):
|
| 517 |
+
"""
|
| 518 |
+
Discretize into a world- space path.
|
| 519 |
+
|
| 520 |
+
Parameters
|
| 521 |
+
------------
|
| 522 |
+
vertices: (n, dimension) float
|
| 523 |
+
Points in space
|
| 524 |
+
scale : float
|
| 525 |
+
Size of overall scene for numerical comparisons
|
| 526 |
+
|
| 527 |
+
Returns
|
| 528 |
+
-------------
|
| 529 |
+
discrete: (m, dimension) float
|
| 530 |
+
Path in space composed of line segments
|
| 531 |
+
"""
|
| 532 |
+
return self._orient(vertices[self.points])
|
| 533 |
+
|
| 534 |
+
@property
|
| 535 |
+
def closed(self):
|
| 536 |
+
return len(self.points) > 2 and self.points[0] == self.points[-1]
|
| 537 |
+
|
| 538 |
+
@closed.setter
|
| 539 |
+
def closed(self, value: bool):
|
| 540 |
+
current = self.points[0] == self.points[-1]
|
| 541 |
+
if value and not current:
|
| 542 |
+
# case where we've been asked to close the line
|
| 543 |
+
# this seems pretty obvious that we should just append the first pointOB
|
| 544 |
+
self.points = np.concatenate((self.points, [self.points[0]]))
|
| 545 |
+
elif not value and current:
|
| 546 |
+
# case where we've been asked to *disconnect* a closed path
|
| 547 |
+
log.debug("ignoring `Line.closed = False`")
|
| 548 |
+
|
| 549 |
+
@property
|
| 550 |
+
def is_valid(self):
|
| 551 |
+
"""
|
| 552 |
+
Is the current entity valid.
|
| 553 |
+
|
| 554 |
+
Returns
|
| 555 |
+
-----------
|
| 556 |
+
valid : bool
|
| 557 |
+
Is the current entity well formed
|
| 558 |
+
"""
|
| 559 |
+
valid = np.any((self.points - self.points[0]) != 0)
|
| 560 |
+
return valid
|
| 561 |
+
|
| 562 |
+
def explode(self):
|
| 563 |
+
"""
|
| 564 |
+
If the current Line entity consists of multiple line
|
| 565 |
+
break it up into n Line entities.
|
| 566 |
+
|
| 567 |
+
Returns
|
| 568 |
+
----------
|
| 569 |
+
exploded: (n,) Line entities
|
| 570 |
+
"""
|
| 571 |
+
# copy over the current layer
|
| 572 |
+
layer = self.layer
|
| 573 |
+
points = (
|
| 574 |
+
np.column_stack((self.points, self.points)).ravel()[1:-1].reshape((-1, 2))
|
| 575 |
+
)
|
| 576 |
+
exploded = [Line(i, layer=layer) for i in points]
|
| 577 |
+
return exploded
|
| 578 |
+
|
| 579 |
+
def _bytes(self):
|
| 580 |
+
# give consistent ordering of points for hash
|
| 581 |
+
if self.points[0] > self.points[-1]:
|
| 582 |
+
return b"Line" + self.points.tobytes()
|
| 583 |
+
else:
|
| 584 |
+
return b"Line" + self.points[::-1].tobytes()
|
| 585 |
+
|
| 586 |
+
def to_dict(self) -> dict:
|
| 587 |
+
"""
|
| 588 |
+
Returns a dictionary with all of the information
|
| 589 |
+
about the Line. `closed` is not additional information
|
| 590 |
+
for a Line like it is for Arc where the value determines
|
| 591 |
+
if it is a partial or complete circle. Rather it is a check
|
| 592 |
+
which indicates the first and last points are identical,
|
| 593 |
+
and thus should not be included in the export
|
| 594 |
+
|
| 595 |
+
Returns
|
| 596 |
+
-----------
|
| 597 |
+
as_dict
|
| 598 |
+
Has keys 'type', 'points'
|
| 599 |
+
"""
|
| 600 |
+
return {
|
| 601 |
+
"type": self.__class__.__name__,
|
| 602 |
+
"points": self.points.tolist(),
|
| 603 |
+
}
|
| 604 |
+
|
| 605 |
+
|
| 606 |
+
class Arc(Entity):
|
| 607 |
+
@property
|
| 608 |
+
def closed(self):
|
| 609 |
+
"""
|
| 610 |
+
A boolean flag for whether the arc is closed (a circle) or not.
|
| 611 |
+
|
| 612 |
+
Returns
|
| 613 |
+
----------
|
| 614 |
+
closed : bool
|
| 615 |
+
If set True, Arc will be a closed circle
|
| 616 |
+
"""
|
| 617 |
+
return getattr(self, "_closed", False)
|
| 618 |
+
|
| 619 |
+
@closed.setter
|
| 620 |
+
def closed(self, value):
|
| 621 |
+
"""
|
| 622 |
+
Set the Arc to be closed or not, without
|
| 623 |
+
changing the control points
|
| 624 |
+
|
| 625 |
+
Parameters
|
| 626 |
+
------------
|
| 627 |
+
value : bool
|
| 628 |
+
Should this Arc be a closed circle or not
|
| 629 |
+
"""
|
| 630 |
+
self._closed = bool(value)
|
| 631 |
+
|
| 632 |
+
@property
|
| 633 |
+
def is_valid(self):
|
| 634 |
+
"""
|
| 635 |
+
Is the current Arc entity valid.
|
| 636 |
+
|
| 637 |
+
Returns
|
| 638 |
+
-----------
|
| 639 |
+
valid : bool
|
| 640 |
+
Does the current Arc have exactly 3 control points
|
| 641 |
+
"""
|
| 642 |
+
return len(np.unique(self.points)) == 3
|
| 643 |
+
|
| 644 |
+
def _bytes(self):
|
| 645 |
+
# give consistent ordering of points for hash
|
| 646 |
+
order = int(self.points[0] > self.points[-1]) * 2 - 1
|
| 647 |
+
return b"Arc" + bytes(self.closed) + self.points[::order].tobytes()
|
| 648 |
+
|
| 649 |
+
def length(self, vertices):
|
| 650 |
+
"""
|
| 651 |
+
Return the arc length of the 3-point arc.
|
| 652 |
+
|
| 653 |
+
Parameter
|
| 654 |
+
----------
|
| 655 |
+
vertices : (n, d) float
|
| 656 |
+
Vertices for overall drawing.
|
| 657 |
+
|
| 658 |
+
Returns
|
| 659 |
+
-----------
|
| 660 |
+
length : float
|
| 661 |
+
Length of arc.
|
| 662 |
+
"""
|
| 663 |
+
# find the actual radius and angle span
|
| 664 |
+
if self.closed:
|
| 665 |
+
# we don't need the angular span as
|
| 666 |
+
# it's indicated as a closed circle
|
| 667 |
+
fit = self.center(vertices, return_normal=False, return_angle=False)
|
| 668 |
+
return np.pi * fit.radius * 2
|
| 669 |
+
# get the angular span of the circular arc
|
| 670 |
+
fit = self.center(vertices, return_normal=False, return_angle=True)
|
| 671 |
+
return fit.span * fit.radius
|
| 672 |
+
|
| 673 |
+
def discrete(self, vertices, scale=1.0):
|
| 674 |
+
"""
|
| 675 |
+
Discretize the arc entity into line sections.
|
| 676 |
+
|
| 677 |
+
Parameters
|
| 678 |
+
------------
|
| 679 |
+
vertices : (n, dimension) float
|
| 680 |
+
Points in space
|
| 681 |
+
scale : float
|
| 682 |
+
Size of overall scene for numerical comparisons
|
| 683 |
+
|
| 684 |
+
Returns
|
| 685 |
+
-------------
|
| 686 |
+
discrete : (m, dimension) float
|
| 687 |
+
Path in space made up of line segments
|
| 688 |
+
"""
|
| 689 |
+
|
| 690 |
+
return self._orient(
|
| 691 |
+
discretize_arc(vertices[self.points], close=self.closed, scale=scale)
|
| 692 |
+
)
|
| 693 |
+
|
| 694 |
+
def center(self, vertices, **kwargs):
|
| 695 |
+
"""
|
| 696 |
+
Return the center information about the arc entity.
|
| 697 |
+
|
| 698 |
+
Parameters
|
| 699 |
+
-------------
|
| 700 |
+
vertices : (n, dimension) float
|
| 701 |
+
Vertices in space
|
| 702 |
+
|
| 703 |
+
Returns
|
| 704 |
+
-------------
|
| 705 |
+
info : dict
|
| 706 |
+
With keys: 'radius', 'center'
|
| 707 |
+
"""
|
| 708 |
+
return arc_center(vertices[self.points], **kwargs)
|
| 709 |
+
|
| 710 |
+
def bounds(self, vertices):
|
| 711 |
+
"""
|
| 712 |
+
Return the AABB of the arc entity.
|
| 713 |
+
|
| 714 |
+
Parameters
|
| 715 |
+
-----------
|
| 716 |
+
vertices: (n, dimension) float
|
| 717 |
+
Vertices in space
|
| 718 |
+
|
| 719 |
+
Returns
|
| 720 |
+
-----------
|
| 721 |
+
bounds : (2, dimension) float
|
| 722 |
+
Coordinates of AABB in (min, max) form
|
| 723 |
+
"""
|
| 724 |
+
if util.is_shape(vertices, (-1, 2)) and self.closed:
|
| 725 |
+
# if we have a closed arc (a circle), we can return the actual bounds
|
| 726 |
+
# this only works in two dimensions, otherwise this would return the
|
| 727 |
+
# AABB of an sphere
|
| 728 |
+
info = self.center(vertices, return_normal=False, return_angle=False)
|
| 729 |
+
bounds = np.array(
|
| 730 |
+
[info.center - info.radius, info.center + info.radius], dtype=np.float64
|
| 731 |
+
)
|
| 732 |
+
else:
|
| 733 |
+
# since the AABB of a partial arc is hard, approximate
|
| 734 |
+
# the bounds by just looking at the discrete values
|
| 735 |
+
discrete = self.discrete(vertices)
|
| 736 |
+
bounds = np.array(
|
| 737 |
+
[discrete.min(axis=0), discrete.max(axis=0)], dtype=np.float64
|
| 738 |
+
)
|
| 739 |
+
return bounds
|
| 740 |
+
|
| 741 |
+
|
| 742 |
+
class Curve(Entity):
|
| 743 |
+
"""
|
| 744 |
+
The parent class for all wild curves in space.
|
| 745 |
+
"""
|
| 746 |
+
|
| 747 |
+
@property
|
| 748 |
+
def nodes(self):
|
| 749 |
+
# a point midway through the curve
|
| 750 |
+
mid = self.points[len(self.points) // 2]
|
| 751 |
+
return [[self.points[0], mid], [mid, self.points[-1]]]
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
class Bezier(Curve):
|
| 755 |
+
"""
|
| 756 |
+
An open or closed Bezier curve
|
| 757 |
+
"""
|
| 758 |
+
|
| 759 |
+
def discrete(self, vertices, scale=1.0, count=None):
|
| 760 |
+
"""
|
| 761 |
+
Discretize the Bezier curve.
|
| 762 |
+
|
| 763 |
+
Parameters
|
| 764 |
+
-------------
|
| 765 |
+
vertices : (n, 2) or (n, 3) float
|
| 766 |
+
Points in space
|
| 767 |
+
scale : float
|
| 768 |
+
Scale of overall drawings (for precision)
|
| 769 |
+
count : int
|
| 770 |
+
Number of segments to return
|
| 771 |
+
|
| 772 |
+
Returns
|
| 773 |
+
-------------
|
| 774 |
+
discrete : (m, 2) or (m, 3) float
|
| 775 |
+
Curve as line segments
|
| 776 |
+
"""
|
| 777 |
+
return self._orient(
|
| 778 |
+
discretize_bezier(vertices[self.points], count=count, scale=scale)
|
| 779 |
+
)
|
| 780 |
+
|
| 781 |
+
|
| 782 |
+
class BSpline(Curve):
|
| 783 |
+
"""
|
| 784 |
+
An open or closed B- Spline.
|
| 785 |
+
"""
|
| 786 |
+
|
| 787 |
+
def __init__(self, points, knots, layer=None, metadata=None, color=None, **kwargs):
|
| 788 |
+
self.points = np.asanyarray(points, dtype=np.int64)
|
| 789 |
+
self.knots = np.asanyarray(knots, dtype=np.float64)
|
| 790 |
+
if layer is not None:
|
| 791 |
+
self.layer = layer
|
| 792 |
+
if metadata is not None:
|
| 793 |
+
self.metadata.update(metadata)
|
| 794 |
+
self._cache = {}
|
| 795 |
+
self.kwargs = kwargs
|
| 796 |
+
self.color = color
|
| 797 |
+
|
| 798 |
+
def discrete(self, vertices, count=None, scale=1.0):
|
| 799 |
+
"""
|
| 800 |
+
Discretize the B-Spline curve.
|
| 801 |
+
|
| 802 |
+
Parameters
|
| 803 |
+
-------------
|
| 804 |
+
vertices : (n, 2) or (n, 3) float
|
| 805 |
+
Points in space
|
| 806 |
+
scale : float
|
| 807 |
+
Scale of overall drawings (for precision)
|
| 808 |
+
count : int
|
| 809 |
+
Number of segments to return
|
| 810 |
+
|
| 811 |
+
Returns
|
| 812 |
+
-------------
|
| 813 |
+
discrete : (m, 2) or (m, 3) float
|
| 814 |
+
Curve as line segments
|
| 815 |
+
"""
|
| 816 |
+
discrete = discretize_bspline(
|
| 817 |
+
control=vertices[self.points], knots=self.knots, count=count, scale=scale
|
| 818 |
+
)
|
| 819 |
+
return self._orient(discrete)
|
| 820 |
+
|
| 821 |
+
def _bytes(self):
|
| 822 |
+
# give consistent ordering of points for hash
|
| 823 |
+
if self.points[0] > self.points[-1]:
|
| 824 |
+
return b"BSpline" + self.knots.tobytes() + self.points.tobytes()
|
| 825 |
+
else:
|
| 826 |
+
return b"BSpline" + self.knots[::-1].tobytes() + self.points[::-1].tobytes()
|
| 827 |
+
|
| 828 |
+
def to_dict(self) -> dict:
|
| 829 |
+
"""
|
| 830 |
+
Returns a dictionary with all of the information
|
| 831 |
+
about the entity.
|
| 832 |
+
"""
|
| 833 |
+
return {
|
| 834 |
+
"type": self.__class__.__name__,
|
| 835 |
+
"points": self.points.tolist(),
|
| 836 |
+
"knots": self.knots.tolist(),
|
| 837 |
+
"closed": self.closed,
|
| 838 |
+
}
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__init__.py
ADDED
|
File without changes
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (261 Bytes). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/dxf.cpython-311.pyc
ADDED
|
Binary file (41.2 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/export.cpython-311.pyc
ADDED
|
Binary file (3.12 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/load.cpython-311.pyc
ADDED
|
Binary file (3.97 kB). View file
|
|
|
physworld_p1_20261018/envs/p1/lib/python3.11/site-packages/trimesh/path/exchange/__pycache__/misc.cpython-311.pyc
ADDED
|
Binary file (8.3 kB). View file
|
|
|