|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2021-2025 DexForce Technology Co., Ltd. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ---------------------------------------------------------------------------- |
| 16 | + |
| 17 | +import dexsim |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +from dexsim.engine import RenderBody |
| 21 | + |
| 22 | + |
| 23 | +def set_projective_uv(self: RenderBody, proj_direct: np.ndarray | None = None) -> None: |
| 24 | + """Set projective uv mapping to render body. |
| 25 | +
|
| 26 | + Args: |
| 27 | + proj_direct: UV project direction. Default to be None, using svd. |
| 28 | + """ |
| 29 | + import open3d as o3d |
| 30 | + from dexsim.kit.meshproc import get_mesh_auto_uv |
| 31 | + |
| 32 | + n_mesh = self.get_mesh_count() |
| 33 | + if n_mesh <= 0: |
| 34 | + return |
| 35 | + n_vert_list = [] |
| 36 | + verts = np.empty((0, 3), dtype=np.float32) |
| 37 | + faces = np.empty((0, 3), dtype=np.int32) |
| 38 | + # gather all vertices |
| 39 | + for i in range(n_mesh): |
| 40 | + mesh_verts = self.get_vertices(mesh_id=i) |
| 41 | + n_vert_list.append(mesh_verts.shape[0]) |
| 42 | + verts = np.vstack((verts, mesh_verts)) |
| 43 | + |
| 44 | + mesh_faces = self.get_triangles(mesh_id=i) |
| 45 | + faces = np.vstack((faces, mesh_faces)) |
| 46 | + if (verts.shape[0] == 0) or (faces.shape[0] == 0): |
| 47 | + return |
| 48 | + # project uv for all vertices |
| 49 | + mesh_o3dt = o3d.t.geometry.TriangleMesh() |
| 50 | + mesh_o3dt.vertex.positions = o3d.core.Tensor(verts, dtype=o3d.core.Dtype.Float32) |
| 51 | + mesh_o3dt.triangle.indices = o3d.core.Tensor(faces, dtype=o3d.core.Dtype.Int32) |
| 52 | + is_success, vert_uvs = get_mesh_auto_uv(mesh_o3dt, proj_direct) |
| 53 | + |
| 54 | + # set uv mapping for each mesh |
| 55 | + start_idx = 0 |
| 56 | + for i in range(n_mesh): |
| 57 | + mesh_vert_uvs = vert_uvs[start_idx : start_idx + n_vert_list[i], :] |
| 58 | + self.set_uv_mapping(uvs=mesh_vert_uvs, mesh_id=i) |
| 59 | + start_idx += n_vert_list[i] |
| 60 | + |
| 61 | + |
| 62 | +def init_dynamic_pybind() -> None: |
| 63 | + """Initialize dynamic pybind interface.""" |
| 64 | + |
| 65 | + RenderBody.set_projective_uv = set_projective_uv |
0 commit comments