File: triangle_mesh_deformation.py

package info (click to toggle)
open3d 0.16.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: cpp: 193,088; python: 24,973; ansic: 8,356; javascript: 1,869; sh: 1,473; makefile: 236; xml: 69
file content (107 lines) | stat: -rw-r--r-- 4,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# ----------------------------------------------------------------------------
# -                        Open3D: www.open3d.org                            -
# ----------------------------------------------------------------------------
# The MIT License (MIT)
#
# Copyright (c) 2018-2021 www.open3d.org
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# ----------------------------------------------------------------------------

import numpy as np
import open3d as o3d
import time
import os
import sys

pyexample_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(pyexample_path)

import open3d_example as o3dex


def problem0():
    mesh = o3dex.get_plane_mesh(height=1, width=1)
    mesh = mesh.subdivide_midpoint(3)
    vertices = np.asarray(mesh.vertices)
    static_ids = [
        1, 46, 47, 48, 16, 51, 49, 50, 6, 31, 33, 32, 11, 26, 27, 25, 0, 64, 65,
        20, 66, 68, 67, 7, 69, 71, 70, 22, 72, 74, 73, 3, 15, 44, 43, 45, 5, 41,
        40, 42, 13, 39, 37, 38, 2, 56, 55, 19, 61, 60, 59, 8, 76, 75, 77, 23
    ]
    static_positions = []
    for id in static_ids:
        static_positions.append(vertices[id])
    handle_ids = [4]
    handle_positions = [vertices[4] + np.array((0, 0, 0.4))]

    return mesh, static_ids + handle_ids, static_positions + handle_positions


def problem1():
    mesh = o3dex.get_plane_mesh(height=1, width=1)
    mesh = mesh.subdivide_midpoint(3)
    vertices = np.asarray(mesh.vertices)
    static_ids = [
        1, 46, 15, 43, 5, 40, 13, 38, 2, 56, 37, 39, 42, 41, 45, 44, 48, 47
    ]
    static_positions = []
    for id in static_ids:
        static_positions.append(vertices[id])
    handle_ids = [21]
    handle_positions = [vertices[21] + np.array((0, 0, 0.4))]

    return mesh, static_ids + handle_ids, static_positions + handle_positions


def problem2():
    armadillo_data = o3d.data.ArmadilloMesh()
    mesh = o3d.io.read_triangle_mesh(armadillo_data.path)
    vertices = np.asarray(mesh.vertices)
    static_ids = [idx for idx in np.where(vertices[:, 1] < -30)[0]]
    static_positions = []
    for id in static_ids:
        static_positions.append(vertices[id])
    handle_ids = [2490]
    handle_positions = [vertices[2490] + np.array((-40, -40, -40))]

    return mesh, static_ids + handle_ids, static_positions + handle_positions


if __name__ == "__main__":
    o3d.utility.set_verbosity_level(o3d.utility.Debug)

    for mesh, constraint_ids, constraint_pos in [
            problem0(), problem1(), problem2()
    ]:
        constraint_ids = np.array(constraint_ids, dtype=np.int32)
        constraint_pos = o3d.utility.Vector3dVector(constraint_pos)
        tic = time.time()
        mesh_prime = mesh.deform_as_rigid_as_possible(
            o3d.utility.IntVector(constraint_ids), constraint_pos, max_iter=50)
        print("deform took {}[s]".format(time.time() - tic))
        mesh_prime.compute_vertex_normals()

        mesh.paint_uniform_color((1, 0, 0))
        handles = o3d.geometry.PointCloud()
        handles.points = constraint_pos
        handles.paint_uniform_color((0, 1, 0))
        o3d.visualization.draw_geometries([mesh, mesh_prime, handles])

    o3d.utility.set_verbosity_level(o3d.utility.Info)