forked from NVlabs/curobo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeom_test.py
217 lines (192 loc) · 8.8 KB
/
geom_test.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
# property and proprietary rights in and to this material, related
# documentation and any modifications thereto. Any use, reproduction,
# disclosure or distribution of this material and related documentation
# without an express license agreement from NVIDIA CORPORATION or
# its affiliates is strictly prohibited.
#
# Third Party
import torch
# CuRobo
from curobo.geom.sdf.world import (
CollisionQueryBuffer,
WorldCollisionConfig,
WorldPrimitiveCollision,
)
from curobo.geom.sdf.world_mesh import WorldMeshCollision
from curobo.geom.types import Cuboid, WorldConfig
from curobo.types.base import TensorDeviceType
from curobo.types.math import Pose
from curobo.util_file import get_world_configs_path, join_path, load_yaml
def test_world_primitive():
# load a world:
tensor_args = TensorDeviceType()
world_file = "collision_test.yml"
data_dict = load_yaml(join_path(get_world_configs_path(), world_file))
world_cfg = WorldConfig.from_dict(data_dict)
coll_cfg = WorldCollisionConfig(world_model=world_cfg, tensor_args=TensorDeviceType())
coll_check = WorldPrimitiveCollision(coll_cfg)
x_sph = torch.as_tensor(
[[0.0, 0.0, 0.0, 0.1], [10.0, 0.0, 0.0, 0.0], [0.01, 0.01, 0.0, 0.1]],
**(tensor_args.as_torch_dict())
).view(1, 1, -1, 4)
# create buffers:
query_buffer = CollisionQueryBuffer.initialize_from_shape(
x_sph.shape, tensor_args, coll_check.collision_types
)
weight = tensor_args.to_device([1])
act_distance = tensor_args.to_device([0.0])
d_sph = coll_check.get_sphere_distance(x_sph, query_buffer, weight, act_distance).view(-1)
assert abs(d_sph[0].item() - 0.1) < 1e-3
assert abs(d_sph[1].item() - 0.0) < 1e-9
assert abs(d_sph[2].item() - 0.1) < 1e-3
def test_batch_world_primitive():
"""This tests collision checking across different environments"""
tensor_args = TensorDeviceType()
world_file = "collision_test.yml"
data_dict = load_yaml(join_path(get_world_configs_path(), world_file))
world_cfg = WorldConfig.from_dict(data_dict)
coll_cfg = WorldCollisionConfig(world_model=world_cfg, tensor_args=TensorDeviceType())
coll_check = WorldPrimitiveCollision(coll_cfg)
x_sph = torch.as_tensor(
[[0.0, 0.0, 0.0, 0.1], [10.0, 0.0, 0.0, 0.0], [0.01, 0.01, 0.0, 0.1]],
**(tensor_args.as_torch_dict())
).view(-1, 1, 1, 4)
env_query_idx = torch.zeros((x_sph.shape[0]), device=tensor_args.device, dtype=torch.int32)
# create buffers:
query_buffer = CollisionQueryBuffer.initialize_from_shape(
x_sph.shape, tensor_args, coll_check.collision_types
)
act_distance = tensor_args.to_device([0.0])
weight = tensor_args.to_device([1])
d_sph = coll_check.get_sphere_distance(
x_sph, query_buffer, weight, act_distance, env_query_idx
).view(-1)
assert abs(d_sph[0].item() - 0.1) < 1e-3
assert abs(d_sph[1].item() - 0.0) < 1e-9
assert abs(d_sph[2].item() - 0.1) < 1e-3
def test_swept_world_primitive():
"""This tests collision checking across different environments"""
tensor_args = TensorDeviceType()
world_file = "collision_test.yml"
data_dict = load_yaml(join_path(get_world_configs_path(), world_file))
world_cfg = WorldConfig.from_dict(data_dict)
coll_cfg = WorldCollisionConfig(world_model=world_cfg, tensor_args=TensorDeviceType())
coll_cfg.cache = {"obb": 5}
coll_check = WorldPrimitiveCollision(coll_cfg)
# add an obstacle:
new_cube = Cuboid("cube_1", [0, 0, 1, 1, 0, 0, 0], None, [0.1, 0.2, 0.2])
coll_check.add_obb(new_cube, 0)
x_sph = torch.as_tensor(
[[0.0, 0.0, 0.0, 0.1], [10.0, 0.0, 0.0, 0.0], [0.01, 0.01, 0.0, 0.1]],
**(tensor_args.as_torch_dict())
).view(1, 1, -1, 4)
env_query_idx = None
env_query_idx = torch.zeros((x_sph.shape[0]), device=tensor_args.device, dtype=torch.int32)
# create buffers:
query_buffer = CollisionQueryBuffer.initialize_from_shape(
x_sph.shape, tensor_args, coll_check.collision_types
)
weight = tensor_args.to_device([1])
act_distance = tensor_args.to_device([0.0])
dt = act_distance.clone()
d_sph_swept = coll_check.get_swept_sphere_distance(
x_sph, query_buffer, weight, act_distance, dt, 10, env_query_idx
).view(-1)
d_sph = coll_check.get_sphere_distance(
x_sph, query_buffer.clone() * 0.0, weight, act_distance, env_query_idx
).view(-1)
assert abs(d_sph[0].item() - 0.1) < 1e-3
assert abs(d_sph[1].item() - 0.0) < 1e-9
assert abs(d_sph[2].item() - 0.1) < 1e-3
assert abs(d_sph_swept[0].item() - 0.1) < 1e-3
assert abs(d_sph_swept[1].item() - 0.0) < 1e-9
assert abs(d_sph_swept[2].item() - 0.1) < 1e-3
def test_world_primitive_mesh_instance():
# load a world:
tensor_args = TensorDeviceType()
world_file = "collision_test.yml"
data_dict = load_yaml(join_path(get_world_configs_path(), world_file))
world_cfg = WorldConfig.from_dict(data_dict)
coll_cfg = WorldCollisionConfig(world_model=world_cfg, tensor_args=TensorDeviceType())
coll_check = WorldMeshCollision(coll_cfg)
x_sph = torch.as_tensor(
[[0.0, 0.0, 0.0, 0.1], [10.0, 0.0, 0.0, 0.0], [0.01, 0.01, 0.0, 0.1]],
**(tensor_args.as_torch_dict())
).view(1, 1, -1, 4)
# create buffers:
query_buffer = CollisionQueryBuffer.initialize_from_shape(
x_sph.shape, tensor_args, coll_check.collision_types
)
act_distance = tensor_args.to_device([0.0])
weight = tensor_args.to_device([1])
d_sph = coll_check.get_sphere_distance(x_sph, query_buffer, weight, act_distance).view(-1)
assert abs(d_sph[0].item() - 0.1) < 1e-3
assert abs(d_sph[1].item() - 0.0) < 1e-9
assert abs(d_sph[2].item() - 0.1) < 1e-3
def test_batch_world_primitive_mesh_instance():
"""This tests collision checking across different environments"""
tensor_args = TensorDeviceType()
world_file = "collision_test.yml"
data_dict = load_yaml(join_path(get_world_configs_path(), world_file))
world_cfg = WorldConfig.from_dict(data_dict)
coll_cfg = WorldCollisionConfig(world_model=world_cfg, tensor_args=TensorDeviceType())
coll_check = WorldMeshCollision(coll_cfg)
x_sph = torch.as_tensor(
[[0.0, 0.0, 0.0, 0.1], [10.0, 0.0, 0.0, 0.0], [0.01, 0.01, 0.0, 0.1]],
**(tensor_args.as_torch_dict())
).view(-1, 1, 1, 4)
env_query_idx = torch.zeros((x_sph.shape[0]), device=tensor_args.device, dtype=torch.int32)
# create buffers:
query_buffer = CollisionQueryBuffer.initialize_from_shape(
x_sph.shape, tensor_args, coll_check.collision_types
)
weight = tensor_args.to_device([1])
act_distance = tensor_args.to_device([0.0])
d_sph = coll_check.get_sphere_distance(
x_sph, query_buffer, weight, act_distance, env_query_idx
).view(-1)
assert abs(d_sph[0].item() - 0.1) < 1e-3
assert abs(d_sph[1].item() - 0.0) < 1e-9
assert abs(d_sph[2].item() - 0.1) < 1e-3
def test_swept_world_primitive_mesh_instance():
"""This tests collision checking across different environments"""
tensor_args = TensorDeviceType()
world_file = "collision_test.yml"
data_dict = load_yaml(join_path(get_world_configs_path(), world_file))
world_cfg = WorldConfig.from_dict(data_dict)
coll_cfg = WorldCollisionConfig(world_model=world_cfg, tensor_args=TensorDeviceType())
coll_cfg.cache = {"obb": 5}
coll_check = WorldMeshCollision(coll_cfg)
# add an obstacle:
dims = tensor_args.to_device([0.1, 0.2, 0.2])
w_obj_pose = Pose.from_list([0, 0, 1, 1, 0, 0, 0], tensor_args)
coll_check.add_obb_from_raw("cube_1", dims, 0, w_obj_pose=w_obj_pose)
x_sph = torch.as_tensor(
[[0.0, 0.0, 0.0, 0.1], [10.0, 0.0, 0.0, 0.0], [0.01, 0.01, 0.0, 0.1]],
**(tensor_args.as_torch_dict())
).view(1, 1, -1, 4)
env_query_idx = None
env_query_idx = torch.zeros((x_sph.shape[0]), device=tensor_args.device, dtype=torch.int32)
# create buffers:
query_buffer = CollisionQueryBuffer.initialize_from_shape(
x_sph.shape, tensor_args, coll_check.collision_types
)
weight = tensor_args.to_device([1])
act_distance = tensor_args.to_device([0.0])
dt = act_distance.clone() + 0.01
d_sph_swept = coll_check.get_swept_sphere_distance(
x_sph, query_buffer, weight, act_distance, dt, 10, env_query_idx
).view(-1)
d_sph = coll_check.get_sphere_distance(
x_sph, query_buffer.clone() * 0.0, weight, act_distance, env_query_idx
).view(-1)
assert abs(d_sph[0].item() - 0.1) < 1e-3
assert abs(d_sph[1].item() - 0.0) < 1e-9
assert abs(d_sph[2].item() - 0.1) < 1e-3
assert abs(d_sph_swept[0].item() - 0.1) < 1e-3
assert abs(d_sph_swept[1].item() - 0.0) < 1e-9
assert abs(d_sph_swept[2].item() - 0.1) < 1e-3